var tableAPR = new Array;
var tableROI = new Array;
tableAPR[0] = "10.9";	tableROI[0] = 0.00869;
tableAPR[1] = "9.9";	tableROI[1] = 0.00789;
tableAPR[2] = "8.9";	tableROI[2] = 0.0071;
tableAPR[3] = "7.3";	tableROI[3] = 0.00582;


function getAPRIndex( nPrinciple )
	{
	if( nPrinciple <= 9999 ) return 0;
	if( nPrinciple >= 10000 && nPrinciple <= 14999 ) return 1;
	if( nPrinciple >= 15000 && nPrinciple <= 24999 ) return 2;
	if( nPrinciple >= 25000 ) return 3;
	return 4;
	}
function getMonthlyPayment( nPrinciple, nTerm, nRate )
	{
	var nResult = 0;
	nResult = 1 / (1 + nRate );
	nResult = Math.pow(nResult, nTerm);
	nResult = 1 - nResult;
	nResult = nResult / nRate;
	nResult = nPrinciple / nResult;
	nResult += 0.005;
	return nResult;
	}
function getMaxLoan( nPayment, nTerm )
	{
	var nLoanArray = new Array(0,0,0,0);
	var result = 0;
	for( var i=0; i<4; i++ )
		{
		nLoanArray[i] = 1 / (1+tableROI[i]);
		nLoanArray[i] = Math.pow( nLoanArray[i], nTerm);
		nLoanArray[i] = 1 - nLoanArray[i];
		nLoanArray[i] = nLoanArray[i] / tableROI[i];
		nLoanArray[i] = nPayment * nLoanArray[i];
		nLoanArray[i] += 0.005;
		}
		if( nLoanArray[0]<=14999 ){ result = nLoanArray[0]; }
		if( nLoanArray[1]>=15000 && nLoanArray[1]<=24999 ){ result = nLoanArray[1]; }
		if( nLoanArray[2]>=25000 && nLoanArray[2]<=34999 ){ result = nLoanArray[2]; }
		if( nLoanArray[3]>=35000 ){ result = nLoanArray[3]; }
	return result;
	}
	
function toTwoDecimal( nVal )
	{	
	var sResult = "";
	var nPointPos = 0;
	
	sResult = nVal.toString();
	nPointPos = sResult.indexOf(".");
	if( nPointPos == -1 )
		{
		sResult += ".00";
		nPointPos = sResult.indexOf(".");
		}
	if( sResult.charAt(nPointPos + 2)=="" ) sResult += "0";
	sResult = sResult.slice(0, nPointPos+3 );
	return sResult;
	}
function doCalcs(frm)
	{
	var nPrinciple = frm.ed_Principle.value;
	var nMonths = frm.ed_Term.value;
	var nMonthlyPayment = 0;
	var sMonthlyPayment = "0.00";
	var nTotalPayment = 0;
	var sTotalPayment = "0.00";
	if( nPrinciple == "" ){
		alert("Please enter the value you wish to borrow")
		return false;
		}
	if( isNaN( nPrinciple )){
		alert("Please enter a value to borrow, enter a whole number from £5,000 to £75,000.\nDo not enter the preceding £ symbol");
		return false;
		}
	if( (nPrinciple < 5000) || (nPrinciple > 600000) ) {
		alert("Please enter an amount between £5,000 and £600,000");
		return false;
		}
	if( nMonths == "" ) {
		alert("Please enter the number of months you wish to borrow over");
		return false;
		}
	if( isNaN(nMonths) || (nMonths<36) || (nMonths>300) ){
		alert("Please enter a term of between 36 and 300 months");
		return false;
		}
	nLookupTable = getAPRIndex( nPrinciple );
	nPrinciple *= 1.15;
	nMonthlyPayment = getMonthlyPayment( nPrinciple, nMonths, tableROI[nLookupTable]);
	sMonthlyPayment = toTwoDecimal(nMonthlyPayment);
	nTotalPayment	= Math.round(sMonthlyPayment * nMonths * 100)/100;
	sTotalPayment	= toTwoDecimal(nTotalPayment);
	frm.ed_MonthlyPayment.value = "£ " + sMonthlyPayment;
	frm.ed_APR.value = tableAPR[nLookupTable] + "%";
	frm.ed_TotPay.value = "£ " + sTotalPayment;
	return false;
	}

document.write('<FORM id="calcForm" name="calcForm" onsubmit="return doCalcs(this);">');
document.write('<table border="0" align="center" cellpadding="3" cellspacing="0">');
document.write('<tr>');
document.write('<td>Loan Amount:</td>');
document.write('<td>');
document.write('<input class="norm" id="ed_Principle" name="ed_Principle"></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td>Loan term in months:</td>');
document.write('<td><input class="norm" id="ed_Term" name="ed_Term"></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td colspan="2"><div align="center">');
document.write('<input class="button" type="submit" value="Calculate your monthly payment" name="submit">');
document.write('</div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td>Monthly Payment:</td>');
document.write('<td><input class="loan" id="ed_MonthlyPayment" name="ed_MonthlyPayment"></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td>APR:</td>');
document.write('<td><input class="apr" id="ed_APR" name="ed_APR"></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td>Total Payable:</td>');
document.write('<td><input class="loan" id="ed_TotPay" name="ed_TotPay"></td>');
document.write('</tr>');
document.write('</table>');
document.write('</FORM>');