<!--hide 

calcdone = "no";

function exitapp ()
	{
	var exitapplication= confirm("Exit the calculator?");
 	if (exitapplication == true)
 		{
  		window.status="Closing"
		parent.window.close();
 		}
	}

function checkfloat(num)
	{
	num.value=parseFloat(num.value, 10);
	if (num.value == 'NaN' || num.value == 'Nan' || num.value == 'nan' ) num.value = num.defaultValue;
	if (num.value < 5 || num.value > 20) {
		alert ("The interest rate must be between 2 and 20 percent");
		num.value = num.defaultValue;}
	}

function checknum (field, num)
	{
	if (calcdone == "yes"){
	alert ("Please reset to allow you to re-calculate");
	calculate();}
	else {
	validate (field, num);
	if (num.value != ""){
	switch (field)
		{	case "lterm":
			if (num.value < 5 || num.value > 30) {
			alert ("The loan term should be between 5 and 30 years.");
			num.value="";}
			else 
				if (document.form.lamount.value != "" && document.form.ramount.value != ""){
  				alert ("Please clear one of the other fields before entering a loan term.");
				num.value="";}
			break;
			case "lamount":
			if (document.form.ramount.value != "" && document.form.lterm.value != ""){
  			alert ("Please clear one of the other fields before entering a loan amount.");
			num.value="";}
			break;
			case "ramount":
			if (document.form.lamount.value != "" && document.form.lterm.value != ""){
  			alert ("Please clear one of the other fields before entering a repayment amount.");
			num.value="";}
			break;
			}
		if (num.value != "")
		num.value=parseInt(num.value, 10);
		}		
	}
}

function validate(field, num) {
var valid = "0123456789";
var ok = "yes";
var temp;
for (var i=0; i<num.value.length; i++) {
	tempchar = "" + num.value.substring(i, i+1);
	if (valid.indexOf(tempchar) == "-1") ok = "no";
	}
if (ok == "no") {
	alert("This is not a valid entry.\nPlease enter only whole numbers.");
	num.value="";
	}
}

function calculate ()
	{
		

	if (calcdone == "yes"){
		document.form.intrate.value = document.form.intrate.defaultValue;
		document.form.rfrequency.selectedIndex = 2;
		document.form.lamount.value = "";
		document.form.ramount.value = "";
		document.form.lterm.value = "";
		calcdone = "no";}
	else {
	fieldsok = "no";
	prate = (parseFloat(document.form.intrate.value, 10)/parseInt(document.form.rfrequency.value,10))/100;

		if (document.form.lamount.value != "" && document.form.ramount.value != ""){
  		lamount=document.form.lamount.value;
		ramount=document.form.ramount.value;
		nrepayments=(Math.log(ramount/(ramount-parseFloat(lamount*prate))))/(Math.log(1+parseFloat(prate)))/document.form.rfrequency.value;
			if (nrepayments > 35 || isNaN(nrepayments)){ 
			alert ("The term would be too large \n");
			// nrepayments=0 ;
			}
		document.form.lterm.value = parseInt(nrepayments,10);

		fieldsok = "yes";
		calcdone = "yes";}

		if (calcdone != "yes" && document.form.lamount.value != "" && document.form.lterm.value != ""){
  		lamount=document.form.lamount.value;
		nrepayments=document.form.lterm.value*document.form.rfrequency.value;
		ramount=(lamount*(prate/(1-Math.pow(1+prate,-nrepayments))));
		document.form.ramount.value=parseInt(ramount,10);
		fieldsok = "yes";
		calcdone = "yes";
		}

		if (calcdone != "yes" && document.form.ramount.value != "" && document.form.lterm.value != ""){
		ramount=document.form.ramount.value;
		nrepayments=document.form.lterm.value*document.form.rfrequency.value;
		lamount=(ramount*(1-Math.pow(1+prate,-nrepayments)))/prate;
		document.form.lamount.value=parseInt(lamount,10);
		fieldsok = "yes";
		calcdone = "yes";}

		if (fieldsok == "no")
		alert ("Please enter values for 2 of the fields to calculate the third one.");
		}
	}

//--> 
