var cache = '';
var blnPageLoaded = false;

function HiliteField(oField) {
	if(blnPageLoaded) {
		var intBeat = 250;
		var strStylePath = 'document.frmMort.' + oField.name + (document.all ? '.style' : '') + '.backgroundColor';

		setTimeout(strStylePath + ' = "#CCCCFF";', 1);
		setTimeout(strStylePath + ' = "#FFFFFF";', intBeat);
		setTimeout(strStylePath + ' = "#CCCCFF";', intBeat*2);
		setTimeout(strStylePath + ' = "#FFFFFF";', intBeat*3);
		setTimeout(strStylePath + ' = "#CCCCFF";', intBeat*4);
		setTimeout(strStylePath + ' = "#FFFFFF";', intBeat*5);
	}
}


function CheckFields(theForm) {
	var errorMsg = '';
	if(!CheckDollarField(theForm.price))
		errorMsg += ' Property Price\n';
	if(!CheckDollarField(theForm.downpay))
		errorMsg += ' Down Payment dollar\n';
	if(!CheckFloatField(theForm.DownPercent))
		errorMsg += ' Down Payment percent\n';
	if(!CheckIntField(theForm.termMonths))
		errorMsg += ' Loan Term in months\n';
	if(!CheckFloatField(theForm.termYrs))
		errorMsg += ' Loan Term in years\n';
	if(!CheckFloatField(theForm.intYear))
		errorMsg += ' Interest Rate\n';

	if(errorMsg == '')
		return true;
	else {
		alert('Please enter:\n' + errorMsg);
		return false;
	}
}


function CheckFloatField(field) {
	var val = field.value;

	// take trailing "0"s after decimal point
	if(val.indexOf(".") != -1) {
		while(val.charAt(val.length-1) == "0")
			val = val.substring(0,val.length-1);
		if(val.charAt(val.length-1) == ".")
			val = val.substring(0,val.length-1);
	}

	if("" + parseFloat(val) != val)
		return false;
	else
		return true;
}


function CheckIntField(field) {
	var val = field.value;

	if(isNaN(val))
		return false;
	else {
		field.value = '' + parseInt(val)
		return true;
	}
}


function CheckDollarField(field) {
	var flt = ReadDollarField(field);

	if(isNaN(flt))
		return false;
	else {
		str = FloatToDollarString(flt);
		field.value = str;
		return true;
	}
}


function ReadDollarField(field) {
	var str = field.value;
	if(str.charAt(0) == "$")
		str = str.substring(1, str.length);

	var pos = str.lastIndexOf(",");
	while(pos != -1) {
		str = str.substring(0,pos) + str.substring(pos+1, str.length);
		pos = str.lastIndexOf(",", pos);
	}

	return parseFloat(str);
}


function FloatToDollarString(flt) {

	// round to nearest dollar
	var str = "" + Math.round(flt)

	// add commas
	pos = str.length;  // str.indexOf(".");
	pos -= 4;
	while(pos >= 0) {
		str = str.substring(0,pos+1) + "," + str.substring(pos+1, str.length);
		pos -= 3;
	}

	return str;
}


function recalcTermMonths(frm) {
	var tYr = parseFloat(frm.termYrs.value);
	var tMon = Math.round(tYr * 12.0);
	tYr = parseFloat(tMon) / 12.0;
	frm.termYrs.value = "" + tYr;
	frm.termMonths.value = "" + tMon;
	HiliteField(frm.termMonths);
}


function recalctermYrs(frm) {
	var tMon = parseInt(frm.termMonths.value);
	var tYr = parseFloat(tMon) / 12.0;
	frm.termYrs.value = "" + tYr;
	frm.termMonths.value = "" + tMon;
	HiliteField(frm.termYrs);
}


function RecalcMonthlyPay(frm) {
	var Principle  = ReadDollarField(frm.price) - ReadDollarField(frm.downpay);
	var AnnualInt  = parseFloat(frm.intYear.value);
	var MonthlyInt = AnnualInt / (12.0 * 100.0);
	var LenMonths  = parseInt(frm.termMonths.value);

	if(MonthlyInt == 0)
		var MonthlyPay = Principle / LenMonths;
	else
		var MonthlyPay = Principle * ( MonthlyInt / ( 1 - Math.pow((1 + MonthlyInt), -LenMonths) ) );
	MonthlyPay = Math.round(MonthlyPay * 100) / 100;

	frm.payMonth.value = FloatToDollarString(MonthlyPay);
	HiliteField(frm.payMonth);
}

function RecalcDownPercent(frm) {
	var HomePrice  = ReadDollarField(frm.price);
	var DownPay = ReadDollarField(frm.downpay);
	var DownPercent = 100 * DownPay / HomePrice;

	if(DownPercent >= 0  &&  DownPercent <= 100) {
		var DownPercentStr = "" + DownPercent;

		var pos = DownPercentStr.indexOf(".");
		if(DownPercentStr.length > pos + 4)
			DownPercentStr = DownPercentStr.substring(0,pos+4);

		frm.DownPercent.value = DownPercentStr;
	}
	else if(DownPercent < 0) {
		frm.DownPercent.value = "0";
		RecalcDownPayAmount(frm);
	}
	else {
		frm.DownPercent.value = "100";
		RecalcDownPayAmount(frm);
	}
	HiliteField(frm.DownPercent);
}


function RecalcDownPay(frm) {
	var AnnualInt  = parseFloat(frm.intYear.value);
	var MonthlyInt = AnnualInt / (12.0 * 100.0);
	var LenMonths  = parseInt(frm.termMonths.value);
	var MonthlyPay = ReadDollarField(frm.payMonth);
	var Principle  = ReadDollarField(frm.price) - ReadDollarField(frm.downpay);
	var OldDownPay = ReadDollarField(frm.downpay);
	var EffPrinciple

	if(MonthlyInt == 0)
		EffPrinciple = MonthlyPay * LenMonths;
	else
		EffPrinciple = MonthlyPay * ((1 - Math.pow((1 + MonthlyInt), -LenMonths)) / MonthlyInt);

	var NewDownPay = OldDownPay + (Principle - EffPrinciple);
	frm.downpay.value = "" + NewDownPay;
	CheckDollarField(frm.downpay);
	HiliteField(frm.downpay);

	RecalcDownPercent(frm);
	RecalcMonthlyPay(frm);
}




function RecalcDownPayAmount(frm) {
	var HomePrice  = ReadDollarField(frm.price);
	var DownPercent = parseFloat(frm.DownPercent.value);
	if(DownPercent < 0) {
		frm.DownPercent.value = "0";
		RecalcDownPayAmount(frm);
	}
	else if(DownPercent > 100) {
		frm.DownPercent.value = "100";
		RecalcDownPayAmount(frm);
	}
	else {
		var DownPay = HomePrice * DownPercent / 100;
		DownPay = FloatToDollarString(DownPay);
		frm.downpay.value = "" + DownPay;
		HiliteField(frm.downpay);
	}
}


function RecalcCostPerUnit(frm) {
	var Cost  = ReadDollarField(frm.price);
	var NUnits  = parseInt(frm.units.value);
	var CostPerUnit = Cost / NUnits;
	frm.unitcost.value = FloatToDollarString(CostPerUnit);
	HiliteField(frm.unitcost);
}

function resetIt() {
document.frmMort.intYear.value="8.00";

}
