<?php 


?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
 <link href="content/LoanCalc.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="scripts/jquery-1.7.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    </head>
    <body>
    <div id="wraper">
       <div class="clear"></div>
               <div class="textarea-small">
            <div class="nav">
                <h1>HOW LENDERS PAY MORTGAGE BROKERS</h1>
            </div>
            <p>
            Lenders pay Mortgage Brokers 2 types of commission. First the Lender pays Mortgage Brokers an Upfront commission for introducing the business
to the lender and this is a % of the loan amount. The greater the debt the more upfront the Mortgage Broker receives. The Lender then pays the 
Mortgage Broker a Trail commission to maintain the debt with the lender and this is a % of the loan balance each month You can use this Lender 
Commission calculator to see how much the Lender will pay the Mortgage Broker over a period of up to 30 years.
            </p>
     </div>
     <div class="clear"></div>
     <div class="textarea" style="height: 340px!important;">
     <div class="nav">
                <h1>ENTER YOUR DETAILS NOW</h1>
     </div>
     <div id="ld1" class="firstbox lenderbox" >
     <div class="nav3"><h1>YOUR LOAN DETAILS</h1>
     <table width="100%" style="margin-top: 40px" >
     <tr>
     <td>
     <h1> Loan Amount $</h1>
     </td>
     <td>
     <input name="Amount" id="txtAmount" type="text" style="width: 92px;" class="mac" value="500000" onKeyPress="return onlyNumbers();" />
     </td>
     </tr>
     <tr>
     <td>
     <h1>Interest Rate %</h1>
     </td>
     <td>
     <input type="text" id="txtRate" value="5" style="width: 30px;" class="mac" onKeyPress="return onlyNumbers();"  /> 
     </td>
     </tr>
     <tr>
      <td>
     <h1>Considered Term (Years)</h1>
     </td>
     <td>
     <label id="txtTerm" value="30" class="mac" style="width: 30px;color:white">30</label>
     </td>
     </tr>
     <tr>
     <td>
     <h1>Lender</h1>
     </td>
     <td>
     <?php 
  include('config.php');
  $query = "SELECT  id, `lender_name` FROM  `tbllenders` order by `lender_name`";
   // Execute it, or return the error message if there's a problem.
  $result = mysql_query($query) or die(mysql_error());
  $dropdown = "<select name='ddllender1' id='ddllender1' class='mac' style='width: 160px;'><option value='0'>--Select Lender--</option>";
  while($row = mysql_fetch_assoc($result)) {
  	$dropdown .= "\r\n<option value='{$row['id']}'>{$row['lender_name']}</option>";
  }
  $dropdown .= "\r\n</select>";
  echo $dropdown;
  
  ?>
     </td>
     </tr>
     <tr>
     <td colspan="2" class="msg">
     Select lender to get savings values for selected vendor.
     </td>
     </tr>
     </table>
     </div>
      
    </div>
     <div id="ld1" class="firstbox lenderbox" style="float: right;margin-right: 20px;">
     <div class="nav3"><h1>WHAT YOU SAVE</h1>
     <table width="100%" style="margin-top: 40px" >
     <tr>
     <td>
     <h1> Upfront Commission $</h1>
     </td>
     <td>
     <input name="upcomm" id="txtupcomm" type="text" style="width: 80px;" class="mac" value="0"  readonly />
     </td>
     </tr>
     <tr>
     <td>
     <h1>Trail Commission<span style="color: #FC7E8D"><b>*</b></span> $</h1>
     </td>
     <td>
     <input type="text" id="txttrcomm" style="width: 80px;" class="mac"  value="0"  readonly /> 
     </td>
     </tr>
     <tr>
     <td>
     <h1>Total Commission $</h1>
     </td>
     <td>
      <input type="text" id="txttotal"  style="width: 80px;" class="mac" value="0"  readonly />
     </td>
     </tr>
     <tr>
     <td colspan="2" ><span style="color: #FC7E8D;font-style: italic;"><b>*</b> Paid over next 30 years</span></td>
     </tr>
     </table>
     </div>
    </div>
     </div>
      <div class="clear"></div>
               <div class="textarea-small">
            <div class="nav">
                <h1>ASSUMPTION AND WARNING</h1>
            </div>
            <p>
           We rebate 100% of lender commission less GST and our fee, interest rates remain constant for term of the loan, you make your pay minimum repayments 
You do not default, loan is maintained for 30 years. This is an illustration of the potential commission that Lenders may pay a Mortgage Broker. This is not a recommendation or offer of finance. You should not make a decision to engage our services based purely on this illustrated calculation. What you can do however is contact us for a full and concise individual assessment of your financial needs
            </p>
     </div>
   </div>
   <script type="text/javascript">
   $('#calculate').click(function(){
	   GetLenderDetails($('#ddllender1 option:selected').val() ,$('#ddllender1 option:selected').text());  
	   });
   $( "#ddllender1" ).change(function() {
	   GetLenderDetails($('#ddllender1 option:selected').val() ,$('#ddllender1 option:selected').text());
	 });
   
   function addCommas(amount) {

       var result = amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
       return result;
   }
   function removeCommas(amount) {
       var result = amount.toString().replace(/\,/g, '');
       return result;

   }
   function GetLenderDetails(lender,name)
	{
		
		var dataString = 'id='+ lender;
		
		$.ajax({
   	    type: 'POST',
   	    data: dataString,
   	    url: 'lenderdetails.php',
   	    async: false,
   	    success: function(data){
   	    	data = $.parseJSON(data);
   	    	
   	    	
   	    	jQuery.each(data, function(i, val) {
   	    		 GetMonthlyCalculation(val.upfront,val.trail1,val.trail2,val.trail3,val.trail4,val.trail5);
		 		});
   	    	
   	    }
		});
	
	}

   function onlyNumbers(evt) {
       var e = event || evt; // for trans-browser compatibility
       var charCode = e.which || e.keyCode;

       if (charCode != 46 && charCode > 31
         && (charCode < 48 || charCode > 57))
           return false;

       return true;
   }

	function GetMonthlyCalculation(uf,t1,t2,t3,t4,t5)
	{
		var Points = [];
		 var amount = document.getElementById('txtAmount').value;
         amount = removeCommas(amount);
         amount = parseFloat(amount); // Adding Fintrack Fee
       
         var term = 30*12;
         var detUpFront = Math.round(parseFloat(amount) * parseFloat(uf/100));
         var interestRate = document.getElementById('txtRate').value;
         interestRate = interestRate/1200;
         var firstInstall = Math.round(amount * interestRate / (1 - (Math.pow(1 / (1 + interestRate), term))));
         //document.getElementById('txtPayment').value = parseFloat(firstInstall).toFixed(2);
         document.getElementById('txtupcomm').value = '$' + parseFloat(detUpFront).toFixed(2);
         var factor=12;
         //Loop to calculate new term 
         var count=0;
         var countInt=0;
         var newTerm=0;
         var totalTrail=0;
         var trailComVal=0;
         for (var i = amount; i >= 0;) {
             if(i<=0)
             {
            	break;	 
             }
            
             if(count <=12)
             {
            	 var interst = Math.round((parseFloat(i) * parseFloat(interestRate))); /// / parseFloat(parseFloat(factor * 100)));
                 countInt = parseFloat(interst) + parseFloat(countInt);
                 var pricSide = Math.round((parseFloat(firstInstall)) - parseFloat(interst));
                 i = Math.round(parseFloat(i) - parseFloat(pricSide));
                 trailComVal = Math.round(parseFloat(i) * parseFloat(t1/100)) / 12;
                 totalTrail = totalTrail + trailComVal; 
                 i = parseFloat(i) - parseFloat(trailComVal);
                 
              }
             else if(count>12 && count<=24)
             {
                 var interst = Math.round((parseFloat(i) * parseFloat(interestRate))); /// / parseFloat(parseFloat(factor * 100)));
                 countInt = parseFloat(interst) + parseFloat(countInt);
                 var pricSide = Math.round((parseFloat(firstInstall)) - parseFloat(interst));
                 i = Math.round(parseFloat(i) - parseFloat(pricSide));
                 trailComVal = Math.round(parseFloat(i) * parseFloat(t2/100)) / 12;
                 totalTrail = totalTrail + trailComVal;
                 i = parseFloat(i) - parseFloat(trailComVal);
                 
             }
             else if(count>24 && count <=36)
             {
            	 var interst = Math.round((parseFloat(i) * parseFloat(interestRate))); /// / parseFloat(parseFloat(factor * 100)));
                 countInt = parseFloat(interst) + parseFloat(countInt);
                 var pricSide = Math.round((parseFloat(firstInstall)) - parseFloat(interst));
                 i = Math.round(parseFloat(i) - parseFloat(pricSide));
                 trailComVal = Math.round(parseFloat(i) * parseFloat(t3/100)) / 12;
                 i = parseFloat(i) - parseFloat(trailComVal);
                 totalTrail = totalTrail + trailComVal;
                 
             }
             else if(count>36 && count <=48)
             {
            	 var interst = Math.round((parseFloat(i) * parseFloat(interestRate))); /// / parseFloat(parseFloat(factor * 100))); / parseFloat(parseFloat(factor * 100)));
                 countInt = parseFloat(interst) + parseFloat(countInt);
                 var pricSide = Math.round((parseFloat(firstInstall)) - parseFloat(interst));
                 i = Math.round(parseFloat(i) - parseFloat(pricSide));
                 trailComVal = Math.round(parseFloat(i) * parseFloat(t4/100)) / 12;
                 i = parseFloat(i) - parseFloat(trailComVal);
                 totalTrail = totalTrail + trailComVal;
                 
             }
             else if(count>48)
             { 
            	 
             var interst = Math.round((parseFloat(i) * parseFloat(interestRate))); /// / parseFloat(parseFloat(factor * 100))); / parseFloat(parseFloat(factor * 100)));
             countInt = parseFloat(interst) + parseFloat(countInt);
             var pricSide = Math.round((parseFloat(firstInstall)) - parseFloat(interst));
             i = Math.round(parseFloat(i) - parseFloat(pricSide));
             trailComVal = Math.round(parseFloat(i) * parseFloat(t5/100)) / 12;
             i = parseFloat(i) - parseFloat(trailComVal);
             totalTrail = totalTrail + trailComVal;
             
             }

             count ++;
         }
      
         document.getElementById('txttrcomm').value = '$' + addCommas(totalTrail.toFixed(2));
         var tcomm=0;
          tcomm = parseFloat(detUpFront) + parseFloat(totalTrail);
          document.getElementById('txttotal').value = '$' + addCommas(tcomm.toFixed(2));
         return ""

         
	}
	
   </script>
    </body>
    </html>