// JavaScript Document
function range(from,to){
var temp=[];
for(var i=from;i<=to;i++){
temp.push(i);
}
return temp;
}

function populateMenuFromArray(men,arr,header){
var start=header?+1:+0,a=arr.length;
for(var i=start,j=0;j<a;i++){
men.options[i]=new Option(arr[j++]);
}
}

function toCurrency(val,currency){
val=(val+"").replace(/\,/g,"");
return  ((currency? currency:"") + (Math.round(val*100)+(val<0?-0.1:+0.1))/ 100).replace(/(.*\.\d\d)\d*/,'$1');
}

onload=function(){
populateMenuFromArray(document.forms["mort"].years,range(1990,new Date().getFullYear()+1).reverse(),true);
document.forms["mort"].Calc.onclick=function(){
if(checkInputs()){
with(this.form){
monthlyPayment.value=
toCurrency(getMonthlyPayment(prin.value,apr.value,12,yrs.value),"$");
}
}
}
}

function numsOnly(v){
return v.replace(/[^\d.]/g,"")
}

function checkInputs(){var msg="";
with(document.forms["mort"]){
prin.value=numsOnly(prin.value);apr.value=numsOnly(apr.value);
if(isNaN(prin.value) || prin.value<1) msg+="Principal out of range\n";
if(isNaN(apr.value) || apr.value<.1 || apr.value >100) msg+="Interest out of range\n";
if(isNaN(yrs.value) || yrs.value<1 || yrs.value >100 || yrs.value%1!=0) msg+="Years out of range";
if(msg){
alert(msg);
return false;
}
}
return true;
}

function getMonthlyPayment(principal,apr,ppy,years){
a=Math.pow(1+apr/100/ppy,ppy*years);
return principal*a*(apr/100/ppy)/(a-1);
}

// Amortization

function amortize(prin,yrs,apr){
var m,y,thePrincipal=+(prin),thePayment=getMonthlyPayment(prin,apr,12,yrs),f=document.forms["mort"];
var months=["January","February","March","April","May","June","July","August","September","October","November","December"];
var startDate= ((m=f.months.selectedIndex) && (y=f.years.selectedIndex))?new Date(f.years[y].text,m-1,1):new Date();
startDate.setMonth(startDate.getMonth()-1);
var txt='<html>\n<head>\n<title>Repayment Schedule</title>\n';
txt+='<style type="text/css">\ntd, th {font-family: "Courier New", Courier, mono;font-size: 11px; }\nbody { background-color:white; } \ntd{text-align: right;}\n</style>\n</head>\n';
txt+='<body>\n<center>\n<img src="../images/script.gif" width="377" height="40">\n<form>\n';
txt+='<table cellpadding="5" cellspacing="0" width="377">\n';
txt+='<tr bgcolor="#e4e4f2">\n<th>Pmt</th>\n<th>Date</th>\n<th>Payment<br>Amount</th>\n<th>Interest</th>\n<th>Principal</th>\n<th>Balance</th>\n</tr>';
for(i=1;i<=yrs*12;i++){
startDate.setMonth(startDate.getMonth()+1);
var interest=thePrincipal*apr/12/100;
thePrincipal+=interest;
var principal=thePayment-interest;
thePrincipal-=thePayment;

txt+='\n<tr bgcolor="'+((i%2!=0) ? '#E1FFE5' : '#FFFFFF')+'">\n<td>';
txt+=i+'</td>\n<td>';
txt+=months[startDate.getMonth()].substring(0,3)+' '+startDate.getFullYear()+'</td>\n<td>';
txt+=toCurrency(thePayment,"$")+'</td>\n<td>';
txt+=toCurrency(interest,"$")+'</td>\n<td>';
txt+=toCurrency(principal,"$")+'</td>\n<td>';
txt+=toCurrency(thePrincipal,"$")+'</td>\n</tr>\n';
}
txt+='</table>';
txt+='<br><br>\n<input type="button" value="Close the Window" onClick="self.close()">\n</center>\n</form>\n</body>\n</html>';
var theSchedule=window.open('', '','scrollbars=yes,top=50,left=300,width=450,height=450');
theSchedule.document.write(txt);
theSchedule.document.close();
}

