//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Loan Payment Calculator with Amortization Schedule V5
//2008 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 07/14/2008
//Last Modified: 07/14/2008
//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-158-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 computeIntervalPayment(prin, numPmts, intRate, pmtInt) {

if (intRate > 1.0) {
  intRate = intRate / 100.0;
}
intRate /= pmtInt;

var pow = 1;
for (var j = 0; j < numPmts; j++)
   pow = pow * (1 + intRate);

var pmtAmt = (prin * pow * intRate) / (pow - 1);

return pmtAmt;

}




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 calc_down_1(form) {

   form.Hdown_type.value = 1;

   var v_price = stripNum(form.price.value);
   var v_down = stripNum(form.down.value);
   if(v_down < 25) {
      v_down = v_down / 100 * v_price;
      form.down.value = formatNumberDec(v_down,2,0);
   }
   var v_principal = 0;
   if(v_price > 0 && v_down > 0) {
      v_principal = eval(v_price) - eval(v_down);
   }

   form.principal.value = formatNumberDec(v_principal,2,1);

   clear_results(document.lpc_5);

}

function calc_down_2(form) {

   form.Hdown_type.value = 2;

   var v_price = stripNum(form.price.value);
   var v_down = stripNum(form.down.value);
   if(v_down > 25) {
      v_down = v_down / v_price * 100;
      form.down.value = formatNumberDec(v_down,2,0);
   }
   var v_principal = 0;
   if(v_price > 0 && v_down > 0) {
      v_principal = eval(v_price) - eval(v_down / 100 * v_price);
   }

   form.principal.value = formatNumberDec(v_principal,2,1);

   clear_results(document.lpc_5);

}

function calc_prin(form) {

   var v_price = stripNum(form.price.value);
   var v_down = stripNum(form.down.value);
   var v_principal = 0;

   if(form.Hdown_type.value == 1) {
      v_principal = eval(v_price) - eval(v_down);
   } else {
      v_principal = eval(v_price) - eval(v_down / 100 * v_price);
   }

   form.principal.value = formatNumberDec(v_principal,2,1);

   clear_results(document.lpc_5);
}

function computeForm(form) {

   if(form.principal.value == "" || form.principal.value == 0) {
      alert("Please enter an amount in Line #1.");
      form.principal.focus();
   } else
      if(form.rate.value == "" || form.rate.value == 0) {
      alert("Please enter an amount in Line #2.");
      form.rate.focus();
   } else {

      var v_principal = stripNum(form.principal.value);
      form.r_principal.value = "$" + formatNumberDec(v_principal,2,1);

      var v_rate = stripNum(form.rate.value);
      form.r_rate.value = formatNumberDec(v_rate,1,0) + "%";

      var v_term = form.term.options[form.term.selectedIndex].value;
      form.r_term.value = form.term.options[form.term.selectedIndex].text;

      var v_interval = form.interval.options[form.interval.selectedIndex].value;
      form.r_type.value = form.interval.options[form.interval.selectedIndex].text;

      var  v_npr =  v_term * v_interval;

      var r_payment = computeIntervalPayment(v_principal, v_npr, v_rate, v_interval);
      form.r_payment.value = "$" + formatNumberDec(r_payment,2,1);


   }
    
}


function clear_results(form) {

   form.r_principal.value = "";
   form.r_rate.value = "";
   form.r_term.value = "";
   form.r_type.value = "";
   form.r_payment.value = "";


}


function createReport(form) {

   if(form.r_principal.value == "" || form.r_principal.value == 0) {
      alert("Please calculate the payment before clicking the \"Payment Schedule\" button.");
      form.principal.focus();
   } else {

      var v_principal = stripNum(form.r_principal.value);
      var v_rate = stripNum(form.r_rate.value);
      var v_term = form.term.options[form.term.selectedIndex].value;
      var v_interval = form.interval.options[form.interval.selectedIndex].value;

      var v_npr =  v_term * v_interval;

      var v_payment = stripNum(form.r_payment.value);

      var v_prin = v_principal;
      var v_int = v_rate;
      if(v_int >= 1) {
         v_int /= 100;
      }
      v_int /= v_interval;

      var v_int_port = 0;
      var v_accum_int = 0;
      var v_prin_port = 0;
      var v_accum_prin = 0;
      var v_count = 0;
      var v_pmt_row = "";
      var v_pmt_num = 0;


      var v_display_pmt_amt = 0;

      var v_accum_year_prin = 0;
      var v_accum_year_int = 0;

      var v_year = 1;



      while(v_count < v_npr) {

         if(v_count < (v_npr - 1)) {

            v_int_port = v_prin * v_int;
            v_int_port *= 100;
            v_int_port = Math.round(v_int_port);
            v_int_port /= 100;

            v_accum_int = eval(v_accum_int) + eval(v_int_port);
            v_accum_year_int = eval(v_accum_year_int) + eval(v_int_port);

            v_prin_port = eval(v_payment) - eval(v_int_port);
            v_prin_port *= 100;
            v_prin_port = Math.round(v_prin_port);
            v_prin_port /= 100;

            v_accum_prin = eval(v_accum_prin) + eval(v_prin_port);
            v_accum_year_prin = eval(v_accum_year_prin) + eval(v_prin_port);

            v_prin = eval(v_prin) - eval(v_prin_port);

            v_display_pmt_amt = eval(v_prin_port) + eval(v_int_port);

         } else {

            v_int_port = v_prin * v_int;
            v_int_port *= 100;
            v_int_port = Math.round(v_int_port);
            v_int_port /= 100;

            v_accum_int = eval(v_accum_int) + eval(v_int_port);
            v_accum_year_int = eval(v_accum_year_int) + eval(v_int_port);

            v_prin_port = v_prin;

            v_accum_prin = eval(v_accum_prin) + eval(v_prin_port);
            v_accum_year_prin = eval(v_accum_year_prin) + eval(v_prin_port);

            v_prin = 0;

      //pmtAmt = eval(intPort) + eval(prinPort);
            v_display_pmt_amt = eval(v_prin_port) + eval(v_int_port);
   }

   v_count = eval(v_count) + eval(1);

   v_pmt_num = eval(v_pmt_num) + eval(1);

   v_pmt_row += "<tr><td align=right><font face='arial'>";
   v_pmt_row += "<small>" + v_pmt_num + "</small></font></td>";
   v_pmt_row += "<td align=right><font face='arial'><small>" + formatNumberDec(v_display_pmt_amt,2,1) + "</small>";
   v_pmt_row += "</font></td><td align=right><font face='arial'><small>" + formatNumberDec(v_prin_port,2,1) + "</small>";
   v_pmt_row += "</font></td><td align=right><font face='arial'>";
   v_pmt_row += "<small>" + formatNumberDec(v_int_port,2,1) + "</small>";
   v_pmt_row += "</font></td><td align=right><font face='arial'>";
   v_pmt_row += "<small>" + formatNumberDec(v_prin,2,1) + "</small></font></td></tr>";


   if(v_pmt_num % v_interval == 0) {

      v_pmt_row += "<tr bgcolor='#dddddd'><td align=left>";
      v_pmt_row += "<font face='arial'><small>Year " + v_year + "</small>";
      v_pmt_row += "</font></td><td align=right><font face='arial'>";
      v_pmt_row += "<small> </small>";
      v_pmt_row += "</font></td><td align=right><font face='arial'>";
      v_pmt_row += "<small>" + formatNumberDec(v_accum_year_prin,2,1) + "</small>";
      v_pmt_row += "</font></td><td align=right><font face='arial'>";
      v_pmt_row += "<small>" + formatNumberDec(v_accum_year_int,2,1) + "</small></font></td>";
      v_pmt_row += "<td align=right><font face='arial'>";
      v_pmt_row += "<small> </small></font></td></tr>";

      v_year += 1;
      v_accum_year_prin = 0;
      v_accum_year_int = 0;

   }

      if(v_count > 100 * v_interval) {

         alert("Using your current entries you will never pay off this loan.");

         break;

         } else {

         continue;

         }

    }

   var v_int_text = "";

   if(v_interval == 12) {
      v_int_text = "Monthly";
   } else
   if(v_interval == 4) {
      v_int_text = "Quarterly";
   } else
   if(v_interval == 2) {
      v_int_text = "Semi-Annually";
   } else
   if(v_interval == 1) {
      v_int_text = "Annually";
   }

   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><td colspan=5>";
   part2 += "<font face='arial'><small>Principal: $" + formatNumberDec(v_principal,2,1) + "<br>";
   part2 += "Interest Rate: " + formatNumberDec(v_rate,2,0) + "%<br>";
   part2 += "Payment Interval: " + v_int_text + "<br>";
   part2 += "# of Payments: " + v_npr + "<br>";
   part2 += "Payment: $" + formatNumberDec(v_payment,2,1) + "</b></small></font></td></tr>";
   part2 += "<tr><td colspan=5><center><font face='arial'><b>Schedule of Payments</b></font><br>";
   part2 += "<font face='arial'><small><small>Please allow for slight rounding differences.";
   part2 += "</small></small></font></center></td></tr>";
   part2 += "<tr bgcolor='silver'><td align='center'><font face='arial'><small><b>Pmt #</b>";
   part2 += "</small></font></td>";
   part2 += "<td align='center'><font face='arial'><small><b>Payment</b></small></font></td>";
   part2 += "<td align='center'><font face='arial'><small><b>Principal</b></small></font></td>";
   part2 += "<td align='center'><font face='arial'><small><b>Interest</b></small></font></td>";
   part2 += "<td align='center'><font face='arial'><small><b>Balance</b></small></font></td></tr>";

   var part3 = ("" + v_pmt_row + "");

   var part4 = "<tr><td><font face='arial'><small><b>Grand Total</b></small></font></td><td> </td>";
   part4 += "<td align=right><font face='arial'><small><b>" + formatNumberDec(v_accum_prin,2,1) + "</b>";
   part4 += "</small></font></td><td align=right><font face='arial'>";
   part4 += "<small><b>" + formatNumberDec(v_accum_int,2,1) + "</b></small>";
   part4 += "</font></td><td> </td></tr></table><br><center>";
   part4 += "<form method='post'><input type='button' value='Close Window' onClick='window.close()'></form>";
   part4 += "</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();

   }

}
