<HTML><HEAD>
<!--
-------------------
Mortgage Calculator
-------------------
-->
<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers
/*
THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
Copyright (c)1998 by Charles River Media. All Rights Reserved.
This applet can only be re-used or modifed by license holders of the
JavaScript Cookbook CD-ROM. Credit must be given in the source
code and this copyright notice must be maintained. If you do
not hold a license to the JavaScript Cookbook, you may NOT
duplicate or modify this code for your own use.
Use at your own risk. No warranty is given or implied of the suitability
of this applet for any specific application. Neither Erica Sadun nor
Charles River Media will be held responsible for any unwanted effects
due to the use of this applet or any derivative.
*/
//------------------FORM UTILITIES----------------------------
// Set the focus and selection on a field
function setHilite()
{
document.forms[0].RATE.focus()
document.forms[0].RATE.select()
return true
}
// --------------------Math Strings---------------------
// Strip a floating point's digits to #.### -- you set the digits
function stripDigits(aNumber, digits)
{
var str = "" + aNumber
var b = str.lastIndexOf(".")
// add decimal point if needed
if (b < 0) str += "."
// pad with extra zeros in case we have too "round" a number
for (var i = 0; i < digits; i++) str += "0"
// extract existing decimal or just return
if (b >= 0) return(str).substring(0,b+1+digits)
return str
}
// --------------------Calculate Results--------------------
function calculate(aform)
{
// Recover the information from the form
var totalPayments = (aform.YEARS.options.selectedIndex + 2) * 60
var monthlyInterest = (parseFloat(aform.RATE.value) / 1200)
var principle = (aform.PRINC.options.selectedIndex + 1)*10000
// Compute the monthly payments using JavaScript math and
// a standard compound interest formula
var num = Math.pow(1 + monthlyInterest, totalPayments)
var monthly = principle * monthlyInterest * num / (num - 1)
// Update the form
aform.MONTHLY.value = "$"+stripDigits(monthly, 2)
aform.TOTAL.value = "$" + stripDigits(monthly * totalPayments, 2)
aform.INTEREST.value = "$" + stripDigits(monthly * totalPayments - principle, 2)
}
<!-- done hiding --></SCRIPT></HEAD>
<BODY bgcolor="ffffff" onLoad="return setHilite()">
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = CENTER>Mortgage Calculator</H1></FONT>
<BLOCKQUOTE>
<FONT COLOR="770000">
Use this JavaScript Applet to determine monthly mortgage payments.
</FONT>
<FONT SIZE=4>
<FORM>
Annual Percentage Rate: <INPUT TYPE="text" SIZE="10" NAME="RATE"
VALUE="5">%<p>
Amount of loan (in thousands): <SELECT NAME="PRINC" SIZE="1">
<OPTION VALUE="10">10 <OPTION VALUE="20">20
<OPTION VALUE="30">30 <OPTION VALUE="40">40
<OPTION VALUE="50">50 <OPTION VALUE="60">60
<OPTION VALUE="70" SELECTED>70 <OPTION VALUE="80">80
<OPTION VALUE="90">90 <OPTION VALUE="100">100
<OPTION VALUE="110">110 <OPTION VALUE="120">120
<OPTION VALUE="130">130 <OPTION VALUE="140">140
<OPTION VALUE="150">150 <OPTION VALUE="160">160
<OPTION VALUE="170">170 <OPTION VALUE="180">180
<OPTION VALUE="190">190 <OPTION VALUE="200">200
<OPTION VALUE="210">210 <OPTION VALUE="220">220
<OPTION VALUE="230">230 <OPTION VALUE="240">240
<OPTION VALUE="250">250 </SELECT><p>
Years of Loan: <SELECT NAME="YEARS" SIZE="1"
<OPTION VALUE="5">5
<OPTION VALUE="10">10 <OPTION VALUE="15">15
<OPTION VALUE="20">20 <OPTION VALUE="25">25
<OPTION VALUE="30" SELECTED>30 </SELECT>
<P><CENTER>
<INPUT TYPE="button" VALUE="Calculate Payments"
onClick="calculate(this.form);return setHilite()">
</CENTER>
<P>
Monthly Payments:
<INPUT TYPE="TEXT" SIZE=10 NAME="MONTHLY" VALUE="$0.00">
<P>
Total Cost of Loan:
<INPUT TYPE="TEXT" SIZE=10 NAME="TOTAL" VALUE="$0.00">
<P>
Total Interest Paid:
<INPUT TYPE="TEXT" SIZE=10 NAME="INTEREST" VALUE="$0.00">
</FONT></BLOCKQUOTE>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
This applet shows more extensive use of formulas than the
Tailor example. This mortgage calculator uses a compound interest
equation to determine monthly payments over the life of
a loan. Each piece of the equation is tightly coupled to
the input form. The rate is recovered from the text input's value.
The years and principle are calculated from the choices of
the selection box. Simple arithmetic converts selection
indices to mathematical values.<p>
<b>Please note:</b> These payment numbers are neither warranted nor are
they likely to be accurate.<p>
</FONT>
<b>Calculating Payments:</b>
<PRE><FONT COLOR="770000">
function calculate(aform)
{
// Recover the information from the form
var totalPayments = (aform.YEARS.options.selectedIndex + 2) * 60
var monthlyInterest = (parseFloat(aform.RATE.value) / 1200)
var principle = (aform.PRINC.options.selectedIndex + 1)*10000
// Compute the monthly payments using JavaScript math and
// a standard compound interest formula
var num = Math.pow(1 + monthlyInterest, totalPayments)
var monthly = principle * monthlyInterest * num / (num - 1)
// Update the form
aform.MONTHLY.value = "$"+stripDigits(monthly, 2)
aform.TOTAL.value = "$" + stripDigits(monthly * totalPayments, 2)
aform.INTEREST.value = "$" + stripDigits(monthly * totalPayments - principle, 2)
}
</PRE></FONT>
<h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>