﻿function bereken_maximale_leenbedrag()
{
	// Variables
	var netto_maandinkomen = document.form1.netto_maandinkomen.value;
	var partner_netto_maandinkomen = document.form1.partner_netto_maandinkomen.value;	
	var woonlast = document.form1.woonlast.value;
	var samenwonend = getCheckedValue(document.form1.samenwonend);
	var kinderen = getCheckedValue(document.form1.kinderen);
	
	// Convert String to Integer
	netto_maandinkomen = parseInt(netto_maandinkomen);
	partner_netto_maandinkomen = parseInt(partner_netto_maandinkomen);	
	woonlast = parseInt(woonlast);
	
	// If 'aanvrager netto maandinkomen' is lower than ZERO		
	if ( 
			(netto_maandinkomen < 0) ||
			isNaN(netto_maandinkomen)
		)
	{
		netto_maandinkomen = 0;		
	}
		
	// If 'partner netto maandinkomen' is lower than ZERO		
	if ( 
			(partner_netto_maandinkomen < 0) ||
			isNaN(partner_netto_maandinkomen)
		)
	{
		partner_netto_maandinkomen = 0;		
	}
	
	// Minimum instellen
	if (samenwonend == 'JA')
	{
		//If maandinkomen aanvrager is lower than 173
		if (netto_maandinkomen < 173)
		{
			netto_maandinkomen = 173;
		}		
		
		//If maandinkomen partner is lower than 173
		if (partner_netto_maandinkomen < 173)
		{
			partner_netto_maandinkomen = 173;
		}		
	}
	
	// If 'woonlast' is lower than ZERO		
	if ( woonlast < 0 )
		woonlast = 0;	

	var gezamelijke_maandinkomen = netto_maandinkomen + partner_netto_maandinkomen;

	if (samenwonend == 'NEE')
	if (kinderen == 'NEE')	
	var totaal = (gezamelijke_maandinkomen - ( (gezamelijke_maandinkomen-906) * 0.15 ) - 703 - woonlast) * 50;
	
	if (samenwonend == 'NEE')
	if (kinderen == 'JA')	
	var totaal = (gezamelijke_maandinkomen - ( (gezamelijke_maandinkomen-1073) * 0.15 ) - 870 - woonlast) * 50;	
	
	if (samenwonend == 'JA')
	if (kinderen == 'NEE')	
	var totaal = (gezamelijke_maandinkomen - ( (gezamelijke_maandinkomen-1213) * 0.15 ) - 1010 - woonlast) * 50;	
	
	if (samenwonend == 'JA')
	if (kinderen == 'JA')	
	var totaal = (gezamelijke_maandinkomen - ( (gezamelijke_maandinkomen-1318) * 0.15 ) - 1115 - woonlast) * 50;		
	
	// If value is NaN
	if ( isNaN(totaal) )
		totaal = '0';
	
	// If value is lower than ZERO
	if ( totaal < 0 )
		totaal = '0';

	// If 'samenwonend' is not selected
	if ( samenwonend == '' )
		totaal = '0';
		
	// If 'kinderen' is not selected		
	if ( kinderen == '' )
		totaal = '0';
		
		
	totaal = convertMoney( parseInt(totaal) );
	document.form1.totaal.value = totaal;
		
	//document.form1.totaal.value = parseInt(totaal) + ',00';
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function convertMoney( amount )
{
	if ( amount == 'NaN' )
		return '0,00';
	
	amount = amount.toFixed(2);	
	amount = amount.replace('.', ',');

	if( amount.length == 7)
		amount = amount.substr(0,1)+'[.]'+amount.substr(1,3)+'[,]00';

	if( amount.length == 8)
		amount = amount.substr(0,2)+'[.]'+amount.substr(2,3)+'[,]00';
		
	else if( amount.length == 9) 
		amount = amount.substr(0,3)+'[.]'+amount.substr(2,3)+'[,]00';
	
	amount = amount.replace('[.]', '.');
	amount = amount.replace('[,]', ',');	

	return amount;
}


function calculate_maximum_value() {


	var incomplete = false;

	var income                = document.getElementById('income').value;
	var income_partner        = document.getElementById('income_partner').value;
	var financial_obligations = document.getElementById('financial_obligations').value;
   
	if (isNaN (income) || income < 10000) {
		document.getElementById('mortgage_maximum_calculation').value = '0,00';

		incomplete = true;

	}

	if (isNaN (income_partner) || income_partner < 0) {

		if (incomplete) {

			document.getElementById('mortgage_maximum_calculation').value = '0,00';

		} else {

			document.getElementById('mortgage_maximum_calculation').value = '0,00';

			incomplete = true;

		}

	}

	if (isNaN (financial_obligations) || financial_obligations < 0) {

		if (incomplete) {

			document.getElementById('mortgage_maximum_calculation').value = '0,00';

		} else {

			document.getElementById('mortgage_maximum_calculation').value = '0,00';

			incomplete = true;

		}

	}


	if (incomplete) {

		document.getElementById('mortgage_maximum_calculation').value = '0,00';

	} else {

		var amount_available = parseInt(income) + parseInt(income_partner) - parseInt(financial_obligations);

		var result = Math.round(amount_available * 5.5);
		
		if (isNaN (result)){
		document.getElementById('mortgage_maximum_calculation').value = '0,00';
		}else{
		document.getElementById('mortgage_maximum_calculation').value = result;
		}

	}

}

function calculate_mortgage_value() {

	var incomplete = false;

	var mortgage = document.getElementById('mortgage_value').value;
	var interest = document.getElementById('mortgage_interest').value;
	var duration = document.getElementById('mortgage_duration').value;
   

	if (isNaN (mortgage) || mortgage < 10000) {

		document.getElementById('mortgage_value_calculation').value = "0,00";

		incomplete = true;

	}

	if (isNaN (interest) || interest <= 0 || interest > 12) {

		if (incomplete) {

			document.getElementById('mortgage_value_calculation').value = "0,00";

		} else {

			document.getElementById('mortgage_value_calculation').value = "0,00";

			incomplete = true;

		}

	}

	if (isNaN (duration) || duration < 1 || duration > 45) {

		if (incomplete) {

			document.getElementById('mortgage_value_calculation').value = "0,00";

		} else {

			document.getElementById('mortgage_value_calculation').value = "0,00";

			incomplete = true;

		}

	}

	if (incomplete) {

		document.getElementById('mortgage_value_calculation').value = "0,00";

	} else {

		var i = document.getElementById('mortgage_interest').value;
		i = i / 100.0;

		m = Math.pow((1 + i / 2), (1 / 6));
		i = m - 1;
		n = duration * 12;
		
		var pow = 1;
		
		for (var j = 0; j < n; j++) {

			pow = pow * (1 + i);

		}

		var result = Math.round((document.getElementById('mortgage_value').value * pow * i) / (pow - 1));

		document.getElementById('mortgage_value_calculation').value = result;

	}

}
