//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Mortgage Term Comparison Calculator
//2002 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 12/14/2002
//Last Modified: 12/14/2002
//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 #:5782-1237-89-1198
//Commercial Licence Date:2008-09-11
//*******************************************



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 computeForm(form) {

if(form.principal.value.length == 0) {
   alert("Please enter the amount of the mortgage.");
   form.principal.focus();
} else
if(form.intRate.value == "") {
  alert("Please enter mortgage's annual interest rate.");
  form.intRate.focus();
} else {


var Vprin = stripNum(form.principal.value);
var VintRate = stripNum(form.intRate.value);

var VmoPmt_10 = computeMonthlyPayment(Vprin, 120, VintRate);
form.moPmt_10.value = "$" + formatNumberDec(VmoPmt_10,0,1);

var VmoPmt_15 = computeMonthlyPayment(Vprin, 180, VintRate);
form.moPmt_15.value = "$" + formatNumberDec(VmoPmt_15,0,1);

var VmoPmt_20 = computeMonthlyPayment(Vprin, 240, VintRate);
form.moPmt_20.value = "$" + formatNumberDec(VmoPmt_20,0,1);

var VmoPmt_25 = computeMonthlyPayment(Vprin, 300, VintRate);
form.moPmt_25.value = "$" + formatNumberDec(VmoPmt_25,0,1);

var VmoPmt_30 = computeMonthlyPayment(Vprin, 360, VintRate);
form.moPmt_30.value = "$" + formatNumberDec(VmoPmt_30,0,1);


var VtotPrin_10 = Vprin;
form.totPrin_10.value = "$" + formatNumberDec(VtotPrin_10,0,1);

var VtotPrin_15 = Vprin;
form.totPrin_15.value = "$" + formatNumberDec(VtotPrin_15,0,1);

var VtotPrin_20 = Vprin;
form.totPrin_20.value = "$" + formatNumberDec(VtotPrin_20,0,1);

var VtotPrin_25 = Vprin;
form.totPrin_25.value = "$" + formatNumberDec(VtotPrin_25,0,1);

var VtotPrin_30 = Vprin;
form.totPrin_30.value = "$" + formatNumberDec(VtotPrin_30,0,1);


var VtotInt_10 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_10);
VtotInt_10 = Math.round(VtotInt_10);
form.totInt_10.value = "$" + formatNumberDec(VtotInt_10,0,1);

var VtotInt_15 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_15);
VtotInt_15 = Math.round(VtotInt_15);
form.totInt_15.value = "$" + formatNumberDec(VtotInt_15,0,1);

var VtotInt_20 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_20);
VtotInt_20 = Math.round(VtotInt_20);
form.totInt_20.value = "$" + formatNumberDec(VtotInt_20,0,1);

var VtotInt_25 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_25);
VtotInt_25 = Math.round(VtotInt_25);
form.totInt_25.value = "$" + formatNumberDec(VtotInt_25,0,1);

var VtotInt_30 = computeFixedInterestCost(Vprin, VintRate, VmoPmt_30);
VtotInt_30 = Math.round(VtotInt_30);
form.totInt_30.value = "$" + formatNumberDec(VtotInt_30,0,1);


var VtotPmts_10 = eval(VtotPrin_10) + eval(VtotInt_10);
form.totPmts_10.value = "$" + formatNumberDec(VtotPmts_10,0,1);

var VtotPmts_15 = eval(VtotPrin_15) + eval(VtotInt_15);
form.totPmts_15.value = "$" + formatNumberDec(VtotPmts_15,0,1);

var VtotPmts_20 = eval(VtotPrin_20) + eval(VtotInt_20);
form.totPmts_20.value = "$" + formatNumberDec(VtotPmts_20,0,1);

var VtotPmts_25 = eval(VtotPrin_25) + eval(VtotInt_25);
form.totPmts_25.value = "$" + formatNumberDec(VtotPmts_25,0,1);

var VtotPmts_30 = eval(VtotPrin_30) + eval(VtotInt_30);
form.totPmts_30.value = "$" + formatNumberDec(VtotPmts_30,0,1);

}
}


function createReport(form) {

if(form.principal.value.length == 0) {
   alert("Please enter the amount of the mortgage.");
   form.principal.focus();
} else
if(form.intRate.value == "") {
  alert("Please enter mortgage's annual interest rate.");
  form.intRate.focus();
} else {

computeForm(form);

var Vprin = stripNum(form.principal.value);
var VintRate = stripNum(form.intRate.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_10.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.moPmt_15.value + "</small></font></td><td align=right>";
termRows += "<font face='arial'><small>" + form.moPmt_20.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.moPmt_25.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.moPmt_30.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_10.value + "</small></font></td><td align=right>";
termRows += "<font face='arial'><small>" + form.totPrin_15.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.totPrin_20.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totPrin_25.value + "</small></font></td><td align=right>";
termRows += "<font face='arial'><small>" + form.totPrin_30.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_10.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totInt_15.value + "</small></font></td><td align=right>";
termRows += "<font face='arial'><small>" + form.totInt_20.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.totInt_25.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totInt_30.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_10.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totPmts_15.value + "</small></font></td><td align=right>";
termRows += "<font face='arial'><small>" + form.totPmts_20.value + "</small></font></td>";
termRows += "<td align=right><font face='arial'><small>" + form.totPmts_25.value + "</small>";
termRows += "</font></td><td align=right><font face='arial'>";
termRows += "<small>" + form.totPmts_30.value + "</small></font></td></tr>";

var part1 = "<head><title>Mortgage Term Comparison</title></head>" + "<body bgcolor= '#FFFFFF'>";
part1 += "<br><br><center><font face='arial'><big><strong>Mortgage Term Comparison</strong>";
part1 += "</big></font></center>";

var part2 = "<center><table border=1 cellpadding=2 cellspacing=0><tr><td colspan=6>";
part2 += "<font face='arial'><small><b>Principal: $" + formatNumberDec(Vprin,0,1) + "<br>";
part2 += "Interest Rate: " + formatNumberDec(VintRate,3,1) + "%</b></small></font></td>";
part2 += "</tr><tr bgcolor='silver'><td align='center'><font face='arial'><small><b>";
part2 += "Description</b></small></font></td><td align='center'><font face='arial'>";
part2 += "<small><b>10 Years</b></small></font></td><td align='center'><font face='arial'>";
part2 += "<small><b>15 Years</b></small></font></td><td align='center'><font face='arial'>";
part2 += "<small><b>20 Years</b></small></font></td><td align='center'><font face='arial'>";
part2 += "<small><b>25 Years</b></small></font></td><td align='center'><font face='arial'>";
part2 += "<small><b>30 Years</b></small></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();

   }

}
