//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Mortgage Payment Calculator w/Amortization V1.1
//2003 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 08/28/2003
//Last Modified: 08/28/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-107-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 formatCurrency(num) {

var isNeg=0;

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

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	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;

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

	return finNum;
}




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 formatNumber(num) {

var isNeg=0;

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

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	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;

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

	return finNum;
}


function computeForm(form) {

if(document.calc.principal.value == 0 || document.calc.principal.value == "") {

   alert("Please enter the mortgage's principal amount.");

   document.calc.principal.focus();

} else

if(document.calc.intRate.value == 0 || document.calc.intRate.value == "") {

   alert("Please enter the mortgage's annual interest rate.");

   document.calc.intRate.focus();

} else

if(document.calc.numYears.value == 0 || document.calc.numYears.value == "") {

   alert("Please enter the mortgage's term in number of years.");

   document.calc.numYears.focus();

} else {

var Vprincipal = stripNum(document.calc.principal.value);

var VintRate = stripNum(document.calc.intRate.value);

var VnumYears = stripNum(document.calc.numYears.value);

var VannualTax = document.calc.annualTax.value;
var VmonthlyTax =0;
if(VannualTax == 0 || VannualTax == "") {

   VannualTax = 0;
   VmonthlyTax =0;

} else {

   VannualTax = stripNum(VannualTax);
   VmonthlyTax = VannualTax / 12;
   VmonthlyTax *= 100;
   VmonthlyTax = Math.round(VmonthlyTax);
   VmonthlyTax /= 100;
}



var VannualInsurance = document.calc.annualInsurance.value;
var VmonthlyInsurance = 0;
if(VannualInsurance == 0 || VannualInsurance == "") {

   VannualInsurance = 0;
   VmonthlyInsurance = 0;
} else {

   VannualInsurance = stripNum(VannualInsurance);
   VmonthlyInsurance = VannualInsurance / 12;
   VmonthlyInsurance *= 100;
   VmonthlyInsurance = Math.round(VmonthlyInsurance);
   VmonthlyInsurance /= 100;
}



var VmonthlyPMI = document.calc.monthlyPMI.value;

if(VmonthlyPMI == 0 || VmonthlyPMI == "") {

   VmonthlyPMI = 0;

} else {

   VmonthlyPMI = stripNum(VmonthlyPMI);

}

var VotherPmts = eval(VmonthlyTax) + eval(VmonthlyInsurance) + eval(VmonthlyPMI);


var VnumPmts = VnumYears * 12;



var VpmtAmt = computeMonthlyPayment(Vprincipal, VnumPmts, VintRate);
var VtotalMtgPmt = eval(VpmtAmt) + eval(VotherPmts);


document.calc.monthlyPI.value = formatCurrency(VpmtAmt);
document.calc.otherPmts.value = formatCurrency(VotherPmts);
document.calc.monthlyPmt.value = formatCurrency(VtotalMtgPmt);

   }

}



function monthlyAmortSched(form) {

if(document.calc.principal.value == 0 || document.calc.principal.value == "") {

   alert("Please enter the mortgage's principal amount.");

   document.calc.principal.focus();

} else

if(document.calc.intRate.value == 0 || document.calc.intRate.value == "") {

   alert("Please enter the mortgage's annual interest rate.");

   document.calc.intRate.focus();

} else

if(document.calc.numYears.value == 0 || document.calc.numYears.value == "") {

   alert("Please enter the mortgage's term in number of years.");

   document.calc.numYears.focus();

} else {





var Vprincipal = stripNum(document.calc.principal.value);

var VintRate = stripNum(document.calc.intRate.value);

var VnumYears = stripNum(document.calc.numYears.value);

var VannualTax = document.calc.annualTax.value;
var VmonthlyTax =0;
if(VannualTax == 0 || VannualTax == "") {

   VannualTax = 0;
   VmonthlyTax =0;

} else {

   VannualTax = stripNum(VannualTax);
   VmonthlyTax = VannualTax / 12;
   VmonthlyTax *= 100;
   VmonthlyTax = Math.round(VmonthlyTax);
   VmonthlyTax /= 100;
}



var VannualInsurance = document.calc.annualInsurance.value;
var VmonthlyInsurance = 0;
if(VannualInsurance == 0 || VannualInsurance == "") {

   VannualInsurance = 0;
   VmonthlyInsurance = 0;
} else {

   VannualInsurance = stripNum(VannualInsurance);
   VmonthlyInsurance = VannualInsurance / 12;
   VmonthlyInsurance *= 100;
   VmonthlyInsurance = Math.round(VmonthlyInsurance);
   VmonthlyInsurance /= 100;
}



var VmonthlyPMI = document.calc.monthlyPMI.value;

if(VmonthlyPMI == 0 || VmonthlyPMI == "") {

   VmonthlyPMI = 0;

} else {

   VmonthlyPMI = stripNum(VmonthlyPMI);

}

var VotherPmts = eval(VmonthlyTax) + eval(VmonthlyInsurance) + eval(VmonthlyPMI);


var VnumPmts = VnumYears * 12;



var pmtAmt = computeMonthlyPayment(Vprincipal, VnumPmts, VintRate);
var VtotalMtgPmt = eval(pmtAmt) + eval(VotherPmts);


var prin = Vprincipal;

var Vint = VintRate;

if(Vint >= 1) {

   Vint /= 100;

   }

Vint /= 12;



var nPer = VnumPmts;



var intPort = 0;

var accumInt = 0;

var prinPort = 0;

var accumPrin = 0;

var count = 0;

var pmtRow = "";

var pmtNum = 0;



var today = new Date();

var dayFactor = today.getTime();

var pmtDay = today.getDate();

var loanMM = today.getMonth() + 1;

var loanYY = today.getYear();
if(loanYY < 1900) {
   loanYY += 1900;
}

var loanDate = (loanMM + "/" + pmtDay + "/" + loanYY);

var monthMS = 86400000 * 30.4;

var pmtDate = 0;

var displayPmtAmt = 0;

var accumYearPrin = 0;
var accumYearInt = 0;

while(count < nPer) {

   if(count < (nPer - 1)) {

      intPort = prin * Vint;
      intPort *= 100;
      intPort = Math.round(intPort);
      intPort /= 100;

      accumInt = eval(accumInt) + eval(intPort);
      accumYearInt = eval(accumYearInt) + eval(intPort);

      prinPort = eval(pmtAmt) - eval(intPort);
      prinPort *= 100;
      prinPort = Math.round(prinPort);
      prinPort /= 100;

      accumPrin = eval(accumPrin) + eval(prinPort);
      accumYearPrin = eval(accumYearPrin) + eval(prinPort);

      prin = eval(prin) - eval(prinPort);

      displayPmtAmt = eval(prinPort) + eval(intPort);

   } else {

      intPort = prin * Vint;
      intPort *= 100;
      intPort = Math.round(intPort);
      intPort /= 100;

      accumInt = eval(accumInt) + eval(intPort);
      accumYearInt = eval(accumYearInt) + eval(intPort);

      prinPort = prin;

      accumPrin = eval(accumPrin) + eval(prinPort);
      accumYearPrin = eval(accumYearPrin) + eval(prinPort);

      prin = 0;

      //pmtAmt = eval(intPort) + eval(prinPort);
      displayPmtAmt = eval(prinPort) + eval(intPort);
   }

   count = eval(count) + eval(1);

   pmtNum = eval(pmtNum) + eval(1);

   dayFactor = eval(dayFactor) + eval(monthMS);

   pmtDate = new Date(dayFactor);

   pmtMonth = pmtDate.getMonth();

   pmtMonth = pmtMonth + 1;

   pmtYear = pmtDate.getYear();
   if(pmtYear < 1900) {
      pmtYear += 1900;
   }


   pmtString = (pmtMonth + "/" + pmtDay + "/" + pmtYear);

   pmtRow += "<tr><td align=right><font face='arial'><small>" + pmtNum + "</small></font>";
   pmtRow += "</td><td align=right><font face='arial'><small>" + pmtString + "</small>";
   pmtRow += "</font></td><td align=right><font face='arial'>";
   pmtRow += "<small>" + formatCurrency(prinPort) + "</small></font></td>";
   pmtRow += "<td align=right><font face='arial'>";
   pmtRow += "<small>" + formatCurrency(intPort) + "</small></font></td>";
   pmtRow += "<td align=right><font face='arial'>";
   pmtRow += "<small>" + formatCurrency(displayPmtAmt) + "</small></font></td>";
   pmtRow += "<td align=right><font face='arial'>";
   pmtRow += "<small>" + formatCurrency(prin) + "</small></font></td></tr>";

   if(pmtMonth == 12 || count == nPer) {

   pmtRow += "<tr bgcolor='#dddddd'><td align=right><font face='arial'><small>Total</small>";
   pmtRow += "</font></td><td align=left><font face='arial'><small>" + pmtYear + "</small>";
   pmtRow += "</font></td><td align=right><font face='arial'>";
   pmtRow += "<small>" + formatCurrency(accumYearPrin) + "</small></font></td>";
   pmtRow += "<td align=right><font face='arial'>";
   pmtRow += "<small>" + formatCurrency(accumYearInt) + "</small></font></td>";
   pmtRow += "<td align=right><font face='arial'><small> </small></font></td>";
   pmtRow += "<td align=right><font face='arial'><small> </small></font></td></tr>";

   accumYearPrin = 0;
   accumYearInt = 0;

   }

      if(count > 600) {

         alert("Using your current entries you will never pay off this loan.");

         break;

         } else {

         continue;

         }

    }



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



var part2 = "<center><table border=1 cellpadding=2 cellspacing=0><tr>";
part2 += "<td colspan=6><font face='arial'><small><b>Loan ";
part2 += "Date: " + loanDate + "<br>Principal: $" + formatNumber(Vprincipal) + "<br>";
part2 += "# of Payments: " + nPer + "<br>Interest Rate: " + formatNumber(VintRate) + "%";
part2 += "<br>Payment: $" + formatNumber(pmtAmt) + "</b></small></font></td></tr>";
part2 += "<tr><td colspan=6><center><font face='arial'><b>Schedule of Payments</b>";
part2 += "</font><br><font face='arial'><small><small>Please allow for slight ";
part2 += "rounding differences.</small></small></font></center></td></tr>";
part2 += "<tr bgcolor='silver'><td align='center'><font face='arial'><small>";
part2 += "<b>Pmt #</b></small></font></td><td align='center'><font face='arial'>";
part2 += "<small><b>Date</b></small></font></td><td align='center'>";
part2 += "<font face='arial'><small><b>Principal</b></small></font></td>";
part2 += "<td align='center'><font face='arial'><small><b>Interest</b></small>";
part2 += "</font></td><td align='center'><font face='arial'><small><b>Payment</b>";
part2 += "</small></font></td><td align='center'><font face='arial'><small>";
part2 += "<b>Balance</b></small></font></td></tr>";



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

var totalPmts = eval(accumPrin) + eval(accumInt);

var part4 = "<tr><td colspan='2'><font face='arial'><small><b>Grand Total</b>";
part4 += "</small></font></td><td align=right><font face='arial'><small>";
part4 += "<b>" + formatCurrency(accumPrin) + "</b></small></font></td>";
part4 += "<td align=right><font face='arial'><small>";
part4 += "<b>" + formatCurrency(accumInt) + "</b></small></font></td>";
part4 += "<td><font face='arial'><small>";
part4 += "<b>" + formatCurrency(totalPmts,2,1) + "</b></small></font></td>";
part4 += "<td> </td></tr></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();



   }



}


//GIVE ENTRY INSTRUCTIONS
function help01(form) {
var help_txt = "ENTER: The total amount you would be ";
help_txt += "borrowing to purchase the home.";
document.calc.enterHelp.value = help_txt;
}
function help01_es(form) {
var help_txt = "ENTER: The total amount you would be ";
help_txt += "borrowing to purchase the home.";
document.calc.enterHelp.value = help_txt;
}

function help02(form) {
var help_txt = "ENTER: The annual interest rate you expect to pay on this mortgage. ";
help_txt += "You can enter the rate either as a percentage (8.25) ";
help_txt += "or as a decimal (.0825), whichever you prefer.";
document.calc.enterHelp.value = help_txt;
}
function help02_es(form) {
var help_txt = "ENTER: The annual interest rate you expect to pay on this mortgage. ";
help_txt += "You can enter the rate either as a percentage (8.25) ";
help_txt += "or as a decimal (.0825), whichever you prefer.";
document.calc.enterHelp.value = help_txt;
}

function help03(form) {
var help_txt = "ENTER: The number of years you will be ";
help_txt += "financing the home loan for.";
document.calc.enterHelp.value = help_txt;
}
function help03_es(form) {
var help_txt = "ENTER: The number of years you will be ";
help_txt += "financing the home loan for.";
document.calc.enterHelp.value = help_txt;
}


function help04(form) {
var help_txt = "ENTER: The annual property tax payment you expect to pay. ";
help_txt += "As a rule of thumb, you can expect to pay 1.1% ";
help_txt += "(home price X .011) of the purchase price per year.";
document.calc.enterHelp.value = help_txt;
}
function help04_es(form) {
var help_txt = "ENTER: The annual property tax payment you expect to pay. ";
help_txt += "As a rule of thumb, you can expect to pay 1.1% ";
help_txt += "(home price X .011) of the purchase price per year.";
document.calc.enterHelp.value = help_txt;
}


function help05(form) {
var help_txt = "ENTER: The annual homeowner's insurance payment you expect to pay. ";
help_txt += "As a rule of thumb, you can expect to pay 1.5% ";
help_txt += "(home price X .015) of the purchase price per year.";
document.calc.enterHelp.value = help_txt;
}
function help05_es(form) {
var help_txt = "ENTER: The annual homeowner's insurance payment you expect to pay. ";
help_txt += "As a rule of thumb, you can expect to pay 1.5% ";
help_txt += "(home price X .015) of the purchase price per year.";
document.calc.enterHelp.value = help_txt;
}


function help06(form) {
var help_txt = "ENTER: The monthly Private Mortgage Insurance (PMI) you expect to pay. ";
help_txt += "If your downpayment is less than 20% of the value of the home you are buying, you ";
help_txt += "may be required to pay mortgage insurance of somewhere between 0.02% ";
help_txt += "and 0.07% of your principal balance each month (e.g, principal balance X .00043).";
document.calc.enterHelp.value = help_txt;
}
function help06_es(form) {
var help_txt = "ENTER: The monthly Private Mortgage Insurance (PMI) you expect to pay. ";
help_txt += "If your downpayment is less than 20% of the value of the home you are buying, you ";
help_txt += "may be required to pay mortgage insurance of somewhere between 0.02% ";
help_txt += "and 0.07% of your principal balance each month (e.g, principal balance X .00043).";
document.calc.enterHelp.value = help_txt;
}

//GIVE RESULT EXPLANATIONS
function help07(form) {
var help_txt = "RESULT: This is how much your monthly principal and ";
help_txt += "interest payment will be.";
document.calc.resultHelp.value = help_txt;
}
function help07_es(form) {
var help_txt = "RESULT: This is how much your monthly principal and ";
help_txt += "interest payment will be.";
document.calc.resultHelp.value = help_txt;
}

function help08(form) {
var help_txt = "RESULT: This is the total of your other monthly ";
help_txt += "payments (taxes, insurance & PMI).";
document.calc.resultHelp.value = help_txt;
}
function help08_es(form) {
var help_txt = "RESULTADO: Esta es la suma total de sus otros ";
help_txt += "pagos mensuales (impuestos, seguros y PMI).";
document.calc.resultHelp.value = help_txt;
}

function help09(form) {
var help_txt = "RESULT: This the total of all monthly payments ";
help_txt += "related to your mortgage.";
document.calc.resultHelp.value = help_txt;
}
function help09_es(form) {
var help_txt = "RESULT: This the total of all monthly payments ";
help_txt += "related to your mortgage.";
document.calc.resultHelp.value = help_txt;
}

