function fetch_rate(type){
	$.getJSON('https://secure.onlinefx.com/action/fetch_rate?callback=?&currency=' + $('#currency').val() + '&type=' + type, function(data){ 
		$('#rate').val(data.rate)
		$('#sku').html(data.sku)
		change_cost()
	})
}

function change_cost(){
	var rate = $('#rate').val()
	var amount = parseFloat($('#cost').val())*parseFloat(rate)
	
	if(isNaN(parseInt(amount))){
        amount = 0
    }
	
	$('#amount').val(Math.round(amount*100)/100);
}

function change_amount(){
	var rate = $('#rate').val()
	var cost = parseFloat($('#amount').val())/parseFloat(rate)
	
	if(isNaN(parseInt(cost))){
        cost = 0
    }
	
	$('#cost').val(Math.round(cost*100)/100);
}

function fetch_rate_slide(){
	$.getJSON('https://secure.onlinefx.com/action/fetch_rate?callback=?&currency=' + $('#currency_slide').val(), function(data){ 
	    $('#rate_slide').val(data.rate)
		$('#sku_slide').html(data.sku)
		change_cost_slide()
	})

}

function change_cost_slide(){
	var rate = $('#rate_slide').val()
	var amount = parseFloat($('#cost_slide').val())*parseFloat(rate)
	
	if(isNaN(parseInt(amount))){
        amount = 0
    }
	
	$('#amount_slide').val(Math.round(amount*100)/100);
}

function find_addresses(){
	$.getJSON('https://secure.onlinefx.com/action/find_addresses?f=select_address()&postcode=' + $('#postcode').val() + '&c=?', function(data){
		$('#found_addresses').html(data.html)
	})
}

function select_address(){
	$.getJSON('https://secure.onlinefx.com/action/fetch_address?index=' + $('#addresses').attr("selectedIndex") + '&c=?', function(data){
		$('#address_1').val(data.address_1)
		$('#address_2').val(data.address_2)
		$('#city').val(data.city)
		$('#county').val(data.county)
		$('#postcode_result').val(data.postcode)
	})
}


function recipient_find_addresses(){
	$.getJSON('http://secure.onlinefx.com/action/find_addresses?f=recipient_select_address()&postcode=' + $('#recipient_postcode').val() + '&c=?', function(data){
		$('#recipient_found_addresses').html(data.html)
	})
}

function recipient_select_address(){
	$.getJSON('http://secure.onlinefx.com/action/fetch_address?index=' + $('#addresses').attr("selectedIndex") + '&c=?', function(data){
		$('#recipient_address_1').val(data.address_1)
		$('#recipient_address_2').val(data.address_2)
		$('#recipient_city').val(data.city)
		$('#recipient_county').val(data.county)
		$('#recipient_postcode_result').val(data.postcode)
	})
}

$(document).ready(function () {
    fetch_rate()
	fetch_rate_slide()
});



