/*****************************************
 *   Localization
 *****************************************/
 var errors = new Array();
 errors[1] = 'Wpisz kwote!';
 errors[2] = 'Zly rodzaj kwoty!';
 errors[3] = 'Currencies shold not be equal!';
 errors[4] = 'Wpisz dzien!';
 errors[5] = 'Wpisz miesiac!';
 errors[6] = 'Wpisz rok!';





/**
* Get Google Chart's URL (by AJAX)
*
* @param    string  period number (0-n)
*/

function get_graf(period)
{
	var currency = 'PLN';
	var curr_curency=document.getElementById('current_currency');
	var curr_period=document.getElementById('current_period');
	curr_period.value = period;

	currency = curr_curency.value;
	if(currency == '')
		currency='PLN';
 
	get_graf_currency_period(currency,period);
}

function get_graf_currency(currency)
{
	var period = '0';
	var curr_period=document.getElementById('current_period');
	var curr_curency=document.getElementById('current_currency');
	curr_curency.value = currency;

	period = curr_period.value;

	if(period == '')
		period='0';
	get_graf_currency_period(currency,period);
}

function get_graf_currency_period(currency, period)
{
//	alert(period); 
//	alert(currency);
 
//  ID of custom period:
    var custom_period_id = 4;
    
//  If period - custom, show result block (if hidden)
    var gfc_block = $('.graf_filters_custom');
    if (period == custom_period_id) {
        if (gfc_block.css('display') == 'none') {
            gfc_block.css('display', 'block');
        }
    }
    
//  Else if other periods:
    else {
    //  hide result block:
        gfc_block.css('display', 'none');
   }
    
// Highlight of "LI":
    $('li.active').attr('class', '');
    var a = $('#period_' + period);
    a.attr('class', 'active');
    

// Highlight of "LI":
    $('li.active_curr').attr('class', '');
    var a = $('#currency_' + currency);
    a.attr('class', 'active_curr');
    


//  Load chart URL in target:
    var target = $('.graf');
    $.post('hystory.ajax.php', {period: period, currency: currency}, function (data) {
        target.children('img').attr('src', data);
    });
}

 



/*
* Swap currencies
*/
function swap_selects(sa, sb) {
    
    var sa = $('#' + sa);
    var sa_selected = sa.find('option:selected').attr('value');
    var sb = $('#' + sb);
    var sb_selected = sb.find('option:selected').attr('value');
    
    sa.find("option[value='" + sb_selected + "']").attr('selected', 'selcted');
    sb.children("option[value='" + sa_selected + "']").attr('selected', 'selcted');
}


/**
* Convertation
*/
function converter(form) {
    
    var count = form.count.value;
    var c_a = form.currency.value;
    var c_b = form.currency_base.value;
    var day = form.day.value;
    var month = form.month.value;
    var year = form.year.value;
    
//  validation:
    if (count == '') {
        show_error(errors[1]);
        form.count.select();
        return false;
    }
    
    //alert(count + ': ' + count.search(/\D/i));
    
    if (count.search(/\D/i) != -1) {
        show_error(errors[2]);
        form.count.select();
        return false;
    }
    
    if (c_a == c_b) {
        show_error(errors[3]);
        form.currency.focus();
        return false;
    }
    
    if (day == 0) {
        show_error(errors[4]);
        form.day.focus();
        return false;
    }
    
    if (month == 0) {
        show_error(errors[5]);
        form.month.focus();
        return false;
    }
    
    if (year == 0) {
        show_error(errors[6]);
        form.year.focus();
        return false;
    }
    
//  calculation:
    var target = $('.calc_result');
    $.post('converter.ajax.php', {count: count, c_a: c_a, c_b: c_b, day: day, month: month, year: year}, function (data) {
        target.css('display', 'block').html(data);
    });
    
    return false;
}



/*
 * Default calc settings
 */
 function init_calc() {
//  Default day (today):
    var d = new Date();
    var day = d.getDay();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    
    $('.calc_form select[name=day] option[value=' + day + ']').attr('selected', 'selected');
    $('.calc_form select[name=month] option[value=' + month + ']').attr('selected', 'selected');
    $('.calc_form select[name=year] option[value=' + year + ']').attr('selected', 'selected');
    
//  Default currencies:
    $('#currency option[value=GBP]').attr('selected', 'selected');
    $('#currency_base option[value=PLN]').attr('selected', 'selected');  
}
 
 
 /*
  * Show errors
  */
 function show_error(message) {
     $('.calc_result').css('display', 'block').html('<strong>ERROR:</strong> <br />' + message);
 }




