
var browser;
agt=navigator.userAgent;
uVer=navigator.appVersion.substring(0,4);

if (agt.indexOf("MSIE") != -1)

         if (parseInt(uVer.charAt(0))==2) {
                 browser="msie3";
         }
         else {
                 browser="other";
         }

else {
         browser="other";
}

        //  10.01.99 ->
		bName=navigator.appName;
		bVer=parseInt(navigator.appVersion);
		if (bName=="Netscape" && bVer >=3) br = "n3"; 
		else if  (bName=="Microsoft Internet Explorer" && bVer >=4) br = "e4";
		else br="n2";
		// <- 10.01.99
		
//pads out field to fake right-justification
//length fixed as constant
function pad(number) {
     var str = '' + number;
     while (str.length < 9)
         str = ' ' + str;
     return str;
 }

//convert entry to valid number by removing everything but 0-9 or '.'

function MakeNum(newNumber){
	newStr=new String
	if(newNumber.charAt(0) == ".") newNumber = "0" + newNumber;
	strLength=newNumber.length
	
	for(var i=0; i<=strLength; i++) {
		tmpChar=newNumber.charAt(i)		
		//convert non-alpha to letter to ensure fail (except for space, which we'll skip)
		if (tmpChar=="-" || tmpChar=="+" || tmpChar==")" || tmpChar=="(" || tmpChar=="*" || tmpChar=="&" || tmpChar=="#" || tmpChar=="!" || tmpChar=='"' || tmpChar=="/" || tmpChar=="'" || tmpChar=="$" || tmpChar=="," || tmpChar=="%"){tmpChar="h"}
		if (tmpChar==" "){tmpChar=""}
		
		// unnecessary? testChar=parseInt(tmpChar)
		if(tmpChar=="." || tmpChar<="9") {newStr=newStr+tmpChar}
	
	}	
	
	if(isNaN(newStr)) return "enter";	
	return Math.round(newStr);	
}

function formatMoney(amount) {
	return OutputComma(Math.floor(amount-0) + '') + OutputCents(amount - 0);
}	

function OutputCents(amount) {
    amount = Math.round( ( (amount) - Math.floor(amount) ) *100);
    return (amount < 10 ? '.0' + amount : '.' + amount);
}


function OutputComma(number) {
    number = '' + number
    if (number.length > 3) {
        var mod = number.length%3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if ((mod ==0) && (i ==0))
                output+= number.substring(mod+3*i,mod+3*i+3);
            else
                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
        }
        return (output);
    }
    else return number;
}

	
/* Loan functions */

function clearLoan() {
         //  10.01.99 ->
         
		 	document.calcForm.LN_price.value = "";
			document.calcForm.LN_down.value = "";
			document.calcForm.LN_trade.value = "";
			document.calcForm.LN_term.selectedIndex = 0;
			document.calcForm.LN_APR.value = "";
			document.calcForm.LN_balance.value = "";
			document.calcForm.LN_payment.value = "";
			//doLoanCalc();
			// Oct 29/2001 HR -- focus causes the "enter" to change to 0 when clicked anywhere after hitting clear
			//document.calcForm.LN_price.focus();
		 
		 // <- 10.01.99

}		

//Checking and formatting

function checkLoanPrice() {
         //  10.01.99 ->
        
		 	var thing = document.calcForm.LN_price.value;
			document.calcForm.LN_price.value = OutputComma(MakeNum(thing));
			
					 
		 // <- 10.01.99
		 doLoanCalc();
}

function checkLoanDown() {
         //  10.01.99 ->
         
	        var thing = '0' + document.calcForm.LN_down.value;
			document.calcForm.LN_down.value = OutputComma(MakeNum(thing));
		 // <- 10.01.99
		 doLoanCalc();

}

function checkLoanTrade() { 
         //  10.01.99 ->
         
		 	var thing = '0' + document.calcForm.LN_trade.value;
	        document.calcForm.LN_trade.value = OutputComma(MakeNum(thing));
		 // <- 10.01.99
		 doLoanCalc();

}

function checkLoanAPR() {

	   //  10.01.99 ->
         
		     var thing = document.calcForm.LN_APR.value;	
		 // <- 10.01.99
		 	 
	if(isNaN(thing)) {
	    //  10.01.99 ->
        
		     document.calcForm.LN_APR.value = "";
		 // <- 10.01.99
		doLoanCalc();
		return;
	}
	
	//else...
	
	var rate = (Math.round(10* (thing - 0)))/10;
	if (rate == Math.floor(rate) && rate > 0) rate = rate + '.0'; 

	
	if(rate < 0) rate = 0;
	if(rate > 30) rate = 30;	
	//  10.01.99 ->
         
		     document.calcForm.LN_APR.value = rate;
		 // <- 10.01.99
	     doLoanCalc();
}


//loan payment calculation

function doLoanCalc() {
	//checkLoan for enough valid terms
	     //  10.01.99 ->
         
		     if(document.calcForm.LN_price.value == "")  {
		     document.calcForm.LN_balance.value = "";
		     document.calcForm.LN_payment.value = "";
		     //document.calcForm.LN_finCharge.value = "   ---";
		     return;
	         }
	         //else...
	
	         var cPrice = MakeNum(document.calcForm.LN_price.value) - 0;
	         var cDown = MakeNum(document.calcForm.LN_down.value) - 0;
	         var cTrade = MakeNum(document.calcForm.LN_trade.value) - 0;
		     
		 // <- 10.01.99
	
	
	var cBal = cPrice - cDown - cTrade;
	if((cBal < 0) || isNaN(cBal)) {
		cBal = 0;
		 //  10.01.99 ->
         
		      document.calcForm.LN_balance.value = "Error";
		 
		 // <- 10.01.99
		return;	
	}
	else{ 
	 //  10.01.99 ->
         
		     document.calcForm.LN_balance.value = OutputComma(cBal);
		 
		 // <- 10.01.99
	}	
	
	 //  10.01.99 ->
       
		     if((document.calcForm.LN_term.options[document.calcForm.LN_term.selectedIndex].value == "") || 
	(document.calcForm.LN_APR.value == "")) {
		document.calcForm.LN_payment.value = "";
		//document.calcForm.LN_finCharge.value = "   ---";
		return;
	}
	//else...	
	
	var cRate = (document.calcForm.LN_APR.value)/1200;
	var cTerm = document.calcForm.LN_term.options[document.calcForm.LN_term.selectedIndex].value;
		 
		 // <- 10.01.99
	
	if(cRate == 0) { var payment = cBal/cTerm;}
	else {
		var cInt = Math.pow((1 + cRate),cTerm);
		var payment = cBal * (cRate * cInt) / (cInt - 1);
	}	
	 //  10.01.99 ->
         
		    document.calcForm.LN_payment.value = formatMoney(payment); 
				
		    //debug document.calcForm.LN_dump.value = "\nPrice=" + cPrice + "\nDown=" + cDown + "\nRate=" + cRate + "\nTerm=" + cTerm + "\nIntermediate=" + cInt + "\nPayment=" + payment;  
	
	        //document.calcForm.LN_finCharge.value = (Math.round(((payment * cTerm) - cBal) * 100))/100;
		 
		 // <- 10.01.99
	
	return;
		
}

/* Lease functions */

function clearLease() { 
     //  10.01.99 ->
        
		     document.calcForm.LS_price.value = "";
	         document.calcForm.LS_down.value = "";
	         document.calcForm.LS_trade.value = "";
 	         document.calcForm.LS_balance.value = "";
	         document.calcForm.LS_term.selectedIndex = 0;
	         document.calcForm.LS_APR.value = "";
	         document.calcForm.LS_buyout.value = "";
			 document.calcForm.LS_amort.value = "";
			 document.calcForm.LS_payment.value = "";
	         //doLeaseCalc();
	         // Oct 29/3001 HR -- focus causes the "enter" to change to 0 when clicked anywhere after hitting clear
			 //document.calcForm.LS_price.focus();
		
		 // <- 10.01.99
}

//Checking and formatting

function checkLeasePrice() {
     //  10.01.99 ->
         
		    var thing = document.calcForm.LS_price.value;
	        document.calcForm.LS_price.value = OutputComma(MakeNum(thing));
		 
		 // <- 10.01.99
		 doLeaseCalc();
}

function checkLeaseDown() {
         //  10.01.99 ->
         
		    var thing = '0' + document.calcForm.LS_down.value;
	        document.calcForm.LS_down.value = OutputComma(MakeNum(thing)); 
		 
		 // <- 10.01.99
		doLeaseCalc();
}

function checkLeaseTrade() { 
         //  10.01.99 ->
         
		     var thing = '0' + document.calcForm.LS_trade.value;
	         document.calcForm.LS_trade.value = OutputComma(MakeNum(thing));
		 
		 // <- 10.01.99
		 doLeaseCalc();
}

function checkLeaseBuyout() { 
         //  10.01.99 ->
         
		     var thing = document.calcForm.LS_buyout.value;
	         document.calcForm.LS_buyout.value = OutputComma(MakeNum(thing));
		 
		 // <- 10.01.99
		 doLeaseCalc();
}

function checkLeaseAPR() {
     //  10.01.99 ->
         
		    var thing = document.calcForm.LS_APR.value;	   
		 
		 // <- 10.01.99
	if(isNaN(thing)) { 
	     //  10.01.99 ->
         
		     document.calcForm.LS_APR.value = "";
		 
		 // <- 10.01.99
		doLeaseCalc();
		return;
	}
	
	//else...
	var rate = (Math.round(10* (thing - 0)))/10;
	if (rate == Math.floor(rate) && rate > 0) rate = rate + '.0'; 
	if(rate < 0) rate = 0;
	if(rate > 30) rate = 30;	
	     //  10.01.99 ->
         
		    document.calcForm.LS_APR.value = rate; 
		 
		 // <- 10.01.99
	doLeaseCalc();
}


//lease payment calculation

function doLeaseCalc() {
	//checkLease for enough valid terms
	 //  10.01.99 ->
         
		     if(document.calcForm.LS_price.value == "") {
		     document.calcForm.LS_balance.value = "";
		     document.calcForm.LS_payment.value = "";
		     document.calcForm.LS_amort.value = "";
		     //document.calcForm.LS_finCharge.value = "   ---";
		     return;
	    }
	    //else...
	
	         var cPrice = MakeNum(document.calcForm.LS_price.value) - 0;
	         var cDown = MakeNum(document.calcForm.LS_down.value) - 0;
	         var cTrade = MakeNum(document.calcForm.LS_trade.value) - 0;
		 
		 // <- 10.01.99
	
	
	var cBal = cPrice - cDown - cTrade;
	if((cBal < 0) || isNaN(cBal)) {
		cBal = 0;
		 //  10.01.99 ->
         
		     document.calcForm.LS_balance.value = "Error";
		
		 // <- 10.01.99
		return;	
	}
	else{ 
	 //  10.01.99 ->
         
		     document.calcForm.LS_balance.value = OutputComma(cBal);
		 
		 // <- 10.01.99
	}	
	
	 //  10.01.99 ->
        
		     if((document.calcForm.LS_term.options[document.calcForm.LS_term.selectedIndex].value == "") || 
	  (document.calcForm.LS_APR.value == "") ||
	  (document.calcForm.LS_buyout.value == "") ||
	  (document.calcForm.LS_buyout.value == "Error")) {
		document.calcForm.LS_amort.value = "";
		document.calcForm.LS_payment.value = "";
		//document.calcForm.LS_finCharge.value = "   ---";
		return;
	}
	//else...
		
	var cBuyout = MakeNum(document.calcForm.LS_buyout.value) - 0;	
	var cRate = (document.calcForm.LS_APR.value)/1200;
	var cTerm = document.calcForm.LS_term.options[document.calcForm.LS_term.selectedIndex].value;
	var amort;
	var payment;
		 
		 // <- 10.01.99
	
	
	if(cBuyout >= cBal) { 
	    //  10.01.99 ->
         
		     document.calcForm.LS_buyout.value = "Error";
		 
		 // <- 10.01.99
		return;
	}	
	
	if(cRate == 0) { 
		payment = (cBal-cBuyout)/cTerm;
		amort = cBal - cBuyout;
	}
	else {
		var cInt = Math.pow((1 + cRate),cTerm);
		amort = cBal - cBuyout;
		payment = (cRate * ((cBal * cInt) - cBuyout))/((1 + cRate) * (cInt - 1));
	}
	 //  10.01.99 ->
      
		     document.calcForm.LS_amort.value = OutputComma(amort);
	         document.calcForm.LS_payment.value = formatMoney(payment);
	
	         //debug document.calcForm.LS_dump.value = "\nPrice=" + cPrice + "\nDown=" + cDown + "\nRate=" + cRate + "\nTerm=" + cTerm + "\nIntermediate=" + cInt;  
	
	         //document.calcForm.LS_finCharge.value = (Math.round(((payment * cTerm) - cBal) * 100))/100;
		
		 // <- 10.01.99

	return;
		
}
