<!--

function clearform()
{
 document.Calculator.LoanAmount.value = "";
 document.Calculator.DepositAmount.value = 0;
 document.Calculator.ResidPct.value = 0;
 document.Calculator.Mths24.value = "";
 document.Calculator.Mths36.value = "";
 document.Calculator.Mths48.value = "";
 document.Calculator.Mths60.value = "";
 document.Calculator.Mths72.value = "";
}
function calc(){
	var val;
	//alert("hello");
	val=calculate(24)
	if(val==false){
	//	alert('false');
		return;
	}else{
	//	alert(val);
		document.Calculator.Mths24.value=val;
	}
	
 document.Calculator.Mths36.value = calculate(36);
 document.Calculator.Mths48.value = calculate(48);
 document.Calculator.Mths60.value = calculate(60);
 document.Calculator.Mths72.value = calculate(72);
	
	
}

function calculate(nper){

	//var fv = formfield.balloon.value;	//residual value
	var fv="";
	
	var balper = document.Calculator.ResidPct.value	//percentage of residual
	//var nper_item = document.formfield.term.selectedIndex;	//index of term
	
	//var rate_item = document.formfield.rate.selectedIndex;//index number of rate
	var rate = document.Calculator.InterestRate1.value; //value of index number of rate
	
	//var pv = formfield.amount.value;	//amount to be financed
	var pv=document.Calculator.LoanAmount.value;
	
	//trade in amount
	var ti=document.Calculator.DepositAmount.value;
	
	
	//this will be passed in instead
	//var nper = document.formfield.term.options[nper_item].value;//value of index of term

	if ((pv < 0) || (pv == 0)) {
		alert("The Amount to be Financed must be greater than $0.00");
		return false;
	}
	if (rate == "") {
		alert("You must select an Interest Rate.");
		return false;
	}
	if (nper == "") {
		alert("You must select the Term of your finance.");
		return false;
	}
	pv = pv.replace('$','');
	pv = pv.replace(',','');
	pv = pv.replace(',','');
	pv = pv.replace(',','');
	
	if(ti!=""){
		pv=pv-ti;
	}
	document.Calculator.LoanAmount.value = pv;
	pv = -pv;
	
	if ((balper < 0) || (balper > 100)) {
		alert("The Residual Payment must be between 0% and 100%.");
		return false;
	}
	
		//perorm the calculation
	if ((fv == 0) || (fv == "")) {
		fv = -(pv*balper/100);
	}
	fv = parseFloat(fv);
	nper = parseFloat(nper);
	pv = parseFloat(pv);
	rate = parseFloat(rate);
	rate = eval((rate)/(12 * 100));
	//the calculation based on the entered term. We want to do this for each set term
	if ( rate == 0 ) {
		// Interest rate is 0
		pmt_value = - (fv + pv)/nper;
	} else {
		x = Math.pow(1 + rate,nper);
		pmt_value = -((rate * (fv + x * pv))/(-1 + x));
	}
	pmt_value = conv_number(pmt_value,2);		
	//formfield.repayment.value = pmt_value;
	return pmt_value;

}

function conv_number(expr, decplaces) {       
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
			str = "0" + str;
		}
	var decpoint = str.length - decplaces;
	//function returns the result. 
	return (str.substring(0,decpoint) + "." + str.substring(decpoint,str.length));
}


function OLDcalc()
{

 if ((document.Calculator.LoanAmount.value != "") && (document.Calculator.InterestRate1.value != ""))
 	{
	if (document.Calculator.InterestRate1.value >= 1)
	   var Interest=document.Calculator.InterestRate1.value/100; //interest (rate)
	else
		var Interest=document.Calculator.InterestRate1.value; //interest (rate)
		document.Calculator.LoanAmount.value=document.Calculator.LoanAmount.value.replace(',','');
		document.Calculator.LoanAmount.value=document.Calculator.LoanAmount.value.replace('$','');
		if (document.all)	//this is just displaying the rate used in calc
		{
		   	   document.all['APRspan'].innerHTML =" for " + String(Interest*100).substring(0, 5) + "% Interest";
		}
		
		//loan amount (pv)
		var Loanval=document.Calculator.LoanAmount.value - (document.Calculator.LoanAmount.value * (document.Calculator.ResidPct.value /100)) - document.Calculator.DepositAmount.value;
		var adjust365 = Math.pow((1/(1+Interest)),0.00141643836)
		
		//residPct.value holds the balloon amt % (balper)
		document.Calculator.ResidAmt.value = "$" + Math.round(document.Calculator.LoanAmount.value * (document.Calculator.ResidPct.value /100));
		
		//each of these will be using nper of 24,36,48,60,72
		/*document.Calculator.Mths24.value = "$" + Math.round(((Interest/12)*Loanval ) / ((1- Math.pow(1+(Interest/12),-24)))/adjust365);
		document.Calculator.Mths36.value = "$" + Math.round(((Interest/12)*Loanval ) / ((1- Math.pow(1+(Interest/12),-36)))/adjust365);
		document.Calculator.Mths48.value = "$" + Math.round(((Interest/12)*Loanval ) / ((1- Math.pow(1+(Interest/12),-48)))/adjust365);
		document.Calculator.Mths60.value = "$" + Math.round(((Interest/12)*Loanval ) / ((1- Math.pow(1+(Interest/12),-60)))/adjust365);
		document.Calculator.Mths72.value = "$" + Math.round(((Interest/12)*Loanval ) / ((1- Math.pow(1+(Interest/12),-72)))/adjust365);*/
		
		
		if (document.Calculator.Mths24.value == "$N")
			document.Calculator.Mths24.value = 0.0;
		if (document.Calculator.Mths36.value == "$N")
			document.Calculator.Mths36.value = 0.0;
		if (document.Calculator.Mths48.value == "$N")
			document.Calculator.Mths48.value = 0.0;
		if (document.Calculator.Mths60.value == "$N")
			document.Calculator.Mths60.value = 0.0;
		if (document.Calculator.Mths72.value == "$N")
			document.Calculator.Mths72.value = 0.0;
	}
 else
    {
	alert('Please enter both Loan Amount and Interest Rate');
	 }
}
//-->