
//<!--
function pmt(rate, nper, pv, fv)
{
        var rVal;
        if (rate==0)
                rVal=-(fv + pv)/nper;
        else {
                ir=Math.pow(1 + rate,nper);
                rVal=-((rate * (fv + ir * pv))/(ir-1));
        }
        return rVal;
}
function getRate(rate)
{
        //calculate the interest rate
        var vRate;
        vRate = (parseFloat(rate.substring(rate.lastIndexOf("..")+2,rate.length))/100)/12;
        return vRate;
}
function getTerm(term)
{
        //calculate the term of the interest interest
        var vTerm;
        vTerm = parseInt(term.substring(0,term.indexOf(".")))*12;
        return vTerm;
}
function reformat (expr,decplaces)
{
        var str = "" + Math.round(eval(expr)*Math.pow(10,decplaces));
        while (str.length <=decplaces)
        {
                str="0" + str;
        }
        var decpoint = str.length - decplaces;
        var strUnit= str.substring(0,decpoint);
        var strTemp;
        var strUnitDisp="";
        var strPoint = str.substring(decpoint,str.length);
        while (strUnit.length>3)
        {
                strUnitDisp = "," + strUnit.substring(strUnit.length - 3,strUnit.length)+strUnitDisp;
                strUnit = strUnit.substring(0,strUnit.length - 3);              
        }       
        if (strUnit.length>0)
                strUnitDisp = strUnit + strUnitDisp;
        else
                strUnitDisp = strUnitDisp.substring(1,strUnit.length);
        
        return strUnitDisp;
}
function initLoan(loan)
{
	return "$" + reformat(loan,0);
}
function initDown(down)
{
	return "$" + reformat(down,0);
}
function initPayment()
{       
        // this is to calculate the monthly payment
        var lrate,lterm,pay,down;
	lrate=parseFloat(rate);
	lterm=parseFloat(term);
        if (!(document.lf.ap.value=="") && !(document.lf.ap.value==null))       
	{
		pay=getCleanNumber(document.lf.ap.value);
		down=getCleanNumber(document.lf.dp.value);
		if(pay - down <0)
                {
                        alert("To calculate a payment, you must enter a down payment that is less than the asking price.");
                        return "bad1";
                }
	        return "$" + reformat(-pmtCalc(rate,term,pay-down,0),0);
	}
	return "bad2";
}       
function calcPayment(vthis)
{       
        // this is to calculate the monthly payment
        var lrate,lterm;
        if (vthis.name!="ir")
        {
                if (!(vthis.value=="") && !(vthis.value == null))
                        numFormat(vthis,'$',',');
                        
                lrate=parseFloat(rate);
                lterm=parseFloat(term);
        }
        else
        {
                lrate = parseFloat(getRate(document.forms.lf.ir[document.lf.ir.selectedIndex].value));
                lterm = parseFloat(getTerm(document.forms.lf.ir[document.lf.ir.selectedIndex].value));
                
                rate = lrate;
                term = lterm;
        }
        if (!(document.lf.ap.value=="") && !(document.lf.ap.value==null))       
        {
                if(getCleanNumber(document.lf.ap.value)-getCleanNumber(document.lf.dp.value) <0)
                {
                        alert("To calculate a payment, you must enter a down payment that is less than the asking price.");
                        vthis.value = '';
                        vthis.focus();
                        return false;
                }
                document.lf.ep.value = "$" + reformat(-pmtCalc(rate,term,getCleanNumber(document.lf.ap.value)-getCleanNumber(document.lf.dp.value),0),0);
        }
        else
                return false;
}       
function getCleanNumber(nVal)
{
        //convert field value to numeric value
        if(nVal != "")
        {
                nVal = nVal.replace(/\$/,"");
		nVal = nVal.replace(/,/gi,"");
                if(isNaN(parseFloat(nVal)))
                        nVal = "0";
        }
        else
        {
                nVal = "0";
        }
                
        return parseFloat(nVal);
}
function pmtCalc(rate, nper, pv, fv)
{       
        //calculate payment (called by payment calculation subrouting)
        var rVal;
        if (isNaN(rate) || isNaN(nper))
                return ""
        
        if (rate==0)
        {
                rVal=-(fv + pv)/nper;
        }
        else
        {
                var ir;
                ir = Math.pow(1+rate,nper);
                rVal=-((rate * (fv + ir * pv))/(ir-1));
        }
                
        return rVal;
}
//-->
