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;

  }
}





function computeForm(form) {

if(form.payment.value == "") {
   alert("Please enter the amount of your mortgage payment.");
   } else 
     if(form.principal.value == "") {
       alert("Please enter the your mortgage's current principal balance.");
       }
       else
       if(form.intRate.value == "") {
           alert("Please enter your mortgage's current annual interest rate.");
           }
           else
           if(form.intRate2.value == "") {
               alert("Please enter the annual interest rate you'll be refinancing at.");
               } 
               else
               if(form.nper2.value == "") {
                   alert("Please enter the number of years you are refinancing for.");
                    } 
                    else
                    if(form.closingCost.value == "") {
                         alert("Please enter the closing cost percentage points.");
                         } 
                         else
                         if(eval(form.intRate2.value) > eval(form.intRate.value)) {
                            alert("You've entered a refinancing rate that is higher than your present rate.  The refinancing rate must be lower than your present rate in order for this calculator to work.");
                            } else {



    var pmt1 = form.payment.value;

    var prin = form.principal.value;

     var prin1 = prin;

    var closeCostAmt = 0;

    if(form.ptsDol.selectedIndex == 0) {
    var closeCostPerc = eval(form.closingCost.value) / 100;
      closeCostAmt = closeCostPerc * prin;
       } else {
      closeCostAmt = form.closingCost.value;
       }
     
    

    var intPort1 = 0;

    var prinPort1 = 0;

    var accumInt1 = 0;

    var accumPrin1 = 0;

    var i1 = form.intRate.value;

    if (i1 > 1.0) {

        i1 = i1 / 100.0;

    var sumi1 = i1;

    }

    var i1  = i1  / 12;

    var i2 = form.intRate2.value;

       if (i2 > 1.0) {

           i2 = i2 / 100.0;

        var sumi2 = i2;   

       }

       var i2  = i2  / 12;

    var count1 = 0;

    while(prin1 > 0) {

        intPort1 = prin1 * i1;

        prinPort1 = pmt1 - intPort1;

        prin1 = prin1 - prinPort1;

        accumPrin1 = accumPrin1 + prinPort1;

        accumInt1 = accumInt1 + intPort1;

        count1 = count1 + 1;

        if(count1 > 600) {break; } else {continue; }

       }

    form.origInt.value = "$" + formatNum(accumInt1);

     var prin2 = 0;

    if(form.yesNo.selectedIndex == 0) {
       prin2 = prin;
       } else {
       prin2 = eval(prin) + eval(closeCostAmt);
       }

    var pow = 1;

    for (var j = 0; j < form.nper2.value *12; j++)

        pow = pow * (1 + i2);

    var fpayment2 = (prin2 * pow * i2) / (pow - 1);

    form.payment2.value = "$" + formatNum(fpayment2);


   var fmoSave = form.payment.value - fpayment2;

   form.moSave.value = "$" + formatNum(fmoSave);
                
                var ftotInt2 = (fpayment2 * form.nper2.value *12)
    - prin2;

   form.totInt2.value = "$" + formatNum(ftotInt2);

   var fintSave = accumInt1 - ftotInt2;

    form.intSave.value = "$" + formatNum(fintSave);

    var prin3 = prin2;
    var prin4 = prin;

    var intPort3 = 0;
    var intPort4 = 0;

    var prinPort3 = 0;
    var prinPort4 = 0;

    var accumInt3 = 0;
    var accumInt4 = 0;

    var accumPrin3 = 0;
    var accumPrin4 = 0;

    var amortIntSave = 0;

    var count3 = 0;

    while(amortIntSave < closeCostAmt) {

        intPort3 = prin3 * i2;
        intPort4 = prin4 * i1;

        prinPort3 = fpayment2 - intPort3;
        prinPort4 = pmt1 - intPort4;

        prin3 = prin3 - prinPort3;
        prin4 = prin4 - prinPort4;

        accumPrin3 = accumPrin3 + prinPort3;
        accumPrin4 = accumPrin4 + prinPort4;

        accumInt3 = accumInt3 + intPort3;
        accumInt4 = accumInt4 + intPort4;

        amortIntSave = accumInt4 - accumInt3;

        count3 = count3 + 1;

        if(count3 > 600) {break; } else {continue; }

       }


    form.closeMo.value = count3;

   var fnetSave = eval(fintSave) - eval(closeCostAmt);

   var pmtUpDown = "";
      if(fpayment2 > pmt1) {
          pmtUpDown = "increase by $" + formatNum(eval(fpayment2) - eval(pmt1)) + "";
          } else {
          pmtUpDown = "decrease by $" + formatNum(eval(pmt1) - eval(fpayment2)) + "";
          }

    var intSaveYesNo = "";
      if(accumInt1 < ftotInt2) {
          intSaveYesNo = "pay an additional $" + formatNum(eval(ftotInt2) - eval(accumInt1)) + " in interest charges over the life of the mortgage.";
          } else {
          intSaveYesNo = "save $" + formatNum(eval(accumInt1) - eval(ftotInt2)) + " in interest charges over the life of the mortgage. However, in order for this refinancing to yield any savings at all you will need to stay in your current home for at least " + count3 + " months.  That's how long it will take for the monthly interest savings to offset the closing costs attributable to refinancing.";
          }

   form.netSave.value = "$" +formatNum(fnetSave);

    form.summary.value = "If you refinance your current " + formatNum(sumi1 * 100) + "% mortgage to a " + formatNum(sumi2 * 100) + "% mortgage, your monthly payment will " + pmtUpDown + " and you will " + intSaveYesNo + "";

   }
                
}



function clearForm(form)

{

    form.principal.value = "";
    form.payment.value = "";
    form.intRate.value = "";
    form.intRate2.value = "";
    form.nper2.value = "";
    form.closingCost.value = "";
    form.payment2.value = "";
    form.moSave.value = "";
    form.closeMo.value = "";
    form.intSave.value = "";
    form.origInt.value = "";
    form.netSave.value = "";
    form.totInt2.value = "";
    form.summary.value = "";

}