// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function SetValue() {
   isNumber(document.theForm.AMOUNT.value);
   isNumber(document.theForm.RATE.value);
   document.theForm.PAYMENT.value = calcRound(calcPayment())
   document.theForm.INTEREST.value = calcRound(calcInterest())
    return(true)
}
function calcInterest() {
 payment=calcPayment();
   return ((payment*(document.theForm.YEARS.options[x].value*12))-document.theForm.AMOUNT.value );
}

function calcPayment() {
var payment
x=document.theForm.YEARS.selectedIndex ;
//alert(YEARS.options[x].value);
  payment= (document.theForm.AMOUNT.value*((document.theForm.RATE.value /(12*100))/(1-(Math.pow(1+(document.theForm.RATE.value /(12*100)),((document.theForm.YEARS.options[x].value *12)*-1))))));
 return(payment);
}

function isNumber(input) {
   for (var i=0;i<input.length;i++) {
       var oneChar = input.substring(i, i+1)
       if (oneChar < "0" || oneChar > "9") {
          if (oneChar != "." ) { 
             alert("You must fill in a number");
             return(false)
          }
       }
   }
   return(true)
}

function calcRound(num) {
   result="$"+Math.floor(num)+"." 
   n = result.length
   if (num>1000 && num<999999) {  
     result="$"+result.substring(1,n-4)+","+result.substring(n-4,n)
   }
   if (num>1000000) {  
     result = "$"+result.substring(1,n-7)+","+result.substring(n-7,n-4)+","+result.substring(n-4,n)
   }
   var cents=100*(num-Math.floor(num))+0.5
   result += Math.floor(cents/10)
   result += Math.floor(cents%10)
   return(result)
}



function NewPage() {
  
      text = ("<HEAD><TITLE>Amortization Table</TITLE><link rel=stylesheet href=webstyle.css type=text/css></HEAD>");
      text = (text +"<BODY BGCOLOR =  '#ffffff'>");
      text = (text +"<span class=heading>Amortization Table</span>");
      text = (text +"<UL><span class=maintextbold>Mortgage Amount: " + calcRound(document.theForm.AMOUNT.value));
      text = (text +"<BR><span class=maintextbold>Interest Rate: " + document.theForm.RATE.value + " %");
      text = (text +"<BR><span class=maintextbold>Mortgage Length: </span>" +document.theForm.YEARS.value + " Years" +"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Print' onClick='window.print()'></UL>");
      text = (text +"<div align=center><FONT SIZE=1>The following table is based on the information entered in the calculator form.</span>");
      text = (text +"<CENTER><table border='1' width='100%'>");
      text = (text +"<TR><TD ALIGN=CENTER BGCOLOR='#003466' class=maintext><FONT COLOR=WHITE><B>Year</B></FONT></TD><TD ALIGN=left BGCOLOR='#003466'><FONT COLOR=WHITE size=1><B>Interest&nbsp;</B></FONT></TD><TD ALIGN=left BGCOLOR='#003466'><FONT COLOR=WHITE size=1><B>Principal&nbsp;</B></FONT></TD><TD ALIGN=left BGCOLOR='#003466'><FONT COLOR=WHITE size=1><B>Balance&nbsp;</B></FONT></TD></TR>\n");
      
      makeTable()

      text = (text +"</TABLE></CENTER>");
      msgWindow=window.open("","displayWindow","toolbar=no,width=450,height=300,directories=no,status=no,scrollbars=yes,resize=no,menubar=no")
      msgWindow.document.write(text)
      msgWindow.document.close()
      return(true)
  
}

function makeTable() {
   var currInt = 0
   var currPrin = 0
   y=document.theForm.START.selectedIndex ;
   prevBalance = document.theForm.AMOUNT.value;
   InterestRate = ( document.theForm.RATE.value /100) / 12;
   MonthlyPayment = calcPayment();
   currStart = document.theForm.START.options[y].value;
  // alert(currStart);
   MortgagePeriods=12;
   for(i=1;i<=30;i++) {
      for(j=1;j<=MortgagePeriods;j++) {
         periodInt = prevBalance * InterestRate
         periodPrin = MonthlyPayment - periodInt
         currBal = prevBalance - periodPrin
         currInt += periodInt
         currPrin += periodPrin
         prevBalance = currBal
      }
      if( currBal <= 0 ){ 
         currBal = 0
      }
      text = (text +"<TR><TD ALIGN=CENTER class=maintext>"+ currStart +"</TD><TD ALIGN=RIGHT class=maintext>"+ calcRound(currInt) +"&nbsp;</TD><TD ALIGN=RIGHT class=maintext>"+ calcRound(currPrin) +"&nbsp;</TD><TD ALIGN=RIGHT class=maintext>"+ calcRound(currBal)+"&nbsp;</TD></TR>");
      currInt = 0
      currPrin = 0
      currStart = parseInt(currStart)
      currStart += 1
      if(currBal<=0) {
         return(true)
      }       
   }
   return (true)
}
//function setfocus() {   
 //  document.MORTGAGE.AMOUNT.focus()
//   document.MORTGAGE.AMOUNT.select()
//}

function printWindow() {
bV = parseInt(navigator.appVersion);
if (bV >= 4) window.print();
}