// JavaScript Document
/***************************
(c) 2008 www.TUFaT.com
All Rights Reserved. Please
do not re-sell/re-distribute
this software.
***************************/
window.onerror=null
bState = true
oReq = new Collection("AMOUNT","TAXRATE","RATE15","RATE30","","")
oVal = new Collection("AMOUNT","TAXRATE","RATE15","RATE30","","")
oTst = new Collection("N","N","N","N","","")

function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function controller(oForm, oBtn) {
   while (bState) {
      if (!Required(oForm))
         break
      if (!Validate(oForm))
         break
      if (!SetValue(oForm))
         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 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 SetValue(oView) {
   Mortgage = new Loan(oView.AMOUNT.value, oView.TAXRATE.value, oView.RATE15.value, oView.RATE30.value)
   
   Mortgage.calcPeriods()
   Mortgage.calcPayment()
   Mortgage.calcInterest()
   Mortgage.calcAvgTaxSaving()
   Mortgage.NewPage()
   
   return(true)
}

function isMissing(oCtrl) {
   if (oCtrl.value == "") {
      alert("You have left a required value blank. Please type a number")
      oCtrl.focus()
      oCtrl.select()
      return(true)
      }
   else
      {
      return(false)
   }
}

function isTest(oCtrl, oTest) {
   if (oTest=="N" && !isNumber(oCtrl.value) ) {
      alert(oCtrl.value+" contains an invalid character. Please type a number")
      oCtrl.focus()
      oCtrl.select()
      return(true)
      }
      else
      {
      return(false)
   }
}

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 != "." ) {
             return(false)
          }
       }
   }
   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, TaxRate, Rate15, Rate30) {
   this.Amount = Amount
   this.TaxRate = TaxRate
   this.Rate15 = Rate15
   this.Rate30 = Rate30

	this.calcPeriods = calcPeriods
   this.calcPayment = calcPayment
   this.calcInterest = calcInterest
   this.calcAvgTaxSaving=calcAvgTaxSaving
   this.NewPage = NewPage
}

function calcPayment() {
   this.Payment15 = (this.Amount*((this.Rate15/(this.Periods*100))/(1-(Math.pow(1+(this.Rate15/(this.Periods*100)),((15*this.Periods)*-1))))))
   this.Payment30 = (this.Amount*((this.Rate30/(this.Periods*100))/(1-(Math.pow(1+(this.Rate30/(this.Periods*100)),((30*this.Periods)*-1))))))
   this.Payment15 = Math.round(this.Payment15*100)/100
   this.Payment30 = Math.round(this.Payment30*100)/100
}

function calcInterest() {
   this.Interest15 = ((this.Payment15*(15*this.Periods))-this.Amount)
   this.Interest30 = ((this.Payment30*(30*this.Periods))-this.Amount)
}

function calcAvgTaxSaving(){
	this.AvgTax15 = this.Interest15 * this.TaxRate / 100 / 15
	this.AvgTax30 = this.Interest30 * this.TaxRate / 100 / 30
}

function calcPeriods() {
   this.Periods=12
}

function calcRound(num) {
   result1="$"+Math.floor(num)+"."
   n = result1.length
   if (num>1000 && num<999999) {
     result1="$"+result1.substring(1,n-4)+","+result1.substring(n-4,n)
   }
   if (num>1000000) {
     result1 = "$"+result1.substring(1,n-7)+","+result1.substring(n-7,n-4)+","+result1.substring(n-4,n)
   }
   var cents=100*(num-Math.floor(num))+0.5
   result1 += Math.floor(cents/10)
   result1 += Math.floor(cents%10)
   return(result1)
}

function select_item(name, value) {
   this.name = name
   this.value = value
}

function get_selection(select_object) {
   contents = new select_item()
   for(var i=0;i<select_object.options.length;i++)
      if(select_object.options[i].selected == true) {
        contents.name = select_object.options[i].text
        contents.value = select_object.options[i].value
      }
   return(contents.name)
}

function setCookie (name, value ) {
  document.cookie = name + "=" + escape (value) + "; path=/";
}

function NewPage() {
	text = "";
	text = text + '<hr />'
	text = text + '	 <span class="style1">15 Year Payment Schedule    </span>'
	text = text + '    <table width="100%" border="0">'
	text = text + '  <tr>'
	text = text + '    <th width="6%" bgcolor="#EEEEEE" scope="col">Nbr</th>'
	text = text + '    <th width="16%" bgcolor="#EEEEEE" scope="col">Payment</th>'
	text = text + '    <th width="16%" bgcolor="#EEEEEE" scope="col">Principal</th>'
	text = text + '    <th width="15%" bgcolor="#EEEEEE" scope="col">Interest</th>'
	text = text + '    <th width="47%" bgcolor="#EEEEEE" scope="col">Ending Principal Balance</th>'
	text = text + '    </tr>'
	waktu = 15 * 12
	totPay = this.Payment15 * 15 * this.Periods
	totBalance = this.Amount
	totIntMonth15=0
	totPrinMonth15=0
	totIntYear15 = 0;
	totTaxSaveYear15 = 0;
	colorrow=0;
	for(counter=0;counter<waktu;counter++){
		if(totPay>this.Payment15){
			Pay = this.Payment15;
		}
		else{
			Pay = totPay;
		}
		totPay = totPay - Pay
		interest = totBalance * this.Rate15 / 12 / 100
		interest = Math.round(interest*100)/100
		prin = Pay-interest
		prin = Math.round(prin*100)/100
		totBalance = totBalance - prin
		totBalance = Math.round(totBalance*100)/100
		if(totBalance<2){
			totBalance = 0
		}
		if(counter<1){
			totIntMonth15= totIntMonth15 + interest
			totPrinMonth15= totPrinMonth15 + prin
		}
		if(counter<12){
			totIntYear15 = totIntYear15 + interest
		}
		if(colorrow==0){
			colorrow = 1
			text = text + '  <tr>'
			text = text + '    <td>'+(counter+1)+'</td>'
			text = text + '    <td>'+calcRound(Pay)+'</td>'
			text = text + '    <td>'+calcRound(prin)+'</td>'
			text = text + '    <td>'+calcRound(interest)+'</td>'
			text = text + '    <td>'+calcRound(totBalance)+'</td>'
			text = text + '    </tr>'
		}
		else{
			colorrow=0;
			text = text + '  <tr>'
			text = text + '    <td bgcolor="#EEEEEE">'+(counter+1)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(Pay)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(prin)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(interest)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totBalance)+'</td>'
			text = text + '    </tr>'
		}
	}
	
	text = text + '</table>'
	
	text = text + '<hr />'
	text = text + '	<span class="style1">30 Year Payment Schedule    </span>'
	text = text + '    <table width="100%" border="0">'
	text = text + '  <tr>'
	text = text + '    <th width="5%" bgcolor="#EEEEEE" scope="col">Nbr</th>'
	text = text + '    <th width="17%" bgcolor="#EEEEEE" scope="col">Payment</th>'
	text = text + '    <th width="16%" bgcolor="#EEEEEE" scope="col">Pricipal</th>'
	text = text + '    <th width="16%" bgcolor="#EEEEEE" scope="col">Interest</th>'
	text = text + '    <th width="46%" bgcolor="#EEEEEE" scope="col">Ending Pricipal Balance</th>'
	text = text + '  </tr>'
	waktu = 30* 12
	totPay = this.Payment30 * 30 * this.Periods
	totBalance = this.Amount
	totIntMonth30=0
	totPrinMonth30=0
	totIntYear30 = 0;
	totTaxSaveYear30 = 0;
	colorrow=0
	for(counter=0;counter<waktu;counter++){
		if(totPay>this.Payment30){
			Pay = this.Payment30;
		}
		else{
			Pay = totPay;
		}
		totPay = totPay - Pay
		interest = totBalance * this.Rate30 / 12 / 100
		interest = Math.round(interest*100)/100
		prin = Pay-interest
		prin = Math.round(prin*100)/100
		totBalance = totBalance - prin
		totBalance = Math.round(totBalance*100)/100
		if(totBalance<2){
			totBalance = 0
		}
		if(counter<1){
			totIntMonth30= totIntMonth30 + interest
			totPrinMonth30= totPrinMonth30 + prin
		}
		if(counter<12){
			totIntYear30 = totIntYear30 + interest
		}
		if(colorrow==0){
			colorrow = 1;
			text = text + '  <tr>'
			text = text + '    <td>'+(counter+1)+'</td>'
			text = text + '    <td>'+calcRound(Pay)+'</td>'
			text = text + '    <td>'+calcRound(prin)+'</td>'
			text = text + '    <td>'+calcRound(interest)+'</td>'
			text = text + '    <td>'+calcRound(totBalance)+'</td>'
			text = text + '    </tr>'
		}
		else{
			colorrow=0;
			text = text + '  <tr>'
			text = text + '    <td bgcolor="#EEEEEE">'+(counter+1)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(Pay)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(prin)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(interest)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totBalance)+'</td>'
			text = text + '    </tr>'
		}
	}
	
	text = text + '</table>'
	
	tmptext = text;
	text="";
	
	text = "<hr />"
	text = text + '   <table width="100%" border="0">'
	text = text + '        <tr>'
	text = text + '          <td colspan="3" bgcolor="#EEEEEE"><div align="center"><strong>Mortgage Comparison</strong></div></td>'
	text = text + '          </tr>'
	text = text + '        <tr>'
	text = text + '          <td width="25%" bgcolor="#EEEEEE">&nbsp;</td>'
	text = text + '          <td width="38%" bgcolor="#EEEEEE"><div align="center"><strong>15 Year Mortgage</strong></div></td>'
	text = text + '          <td width="37%" bgcolor="#EEEEEE"><div align="center"><strong>30 Year Mortgage</strong></div></td>'
	text = text + '          </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Loan Amount</strong></td>'
	text = text + '          <td>'+calcRound(this.Amount)+'</td>'
	text = text + '          <td>'+calcRound(this.Amount)+'</td>'
	text = text + '          </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Interest Rate</strong></td>'
	text = text + '          <td>'+this.Rate15+'</td>'
	text = text + '          <td>'+this.Rate30+'</td>'
	text = text + '          </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Monthly Payment</strong></td>'
	text = text + '          <td><strong>'+calcRound(this.Payment15)+'</strong></td>'
	text = text + '          <td><strong>'+calcRound(this.Payment30)+'</strong></td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Total Interest</strong></td>'
	text = text + '          <td>'+calcRound(this.Interest15)+'</td>'
	text = text + '          <td>'+calcRound(this.Interest30)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Total Payment</strong></td>'
	text = text + '          <td><strong>'+calcRound(this.Payment15*15*this.Periods)+'</strong></td>'
	text = text + '          <td><strong>'+calcRound(this.Payment30*30*this.Periods)+'</strong></td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td>&nbsp;</td>'
	text = text + '          <td>&nbsp;</td>'
	text = text + '          <td>&nbsp;</td>'
	text = text + '          </tr>'
	text = text + '      </table>'
	
	text = text + '<hr />'
	text = text + '	<table width="100%" border="0">'
	text = text + '  <tr>'
	text = text + '    <td colspan="3" bgcolor="#EEEEEE"><div align="center"><strong>Interest and Income Tax Comparison</strong></div></td>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + '    <td bgcolor="#EEEEEE"><div align="center"></div></td>'
	text = text + '    <td bgcolor="#EEEEEE"><div align="center"><strong>15 Year Mortgage</strong></div></td>'
	text = text + '    <td bgcolor="#EEEEEE"><div align="center"><strong>30 Year Mortgage</strong></div></td>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + "    <td><strong>First Month's Interest</strong></td>"
	text = text + '    <td>'+calcRound(totIntMonth15)+'</td>'
	text = text + '    <td>'+calcRound(totIntMonth30)+'</td>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + "    <td><strong>First Month's Principal</strong></td>"
	text = text + '    <td>'+calcRound(totPrinMonth15)+'</td>'
	text = text + '    <td>'+calcRound(totPrinMonth30)+'</td>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + "    <td><strong>First Year's Interest</strong></td>"
	text = text + '    <td>'+calcRound(totIntYear15)+'</td>'
	text = text + '    <td>'+calcRound(totIntYear30)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + "    <td><strong>First Year's Tax Saving</strong></td>"
	text = text + '    <td>'+calcRound(totIntYear15*this.TaxRate/100)+'</td>'
	text = text + '    <td>'+calcRound(totIntYear30*this.TaxRate/100)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + "    <td><strong>Avg. Year's Tax Saving</strong></td>"
	text = text + '    <td>'+calcRound(this.AvgTax15)+'</td>'
	text = text + '    <td>'+calcRound(this.AvgTax30)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '    </tr>'
	text = text + '</table>'
	
	text = text + tmptext
	
	document.getElementById('result').innerHTML = text;
}

