$(function() {

	function calculateGo() {
		$.getJSON("../../functions/form-financecalc.php", { vehicleprice: $("#vehicleprice").val(), partex: $("#partex").val(), deposit: $("#deposit").val(), term: $("#period").val() }, function(data){
			if (data.error=='') {
				$("#monthlypayment").text(data.monthlypayment);
				$("#weeklypayment").text(data.weeklypayment);
				$("#cashprice").text(data.vehicleprice);
				$("#totaldeposit").text(data.totaldeductions);
				$("#balancetofinance").text(data.amountofloan);
				$("#apr").text(data.apr + "%");
				$("#periodofloan").text(data.term);
				$("#totalamountpayable").text(data.totalamountpayableincdeposit);
				$("#creditfee").text(data.creditfacilityfee);
			} else {
				alert(data.error);
			}
		});
	}
	
	jQuery.fn.gofinancecalc = function () {
		calculateGo();
	}

	$("#form-calculate-submit").click(function() {
		calculateGo();
		return false;
	});
	
	$("#form-quote-submit").click(function() {
		if ($("#monthlypayment").text()=='') {
			alert('Please generate a finance quote first, by using the boxes on the left.');
			return false;
		} else {
			var cookiestring = $("#cashprice").text() + "|" + $("#totaldeposit").text() + "|" + $("#balancetofinance").text() + "|" + $("#apr").text() + "|" + $("#periodofloan").text() + "|" + $("#totalamountpayable").text() + "|" + $("#monthlypayment").text();
			cookiestring = cookiestring.replace("%", "");
			$.cookie("calculator", cookiestring);
			return true;
		}	
	});
	
});


