// Javascript functions to work on Payment Search's What you can afford

	function KeepOnly(keep,strString)
   	{
	var strChar;
	var result = "";
	for (i = 0; i < strString.length; i++)
		{
		strChar = strString.charAt(i);
		if (keep.indexOf(strChar) > -1)
			{
			result = result + strChar;
			}
		}
	return result;
	}
	function IsNumeric(strString)
	{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;
	for (i = 0; i < strString.length && blnResult == true; i++)
		{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			{
			blnResult = false;
			}
		}
		if (findOccurances(strString,".")>1)blnResult=false;
		
	return blnResult;
	}
	function findOccurances(str,ch)
	{
	var l=0;
		for (var i=0;i<str.length;i++)
			if (str.charAt(i)==ch)
			{
				l++
			}
	return l;
	}
	function CCents(dF)
	{  
	dF *= 100;
	dF = Math.round(dF);
	return (dF / 100);
	}
	function CalcLoanAmount(payment, term, rate)
	{
	var dMonthlyInterest = parseFloat(rate) / (12*100);
	var dTerm = parseInt(term);
	var dMoPmt = parseFloat(payment);
	var dLoanAmount = dMoPmt * ((1/dMonthlyInterest) - 
								(1/(dMonthlyInterest * 
								Math.pow(dMonthlyInterest+1, dTerm))));
	dLoanAmount = CCents(dLoanAmount);
	if (isNaN(dLoanAmount))dLoanAmount=dMoPmt * dTerm
	return dLoanAmount;
	} 
	function validateInputs(pa)
	{
	if (pa==1)
	{
		if (IsNumeric(document.Form1.CashDown.value))
		{
			document.getElementById("CashDownErrorCell").innerHTML=""
			cashDown = parseFloat(document.Form1.CashDown.value);
		}
		else
		{
			document.getElementById("CashDownErrorCell").innerHTML=GetErrMsg();//"Must be numeric"
			document.Form1.CashDown.value = 0;
			cashDown = 0;
			document.Form1.CashDown.focus();
		}
	}
	else if (pa==2)
	{	
		if (IsNumeric(document.Form1.TradeIn.value))
		{
			document.getElementById("TradeInErrorCell").innerHTML=""
			tradeIn = parseFloat(document.Form1.TradeIn.value);
		}
		else
		{
			document.getElementById("TradeInErrorCell").innerHTML=GetErrMsg();//"Must be numeric"
			document.Form1.TradeIn.value = 0;
			tradeIn = 0;
			document.Form1.TradeIn.focus();
		}
	}
	else if (pa==3)
	{
		if (IsNumeric(document.Form1.BalanceOwed.value))
		{
			document.getElementById("BalanceOwedErrorCell").innerHTML=""
			balOwed = parseFloat(document.Form1.BalanceOwed.value);
		}
		else
		{
			document.getElementById("BalanceOwedErrorCell").innerHTML=GetErrMsg();//"Must be numeric"
			document.Form1.BalanceOwed.value = 0;
			balOwed = 0;
			document.Form1.BalanceOwed.focus();
		}
	}
	}
	function Calculate(paymentAmount)
	{
	var lTerm = document.Form1.TermsInMonths.options[document.Form1.TermsInMonths.selectedIndex].value;
	var lRate = document.Form1.InterestRate.options[document.Form1.InterestRate.selectedIndex].value;
	var lTax = parseFloat(document.Form1.SalesTax.options[document.Form1.SalesTax.selectedIndex].value);
	var lGross = CalcLoanAmount(paymentAmount, lTerm, lRate);
	
	if (document.Form1.CashDown.value == "") { document.Form1.CashDown.value = 0; }
	if (document.Form1.TradeIn.value == "") { document.Form1.TradeIn.value = 0; }
	if (document.Form1.BalanceOwed.value == "") { document.Form1.BalanceOwed.value = 0; }
	cashDown = parseFloat(document.Form1.CashDown.value);
	tradeIn = parseFloat(document.Form1.TradeIn.value);
	balOwed = parseFloat(document.Form1.BalanceOwed.value);

	lTax = lTax / 100;
	lTax+=1;
	var lNet = lGross/lTax;
	return CCents(lNet + cashDown + (tradeIn - balOwed));
	}

	function CalcLoan()
	{
	//document.Form1.MinPayment.value = KeepOnly("0123456789",document.Form1.MinPayment.value);
	//document.Form1.MaxPayment.value = KeepOnly("0123456789",document.Form1.MaxPayment.value);
	var lMin = CCents(document.Form1.MinPayment.value);
	var lMax = CCents(document.Form1.MaxPayment.value);
	document.Form1.MinPrice.value = Calculate(lMin)
	document.Form1.MaxPrice.value = Calculate(lMax)
	
	if (lMin!=0 && !isNaN(lMin)) 
	document.Form1.MinPrice1.value= Calculate(lMin)
	else
	document.Form1.MinPrice1.value=""
	
	if (lMax!=99000 && !isNaN(lMax))
	document.Form1.MaxPrice1.value = Calculate(lMax)
	else
	document.Form1.MaxPrice1.value=""
	
	}
	
	function breakPayments()
	{
	var SV=document.Form1.PaymentRanges.value;
	var dPos=SV.indexOf("|");
	var Min,Max;
		if (dPos>=0)
		{
			Min=SV.substring(0,dPos)
			Max=SV.substring(dPos+1,SV.length)
			document.Form1.MinPayment.value=Min
			document.Form1.MaxPayment.value=Max
		}
		else
		{
			document.Form1.MinPayment.value=""
			document.Form1.MaxPayment.value=""
		}		
		CalcLoan()
	}
	
	function GetErrMsg()
	{
	
		if(document.getElementById("MustBeNumeric")!=null)
		{
		 return document.getElementById("MustBeNumeric").value;
		}
		
		else
		 return " Must be numeric.";
	}