$(function(){
	var number_format = "#.00";

	$('#calculator-form input.text').numeric();

	//update the loan amount
	$('#purchase-price').format({format:number_format, locale:"us"});
	updateLoanAmount();
	
	$("#purchase-price").blur(function(){
		$(this).format({format:number_format, locale:"us"}); 
	});

	$('#deposit').change(function() {
		var price = getNum($('#purchase-price').val());
		var smartcredit_margin = getNum($('#smartcredit').val());
		
		//get the licensing cost
		var license_cost = getNum($('#calculator-form input:radio:checked').val());
		
		//get the total
		var total_price = price + smartcredit_margin + license_cost;
	
		//the deposit
		var deposit = getNum($('#deposit').val());
		var deposit_amount = (total_price * deposit);
		$('#deposit-text').val(deposit_amount);
		$('#deposit-text').format({format:number_format, locale:"us"});
		
		//update the loan amount
		updateLoanAmount();
	});
	
	$('#deposit-text').keyup(function() {
		//reset the deposit drop down
		$('#deposit').val('-');
		
		//update the amount
		updateLoanAmount();
	});

	$('#calculator-form input:radio').change(function() {
		updateLoanAmount();
	});

	$('#purchase-price').keyup(function() {
		updateLoanAmount();
	});
	
	$('#interest-rate').change(function() {
		updateLoanAmount();
	});
	
	$('#repayment-period').change(function() {
		updateLoanAmount();
	});
	
	$('#insurance').change(function() {
		updateLoanAmount();
	});
	
	$('#credit-life').change(function() {
		updateLoanAmount();
	});
	
	$('#shortfall').change(function() {
		updateLoanAmount();
	});

	function updateLoanAmount() {
		var price = getNum($('#purchase-price').val());
		var smartcredit_margin = getNum($('#smartcredit').val());
				
		//get the licensing cost
		var license_cost = getNum($('#calculator-form input:radio:checked').val());
		
		//get the total
		var total_price = price + smartcredit_margin + license_cost;
		
		//the deposit
		var deposit = $('#deposit').val();
		var deposit_amount = getNum($('#deposit-text').val());
		if (deposit != '-') { //if something was selected
			deposit_amount = (total_price * getNum(deposit));
			$('#deposit-text').val(deposit_amount);
			$('#deposit-text').format({format:number_format, locale:"us"});
		}	
				
		//the total loan amount
		var loan_amount = total_price - deposit_amount;
		$('#loan-amount').val(loan_amount);
		$('#loan-amount').format({format:number_format, locale:"us"});
		
		//get the repayment period
		var repayment_period = getNum($('#repayment-period').val());
		var interest_rate = getNum($('#interest-rate').val());
		
		//calculate the initiation amount
		var initiation_fee = 150 + ((loan_amount-1000) * 0.1);
		if (initiation_fee > 1000) { //capped at R1000
			initiation_fee = 1000;
		}
		$('#initiation').val(initiation_fee);
		$('#initiation').format({format:number_format, locale:"us"});
		
		//calculate VAT
		var vat_fee = (initiation_fee * 0.14) / repayment_period;
		$('#vat').val(vat_fee);
		$('#vat').format({format:number_format, locale:"us"});
		
		var collection_fee = getNum($('#collection').val());
		
		//calculate the basic installment
		var rate = interest_rate / 12;
		var nper = repayment_period;
		var pv = initiation_fee + loan_amount;
		var installment = (pv * rate) / (1 - Math.pow(1 + rate, -nper))
		var basic_installment = collection_fee + vat_fee + installment;
		$('#basic-installment').val(basic_installment);
		$('#basic-installment').format({format:number_format, locale:"us"});
		
		//insurance
		var insurance = 89;
		if (insurance < 89) {
			insurance = 89;
		}
		$('#insurance-text').val(insurance);
		$('#insurance-text').format({format:number_format, locale:"us"});

		//credit life
		var credit_life = (price / 1000) * 2.5;
		if (credit_life < 29.5) {
			credit_life = 29.5;
		}
		$('#credit-life-text').val(credit_life);
		$('#credit-life-text').format({format:number_format, locale:"us"});
		
		//shortfall
		var shortfall = (loan_amount * 0.0175) / repayment_period;
		if (shortfall < 28.5) {
			shortfall = 28.5;
		}
		$('#shortfall-text').val(shortfall);
		$('#shortfall-text').format({format:number_format, locale:"us"});
		
		//the total installment
		var total_installment = basic_installment;
		if ($('#insurance').is(':checked')) {
			total_installment += insurance;
		}
		if ($('#credit-life').is(':checked')) {
			total_installment += credit_life;
		}
		if ($('#shortfall').is(':checked')) {
			total_installment += shortfall;
		}
		$('#total-installment').val(total_installment);
		$('#total-installment').format({format:number_format, locale:"us"});
	}
});

function getNum(text) {
	//converts empty values to 0 and always returns a number
	if (text == '') {
		return 0;
	}
	else {
		return (text * 1);
	}
}