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) {

if(form.principal.value == "" || form.principal.value == 0) {
   alert("Please enter an amount in Line #1.");
   form.principal.focus();
   } else
      if(form.intRate.value == "" || form.intRate.value == 0) {
      alert("Please enter an amount in Line #2.");
      form.intRate.focus();
   } else
      if(form.numMonths.value == "" || form.numMonths.value == 0) {
      alert("Please enter an amount in Line #3.");
      form.numMonths.focus();
   } else {

var Vprincipal = form.principal.value;

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;
   }

    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;

    form.moPmt.value = "$" + formatNum(moPmt);
    form.HmoPmt.value = moPmt;
    form.HperiodPmt.value = periodPmt;
    form.HintRate.value = intRate;
    form.Hdivisor.value = divisor;
    form.HnumMonths.value = numMonths;
    form.HnumPeriods.value = numPeriods;

   }
    
}


function createReport(form) {

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);


if(form.HmoPmt.value == 0 || form.HmoPmt.value == "") {
   alert("Please compute the payment before creating the schedule.");
   } else {

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 Vmonth = eval(form.month.selectedIndex) + eval(1);
var Vday = eval(form.day.selectedIndex) + eval(1);
var Vyear = eval(form.year.selectedIndex) + eval(2004);
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 ALIGN=CENTER bgcolor=59929F>" + aPmtNum + "</TD><TD ALIGN=RIGHT>" + pmtString + "</TD><TD ALIGN=RIGHT>" + formatNum(aPrinPort) + "</TD><TD ALIGN=RIGHT>" + formatNum(aIntPort) + "</TD><TD ALIGN=RIGHT>" + formatNum(aPrin) + "</TD></TR>");

   monthCount = eval(monthCount) +  eval(1);
   }

   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>Amortization Schedule</TITLE><link rel=stylesheet type=text/css href=../styles/amortize.css /></HEAD>" + "<BODY BGCOLOR = '#89A4AB' TEXT='#191919'>");

var part2 = ("<CENTER><TABLE WIDTH='500' BORDER=0 CELLPADDING=0 BORDERCOLOR='#698CB1'><TR><TD COLSPAN=5><B>Loan Date:</B> " + loanDate + "<BR><b>Principal:</b> $" + formatNum(form.principal.value) + "<BR><b># of Payments:</b> " + anumMonths + "<BR><b>Interest Rate:</b> " + formatNum(aIntRate * 12 / adivisor * 100) + "%<BR><b>Payment:</b> $" + formatNum(form.HmoPmt.value) + "</TD></TR><TR><TD COLSPAN=5><CENTER><FONT SIZE=+2 COLOR=#004D5F><b>Schedule of Payments</b></FONT></CENTER></TD></TR><TR><TD bgcolor=#59929F><B>Payment #</B></TD><TD bgcolor=#59929F><B>Date</B></TD><TD bgcolor=#59929F><B>Principal</B></TD><TD bgcolor=#59929F><B>Interest</B></TD><TD bgcolor=#59929F><B>Balance</B></TD></TR>");

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

var part4 = ("<TR><TD colspan=2 bgcolor=59929F><B>Totals</B></TD><TD ALIGN=RIGHT><B>" + formatNum(aAccumPrin) + "</B></TD><TD><B>" + formatNum(aAccumInt) + "</B></TD><TD align=right>0.00</TD></TR></TABLE></BODY></HTML>");

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

  reportWin = window.open("","Amortization","width=525,height=600,scrollbars=yes");
  reportWin.document.write(schedule);
  reportWin.document.close();

  }
}

function clearForm(form) {
   form.customerName.value = "";
   form.principal.value = "";
   form.intRate.value = "";
   form.numMonths.value = "";
   form.moPmt.value = "";
   form.HmoPmt.value = "";
   form.HperiodPmt.value= "";
   form.Hdivisor.value = "";
   form.HintRate.value= "";
   form.HnumMonths.value= "";
   form.HnumPeriods.value= "";
   }