runOnLoad(function(){
	updateField('rateCal');
	$('#call_type').select().focus();
	$('#call_type').bind("change blur",function() {
		updateField('rateCal');
	});
	$('#country_src').bind("change blur",function() {
		updateField('rateCal');
	});
	$('#country_dest').bind("change blur",function() {
		updateField('rateCal');
	});
});
function updateField(fieldid) {
	var calltype=document.getElementById('call_type').value.valueOf();
	var country_src=document.getElementById('country_src').value.valueOf();
	var country_dest=document.getElementById('country_dest').value.valueOf();
	if (country_dest>=0) {
		document.getElementById(fieldid).value=(calltype=="-1")?(parseFloat(country_src)+parseFloat(country_dest)):country_dest;
	}
	//alert('src='+document.getElementById('country_src').value+'dest='+document.getElementById('country_dest').value+'rateCal='+document.getElementById('rateCal').value);
}
$(function() {
	$("#send").click(function() {
		// validate and process form
		// first hide any error messages
		$('.error').hide();

		var username = $("#username").val();
		var email = $("#email").val();
		var mobilenum = $("#mobilenum").val();
		var remote_ip = $("#remote_ip").val();
		if(isNaN(mobilenum) ) {
			$("span#error").show();
			$("#phone").focus(function(){
				$(this).css({backgroundColor:"#FFDDAA"});
			});
			return false;
		}
		else if(!email) {
			$("span#error_email").show();
			$("#email").focus(function(){
				$(this).css({backgroundColor:"#FFDDAA"});
			});
			return false;
		}
		$('form').hide();
		$('#loadFunc').hide();

		$('#message').html("<h1 class='underline'><img src='images/loading.gif' ></img>&nbsp;Submitting Request...</h1>");
		var confirmBox=confirm("Your Email address: " +email+"\nConfirm to email download URL to this address?");
		if (confirmBox==false) {
			$('form').show();
			$('#message').html("<p class='cancelSub'>* Your Submission is cancelled.</p>");
			return false;
		}
		else {
			var dataString = 'username=' + username + '&email=' + email + '&mobilenum=' + mobilenum+ '&remote_ip=' + remote_ip;
			//alert(dataString);
			makePOSTRequest(dataString);
			$('#myspan').fadeIn(5000, function() {
				$('#message').html("<h1>Request Submitted!</h1><h2 class='underline'>You will receive a download URL<br />via email shortly.</h2>");
				$('#loadFunc').html('<img src="images/check.png" ></img>');
				$('#loadFunc').show("slow");
			});
			return false;
		}
	});
});

