// JavaScript Document
/***************************
(c) 2008 www.TUFaT.com
All Rights Reserved. Please
do not re-sell/re-distribute
this software.
***************************/
window.onerror=null
bState = true

window.onerror=null
bState = true
oReq = new Collection("Purchase","DP","Amount1","Rate1","Rate2","","")
oVal = new Collection("Purchase","DP","Amount1","Rate1","Rate2","","")
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) {
	amount = oView.Purchase.value - oView.DPvalue

	if(Number(amount)<Number(oView.Amount1.value)){
		alert("Loan amount bigger maximum allowed: "+calcRound(amount));
		return(false);
	}
   Mortgage = new Loan(oView.Purchase.value,oView.DP.value,oView.Amount1.value,oView.Rate1.value,get_selection(oView.Term1),oView.Rate2.value,get_selection(oView.Term2) )
   
   Mortgage.calcPeriods();
   Mortgage.calcPayment()
   Mortgage.calcBlend()
   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) {
   this.item1 = item1
   this.item2 = item2
   this.item3 = item3
   this.item4 = item4
   this.item5 = item5
   this.item6 = item6
   this.item7 = item7
}

function Loan(Purchase,DP,Amount1,Rate1,Term1,Rate2,Term2) {
	this.Purchase = Purchase
	this.DP = DP
	this.Amount1 = Amount1
	this.Amount2 = Number(this.Purchase) - Number(this.Amount1) - Number(this.DP)
	this.Rate1 = Rate1
	this.Rate2 = Rate2
	this.Term1 = Term1
	this.Term2 = Term2
	
	this.calcPeriods = calcPeriods;
	this.calcPayment = calcPayment
	this.calcBlend = calcBlend
   	this.NewPage = NewPage
}

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 calcBlend(){
	this.total = Number(this.Amount1) + Number(this.Amount2)
	this.Blend = Number(this.Rate1) * Number(this.Amount1)/Number(this.total) + Number(this.Rate2) * Number(this.Amount2) / Number(this.total)
	this.Blend = Math.round(this.Blend * 100) / 100
	this.Payment3 = Number(this.Payment1) + Number(this.Payment2)
}

function calcPeriods() {
  this.Periods=12 
}

function calcRound(num) {
   result="$"+Math.floor(num)+"."
   n = result.length
   if (num>1000 && num<999999) {
     result="$"+result.substring(1,n-4)+","+result.substring(n-4,n)
   }
   if (num>1000000) {
     result = "$"+result.substring(1,n-7)+","+result.substring(n-7,n-4)+","+result.substring(n-4,n)
   }
   var cents=100*(num-Math.floor(num))+0.5
   result += Math.floor(cents/10)
   result += Math.floor(cents%10)
   return(result)
}

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" scope="col" bgcolor="#EEEEEE"><strong>Mortgage Loan Summary</strong></th>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td width="50%"><strong>Purchase Price</strong></td>'
	text = text + '          <td width="50%">'+calcRound(this.Purchase)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Down payment </strong></td>'
	text = text + '          <td>'+calcRound(this.DP)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Loan one amount </strong></td>'
	text = text + '          <td>'+calcRound(this.Amount1)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>&nbsp;&nbsp;&nbsp;Interest Rate</strong></td>'
	text = text + '          <td>'+this.Rate1+' %</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>&nbsp;&nbsp;&nbsp;Term</strong></td>'
	text = text + '          <td>'+this.Term1+' Years</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>&nbsp;&nbsp;&nbsp;Monthly payment</strong></td>'
	text = text + '          <td>'+calcRound(this.Payment1)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>Loan two amount </strong></td>'
	text = text + '          <td>'+calcRound(this.Amount2)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>&nbsp;&nbsp;&nbsp;Interest Rate</strong></td>'
	text = text + '          <td>'+this.Rate2+' %</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>&nbsp;&nbsp;&nbsp;Term</strong></td>'
	text = text + '          <td>'+this.Term2+' Year</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td><strong>&nbsp;&nbsp;&nbsp;Monthly payment</strong></td>'
	text = text + '          <td>'+calcRound(this.Payment2)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td bgcolor="#EEEEEE"><strong>Total amount financed</strong></td>'
	text = text + '          <td bgcolor="#EEEEEE">'+calcRound(this.total)+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td bgcolor="#EEEEEE"><strong>Total monthly payment</strong></td>'
	text = text + '          <td bgcolor="#EEEEEE">'+calcRound(Number(this.Payment1) + Number(this.Payment2))+'</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td bgcolor="#EEEEEE"><strong>Blended interest rate</strong></td>'
	text = text + '          <td bgcolor="#EEEEEE">'+this.Blend+' %</td>'
	text = text + '        </tr>'
	text = text + '        <tr>'
	text = text + '          <td>&nbsp;</td>'
	text = text + '          <td>&nbsp;</td>'
	text = text + '        </tr>'
	text = text + '      </table>'
	
	if(Number(this.Term1) > Number(this.Term2)){
		waktu = this.Term1 * this.Periods
		waktu2 = this.Term2 * this.Periods
		Payment = this.Payment1
		interestrate = this.Rate1
	}
	else{
		waktu = this.Term2 * this.Periods
		waktu2 = this.Term1 * this.Periods
		Payment = this.Payment2
		interestrate = this.Rate2
	}
	totAmount1 = this.Amount1
	totAmount2 = this.Amount2;
	
	text = text + '<hr />'
	text = text + '	  <table width="100%" border="0">'
	text = text + '  <tr>'
	text = text + '    <th width="5%" bgcolor="#EEEEEE" scope="col"><strong>Nbr</strong></th>'
	text = text + '    <th bgcolor="#EEEEEE" scope="col"><strong><center>Monthly Payment</center></strong></th>'
	text = text + '    <th bgcolor="#EEEEEE" scope="col"><center>Interest Paid</center></th>'
	text = text + '    <th bgcolor="#EEEEEE" scope="col"><center>Principal Paid</center></th>'
	text = text + '    <th bgcolor="#EEEEEE" scope="col"><strong><center>Total Ending<br />'
	text = text + '      Principle Balance</center></strong></th>'
	text = text + '  </tr>'
	
	colorrow=0;
	for(counter=0;counter<waktu;counter++){
		if(colorrow==0){
			colorrow=1;
			text = text + '  <tr>'
			text = text + '    <td>'+(counter+1)+'</td>'
			if(counter<waktu2){
				if(counter==(waktu-1)){
					tmp1 = totAmount1 * (1 + Number(this.Rate1) / this.Periods / 100)
					tmp2 = totAmount2 * (1 + Number(this.Rate2) / this.Periods / 100)
				
					text = text + '    <td>'+calcRound(tmp1+tmp2)+'</td>'
				}
				else{
					text = text + '    <td>'+calcRound(this.Payment3)+'</td>'
				}
				interest = totAmount1 * this.Rate1 / 100 / this.Periods + totAmount2 * this.Rate2 / 100 / this.Periods
				interest = Math.round(interest * 100) / 100
				prin = Number(this.Payment3) - Number(interest)
			}
			else{
				if(counter==(waktu-1)){
					tmp1 = totAmount1 * (1 + Number(this.Rate1) / this.Periods / 100)
					tmp2 = totAmount2 * (1 + Number(this.Rate2) / this.Periods / 100)
				
					text = text + '    <td>'+calcRound(tmp1+tmp2)+'</td>'
				}
				else{
					text = text + '    <td>'+calcRound(Payment)+'</td>'
				}
				interest = totAmount1 * this.Rate1 / 100 / this.Periods + totAmount2 * this.Rate2 / 100 / this.Periods
				interest = Math.round(interest * 100) / 100
				prin = Number(Payment) - Number(interest)
			}
			totAmount1 = totAmount1 - (this.Payment1 - totAmount1 * this.Rate1 / 100 / this.Periods)
			totAmount2 = totAmount2 - (this.Payment2 - totAmount2 * this.Rate2 / 100 / this.Periods)
			totAmount1 = Math.round(totAmount1 * 100) / 100
			totAmount2 = Math.round(totAmount2 * 100) / 100
			if(totAmount1<5){
				totAmount1 = 0
			}
			if(totAmount2<5){
				totAmount2 = 0
			}
		
			text = text + '    <td>'+calcRound(interest)+'</td>'
			text = text + '    <td>'+calcRound(prin)+'</td>'
			text = text + '    <td>'+calcRound(totAmount2+totAmount1)+'</td>'
			text = text + '  </tr>'
		}
		else{
			colorrow=0;
			text = text + '  <tr>'
			text = text + '    <td bgcolor="#EEEEEE">'+(counter+1)+'</td>'
			if(counter<waktu2){
				if(counter==(waktu-1)){
					tmp1 = totAmount1 * (1 + Number(this.Rate1) / this.Periods / 100)
					tmp2 = totAmount2 * (1 + Number(this.Rate2) / this.Periods / 100)
				
					text = text + '    <td bgcolor="#EEEEEE">'+calcRound(tmp1+tmp2)+'</td>'
				}
				else{
					text = text + '    <td bgcolor="#EEEEEE">'+calcRound(this.Payment3)+'</td>'
				}
				interest = totAmount1 * this.Rate1 / 100 / this.Periods + totAmount2 * this.Rate2 / 100 / this.Periods
				interest = Math.round(interest * 100) / 100
				prin = Number(this.Payment3) - Number(interest)
			}
			else{
				if(counter==(waktu-1)){
					tmp1 = totAmount1 * (1 + Number(this.Rate1) / this.Periods / 100)
					tmp2 = totAmount2 * (1 + Number(this.Rate2) / this.Periods / 100)
				
					text = text + '    <td bgcolor="#EEEEEE">'+calcRound(tmp1+tmp2)+'</td>'
				}
				else{
					text = text + '    <td bgcolor="#EEEEEE">'+calcRound(Payment)+'</td>'
				}
				interest = totAmount1 * this.Rate1 / 100 / this.Periods + totAmount2 * this.Rate2 / 100 / this.Periods
				interest = Math.round(interest * 100) / 100
				prin = Number(Payment) - Number(interest)
			}
			totAmount1 = totAmount1 - (this.Payment1 - totAmount1 * this.Rate1 / 100 / this.Periods)
			totAmount2 = totAmount2 - (this.Payment2 - totAmount2 * this.Rate2 / 100 / this.Periods)
			totAmount1 = Math.round(totAmount1 * 100) / 100
			totAmount2 = Math.round(totAmount2 * 100) / 100
			if(totAmount1<5){
				totAmount1 = 0
			}
			if(totAmount2<5){
				totAmount2 = 0
			}
		
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(interest)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(prin)+'</td>'
			text = text + '    <td bgcolor="#EEEEEE">'+calcRound(totAmount2+totAmount1)+'</td>'
			text = text + '  </tr>'
		}
	}
	text = text + '</table>'
	
	document.getElementById('results').innerHTML = text
}