I modified a mortgage calculator to amortize based on the starting month. The logic is fine and everything works nice in IE, but the algorythm gets hung in Netscape. Can anyone identify if one of the methods or functions is erroneous.
Whole code for the page is included...
<HTML>
<HEAD>
<TITLE>Loan Calculator</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function RND2
{
pennies = n * 100;
pennies = Math.round(pennies);
strPennies = "" + pennies;
len = strPennies.length;
return strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);
}
function Monthly(principal, years, apr)
{
rate = apr / 12;
payments = years * 12;
return RND2(principal * rate / (1 - (1 / Math.pow(1 + rate, payments))));
}
function Amortize(principal, years, apr, mon)
{
payments = years * 12;
monthlyInterest = apr / 12;
monthlyPayment = Monthly(principal, years, apr);
document.write("<HEAD><TITLE>AMORTIZED!!!</TITLE></HEAD>"
document.write("<style type='text/css'>td, th {font-family : Verdana, Arial, Helvetica;font-size : 9pt;font-style : normal}</style>"
document.write("<BODY>"
document.write("<TABLE BORDER=0 align=center>"
document.write("<TR><TD align=center colspan=4><H1>Amortization Table</H1></TD></TR>"
document.write("<TR><TD align=center colspan=4><HR></TD></TR>"
document.write("<TR>"
document.write("<TH COLSPAN=4>"
document.write("$" + RND2(principal));
document.write(" at " + RND2(apr) + "%"
document.write(" over " + years + " years.<BR>"
document.write("Monthly payment: $" + Monthly(principal, years, apr));
document.write("</TH>"
document.write("</TR>"
document.write("<TR>"
document.write("<TH></TH>"
document.write("<TH COLSPAN=2>Payment</TH>"
document.write("</TR>"
document.write("<TR>"
document.write("<TH>Month</TH>"
document.write("<TH>Interest</TH>"
document.write("<TH>Principal</TH>"
document.write("<TH>Balance</TH>"
document.write("</TR>"
var mnth = mon + 1;
var strMnth = "";
var yr = 1;
var yrInt = 0;
for(i = 1; i <= payments; i++)
{
if (mnth == 1)
strMnth="January";
if (mnth == 2)
strMnth="February";
if (mnth == 3)
strMnth="March";
if (mnth == 4)
strMnth="April";
if (mnth == 5)
strMnth="May";
if (mnth == 6)
strMnth="June";
if (mnth == 7)
strMnth="July";
if (mnth == 8)
strMnth="August";
if (mnth == 9)
strMnth="September";
if (mnth == 10)
strMnth="October";
if (mnth == 11)
strMnth="November";
if (mnth == 12)
strMnth="December";
if (mnth > 12)
strMnth = i;
if ((i == 1) || (mnth == 1))
{
document.write("<TR><TD><B>YEAR " + yr + "</B></TD>"
document.write("<TD colspan=3>Total Interest Paid: $" + RND2(yrInt) + "</TD></TR>"
yrInt = 0;
}
document.write("<TR>"
document.write("<TD> " + i + " " + strMnth + " </TD>"
interestPayment = principal * monthlyInterest;
yrInt = yrInt + interestPayment;
document.write("<TD> $" + RND2(interestPayment) + " </TD>"
principalPayment = monthlyPayment - interestPayment;
document.write("<TD> $" + RND2(principalPayment) + " </TD>"
principal -= principalPayment;
document.write("<TD> $" + RND2(principal) + " </TD>"
document.write("</TR>"
if (mnth <= 12)
{
mnth = mnth + 1;
}
if (mnth > 12)
{
mnth = 1;
yr = yr + 1;
}
}
document.write("</TABLE>"
document.write("</BODY>"
}
function calculate(principal, years, apr, mon)
{
payments = years * 12;
monthlyInterest = apr / 12;
monthlyPayment = Monthly(principal, years, apr);
document.form.Total.value = RND2(monthlyPayment);
var Tint = 0;
for(i=1;i<payments;i++)
{
interestPayment = principal * monthlyInterest;
principalPayment = monthlyPayment - interestPayment;
principal -= principalPayment;
Tint = Tint + interestPayment;
}
document.form.Tint.value = RND2(Tint);
}
function compute(form)
{
if((form.principal.value.length != 0) &&
(form.apr.value.length != 0) &&
(form.years.value != "0" && (form.month.value != "0")
{
principal = eval(form.principal.value);
apr = eval(form.apr.value) / 100.0;
years = (form.years.value);
month = eval(form.month.value);
if(years == 0.0)
{
alert(
"You have no monthly payment, since the number of years is zero."
}
else
{
Amortize(principal, years, apr, month);
}
}
else
{
alert("You must fill in all the fields."
}
}
function compute2(form)
{
if((form.principal.value.length != 0) &&
(form.apr.value.length != 0) &&
(form.years.value != "0" && (form.month.value != "0")
{
principal = eval(form.principal.value);
apr = eval(form.apr.value) / 100.0;
years = (form.years.value);
month = eval(form.month.value);
if(years == 0.0)
{
alert(
"You have no monthly payment, since the number of years is zero."
}
else
{
calculate(principal, years, apr, month);
}
}
else
{
alert("You must fill in all the fields."
}
}
//->
</SCRIPT>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></HEAD>
<style type="text/css">
input, td {
font-family : Verdana, Arial, Helvetica;
font-size : 9pt;
font-style : normal
}
</style>
<BODY bgcolor="333366">
<CENTER>
</CENTER>
<CENTER>
<FORM name=form>
<CENTER>
<table width="500" border="0" cellspacing="0" cellpadding="0" align=center>
<tr>
<td valign="top"><div align="center"><img src="images/mort_calc_txt.gif"></div></td>
</tr>
<tr>
<td>
<TABLE BORDER=0 align=center>
<TR>
<TD colspan=2 align=center><b><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">Please
fill in the fields.</font></b></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Loan
Amount ($)</font></TD>
<TD><INPUT TYPE=TEXT NAME=principal></TD>
</TR>
<TR>
<TD> </TD>
<TD valign="top"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">Please do not include commas.
<TR>
<TD align=right><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">APR(%) </font></TD>
<TD><INPUT TYPE=TEXT NAME=apr></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Length
of loan: </font></TD>
<TD><SELECT NAME=years>
<OPTION value="0"> -SELECT- </OPTION>
<OPTION value="5">5</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="15">15</OPTION>
<OPTION value="20">20</OPTION>
<OPTION value="25">25</OPTION>
<OPTION value="30">30</OPTION>
</SELECT> </TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF" size="2">Starting month: </font></TD>
<TD><SELECT NAME=month>
<OPTION value=0>-SELECT-</OPTION>
<OPTION value=1>January</OPTION>
<OPTION value=2>February</OPTION>
<OPTION value=3>March</OPTION>
<OPTION value=4>April</OPTION>
<OPTION value=5>May</OPTION>
<OPTION value=6>June</OPTION>
<OPTION value=7>July</OPTION>
<OPTION value=8>August</OPTION>
<OPTION value=9>September</OPTION>
<OPTION value=10>October</OPTION>
<OPTION value=11>November</OPTION>
<OPTION value=12>December</OPTION>
</SELECT> </TD>
</TR>
<TR>
<TD align=right><INPUT name="BUTTON" TYPE=BUTTON onClick=compute(this.form) VALUE="Amortize">
</TD>
<TD><INPUT name="BUTTON" TYPE=BUTTON onClick=compute2(this.form) VALUE="Calculate"></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF">Payment Amount: $</font></TD>
<TD><INPUT type=text name=Total></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF">Total Interest: $</font></TD>
<TD><INPUT type=text name=Tint></TD>
</TR>
</TABLE></td>
</tr>
</table>
</CENTER>
</FORM>
</CENTER>
</BODY>
</HTML>
Whole code for the page is included...
<HTML>
<HEAD>
<TITLE>Loan Calculator</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function RND2
{
pennies = n * 100;
pennies = Math.round(pennies);
strPennies = "" + pennies;
len = strPennies.length;
return strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);
}
function Monthly(principal, years, apr)
{
rate = apr / 12;
payments = years * 12;
return RND2(principal * rate / (1 - (1 / Math.pow(1 + rate, payments))));
}
function Amortize(principal, years, apr, mon)
{
payments = years * 12;
monthlyInterest = apr / 12;
monthlyPayment = Monthly(principal, years, apr);
document.write("<HEAD><TITLE>AMORTIZED!!!</TITLE></HEAD>"
document.write("<style type='text/css'>td, th {font-family : Verdana, Arial, Helvetica;font-size : 9pt;font-style : normal}</style>"
document.write("<BODY>"
document.write("<TABLE BORDER=0 align=center>"
document.write("<TR><TD align=center colspan=4><H1>Amortization Table</H1></TD></TR>"
document.write("<TR><TD align=center colspan=4><HR></TD></TR>"
document.write("<TR>"
document.write("<TH COLSPAN=4>"
document.write("$" + RND2(principal));
document.write(" at " + RND2(apr) + "%"
document.write(" over " + years + " years.<BR>"
document.write("Monthly payment: $" + Monthly(principal, years, apr));
document.write("</TH>"
document.write("</TR>"
document.write("<TR>"
document.write("<TH></TH>"
document.write("<TH COLSPAN=2>Payment</TH>"
document.write("</TR>"
document.write("<TR>"
document.write("<TH>Month</TH>"
document.write("<TH>Interest</TH>"
document.write("<TH>Principal</TH>"
document.write("<TH>Balance</TH>"
document.write("</TR>"
var mnth = mon + 1;
var strMnth = "";
var yr = 1;
var yrInt = 0;
for(i = 1; i <= payments; i++)
{
if (mnth == 1)
strMnth="January";
if (mnth == 2)
strMnth="February";
if (mnth == 3)
strMnth="March";
if (mnth == 4)
strMnth="April";
if (mnth == 5)
strMnth="May";
if (mnth == 6)
strMnth="June";
if (mnth == 7)
strMnth="July";
if (mnth == 8)
strMnth="August";
if (mnth == 9)
strMnth="September";
if (mnth == 10)
strMnth="October";
if (mnth == 11)
strMnth="November";
if (mnth == 12)
strMnth="December";
if (mnth > 12)
strMnth = i;
if ((i == 1) || (mnth == 1))
{
document.write("<TR><TD><B>YEAR " + yr + "</B></TD>"
document.write("<TD colspan=3>Total Interest Paid: $" + RND2(yrInt) + "</TD></TR>"
yrInt = 0;
}
document.write("<TR>"
document.write("<TD> " + i + " " + strMnth + " </TD>"
interestPayment = principal * monthlyInterest;
yrInt = yrInt + interestPayment;
document.write("<TD> $" + RND2(interestPayment) + " </TD>"
principalPayment = monthlyPayment - interestPayment;
document.write("<TD> $" + RND2(principalPayment) + " </TD>"
principal -= principalPayment;
document.write("<TD> $" + RND2(principal) + " </TD>"
document.write("</TR>"
if (mnth <= 12)
{
mnth = mnth + 1;
}
if (mnth > 12)
{
mnth = 1;
yr = yr + 1;
}
}
document.write("</TABLE>"
document.write("</BODY>"
}
function calculate(principal, years, apr, mon)
{
payments = years * 12;
monthlyInterest = apr / 12;
monthlyPayment = Monthly(principal, years, apr);
document.form.Total.value = RND2(monthlyPayment);
var Tint = 0;
for(i=1;i<payments;i++)
{
interestPayment = principal * monthlyInterest;
principalPayment = monthlyPayment - interestPayment;
principal -= principalPayment;
Tint = Tint + interestPayment;
}
document.form.Tint.value = RND2(Tint);
}
function compute(form)
{
if((form.principal.value.length != 0) &&
(form.apr.value.length != 0) &&
(form.years.value != "0" && (form.month.value != "0")
{
principal = eval(form.principal.value);
apr = eval(form.apr.value) / 100.0;
years = (form.years.value);
month = eval(form.month.value);
if(years == 0.0)
{
alert(
"You have no monthly payment, since the number of years is zero."
}
else
{
Amortize(principal, years, apr, month);
}
}
else
{
alert("You must fill in all the fields."
}
}
function compute2(form)
{
if((form.principal.value.length != 0) &&
(form.apr.value.length != 0) &&
(form.years.value != "0" && (form.month.value != "0")
{
principal = eval(form.principal.value);
apr = eval(form.apr.value) / 100.0;
years = (form.years.value);
month = eval(form.month.value);
if(years == 0.0)
{
alert(
"You have no monthly payment, since the number of years is zero."
}
else
{
calculate(principal, years, apr, month);
}
}
else
{
alert("You must fill in all the fields."
}
}
//->
</SCRIPT>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></HEAD>
<style type="text/css">
input, td {
font-family : Verdana, Arial, Helvetica;
font-size : 9pt;
font-style : normal
}
</style>
<BODY bgcolor="333366">
<CENTER>
</CENTER>
<CENTER>
<FORM name=form>
<CENTER>
<table width="500" border="0" cellspacing="0" cellpadding="0" align=center>
<tr>
<td valign="top"><div align="center"><img src="images/mort_calc_txt.gif"></div></td>
</tr>
<tr>
<td>
<TABLE BORDER=0 align=center>
<TR>
<TD colspan=2 align=center><b><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">Please
fill in the fields.</font></b></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Loan
Amount ($)</font></TD>
<TD><INPUT TYPE=TEXT NAME=principal></TD>
</TR>
<TR>
<TD> </TD>
<TD valign="top"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">Please do not include commas.
<TR>
<TD align=right><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">APR(%) </font></TD>
<TD><INPUT TYPE=TEXT NAME=apr></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Length
of loan: </font></TD>
<TD><SELECT NAME=years>
<OPTION value="0"> -SELECT- </OPTION>
<OPTION value="5">5</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="15">15</OPTION>
<OPTION value="20">20</OPTION>
<OPTION value="25">25</OPTION>
<OPTION value="30">30</OPTION>
</SELECT> </TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF" size="2">Starting month: </font></TD>
<TD><SELECT NAME=month>
<OPTION value=0>-SELECT-</OPTION>
<OPTION value=1>January</OPTION>
<OPTION value=2>February</OPTION>
<OPTION value=3>March</OPTION>
<OPTION value=4>April</OPTION>
<OPTION value=5>May</OPTION>
<OPTION value=6>June</OPTION>
<OPTION value=7>July</OPTION>
<OPTION value=8>August</OPTION>
<OPTION value=9>September</OPTION>
<OPTION value=10>October</OPTION>
<OPTION value=11>November</OPTION>
<OPTION value=12>December</OPTION>
</SELECT> </TD>
</TR>
<TR>
<TD align=right><INPUT name="BUTTON" TYPE=BUTTON onClick=compute(this.form) VALUE="Amortize">
</TD>
<TD><INPUT name="BUTTON" TYPE=BUTTON onClick=compute2(this.form) VALUE="Calculate"></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF">Payment Amount: $</font></TD>
<TD><INPUT type=text name=Total></TD>
</TR>
<TR>
<TD align=right><font color="#FFFFFF">Total Interest: $</font></TD>
<TD><INPUT type=text name=Tint></TD>
</TR>
</TABLE></td>
</tr>
</table>
</CENTER>
</FORM>
</CENTER>
</BODY>
</HTML>