// 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","RATE","POINT","YearInHome","RATENOPOINT","")
oVal = new Collection("AMOUNT","RATE","POINT","YearInHome","RATENOPOINT","")
oTst = new Collection("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, oView.RATE.value, get_selection(oView.YEARS), oView.POINT.value, oView.YearInHome.value, oView.RATENOPOINT.value)
   Mortgage.calcPeriods()
   Mortgage.calcPointAmount()
   Mortgage.calcPayment()
   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, Rate, Term, DiscountPoint, year, RateNoPoint) {
   this.Amount = Amount
   this.Rate = Rate
   this.Term = Term
   this.DiscountPoint = DiscountPoint
   this.Year = year
   this.RateNoPoint = RateNoPoint

   this.calcPeriods = calcPeriods
   this.calcPayment = calcPayment
   this.calcPointAmount = calcPointAmount
   this.NewPage = NewPage
}

function calcPointAmount(){
	this.PointAmount = this.DiscountPoint * this.Amount  / 100
	this.PointAmount = Math.round(this.PointAmount*100) / 100
}

function calcPayment() {
   this.PaymentPoint = (this.Amount*((this.Rate/(this.Periods*100))/(1-(Math.pow(1+(this.Rate/(this.Periods*100)),((this.Term*this.Periods)*-1))))))
   this.PaymentNoPoint = ((this.Amount-this.PointAmount)*((this.RateNoPoint/(this.Periods*100))/(1-(Math.pow(1+(this.RateNoPoint/(this.Periods*100)),((this.Term*this.Periods)*-1))))))
   
   this.PaymentPoint = Math.round(this.PaymentPoint*100) / 100
   this.PaymentNoPoint = Math.round(this.PaymentNoPoint*100) / 100
}

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 + '      <table width="100%" border="0" cellspacing="0" cellpadding="3">'
	text = text + '        <tr>'
	text = text + '          <td nowrap="nowrap" bgcolor="#EEEEEE"></td>'
	text = text + '          <td nowrap="nowrap" colspan="3" bgcolor="#EEEEEE"><center><strong>Without Points</strong></center></td>'
	text = text + '          <td nowrap="nowrap" colspan="3" bgcolor="#EEEEEE"><center><strong>With Points</strong></center></td>'
	text = text + '          </tr>'
	text = text + '        <tr>'
	text = text + '          <td bgcolor="#EEEEEE"><div align="center"><strong>Nbr</strong></div></td>'
	text = text + '          <td bgcolor="#EEEEEE"><div align="center"><strong>Payment</strong></div></td>'
	text = text + '          <td bgcolor="#EEEEEE"><div align="center"><strong>Interest</strong></div></td>'
	text = text + '          <td bgcolor="#EEEEEE"><div align="center"><strong>Ending Balance</strong></div></td>'
	text = text + '          <td bgcolor="#EEEEEE"><div align="center"><strong>Payment</strong></div></td>'
	text = text + '          <td bgcolor="#EEEEEE"><div align="center"><strong>Interest</strong></div></td>'
	text = text + '          <td bgcolor="#EEEEEE"><div align="center"><strong>Ending Balance</strong></div></td>'
	text = text + '        </tr>'
	waktu = this.Term * this.Periods
	totalPoint = this.PaymentPoint * this.Term * 12;
	totalNoPoint = this.PaymentNoPoint * this.Term * 12;
	BalancePoint = this.Amount
	BalanceNoPoint = this.Amount - this.PointAmount
	
	BalanceYearPoint = 0;
	BalanceYearNoPoint = 0;
	waktuYear = this.Year * 12 -1
	
	colorrow=0
	
	for(counter=0;counter<waktu;counter++){
		if(colorrow==0){
			colorrow=1
			text = text + '        <tr>'
			text = text + '          <td>'+(counter+1)+'</td>'
			if(totalNoPoint > this.PaymentNoPoint){
				text = text + '          <td>'+this.PaymentNoPoint+'</td>'
				pay = this.PaymentNoPoint
			}
			else{
				text = text + '          <td>'+totalNoPoint+'</td>'
				pay = totalNoPoint;
			}
			totalNoPoint = Number(totalNoPoint) - Number(this.PaymentNoPoint)
			interestNoPoint = BalanceNoPoint * this.RateNoPoint / 100 / 12
			interestNoPoint = Math.round(interestNoPoint*100)/100
			BalanceNoPoint = BalanceNoPoint - (pay - interestNoPoint)
			BalanceNoPoint = Math.round(BalanceNoPoint * 100)/100
			if(BalanceNoPoint<2){
				BalanceNoPoint = 0
			}
		
			if(totalPoint > this.PaymentPoint){
				payPoint = this.PaymentPoint
			}
			else{
				payPoint = totalPoint
			}
			totalPoint = totalPoint - payPoint
			interestPoint = BalancePoint * this.Rate / 100 / 12
			interestPoint = Math.round(interestPoint*100)/100
			BalancePoint = BalancePoint - (payPoint - interestPoint)
			BalancePoint = Math.round(BalancePoint * 100)/100
			if(BalancePoint<2){
				BalancePoint = 0
			}
			if(counter==waktuYear){
				BalanceYearPoint = BalancePoint;
				BalanceYearNoPoint = BalanceNoPoint
			}
			
			text = text + '          <td>'+calcRound(Number(interestNoPoint))+'</td>'
			text = text + '          <td>'+calcRound(BalanceNoPoint)+'</td>'
			text = text + '          <td>'+calcRound(payPoint)+'</td>'
			text = text + '          <td>'+calcRound(interestPoint)+'</td>'
			text = text + '          <td>'+calcRound(BalancePoint)+'</td>'
			text = text + '        </tr>'
		}
		else{
			colorrow=0
			text = text + '        <tr>'
			text = text + '          <td bgcolor="#EEEEEE">'+(counter+1)+'</td>'
			if(totalNoPoint > this.PaymentNoPoint){
				text = text + '          <td bgcolor="#EEEEEE">'+this.PaymentNoPoint+'</td>'
				pay = this.PaymentNoPoint
			}
			else{
				text = text + '          <td bgcolor="#EEEEEE">'+totalNoPoint+'</td>'
				pay = totalNoPoint
			}
			totalNoPoint = totalNoPoint - this.PaymentNoPoint
			interestNoPoint = BalanceNoPoint * this.RateNoPoint / 100 / 12
			interestNoPoint = Math.round(interestNoPoint*100)/100
			BalanceNoPoint = BalanceNoPoint - (pay - interestNoPoint)
			BalanceNoPoint = Math.round(BalanceNoPoint * 100)/100
			if(BalanceNoPoint<2){
				BalanceNoPoint = 0
			}
		
			if(totalPoint > this.PaymentPoint){
				payPoint = this.PaymentPoint
			}
			else{
				payPoint = totalPoint
			}
			totalPoint = totalPoint - payPoint
			interestPoint = BalancePoint * this.Rate / 100 / 12
			interestPoint = Math.round(interestPoint*100)/100
			BalancePoint = BalancePoint - (payPoint - interestPoint)
			BalancePoint = Math.round(BalancePoint * 100)/100
			if(BalancePoint<2){
				BalancePoint = 0
			}
			if(counter==waktuYear){
				BalanceYearPoint = BalancePoint;
				BalanceYearNoPoint = BalanceNoPoint
			}
			text = text + '          <td bgcolor="#EEEEEE">'+calcRound(interestNoPoint)+'</td>'
			text = text + '          <td bgcolor="#EEEEEE">'+calcRound(BalanceNoPoint)+'</td>'
			text = text + '          <td bgcolor="#EEEEEE">'+calcRound(payPoint)+'</td>'
			text = text + '          <td bgcolor="#EEEEEE">'+calcRound(interestPoint)+'</td>'
			text = text + '          <td bgcolor="#EEEEEE">'+calcRound(BalancePoint)+'</td>'
			text = text + '        </tr>'
		}
	}
	text = text + '      </table>'
	text = text + '      <p>&nbsp;</p>'
	
	tmptext = text
	text = "";
	text = "<hr />"
	text = text + '   <table width="100%" border="0">';
    text = text + '    <tr>'
    text = text + '     <th colspan="2" bgcolor="#EEEEEE" scope="col"><div align="center">Loan with Points Summary</div></th>'
	text = text + '          </tr>'
	text = text + '        <tr>'
	text = text + '          <td width="50%"><strong>Term</strong></td>'
	text = text + '          <td width="50%">'+this.Term+' years</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Loan Amount</strong></td>';
	text = text + '          <td>'+calcRound(this.Amount)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Monthly Payment</strong></td>'
	text = text + '          <td>'+calcRound(this.PaymentPoint)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Interest Rate</strong></td>'
	text = text + '          <td>'+this.Rate+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Discount Points</strong></td>'
	text = text + '          <td>'+this.DiscountPoint+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Discount Points Cost</strong></td>'
	text = text + '          <td>'+this.PointAmount+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Total Payment (principal &amp; inerest)</strong></td>'
	text = text + '          <td><strong>'+calcRound(this.PaymentPoint*12*this.Year)+' after '+this.Year+' years</strong></td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Load balance at move out</strong></td>'
	text = text + '          <td>'+calcRound(BalanceYearPoint)+' after '+this.Year+' years</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	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="2" bgcolor="#EEEEEE" scope="col"><div align="center">Loan without Points Summary</div></th>'
	text = text + '    </tr>'
	text = text + '  <tr>'
	text = text + '    <td width="50%"><strong>Term</strong></td>'
	text = text + '    <td width="50%">'+this.Term+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Loan Amount</strong></td>'
	text = text + '    <td>'+calcRound(this.Amount-this.PointAmount)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Monthly Payment</strong></td>'
	text = text + '    <td>'+calcRound(this.PaymentNoPoint)+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Interest Rate</strong></td>'
	text = text + '    <td>'+this.RateNoPoint+'</td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Total Payment (pricipal &amp; interest)</strong></td>'
	text = text + '    <td><strong>'+calcRound(this.PaymentNoPoint*12*this.Year)+' after '+this.Year+' years</strong></td>'
	text = text + '  </tr>'
	text = text + '  <tr>'
	text = text + '    <td><strong>Load balance at move out</strong></td>'
	text = text + '    <td>'+calcRound(BalanceYearNoPoint)+' after '+this.Year+' years</td>'
	text = text + '  </tr>'
	text = text + '</table>'
	
	PayYearPoint = this.PaymentPoint*12*this.Year
	PayYearNoPoint = this.PaymentNoPoint*12*this.Year
	text = text + '<p class="style2">Buying points can save you '+calcRound(PayYearNoPoint - PayYearPoint + (BalanceYearNoPoint-BalanceYearPoint))+' over '+this.Year+' years.      </p>'
	
	text = text + tmptext
	
	document.getElementById('result').innerHTML = text
}
