/* 
	Lookup tables for APR / Monthly % rates 
*/
var tableAPR = new Array;
var tableROI = new Array;
tableAPR[0] = "12.4";	tableROI[0] = 0.00982;
tableAPR[1] = "10.4";	tableROI[1] = 0.00831;
tableAPR[2] = "9.4";	tableROI[2] = 0.00755;
tableAPR[3] = "7.8";	tableROI[3] = 0.00631;
tableAPR[4] = "6.9";	tableROI[4] = 0.00561;
tableAPR[5] = "5.9";	tableROI[5] = 0.00482;

/*
	getAPRIndex( nPrinciple )
	returns the index of the APR/ROI table to use
	dependent upon the principle amount being borrowed
*/
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 && nPrinciple <= 49999 ) return 3;
	if( nPrinciple >= 50000 && nPrinciple <= 74999 ) return 4;
	if( nPrinciple >= 75000 ) return 5;
	return 6;
	}

	
/*
	getMonthlyPayment( nPrinciple, nTerm, nRate )
	Returns the monthly payment necessary to pay off the principle,
	over term, using interest rate nRate
*/
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;

	// Add half a penny to ensure it rounds UP, not DOWN
	nResult += 0.005;
	return nResult;
	}

/*
	getMaxLoan( nPayment, nTerm )
	Works out the max borrowing available when paying nPayment
	per month over nTerm months. This is worked out against each
	individual interest rate and then the loan that falls into the
	appropriate banding is chosen
*/
function getMaxLoan( nPayment, nTerm )
	{
	var nLoanArray = new Array(0,0,0,0,0,0);
	var result = 0;
	// Populate each element with maximum borrowing	
	for( var i=0; i<6; 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;
		}

	// Now find the appropriate loan from the various APR's
	
		if( nLoanArray[0]<=9999 ){ result = nLoanArray[0]; }
		if( nLoanArray[1]>=10000 && nLoanArray[1]<=14999 ){ result = nLoanArray[1]; }
		if( nLoanArray[2]>=15000 && nLoanArray[2]<=24999 ){ result = nLoanArray[2]; }
		if( nLoanArray[3]>=25000 && nLoanArray[3]<=49999 ){ result = nLoanArray[3]; }
		if( nLoanArray[4]>=50000 && nLoanArray[4]<=74999 ){ result = nLoanArray[4]; }
		if( nLoanArray[5]>=75000 ){ result = nLoanArray[5]; }
	

	return result;
	}
	
/*
	toTwoDecimal( nVal )
	Converts the numeric value nVal to a string, trimmed/extended
	to two decimal places
*/
function toTwoDecimal( nVal )
	{	
	var sResult = "";
	var nPointPos = 0;
	
	sResult = nVal.toString();
	// Find decimal
	nPointPos = sResult.indexOf(".");
	// No decimal, add them
	if( nPointPos == -1 )
		{
		sResult += ".00";
		nPointPos = sResult.indexOf(".");
		}

	// Extend to two decimal places if not already
	if( sResult.charAt(nPointPos + 2)=="" ) sResult += "0";
	// Trim to two decimal places
	sResult = sResult.slice(0, nPointPos+3 );
	return sResult;
	}
