function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function isEmpty(field) {return (field.value.length == 0)}

// check for a valid number greater than zero
function isNumber(field) {
	var valid = "0123456789., "
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == -1) {
			return false;
		}
	}
	return true;
}

// check a number field when the user has finished entering it (onBlur)
function checkNumber(field, name) {
	if (isEmpty(field)) {
		field.focus();
		alert("The " + name + " must be entered.");
		return false;
	} else if (!isNumber(field) || (field.value==0)) {
		field.focus();
		alert("The " + name + " must be a number greater than zero.");
		return false;
	}
	return true;
}

// turn a monetary string containing digits, commas and spaces into a number
function parseNumber(strNumber) {
	var num = strNumber.value;
	num = num.replace(/,/g,'');
	num = num.replace(/ /g,'');
	return num;
}

// turn a number into a monetary string by adding in commas and rounding
function parseMoney(number) {
	var num = "" + Math.round(number);
	var money = "";
	var c = 3;
	while (c < num.length) {
		money = "," + num.substring(num.length - c, num.length - c + 3) + money;
		c = c + 3;
	}
	money = num.substring(num.length - c, num.length - c + 3) + money;
	return money;
}

// turn year and month values into just months
function parseMonths(years) {
	var years = parseNumber(years);
	var period = years * 12;
    return period;
}

// ensure the years entered are ok
function checkYear(field, name) {
	if (isEmpty(field)) {
		field.focus();
		alert("The " + name + " must be entered.");
		return false;
	} else if (!isNumber(field) || (field.value > 30)) {
		field.focus();
		alert("The " + name + " must be a number and no longer than 30 years.");
		return false;
	}
	return true;
}

////////////////////////////////// Calculate the Repayment Amount
function calculateRepay() {
	// get all the inputs
	var LoanAmount = parseNumber(document.calculator.LoanAmt);
	var InterestRate = parseNumber(document.calculator.IntRate)/1200;
	var TermMonth = parseMonths(document.calculator.TermYear);

	// find minimum repayment amount needed for the loan amount given
	// Math.pow - This method returns the value of x to the power of y (xy), where x is the base, and y is the exponent.
	var MinRepay = LoanAmount * InterestRate / (1 - Math.pow((1 + InterestRate * 1),-TermMonth));
	MinRepay = Math.ceil(MinRepay);
	
	document.calculator.RepayAmt.value = parseMoney(MinRepay);
	document.calculator.RepayAmt.focus();
}
////////////////////////////////// Calculate the TERM

function calculateTerm() {
	// preset numbers
	var MaxTerm = 30 * 12;     /* 30 years */
	
	// get all the inputs
	var LoanAmount = parseNumber(document.calculator.LoanAmt);
	var RepayAmount = parseNumber(document.calculator.RepayAmt);
	var RepayFreq = document.calculator.RepayFreq.options[document.calculator.RepayFreq.selectedIndex].value;
	var InterestRate = parseNumber(document.calculator.IntRate)/1200;
	RepayAmount = RepayAmount * RepayFreq/12;
	
	// find minimum repayment amount needed for the loan amount given
	// Math.pow - This method returns the value of x to the power of y (xy), where x is the base, and y is the exponent.
	var MinRepay = LoanAmount * InterestRate / (1 - Math.pow((1 + InterestRate),-MaxTerm));
	MinRepay = Math.ceil(MinRepay);
	
	if (RepayAmount < MinRepay) {
		alert("Sorry, the minimum Repayment Amount for the specified\nLoan Amount and Interest Rate is " + MinRepay + " dollars.");
		document.calculator.TermYear.value = " ";
		document.calculator.TermMonth.value = " ";
	} else {
		// calculate the repayment period (years & months)
		var TermMonth = Math.log(RepayAmount/(RepayAmount - LoanAmount * InterestRate)) / Math.log(1 + InterestRate);
		TermMonth = Math.ceil(TermMonth);
		var TermYear = Math.floor(TermMonth / 12);
		TermMonth = TermMonth - (TermYear * 12);
		document.calculator.TermYear.value = TermYear;
		document.calculator.TermMonth.value = TermMonth;
		document.calculator.TermYear.focus();
	}
}
////////////////////////////////// Calculate the LOAN AMOUNT
function calculateLoan() {
	// get all the inputs
	var RepayAmount = parseNumber(document.calculator.RepayAmt);
	var InterestRate = parseNumber(document.calculator.IntRate)/1200;
	var TermMonth = parseMonths(document.calculator.TermYear);
	
	// calculate the loan amount
	var LoanAmt = RepayAmount * (1 - Math.pow((1 + InterestRate * 1),-TermMonth))/InterestRate;
	document.calculator.LoanAmt.value = parseMoney(LoanAmt);
	document.calculator.LoanAmt.focus();
}