$(function () {
    var xml;
    $.ajax({
        type: "GET",
        url: "assets/files/ratesceltictel.xml",
        dataType: "xml",
        success: function (xml) {
            var a = 0;
            $(xml).find('rates').each(function () {
                var country = $(this).find('country').text();
                var to = $(this).find('to').text();
                var from = $(this).find('from').text();

                if (!isNaN(to) && to != '') $('#to').append('<option value="' + a + '">' + country + '</country>');
                if (!isNaN(from) && from != '') $('#from').append('<option value="' + a + '">' + country + '</country>');
                a++;
            });
            $('#from, #to').change(function () {

                var from = $('#from').val();
                var to = $('#to').val();

                if (from != 'none' && to != 'none') {
                    from = $(xml).find('rates').eq(from);
                    to = $(xml).find('rates').eq(to);

                    from = parseFloat(from.find('from').text());
                    to = parseFloat(to.find('to').text());

                    var rate = (from + to);
                    var tendollars = Math.round(20 / (from + to));
                    $('#rate').html('<strong>&euro;' + rate.toFixed(3) + '</strong> per minute');
                    $('#tendollars').html('<strong>'+tendollars +'</strong> minutes of talk time for just <strong>&euro;20</strong>');

                } else {
                    $('#rate').html('');
                    $('#tendollars').html('');

                }

            });
        }
    });
});
