/**
 * Меняем курс валют ajax`ом
 * @author Nick
 */

/**
 * Устанавливаем обработчики события.
 * Изменения курса валют за разные периоды.
 */
$(document).ready(function () {
    $('#changeDate').click(changeDate);
    setDate();
});

/**
 * Устанавливаем текущую дату
 */
function setDate()
{
    var date = new Date();
    var day = date.getDate();
    var month = date.getMonth()+1;
    if (month <= 10) {
        month = '0' + month;
    }
    var year = date.getFullYear();
    var xChangeDate = day + '.' + month + '.' + year;
    
    $('#xchangedate').attr('value', xChangeDate);
}

function changeDate()
{
    $('#load_x').html('<img src="/ajax-loader.gif" alt="load" />').show();
    $.ajax({
		url: '/xchange.php',
		type: 'get',
		dataType: 'text',
		data: {
			change_date: $('#xchangedate').val()
		},
		success: onSuccess,
        error: onError
	});
}

function onSuccess(result)
{
    if (result == 'false') {
        var errorMessage = $('#noData').val();
        alert(errorMessage);
    } else {
    
        response = result.split(';');
        
        $('#uah_x').text(response[1]); 
        $('#rub_x').text(response[2]);
        $('#eur_x').text(response[3]);
          
    }
    $('#load_x').fadeOut('slow');
}


function onError(request,error) {
    alert('Error');
}