function addCommas(nStr){
	nStr += '';
	var x = nStr.split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

(function($){
	$.fn.commifyValue = function(value){
		if(value === undefined) {
			return this.each(function(){
				var $this = $(this), sValue = $this.val();
				sValue = sValue.replace(/,/g, "");
				$this.val(addCommas(sValue));
			});
		} else {
			return this.each(function(){
				$(this).val(addCommas(value));
			});
		}
	};
	
	$.fn.serializeObject = function(){
		var o = {}, a = this.serializeArray();
		$.each(a, function() {
			 if (o[this.name]) {
				  if (!o[this.name].push) {
						o[this.name] = [o[this.name]];
				  }
				  o[this.name].push(this.value || '');
			 } else {
				  o[this.name] = this.value || '';
			 }
		});
		return o;
	};
})(jQuery);

(function setDefaultMonthlyPayment(){
	var q = $('#mortgage-cal').serializeObject();
	var price = parseInt(q['property-price']);
	if(q['loan-val'] === '' && q['payment-val'] === ''){
		$('#loan-val').commifyValue(0.8 * price);
		$('#payment-val').commifyValue(0.2 * price);
	}
})();

function monthly(sC, R, sN) {
	/*
		C = Loan Amount
		R = Interest Rate
		N = Number of Months
	*/
	sC = sC.replace(/,/g, "");
	var C = parseInt(sC),
	r = parseFloat(R)/1200,
	N = parseInt(sN) * 12;
	return C * r / (1 - (1 / Math.pow(1+r,  N)));
}

function bindMortgageCalBehaviors(){
	var price = parseInt(document.forms['mortgage-cal'].elements['property-price'].value);
	
	$('#show-mortgage-calculator').click(function(){
		$('#mortgage_calculator').removeClass('hide');
		return false;
	});
	
	$('#hide-mortgage-calculator').click(function(){
		$('#mortgage_calculator').addClass('hide');
		return false;
	});
	
	$('#loan-val').change(function(){
		var loan = $(this).val();
		loan = loan.replace(/,/g, "");
		var payment = price - parseInt(loan),
				fPercent = (payment / price) * 100;
		$('#payment-val').commifyValue(payment);
		$(this).commifyValue();
		
		$('#payment-percent').val(fPercent.toFixed(2));
	}).commifyValue();
	
	$('#payment-val').change(function(){
		var sPayment = $(this).val();
		sPayment = sPayment.replace(/,/g, "");
		var payment =  parseInt(sPayment),
				fPercent = (payment / price) * 100;
		$('#loan-val').commifyValue(price - payment);
		$(this).commifyValue();
		
		$('#payment-percent').val(fPercent.toFixed(2));
	}).commifyValue();
	
	$('#mortgage-cal').submit(function(){
		var q = $(this).serializeObject();
		var fMonthly = monthly(q['loan-val'], q['rate-val'], q['years-val']);
		fMonthly = fMonthly.toFixed(2);
		$('#monthly-val').html('$'+addCommas(fMonthly));
		return false;
	}).submit();
	
	$('#unit-val').change(function(){
		$('#payment-val, #payment-percent').toggleClass('hide');
	}).val("p");
	
	$('#payment-percent').change(function(){
		var percent = parseInt($(this).val()),
				loan = price * ((100 - percent) / 100);
		$('#loan-val').commifyValue(loan);
		$('#payment-val').commifyValue(price - loan);
	});
}
jQuery(bindMortgageCalBehaviors);