var agt=navigator.userAgent.toLowerCase(); 
var is_major = parseInt(navigator.appVersion); 
var is_ie   = (agt.indexOf("msie") != -1); 
var is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.")==-1) ); 
var is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) ); 

window.onerror=null
var bState = true;
var rec = 0;
var oTable = new Array();

oReq = new Collection("AMOUNT","RATE","","","","")
oVal = new Collection("AMOUNT","RATE","","","","")
oTst = new Collection("N","N","","","","")

function startAmortization(formname) {
   while (bState) {
      if (!Required(formname)) break;
      if (!Validate(formname)) break;
      if (!Create()) break;
      if (!SetValue(formname)) break;
      createRecords();
      showAmortize(formname);
      if (bState) bState = false;
   }
   bState = true
}

function showPayment(formname) {
   while (bState) {
      if (!Required(formname)) break;
      if (!Validate(formname)) break;
      if (!Create()) break;
      if (!SetValue(formname)) break;
      if (bState) bState = false;
   }
   bState = true
}

function Required(oView) {
   for (i in oView ) {
      for (j in oReq) {
          if (i == oReq[j]) {      
             if (isMissing(oView[i])) {
               return(false)
             }
          }
      } 
  }
  return(true)
}

function isMissing(oCtrl) {
   if (oCtrl.value == "") {  
      alert("You have left a required value blank.");
      oCtrl.focus();
      oCtrl.select();
      return(true)
      }
   else { return(false) }
}

function Validate(oView) {
   for (i in oView) {
      for (j in oVal) {
        if (i==oVal[j] && oTst[j]=="N") {      
           if (isTest(oView[i], oTst[j])) {
               return(false)
           }
        }
     }
  }
  return(true)
}

function isTest(oCtrl, oTest) {
   if (oTest=="N" && !isNumber(oCtrl.value) ) {
      alert("'"+oCtrl.value+"' contains an invalid character. Please make sure this is a number.");
      oCtrl.focus();
      oCtrl.select();
      return(true)
      }
      else { return(false) }
}


function isNumber(input) {
var result = ""
var nperiods = 0;

   for (var i=0;i<input.length;i++) {
      var ch = input.substring(i, i+1);
      if (!isNaN(parseInt(ch))) { result = result + ch }
      if (ch==".") { nperiods++; }
      if (nperiods==1){ result = result + ch }
   }

   if (result == ""){ return(false); }
return(true)
}

function makeNumber(input) {
var result = ""
var nperiods = 0;

   for (var i=0;i<input.length;i++) {
       var ch = input.substring(i, i+1);
       var flag = true;

       if (!isNaN(parseInt(ch))) { result = result + ch }
       if (ch==".") { nperiods++; }

       if (nperiods==1){ result = result + ch }
     }
  return(result)
}


function Create() {
   Mortgage = new Loan(50000, 8, 30, 0, 0, "Monthly", 0 );
   return(true)
}

function SetValue(formname) { 
   Mortgage.Amount = makeNumber(formname.AMOUNT.value);
   formname.AMOUNT.value = calcRound(Mortgage.Amount);
   Mortgage.Rate = Number(formname.RATE.value).toFixed(2); //round off to 2 decimal places
   //Mortgage.Rate = makeNumber(formname.RATE.value);
   Mortgage.Frequency = "Monthly";
   Mortgage.Term = makeNumber(formname.YEARS.options[formname.YEARS.selectedIndex].value);
   //Mortgage.Term = 10;
   Mortgage.calcPeriods();
   Mortgage.calcPayment();
   Mortgage.calcInterest();
   Show()
   return(true)
}

function createRecords(){
   rec = 0;
   oTable = new Array();

   var currInt = 0;
   var currPrin = 0;
   var months = 0;
   prevBalance = Mortgage.Amount;
   InterestRate = ( Mortgage.Rate / 100 ) / Mortgage.Periods;
   MonthlyPayment = Mortgage.Payment;
   months = Mortgage.Term * 12;
   for(i=1; i<=months; i++) {
         periodInt = prevBalance * InterestRate;
         periodPrin = MonthlyPayment - periodInt; 
         currBal = prevBalance - periodPrin;
         currInt += periodInt;
         currPrin += periodPrin;
         prevBalance = currBal;	
      if( currBal <= 0 ) { currBal = 0; }
	  Month = i;
      Interest = calcRound(currInt);
      Principle = calcRound(currPrin);
      Balance = calcRound(currBal);
      rec++;
      addRecord(Month, Interest, Principle, Balance);
      currInt = 0;
      currPrin = 0;
      if(currBal<=0) { return(true) }       
   }
   return (true)
}

function showAmortize(oForm) {
   var monthPm = oTable.length - 1;
   text = ("<html><HEAD><link rel=StyleSheet href=master.css></HEAD>");
   text = (text +"<BODY bgcolor=#8aa740 class=bodytext text=black>");
   text = (text +"<img border=0 src='pix/t_amortizationtable.gif'><br><br>");
   text = (text +"<FONT COLOR=black>The following table is based on the information entered in the calculator form.</font>");
   text = (text +"<p><FONT COLOR=black> <b>Mortgage Amount:</b> </FONT><FONT COLOR=black>" + calcRound(Mortgage.Amount) + "</font>");
   text = (text +"<BR><FONT COLOR=black> <b>Interest Rate:</b> </FONT><FONT COLOR=black>" + Mortgage.Rate + " %</font>");
   text = (text +"<BR><FONT COLOR=black> <b>Mortgage Length:</b> </FONT><FONT COLOR=black>" + Mortgage.Term + " Years</font>");
   text = (text +"<BR><FONT COLOR=black> <b>Payment Frequncy:</b> </FONT><FONT COLOR=black>" + Mortgage.Frequency + "</font>");
text = (text +"<BR><FONT COLOR=black> <b>Payment Months:</b> </FONT><FONT COLOR=black>" + monthPm + "</font>");
   text = (text +"<BR><br><div=center><table border='0' width='100%' cellpadding=2 cellspacing=1>");
   text = (text +"<TR><TD ALIGN=CENTER BGCOLOR='#435639' class=bodytext><FONT COLOR=white><B>Payment #</B></FONT></TD><TD ALIGN=RIGHT BGCOLOR='#435639' class=bodytext><FONT COLOR=white><B>Interest&nbsp;</B></FONT></TD><TD ALIGN=RIGHT BGCOLOR='#435639' class=bodytext><FONT COLOR=white><B>Principal&nbsp;</B></FONT></TD><TD ALIGN=RIGHT BGCOLOR='#435639' class=bodytext><FONT COLOR=white><B>Balance&nbsp;</B></FONT></TD></TR>\n");
     
   for (var q=1; q<oTable.length;q++) {
   	  if (q % 2 == 0) bg = '#dcdcdc'; else bg = 'white';
      text = (text +"<TR bgcolor=" + bg + "><TD ALIGN=CENTER class=bodytext>"+ oTable[q].Month +"</TD><TD ALIGN=RIGHT class=bodytext>$"+ oTable[q].Interest +"&nbsp;</TD><TD ALIGN=RIGHT class=bodytext>$"+ oTable[q].Principle +"&nbsp;</TD><TD ALIGN=RIGHT class=bodytext>$"+ oTable[q].Balance +"&nbsp;</TD></TR>");
   }

   text = (text +"</TABLE></div><br><center><a href='javascript:self.close();'><font color=white face=arial size=2><b>Close Window</b></a></center></body></html>");
   oWindow=window.open("","displayWindow","toolbar=no,width=540,height=500,directories=no,status=no,scrollbars=yes,resize=no,menubar=yes");
   oWindow.document.write(text);
   oWindow.document.close();
   return(true)
}

function Collection(item1, item2, item3, item4, item5, item6) {
   this.item1 = item1;
   this.item2 = item2;
   this.item3 = item3;
   this.item4 = item4;
   this.item5 = item5;
   this.item6 = item6;
}

function Loan(Amount, Rate, Term, Payment, Interest, Frequency, Periods ) {
   this.Amount = Amount;
   this.Rate = Rate;
   this.Term = Term;
   this.Payment = Payment;
   this.Interest = Interest;
   this.Frequency = Frequency;
   this.Periods = Periods;
   this.calcPeriods = calcPeriods;
   this.calcPayment = calcPayment;
   this.calcInterest = calcInterest;
}

function calcPayment() {
   this.Payment = (this.Amount*((this.Rate/(1200))/(1-(Math.pow(1+(this.Rate/(1200)),((this.Term*12)*-1))))));
}

function calcInterest() {
   this.Interest = ((this.Payment*(this.Term*this.Periods))-this.Amount);
}

function calcPeriods() {
   if (this.Frequency=="Monthly") { this.Periods=12 } else { this.Periods=26 }
}

function Show(oView) {
   document.MORTGAGE.PAYMENT.value = calcRound(Mortgage.Payment)
   document.MORTGAGE.INTEREST.value = calcRound(Mortgage.Interest)
}

function oRecord(Month, Interest, Principle, Balance) {
   this.Month = Month;
   this.Interest = Interest;
   this.Principle = Principle;
   this.Balance = Balance;
}

function addRecord(Month, Interest, Principle, Balance) {
   oTable[rec] = new oRecord(Month, Interest, Principle, Balance);
}

function calcRound(num) {
   result=Math.floor(num).toString();
   n = result.length;
   var resultA;
   var cents=100*(num-Math.floor(num))+0.5;
	resultA = "."+Math.floor(cents/10);
   	resultA += Math.floor(cents%10);
	newresult = ""
	if (resultA == ".100")	{ result = (Math.floor(num)+1)+".00" } else { result += resultA }
	return(result)
}