jQuery(document).ready(function(){
	jQuery("#ConverterForm #FromCurrency").change(function() {
		convert();
	});
	
	jQuery("#ConverterForm #ToCurrency").change(function() {
		convert();
	});
	
	jQuery("#ConverterForm #FromAmount").keyup(function() {
		convert();
	});
	
	function convert() {
		fromAmountField = jQuery("#ConverterForm #FromAmount");
		if ( isNaN(parseFloat(fromAmountField.val())) ) {
			jQuery("#ConverterForm #ToAmount").html("Invalid input amount");
			jQuery("#ConverterForm #FromAmount").focus();
			return;
		}
		
		fromCurrency = jQuery("#ConverterForm #FromCurrency").val();
		toCurrency = jQuery("#ConverterForm #ToCurrency").val();
		fromAmount = parseFloat(fromAmountField.val());
		
		conversion = convertRates(fromCurrency, toCurrency);
		jQuery("#ConverterForm #ToAmount").html( fromAmount * conversion );
		
		rate = '1 ' + fromCurrency + ' = ' + convertRates(fromCurrency, toCurrency) + " " + toCurrency;
		inverseRate = '1 ' + toCurrency + ' = ' + convertRates(toCurrency, fromCurrency) + " " + fromCurrency;
		jQuery('#Rate').html( rate + '<span>' + inverseRate + '</span>');
	}
});

function convertRates(from, to) {
	// console.log(" from " + from);
	fromToBaseRate = 1 / jQuery("input[@name=" + from + "]").val();
	toToBaseRate = jQuery("input[@name=" + to + "]").val();

	return (fromToBaseRate * toToBaseRate).toFixed(5);
}

//jQuery('#CurrencyRatesForm input[@type=hidden]')[1]