// 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("FullRate","FullFixMonth","FullRateMax","AMOUNT","RateChange","FixRate","OnlyRate","OnlyFixMonth","OnlyInterestMax")
oVal = new Collection("FullRate","FullFixMonth","FullRateMax","AMOUNT","RateChange","FixRate","OnlyRate","OnlyFixMonth","OnlyInterestMax")
oTst = new Collection("N","N","N","N","N","N","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, get_selection(oView.YEARS), oView.RateChange.value, oView.FixRate.value, oView.FullRate.value,oView.FullFixMonth.value, oView.FullRateMax.value, oView.OnlyRate.value, oView.OnlyFixMonth.value, oView.OnlyInterestMax.value)
   Mortgage.calcPeriods()
   Mortgage.calcPayment()
   Mortgage.calcInterest()
   Mortgage.NewPage()
}

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, item7, item8, item9, item10) {
   this.item1 = item1
   this.item2 = item2
   this.item3 = item3
   this.item4 = item4
   this.item5 = item5
   this.item6 = item6
   this.item7 = item7
   this.item8 = item8
   this.item9 = item9
   this.item10 = item10
}

function Loan(Amount, Term, RateChange, FixRate, FullRate, FullFixMonth, FullRateMax,OnlyRate, OnlyFixMonth, OnlyInterestMax ) {
   this.Amount = Amount
   this.AmountFull = Amount
   this.RateChange = RateChange
   this.Term = Term
   this.FixRate = FixRate
   this.FullRate = FullRate
   this.FullFixMonth = FullFixMonth
   this.FullTime = this.Term * 12
   this.FullRateMax = FullRateMax
   this.OnlyRate = OnlyRate
   this.OnlyFixMonth = OnlyFixMonth
   this.OnlyInterestMax = OnlyInterestMax

   this.calcPeriods = calcPeriods
   this.calcPayment = calcPayment
   this.calcInterest = calcInterest
   this.NewPage = NewPage
}

function calcPayment() {
   this.PaymentFix = (this.Amount*((this.FixRate/(this.Periods*100))/(1-(Math.pow(1+(this.FixRate/(this.Periods*100)),((this.Term*this.Periods)*-1))))))
    this.PaymentFull = (this.AmountFull*((this.FullRate/(this.Periods*100))/(1-(Math.pow(1+(this.FullRate/(this.Periods*100)),(this.FullTime*-1))))))
	 this.PaymentOnly = this.Amount*(this.OnlyRate/(this.Periods*100))
	 
	 this.PaymentFix = Math.round(this.PaymentFix * 100)/100
	 this.PaymentFull = Math.round(this.PaymentFull * 100)/100
	 this.PaymentOnly = Math.round(this.PaymentOnly * 100)/100
}

function calcInterest() {
   this.Interest = ((this.Payment*(this.Term*this.Periods))-this.Amount)
}

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 = "<hr />";
	text = text + '<table width="100%" border="0">'
	text = text + '  <tr>'
	text = text + '    <th width="7%" bgcolor="#EEEEEE" scope="col">&nbsp;</th>'
	text = text + '    <th colspan="2" bgcolor="#EEEEEE" scope="col"><strong><u>Fixed Rate Mortgage</u></strong></th>'
	text = text + '    <th colspan="2" bgcolor="#EEEEEE" scope="col"><strong><u>ARM Fully Amortizing</u></strong></th>'
	text = text + '    <th colspan="2" bgcolor="#EEEEEE" scope="col"><strong><u>Interest Only ARM</u></strong></th>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + '    <td bgcolor="#EEEEEE"><strong>#</strong></td>'
	text = text + '    <td width="16%" bgcolor="#EEEEEE"><strong>Payment</strong></td>'
	text = text + '    <td width="15%" bgcolor="#EEEEEE"><strong>Balance</strong></td>'
	text = text + '    <td width="19%" bgcolor="#EEEEEE"><strong>Payment</strong></td>'
	text = text + '    <td width="14%" bgcolor="#EEEEEE"><strong>Balance</strong></td>'
	text = text + '    <td width="14%" bgcolor="#EEEEEE"><strong>Payment</strong></td>'
	text = text + '    <td width="15%" bgcolor="#EEEEEE"><strong>Balance</strong></td>'
	text = text + '  </tr>'
	waktu = this.Term * this.Periods
	balanceFix = this.Amount
	balanceFull = this.Amount
	balanceOnly = this.Amount
	counteryearFull=0
	counteryearOnly= 0
	savingFirstYearFull=0
	savingFistYearOnly=0
	savingFourYearFull=0
	savingFourYearOnly=0
	interestFirstYearFull=0;
	interestFirstYearOnly=0;
	interestFirstYearFix=0;
	paymentFirstYearFull=0
	paymentFirstYearOnly=0
	paymentFirstYearFix=0
	interestFourYearFull=0
	interestFourYearOnly=0
	interestFourYearFix=0
	paymentFourYearFull=0
	paymentFourYearOnly=0
	paymentFourYearFix=0
	
	initFix = this.PaymentFix;
	initFull = this.PaymentFull
	initOnly = this.PaymentOnly
	
	PaymentFourFix=0;
	PaymentFourFull=0
	PaymentFourOnly=0
	
	totInterestFix = 0
	totInterestFull = 0
	totInterestOnly = 0
	
	totPaymentFix=0
	totPaymentFull=0
	totPaymentOnly=0
	
	colorrow = 0;
	for(counter=0;counter<waktu;counter++){
		if(counter>(this.FullFixMonth-1)){
			if(counteryearFull==0){
				this.FullRate = Number(this.FullRate) + Number(this.RateChange)
				this.FullTime = this.Term * this.Periods - counter
				this.AmountFull = balanceFull
				if(this.FullRate>this.FullRateMax){
					this.FullRate = this.FullRateMax;
				}
				this.calcPayment();
			}
			counteryearFull = counteryearFull + 1;
			if(counteryearFull==12){
				counteryearFull=0
			}
		}
		
		if(counter>(this.OnlyFixMonth-1)){
			if(counteryearOnly==0){
				this.OnlyRate = Number(this.OnlyRate) + Number(this.RateChange)
				if(this.OnlyRate>this.OnlyInterestMax){
					this.OnlyRate = this.OnlyInterestMax;
				}
				this.calcPayment();
			}
			counteryearOnly = counteryearOnly + 1;
			if(counteryearOnly==12){
				counteryearOnly=0
			}
		}
  	    
		interestFix = balanceFix * this.FixRate /100 / this.Periods
		interestFull = balanceFull * this.FullRate / 100 / this.Periods
		balanceFix = balanceFix - (this.PaymentFix - interestFix)
		balanceFull = balanceFull - (this.PaymentFull - interestFull)
		if(balanceFix<2){
			balanceFix=0;
		}
		if(balanceFull<2){
			balanceFull = 0;
		}
		
		if(counter<12){
			savingFirstYearFull= savingFirstYearFull + (this.PaymentFix - this.PaymentFull)
			savingFistYearOnly= savingFistYearOnly + (this.PaymentFix - this.PaymentOnly)
			interestFirstYearFull=interestFirstYearFull + interestFull;
			interestFirstYearOnly=interestFirstYearOnly + + this.PaymentFix;
			interestFirstYearFix=interestFirstYearFix + interestFix;
			paymentFirstYearFull=paymentFirstYearFull + this.PaymentFull
			paymentFirstYearOnly=paymentFirstYearOnly + this.PaymentOnly
			paymentFirstYearFix=paymentFirstYearFix + this.PaymentFix
		}
		if(counter<48){
			savingFourYearFull= savingFourYearFull + (this.PaymentFix - this.PaymentFull)
			savingFourYearOnly= savingFourYearOnly + (this.PaymentFix - this.PaymentOnly)
			interestFourYearFull=interestFourYearFull + interestFull;
			interestFourYearOnly=interestFourYearOnly  + this.PaymentOnly;
			interestFourYearFix=interestFourYearFix + interestFix;
			paymentFourYearFull=paymentFourYearFull + this.PaymentFull
			paymentFourYearOnly=paymentFourYearOnly + this.PaymentOnly
			paymentFourYearFix=paymentFourYearFix + this.PaymentFix
		}
		if(counter==47){
			PaymentFourFix=this.PaymentFix;
			PaymentFourFull=this.PaymentFull
			PaymentFourOnly=this.PaymentOnly
		}
		
		totInterestFix = totInterestFix + interestFix
		totInterestFull = totInterestFull + interestFull
		totInterestOnly = totInterestOnly + this.PaymentOnly
	
		totPaymentFix=totPaymentFix + this.PaymentFix
		totPaymentFull=totPaymentFull + this.PaymentFull
		totPaymentOnly=totPaymentOnly + this.PaymentOnly
		if(colorrow==0){
			colorrow=1;
			text = text + '  <tr>'
			text = text + '    <td>'+(counter+1)+'</td>'
			text = text + '    <td>'+calcRound(this.PaymentFix)+'</td>'
			text = text + '    <td>'+calcRound(balanceFix)+'</td>'
			text = text + '    <td>'+calcRound(this.PaymentFull)+'</td>'
			text = text + '    <td>'+calcRound(balanceFull)+'</td>'
			text = text + '    <td>'+calcRound(this.PaymentOnly)+'</td>'
			text = text + '    <td>'+calcRound(balanceOnly)+'</td>'
			text = text + '  </tr>'
		}
		else{
			colorrow=0;
			text = text + '  <tr>'
			text = text + '    <td bgcolor="#EEEEEE">'+(counter+1)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(this.PaymentFix)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(balanceFix)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(this.PaymentFull)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(balanceFull)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(this.PaymentOnly)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(balanceOnly)+'</td>'
			text = text + '  </tr>'
		}
	}
	text = text + '</table>'
	
	tmptext = text;
	text = '<hr />';
	text = text + '<table width="100%" border="0">'
    text = text + '    <tr>'
	text = text + '          <th colspan="3" bgcolor="#EEEEEE" scope="col">ARM vs. Fixed Rate Mortgage</th>'
	text = text + '          </tr>'
	text = text + '        <tr>'
	text = text + '          <td>&nbsp;</td>'
	text = text + '          <td><div align="center"><strong>Fully Amortizing ARM</strong></div></td>'
	text = text + '          <td><div align="center"><strong>Interest Only ARM</strong></div></td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Initial Saving on Monthly Payment</strong></td>'
	text = text + '          <td>'+calcRound(initFix-initFull)+'</td>'
	text = text + '          <td>'+calcRound(initFix-initOnly)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Payment Savings First Year</strong></td>'
	text = text + '          <td>'+calcRound(savingFirstYearFull)+'</td>'
	text = text + '          <td>'+calcRound(savingFistYearOnly)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Payment Savings for Four Years</strong></td>'
	text = text + '          <td>'+calcRound(savingFourYearFull)+'</td>'
	text = text + '          <td>'+calcRound(savingFourYearOnly)+'</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 + '    <th colspan="4" bgcolor="#EEEEEE" scope="col">Results Summary</th>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '    <td><strong>Fixed Rate Mortgage</strong></td>'
	text = text + '    <td><strong>Fully Amortizing ARM</strong></td>'
	text = text + '    <td><strong>Interest Only ARM</strong></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 + '    <td>'+calcRound(this.Amount)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Term</strong></td>'
	text = text + '    <td>'+this.Term+' years</td>'
	text = text + '    <td>'+this.Term+' years</td>'
	text = text + '    <td>'+this.Term+' years</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Interest rate</strong></td>'
	text = text + '    <td>'+this.FixRate+' %</td>'
	text = text + '    <td>'+this.FullRate+' %</td>'
	text = text + '    <td>'+this.OnlyRate+' %</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Initial monthly payment</strong></td>'
	text = text + '    <td>'+calcRound(initFix)+'</td>'
	text = text + '    <td>'+calcRound(initFull)+'</td>'
	text = text + '    <td>'+calcRound(initOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Payment after four years</strong></td>'
	text = text + '    <td>'+calcRound(PaymentFourFix)+'</td>'
	text = text + '    <td>'+calcRound(PaymentFourFull)+'</td>'
	text = text + '    <td>'+calcRound(PaymentFourOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>First year totals:</strong></td>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;&nbsp;&nbsp;<strong>Interest</strong></td>'
	text = text + '    <td>'+calcRound(interestFirstYearFix)+'</td>'
	text = text + '    <td>'+calcRound(interestFirstYearFull)+'</td>'
	text = text + '    <td>'+calcRound(interestFirstYearOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;&nbsp;&nbsp;<strong>Principal</strong></td>'
	text = text + '    <td>'+calcRound(paymentFirstYearFix-interestFirstYearFix)+'</td>'
	text = text + '    <td>'+calcRound(paymentFirstYearFull-interestFirstYearFull)+'</td>'
	text = text + '    <td>'+calcRound(paymentFirstYearOnly-interestFirstYearOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;&nbsp;&nbsp;<strong>Payments</strong></td>'
	text = text + '    <td>'+calcRound(paymentFirstYearFix)+'</td>'
	text = text + '    <td>'+calcRound(paymentFirstYearFull)+'</td>'
	text = text + '    <td>'+calcRound(paymentFirstYearOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Four year totals:</strong></td>'
	text = text + '    <td>&nbsp;</td>	'
	text = text + '    <td>&nbsp;</td>'
	text = text + '    <td>&nbsp;</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;&nbsp;&nbsp;<strong>Interest</strong></td>'
	text = text + '    <td>'+calcRound(interestFourYearFix)+'</td>'
	text = text + '    <td>'+calcRound(interestFourYearFull)+'</td>'
	text = text + '    <td>'+calcRound(interestFourYearOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;&nbsp;&nbsp;<strong>Principal</strong></td>'
	text = text + '    <td>'+calcRound(paymentFourYearFix-interestFourYearFix)+'</td>'
	text = text + '    <td>'+calcRound(paymentFourYearFull-interestFourYearFull)+'</td>'
	text = text + '    <td>'+calcRound(paymentFourYearOnly-interestFourYearOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;&nbsp;&nbsp;<strong>Payments</strong></td>'
	text = text + '    <td>'+calcRound(paymentFourYearFix)+'</td>'
	text = text + '    <td>'+calcRound(paymentFourYearFull)+'</td>'
	text = text + '    <td>'+calcRound(paymentFourYearOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td bgcolor="#EEEEEE"><strong>Total interest</strong></td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totInterestFix)+'</td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totInterestFull)+'</td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totInterestOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td bgcolor="#EEEEEE"><strong>Total payments</strong></td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totPaymentFix)+'</td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totPaymentFull)+'</td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totPaymentOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td bgcolor="#EEEEEE"><strong>Ending balance </strong></td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(balanceFix)+'</td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(balanceFull)+'</td>'
	text = text + '    <td bgcolor="#EEEEEE">'+calcRound(balanceOnly)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td>&nbsp;</td>'
	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;
}



