//DO NOT REMOVE THIS COPYWRITE INFO!
//Lifetime Savings Calculator
//By Daniel C. Peterson
//Web Winder Website Services, 1997-2009  All Rights Reserved.
//Distribution, editing or reselling of this script is strictyly prohibited
//without expressed written permission from Daniel C. Peterson.
//For commercial grade (professional) versions of this and many other
//calculators, visit http://www.webwinder.com.

function calcSave(form) {

   var yrsTillRetire = eval(form.retireAge.value) - eval(form.nowAge.value);
   var lifeYrs = eval(form.dieAge.value) - eval(form.nowAge.value);
   var retireYrs = eval(form.dieAge.value) - eval(form.retireAge.value);
   var feachSave = eval(form.nameBrand.value) - eval(form.generic.value);
   var fmoSave = feachSave * form.buyTimes.value;

   form.eachSave.value = feachSave;

   form.moSave.value = fmoSave;
   var fannSave = fmoSave * 12;
   form.annSave.value = "" + parseInt(fannSave,10);
   form.retireSave.value = "" + parseInt(fannSave * retireYrs,10);
   form.lifeSave.value = "" + parseInt(fannSave * lifeYrs,10);

}

function calcInvest(form) {

var i = form.intRate.value;

    if (i > 1.0) {i = form.intRate.value / 100} else {i = form.intRate.value};

    i /= 12;

   var ma = eval(form.moSave.value);

   var prin = eval(form.eachSave.value);

   var pmts = ((eval(form.retireAge.value) - eval(form.nowAge.value)) * 12);

   var count = 0;
    
   while(count < pmts) {

        newprin = prin + ma;

        prin = (newprin * i) + eval(prin + ma);

        count = count + 1;

      }

    form.retireInvest.value = "" + parseInt(prin,10);

    var pmts = (eval(form.dieAge.value) - eval(form.retireAge.value)) * 12;
		
		var pow = 1;

    for (var j = 0; j < pmts; j++)

        pow = pow * (1 + i);

    var moPmt = (prin * pow * i) / (pow - 1);

    form.moIncome.value = "" + parseInt(moPmt,10);

    var totPmts = pmts * moPmt;
		
		form.dieInvest.value = "" + parseInt(totPmts,10);

    form.summary.value = "Summary:  If you were to permanently switch from the " + form.nameBrand.value + " product to the " + form.generic.value + " product, and then invested the resulting monthly savings in an investment that earned " + form.intRate.value + "% per year, between now and age " + form.retireAge.value + ", you would then be able to withdraw " + form.moIncome.value + " from your investment each month...for the rest of your life!"

}


function clearSave(form)

{

    
    form.nowAge.value = "";
    form.retireAge.value = "";
    form.dieAge.value = "";
    form.nameBrand.value = "";
    form.generic.value = "";
    form.buyTimes.value = "";
    form.eachSave.value = "";
    form.moSave.value = "";
    form.annSave.value = "";
    form.retireSave.value = "";
    form.lifeSave.value = "";
    

}

function clearInvest(form)

{

    
    form.intRate.value = "";
    form.retireInvest.value = "";
    form.moIncome.value = "";
    form.dieInvest.value = "";
    form.summary.value = "";

}
