// JavaScript Document
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function stripCharsInBag (s) {
var i;
var returnString = "";
var bag;
bag = "£$,"
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}

function computeForm() {
var i=0;
var ffv=0;
ffv+=stripCharsInBag(document.frmCalculator.frmAmount.value);
fmv=0;
var term = 0;
var flatRate = new Array(0.90443,0.782,0.6360);
var showRate = new Array('10.9%','9.7%','7.9%');
term = document.frmCalculator.frmPayPeriod.value;

var displayRate = 0;

if(ffv < 5000) {
i = flatRate[0]*12/100;
displayRate = showRate[0];
}
if(ffv >= 5000) {
i = flatRate[0]*12/100;
displayRate = showRate[0];
} 
if(ffv >= 10000) {
i = flatRate[1]*12/100;
displayRate = showRate[1];
} 
if(ffv >= 25000) {
i = flatRate[2]*12/100;
displayRate = showRate[2];
}

fmv+=term;
i /= 12;
var isn = 1;
var monthPayment
for (var j=0;j<term;j++)
isn=isn*(1+i);
monthPayment = (stripCharsInBag(document.frmCalculator.frmAmount.value)*isn*i)/(isn-1);
fpv = 0;
fpv += monthPayment;
var fmt = "";
var add = 9;
fmt += monthPayment;
for (var i=0;i<fmt.length;i++) {
if (fmt.charAt(i)==".") {
add=i+3;
i=fmt.length;
}
}
fmt=fmt.substring(0,add);
fcalc=((fmv*fpv)-ffv);
var fmtb="";
var add=9;
fmtb+=fcalc;
for (var i=0;i<fmtb.length;i++) {
if (fmtb.charAt(i)==".") {
add=i+3;
i=fmtb.length;
}
}
fmtb=fmtb.substring(0,add);
showPayment = "£"+addCommas(fmt);
//Display results
document.frmCalculator.frmAmount.value = ffv.substring(1,20);
document.frmCalculator.frmPayment.value = showPayment;
document.frmCalculator.frmPercent.value = "APR = "+displayRate;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}