
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);
     }

 //START UPGRADE
var Vcents = 0;

if(Vnum % 1 * 100 < 1) {
   Vcents = 0;
   } else {
   Vcents = parseInt(((eval(Vnum % 1) * 100)),10);
   }
//END UPGRADE

 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;

  }
}

//159
function computeForm(form)  {

if(form.interest.value == "") {
   alert("Please enter the Interest Rate.");
   } else 
     if(form.moAdd.value == "") {
       alert("Please enter the Monthly Addition.");
       }
       else
       if(form.payments.value == "") {
           alert("Please enter the Number of Years.");
           } else {
  

    var i = form.interest.value;

    //if (i > 1.0) {i = form.interest.value / 100} else {i = form.interest.value};
	i = form.interest.value / 100;

var VcompDays = 0;
var maxCompsPerYr = 0;

if(form.compInt.selectedIndex == 0) {
 i /= 365;
VcompDays = 1;
maxCompsPerYr = 365;
} else
if(form.compInt.selectedIndex == 1) {
 i /= 12;
VcompDays = 30;
maxCompsPerYr = 12;
} else
if(form.compInt.selectedIndex == 2) {
 i /= 4;
VcompDays = 91;
maxCompsPerYr = 4;
} else
if(form.compInt.selectedIndex == 3) {
 i /= 2;
VcompDays = 182;
maxCompsPerYr = 2;
} else
if(form.compInt.selectedIndex == 4) {
 i /= 1;
VcompDays = 365;
maxCompsPerYr = 1;
}

var ma = eval(form.moAdd.value);

var VperiodDays = 0;
var maxAddsPerYr = 0;

if(form.period.selectedIndex == 0) {
VperiodDays = 1;
maxAddsPerYr = 365;
} else
if(form.period.selectedIndex == 1) {
VperiodDays = 7;
maxAddsPerYr = 52;
} else
if(form.period.selectedIndex == 2) {
VperiodDays = 14;
maxAddsPerYr = 26;
} else
if(form.period.selectedIndex == 3) {
VperiodDays = 15;
maxAddsPerYr = 24;
} else
if(form.period.selectedIndex == 4) {
VperiodDays = 30;
maxAddsPerYr = 12;
} else
if(form.period.selectedIndex == 5) {
VperiodDays = 61;
maxAddsPerYr = 6;
} else
if(form.period.selectedIndex == 6) {
VperiodDays = 91;
maxAddsPerYr = 4;
} else
if(form.period.selectedIndex == 7) {
VperiodDays = 182;
maxAddsPerYr = 2;
} else
if(form.period.selectedIndex == 8) {
VperiodDays = 365;
maxAddsPerYr = 1;
}

//IF DEPOSIT FREQUENCY EQUALS COMPOUNDING FREQUENCY
if(VperiodDays == VcompDays) {

   var ma = eval(form.moAdd.value);

   
   if(form.principal.value == "") {form.principal.value = 0; } else {form.principal.value = form.principal.value; };
 
   var prin = eval(form.principal.value);

   var pmts = eval(form.payments.value * maxCompsPerYr);

   var count = 0;
    
   while(count < pmts) {

        newprin = prin + ma;

        prin = (newprin * i) + eval(prin + ma);

        count = count + 1;

      }

   var Vfv = prin;

    form.fv.value = "$" + formatNum(prin);

var totinv = eval(count * ma) + eval(form.principal.value);

form.totDeposits.value = "$" + formatNum(totinv);

    var Vtotalint = eval(prin - totinv);

    form.totalint.value = "$" + formatNum(Vtotalint);


//IF DEPOSITS ARE LESS FREQUENT THAN COMPOUNDING FREQUENCY
} else
if(VperiodDays > VcompDays) {

 if(form.principal.value == "") {form.principal.value = 0; } else {form.principal.value = form.principal.value; };
 
   var prin = eval(form.principal.value);

   var pmts = eval(form.payments.value * 365);

var maxComps = eval(form.payments.value * maxCompsPerYr);
var maxAdds = eval(form.payments.value * maxAddsPerYr);

var count = 0;
var accumAdd = eval(ma);
var numAdds = 1;
var countAddDays = 0;
var countCompDays = 0;
var numComps = 0;
var accumComp = 0;
var currentInt = 0;
prin =  eval(prin) + eval(ma);
    
while(count < pmts) {

//Add Addition if interval is met
if(countAddDays == VperiodDays && numAdds < maxAdds) {
   prin = eval(prin) + eval(ma);
   accumAdd = eval(accumAdd) + eval(ma);
   accumPrin = eval(accumPrin) + eval(prin);
   numAdds = eval(numAdds) + eval(1);
   countAddDays = 1;
} else {
   countAddDays = eval(countAddDays) + eval(1);
}

//Compound interest if interval is met
if(countCompDays == VcompDays && numComps < maxComps) {
accumComp = eval(accumComp) + eval(prin * i)
prin = eval(prin * i) + eval(prin);
currentInt = eval(prin * i);
numComps = eval(numComps) + eval(1);
countCompDays = 1;
} else {
countCompDays = eval(countCompDays) + eval(1);
}




count = eval(count) + eval(1);

}

var Vfv = prin;

form.fv.value = "$" + formatNum(prin);

var totinv = eval(accumAdd) + eval(form.principal.value);

form.totDeposits.value = "$" + formatNum(totinv);

    var Vtotalint = eval(prin - totinv);

form.totalint.value = "$" + formatNum(Vtotalint);



//IF DEPOSITS ARE MORE FREQUENT THAN COMPOUNDING FREQUENCY
} else {
   
   if(form.principal.value == "") {form.principal.value = 0; } else {form.principal.value = form.principal.value; };

if(form.period.selectedIndex == 5) {
VperiodDays = 60;
}
 
   var prin = eval(form.principal.value);

   var pmts = eval(form.payments.value * 365);

var maxComps = eval(form.payments.value * maxCompsPerYr);
var maxAdds = eval(form.payments.value * maxAddsPerYr);

var count = 0;
var accumAdd = 0;
var numAdds = 0;
var countAddDays = 0;
var countCompDays = 0;
var numComps = 0;
var accumComp = 0;
var depositIntervalDays = 0;
var periodsPast = 0;
var accumPrin = 0;
var prinAvg = 0;

var periodsPerComp = parseInt(VcompDays / VperiodDays,0);
    
while(count < pmts) {
//while(count < form.testNum.value) {

depositIntervalDays = eval(depositIntervalDays) + eval(1);

//Accumulate period balances to figure average balance
if(depositIntervalDays == VperiodDays || countCompDays == VcompDays) {
periodsPast = eval(periodsPast) + eval(1);
depositIntervalDays = 0;
}

//Add Addition if interval is met
if(countAddDays == VperiodDays && numAdds < maxAdds) {
   prin = eval(prin) + eval(ma);
   accumAdd = eval(accumAdd) + eval(ma);
   accumPrin = eval(accumPrin) + eval(prin);
   prinAvg = accumPrin / periodsPast;
   numAdds = eval(numAdds) + eval(1);
   countAddDays = 1;
} else {
   countAddDays = eval(countAddDays) + eval(1);
}

//Compound interest if interval is met
if(countCompDays == (VcompDays - 1) && numComps < maxComps) {
periodsPast = 0;
prin = eval(prinAvg * i) + eval(prin);
accumPrin = 0;
accumComp = eval(accumComp) + eval(prinAvg * i)
numComps = eval(numComps) + eval(1);
countCompDays = 1;
} else {
countCompDays = eval(countCompDays) + eval(1);
}





count = eval(count) + eval(1);

}

var Vfv = prin;

form.fv.value = "$" + formatNum(prin);

var totinv = eval(accumAdd) + eval(form.principal.value);

form.totDeposits.value = "$" + formatNum(totinv);

    var Vtotalint = eval(prin - totinv);

form.totalint.value = "$" + formatNum(Vtotalint);


//END IF ELSE STATEMENT THAT DETERMINES WHICH ROUTINE TO RUN
      }

//END NESTED IF TO CHECK FOR NON-NUMERIC ENTRIES
   }

}



function clearForm(form)

{

    form.payments.value = "";

    form.interest.value = "";

    form.principal.value = "";

    form.moAdd.value = "";

}



<!-- done hiding from old browsers -->
