    <SCRIPT LANGUAGE="JavaScript">

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num);
}

</script>

    <script type="text/javascript">

        var age;
        var retirementAge;
        var workingLife;
        var income;
        var lifeInsurance;
        var inflationRate;
        var additionalInsurance;
        var futureTotalEarnings;
        var NPV;
        var insuranceMultiple;
        var termPeriod;        
        
        futureTotalEarnings = 0;
        additionalInsurance = 0;
        NPV = 0;
        
        function calcAmt(frm) {
        
        age = frm.txtAge.value;
        retirementAge = frm.txtRetAge.value;
        workingLife = retirementAge - age;
        income = frm.txtIncome.value;
        lifeInsurance = frm.txtInsurance.value;
        inflationRate = frm.txtRate.value; 
        
        // check age to set insurance multiple
        if (age < 36)
        {
            insuranceMultiple = 20;
        }
        else if (age < 41)
        {
            insuranceMultiple = 17;
        }
        else if (age < 46)
        {
            insuranceMultiple = 14;
        }
        else if (age < 51)
        {
            insuranceMultiple = 12;
        }
        else if (age < 60)
        {
            insuranceMultiple = 10;
        }
        else if (age < 66)
        {
            insuranceMultiple = 7;
        }
        else
        {
            insuranceMultiple = 5;
        }
        
        // check age to set term period
        if (age < 27)
        {
            termPeriod = 30;
        }
        else if (age < 38)
        {
            termPeriod = 20;
        }
        else
        {
            termPeriod = 10;  
        }     
        
        var currentValue = 0;
        var prevValue = income;
        futureTotalEarnings = 0;
                
        for (var x = 0; x < workingLife; x++)
        {            
           currentValue = Math.round(prevValue * Math.pow((1 + inflationRate/100),x));
           futureTotalEarnings = futureTotalEarnings + currentValue;                              
        }        
        
        NPV = workingLife * income/(1 + (inflationRate/100)); 
        
        frm.txtFutureTotal.value = formatCurrency(futureTotalEarnings);
        frm.txtNPV.value = formatCurrency(Math.round(NPV));  
        frm.txtAddInsurance.value = formatCurrency(income*insuranceMultiple - frm.txtInsurance.value); 
        frm.txtWorkRemain.value = workingLife; 
        frm.txtTerm.value = termPeriod;   
        
        }
        function checkRate(frm) {
        if (inflationRate == 0 || inflationRate > 30){
        //window.alert("Make sure that the Interest Rate really is " + inflationRate +"%?")
        }
        }
        function checkTerm(frm) {
        if (workingLife == 0 || workingLife > 50){
        window.alert("Make sure that the Term really is " + workingLife + " years?")
        }
        }

    </script>
