
function StringToAdv()
{
	var str
	str="?Price=" + document.calc.txtPrice.value + "&Down=" + document.calc.txtDown.value + "&Rate=" + document.calc.txtInterestRate.value + "&Term=" + document.calc.txtTerm.value  + "&Taxes=" + document.calc.txtTaxes.value + "&Insurance=" + document.calc.txtInsurance.value
	document.location.href = "payment_calc.asp" + str
}

function Prepopulate(prmPrice, prmDown, prmInterestRate, prmTerm, prmClosing, prmPoints, prmTaxes, prmInsurance)
{
	document.calc.txtPrice.value= prmPrice
	document.calc.txtInterestRate.value  = prmInterestRate
	document.calc.txtDown.value = prmDown
	document.calc.txtLoanAmount.value = prmPrice- prmDown
	document.calc.txtTaxes.value = prmTaxes
	document.calc.txtInsurance.value  = prmInsurance
}
function Resize_Window(){
	var i, x, w, num
	
	if (navigator.appName=="Netscape") {
		num=6;
		}
	else num=5;
	
	for(i = 1;i < 15;i++){
		for(x = 1;x < 5000; x++){
			w = 1;
		}
		self.resizeBy(0, -num);
	}
}
function SetFocusOn(){
	document.calc.txtLoanAmount.focus()
}
    
function saveTerm(prmTerm){
	document.calc.txtTerm.value = prmTerm;
}      
    
function CalcTermMonthYr() {
	var amount = document.calc.txtLoanAmount.value
	var rate = document.calc.txtInterestRate.value
	var price = document.calc.txtPrice.value 
	var down = document.calc.txtDown.value 
	var term = document.calc.txtTerm.value 
	var taxes = document.calc.txtTaxes.value 
	var insurance = document.calc.txtInsurance.value 
	var monthYr = 12
	var flg = true
	var total
	var pmi = .00;
	
	if (term == "") term = 30;		
	
	// make sure the numbers are rounded
	amount = amount * 1;
	rate = rate * 1;
	term = term * 1;
	price = price *1;
	down = down *1;				
	taxes = taxes *1;
	insurance = insurance *1;
				
	if(isNaN(price)) {
		alert("Please enter a numeric value for the Price.")
		document.calc.txtPrice.focus()
		flg=false
	}
	else{
		if (isNaN(down)){
			alert("Please enter a numeric value for the Down Amount.")
			document.calc.txtDown.focus()
			flg=false
		}
		else{
			document.calc.txtLoanAmount.value=price - down
			var ltv = parseFloat(document.calc.txtLoanAmount.value / price);
			
	
			if(ltv < .85) pmi = .00;
			else if(ltv < .90) pmi = (term == 30 ? .12 : .06);
			else if(ltv < .95) pmi = (term == 30 ? .51 : .12);
			else if(ltv < .97) pmi = (term == 30 ? .77 : .25);
			else if(ltv < 1.01) pmi = (term == 30 ? 1.04 : .25);
			
			pmi = (pmi * (document.calc.txtLoanAmount.value/100))/12;
			
			document.calc.txtPMI.value = round(pmi);
		}
	}
		
		
	if (isNaN(taxes)) {
		alert("Please enter a numeric value for the taxes.")
		document.calc.txtTaxes.focus()
		flg=false
	}
	else
	{
		taxes= taxes/100;
		taxes= taxes * price;
		document.calc.txtTaxesAmount.value  = taxes;
		document.calc.txtTaxesAmount.value = dollarFormat(document.calc.txtTaxesAmount.value);
	}
	if (isNaN(insurance)) {
		alert("Please enter a numeric value for the Insurance.")
		document.calc.txtInsurance.focus()
		flg=false
	}
	else
	{
		insurance=insurance/12;			
	}
		
		//Math.round(price * 100.00 * 0.005417)/100.00;
	if (isNaN(amount)) {
         alert("Please enter a numeric value for the Loan Amount.")
         //document.calc.txtLoanAmount.value = ""
         document.calc.txtLoanAmount.focus()
         flg = false
         if (document.calc.txtInterestRate.value != "") {
			if (isNaN(rate)) {
			   document.calc.txtInterestRate.value = ""
			}
		 }			 
      }
        else {
            if (isNaN(rate)) {
                alert("Please enter a numeric value for the Interest Rate.")
                //document.calc.txtInterestRate.value = ""
                document.calc.txtInterestRate.focus()
                flg = false                    
            }                 
         }
        
    //-- Actual Calculation     
	if (document.calc.txtLoanAmount.value != "" && document.calc.txtInterestRate.value != "" && term != "") {			
	  if (term != 0 && document.calc.txtInterestRate.value != 0) {			
			rate = rate /1200;
			if (monthYr == 12) {
				term = term * 12;
			}
			    total = Math.pow(1 + rate,term);			   
				document.calc.txtPayment.value = (amount*total*rate) / (total-1);				
				document.calc.txtTotalPayment.value = ((amount*total*rate) / (total-1))  + insurance + (taxes/12) + pmi;					
				document.calc.txtPayment.value = dollarFormat(document.calc.txtPayment.value);	
				document.calc.txtTotalPayment.value = dollarFormat(document.calc.txtTotalPayment.value);	
	  } else {
		document.calc.txtPayment.value = ""
		document.calc.txtTotalPayment.value = ""
	  }
	} else {
		document.calc.txtPayment.value = ""
		document.calc.txtTotalPayment.value = ""
	}
}	
    
//-- Format dollar
function dollarFormat(valueIn) {	      
	var formatStr = ""
	var OutDollars = ""
	var deciPos = valueIn.indexOf(".")
				
	if (deciPos == -1) 
		deciPos = valueIn.length
		var dollars = valueIn.substring(0,deciPos)
		var dollen = dollars.length
		if (dollen > 3) {
			while(dollen > 0) {
				totalDollar = dollars.substring(dollen - 3, dollen)
				if (totalDollar.length == 3) {
					OutDollars = "," + totalDollar + OutDollars
					dollen = dollen - 3
				} else {
					OutDollars = totalDollar + OutDollars
					dollen = 0
				  }					
		}
			
	if (OutDollars.substring(0,1) == ",") 
		dollars = OutDollars.substring(1,OutDollars.length)
	else
		dollars = OutDollars
					
	}
	var cents = valueIn.substring(deciPos+1,deciPos+3)
	if (cents == "")
		cents = "00"		
		var formatStr = "$"+dollars+"."+cents
	return formatStr		
}   
         
//-- Round number
function round(x) {
	return Math.round(x*100)/100;        
}

