
/* initiate on load */

$(document).ready(function(){ 
	
	
	/* 
	 * General
	 * 
	 ***********************************************************/
	
	
	// add last child to main nav
	$("#nav-main li:last-child").addClass("last-child");
	
	
	
	/* 
	 * Calculators
	 * 
	 ***********************************************************/
	
	// global variables
	
	var multiple_type = "single";
	var recalculate_borrowing = false;
	var recalculate_monthly = false;
	var recalculate_moving = false;
	
	// income values
	
	var c1_basic;
	var c2_basic;
	var c1_other;
	var c2_other;
	var c1_regular;
	var c2_regular;
	var c1_payments;
	var c2_payments;
	
	
	/* CALCULATE BORROWING */
	
	$("button#calculate-borrowing").click(function() {
	
		// find income values
	
		c1_basic = $("#c1-basic").val() ? $("#c1-basic").val() : 0;
		c2_basic = $("#c2-basic").val() ? $("#c2-basic").val() : 0;
		c1_other = $("#c1-other").val() ? $("#c1-other").val() : 0;
		c2_other = $("#c2-other").val() ? $("#c2-other").val() : 0;
		c1_regular = $("#c1-regular").val() ? $("#c1-regular").val() : 0;
		c2_regular = $("#c2-regular").val() ? $("#c2-regular").val() : 0;
		c1_payments = $("#c1-payments").val() ? $("#c1-payments").val() : 0;
		c2_payments = $("#c2-payments").val() ? $("#c2-payments").val() : 0;
		
		var deposit = $("#deposit").val();
		var multiple;
		
		// validate income for customer 1
		
		if(c1_basic == 0) {
			
			alert("Please enter a 'Basic Salary' for customer 1.")
			
			$("#c1-basic").focus();
			
			return false;
		}
		
		// if second income is present make it a joint application
		
		if($("#c2-basic").val() != "" && $("#c2-basic").val() > 0) {
			multiple_type = "joint";
		}
				
		// find multiple value
		
		if(multiple_type == "single") {
			
			if(deposit == 0) {
				multiple = 4;
			} else if(deposit == 5) {
				multiple = 4.5;
			} else if(deposit == 10) {
				multiple = 4.75;
			}
			
			borrowing_total = (parseInt(c1_basic)+parseInt(c1_other)+parseInt(c1_regular))-parseInt(c1_payments);
			
			borrowing_total = borrowing_total * multiple;
			
		}
		
		if(multiple_type == "joint") {
			
			if(deposit == 0) {
				multiple = 3.75;
			} else if(deposit == 5) {
				multiple = 4.25;
			} else if(deposit == 10) {
				multiple = 4.60;
			}
			
			borrowing_total = ((parseInt(c1_basic)+parseInt(c1_other)+parseInt(c1_regular))-parseInt(c1_payments) + (parseInt(c2_basic)+parseInt(c2_other)+parseInt(c2_regular))-parseInt(c2_payments));
			
			borrowing_total = borrowing_total * multiple;
			
			borrowing_total = parseInt(borrowing_total);
			
		}
		
		// set borrowing total
		
		$("#borrowing_total").val(borrowing_total);
		
		// show results
		
		$(this).hide();
		
		$(".borrowing-result, #monthly-cost, .recalculate-borrowing a").show();
		$(".borrowing-result > span").empty().append("&pound;"+borrowing_total);
		$(".monthly-result em.note span").empty().append("&pound;"+borrowing_total);
		
		if (!recalculate_borrowing) {
			$.scrollTo($(".borrowing-result"),{speed:1000, offset:-20});
		}
		
		return false;
	
	});
	
	// add reclaculate onclick
	
	$("p.recalculate-borrowing").css("display", "inline").click(function() {
		
		recalculate_borrowing = true;
		
		$("button#calculate-borrowing").click();
		
		return false;
		
	});
	
	
	
	/* CALCULATE MONTHLY COST */
	
	$("button#calculate-monthly").click(function() {
		
		// get values
		
		var term = $("#mortgage_term").val();
		var rate = $("#interest_rate").val();
		purchase_price = parseInt(borrowing_total + (borrowing_total * ($("#interest_rate").val()/100)));
		
		rate = "multiple_"+rate.replace(".","_");
		
		monthly_cost_total = parseInt((borrowing_total/1000) * window[rate][term]);
		
		$("#monthly_cost_total").val(monthly_cost_total);
		
		// show results
				
		$(this).hide();
		
		$(".monthly-result, #moving-cost, .recalculate-monthly a").show();
		
		$(".monthly-result > span").empty().append("&pound;" + monthly_cost_total);
		
		$("input#purchase_price").val(purchase_price);
		
		if (!recalculate_monthly) {
			$.scrollTo($(".monthly-result"),{speed:1000, offset:-20});
		}
		return false;
	
	});
	
	// add reclaculate onclick
	
	$("p.recalculate-monthly").css("display", "inline").click(function() {
		
		recalculate_monthly = true;
		
		$("button#calculate-monthly").click();
		
		return false;
		
	});
	
	
	
	/* CALCULATE MOVING COST */
	
	$("button#calculate-moving").click(function() {
	
		// get values
		
		var purchase_price = $("#purchase_price").val();
		var valuation_homebyuers = $("#valuation_homebuyers").val();
		var solicitors_fee = $("#solicitors_fee").val();
		var stamp_duty = 0;
		var stamp_duty_rate = 0;
		
		// work out stamp duty
		
		if(purchase_price >= 125001 && purchase_price < 250000) {
			stamp_duty = (purchase_price * 0.01);
			stamp_duty_rate = 1;
		} else if(purchase_price >= 250001 && purchase_price <= 500000) {
			stamp_duty = (purchase_price * 0.03);
			stamp_duty_rate = 3;
		} else if(purchase_price >= 500001) {
			stamp_duty = (purchase_price * 0.04);
			stamp_duty_rate = 4;
		}
		
		stamp_duty = parseInt(stamp_duty);
		
		$("#stamp-label span").text("(at " + stamp_duty_rate + "%)");
		
		moving_cost_total = parseInt(valuation_homebyuers) +  parseInt(solicitors_fee) + parseInt(stamp_duty);
		
		// show results
		
		$("#moving_cost_total").val(moving_cost_total);
		$("#stamp_duty").val(stamp_duty);
		
		$(this).hide();
		
		$(".moving-result, .recalculate-moving a").show();
		$("#moving-costs-extras").show();
		
		$(".moving-result > span").empty().append("&pound;" + moving_cost_total);
				
		return false;
	
	});
	
	// add reclaculate onclick
	
	$("p.recalculate-moving").css("display", "inline").click(function() {
		
		recalculate_moving = true;
		
		$("button#calculate-moving").click();
					
		return false;
		
	});

	
	// override submit event
	
	$("#calculator").submit(function() {
		return false;
	});
	
	// check customer 2 income
	
	/*
	setInterval(function() {
		
		if($("#c2-basic").val() != "") {
		
			$("dl#c2-borrowing dd input.input-text").removeAttr("disabled").removeClass("input-disabled");
		
		} else {
		
			$("dl#c2-borrowing dd input.input-text").attr("disabled", "disabled").addClass("input-disabled");;
			$("dl#c2-borrowing dd input.input-text:eq(0)").removeAttr("disabled").removeClass("input-disabled");;
		
		}
	
	}, 1000);
	*/
});