$().ready(function() {
	// validate signup form on keyup and submit
  if(document.getElementById('mortgagecalculator')) { // error checking 
	$("#mortgagecalculator").validate({ // Select Boxes
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("p"));
		},
		submitHandler: calculateMortgage,
		rules: {
			mort_listprice: {
				required: true,
				digits:true
			},
			mort_interestrate: {
				required: true,
				rangeValue:[2,20]
			},
			mort_amortization: {
				required: true,
				rangeValue:[1,40]
			}
		},
		messages: {
			mort_listprice: {
				required: "Please provide a Mortgage Amount"
			},
			mort_interestrate:{
				required: "Please provide an Interest Rate"
			},
			mort_amortization: {
				required: "Please provide an Amortization Period"
			}
		}
	});
  }

  if(document.getElementById('affordabilitycalculator')) { // error checking 
	$("#affordabilitycalculator").validate({ // Select Boxes
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("p"));
		},
		submitHandler: calculateAffordability,
		rules: {
			affordability_income: {
				required: true,
				digits:true
			},
			affordability_interestrate: {
				required: true,
				rangeValue:[2,20]
			}
		},
		messages: {
			affordability_income: {
				required: "Please provide an Annual Income"
			},
			affordability_interestrate:{
				required: "Please provide an Interest Rate"
			}
		}
	});
  }

  if(document.getElementById('landtransfertaxcalculator')) { // error checking 
	$("#landtransfertaxcalculator").validate({ // Select Boxes
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("p"));
		},
		submitHandler: calculateLandTransferTax,
		rules: {
			ltt_listprice: {
				required: true,
				digits:true
			},
			ltt_province: {
				required: true
			}
		},
		messages: {
			ltt_listprice: {
				required: "Please provide a Amount"
			},
			ltt_province:{
				required: "Please select a Province"
			}
		}
	});
  }

	$("#redbdialogtabs a").click(function(){
		$(".calculator").hide();
		$($(this).attr("href")).show();
		return false;
	});

	$(".calculator").hide();
	$("#mortgage").show();
});

var compound = 2;
function calculateMortgage(){
	// get our initial values
	var listprice = $("#mort_listprice").val();
	var downpayment = ($("#mort_downpayment").val())? $("#mort_downpayment").val() : 0;
	var interestrate = $("#mort_interestrate").val()/100;
	var amortization = $("#mort_amortization").val();
	
	// caclulate our base mortgage
	var mortgage = listprice - downpayment;
	
	
	var compoundrate = compound / 12;
	var monthlytime = amortization * 12;
	var yearlyrate = interestrate / 2;
	var rdefine = Math.pow((1.0 + yearlyrate),compoundrate) - 1.0;
	var compoundfactor = Math.pow((1.0 + rdefine),monthlytime);
	var basepayment = (mortgage * rdefine * compoundfactor) / (compoundfactor - 1.0);
	
	var payment = (Math.ceil(basepayment * 100) / 100).toFixed(2);
	
	$("#paymentmontlyvalue").html("$ "+payment);
	$("#paymentsemimontlyvalue").html("$ "+(payment / 2.0).toFixed(2));
	$("#paymentbiweeklyvalue").html("$ "+(payment / 2.0).toFixed(2));
	$("#paymentweeklyvalue").html("$ "+(payment / 4.0).toFixed(2));
	
	
	$("#amortizationmonthlyvalue").html(calculateAmortization((payment / 2.0), 2).toFixed(2) + " yrs");
	$("#amortizationsemimonthlyvalue").html(calculateAmortization((payment / 2.0), 2).toFixed(2) + " yrs");
	$("#amortizationbiweeklyvalue").html(calculateAmortization((payment / 4.0), 4).toFixed(2) + " yrs");
	$("#amortizationweeklyvalue").html(calculateAmortization((payment / 4.0), 4).toFixed(2) + " yrs");
	
	return false;
}

function calculateAmortization(payment, freq){
	switch (freq){
		case 2:
			var returnpay = payment * 2.0;
			break
		case 4:
			var returnpay = payment * 4.0;
			break
	}
	var wholecom = findAmortization(1,1,returnpay,freq)
	var solvecom = findAmortization(wholecom,.001,returnpay,freq)
	return solvecom;
}

function findAmortization(q,n,returnpay,freq){
	var listprice = $("#mort_listprice").val();
	var downpayment = ($("#mort_downpayment").val())? $("#mort_downpayment").val() : 0;
	var interestrate = $("#mort_interestrate").val()/100;
	var amortization = $("#mort_amortization").val();

	// caclulate our base mortgage
	var mortgage = listprice - downpayment;

	var compoundfactor = compound / 12;
	var yearlyrate = interestrate / compound;
	var rdefine = Math.pow((1.0 + yearlyrate),compoundfactor) - 1.0;
	var basepayment = 1000000000;
	
	switch (freq){
		case 2:
			var p = 12;
			break
		case 4:
			var p = 13;
			break
	}

	for(i = q; basepayment >= returnpay; i = i + n){
		var monTime = i * p;
		var comfact = Math.pow((1.0 + rdefine),monTime);
		var basepayment = (mortgage * rdefine * comfact) / (comfact - 1.0);
	}
	return (i - n - n);
}

function calculateAffordability(){
	var income = $("#affordability_income").val();
	var taxes = ($("#affordability_propertytaxes").val())? $("#affordability_propertytaxes").val() : 0;
	var heatingcost = ($("#affordability_heatingcost").val())? $("#affordability_heatingcost").val() * 12 : 0;
	var debt = ($("#affordability_debt").val())? $("#affordability_debt").val() * 12 : 0;
	var condofees = ($("#affordability_condofees").val())? $("#affordability_condofees").val() * 12 : 0;
	var interestrate = $("#affordability_interestrate").val() / 100;

	var compoundfactor = compound / 12;
	var monthlytime = 25 * 12;
	var yearlyrate = interestrate / 2;
	var rdefine = Math.pow((1.0 + yearlyrate),compoundfactor) - 1.0;
	var purchcompound = Math.pow((1.0 + rdefine),monthlytime);

	var maxgdsr = .32;
	var maxtdsr = .42;

	var gdspay = (maxgdsr * income) - taxes - heatingcost - condofees;
	var tdspay = (maxtdsr * income) - taxes - heatingcost - condofees - debt;
	var payment = (gdspay < tdspay) ? gdspay / 12 : tdspay / 12;
	var mortgage = (0 + ((payment * (purchcompound - 1.0)) / rdefine)) / purchcompound;

	$("#affordabilitypayment").html("$ "+(payment).toFixed(2));
	$("#affordabilitymaxmortgage").html("$ "+(mortgage).toFixed(2));
	return false;
}
function calculateLandTransferTax(){
	var landtransfertax = 0;
	var listprice = $("#ltt_listprice").val();
	switch($("#ltt_province").val()){
		case "Alberta":
			landtransfertax = ((0.001 * listprice) + 35);
		break;
		
		case "British Columbia":
			if(listprice <= 200000){
				landtransfertax = (0.01 * listprice);			
			}
			else{
				landtransfertax = ((0.02 * listprice) - 2000);
			}
		break;
		
		case "Manitoba":
			if(listprice <= 30000){
				landtransfertax = 0;
			}
			else if(listprice > 30000 && listprice <= 90000){
				landtransfertax = ((0.005 * listprice) - 110);
			}
			else if(listprice > 90000 && listprice <= 150000){
				landtransfertax = ((0.01 * listprice) - 560);
				
			}
			else{
				landtransfertax = ((0.015 * listprice) - 1350);
			}
		break;
		
		case "New Brunswick":
			landtransfertax = ((0.0025 * listprice) + 55);
		break;
		
		case "Newfoundland":				
			landtransfertax = ((0.004 * listprice) + 50);
		break;
		
		case "Northwest Territories":
			if(listprice <= 1000000){
				landtransfertax = ((0.0015 * listprice) + 40);
				
			}
			else{
				landtransfertax = (0.001 * listprice);
				
			}
		break;
		
		case "Nova Scotia, Halifax County":
			landtransfertax = (0.015 * listprice);
		break;
		
		case "Nova Scotia, not Halifax County":
			landtransfertax = (0.0005 * listprice);
		break;
		
		case "Ontario":
			if(listprice <= 55000){
				landtransfertax = (0.005 * listprice);
			}
			else if(listprice > 55000 && listprice <= 250000){
				landtransfertax = ((0.01 * listprice) - 275);
			}
			else if(listprice > 250000 && listprice <= 400000){
				landtransfertax = ((0.015 * listprice) - 1525);
			}
			else{
				landtransfertax = ((0.02 * listprice) - 3525);
			}
		break;										
		
		case "Prince Edward Island":
			if(listprice > 500){
				landtransfertax = (0.01 * listprice);
				
			}
			else{
				landtransfertax = 0;				
			}
		break;
		
		case "Quebec":
			if(listprice <= 50000){
				landtransfertax = (0.005 * listprice);
				
			}
			else if(listprice > 50000 && listprice <= 250000){
				landtransfertax = ((0.01 * listprice) - 250);
				
			}
			else{
				landtransfertax = ((0.015 * listprice) - 1500);
				
			}
		break;
		
		case "Saskatchewan":
			if(listprice <= 1000){
				landtransfertax = "0";
				
			}
			else{
				landtransfertax = ((0.0015 * listprice) + 15);
				
			}
		break;
		
		case "Yukon":
			if(listprice <= 10000){
				landtransfertax = (0.001 * listprice);
				
			} else if(listprice > 10000 && listprice <= 25000){
				landtransfertax = (0.005 * listprice);
				
			}
			else{
				landtransfertax = (0.0025 * listprice);
				
			}
		break;		
		default:
			landtransfertax = 0;
	}
	$("#landtransfertaxestimate").html("$ "+(landtransfertax).toFixed(2));
}