<!-- //

function clicked_interestonly(form)
{
	if (form.interestonly.checked == true) 
	{
		form.term.disabled = true;
	} else
	{
		form.term.disabled = false;
	}
}

function calculate(form) 
{
	var principal = parseInt(form.principal.value.replace (/[^0-9\.]/g, ''));
	$('principal').value = !isNaN(principal) ? formatCurrency(principal) : 0;
	var interest = parseFloat(form.interest.value.replace (/[^0-9\.]/g, '')) / 100;
	$('interest').value = !isNaN(interest) ?  Math.round(interest * 100000)/1000 : 0;
	var term = parseInt(form.term.value.replace (/[^0-9\.]/g, ''));
	
	if (!principal || !interest || !term){
		if (!principal ){
			var error = "Mortgage Amount";
			$('principal').select();
		}
		if (!term ){
			var error = "Mortgage term";
			$('term').focus();
		}
		if (!interest || interest.isNan){
			var error = error ? error + " & " + "Interest Rate" : "Interest Rate";
			$('interest').select();
		}
		alert ("please complete the " + error);
		return false;
	}
	
	if (form.interestonly.checked == true) 
	{
		var annual = principal * interest;
		var monthly = annual / 12;
		var total = "-"
	} else
	{
		var annual = principal * ( ( (1- (1+interest)) / (1- Math.pow((1+interest),term)) )+interest);
		var monthly = annual / 12;
		var total = annual * term;
		total = Math.round(total);
	}
	
	monthly = Math.round(monthly);
	annual= Math.round(annual);

	
	form.monthly.value = formatCurrency(monthly) ;
	form.annual.value = formatCurrency(annual);
	form.total.value = !isNaN(total) ? formatCurrency(total) : total;
	
	return true;
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '£' + num + '.' + cents);
}

window.addEvent ('domready', function(){
	$('principal').focus();
});

//-->