<HTML>
<HEAD>
<TITLE>Total Value of Regular Monthly Deposits</TITLE>
<SCRIPT Language="JavaScript">
<!--
/**
* Function that returns the selected value
* after being passed a reference to a single choice
* selection list.
**/
function getSelectedValue(selectList){
return selectList[selectList.selectedIndex].value
}
/**
* Function to calculate the total savings based on fixed
* monthly deposits.
* This function does no testing of user data other than
* to compare the minimum number of years a GIC requires
* to the number of years entered. No checking is done
* that properly formatted data has been entered!
* Adding error checking is left as an exercise.
* The formula used here is:
* total = deposits * (((1 + monthly interest)^N*Y) - 1)/(monthly interest)
* where N the number of deposits per year (in this case 12),
* Y the number of years
* monthly interest = (%yearly interest/100)/number of deposits per year
* ^ means to the power of
* Here is the formula in JavaScript using the Math.pow(x,y) function
* that raises x to the power of y:
* total = deposits * (Math.pow((1 + monthlyInt),(depPerYear * years)) - 1)/(monthlyInt)
*
* Note: The GICType selection list values are in the format: "minYears,yearlyInterest"
* In other words the value contains a string with two separate pieces of data
* separated by a comma. To extract both pieces of data into separate variables,
* the comma is found and the substrings before and after it are extracted.
**/
function calculateTotal(frm){
var years = frm.years.value
var deposits = frm.deposits.value
var depPerYear = 12
var GICType = getSelectedValue(frm.GICType)
var comma = GICType.indexOf(",") //Find comma location in string
var minYears = GICType.substring(0,comma) //Extract substring before comma
var interest = GICType.substring(comma + 1) //Extract substring after comma
var monthlyInt = 0
var total = 0
//Convert all values to integers or numbers
years = parseFloat(years)
deposits = parseFloat(deposits)
minYears = parseInt(minYears)
interest = parseFloat(interest)/100
if (minYears > years){
frm.total.value = "Error"
alert("The number of years must be equal \nto or more than GIC minimum.\n")
}
monthlyInt = interest/depPerYear
total = deposits * (Math.pow((1 + monthlyInt),(depPerYear * years)) - 1)/(monthlyInt)
frm.total.value = total
}
//-->
</SCRIPT>
</HEAD>
<BODY bgcolor="#FFFFFF">
<p> </p>
<H2>Total Savings Calculator</H2>
<P>(fixed interest and fixed monthly deposits)</p>
<form method="post" action="">
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="right">Number of Years:</td>
<td><input type="text" name="years" size="4" maxlength="3"></td>
</tr>
<tr>
<td align="right">Monthly Deposit Amount:</td>
<td><input type="text" name="deposits" size="6" maxlength="5"></td>
</tr>
<tr>
<td align="right">GIC Type:</td>
<td>
<SELECT name="GICType">
<OPTION value="0,5">Grade A, No Fixed Term Certificate: 5%</OPTION>
<OPTION value="2,6">Grade B, Minimum 2 Year Certificate: 6%</OPTION>
<OPTION value="3,6.2">Grade B, Minimum 3 Year Certificate: 6.2%</OPTION>
<OPTION value="5,7">Grade C, Minimum 5 Year Certificate: 7%</OPTION>
</SELECT>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" name="Button" value="Calculate Total Savings" onClick="calculateTotal(this.form)">
<hr size="1" noshade>
</td>
</tr>
<tr>
<td align="right">Total Savings:</td>
<td>
<input type="text" name="total" size="30" maxlength="30">
</td>
</tr>
</table>
</form>
<p> </p>
</BODY>
</HTML>
|