   //
   //	compound.js
   //	Action Machinery's Web Site
   //	Written by: George Patterson
   //	CopyRight(c) 2003 George Patterson all rights reserved
   //	Last modified 7-10-2003
   //	Creates an Amortization Table from input based on the Form on compoundinterest.htm
   //

function ofmt(num){
num = Math.round(num * 100);
num = num / 100;
return num;
}

function fmt(num){
num = Math.round(num * 100);
num = num + ".";
numd = num.substring(0 , num.length - 3)
numc = num.substring(num.length - 3 , num.length - 1)
numdd = numd

if (numd.length > 18){
numh = numd.substring(numd.length - 3, numd.length)
numt = numd.substring(numd.length - 6, numd.length - 3)
numm = numd.substring(numd.length - 9, numd.length - 6)
numb = numd.substring(numd.length - 12, numd.length - 9)
numtr = numd.substring(numd.length - 15, numd.length - 12)
numz = numd.substring(numd.length - 18, numd.length - 15)
numzz = numd.substring(0, numd.length - 18)
numdd = numzz + "," + numz + "," + numtr + "," + numb + "," + numm + "," + numt + "," + numh
numd = ""
}


if (numd.length > 15){
numh = numd.substring(numd.length - 3, numd.length)
numt = numd.substring(numd.length - 6, numd.length - 3)
numm = numd.substring(numd.length - 9, numd.length - 6)
numb = numd.substring(numd.length - 12, numd.length - 9)
numtr = numd.substring(numd.length - 15, numd.length - 12)
numz = numd.substring(0, numd.length - 15)
numdd = numz + "," + numtr + "," + numb + "," + numm + "," + numt + "," + numh
numd = ""
}

if (numd.length > 12){
numh = numd.substring(numd.length - 3, numd.length)
numt = numd.substring(numd.length - 6, numd.length - 3)
numm = numd.substring(numd.length - 9, numd.length - 6)
numb = numd.substring(numd.length - 12, numd.length - 9)
numtr = numd.substring(0, numd.length - 12)
numdd = numtr + "," + numb + "," + numm + "," + numt + "," + numh
numd = ""
}

if (numd.length > 9){
numh = numd.substring(numd.length - 3, numd.length)
numt = numd.substring(numd.length - 6, numd.length - 3)
numm = numd.substring(numd.length - 9, numd.length - 6)
numb = numd.substring(0, numd.length - 9)
numdd = numb + "," + numm + "," + numt + "," + numh
numd = ""
}

if (numd.length > 6){
numh = numd.substring(numd.length - 3, numd.length)
numt = numd.substring(numd.length - 6, numd.length - 3)
numm = numd.substring(0, numd.length - 6)
numdd = numm + "," + numt + "," + numh
numd = ""
}

if (numd.length > 3){
numh = numd.substring(numd.length - 3, numd.length)
numt = numd.substring(0, numd.length - 3)
numdd = numt + "," + numh
numd = ""
}
numd = numdd
num =  "$ " + numd + "." + numc
return num;
}

function Calc(form){
var Prin = form.prin.value
var Down = form.deposit.value
if (Down > 0){
if (form.deptype[1].checked){
var temp = (Prin * (Down / 100 ))
var Down = temp}
var temp = (Prin - Down)
var Prin = temp
}
form.principal.value = fmt(Prin)
var Interest = (form.interest.value / 100)
var Years = form.years.value 
var Months = (Years * 12)
var Periods = (Years * 12)
var side1 = (1+(Interest/12))
var side2 = (-1)*(Months)
var tempa =(1-(Math.pow(side1, side2))) / (Interest / 12)
var temp = Prin / tempa
var payment = temp 
var TotalInterest = (payment * Months) - Prin
var Perhour = (payment / 160)
form.payment.value =fmt(payment)
form.totalinterest.value = fmt(TotalInterest)
form.perhour.value = fmt(Perhour)
}



function amort(form){
amor = open("","amor");
window.amor.focus()
var Prin = form.prin.value
var Down = form.deposit.value
if (Down > 0){
if (form.deptype[1].checked){
var temp = (Prin * (Down / 100 ))
var Down = temp}
var temp = (Prin - Down)
var Prin = temp
}
var Interest = (form.interest.value / 100)
var Years = form.years.value 
var Months = (Years * 12)
var Periods = (Years * 12)
var side1 = (1+(Interest/12))
var side2 = (-1)*(Months)
var tempa =(1-(Math.pow(side1, side2))) / (Interest / 12)
var temp = Prin / tempa
var payment = temp 
var TotalInterest = (payment * Months) - Prin
var Perhour = (payment / 160)
var Intsum = 0
var Prevprin = Prin
amor.document.writeln ("<html>")
amor.document.writeln ("<head>")
amor.document.writeln ("<title>Action Machinery Co., Inc. Compound Interest Amort Table</title>")
amor.document.writeln ("</head>")
amor.document.writeln ("<body background=actionbk.gif bgcolor=#000000 text=yellow link=#3399FF vlink=#88888>")
amor.document.writeln ("<center>")
amor.document.writeln ("<font size=+2 color=yellow>")
amor.document.writeln ("Action Machinery's Compound Interest Calculator<BR>")
amor.document.writeln ("</font>")
amor.document.writeln ("(610) 691-6677 Phone (610) 694-0944 Fax<BR>")
amor.document.writeln ("2320 Highland Avenue - Bethlehem PA - 18020<BR>")
amor.document.writeln ("<a href=http://www.actionmachinery.com>WWW.ACTIONMACHINERY.COM</A><BR><BR>")
amor.document.writeln ("<font color=yellow>(See bottom of page for Printing Instructions)</font><BR>")
amor.document.writeln ("<HR>")
amor.document.writeln ("<font color=yellow>")
amor.document.writeln ("Total Amount " + fmt(form.prin.value) +"<br>")
if (Down <= 0 ){var Down = 0}
amor.document.writeln ("Down Payment Amount " + fmt(Down) + "<br>")
amor.document.writeln ("Interest Rate %" + form.interest.value + "<br>")
amor.document.writeln ("Years " + form.years.value + " Number of payments " + Months + "<br>")
amor.document.writeln ("Compounding Periods / Year 12<br>")
amor.document.writeln ("Monthly Payment " + fmt(payment) + "<br>")
amor.document.writeln ("Total Interest " + fmt(TotalInterest) + "<br>")
amor.document.writeln ("Cost Per Hour " + fmt(Perhour) + "<br>")
amor.document.writeln ("<HR>")
amor.document.writeln ("<table border=1 cellspacing=2 cellpadding=2>")
amor.document.writeln ("<tr><td width=100 align=right>Payment #</td><td width=160 align=right>Payment $</td><td width=160 align=right>To Interest</td><td width=160 align=right>To Principal</td><td width=160 align=right>Balance</td></tr>")

for (var i = 1; i<=Months; i++){
amor.document.writeln ("<table border=1 cellspacing=2 cellpadding=2>")
amor.document.write ("<tr><td width=100 align=right> " + i + "</td>");
amor.document.write ("<td width=160 align=right> " + fmt(payment) + "</td>");
var side1 = ( 1 + (Interest / 12) )
var side2 = (i - Months)
var tempa = (1-(Math.pow(side1, side2))) / (Interest / 12)
var temp1 = payment * tempa
var temp2 = Prevprin - temp1 
var temp3 = payment - temp2 
Intsum = Intsum + temp3
amor.document.write ("<td width=160 align=right>" + fmt(temp3) + "</td>");
amor.document.write ("<td width=160 align=right>" + fmt(temp2) + "</td>");
if (temp1 < .0001) {var temp1 = 0}
amor.document.write ("<td width=160 align=right>" + fmt(temp1) + "</td>");
Prevprin = temp1
amor.document.writeln ("</tr>");
amor.document.writeln ("</table>");
}
amor.document.writeln ("</font><BR><HR><BR>")
amor.document.writeln ("<table border=1 width=60%>")
amor.document.writeln ("<tr><td>")
amor.document.writeln ("To PRINT this Page - on the Menu Bar Select:<BR>")
amor.document.writeln ("EDIT - Select ALL<BR>")
amor.document.writeln ("FILE - Print<BR>")
amor.document.writeln ("Check off the \"SELECTION\" box in the Print Dialog window.<BR>")
amor.document.writeln ("Then Click OK and the page will print.<BR>")
amor.document.writeln ("</td></tr>")
amor.document.writeln ("</table><BR>")
amor.document.writeln ("<BR><HR><BR>")
amor.document.writeln ("</CENTER>")
amor.document.writeln ("</body>")
amor.document.writeln ("</html>")
amor.document.close

}
