Sample ASP Code: Formula Calculations

<%@ language="JavaScript" %>

<%

var Current_Age;

var Desired_Retirement_Age;

var Monthly_savings;

var Annual_Investment_growth_rate;

var Annual_Salary_Increase_Rate;

var Retirement_Nest_Egg;

var duration;

var i;

var isPostBack;

 

isPostBack=Request.Form.Count!=0;

 

if(isPostBack)

{

      Current_Age=parseInt(Request.Form("DdlCurrent_Age"));

      Desired_Retirement_Age=parseInt(Request.Form("DdlDesired_Retirement_Age"));

      Monthly_savings=parseInt(Request.Form("TxtMonthly_savings"));

      Annual_Investment_growth_rate=parseFloat(Request.Form("TxtAnnual_Investment_growth_rate"));

      Annual_Salary_Increase_Rate=parseFloat(Request.Form("TxtAnnual_Salary_Increase_Rate"));

 

      // Create an instance of Application

      var app = Server.CreateObject("PagosSpreadSheet.Application");

 

      //Open retirement workbook

      app.Workbooks.Open(Server.MapPath("Spreadsheets/Retirement/Retirement.xls"));

 

      //Load workbook

      var wb = app.Workbooks("Retirement.xls");

 

      //Set input values

      wb.Names("Current_Age").Range.Value = Current_Age;

      wb.Names("Desired_Retirement_Age").Range.Value = Desired_Retirement_Age;

      wb.Names("Monthly_savings").Range.Value = Monthly_savings;

      wb.Names("Annual_Investment_growth_rate").Range.Value = Annual_Investment_growth_rate;

      wb.Names("Annual_Salary_Increase_Rate").Range.Value = Annual_Salary_Increase_Rate;

 

      Retirement_Nest_Egg=wb.Names("Retirement_Nest_Egg").Range.Value;

      duration=wb.Names("duration").Range.Value;

}

else

{

      Current_Age=36;

      Desired_Retirement_Age=80;

      Monthly_savings=100;

      Annual_Investment_growth_rate=0.05;

      Annual_Salary_Increase_Rate=0.05;

      Retirement_Nest_Egg=430302.43;

      duration=44;

}

%>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

 

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head><title>

      Retirement

</title>

    <style type="text/css">

    TD, INPUT, SELECT

    {

            font-family: Arial;

            font-size: 10pt;

      }

     

      TABLE#HTInput, #HTOutput

      {

            border-top:solid 1px #C0C0C0;

            border-left:solid 1px #C0C0C0;

      }

     

      TABLE#HTInput TD, TABLE#HTOutput TD

      {

            border-bottom:solid 1px #C0C0C0;

            border-right:solid 1px #C0C0C0;

      }

     

      TABLE#HTOutput TD

      {

            background-color:#CCCCFF;

      }

    </style>

</head>

<body>

    <form name="form1" method="post" action="Retirement.asp" id="form1">

    <div>

            <table id="HTInput" border="0" cellpadding="0" cellspacing="0" width="600">

      <tr>

            <td style="width: 328px">

                             Current Age

                        </td>

            <td>

            <select name="DdlCurrent_Age" id="DdlCurrent_Age">

            <%

                  for (i = 0; i <= 65; i++)

                  {

                        if(i==Current_Age)

                        {

            %>

                  <option selected="selected" value="<%=i%>"><%=i%></option>

            <%

                        }

                        else

                        {

            %>

                  <option value="<%=i%>"><%=i%></option>

            <%

                        }

                  }

            %>

            </select></td>

      </tr>

      <tr>

            <td style="width: 328px">

                             Desired Retirement Age

                        </td>

            <td>

            <select name="DdlDesired_Retirement_Age" id="DdlDesired_Retirement_Age">

            <%

                  for (i = 45; i <= 85; i++)

                  {

                        if(i==Desired_Retirement_Age)

                        {

            %>

                  <option selected="selected" value="<%=i%>"><%=i%></option>

            <%

                        }

                        else

                        {

            %>

                  <option value="<%=i%>"><%=i%></option>

            <%

                        }

                  }

            %>

            </select></td>

      </tr>

      <tr>

            <td style="width: 328px">

                             Monthly savings

            </td>

            <td>

                             <input name="TxtMonthly_savings" type="text" value="<%=Monthly_savings%>" id="TxtMonthly_savings" />

            </td>

      </tr>

      <tr>

            <td style="width: 328px">

                             Annual Investment growth rate (between 0-1)

            </td>

            <td>

                             <input name="TxtAnnual_Investment_growth_rate" type="text" value="<%=Annual_Investment_growth_rate%>" id="TxtAnnual_Investment_growth_rate" />

            </td>

      </tr>

      <tr>

            <td style="width: 328px">

                             Annual Salary Increase Rate (between 0-1)

            </td>

            <td>

                             <input name="TxtAnnual_Salary_Increase_Rate" type="text" value="<%=Annual_Salary_Increase_Rate%>" id="TxtAnnual_Salary_Increase_Rate" />

                             </td>

      </tr>

      <tr>

            <td style="width: 328px">

                             &nbsp;</td>

            <td>

                             &nbsp;</td>

      </tr>

      <tr>

            <td style="width: 328px">

            &nbsp;

            </td>

            <td>

                             <input type="submit" name="BtnCalculate" value="Calculate" id="BtnCalculate" /></td>

      </tr>

</table>

 

            <br />

            <%

            if (isPostBack)

            {

            %>

            <table id="HTOutput" border="0" cellpadding="0" cellspacing="0" width="600">

      <tr>

            <td style="width: 328px">

                             Retirement Nest Egg in $

                        </td>

            <td>

                             <span id="LblRetirement_Nest_Egg" style="font-weight:bold;"><%=Retirement_Nest_Egg.toLocaleString()%></span></td>

      </tr>

      <tr>

            <td style="width: 328px; height: 19px;">

                             Duration (max 100 years)

                        </td>

            <td style="height: 19px">

                             <span id="Lblduration" style="font-weight:bold;"><%=duration %></span></td>

      </tr>

</table>

            <%

            }

            %>

    </div>

</form>

</body>

</html>