var mortgageCalc = (function(){
	
	var isCan = true; //default to canadian mortgage formula (semi-annual compounding)
	var interestRate = 5.0;
	var principle = 350000;
	var paymentsPerMonth = 1;
	var amortization = 25;
	var loanTerm = 5;
	var theForm;
	
	function init(){
		
		theForm = document.getElementById('mortgage-calc');
		
		$(theForm.dpSelect).on('change', function(e){
			if(!$.isNumeric(theForm.price.value)){
				return;
			}
			theForm.dp.value = theForm.dpSelect[theForm.dpSelect.selectedIndex].value * theForm.price.value;
			calculateMortgage();
		});
		
		$("#mortgage-calc input").on('keyup', function(e){
			calculateMortgage();
		});
		
		$(theForm.dpSelect).change(); //trigger calculation

	}
	
	function calculateMortgage(){
		
		if(!$.isNumeric(theForm.price.value) ||
				!$.isNumeric(theForm.dp.value) ||
				!$.isNumeric(theForm.rate.value) ||
				!$.isNumeric(theForm.amort.value) ||
				!$.isNumeric(theForm.term.value)
		){
			return;
		}
		
		if (parseInt(theForm.amort.value) > parseInt(theForm.amort.max)) {
			theForm.amort.value = theForm.amort.max;
		}

		principle = theForm.price.value - theForm.dp.value;
		interestRate = theForm.rate.value;
		amortization = theForm.amort.value;
		loanTerm = theForm.term.value;
		
		$('#mortMonthly').html(accounting.formatMoney(getMonthlyPayment(),"$",2));
		$('#mortCost').html(accounting.formatMoney(getTotalCost(),"$",2));
		$('#mortInt').html(accounting.formatMoney(getTotalInterestPaid(),"$",2));
		$('#mortPrin').html(accounting.formatMoney(getTotalPrinciplePaid(),"$",2));
	}
	
	function getInterestPerMonth(){
		
		var interestPerMonth = 0.0;
		
		if (!isCan) {
			interestPerMonth = interestRate / 1200;
		}else{
			var a = (1 + (interestRate / 200));
			var b = (2.0 / (paymentsPerMonth * 12.0));
			interestPerMonth = Math.pow(a, b) - 1;
		}

		return interestPerMonth;
	}
	
	function getMonthlyPayment(){
		
		var monthlyPayment = (principle * getInterestPerMonth())
		/ (1 - Math.pow(((1 + getInterestPerMonth())) , (0.0 - getNumPayments())));
		return monthlyPayment;
	}
	
	function getTotalInterestPaid(){
		
		var paymentNumber = 0;
		var pmnt = getMonthlyPayment();
		var interest = 0;
		var balance = principle;
		var numPaymentsGenerated = loanTerm * paymentsPerMonth * 12;
		var totalInterestPaid = 0;
		var prin = 0.0;
		
		for (i = 1; i <= numPaymentsGenerated; i++) {
			paymentNumber = paymentNumber + 1;
			if (pmnt > (balance * (1 + getInterestPerMonth()))) {
				pmnt = (balance * (1 + getInterestPerMonth()));
			}
			interest = balance * getInterestPerMonth();
			totalInterestPaid = totalInterestPaid + interest;
			prin = pmnt - interest;
			balance = balance - prin;
		}
		
		return totalInterestPaid;
	}
	
	function getTotalPrinciplePaid(){
		
		return getTotalCost() - getTotalInterestPaid();
	}
	
	function getTotalCost(){
		
		return getMonthlyPayment() * loanTerm * paymentsPerMonth * 12;
	}
	
	function getNumPayments(){
		
		return amortization * paymentsPerMonth * 12;
	}
	
	var appObject = {
		
		init: init
	};

	return appObject;
	
})();