<!--

/*	 loan_calculator.js

	 ---

	 Loan calculator script---calculates monthly repayments, total repayment amount

   and displays the interest rate.

   	 Author: JB Gray

	 Based on the original script by Euan Fergusson.

	 ---



 	 The values below can be used to alter script behaviour without changing

   the script itself; there should be no need to alter it unless the formula

   for calculating APR changes.

*/



var minLoan = 10000;            // Minimum loan amount

var maxLoan = 150000;           // Maximum loan amount

var rates = new Array();	 		  // Loan rates in ascending order of principal sum

var newRate = new Array();      // Values at which rate changes

var showTrailingZeroes = false; // When true, displays the '.00' after currency

var showPoundSign = true;       // When true, prepends a pound sign to currency

var showPercent	= true;         // When true, appends a percent sign to the rate



/*   In the 'rates' array, rates[0] is the interest rate for any loan above the

   minLoan value. Thereafter, rates[n] represents the interest rate for a

   principal amount above the (n - 1)th amount in the newRate array.

   	 eg. To add a new interest rate of 7.2% for principal amounts above (say)

   a £40,000 threshold value, we would add this code:



   	 newRate[1] = 40000;

		 rates[2] = 7.2;



   And that's all!

*/

rates[0] = 8.4;

newRate[0] = 25000;

rates[1] = 6.7;

/* ----------------- Nothing below this line needs to be edited ----------------- */



// Call this function to calculate and display loan details

function computeForm() {

	// Remove non-digit/decimal point characters and convert string to float

	var principal = parseFloat(document.frmCalculator.frmAmount.value.replace(/[^\d\.]/g, ''));



	// Ensure that the principal amount is within limits

	if (!(principal >= minLoan & principal <= maxLoan)) {

		alert("The loan amount must be a number between \u00a3" + minLoan + " and \u00a3" + maxLoan + ".");

		return;

	}



	// Get the APR

	var percent = rates[0];

	for (var i = 0; i < newRate.length; i++)

		if (principal >= newRate[i])

			percent = rates[i + 1]



	// Calculate the monthly interest rate

	var rate = (percent / 100) + 1;

	var sf = (rate + '').length - 1;	// Significant figures in rate (might not always be 2)

	/* Below we use the standard formula for converting APR -> mr: (r + 0.0...43)^1/12 - 1

	   (Note: the '0.0...43' is appended after the last significant figure, and is a constant

	   peculiar to this particular lender.) */

	var mRate = (Math.pow(rate + (4.3 / Math.pow(10, sf)), 1/12) - 1);

	mRate = Math.round(mRate * 100000) / 100000;	// Round to 4 d.p.



	// Calculate the monthly repayment R over m months from geometric sum:

	//   [P * r/12 * (1 + r/12)^m] / [(q + r/12)^m - 1]

	var months = document.frmCalculator.frmPayPeriod.value * 12;	// Term in months

	var repayment = (principal * mRate * Math.pow(1 + mRate, months)) / (Math.pow(1 + mRate, months) - 1);



	// Obtain total

	var total = repayment * months;



	// Display the loan repayment details

	setText(document.getElementById('repayment'),  formatCurrency(repayment));

	setText(document.getElementById('total'),  formatCurrency(total));

	setText(document.getElementById('rate'),  percent + ((showPercent) ? '%' : ''));

	document.frmCalculator.frmAmount.value = principal;

}



// Returns the correctly formatted currency value

function formatCurrency(value) {

	value = value.toFixed(2);

	if (!showTrailingZeroes) {

		var intVal = parseInt(value);

		if (intVal == value)

			value = intVal;

	}

	return ((showPoundSign) ? '\u00a3' : '') + value;

}



// Set object text

function setText(object, text) {

	textNode = document.createTextNode(text);

	objNodes = object.childNodes;

	if (objNodes.length > 0)

		object.removeChild(object.childNodes[0])

	object.appendChild(textNode);

}

//-->