﻿    function formatNum(Vnum) {

       if(Vnum > 99000000) {
       alert("Sorry, this will not generate numbers larger that 99 million.");
       focus();
       } else {

       var V10million = parseInt(Vnum / 10000000);

       var V1million = (Vnum % 10000000)  / 1000000;
          if(V1million / 1000000 == 1) {
          V1million = 1;
          } else
          if(V1million < 1) {
          V1million = "0";
         } else {
          V1million = parseInt(V1million,10);
         }

        var V100thousand = (Vnum % 1000000)  / 100000;
          if(V100thousand / 100000 == 1) {
          V100thousand = 1;
          } else
          if(V100thousand < 1) {
          V100thousand = "0";
         } else {
          V100thousand = parseInt(V100thousand,10);
         }

       var V10thousand = (Vnum % 100000)  / 10000;
          if(V10thousand / 10000 == 1) {
          V10thousand = 1;
          } else
          if(V10thousand < 1) {
          V10thousand = "0";
          } else {
          V10thousand = parseInt(V10thousand,10);
          }

       var V1thousand = (Vnum % 10000)  / 1000;
          if(V1thousand / 1000 == 1) {
          V1thousand = 1;
          } else
          if(V1thousand < 1) {
          V1thousand = "0";
         } else {
          V1thousand = parseInt(V1thousand,10);
         }

       var Vhundreds = (Vnum % 1000)  / 100;
          if(Vhundreds / 100 == 1) {
          Vhundreds = 1;
          } else
          if(Vhundreds < 1) {
          Vhundreds = "0";
         } else {
          Vhundreds = parseInt(Vhundreds,10);
         }

       var Vtens = (Vnum % 100)  / 10;
          if(Vtens / 10 == 1) {
          Vtens = 1;
          } else
          if(Vtens < 1) {
          Vtens = "0";
         } else {
          Vtens = parseInt(Vtens,10);
         }

       var Vones = (Vnum % 10)  / 1;
          if(Vones / 1 == 1) {
          Vones = 1;
          } else
          if(Vones < 1) {
          Vones = "0";
         } else {
          Vones = parseInt(Vones,10);
         }

    var Vcents = 0;

    if(Vnum % 1 * 100 < 1) {
       Vcents = 0;
       } else {
       Vcents = parseInt(((eval(Vnum % 1) * 100)),10);
       }



     if(Vcents < 1) {
      Vcents = "00";
      }
      else
      if(Vcents % 10 == 0) {
      Vcents = Vcents + "0";
      }
      else
      if(Vcents % 10 == Vcents) {
      Vcents = "0" + Vcents;
      } else {
      Vcents = Vcents;
      }

      if(Vcents == "900") {
      Vcents = "90";
      } else
      if(Vcents == "800") {
      Vcents = "80";
      } else
      if(Vcents == "700") {
      Vcents = "70";
      } else
      if(Vcents == "600") {
      Vcents = "60";
      } else
      if(Vcents == "500") {
      Vcents = "50";
      } else
      if(Vcents == "400") {
      Vcents = "40";
      } else
      if(Vcents == "300") {
      Vcents = "30";
      } else
      if(Vcents == "200") {
      Vcents = "20";
      } else
      if(Vcents == "100") {
      Vcents = "10";
      } else {
      Vcents = Vcents;
      }


       var Vformat = "";

       if(Vnum >= 10000000) {
       Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
       }
       else
       if(Vnum >= 1000000) {
       Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
       }
       else
       if(Vnum >= 100000) {
       Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
       }
       else
       if(Vnum >= 10000) {
       Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
       }
       else
       if(Vnum >= 1000) {
       Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
       }
       else
       if(Vnum >= 100) {
       Vformat = (Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
       }
       else
       if(Vnum >= 10) {
       Vformat = (Vtens + "" + Vones + "." + Vcents);
       }
       else
       if(Vnum >= 1) {
       Vformat = (Vones + "." + Vcents);
       } else {
       Vformat = ("0." + Vcents);
       }

      return Vformat;

      }
    }


    function computeForm(form, showamorttable) {
    if(form.principal.value == "" || form.principal.value == 0) {
       alert("Please enter a loan amount");
       form.principal.focus();
       } else
          if(form.intRate.value == "" || form.intRate.value == 0) {
          alert("Please enter an interest rate.");
          form.intRate.focus();
       } else
          if(form.numMonths.value == "" || form.numMonths.value == 0) {
          alert("Please enter the length of loan in months.");
          form.numMonths.focus();
       } else {

    var Vprincipal = form.principal.value.replace(",","");

    var intRate = form.intRate.value;

    if (intRate > 1.0) {

            intRate = intRate / 100.0;
            } else {
            intRate = intRate;
            }

    var numMonths = form.numMonths.value;
    var numPeriods = 0;
    var divisor = 0;

    if(form.interval.selectedIndex == 0) {
       intRate = intRate / 12;
       numPeriods = numMonths;
       divisor = 1;
       } else
    if(form.interval.selectedIndex == 1) {
       intRate = intRate / 4;
       numPeriods = numMonths / 3;
       divisor = 3;
       } else
    if(form.interval.selectedIndex == 2) {
       intRate = intRate / 2;
       numPeriods = numMonths / 6;
       divisor = 6;
       } else {
       intRate = intRate;
       numPeriods = numMonths / 12;
       divisor = 12;
       }



    //FIGURE PAYMENT NEEDED TO PAY OFF $1,000

        var factor = 1;

        for (var j = 0; j < numPeriods; j++) {

            factor = factor * (eval(1) + eval(intRate));
            }


        var periodPmt = (Vprincipal * factor * intRate) / (eval(factor) - eval(1));
        var moPmt = periodPmt / divisor;

        getElement('moPmt').innerHTML = "$" + formatNum(moPmt);
	    getElement('moPmt').style.display = "inline";
	    getElement('moPmtLabel').style.display = "inline"
        form.HmoPmt.value = moPmt;
        form.HperiodPmt.value = periodPmt;
        form.HintRate.value = intRate;
        form.Hdivisor.value = divisor;
        form.HnumMonths.value = numMonths;
        form.HnumPeriods.value = numPeriods;
       }
       
       if (showamorttable)
       {
            createReport(form);
       }

    }

    function validinput()
    {
	    var regExPattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	    if (getElement('loanstartdate').value.match(regExPattern))
	    {
		    getElement('divDateError').style.display="none";
		    return true;
	    }
	    else
	    {
		    getElement('divDateError').style.display="inline";
		    return false;
	    }
    }

    function createReport(form) {
    // COMPUTE THE PAYMENT AMOUNT
    if(form.HmoPmt.value == 0 || form.HmoPmt.value == "") {
	    computeForm(form); 
	    //alert("Please compute the payment before creating the schedule.");
    } //else {

    //GRAB VARIABLES
    var aPrin = eval(form.principal.value);
    var aIntRate = eval(form.HintRate.value);
    var aNPer = eval(form.HnumPeriods.value);
    var anumMonths = eval(form.HnumMonths.value);
    var adivisor = eval(form.Hdivisor.value);

    var aPmt = form.HmoPmt.value;
    var aIntPort = 0;
    var aAccumInt = 0;
    var aPrinPort = 0;
    var aAccumPrin = 0;
    var aCount = 0;
    var monthCount = 0;
    var aPmtRow = "";
    var aPmtNum = 0;
    var mIntPort = 0;

    var loanstartdate = new Date(form.loanstartdate.value);
    var Vmonth = loanstartdate.getMonth() + 1; // base 0
    var Vday = loanstartdate.getDate();
    var Vyear = loanstartdate.getUTCFullYear();
    var loanDate = (Vmonth + "/" + Vday + "/" + Vyear);

    while(eval(aCount) < eval(aNPer)) {
       aIntPort = aPrin * aIntRate;
       monthCount = 0;
       mIntPort = aIntPort

       while(eval(monthCount) < eval(adivisor)) {

       if(mIntPort > aPmt) {
          aIntPort = aPmt;
          aPrinPort = 0;
          } else
          if(mIntPort <= 0) {
          aIntPort = 0;
          aPrinPort = aPmt;
          } else {
          aPrinPort = eval(aPmt) - eval(mIntPort);
          aIntPort = eval(aPmt) - eval(aPrinPort);
          }

       mIntPort = eval(mIntPort) - eval(aIntPort);
       aPrin = eval(aPrin) - eval(aPrinPort);
       aAccumInt = eval(aAccumInt) + eval(aIntPort);
       aAccumPrin = eval(aAccumPrin) + eval(aPrinPort);
       aPmtNum = eval(aPmtNum) + eval(1);

       Vmonth = Vmonth + 1;
       if(Vmonth == 13) {
          Vmonth = 1;
          Vyear = Vyear + 1;
          } else {
          Vmonth = Vmonth;
          Vyear = Vyear;
          }
       pmtString = (Vmonth + "/" + Vday + "/" + Vyear);

       aPmtRow = ("" + aPmtRow + "<tr><td style=\"text-align: center;\">" + aPmtNum + "</td><td style=\"text-align: right; padding-right: 30px;\">" + pmtString + "</td><td style=\"text-align: right; padding-right: 30px;\">$" + formatNum(aPrinPort) + "</td><td style=\"text-align: right; padding-right: 30px;\">$" + formatNum(aIntPort) + "</td><td style=\"text-align: right;\">$" + formatNum(aPrin) + "</td></tr>\n");

       monthCount = eval(monthCount) +  eval(1);
       }
    //end inner loop
       aCount = eval(aCount) + eval(1);
          if(aCount > 600) {
             alert("Using your current entries you will never pay off this loan.");
             break;
             } else {
             continue;
             }
        }
        var part1 = ("<HEAD><TITLE>Schedule of Payments</TITLE><style> body, td {font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; }</style></HEAD>\n" + "<BODY BGCOLOR = \"#FFFFFF\">\n");

        var part2 = ("<table align=\"center\" border=\"0\" cellpadding=\"3\">\n<tr><td colspan=\"5\" class=\"printableonly\"><b>Loan Date: " + loanDate + "<BR>Principal: $" + formatNum(form.principal.value) + "<BR># of Payments: " + anumMonths + "<BR>Interest Rate: " + formatNum(Math.round((aIntRate * 12 / adivisor * 100)*100)/100) + "%<BR>Payment: $" + formatNum(form.HmoPmt.value) + "</b></td></tr><tr><td COLSPAN=5 class=\"header\"><CENTER><b>Schedule of Payments</b><BR><span>Please allow for slight rounding differences.</span></CENTER></td></tr>\n" + 
                     "<tr><td style=\"width: 25px;\"><b>Pmt #</b></td><td style=\"width: 70px; text-align: center; padding-right: 30px;\"><b>Date</b></td><td style=\"width: 50px; text-align: right; padding-right: 30px;\"><b>Principal</b></td><td style=\"text-align: right; padding-right: 30px;\"><b>Interest</b></td><td style=\"text-align: right;\"><b>Balance</b></td></tr>\n");

        var part3 = ("" + aPmtRow + "");

        var part4 = ("<tr><td><B>Totals</B></td><td></td><td style=\"text-align: right; padding-right: 30px;\"><B>$" + formatNum(aAccumPrin) + "</B></td><td style=\"padding-right: 30px;\"><B>$" + formatNum(aAccumInt) + "</B></td><td></td></tr>\n</table>\n");

        var part5 = ("</body></html>")

        var schedule = (part1 + "" + part2 + "<tr><td colspan=\"5\"><hr width=\"100%\"></td></tr>" + part3 + "<tr><td colspan=\"5\"><hr width=\"100%\"></td></tr>" + part4 + "" + part5);

        reportWin = window.open("","","width=500,height=300,scrollbars=yes,menubar=yes, resizable=yes");
        reportWin.document.write(schedule);
        reportWin.document.close();
        //getElement('divAmortTable').innerHTML = part2 + part3 + part4;
    }

    function show(num){
	    if(document.getElementById("calc" + num)){
		    menu = document.getElementById("calc" + num);
		    menu.style.display = "block";
	    }
    }

    function hide(num){
	    if(document.getElementById("calc" + num)){
		    menu = document.getElementById("calc" + num);
		    menu.style.display = "none";
	    }
    }


    function clearForm(form) {
       form.principal.value = "100000";
       form.intRate.value = "6.00";
       form.numMonths.value = "360";
       getElement('moPmt').style.display = "none";
       getElement('moPmtLabel').style.display = "none";
       form.HmoPmt.value = "";
       form.HperiodPmt.value= "";
       form.Hdivisor.value = "";
       form.HintRate.value= "";
       form.HnumMonths.value= "";
       form.HnumPeriods.value= "";
       }
