function bereken_maximale_leenbedrag()
{
	// Variables
	var netto_maandinkomen = document.form2.netto_maandinkomen.value;
	var partner_netto_maandinkomen = document.form2.partner_netto_maandinkomen.value;	
	var woonlast = document.form2.woonlast.value;
	var samenwonend = getCheckedValue(document.form2.samenwonend);
	var kinderen = getCheckedValue(document.form2.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.form2.totaal.value = totaal;
		
	//document.form2.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;
}