﻿$(document).ready(function() {
	var PrevResult = "";
	calculate();
	
	attachFormatter($("#tbLoanAmount"));
	attachFormatter($("#tbDownPayment"));
	
	$("#rbCanada").click(calculate);
	$("#rbUS").click(calculate);
	$("#tbLoanAmount").keyup(calculate);
	$("#tbDownPayment").keyup(calculate);
	$("#tbInterestRate").keyup(calculate);
	$("#tbAmortization").keyup(calculate);
	
	
	$("#tbLoanAmount").change();

	
	$("#ddlDownPayment").change(function() {
		var percent = parseFloat($("#ddlDownPayment option:selected").val());
		var amount = parseNumber($("#tbLoanAmount").val());
		
		$("#tbDownPayment").val(currencyFormated(percent*amount));
		calculate();
	});
	
	function attachFormatter(el)
	{
		el.change(function() {
			el.val(currencyFormated(parseNumber(el.val())));
		});
	}

	function calculate()
	{
		var mc = new MortgageCalc();
		
		if ($("#rbCanada").attr("checked") != undefined)
			mc.Canadian = $("#rbCanada").attr("checked");
		mc.List = parseNumber($("#tbLoanAmount").val());
		if ($("#tbDownPayment").val() != "")
			mc.DownPayment = parseNumber($("#tbDownPayment").val());
		mc.SetInterestRate(parseNumber($("#tbInterestRate").val()));
		mc.PeriodYears = parseInt($("#tbAmortization").val());
		
		mc.ComputePayments();
		
		var result = mc.PaymentMonthly;
		
		if (result == undefined || isNaN(result) || result == Infinity)
			result = "";
		
		if (PrevResult != result)
			$("#lblPaymentMonthly").hide("fast");
		PrevResult = result;	
				
		$("#lblPaymentMonthly").text(currencyFormated(result)).show("fast"); 
	}
});


/// <summary>
/// Used for mortgage calculations
/// </summary>
function MortgageCalc() {
	/// <summary>
	/// Input properties
	/// have to be initialized before Compute functions are executed
	/// </summary>	
	this.Canadian = false;
	this.List = 0;
	this.DownPayment = 0;
	this.GetLoan = function()
	{
		return this.List - this.DownPayment;
	}
	this.PeriodYears = 0;
	this.InterestRate = 0;
	this.SetInterestRate = function(value)
	{
		this.InterestRate = value / 100; // set percent value
	}
	
	/// <summary>
	/// Result of the calculations will be in the following properties
	/// </summary>
	this.PaymentMonthly = 0;
	this.PaymentBiWeekly = 0;
	this.PaymentWeekly = 0;
	this.PaymentAccelBiWeekly = 0;
	this.PaymentAccelWeekly = 0;
	
	
	/// <summary>
	/// Calcupates the payment amount based on payments frequency
	/// </summary>
	/// <param name="PeriodsPerYear">Number of payments per year</param>
	/// <returns></returns>
	this.ComputePayment = function(PeriodsPerYear)
	{
		var PeriodsToPay = this.PeriodYears * PeriodsPerYear;
		var Rate = 0;

		if (this.Canadian) //diferent interest calculation for canadian mortgages
			Rate = Math.pow((1 + this.InterestRate / 2), (2 / PeriodsPerYear)) - 1;
		else
			Rate = this.InterestRate / PeriodsPerYear;

		//basic mortgage formula below
		return (this.GetLoan() * Rate) / (1 - Math.pow((1 + Rate), - PeriodsToPay));
	}
	
	/// <summary>
	/// Calculates five diferent payment options
	/// </summary>
	this.ComputePayments = function()
	{
		this.PaymentMonthly = this.ComputePayment(12);
		this.PaymentBiWeekly = this.ComputePayment(26);
		this.PaymentWeekly = this.ComputePayment(52);
		this.PaymentAccelBiWeekly = this.PaymentMonthly / 2;
		this.PaymentAccelWeekly = this.PaymentMonthly / 4;
	}
}

