var amount, apr, n, payment, npy, tablebuilt=false;

function calculate(){
         var d=document.f;
         amount=d.amount.value;
         apr=d.apr.value;
         n=d.n.value;
         npy=d.npy.value;
         if( (amount!='') && (n!='') && (apr!='') && (npy!='') ){
         tmp=Math.pow((1+(apr/100/npy)), (n*npy));
         //alert((1+(apr/100/npy)));
         //alert(n*npy);
         //alert(tmp);
         alert(apr/100/npy);
         payment=(amount*tmp*(apr/100/npy))/(tmp-1);

         if((!isNaN(payment))&&(payment!=Number.POSITIVE_INFINITY)&&(payment!=Number.NEGATIVE_INFINITY)){
         d.payment.value=round(payment);
         d.totpaid.value=round(payment*n*npy);
         d.intpaid.value=round((payment*n*npy)-amount);
         }else alert('Error: One or more fields contain data\nwhich cannot be used in the calculation. \n\nCheck you have only used numbers and \ndecimal points in the text fields.');
         }else alert('Error:\nYou did not provide enough data.');

d.payment.focus();
}

function round(val){
tmp=Math.round(val*100)/100+'';
if(tmp.indexOf('.')==-1)tmp+='.00';
else if(tmp.length-tmp.indexOf('.')==2)tmp+='0';
return tmp;
}

function resetall(){
var d=document.f;
d.amount.value='';
d.apr.value='';
d.n.value='';
d.npy.value=12;
d.payment.value='';
d.totpaid.value='';
d.intpaid.value='';
d.amount.focus();
}





function buildtable(){
tablebuilt=true;
txt='<html><head><title>Payment Schedule</title></head><body bgcolor="#FFFFFF"><center><form>';
txt+='<input type="button" value="Close" onClick="window.close()"><br><br><em>to print this table, right-click with your mouse and<br>select print from the context menu...</em><br><br>';
txt+='<table bgcolor="#FFFFFF" border="0" cellpadding="4" cellspacing="3">';
txt+='<tr><strong><td align="center">Payment<br>Number</td><td align="center">Monthly<br>payment</td><td align="center">Principle<br>amount</td><td align="center">Interest<br>amount</td><td align="center">Remaining<br>balance</td></strong></tr>';
amount=eval(amount);
for(i=1;i<=n*npy;i++){
tbldata='<td bgcolor="'+((i%2!=0) ? '#99cc66' : '#99cc88')+'" align="right">';
interest=amount*apr/npy/100;
amount+=interest;
principle=payment-interest;
amount-=payment;
    txt+='<tr>'+tbldata+i+'</td>'+tbldata+round(payment)+'</td>'+tbldata+round(principle)+'</td>'+tbldata+round(interest)+'</td>'+tbldata+round(amount)+'</td></tr>';
}
txt+='</table><br><br><input type="button" value="Close" onClick="self.close()"></center></form></body></html>';
var psch=window.open('', 'viewsch' ,'top=0,left=0,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,menubar=no,status=no');
psch.document.write(txt);
}
