
/***************************
(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("Amount1","Rate1","Appraise1","Term1","Remaining","IncomeTax","Amount2","Rate2","Appraise2","Term2","Origination","OtherClose","Point")
oVal = new Collection("Amount1","Rate1","Appraise1","Term1","Remaining","IncomeTax","Amount2","Rate2","Appraise2","Term2","Origination","OtherClose","Point")
oTst = new Collection("N","N","N","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.Amount1.value,oView.Rate1.value,oView.Appraise1.value,oView.Term1.value,oView.Remaining.value,oView.IncomeTax.value,oView.Amount2.value,oView.Rate2.value,oView.Appraise2.value,oView.Term2.value,oView.Origination.value,oView.OtherClose.value,oView.Point.value)
   
   Mortgage.calcPeriods()
   Mortgage.calcPayment()
   Mortgage.calcPoint()
   Mortgage.calcPMI()
   Mortgage.calcTotCost()
   Mortgage.calcCurBalance1()
   Mortgage.calcInterest()
   Mortgage.calcPrepay()
   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, item7, item8, item9, item10, item11, item12, item13) {
   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
   this.item11 = item11
   this.item12 = item12
   this.item13 = item13
}

function Loan(Amount1,Rate1,Appraise1,Term1,Remaining,IncomeTax,Amount2,Rate2,Appraise2,Term2,Origination,OtherClose,Point) {
   	this.Amount1 = Amount1
	this.Rate1 = Rate1
	this.Appraise1 = Appraise1
	this.Term1 = Term1
	this.Remaining = Remaining
	this.IncomeTax = IncomeTax
	this.Amount2 = Amount2
	this.Rate2 = Rate2
	this.Appraise2 = Appraise2
	this.Term2 = Term2
	this.Origination = Origination
	this.OtherClose = OtherClose
	this.Point = Point
	
	this.NoPMIAmount1 = Number(this.Appraise1) - Number(this.Appraise1) * 0.2
	this.NoPMIAmount2 = Number(this.Appraise2) - Number(this.Appraise2) * 0.2
	
	this.CurAmount1 = this.Amount1
	this.CurAmount2 = this.Amount2
	
	this.calcPeriods = calcPeriods
   	this.calcPayment = calcPayment
   	this.calcPoint = calcPoint
	this.calcTotCost = calcTotCost
	this.calcPMI = calcPMI
	this.calcCurBalance1 = calcCurBalance1
	this.calcInterest=calcInterest
	this.calcPrepay = calcPrepay
   	this.NewPage = NewPage
}

function calcPoint(){
	this.PointAmount = this.Amount2 * this.Point / 100
}

function calcTotCost(){
	OriAmount = Number(this.Origination) * Number(this.Amount2) / 100 
	this.totCost = Number(OriAmount) + Number(this.OtherClose) + Number(this.PointAmount)
}

function calcPrepay(){
	this.PreAmount = Number(this.curBalance1) - Number(this.totCost)
}

function calcPMI(){
	if(Number(this.CurAmount1)>Number(this.NoPMIAmount1)){
		this.PMI1 = this.CurAmount1 * 0.005 / 12
	}
	else{
		this.PMI1 = 0
	}
	if(Number(this.CurAmount2)>Number(this.NoPMIAmount2)){
		this.PMI2 = this.CurAmount2 * 0.005 / 12
	}
	else{
		this.PMI2 = 0
	}
}

function calcCurBalance1(){
	waktu = Number(this.Term1) - Number(this.Remaining)
	waktu = waktu * this.Periods 
	this.curBalance1 = this.Amount1
	this.intPay = 0
	for(counter=0;counter<waktu;counter++){
		interest = Number(this.curBalance1) * Number(this.Rate1) /100/Number(this.Periods)
		interest = Math.round(interest*100)/100
		this.intPay = Number(this.intPay) + interest
		this.curBalance1 = Number(this.curBalance1) - (this.Payment1 - interest)
	}
}

function calcPayment() {
   this.Payment1 = (this.Amount1*((this.Rate1/(this.Periods*100))/(1-(Math.pow(1+(this.Rate1/(this.Periods*100)),((this.Term1*this.Periods)*-1))))))
   this.Payment2 = (this.Amount2*((this.Rate2/(this.Periods*100))/(1-(Math.pow(1+(this.Rate2/(this.Periods*100)),((this.Term2*this.Periods)*-1))))))
   this.Payment1 = Math.round(this.Payment1*100)/100
   this.Payment2 = Math.round(this.Payment2*100)/100
}

function calcInterest() {
   this.Interest1 = ((this.Payment1*(this.Term1*this.Periods))-this.Amount1)
   this.Interest2 = ((this.Payment2*(this.Term2*this.Periods))-this.Amount2)
   this.RestInterest1 = Number(this.Interest1) - Number(this.intPay)
}

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 = "<hr />"
	text = text + '	  <table width="100%" border="0">'
	text = text + '  <tr>'
	text = text + '    <th colspan="2" bgcolor="#EEEEEE" scope="col"><div align="center"><strong>Original Loan</strong></div></th>'
	text = text + '    <th colspan="2" bgcolor="#EEEEEE" scope="col"><div align="center"><strong>New Loan</strong></div></th>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + '    <td width="24%"><strong>Mortgage amount</strong></td>'
	text = text + '    <td width="23%">'+calcRound(this.Amount1)+'</td>'
	text = text + '    <td width="26%"><strong>Mortgage amount</strong></td>'
	text = text + '    <td width="27%">'+calcRound(this.Amount2)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Appraised value</strong></td>'
	text = text + '    <td>'+calcRound(this.Appraise1)+'</td>'
	text = text + '    <td><strong>Appraised value</strong></td>'
	text = text + '    <td>'+calcRound(this.Appraise2)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Interest rate</strong></td>'
	text = text + '    <td>'+this.Rate1+' %</td>'
	text = text + '    <td><strong>Interest rate</strong></td>'
	text = text + '    <td>'+this.Rate2+' %</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Term in years</strong></td>'
	text = text + '    <td>'+(this.Term1)+' Years</td>'
	text = text + '    <td><strong>Term in years</strong></td>'
	text = text + '    <td>'+(this.Term2)+' Years</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Years remaining</strong></td>'
	text = text + '    <td>'+(this.Remaining)+' Years</td>'
	text = text + '    <td><strong>Years remaining</strong></td>'
	text = text + '    <td>'+(this.Term2)+' Years</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>PI payment</strong></td>'
	text = text + '    <td>'+calcRound(this.Payment1)+'</td>'
	text = text + '    <td><strong>PI payment</strong></td>'
	text = text + '    <td>'+calcRound(this.Payment2)+'</td>;'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Monthly PMI</strong></td>'
	text = text + '    <td>'+calcRound(this.PMI1)+'</td>'
	text = text + '    <td><strong>Monthly PMI</strong></td>'
	text = text + '    <td>'+calcRound(this.PMI2)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Total interest remaining</strong></td>'
	text = text + '    <td>'+calcRound(this.RestInterest1)+'</td>'
	text = text + '    <td><strong>Total interest</strong></td>'
	text = text + '    <td>'+calcRound(this.Interest2)+'</td>'
	text = text + '  </tr>'
	text = text + '</table>'

	text = 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="4" bgcolor="#EEEEEE" scope="col"><div align="center"><strong>Original Loan with '+calcRound(Number(this.Payment1)+Number(this.PMI1))+' payment</strong></div></th>'
	text = text + '    <th colspan="4" bgcolor="#EEEEEE" scope="col"><div align="center"><strong>New Loan with '+calcRound(Number(this.Payment2)+Number(this.PMI2))+' payment</strong></div></th>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + '    <td bgcolor="#EEEEEE"><strong>Month</strong></td>'
	text = text + '    <td width="14%" bgcolor="#EEEEEE"> <div align="center"><strong>Equity</strong></div></td>'
	text = text + '    <td width="14%" bgcolor="#EEEEEE"><div align="center"><strong>Interest</strong></div></td>'
	text = text + '    <td width="8%" bgcolor="#EEEEEE"><div align="center"><strong>PMI</strong></div></td>'
	text = text + '    <td width="14%" bgcolor="#EEEEEE"><div align="center"><strong>Balance</strong></div></td>'
	text = text + '    <td width="10%" bgcolor="#EEEEEE"><div align="center"><strong>Equity</strong></div></td>'
	text = text + '    <td width="12%" bgcolor="#EEEEEE"><div align="center"><strong>Interest</strong></div></td>'
	text = text + '    <td width="8%" bgcolor="#EEEEEE"><div align="center"><strong>PMI</strong></div></td>'
	text = text + '    <td width="13%" bgcolor="#EEEEEE"><div align="center"><strong>Balance</strong></div></td>'
	text = text + '  </tr>'
	if(Number(this.Remaining)>Number(this.Term2)){
		waktu = Number(this.Remaining) * Number(this.Periods)
	}
	else{
		waktu = Number(this.Term2) * Number(this.Periods)
	}
	waktu1 = Number(this.Remaining) * Number(this.Periods)
	waktu2 = Number(this.Term2) * Number(this.Periods)
	CurBalance1 = this.curBalance1
	CurBalance2 = this.Amount2
	CurBalance3 = this.PreAmount
	
	PaymentSaving = 0
	counterPayment = 0;
	IntPMISaving = 0
	counterInt = 0
	IntTaxSave = 0;
	counterTaxSave = 0
	IntTaxPre = 0;
	counterTaxPre = 0
	
	colorrow=0
	for(counter=0;counter<waktu;counter++){
		if(counter>=waktu1){
			Payment1 = CurBalance1 * (1+Number(this.Rate1)/Number(this.Periods)/100)
			Payment1 = Math.round(Payment1*100)/100
			tmpPay1 = Payment1
		}
		else{
			Payment1 = this.Payment1
			tmpPay1 = Payment1
		}
		if(counter>=waktu2){
			Payment2 = CurBalance2 * (1+Number(this.Rate2)/Number(this.Periods)/100)
			Payment2 = Math.round(Payment2*100)/100
			tmpPay2 = Payment2
		}
		else{
			Payment2 = this.Payment2
			tmpPay2 = Payment2
		}
		interest1 = CurBalance1 * Number(this.Rate1) / Number(this.Periods) / 100
		interest2 = CurBalance2 * Number(this.Rate2) / Number(this.Periods) / 100
		interest3 = CurBalance3 * Number(this.Rate1) / Number(this.Periods) / 100
		interest1 = Math.round(interest1*100)/100
		interest2 = Math.round(interest2*100)/100
		interest3 = Math.round(interest3*100)/100
		prin1 = Payment1 - interest1;
		prin2 = Payment2 - interest2
		
		if(CurBalance1<Number(this.NoPMIAmount1)){
			totPayment1 = Payment1
			PMI1 = 0
		}
		else{
			totPayment1 = Payment1 + Number(this.PMI1)
			PMI1 = this.PMI1
		}
		
		if(CurBalance3<Number(this.NoPMIAmount1)){
			totPayment3 = Payment1
			PMI3 = 0
		}
		else{
			totPayment3 = Payment1 + Number(this.PMI1)
			PMI3 = this.PMI1
		}
		
		if(CurBalance2<Number(this.NoPMIAmount2)){
			totPayment2 = Payment2
			PMI2 = 0
		}
		else{
			totPayment2 = Payment2 + Number(this.PMI2)
			PMI2 = this.PMI2
		}
		CurBalance1 = CurBalance1 - prin1
		CurBalance2 = CurBalance2 - prin2
		if(CurBalance1<2){
			CurBalance1=0
		}
		if(CurBalance2<2){
			CurBalance2=0
		}
		
		if(PaymentSaving<Number(this.totCost)){
			PaymentSaving = PaymentSaving + (totPayment1 - totPayment2)
			counterPayment = counter + 1
		}
		if(IntPMISaving<Number(this.totCost)){
			IntPMISaving = IntPMISaving + (PMI1 - PMI2) + (interest1 - interest2)
			counterInt = counter + 1
		}
		if(IntTaxSave<Number(this.totCost)){
			counterTaxSave = counter + 1
		}
		if((IntTaxSave)<(Number(this.totCost)+IntTaxPre)){
			counterTaxPre = counter + 1
		}
		IntTaxSave = IntTaxSave + (PMI1 - PMI2) + (tmpPay1 - tmpPay2) - (interest1 - interest2) * this.IncomeTax / 100 
		IntTaxSave = Math.round(IntTaxSave*100)/100
		IntTaxPre = IntTaxPre +  (interest1 - interest3) 
		IntTaxPre = Math.round(IntTaxPre*100)/100
		
		if(colorrow==0){
			colorrow=1
			text = text + '  <tr>'
			text = text + '    <td>'+(counter+1)+'</td>'
			text = text + '    <td>'+calcRound(prin1)+'</td>'
			text = text + '    <td>'+calcRound(PaymentSaving)+'</td>'
			text = text + '    <td>'+calcRound(PMI1)+'</td>'
			text = text + '    <td>'+calcRound(CurBalance1)+'</td>'
			text = text + '    <td>'+calcRound(prin2)+'</td>'
			text = text + '    <td>'+calcRound(interest2)+'</td>'
			text = text + '    <td>'+calcRound(PMI2)+'</td>'
			text = text + '    <td>'+calcRound(CurBalance2)+'</td>'
			text = text + '  </tr>'
		}
		else{
			colorrow=0;
			text = text + '  <tr>'
			text = text + '    <td bgcolor="#EEEEEE">'+(counter+1)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(prin1)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(PaymentSaving)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(PMI1)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(CurBalance1)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(prin2)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(interest2)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(PMI2)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(CurBalance2)+'</td>'
			text = text + '  </tr>'
		}
	}
	text = text + '</table>'
	
	tmptext = text
	
	text = "<hr />"
	text = text + '<table width="100%" border="0">'
	text = text + '    <tr>'
	text = text + '          <th scope="col" bgcolor="#EEEEEE"><strong>Measure</strong></th>'
	text = text + '          <th scope="col" bgcolor="#EEEEEE"><strong>Months</strong></th>'
	text = text + '          <th scope="col" bgcolor="#EEEEEE"><strong>Definition</strong></th>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td valign="top"><strong>Monthly Payment Saving</strong></td>'
	text = text + '          <td align="center" valign="top">'+counterPayment+'</td>'
	text = text + '          <td valign=top>The number of months it will take for your monthly payment reduction to be greater then your closing costs.</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td valign="top"><strong>PMI &amp; interest savings:</strong></td>'
	text = text + '          <td align="center" valign="top">'+counterInt+'</td>'
	text = text + '        <td valign=top>The number of months it will take for your interest and PMI savings to exceed your closing costs.</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td valign="top"><strong>Total savings after tax:</strong></td>'
	text = text + '          <td align="center" valign="top">'+counterTaxSave+'</td>'
	text = text + '          <td valign=top>The number of months it will take for the after-tax interest and PMI  savings to exceed your closing costs. Your income tax rate was entered  as '+this.IncomeTax+'%.<br /></td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td valign="top"><strong>Total savings vs. prepayment:</strong></td>'
	text = text + '          <td align="center" valign="top">'+counterTaxPre+'</td>'
	text = text + '          <td valign=top>This is the most conservative breakeven measure. It is the number of  months it will take for the after-tax interest and PMI savings to  exceed both the closing costs and any interest savings from prepaying  your mortgage. The prepayment amount used in this calculation is the  amount that you would have to spend on closing costs.</td>'
	text = text + '        </tr>'
	text = text + '      </table>'

	text = text + tmptext
	
	document.getElementById('result').innerHTML = text
}

