//CMMC.JS
//Copyright 2003-2006 Continental Mutual Mortgage Corporation
//All Rights Reserved.

function toCurrency(num)
{
  num = Math.floor(num*100)/100;
  var s = num.toString();
  var c = s.length;
  var i = s.indexOf('.');
  if (i == 0)
    s = "0." + s;
  else  
  if (i < 0)
    s += ".00";
  else
  if ((c - i) < 3)
    s += "0";
  return s;
}

function Floor(number)
{
  return Math.floor((number+0.005)*100)/100;
}

function CalculatePayment(LoanAmount, InterestRate, Years)
{
  var j = InterestRate / 1200;
  var n = Years * 12;
  var m = LoanAmount * (j / (1 - Math.pow((1 + j),-n)))
  return Floor(m);
}

function Mortgage(LoanAmount, InterestRate, Years)
{
    this.LoanAmount = LoanAmount;
    this.InterestRate = InterestRate;
    this.Years = Years;
    this.Payment = CalculatePayment(LoanAmount, InterestRate, Years);
    this.ShowAmortizationTable = ShowAmortizationTable;
}

function SetLoanAmount(LoanAmount)
{
    this.LoanAmount = LoanAmount;
}

function SetInterestRate(InterestRate)
{
    this.InterestRate = InterestRate;
}
function SetYears(Years)
{
    this.Years = Years;
}

function ShowAmortizationTable()
{
   // Here are the parameters for the popup window.
   var width = 500;
   var height = 400;
   var left = (screen.width - width) / 2;
   var top = (screen.height - height) / 2;

	var popup = window.open('','printable', 'top=' + top + ',left=' + left + ',height=' + height + ',width=' + width + ',scrollbars,resizable');

   //this writes out the header to the popup.
   var head = '<HTML><HEAD><TITLE>Continental Mutual Mortgage Corp - Mortgage Calculator</TITLE>'+
              '<LINK REL="stylesheet" TYPE="text/css" HREF="cmmc.css"/>Disclaimer: The figures reflected are an estimate only. We believe these figures to be accurate, however, they are subject to review and calculation.</HEAD><br><br><BODY>';
   var closewin = '<input type="BUTTON" value="Close Window" onClick="javascript:window.close();"><br><br>';

   //this writes out the monthly payment to the popup
   popup.document.write(head + closewin);
   popup.document.write('<strong>Monthly Payment: ' + this.Payment + '</strong><br><br>');

   //this writes out the amortization table to the popup
   popup.document.write('<TABLE BORDER="0" CELLSPACING=2>' +
                     '<TR class="AmortTableHeading">' +
                     '<TD><U><STRONG>&nbsp&nbsp Payment No. &nbsp&nbsp</STRONG></u></TD>' +
                     '<TD><U><STRONG>&nbsp&nbsp Principal Balance &nbsp&nbsp</STRONG></u></TD>' + 
                     '<TD><U><STRONG>&nbsp&nbsp Interest &nbsp&nbsp</STRONG></u></TD>' + 
                     '<TD><U><STRONG>&nbsp&nbsp Principal &nbsp&nbsp</STRONG></u></TD>' + 
                     '</TR>');

   var period = this.Years * 12;
   var principalBalance = this.LoanAmount;

   for (var i = 1; i <= period; i++)
      {
      var interestPayment = Floor(principalBalance * (this.InterestRate / 1200));
      var principalPayment = Floor(this.Payment - interestPayment);

      if (i % 2 == 0)
           popup.document.write('<TR class="AmortTableRow">');
      else
           popup.document.write('<TR>');
            
      popup.document.write(
                        '<TD ALIGN="CENTER">' + i + '</TD>' +
                        '<TD ALIGN="CENTER">' + toCurrency(principalBalance) + '</TD>' +
                        '<TD ALIGN="CENTER">' + toCurrency(interestPayment) + '</TD>' +
                        '<TD ALIGN="CENTER">' + toCurrency(principalPayment) + '</TD>' +
                     '</TR>');
      principalBalance = Floor(principalBalance - principalPayment);
      }

   popup.document.write('</table>');

   //this writes out the footer to the popup.
	var footer = '<FONT FACE="sans-serif, arial" size=-2>&copy;2006 Continental Mutual Mortgage Corp. All rights reserved.</FONT>';
	var tail = '</BODY></HTML>';
    popup.document.write(footer + tail);

   //this focuses the popup.
	popup.document.close();
	popup.focus();
}

