
 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	 
 // Mortgage implementation (calculette.html)
 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	

 var oMortgage = new mortgage();
		
 function calc_mortgage()
 {
 calc_pret();
 document.forms[0].f_total.value = oMortgage.get_amount(
														document.forms[0].f_pret.value, 
														document.forms[0].f_interets.value, 
														document.forms[0].f_amortissement.value, 
														document.forms[0].f_versements.value
																													
													  ) + '$';
 }
		
 function calc_pret()
 {
 document.forms[0].f_pret.value = document.forms[0].f_cout.value - document.forms[0].f_fonds.value;
 }


 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	 
 // Mortgage object
 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	

 function mortgage()
 {
 	this.TYPE_WEEKLY        = 0;
 	this.TYPE_BIWEEKLY      = 1;
 	this.TYPE_MONTHLY       = 2;
 	this.TYPE_2XMONTHLY     = 3;
 	this.TYPE_ACCELL_BI     = 4;
 	this.TYPE_ACCELL_WE     = 5;
	
	this.vPayments_per_year = new Array(52, 26, 12, 24, 12, 12);
	this.get_amount = __get_amount;
 }	

 
 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	 
 // Get the amount of each payments
 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	

 function __get_amount(iLoan_amount, iInterest_rate, iTerm, iPayments_type)
 {
 
	iPayments_per_year = this.vPayments_per_year[iPayments_type]; 
    d = (Math.pow(1.0 + iInterest_rate / 200.0, 1.0 / (iPayments_per_year / 2.0)) - 1.0) * (iPayments_per_year * 100.0);
	
	if (iPayments_type == this.TYPE_ACCELL_WE || iPayments_type == this.TYPE_ACCELL_BI)
		{
		iMonthly_payment   = _PMT(d / (iPayments_per_year * 100), iTerm * iPayments_per_year, iLoan_amount);
		iPayments_per_year = (iPayments_type == this.TYPE_ACCELL_WE) ? 52 : 26;
		d 				   = (Math.pow(1.0 + iInterest_rate / 200.0, 1.0 / (iPayments_per_year / 2.0)) - 1.0) * (iPayments_per_year * 100.0);
		iMonthly_payment   = (iMonthly_payment * 13.0) / iPayments_per_year;
		}
	   else
	   	{
		iMonthly_payment   = _PMT(d / (iPayments_per_year * 100), iTerm * iPayments_per_year, iLoan_amount);
		}
		
	return _format_money(iMonthly_payment);
	
 }

 
 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	 
 // PMT Lib
 //=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*// 	

 function _PMT(d, d1, d2)
 {
    if(d1 <= 0.0)
        return d2;
    if(d1 <= 1.0)
        return d2 * (1.0 + d);
    if(d  == 0.0)
        return d2 / d1;
    else
        return (d2 * d) / (1.0 - Math.pow(1.0 + d, d1 * -1));
 }
	
 function _format_money(mnt) 
 {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/00;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
 }
 




