﻿
function IsAlpha(sChar) {
	if(sChar.length != 1) {	return false;}
	var sChar1, sChar2;
	sChar1 = sChar.toLowerCase();
	sChar2 = sChar.toUpperCase();
	if (sChar1 == sChar2) {	return false;	} else {return true;}
}

function msg(s) {
    document.getElementById("calcMessages").innerHTML = s;
}

function IsNumer(sChar) {
	if(sChar.length != 1) {
		return false;
	}
	sNumbers = '0123456789';
	
	for(i=0; i<10; i++) {
		if(sNumbers.substr(i,1) == sChar) {
			return true;
		}
	}       
	
	return false;   
}       
function numFormat(elem, lead, sep) {
        if (elem.value == '') {
                elem.value = format('0', lead, sep);
                return true;
        }
        
        var value = parseInt(cleanNumber(elem.value), 10);
        
        if (lead == '') 
        {
                if (0 > value)
                {
                        value = 0;
                }
                
                if (value > 99999) 
                {
                        value = 99999;
                }
        }
        
        if (lead == '$')
        {
                if (0 > value)
                {
                        value = 0;
                }
                if (value > 100000000) 
                {
                        value = 100000000;
                }
        }
        
        if (isNaN(value)) {
                msg('Invalid number.  Please correct.');
                elem.value = format('0', lead, sep);
                elem.focus();
                return false;
        }
        elem.value = format(value, lead, sep);
        msg("");
        return true;
}
function format(value, lead, sep)
{
        var strValue = new String(value);
        var len = strValue.length;
        var n;
        var strRet = '';
        var ctChar = 3 - (len%3);
        if (ctChar == 3) ctChar =0;
        for (n=0; len > n; n++) {
                if (ctChar == 3) {
                        strRet += sep;
                        ctChar = 0;
                }
                ctChar++;
                strRet += strValue.substring(n,n+1)             
        }
        if (lead == '%') {
                return strRet + lead;
        }
        else {
                return lead + strRet;
        }
}
function percentrate(elem, lead)
{
        if (elem.value == '') return true;
        
        var strRet = '';
        var value = parseFloat(cleanNumber(elem.value));
        if (0 > value) value = 0;
        if (value > 100) value = 99.999;
        if (isNaN(value)) {
                elem.value = '';
                elem.focus();
                return false;
        }
        var strvalue = new String(value);
        if (value > 9.9999)
        {
                if(strvalue.length > 6)
                {
                        strRet = strvalue.substr(0, 6);
                }
                else
                {
                        if(strvalue.length == 5)
                        {
                                strRet = strvalue + '0';
                        }
                        else
                        {
                                if(strvalue.length == 4)
                                {
                                        strRet = strvalue + '00';
                                }
                                else
                                {
                                        if (strvalue.length == 2)
                                        {
                                                strRet = strvalue + '.000';
                                        }
                                        else
                                        {
                                                strRet = strvalue;
                                        }
                                }
                        }
                }
        }
        else
        {
                if(strvalue.length > 5)
                {
                        strRet = strvalue.substr(0, 5);
                }
                else
                {
                        if(strvalue.length == 4)
                        {
                                strRet = strvalue + '0';
                        }
                        else
                        {
                                if(strvalue.length == 3)
                                {
                                        strRet = strvalue + '00';
                                }
                                else
                                {
                                        if (strvalue.length == 1)
                                        {
                                                strRet = strvalue + '.000';
                                        }
                                        else
                                        {
                                                strRet = strvalue;
                                        }
                                }
                        }
                }
        }
        elem.value = strRet + lead;
        return true;
}
                
function replace(szBuf, szFind, szReplace, lStart)
{
        var lFind = 0;
        if (!lStart) lStart = 0;
        
        while (lFind != -1) {
                lFind = szBuf.indexOf(szFind, lStart);
                if (lFind != -1) {
                        szBuf = szBuf.substring(0,lFind) + szReplace + szBuf.substring(lFind + szFind.length);
                        lStart = lFind + szReplace.length;
                }
        }
        return szBuf;
}
function cleanNumber(strNum)
{
        if (!strNum) return strNum;
        strNum = replace(strNum, '$', '', 0);
        strNum = replace(strNum, ',', '', 0);
        strNum = replace(strNum, '%', '', 0);   
        return strNum;
}

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 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 initDollars(amt)
{
	return "$" + reformat(amt,0);
}
function initPayment()
{       
        // this is to calculate the monthly payment
        var lrate,lterm,pay,down;
	lrate=parseFloat(rate);
	lterm=parseFloat(term);
    if (!(document.Details.ap.value=="") && !(document.Details.ap.value==null))       
	{
		pay=getCleanNumber(document.Details.ap.value);
		down=getCleanNumber(document.Details.dp.value);
		
		if(pay - down <0)		{down = pay}
        return "$" + reformat(-pmtCalc(rate,term,pay-down,0),0);
	}
}       
    
function getCleanNumber(nVal)
{
        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)
{       
        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;
}
function fullCalcPayment()
{       
        var lrate,lterm;

        numFormat(document.Details.ap,'$',',');
        numFormat(document.Details.dp,'$',',');
        lrate = parseFloat(document.forms.Details.rate[document.Details.rate.selectedIndex].value)/12/100;
        lterm = parseFloat(document.forms.Details.term[document.Details.term.selectedIndex].value);
        rate = lrate;
        term = lterm;
        
        if (!(document.Details.ap.value=="") && !(document.Details.ap.value==null))       
        {
                if(getCleanNumber(document.Details.ap.value)-getCleanNumber(document.Details.dp.value) <0)
                {
                        msg("Down payment is larger than the asking price.");
                        return false;
                }
                document.getElementById("yreDetailMarketEstimatedPayment").innerHTML = "&nbsp;$" + reformat(-pmtCalc(rate,term,getCleanNumber(document.Details.ap.value)-getCleanNumber(document.Details.dp.value),0),0);
                msg("");
        }
        else
            msg("");
                return false;
}

