// JavaScript Document
function calcRepayment() {
	if (!document.opcalc) return false;
	document.opcalc.loanamount.value	= ForceNumeric(document.opcalc.loanamount.value);
	document.opcalc.loanperiod.value	= ForceNumeric(document.opcalc.loanperiod.value);
	document.opcalc.interestrate.value	= ForceNumeric(document.opcalc.interestrate.value);
	document.opcalc.overpayment.value	= ForceNumeric(document.opcalc.overpayment.value);
	var loanamount		= document.opcalc.loanamount.value;
	var interestrate	= document.opcalc.interestrate.value;
	var loanperiod		= document.opcalc.loanperiod.value;
	var over			= document.opcalc.overpayment.value;
	if (interestrate > 0 && loanperiod > 0) {
		if (parseFloat(over) < parseFloat(loanamount)) {
				var I = interestrate / 12;
				var X = 1/(1+I/100);
				var N = loanperiod * 12;
				var L = loanamount;
				var P1 = 0;
				var P2 = loanamount;
				var A1 = (L - P1 * Math.pow(X,N)) * (X - 1)/(Math.pow(X,N+1)-X);
        		var m = (A1+ +over);
  				var Q = ((A1 + +over)) - (L * (I/100));
  				var A2 = (Math.log(m/Q))/(Math.log(1 + (I/100)))/12;
				document.opcalc.repayment.value			= FormatNumber(A1,2);
				document.opcalc.totalpayment.value		= FormatNumber((A1 * 12) * loanperiod,2) ;
				document.opcalc.term.value				= FormatNumber(loanperiod,1);
				document.opcalc.newrepayment.value		= FormatNumber(m);
				document.opcalc.newtotalpayment.value 	= FormatNumber((m * 12) * A2,2);
				document.opcalc.newterm.value			= FormatNumber(A2,1);
		
				if (over = 0) {
					document.opcalc.totalsaving.value	= FormatNumber(0,0);
					document.opcalc.years.value			= FormatNumber(document.opcalc.term.value,1);
				}
				else
				{
					document.opcalc.totalsaving.value	= FormatNumber(document.opcalc.totalpayment.value - document.opcalc.newtotalpayment.value,0);
					document.opcalc.years.value			= FormatNumber(document.opcalc.term.value - document.opcalc.newterm.value,1);	
					document.opcalc.newterm.value		= document.opcalc.newterm.value + "yrs";
					document.opcalc.years.value			= document.opcalc.years.value + "yrs";
				}
		}
		else
		{
			document.opcalc.overpayment.value		= "0";
			document.opcalc.newrepayment.value		= "0";
			document.opcalc.newtotalpayment.value	= "0";
			document.opcalc.newterm.value			= "0";
			document.opcalc.repayment.value			= "0";
			document.opcalc.totalpayment.value		= "0";
			document.opcalc.term.value				= "0";
			document.opcalc.totalsaving.value		= "0";
			document.opcalc.years.value				= "0";
		}
	}
	else
	{
		document.opcalc.repayment.value		= "*ERROR*";
		document.opcalc.interestonly.value	= "*ERROR*";
	}
}

function calcTotal( ) {
	if (!document.mortform) return false;
	document.mortform["mortamount"].value	= ForceNumeric( document.mortform["mortamount"].value );
	document.mortform["intrate"].value		= ForceNumeric( document.mortform["intrate"].value );
	document.mortform["mortterm"].value		= ForceNumeric( document.mortform["mortterm"].value );
	var p = document.mortform["mortamount"].value;
	var	r = document.mortform["intrate"].value;
	var n = document.mortform["mortterm"].value;
	var i = .0;

	if ( document.mortform.repaymethod[0].checked )	{
		i =  p * Math.pow( (1 + (r / 100)),n) * (r / 100) * ( 1 / ( Math.pow( 1 + (r / 100), n) -1) ) * ( 1 / 12);
	} 
	else	
	{
		i = r * p / 1200;
	}

   	document.mortform["repayment"].value = trimWhiteSpace(atocurr( i ));
	//document.mortform["repayment"].value = "£" + document.mortform["repayment"].value;
}