
	//<![CDATA[
	// Input variables
	var netMonthylIncome;
	var netMonthylIncomePartner;
	var netMonthlyMortgage;		
	var isMarried;
	var eigenWoning;
	var hasChild;

	//Intermediate result variables
	var maxLoan = 0;
	var monthlyCosts = 0;
	var period = 0;
	var monthlyInterest = 0;
	var yearlyInterest = 0;
	var totalPrice = 0;

	function start()
    {
		init();
		maxLoan = calcMaxLoan();
		monthlyCosts = calcMonthlyCosts();
		monthlyInterest = calcMonthlyInterest();
		yearlyInterest = calcYearlyInterest();
		period = calcPeriod();
		totalPrice = calcTotalPrice();
	
		// Output parameters
		if (Math.round(maxLoan) > 50000) {
			maxLoan = 50000;
			monthlyCosts = 1000;
			period = 57;
			yearlyInterest = 0.057;
			totalPrice = 57000;
		} 
     	document.getElementById('block-midden').style.display = 'none';
     	document.getElementById('block-links').style.display = 'none';
		document.getElementById('block-rechts').style.display = 'none';
		document.getElementById('block-breed').style.display = 'none';
		document.getElementById('header-rechts').style.display = 'none';

		document.getElementById('block2').style.display = 'block';
		
		document.getElementById('maxloan').innerHTML = '&euro; ' +  Math.round(maxLoan);
		document.getElementById('maandlasten').innerHTML = '&euro; ' +  Math.round(monthlyCosts);
		document.getElementById('looptijd').innerHTML = '' +   Math.round(period) + ' maanden';
		document.getElementById('rente').innerHTML = '' +  (yearlyInterest * 100) + '%';
		document.getElementById('rente2').innerHTML = '' +  (yearlyInterest * 100) + '%';
		document.getElementById('totaalp').innerHTML = '&euro; ' +  Math.round(totalPrice);

		document.calculation.maxbedrag.value= Math.round(maxLoan);

		//document.getElementById('offerteLink').href=  document.getElementById('resulturl').value + '/overige-leningen.html?max=' + Math.round(maxLoan);
		//alert(maxLoan+"\n"+monthlyCosts+"\n"+period+"\n"+yearlyInterest+"\n"+totalPrice);
	}

	function init()
	{
		netMonthylIncome =parseInt(document.getElementById('inp1').value);
		if (isNaN(netMonthylIncome)) {netMonthylIncome=0;}
		netMonthylIncomePartner = parseInt(document.getElementById('inp2').value);
		if (isNaN(netMonthylIncomePartner)) {netMonthylIncomePartner=0;}
		netMonthlyMortgage = parseInt(document.getElementById('inp3').value);
		if (isNaN(netMonthlyMortgage)) {netMonthlyMortgage=0;}
		isMarried = document.getElementById('radio10').checked;
		eigenWoning = document.getElementById('radio14').checked;
		hasChild = document.getElementById('radio12').checked;
		
		/*
		netMonthylIncome = 5000;
		netMonthylIncomePartner = 5000;
		netMonthlyMortgage = 5000;
		isMarried = true;
		eigenWoning = true;
		hasChild = true;
		*/
	}

	function calcMaxLoan()
	{
		var basisnorm = 0;
		var basishuur = 209;
		var normbedrag = 0;
		var berekendNormbedrag = 0;

		if (!isMarried) {netMonthylIncomePartner= 0;}
		
		//basis normbedrag
		if (!isMarried && !hasChild) {basisnorm = 723;}
		else if (!isMarried && hasChild) {basisnorm = 904;}
		else if (isMarried && !hasChild) {basisnorm = 1090;}
		else if (isMarried && hasChild) {basisnorm = 1178;}
		
		//woonlasten
		if (eigenWoning) {
			if (netMonthlyMortgage==0) {netMonthlyMortgage=1;}
			var woonlasten = (netMonthlyMortgage - (netMonthlyMortgage/3));
		} else {
			var woonlasten = netMonthlyMortgage; }
		if (woonlasten < 350) {woonlasten = 350;}
		
		//berekend normbedrag
		berekendNormbedrag = 0.15 * (netMonthylIncome + netMonthylIncomePartner - basishuur - basisnorm) + basisnorm;
		
		//max kosten levensonderhoud
		var maxLevensoh = 0;
		if (!isMarried && !hasChild) {maxLevensoh = 1014;}
		else if (!isMarried && hasChild) {maxLevensoh = 1144;}
		else if (isMarried && !hasChild) {maxLevensoh = 1326;}
		else if (isMarried && hasChild) {maxLevensoh = 1377;}
		
		//normbedrag
		var tmpNormbedrag = basisnorm;
		if (berekendNormbedrag > basisnorm) {tmpNormbedrag = berekendNormbedrag;}
		normbedrag = tmpNormbedrag
		if (maxLevensoh < normbedrag) {normbedrag = maxLevensoh;}
				
		return (netMonthylIncome + netMonthylIncomePartner - woonlasten - normbedrag) / 0.02;
	}

	function calcMonthlyCosts()
	{
		return maxLoan * 0.02;
	}

	function calcMonthlyInterest()
	{
		var rate = 0;

		if (maxLoan >= 12000) {rate = 0.00466;}
		else if (maxLoan >= 9000) {rate = 0.0053;}
		else if (maxLoan >= 6000) {rate = 0.00561;}
		else if (maxLoan >= 3000) {rate = 0.00577;}
		else
		{
			// Not possible to get a loan
			maxLoan = 0;
			monthlyCosts = 0;
			rate = 0;
		}
		return rate;
	}

	function calcYearlyInterest()
	{
		var rate = 0;

		if (maxLoan >= 12000) {rate = 0.057;}
		else if (maxLoan >= 9000) {rate = 0.065;}
		else if (maxLoan >= 6000) {rate = 0.069;}
		else if (maxLoan >= 3000) {rate = 0.071;}
		else
		{
			// Not possible to get a loan
			maxLoan = 0;
			monthlyCosts = 0;
			rate = 0;
		}
		return rate;
	}

	function calcPeriod()
	{
		if (maxLoan !=0)
		{
			//	nper = ln(pmt/(pmt - rate * pv))/ln(1+rate) where nper is the number of payment periods, pmt is the amount of the payment, rate is the interest rate for the payment period and pv is the value of the loan. Source: http://www.ehow.com/how_2154015_calculate-term-investment-using-microsoft.html

			return Math.log(monthlyCosts / (monthlyCosts - monthlyInterest * maxLoan)) / Math.log(1 + monthlyInterest);
		}
		else
		{
			return 0;
		}
	}

	function calcTotalPrice()
	{
		return monthlyCosts * period;
	}
//]]>


			$(function() {
				$("#slider").slider({
					range: "min",
					value: 2000,
					min: 0,
					max: 4000,
					slide: function(event, ui) {
						$("#inp1").val( ui.value);
					}
				});
				$("#inp1").val($("#slider").slider("value"));
			});

			$(function() {
				$("#slider1").slider({
					range: "min",
					value: 2000,
					min: 0,
					max: 4000,
					slide: function(event, ui) {
						$("#inp2").val( ui.value);
					}
				});
				$("#inp2").val($("#slider1").slider("value"));
			});
			
			$(function() {
				$("#slider2").slider({
					range: "min",
					value: 500,
					min: 205,
					max: 2000,
					slide: function(event, ui) {
						$("#inp3").val( ui.value);
					}
				});
				$("#inp3").val($("#slider2").slider("value"));
			});

			function setSlider(objId,inpVal) {
				$("#" + objId).slider('value',inpVal);
			}

			function calc(linkObj) {
				inp1 = document.getElementById("inp1").value;	
				inp2 = document.getElementById("inp2").value;	
				inp3 = document.getElementById("inp3").value;	
				gehuwd_samenwonend = document.gehuwd_samenwonend.value;
				inwonende_kinderen = document.inwonende_kinderen.value;
			}

