<!--// "mortsav.js" A Fortnightly Mortgage Savings Calculator// currfmt.js contains identical formatDollar and padTextPrefix functionsfunction formatDollar(nValue) {    var sDollars = ""    var outDollars = ""    var sValue = new String(nValue)             //convert value to string    var decimalPos = sValue.indexOf(".")        //find position of decimal in the string    if (decimalPos == -1) decimalPos = sValue.length //if there is no decimal point    var dollars = sValue.substring(0,decimalPos) //digits of whole dollars    var dolLen = dollars.length                 //the lenght of the dollars string    //Add commas before each group of three digits (if any).    if (dolLen > 3) {        while (dolLen > 0) {            var tDollars = dollars.substring(dolLen-3,dolLen) //take last three digits, if any            if (tDollars.length == 3) {                 //if there are 3 digits in the portion                outDollars = "," + tDollars + outDollars //ad to comma and append other digits with commas                dolLen = dolLen - 3                     //lessen the left portion by 3 digits            }            else {                outDollars = tDollars + outDollars      //no comma if less than 3 digits left                dolLen = 0                      //stop the process            }        }        //Get rid of the leading comma (if any)        if (outDollars.substring(0,1) == ",")           //if the first character is ","            dollars = outDollars.substring(1,outDollars.length) //start with the next character        else            dollars = outDollars    }    //add $ at the begining and two cents digits at the end    var cents = sValue.substring(decimalPos + 1, decimalPos + 3) //take 2 digits after "." (no rounding)    if (cents.length == 1) cents = cents + "0"          //if only one cents digit    else if (cents.length == 0) cents = cents + "00"    //if no cents digits    var formatStr = "$" + dollars + "." + cents         //compose the output string    return formatStr}function padTextPrefix (InString, PadChar, DefLength)  {	if (InString.length>=DefLength)		return (InString);	OutString=InString	for (Count=InString.length; Count < DefLength; Count++)  {		OutString=PadChar+OutString;	}	return (OutString);}function Round2(amount) {	// Converts amount to a formatted string with NO leading dollar sign	// and rounded to 2 decimal places	var dollars = Math.floor(amount)+".";	var cents = 100*(amount-Math.floor(amount))+0.5;	result = dollars + Math.floor(cents/10) + Math.floor(cents%10);	return result;}function calcul8bi() {	// Performs monthly payment calcs and places results into fields on form	// First do traditional calculation of monthly payment amount	var d = document.forms[0]	var rPrincipal = (d.PurchPrice.value)-(d.Downpayment.value);	var rAnnualRate = (d.IntRate.value);	var rRateFactor = rAnnualRate  / 1200;	var rNumPmts = (d.LoanTerm.value) * 12;	var rNumerator = rRateFactor * Math.pow((1 + rRateFactor), rNumPmts);	var rDenominator = Math.pow((1 + rRateFactor), rNumPmts) - 1;	var rPayment = rPrincipal * rNumerator / rDenominator;	var rTaxes = (d.Taxes.value) / 12;	var rTotPayment = rPayment + rTaxes;	if (rPrincipal < 1000) {		alert ("Principal amount is too small.  Try again.");		return;	}	else if (rAnnualRate < 1) {		alert ("Interest rate less than 1%.  Try again.");		return;	}	else if (rAnnualRate >= 40) {		alert ("Interest rate greater than 40%.  Try again.");		return;	}	// Figure total interest for monthly mortgage	var rTotInterest = (rPayment * rNumPmts) - rPrincipal;	// Figure total interest and time to pay off if fortnightly	// by generating amortization schedule	var rBiPayment = rPayment / 2;	var rBiTaxes = (d.Taxes.value) / 26;	var rBiRateFactor = (d.IntRate.value) * 14 / 36500;	var rBiTotPayment = rBiPayment + rBiTaxes;	var i = 0;	var p = 0;	var rPrinBal = rPrincipal;	var rBiTotInt = 0;	var rBiNumPmts = 0;	while(rPrinBal >= (rBiPayment - 1)) {			// Interest for this payment        	i = rPrinBal * rBiRateFactor;			// Principal portion of payment			p = rBiPayment - i;			if(p < 1) {				// Principal reduction too small to pay off mortgage!				break;				}			// Total interest, number payments			rBiTotInt = rBiTotInt + i;			rBiNumPmts = rBiNumPmts + 1;        	rPrinBal = rPrinBal - p;       }	// alert(rTotInterest);	// alert(rBiTotInt);	// for testing, leave here	var rBiSavings = rTotInterest - rBiTotInt;	var rBiYears = (rBiNumPmts * 14 / 365.25);	// Display results	d.Payment.value = padTextPrefix(formatDollar(rTotPayment)," ",18);	d.BiPayment.value = padTextPrefix(formatDollar(rBiTotPayment)," ",18);	d.BiSavings.value = padTextPrefix(formatDollar(rBiSavings)," ",16);	d.BiYears.value = padTextPrefix(Round2(rBiYears)," ",9);	// Finally do PMI warning	if (rPrincipal > (d.PurchPrice.value) * .8) {		d.PMI_warning1.value = "Downpayment is less than 20%.";		d.PMI_warning2.value = "Required PMI may increase monthly payment.";	}	else {		d.PMI_warning1.value = "";		d.PMI_warning2.value = "";	}}function calcEstTaxes() {	// Calculate estimated taxes based on 3% of purchase price of house	var d = document.forms[0]	var rRate = d.TaxRate.value;	var rEstTax = (d.PurchPrice.value) * rRate / 100;	d.Taxes.value = Math.round(rEstTax);}function calcDown() {	// Calculate downpayment as percentage of purchase price	var d = document.forms[0]	var rDown = d.DownRate.value;	var rDownPer = (d.PurchPrice.value) * rDown / 100;	d.Downpayment.value = Math.round(rDownPer);}function resetform() {	// Reset field on form to default values	var d = document.forms[0]	d.PurchPrice.value = "";	d.Downpayment.value = "";	d.Taxes.value = "0";	d.LoanTerm.value = "30";	d.IntRate.value = "8";	d.Payment.value = "";	d.BiPayment.value = "";	d.BiSavings.value = "";	d.BiYears.value = "";	d.PMI_warning1.value = "";	d.PMI_warning2.value = "";}// End -->