
function myRound(number){
    var temp = number - Math.floor(number);
    temp *= 100;
    temp = Math.round(temp);
    temp /= 100;
    temp = Math.floor(number) + temp;
		
    return temp;
    }    

//Check for querystring appended to url: if yes, then get loan rate, term.
function doAdjustment() {
	if (getParm("?") == 1) {
		document.user_input.term.value = getParm("term");
		document.user_input.home_rate.value = getParm("home_rate");
	}
}

// Get query string and search for name/value pair using fieldName to search.
function getParm(fieldName) {

    var qstr = location.search // passed parameters including question mark
    offset = qstr.indexOf(fieldName);
		if (offset == -1)
			return -1;
		else if (fieldName=="?")
			return 1;
    temp = qstr.substring(offset,qstr.length);
    endPos = temp.indexOf("&");

    if (endPos == null || endPos == -1 || endPos == "") {
      endPos = temp.length
    }
    sub = temp.substring(fieldName.length + 1,endPos)
    return sub
}
	 
	 
// Calculator functions	 
    var loanBal = 1.0;
    var compRate = 1.0;
    var totalInt = 0.0;
    var termLeft = 0;
	    
function doLoan(numTimes){
    var rate = (compRate / 100.0) / 12.0;
    var loanPayments = loanBal * rate / (1 - (1/Math.pow(1+rate,termLeft)));    
    loanPayments = myRound( loanPayments ); 
		     
		for(tem=0; tem < numTimes; tem++){
    	var inter = loanBal * rate;
    	inter = myRound( inter );            
    	var prnpal = loanPayments - inter;
    	prnpal = myRound( prnpal );           
    	loanBal -= prnpal;
    	loanBal = myRound( loanBal );           
    	totalInt += inter;
		}
}  

function isNotValidEntry(s) {
	// added 09 May 2000 - jbh
	//returns true when any character in string s is not 0-9 or the decimal point.
	for(var i = 0; i < s.length; i++)
		var c = s.charAt(i);
		if(c == ' ' || ((c > "9" || c < "0") && (c != ".")))
			return true;
	return false;
}
 
function Answer(){   
    var rateChange = 0.0;
    var down = parseFloat(document.user_input.down.value);
    var term = parseFloat(document.user_input.term.value);
    var homeRate = parseFloat(document.user_input.home_rate.value);
    var price = parseFloat(document.user_input.price.value);
    var taxRate = parseFloat(document.user_input.total_taxes.value);
    var autoRate = parseFloat(document.user_input.auto_rate.value);
    var homeLoanCost = parseFloat(document.user_input.home_costs.value);
  // NEW STUFF as of 09 May 2000 - error checking based on similar checking from registration form, also added isNotValidEntry function above - jbh
 	var ErrorList = "";
	if (isNaN(price)    || isNotValidEntry(document.user_input.price.value))       ErrorList = ErrorList + "\nLoan Amount";
	if (isNaN(autoRate) || isNotValidEntry(document.user_input.auto_rate.value))   ErrorList = ErrorList + "\nConsumer Loan Rate";
	if (isNaN(term)     || isNotValidEntry(document.user_input.term.value))        ErrorList = ErrorList + "\nLoan Term";
	if (isNaN(homeRate) || isNotValidEntry(document.user_input.home_rate.value))   ErrorList = ErrorList + "\nHome Equity Loan Rate";
	if (isNaN(taxRate)  || isNotValidEntry(document.user_input.total_taxes.value)) ErrorList = ErrorList + "\nIncome Tax Rate";
	if (ErrorList != "") {
		alert("Please verify that the following fields contain valid numbers:" + ErrorList);
		return false;
		}
  // END NEW  
    var rate = (autoRate / 100.0) / 12.0;        
    loanBal = (price - down);        
    var autoPay = loanBal * rate / ( 1 - ( 1/Math.pow(1+rate,term)));
    autoPay = myRound( autoPay );    
    termLeft = term;
    compRate = autoRate;
    loanBal = (price - down);
    totalInt = 0.0;
    doLoan( term );    
    var autoInstTotal = totalInt;
    autoInstTotal = myRound( autoInstTotal );        
    autoTotal = myRound( autoInstTotal + (price - down) );        
    totalInt = 0;        
    loanBal = (price - down);        
    document.output.auto_payment.value = autoPay;
    document.output.auto_inter.value = autoInstTotal;
    document.output.auto_total.value = autoTotal;    
    rate = (homeRate / 100.0) / 12.0;
    var homePayment = loanBal * rate / ( 1 - ( 1/Math.pow(1+rate,term)));    
    document.output.home_payment.value = myRound( homePayment );        
    compRate = homeRate;
    loanBal = (price - down);
    totalInt = 0.0;
		
		while( term >= 12){
  	  termLeft = term;
  	  doLoan( 12 );
  	  term -= 12;
  	  compRate += rateChange;
		}        
  	termLeft = term;	
		
		if( term > 0 ){
    	doLoan(term);
		}
    totalInt = myRound( totalInt );
    document.output.home_inter.value = totalInt;        
    var taxSave = myRound( totalInt * (taxRate / 100.0) );
    document.output.home_tax_save.value = taxSave;    
		document.output.auto_tax_save.value= 0;    
    var homeTotal = myRound( totalInt + (price - down) - taxSave + homeLoanCost );       
    document.output.home_total.value = homeTotal;        
    var difference = myRound( autoTotal - homeTotal ); 
		
		document.output.home_principal.value = myRound(price);
		document.output.auto_principal.value = myRound(price);
		   
		if( difference > 0 ){
    	document.output.difference.value = difference;
		} else {
    document.output.difference.value = 0
		}
		
	//scroll down to the answers
	window.scroll(0,400);
}
