function cmhc(sfee){
		
		if(sfee>=20){
		  	 sfee = 0 ;
		  	return sfee;
		  }
		
		if(sfee>=15 && sfee<20){
		  	sfee = 1.75 ;
		  	return sfee;
		  }
		  if(sfee>=10 && sfee<15){
		  	sfee = 2.0 ;
		  	return sfee;
		  }
		  if(sfee>=5 && sfee<10){
		  	sfee = 2.75 ;
		  	return sfee;
		  }
		  if(sfee<5 ){
		  	sfee = N/A;
		  	return sfee;
		  }
		
}


//Mortgage Table
function getAmortization(a,n,p,m_p,fee) {
		
		/*
		a = mortgage amount / balance
		n = terms
		p = interest
		m_p = PMT
		fee = downpayment percentage
		*/
		
		var a = a;
		var n = n;
		var p = p;
		var m_p = m_p;
		var fee = fee;
		
		
		/*alert("a="+a);
		alert("n="+n);
		alert("p="+p);
		alert("m_p="+m_p);
		alert("fee="+fee);
		*/
		
		//graph
		var g = new line_graph();
		
		var m_p2;
		m_p = roundOff(m_p,4);
		m_p2 = Math.round(m_p);
		
		var i=0;
		var sATline="";
		var oAmortizationTable=document.getElementById("amortizationtable");
		//oAmortizationTable.style.visibility="visible";
		var sCR=String.fromCharCode(13); 
		var sTab=String.fromCharCode(27);
	  	
	  	var totalinterest_finaltotal

		/* Calculate amortization and write table to text area **/
		var payment = getPayment(a,n,p);
		oAmortizationTable.value = "AMORTIZATION SCHEDULE" + sCR + sCR;
		oAmortizationTable.value += sCR + "Monthly Payment = $" + roundOff(m_p,2) + sCR + sCR + sCR;
		oAmortizationTable.value += "Month    Mortgage      Interest           Principal        Principal           CMHC            Total Monthly       " + sCR;
		oAmortizationTable.value += "              Payment                                                    Balance          Payments          Payments          " + sCR + sCR;
		
		var balance=a;
		var interest = 0.0;
		var principal=0.0;
		var totalinterest=0.0;
		var totalPMT = 0.0;
		var n = n*12;
		//var n = n;
		var monthly = m_p;
		var fcmhc = 0.0;
		var monthlytotal = 0.0;
		var a_year = 0;
		var Array_balance = new Array();
		var Array_interest = new Array();
		// var Array_years = new Array();		
		var principal_b = a;
		
		var p1_p = document.getElementById("p1_p");
		var p1_i = document.getElementById("p1_i");
		// p1_p.style.visibility = "visible";
		// p1_i.style.visibility = "visible";
		p1_p.value = Math.round((balance*(p/100))*100)/100; //Payment #1 Principal $
		p1_i.value = Math.round((monthly - (balance*(p/100)))*100)/100; //PAYMENT #1: Interest: $
				
		
		//CMHC calculation
		var fcmhc = 0.0;
		fcmhc = roundOff(((cmhc(Math.round(fee)) / 100) * a)/( n  ),2);
		
		
		// monthly payment
		var monthlypayment = 0.0;
		monthlypayment = (parseInt(monthly*100))/100 + (parseInt(fcmhc*100))/100;
		
		//alert(" " + monthly +" + "+ fcmhc +" "+monthlypayment);
		
		
		// oAmortizationTable.value += "Month | Mortg.Payment | Interest | Mortg.Principal | Princ.Balance | CMHC | Monthly Payments  " + sCR + sCR;
		
		for (i=1; i<=n; i++) {
			
			interest = balance*(p/100);
			totalinterest += interest;
			principal = monthly - interest;
			balance = balance - principal;
			monthlytotal = monthlytotal + monthlypayment;
			//totalPMT = totalPMT + monthly;
			
			
			//-> Month Displays using the for loop = i	
			if(i > 99) {
				sATline = i.toString() + getSpaces(8-i.toString().length);
				if(m_p > 1000000) {
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 	
				}else if(m_p > 100000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 10000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 1000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 100){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 10){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}
			}else if(i > 9) {
				sATline = "0" + i.toString() + getSpaces(9-i.toString().length);	
				if(m_p > 1000000) {
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 	
				}else if(m_p > 100000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 10000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 1000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 100){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 10){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}
			}else {
				sATline = "00" + i.toString() + getSpaces(9-i.toString().length);	
				if(m_p > 1000000) {
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 100000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 10000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 1000){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 100){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}else if(m_p > 10){
					sATline += getSpaces(11-sATline.length);
					sATline += "$" + roundOff(m_p, 2); 
				}
			}	
			//Ends here	
				
			//-> Interest Condition Display
			if(interest > 1000000) {
				sATline += getSpaces(18-sATline.length);
				sATline += "$" + CurrencyFormatted(Math.round((interest)*100)/100);
			}else if(interest > 100000) {
				sATline += getSpaces(20-sATline.length);
				sATline += "$" + CurrencyFormatted(Math.round((interest)*100)/100);
			}else if(interest > 10000){
				sATline += getSpaces(22-sATline.length);
				sATline += "$" + CurrencyFormatted(Math.round((interest)*100)/100);
			}else if(interest > 1000){
				sATline += getSpaces(24-sATline.length);
				sATline += "$" + CurrencyFormatted(Math.round((interest)*100)/100);	
			}else if(interest > 100){
				sATline += getSpaces(26-sATline.length);
				sATline += "$" + CurrencyFormatted(Math.round((interest)*100)/100);
			}else if(interest > 10){
				sATline += getSpaces(28-sATline.length);
				sATline += "$" + CurrencyFormatted(Math.round((interest)*100)/100);
			}else {
				sATline += getSpaces(30-sATline.length);
				sATline += "$" + CurrencyFormatted(Math.round((interest)*100)/100);
			}
			//Ends here
				 
			//-> Principal Condition Display
			if(principal > 1000000){
				if(i > 99){
					sATline += getSpaces(2-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);	
				}else if(i > 9){
					sATline += getSpaces(2-i.toString().length);	
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}else {
					sATline += getSpaces(2-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}				
			}else if(principal > 100000){
				if(i > 99){
					sATline += getSpaces(4-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);	
				}else if(i > 9){
					sATline += getSpaces(4-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}else {
					sATline += getSpaces(4-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}
			}else if(principal > 10000){
				if(i > 99){
					sATline += getSpaces(6-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);	
				}else if(i > 9){
					sATline += getSpaces(6-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}else {
					sATline += getSpaces(6-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}
			}else if(principal > 1000){
				if(i > 99){
					sATline += getSpaces(10-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);	
				}else if(i > 9){
					sATline += getSpaces(9-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}else {
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}
			}else if(principal > 100){
				if(i > 99){
					sATline += getSpaces(12-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);	
				}else if(i > 9){
					sATline += getSpaces(11-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}else {
					sATline += getSpaces(10-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}
			}else if(principal > 10) {
				if(i > 99){
					sATline += getSpaces(14-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);	
				}else if(i > 9){
					sATline += getSpaces(13-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}else {
					sATline += getSpaces(12-i.toString().length);
					sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);
				}
			}else {
				sATline += getSpaces(11-i.toString().length);
				sATline += "$" + CurrencyFormatted(Math.round((principal)*100)/100);				
			}
			//Ends here
				 
			
			//-> Principal Balance Condition Display
			if(balance > 1000000){
				if(i > 99){
					sATline += getSpaces(6-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);		
				}else if(i > 9){
					sATline += getSpaces(6-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);		
				}else {
					sATline += getSpaces(6-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);		
				}				
			}else if(balance > 100000){
				if(i > 99){
					sATline += getSpaces(10-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}else if(i > 9){
					sATline += getSpaces(9-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);	
				}else {
					sATline += getSpaces(8-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);	
				}				
			}else if(balance > 10000){
				if(i > 99){
					sATline += getSpaces(12-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}else if(i > 9){
					sATline += getSpaces(11-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);	
				}else {
					sATline += getSpaces(10-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}				
			}else if(balance > 1000){
				if(i > 99){
					sATline += getSpaces(14-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}else if(i > 9){
					sATline += getSpaces(12-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);	
				}else {
					sATline += getSpaces(12-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}				
			}else if(balance > 100){
				if(i > 99){
					sATline += getSpaces(16-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}else if(i > 9){
					sATline += getSpaces(14-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);	
				}else {
					sATline += getSpaces(14-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}				
			}else if(balance > 10){
				if(i > 99){
					sATline += getSpaces(18-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}else if(i > 9){
					sATline += getSpaces(16-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);	
				}else {
					sATline += getSpaces(16-i.toString().length)
					sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
				}				
			}else {
				sATline += getSpaces(20-i.toString().length)
				sATline += "$" + CurrencyFormatted(Math.round((balance)*100)/100);
			}
			//Ends here
			

			//-> CMHC Payments Condition Display
			if(fcmhc > 1000000){
				if(i > 99){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else if(i > 9){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else {
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}				
			}else if(fcmhc > 100000){
				if(i > 99){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else if(i > 9){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else {
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}	
			}else if(fcmhc > 10000){
				if(i > 99){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else if(i > 9){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else {
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}
			}else if(fcmhc > 1000){
				if(i > 99){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else if(i > 9){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else {
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}
			}else if(fcmhc > 100){
				if(i > 99){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else if(i > 9){
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}else {
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}
			}else if(fcmhc > 10){
				if(i > 99){
					sATline += getSpaces(10-i.toString().length);
					sATline += "$" + fcmhc;	
				}else if(i > 9){
					sATline += getSpaces(9-i.toString().length);
					sATline += "$" + fcmhc;	
				}else {
					sATline += getSpaces(8-i.toString().length);
					sATline += "$" + fcmhc;	
				}
			}else {
				if(i > 99){
					sATline += getSpaces(11-i.toString().length);
					sATline += "$" + fcmhc;	
				}else if(i > 9){
					sATline += getSpaces(10-i.toString().length);
					sATline += "$" + fcmhc;	
				}else {
					sATline += getSpaces(9-i.toString().length);
					sATline += "$" + fcmhc;	
				}
			}					
			//Ends here
			
			//Total Monthly Payment Condition Display
			if(i > 99){
				sATline += getSpaces(14-i.toString().length);
				sATline += "$" + CurrencyFormatted(monthlypayment) + sCR;
			}else if(i > 9){
				sATline += getSpaces(13-i.toString().length);
				sATline += "$" + CurrencyFormatted(monthlypayment) + sCR;
			}else {
				sATline += getSpaces(12-i.toString().length);
				sATline += "$" + CurrencyFormatted(monthlypayment) + sCR;
			}
				
			
			//Ends here			
			oAmortizationTable.value += sATline;
			
			if((i%12)==0)
			{
				// g.add(i/12,balance);
				
				a_year = parseInt(i/12);
				// Array_years[(i/12)-1] = a_year;
				Array_balance[(i/12)-1] = Math.round((roundOff(balance,2))*100)/100;
				Array_interest[(i/12)-1] = Math.round((roundOff(interest,2))*100)/100;
			}
			
		 }
		 //oAmortizationTable.value += "           " + sCR ;
		 oAmortizationTable.value += "";
		 
		 // g.render("lineCanvas","");
		 
			var c = new Chart(document.getElementById("chart"));

			a_year = parseInt(a_year);
			
			c.setDefaultType(CHART_BAR | CHART_STACKED);
			c.setGridDensity(a_year,20);
			c.setVerticalRange(0,principal_b); 
			
			var myArray2= new Array();
			 
			for (i=0;i<45;i++){
				myArray2[i] = i+1;
			}
			
			c.setHorizontalLabels(myArray2);
			//alert(myArray2);
			
			//c.add('balance','#4040FF', Array_balance);
			
			//-> This will draw the graph
			c.add('Balance','#4040FF',Array_balance); 
			c.add('Interest','#FF0000', Array_balance);
			c.draw();			
			
	}
	
	/*
			c.setDefaultType(CHART_BAR | CHART_STACKED);
			c.setGridDensity(10,20);
			c.setVerticalRange(0,10); 

			c.setHorizontalLabels([1,2,3,4,5,6,7,8,9,10]);
			c.add('$ balance','#4040FF',[1,2,3,4,5,6,7,8,9]);
			c.draw();
			}
			
			
			c.setDefaultType(CHART_BAR | CHART_STACKED);
			c.setGridDensity((a_year),20);
			c.setVerticalRange(0,principal_b); 

			c.setHorizontalLabels([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,40]);
			c.add('$ balance','#4040FF',Array_balance);
			c.draw();
			}
			*/

	function getSpaces(n) {
		var i=0; 
		var sSpaces="";
		for (i=0;i<n;i++) {sSpaces += " ";}
		return sSpaces;
	}

	function getPayment(a,n,p) {
		/* Calculates the monthly payment from annual percentage
		   rate, term of loan in months and loan amount. **/
		var acc=0;
		var base = 1 + p/1200;
		for (i=1;i<=n;i++) 
			{ acc += Math.pow(base,-i); }
		return a/acc;
	}
	
	
	

	


//Mortgage calculation >> Payments After
function mort_payafter(m_years,a,m_p,p,years,feee){
    
    
    	if(m_years > years){
    		m_years = years;
    	}
    	
    
    	var yr = m_years*12; // >> Total Number of Paymentst Made
		
    	var num_paymade=document.getElementById("num_paymade");
		// num_paymade.style.visibility="visible";
    	num_paymade.value = yr;
		// alert(num_paymade.value);
    	
    	var principal_repay = document.getElementById("principal_repay");
		// principal_repay.style.visibility="visible";
		
	  	var principal_repay_percent = document.getElementById("principal_repay_percent");
		// principal_repay_percent.style.visibility="visible";
		
		var interest_paid = document.getElementById("interest_paid");
		// interest_paid.style.visibility="visible";
		
	  	var interest_paid_percent = document.getElementById("interest_paid_percent");
		// interest_paid_percent.style.visibility="visible";
		
		var totalprincipal_remaining = document.getElementById("totalprincipal_remaining");
		// totalprincipal_remaining.style.visibility = "visible";
		
		var totalpayments=document.getElementById("totalpayments");
		// totalpayments.style.visibility="visible";
		
		var ownership = document.getElementById("ownership");
		// ownership.style.visibility="visible";
		
		var downpayment = document.getElementById("downpayment");
		// downpayment.style.visibility = "visible";
		
		var term = document.getElementById("term");
		// term.style.visibility = "visible";
		
		var total_payment_percent = document.getElementById("total_payment_percent");
		// use in total for principal paid percent + interest paid percent
	  	
	  	var balance=a;
	  	var mortgageamount = a;
		var interest = 0.0;
		var principal=0.0;
		var totalinterest=0.0;
		var totalPMT = 0.0;
		var n = m_years*12;
		var monthly = m_p;
		var finalprincipal = 0.0;
		var finterestpaid = 0.0;
		var finalinterestpaid = 0.0;
		var finall = 0.0;
		var fprincipal = 0.0;
		
		var fcmhc = 0.0;
		var monthlytotal = 0.0;
		
		// formula for total interest   n*m_p - monthly
		
		//CMHC calculation
		var fcmhc = 0.0;
		fcmhc = roundOff(((cmhc(Math.round(feee)) / 100) * a)/( n  ),2)
		
		for (aa=1;aa<=n;aa++) {
			
			interest = balance*(p/100);
			totalinterest += interest;
			principal = monthly - interest;
			balance = balance - principal;
			totalPMT = totalPMT + monthly;
			
			finterestpaid = (interest*100)/100;
			finalinterestpaid = finalinterestpaid + finterestpaid;			
			
			if(interest_paid<=0){
				interest_paid.value = 0.0;	
			}
			else{
				interest_paid.value = roundOff(finalinterestpaid,2);
			}		
			
			finalprincipal = Math.round((balance)*100)/100;
			if(finalprincipal<= 0){
				totalprincipal_remaining.value = 0.0;
			}
			else{
				totalprincipal_remaining.value = (roundOff(finalprincipal,2));
			}
			
			fprincipal = fprincipal + principal;
			principal_repay.value = roundOff(fprincipal,2);
				
		 }
		 
		/* monthly payment */
		
		var monthlypayment = 0.0;
		monthlypayment = (parseInt(principal_repay.value*100))/100 + (parseInt(interest_paid.value*100))/100;
		totalpayments.value = roundOff(monthlypayment,2);		
		
		if( Math.round((finalprincipal/a)*100) <= 0  ){
			principal_repay_percent.value = 0.0;
		}
		else{
			//principal_repay_percent.value = roundOff(((finalprincipal/a)*100),2);
			/* Formula is (Principal Paid / Total Payments) */
			principal_repay_percent.value = roundOff(((principal_repay.value/totalpayments.value)*100),2);			
		}
		
		finall = (((years*12)*monthly)-mortgageamount);
		
		if(roundOff(((finalprincipal/finall)*100),2) <= 0){
			interest_paid_percent.value = 0.0;
		}
		else{
			//interest_paid_percent.value = roundOff(((interest/m_p)*100),2); - old
			/* Formula is (Interest Paid / Total Payments) */
			interest_paid_percent.value = roundOff(((interest_paid.value/totalpayments.value)*100),2);
		}
		
		// alert("monthly="+m_p+"  final interest="+finalinterestpaid+ " interest="+interest);
		
		//ownership_calc(I,n,mp,dp,m_principal)
		//m_years,a,m_p,p,years,feee
		//alert(" I ="+ p + " dp =" + downpayment.value +);
		
		ownership.value = roundOff(ownership_calc(p,n,m_p,downpayment.value,a,term.value),2);
		
		var totalpayment_percent = 0.0;
		totalpayment_percent = (parseInt(principal_repay_percent.value*100))/100 + (parseInt(interest_paid_percent.value*100))/100;
		total_payment_percent.value = roundOff(totalpayment_percent,2);
		
}

		

	
// Calculator for ownership
/*
Formula

Ownership= [ dp + ( mort_prn - PV ) ] / TotalAmount
Ownership=(D9+H41)/D7
PV = pmt * (((1+I)^n)-1/I*((1+I)^n))

I  = I / 100;        //convert to percentage
v1 = (Math.pow((1 + I), n)) - 1;
v2 = I * Math.pow((1 + I), n);
pv = pmt * (v1 / v2);
pv = number_format(pv, 2, ".", "");
return pv; 
*/


function ownership_calc(I,n,mp,dp,m_principal,term){

	// dp - downpayment
	// term - terms

	
	var I = I/100; // interest
	var n = (term*12)-n; // number of months
	//var mp = mp; //monthly payment
	var pv,pv_upper,pv_lower,ownership,own_,amount = 0.0;
	
	pv_upper = (Math.pow((1+I),n))-1;
	pv_lower = (I*(Math.pow((1+I),n)));
	pv = mp * (pv_upper/pv_lower);

	own_ = (m_principal - pv)+ parseInt(dp);
	amount = parseInt(m_principal) + parseInt(dp);
	ownership = (own_ / amount)*100;
	//ownership = (dp + ( m_principal - pv )) / 500000;
	
		
	return ownership;

	//alert("I="+I+" n="+n+" mp="+mp+" dp="+dp+" m_pr="+m_principal + " terms="+ term + "   PV="+ownership);

}





//roundOff
/**/

function roundOff(value, dplaces){
	value=value.toString()
	if((value.indexOf(".")!=-1)&&(value.length>(value.indexOf(".")+dplaces))){
	    three=value.substring(value.indexOf(".")+dplaces+1,value.indexOf(".")+dplaces+2)
	    one=value.substring(0,value.indexOf(".")+dplaces)
	    two=value.substring(value.indexOf(".")+dplaces,value.indexOf(".")+dplaces+1)
	    if(parseInt(three)>=5){value=one+(parseInt(two)+1);value=parseFloat(value)}
	else{value=one+two;value=parseFloat(value)}
		}
	       return value;
}

/*
function roundOff(value, dplaces){

	value=value.toString()

	if((value.indexOf(".")!=-1)&&(value.length>(value.indexOf(".")+dplaces))){
	    
	    three=value.substring(value.indexOf(".")+dplaces+1,value.indexOf(".")+dplaces+2)
	    one=value.substring(0,value.indexOf(".")+dplaces)
	    two=value.substring(value.indexOf(".")+dplaces,value.indexOf(".")+dplaces+1)
	
			if(parseInt(three)>=5){
				value=one+(parseInt(two)+1);value=parseFloat(value)
			}
	
		else{
		
			value=one+two;value=parseFloat(value);0
			}
	
		}
	value = value.toString()

	x = value.substring(value.indexOf("."),value)

	if(x.indexOf(".")!=-1){
		if((x.length) == 2){value = value + "0"}
		if((x.length) == 1){ value = value + ".00"}	
	}
	else{value = value + ".00"}	
				  
	       return value;
	       
}*/




function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	var s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;

}




	
// rounder
/**/
function rounder(roundnumber){
	
	var number = roundnumber;
	var number_int, number_point = 0;
	
	number_int = parseInt(number);
	number_point = number - number_int;
	
	if(number_point > 0){
	   roundnumber= number_int+1;
	   return roundnumber;
	}
	if(number_point <= 0){
	  roundnumber = number_int;
	  return roundnumber;
	}

}






//Interest calculator
function calc_interest(interest){

	
	var apr2 = document.getElementById("apr2");
	
	
	
	
	var a,b,c,d,e,f,g,h,i = 0.0;

	a = interest/100;
	b = a/2;
	c = b+1;
	d = Math.pow(c,2);
	e = -1+d; // EAR
	
	f = Math.pow(d,(1/12));
	g = (f-1)*100; //effective interest
 	
 	apr2.value = "" + g;
	
 	
 	/*
 	FORMULAS
 	ear = ((1+(interest/2))^2)-1;
 	effective mnthly rate = ( ((1+ear)^(1/12) ) - 1;
 	
 	x = Math.pow(((1+(Math.pow(1+((interest/100)/2)),2))-1),(1/12))-1;
	calculator test: ((1+(((1+((interest/100)/2))^2)-1))^(1/12))-1
 	*/
	
	
}

function calc_interest2(interest,term,mo_am,dp,dd){

	
	var apr2 = document.getElementById("apr2");
	var apr2_php = document.getElementById("apr2_php");
	var term_php = document.getElementById('term_php');
	var mo_am_php = document.getElementById('mo_am_php');
	var downpaymentbypercent_php = document.getElementById('downpaymentbypercent_php');
	var down_php = document.getElementById('down_php');

	
	var v_interest = interest;
		
	if (v_interest <= 0){	
		
		apr2.value = 0;
		apr2_php.value = 0;
	
	}
	
	if(v_interest > 0){
	
	
		//term_php.value = document.getElementById('term');
		//mo_am_php.value = document.getElementById('mo_am');	
		
		term_php.value = term;
		mo_am_php.value = mo_am;
		downpaymentbypercent_php.value = dp;
		down_php.value = dd;
	    
		
		// apr2.style.visibility = "visible";
		
		var a,b,c,d,e,f,g,h,i = 0.0;
		
		a = interest/100;
		b = a/2;
		c = b+1;
		d = Math.pow(c,2);
		e = -1+d; // EAR
		
		f = Math.pow(d,(1/12));
		g = (f-1)*100; //effective interest
	 	
	 			
		 	
	 	apr2.value = "" + g;
	 	apr2_php.value = "" + g;
	
	
	 	//downpaymentbypercent
	 	
	 	/*
	 	FORMULAS
	 	ear = ((1+(interest/2))^2)-1;
	 	effective mnthly rate = ( ((1+ear)^(1/12) ) - 1;
	 	
	 	x = Math.pow(((1+(Math.pow(1+((interest/100)/2)),2))-1),(1/12))-1;
		calculator test: ((1+(((1+((interest/100)/2))^2)-1))^(1/12))-1
	 	*/
	}
}

//Mortgage Laws
//CMHC fee
function mortgagelaws(rate){
  
	/*
	between 
	20%-100% = 0%
	15%-19.99% = 1.75%
	10%-14.99% = 2.0%
	5.0%-9.99% = 2.75%
	0% - 5.0% = Prohibited
	*/  

 var mortgage_am = document.getElementById("mo_am");
 // mortgage_am.style.visibility="visible";
  
  rate = parseInt(rate);
  var rates = 0.0;
  
  if(rate>=20){
  	rates = Math.round((0/100) * mortgage_am.value);
  }
  if(rate>=15 && rate<20){
  	rates = Math.round((1.75/100) * mortgage_am.value);
  }
  if(rate>=10 && rate<15){
  	rates = Math.round((2.00/100) * mortgage_am.value);
  }
  if(rate>=5 && rate<10){
  	rates = Math.round((2.75/100) * mortgage_am.value);
  }
  if(4>=rate){
    rates = Math.round((100/100) * mortgage_am.value);
    alert("Fee rate prohibited");
  }
  else{} 
	
	return rates;
	

}


//calculation for PMT - Total monthly payment
function pmt(r,np,pv) {
		
	var r = r;
	var np = np;
	var pv = pv;
	
	
	
	if(r != 0 &&  np != 0 && pv !=0 && pv != 'NaN' ){
	
		var lineCanvas = document.getElementById('amortizationtable');
		var lgraph = document.getElementById('amortizationgraph');
		var chart = document.getElementById('chart');
		
		//lineCanvas.style.display = "block";
		lgraph.style.display = "block";
		//chart.style.display = "block";
		
		/*
		var r = document.getElementById('apr2').value;
		var np = document.getElementById('term').value;
		var pv = document.getElementById('mo_am').value;
		*/
		// alert("Term " +termm+ ""+np);
		
	
		
		
		
		
		
		np = np * 12;
		r   = r / 100;    // convert to a percentage
		var value1 = r * Math.pow((1 + r), np);
		var value2 = Math.pow((1 + r), np) - 1;
		var pmt    = pv * (value1 / value2);
		
		
		
		var pmt_ = document.getElementById("pmt");
		// pmt_.style.visibility = "visible";
		pmt_.value = roundOff(pmt, 2); // Get Monthly Mortgage
		
		
		var downpaymentbypercent = document.getElementById("downpaymentbypercent");
		// downpaymentbypercent.style.visibility = "visible";
		
		var pmt_cmhc = document.getElementById("pmt_cmhc");
		// pmt_cmhc.style.visibility = "visible";
		
		pmt_cmhc.value = mortgagelaws(downpaymentbypercent.value);
		
		calc_interest2(document.getElementById('apr').value,document.getElementById('term').value,document.getElementById('mo_am').value,document.getElementById('downpaymentbypercent').value,document.getElementById('downpayment').value)
		
		getAmortization(document.getElementById('mo_am').value,document.getElementById('term').value,document.getElementById('apr2').value,document.getElementById('pmt').value,document.getElementById('downpaymentbypercent').value)
		
	
	}
	
	else{
	
	alert("Some fields don't have value or Not Applicable");
	
	}
		
}


//end of PMT

//Automatic calculate the downpayment by amount or percent
function downpayment(input) {

	// alert(input);
	
	var downpayment_amount = document.getElementById("amount");
	var downpayment = document.getElementById("downpayment");
	var downpaymentbypercent = document.getElementById("downpaymentbypercent");
	var mortgage_am = document.getElementById("mo_am");
	var downpayment_percent = 0.0;
	
	    // mortgage_am.style.visibility="visible";
		// downpaymentbypercent.style.visibility = "visible";
		
		mortgage_am.value =+ downpayment_amount.value - input;
		document.getElementById('mo_am').value = downpayment_amount.value - input;
		
		//downpaymentbypercent.value =+ ( 1 - ((mortgage_am.value / downpayment_amount.value)) ) * 100;
		
		downpayment_percent =+ (input / downpayment_amount.value) * 100;
		downpaymentbypercent.value = roundOff(downpayment_percent,2);
			
		if(downpaymentbypercent.value < 20){
    		alert("Downpayment is less than 20%");
		}
				
	// alert("Mortgage amount  $ " + mortgage_am.value);
		
}


function downpaymentbypercent(input) {
	
	// alert(input);
	var downpayment = document.getElementById("downpayment");
	var downpayment_amount = document.getElementById("amount");
	var downpaymentbypercent = document.getElementById("downpaymentbypercent");
	var mortgage_am=document.getElementById("mo_am");
	var downpayment_percent = 0.0;
	
		// mortgage_am.style.visibility="visible";
	    // downpayment.style.visibility = "visible";
	
	if(downpaymentbypercent.value < 20){
    	alert("Downpayment is less than 20%");
    }
      
	    downpayment.value =+ (downpayment_amount.value * (input/100));
			
		//downpayment.value =+ (downpayment.value / downpayment_amount.value) * 100;
	    mortgage_am.value =+ downpayment_amount.value - downpayment.value;
	    document.getElementById('maaa').value = downpayment_amount.value - downpayment.value;
		
	    
	// alert("Mortgage amount  $ " + mortgage_am.value);
	  
	
}
//end of downpayment amount and percentage automatic calculate


function display_alert()
{
	alert("Insert either $ value or % value for downpayment  ( Prohibited = 0%-5%) as per CMHC rules");
		
}





function dropdown(mySel)
{
var myWin, myVal;
myVal = mySel.options[mySel.selectedIndex].value;
if(myVal)
   {
   if(mySel.form.target)myWin = parent[mySel.form.target];
   else myWin = window;
   if (! myWin) return true;
   myWin.location = myVal;
   }
return false;
}



///=====================================/
// Land Transfer Tax Functions

function ltt(amount,first_time){
	
	
	var ttax, tax1, tax2 = 0.0;
	
	if(first_time == false){
	
	//alert("false");
	//city_tax(amount);
	
	tax1 = province_tax(amount);
	tax2 = city_tax(amount);
	ttax = tax1 + tax2;
	
	var city = document.getElementById("city");
	// city.style.visibility = "visible";
	city.value = tax2; 
		
	var prov = document.getElementById("prov");
	// prov.style.visibility = "visible";
	prov.value = tax1;
	
	var tax = document.getElementById("tax");
	// tax.style.visibility = "visible";
	tax.value = ttax;
	
	
	 
	}
	
	else{

	// alert("true" +amount);
	
	tax1 = province_tax(amount);
	tax2 = 0;
	ttax = tax1 + tax2;
	
	var city = document.getElementById("city");
	// city.style.visibility = "visible";
	city.value = tax2; 
		
	var prov = document.getElementById("prov");
	// prov.style.visibility = "visible";
	prov.value = tax1;
	
	var tax = document.getElementById("tax");
	// tax.style.visibility = "visible";
	tax.value = ttax;
	
	}
}

function city_tax(amount){ // toronto 
	 var amount2 = 0.0;
		 if(0 < amount <= 55000){
		 	amount2 = 55000 * 0.005;	 	
		 }
		 if(55000 < amount) {
			if (400000 >= amount){
		 	amount2 = ((55000 * 0.005) + ((amount - 55000) * 0.01));
			}
		 }
		 if(amount > 400000){
		 	amount2 = ((55000 * 0.005) + ((400000 - 55000) * 0.01) + ((amount - 400000) * 0.02));
		 }	
	 return amount2;
}

/*function city_tax(amount){ // toronto 
	 var amount2 = 0.0;	 
		 if(amount < 0){
		   amount2 = amount * 0;
		 }
		 if(0 <= amount <= 55000){
		 	amount2 = amount * 0.005;	 	
		 }
		 if(400000 >= amount > 55000){
		 	amount2 = amount * 0.01;
		 }
		 if(amount > 400000){
		 	amount2 = amount * 0.02;
		 }	
	 return amount2;
}*/

function province_tax(amount){ //ontario
	 var amount3 = 0.000;
	 
	 if(amount < 0){
	   amount3 = amount * 0;
	 }
	 if(0 <= amount){
		if(amount <= 55000){
		amount3 = amount * 0.005;	 	
	 	}
     }
	 if(250000 >= amount){
		if(amount > 55000){
        amount3 = ((55000 * 0.005) + ((amount - 55000) * 0.01)); }
	 }
	 if(400000 >= amount){
		if(amount > 250000){
	 	amount3 = ((55000 * 0.005) + ((250000 - 55000) * 0.01) + ((amount - 250000) * 0.015)); }
	 }
	 if(amount > 400000){
	 	amount3 = ((55000 * 0.005) + ((250000 - 55000) * 0.01) + ((400000 - 250000) * 0.015) + ((amount - 400000) * 0.02));
	 }
 	 else{}
	 
	  	 //	alert(amount3);
	  return amount3;


  
}


/*function province_tax(amount){ //ontario
 
	 var amount2 = 0.0;
	 
	 if(amount < 0){
	   amount2 = amount * 0;
	 }
	 if(0 <= amount <= 55000){
	 	amount2 = amount * 0.005;	 	
	 }
	 if(250000 >= amount > 55000){
	 	amount2 = amount * 0.01;
	 }
	 if(400000 >= amount > 250000){
	 	amount2 = amount * 0.015;
	 }
	 if(amount > 400000){
	 	amount2 = amount * 0.02;
	 }
	 
	  return amount2;
	  
}*/


//////// ==================================
// simple Mortgage

//calculation for simple PMT - Total monthly payment
function s_pmt() {
	
	
	var rr = document.getElementById('s_apr').value;
	var np = document.getElementById('s_term').value;
	var pv = document.getElementById('s_mortgage_amount').value;
	
	
	var a,b,c,d,e,f,g,h,i = 0.0;

	a = rr/100;
	b = a/2;
	c = b+1;
	d = Math.pow(c,2);
	e = -1+d; // EAR
	
	f = Math.pow(d,(1/12));
	g = (f-1)*100;
	
	

	
	var r = g;
	
	np = np * 12;
	r   = r / 100;    // convert to a percentage
	var value1 = r * Math.pow((1 + r), np);
	var value2 = Math.pow((1 + r), np) - 1;
	var pmt    = pv * (value1 / value2);
	
	var pmt_ = document.getElementById("s_pmt");
	// pmt_.style.visibility = "visible";
	pmt_.value =+ roundOff(pmt,2);
	
}

//Interest calculator
function r_calc_interest(interest){

	
	// var apr2 = document.getElementById("apr2");
	// apr2.style.visibility = "visible";
	
	var a,b,c,d,e,f,g,h,i = 0.0;

	a = interest/100;
	b = a/2;
	c = b+1;
	d = Math.pow(c,2);
	e = -1+d; // EAR
	
	f = Math.pow(d,(1/12));
	g = (f-1)*100; //effective interest
 	
 	// apr2.value = "" + g;
	// alert(" Effective Rate -> " +g);
 	
 	return g;
 	
 	/*
 	FORMULAS
 	ear = ((1+(interest/2))^2)-1;
 	effective mnthly rate = ( ((1+ear)^(1/12) ) - 1;
 	
 	x = Math.pow(((1+(Math.pow(1+((interest/100)/2)),2))-1),(1/12))-1;
	calculator test: ((1+(((1+((interest/100)/2))^2)-1))^(1/12))-1
 	*/
	
}

function show_hide(div_id){

var SMC = document.getElementById('SMC');
var AMC = document.getElementById('AMC');
var LTT = document.getElementById('LTT');

if(div_id == "SMC"){
  SMC.style.display = 'block';
  AMC.style.display = 'none';
  LTT.style.display = 'none';
}
if(div_id == "AMC"){
  SMC.style.display = "none";
  AMC.style.display = "block";
  LTT.style.display = "none";
}
if(div_id == "LTT"){
  SMC.style.display = 'none';
  AMC.style.display = 'none';
  LTT.style.display = 'block';
}

}
