//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Mortgage Rate Comparison Calculator
//2003 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/12/2003
//Last Modified: 01/12/2003
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:5114-1170-91-1128
//Commercial Licence Date:2007-12-22
//*******************************************



function stripNum(num) {

   num=num.toString();


   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;

   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }

   for(i = 0; i <= len; i++) {

      b = num.substring(i,i+1);

      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == "9" || b == ".") {
         rnum = rnum + "" + b;

      }

   }

   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }

   rnum = Number(rnum);

   return rnum;

}



function computeMonthlyPayment(prin, numPmts, intRate) {

var pmtAmt = 0;

if(intRate == 0) {
   pmtAmt = prin / numPmts;
} else {
   
   if (intRate >= 1.0) {
     intRate = intRate / 100.0;
   }
   intRate /= 12;

   var pow = 1;
   for (var j = 0; j < numPmts; j++)
      pow = pow * (1 + intRate);

   pmtAmt = (prin * pow * intRate) / (pow - 1);

}

return pmtAmt;

}




function computeFixedInterestCost(principal, intRate, pmtAmt) { 

   var i = eval(intRate);
   if(i >= 1) {
   i /= 100;
   }
   i /= 12;

   var prin = eval(principal);
   var intPort = 0;
   var accumInt = 0;
   var prinPort = 0;
   var pmtCount = 0;
   var testForLast = 0;


   //CYCLES THROUGH EACH PAYMENT OF GIVEN DEBT
   while(prin > 0) {

      testForLast = (prin * (1 + i));

      if(pmtAmt < testForLast) {
         intPort = prin * i;
         accumInt = eval(accumInt) + eval(intPort);
         prinPort = eval(pmtAmt) - eval(intPort);
         prin = eval(prin) - eval(prinPort);
      } else {
      //DETERMINE FINAL PAYMENT AMOUNT
      intPort = prin * i;
      accumInt = eval(accumInt) + eval(intPort);
      prinPort = prin;
      prin = 0;
      }

      pmtCount = eval(pmtCount) + eval(1);

      if(pmtCount > 1000 || accumInt > 1000000000) {
         prin = 0;
      }

   }

return accumInt;

}




function formatNumberDec(num, places, comma) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    var myDecFact = 1;
    var myPlaces = 0;
    var myZeros = "";
    while(myPlaces < places) {
       myDecFact = myDecFact * 10;
       myPlaces = eval(myPlaces) + eval(1);
       myZeros = myZeros + "0";
    }
    
	onum=Math.round(num*myDecFact)/myDecFact;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal=myZeros;
	} else{
		decimal=Math.round((onum-integer)* myDecFact)
	}
	decimal=decimal.toString();
	if (decimal.length<places) {
        fillZeroes = places - decimal.length;
	   for (z=0;z<fillZeroes;z++) {
        decimal="0"+decimal;
        }
     }

   if(places > 0) {
      decimal = "." + decimal;
   }

   if(comma == 1) {
	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}


	finNum=tmpinteger+""+decimal;
   } else {
      finNum=integer+""+decimal;
   }

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}


function fillRates(form, lang) {

   if(form.startRate.value.length == 0) {
      alert("Please enter a starting interest before selecting the increment factor.");
      form.startRate.focus();
   } else {

      var VstartRate = stripNum(form.startRate.value);
      if(VstartRate < 1) {
        VstartRate = VstartRate * 100;
      }

      var Vincrement = form.increment.options[form.increment.selectedIndex].value;

      var Vrate_1 = VstartRate;
      form.rate_1.value = formatNumberDec(Vrate_1,3);

      var Vrate_2 = eval(Vrate_1) + eval(Vincrement);
      form.rate_2.value = formatNumberDec(Vrate_2,3);

      var Vrate_3 = eval(Vrate_2) + eval(Vincrement);
      form.rate_3.value = formatNumberDec(Vrate_3,3);

      var Vrate_4 = eval(Vrate_3) + eval(Vincrement);
      form.rate_4.value = formatNumberDec(Vrate_4,3);

      var Vrate_5 = eval(Vrate_4) + eval(Vincrement);
      form.rate_5.value = formatNumberDec(Vrate_5,3);

      computeForm(form, lang);

   }

}
      

function computeForm(form, lang) {

if(form.principal.value.length == 0) {
   alert("Please enter the amount of the mortgage.");
   form.principal.focus();
} else
if(form.numYears.value == "") {
  alert("Please enter mortgage's term in years.");
  form.numYears.focus();
} else {

var Vprin = stripNum(form.principal.value);
var VnumYears = stripNum(form.numYears.value);

var Vrate_1 = stripNum(form.rate_1.value);
var Vrate_2 = stripNum(form.rate_2.value);
var Vrate_3 = stripNum(form.rate_3.value);
var Vrate_4 = stripNum(form.rate_4.value);
var Vrate_5 = stripNum(form.rate_5.value);

var Vmonths = VnumYears * 12;

var VmoPmt_1 = computeMonthlyPayment(Vprin, Vmonths, Vrate_1);
form.moPmt_1.value = "$" + formatNumberDec(VmoPmt_1,0,1);

var VmoPmt_2 = computeMonthlyPayment(Vprin, Vmonths, Vrate_2);
form.moPmt_2.value = "$" + formatNumberDec(VmoPmt_2,0,1);

var VmoPmt_3 = computeMonthlyPayment(Vprin, Vmonths, Vrate_3);
form.moPmt_3.value = "$" + formatNumberDec(VmoPmt_3,0,1);

var VmoPmt_4 = computeMonthlyPayment(Vprin, Vmonths, Vrate_4);
form.moPmt_4.value = "$" + formatNumberDec(VmoPmt_4,0,1);

var VmoPmt_5 = computeMonthlyPayment(Vprin, Vmonths, Vrate_5);
form.moPmt_5.value = "$" + formatNumberDec(VmoPmt_5,0,1);


var VtotPrin_1 = Vprin;
form.totPrin_1.value = "$" + formatNumberDec(VtotPrin_1,0,1);

var VtotPrin_2 = Vprin;
form.totPrin_2.value = "$" + formatNumberDec(VtotPrin_2,0,1);

var VtotPrin_3 = Vprin;
form.totPrin_3.value = "$" + formatNumberDec(VtotPrin_3,0,1);

var VtotPrin_4 = Vprin;
form.totPrin_4.value = "$" + formatNumberDec(VtotPrin_4,0,1);

var VtotPrin_5 = Vprin;
form.totPrin_5.value = "$" + formatNumberDec(VtotPrin_5,0,1);


var VtotInt_1 = computeFixedInterestCost(Vprin, Vrate_1, VmoPmt_1);
VtotInt_1 = Math.round(VtotInt_1);
form.totInt_1.value = "$" + formatNumberDec(VtotInt_1,0,1);

var VtotInt_2 = computeFixedInterestCost(Vprin, Vrate_2, VmoPmt_2);
VtotInt_2 = Math.round(VtotInt_2);
form.totInt_2.value = "$" + formatNumberDec(VtotInt_2,0,1);

var VtotInt_3 = computeFixedInterestCost(Vprin, Vrate_3, VmoPmt_3);
VtotInt_3 = Math.round(VtotInt_3);
form.totInt_3.value = "$" + formatNumberDec(VtotInt_3,0,1);

var VtotInt_4 = computeFixedInterestCost(Vprin, Vrate_4, VmoPmt_4);
VtotInt_4 = Math.round(VtotInt_4);
form.totInt_4.value = "$" + formatNumberDec(VtotInt_4,0,1);

var VtotInt_5 = computeFixedInterestCost(Vprin, Vrate_5, VmoPmt_5);
VtotInt_5 = Math.round(VtotInt_5);
form.totInt_5.value = "$" + formatNumberDec(VtotInt_5,0,1);


var VtotPmts_1 = eval(VtotPrin_1) + eval(VtotInt_1);
form.totPmts_1.value = "$" + formatNumberDec(VtotPmts_1,0,1);

var VtotPmts_2 = eval(VtotPrin_2) + eval(VtotInt_2);
form.totPmts_2.value = "$" + formatNumberDec(VtotPmts_2,0,1);

var VtotPmts_3 = eval(VtotPrin_3) + eval(VtotInt_3);
form.totPmts_3.value = "$" + formatNumberDec(VtotPmts_3,0,1);

var VtotPmts_4 = eval(VtotPrin_4) + eval(VtotInt_4);
form.totPmts_4.value = "$" + formatNumberDec(VtotPmts_4,0,1);

var VtotPmts_5 = eval(VtotPrin_5) + eval(VtotInt_5);
form.totPmts_5.value = "$" + formatNumberDec(VtotPmts_5,0,1);

}
}


function createReport(form, lang) {

if(form.principal.value.length == 0) {
   alert("Please enter the amount of the mortgage.");
   form.principal.focus();
} else
if(form.numYears.value == "") {
  alert("Please enter mortgage's term in years.");
  form.numYears.focus();
} else {

computeForm(form, lang);

var Vprin = stripNum(form.principal.value);
var VnumYears = stripNum(form.numYears.value);

var termRows = "";

termRows += "<tr><td><font face='arial'><small>Monthly payment:</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.moPmt_1.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.moPmt_2.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.moPmt_3.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.moPmt_4.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'>";
termRows += "<small>" + form.moPmt_5.value + "</small></font></td></tr>";

termRows += "<tr><td><font face='arial'><small>Principal payments:</small></font></td>";
termRows += "<td align=right><font face='arial'>";
termRows += "<small>" + form.totPrin_1.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'>";
termRows += "<small>" + form.totPrin_2.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'>";
termRows += "<small>" + form.totPrin_3.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'>";
termRows += "<small>" + form.totPrin_4.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'>";
termRows += "<small>" + form.totPrin_5.value + "</small></font></td></tr>";

termRows += "<tr><td><font face='arial'><small>Interest payments:</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.totInt_1.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totInt_2.value + "</small></font></td><td align=right>";
termRows += "<font face='arial'><small>" + form.totInt_3.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totInt_4.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'>";
termRows += "<small>" + form.totInt_5.value + "</small></font></td></tr>";

termRows += "<tr><td><font face='arial'><small>Total payments:</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.totPmts_1.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totPmts_2.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.totPmts_3.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totPmts_4.value + "</small></font></td><td align=right>";
termRows += "<font face='arial'><small>" + form.totPmts_5.value + "</small></font></td></tr>";

var part1 = "<head><title>Mortgage Rate Comparison</title>";
part1 += "</head>" + "<body bgcolor= '#FFFFFF'><br><br><center><font face='arial'>";
part1 += "<big><strong>Mortgage Rate Comparison</strong></big></font></center>";

var part2 = "<center><table border=1 cellpadding=2 cellspacing=0><tr><td colspan=6>";
part2 += "<font face='arial'><small><b>Principal:";
part2 += " $" + formatNumberDec(Vprin,0,1) + "<br>Term: " + VnumYears + " years</b>";
part2 += "</small></font></td></tr><tr bgcolor='silver'><td><font face='arial'><small>";
part2 += "<b>Interest Rate</b></small></font></td><td align='center'><font face='arial'>";
part2 += "<small><b>" + form.rate_1.value + "%</b></small></font></td><td align='center'>";
part2 += "<font face='arial'><small><b>" + form.rate_2.value + "%</b></small></font></td>";
part2 += "<td align='center'><font face='arial'><small><b>" + form.rate_3.value + "%</b>";
part2 += "</small></font></td><td align='center'><font face='arial'><small>";
part2 += "<b>" + form.rate_4.value + "%</b></small></font></td><td align='center'>";
part2 += "<font face='arial'><small><b>" + form.rate_5.value + "%</b></small>";
part2 += "</font></td></tr>";

var part3 = ("" + termRows + "");

var part4 = "</table><br><center><form method='post'>";
part4 += "<input type='button' value='Close Window' onClick='window.close()'>";
part4 += "</form></center></body></html>";

var schedule = (part1 + "" + part2 + "" + part3 + part4 + "");

  reportWin = window.open("","","width=500,height=400,toolbar=yes,menubar=yes,scrollbars=yes");

  reportWin.document.write(schedule);

  reportWin.document.close();

   }

}
