function BerekenRuimte() {
	/* CONSTANTEN */
	var Drempel =  11345;
	var Max_Premiegrondslag = 155827; 
	var Max_P = 167172;
	var Basisruimte = 0;
	var Max_Jaarruimte = 26491;
	var Max_Reservering = 6703;
	var Max_Reservering_dubbel = 13238;
	/* VARIABELEN VASTSTELLEN */
	var ToonResultaat = 1;
	var Leeftijd = parseInt(document.theForm.Leeftijd.value);
	var InkomenLoondienst = parseInt(document.theForm.InkomenLoondienst.value);
	var PensioengevendInkomen = parseInt(document.theForm.PensioengevendInkomen.value);
	var OverigeInkomsten = parseInt(document.theForm.OverigeInkomsten.value);
	var NieuwwaardeAuto = parseInt(document.theForm.NieuwwaardeAuto.value);
	var Privekilometers = parseInt(document.theForm.Privekilometers.value);
  var CO2UitstootAuto = parseInt(document.theForm.CO2UitstootAuto.value);
	/* wijziging 8/7/2002 */
	var BetaaldeVergoedingPrive = parseInt(document.theForm.BetaaldeVergoedingPrive.value);
	/* einde wijziging 8/7/2002 */
	var OpbouwPensioen = parseInt(document.theForm.OpbouwPensioen.value);
	var FactorA = parseInt(document.theForm.FactorA.value);
	var Franchise = parseInt(document.theForm.Franchise.value);
	var BeschikbarePremie = parseInt(document.theForm.BeschikbarePremie.value);
	var FORDotatie = parseInt(document.theForm.FORDotatie.value);
	var Opbouwpercentage = document.theForm.Opbouwpercentage.value;
	if (isNaN(Opbouwpercentage)) {
		Opbouwpercentage = parseFloat(Opbouwpercentage.replace(",","."));
	}
  
	/* CHECK OP MINIMALE GEGEVENS */
	if (isNaN(InkomenLoondienst) || isNaN(Leeftijd) || OpbouwPensioen == 0) {
		ToonResultaat = 0;
		window.alert("Om een berekening te kunnen uitvoeren dient u ten minste uw leeftijd en inkomen in\nte voeren alsmede de berekeningsbasis van de opbouw van uw pensioen te kiezen");
   return false;
	}
	if ((theForm.NieuwwaardeAuto.value != "") && (theForm.Privekilometers.value == 0)){
		window.alert("U bent vergeten aan te geven hoeveel privé-kilometers u met de auto van de zaak rijdt");
   theForm.Privekilometers.focus();
   return false;
	}
  if ((theForm.Privekilometers.value == 2) && ((theForm.CO2UitstootAuto.value == 0) || (theForm.CO2UitstootAuto.value == 'n'))){
		window.alert("U bent vergeten aan te geven hoeveel CO2 uw auto van de zaak uitstoot");
   theForm.CO2UitstootAuto.focus();
   return false;
	}
	if (OpbouwPensioen == 1 && isNaN(FactorA) && ToonResultaat == 1) {
		ToonResultaat = 0;
		window.alert("De berekening kan niet worden voltooid omdat u niets heeft ingevuld bij Factor A");
   theForm.FactorA.focus();
	}
	if (OpbouwPensioen == 2 && ToonResultaat == 1) {
		if (isNaN(Franchise)) {
			window.alert("De berekening kan niet worden voltooid omdat u niets heeft ingevuld bij Franchise");
   theForm.Franchise.focus();
			ToonResultaat = 0;
		}
	}
	if (OpbouwPensioen == 3 && isNaN(BeschikbarePremie) && ToonResultaat == 1) {
		window.alert("De berekening kan niet worden voltooid omdat u niets hebt ingevuld bij Beschikbare premie");
   theForm.BeschikbarePremie.focus();
		ToonResultaat = 0;
	}
	/* INKOMEN BEPALEN */
	var InkomensGrondslag = 0;
	if (isNaN(OverigeInkomsten)) { OverigeInkomsten = 0; }
	if (!isNaN(InkomenLoondienst)) {
		InkomensGrondslag = InkomenLoondienst + OverigeInkomsten;
		if (isNaN(PensioengevendInkomen)) {
			PensioengevendInkomen = InkomenLoondienst;
			document.theForm.PensioengevendInkomen.value = PensioengevendInkomen;
		}
	} else {
		ToonResultaat = 0;
	}
	if (isNaN(InkomenLoondienst) && isNaN(PensioengevendInkomen)) {
		ToonResultaat = 0;
	}
	/* BIJTELLING AUTO VAN DE ZAAK BASIS */
	switch (Privekilometers) {
		case 1 :
			Bijtelling = 0;
			break
		case 2 :
			Bijtelling = 0.14;
			break
	}
	/* BIJTELLING AUTO VAN DE ZAAK CO2 */
	if (Privekilometers == 2) {
  	switch (CO2UitstootAuto) {
  		case 1 :
  			Bijtelling = 0.14;
  			break
  		case 2 :
  			Bijtelling = 0.20;
  			break
  		case 3 :
  			Bijtelling = 0.25;
  			break
  	};
	}
		
	if (!isNaN(NieuwwaardeAuto)) {
	/* wijziging 8/7/2002 */
		if (isNaN(BetaaldeVergoedingPrive)) {
			BetaaldeVergoedingPrive = 0;
		}
		InkomensGrondslag = InkomensGrondslag + ((Bijtelling * NieuwwaardeAuto) - BetaaldeVergoedingPrive);
	/* einde wijziging 8/7/2002 */
	}
	/* CONTROLEER OP MAXIMALE INKOMGENSGRONDSLAG */
	if (InkomensGrondslag > Max_P) {
		InkomensGrondslag = Max_P;
	}
	/* JAARRUIMTE BEPALEN */
	var Jaarruimte = 0;
	if (InkomensGrondslag > (Max_Premiegrondslag + Drempel)) {
		InkomensGrondslag = Max_Premiegrondslag + Drempel;
	}
	Jaarruimte = 0.17 * (InkomensGrondslag - Drempel);
	var OpbouwSxD = 0;
	var OpbouwBP = 0;
	var OpbouwAftrek = 0;
	var Factor = 0;
	if (isNaN(Leeftijd)) { ToonResultaat = 0; }
  
	if (OpbouwPensioen == 1) {
		/* BEREKEN AAN DE HAND VAN FACTOR A */
		if (isNaN(FactorA)) {
			OpbouwAftrek = 0;
		} else {
			OpbouwAftrek = 7.5 * FactorA;
		}
	}
	if (OpbouwPensioen == 2) {
		/* BEREKEN AAN DE HAND VAN SALARIS/DIENSTTIJD */
		if (isNaN(Franchise)) {
			OpbouwAftrek = 0;
		} else {
			if (isNaN(Opbouwpercentage)) { Opbouwpercentage = 0; }
			Opbouwpercentage = Opbouwpercentage / 100;
			OpbouwSxD = (PensioengevendInkomen - Franchise) * Opbouwpercentage;
			OpbouwAftrek = 7.5 * OpbouwSxD;
		}
	}
	if (OpbouwPensioen == 3) {
		/* BEREKEN AAN DE HAND VAN BESCHIKBARE PREMIE */
		if (!isNaN(Leeftijd)) {
			if ((Leeftijd < 15) || (Leeftijd > 65)) { Factor = 0; }
			if ((Leeftijd >= 15) && (Leeftijd <= 19)) { Factor = 0.36; }
			if ((Leeftijd >= 20) && (Leeftijd <= 24)) { Factor = 0.30; }
			if ((Leeftijd >= 25) && (Leeftijd <= 29)) { Factor = 0.25; }
			if ((Leeftijd >= 30) && (Leeftijd <= 34)) { Factor = 0.21; }
			if ((Leeftijd >= 35) && (Leeftijd <= 39)) { Factor = 0.17; }
			if ((Leeftijd >= 40) && (Leeftijd <= 44)) { Factor = 0.14; }
			if ((Leeftijd >= 45) && (Leeftijd <= 49)) { Factor = 0.12; }
			if ((Leeftijd >= 50) && (Leeftijd <= 54)) { Factor = 0.10; }
			if ((Leeftijd >= 55) && (Leeftijd <= 59)) { Factor = 0.08; }
			if ((Leeftijd >= 60) && (Leeftijd <= 65)) { Factor = 0.07; }
			if (isNaN(BeschikbarePremie)) { BeschikbarePremie = 0; }
			OpbouwBP = BeschikbarePremie * Factor;
			OpbouwAftrek = 7.5 * OpbouwBP;
		}
	}
	if (isNaN(FORDotatie)) { FORDotatie = 0; }
	Jaarruimte = Jaarruimte - OpbouwAftrek - FORDotatie;
  
	if (Jaarruimte < 0) { Jaarruimte = 0; }
	if (Jaarruimte < Basisruimte) {
		Jaarruimte = Basisruimte;
	}
	if (Jaarruimte > Max_Jaarruimte) {
		Jaarruimte = Max_Jaarruimte;
	}
	/* RESULTAAT TONEN */
	if (ToonResultaat == 1) {
		document.theForm.IndicatieJaarruimte.value = parseInt(Jaarruimte);
	} else {
		document.theForm.IndicatieJaarruimte.value = "niet mogelijk";
	}
}
function CheckLeeftijd(leeftijd) {
	if (leeftijd >= 65) {
		window.alert("Alleen belastingplichtigen, die bij aanvang van het kalenderjaar nog géén 65 jaar\nzijn, mogen gebruik maken van de jaarruimte en/of de reserveringsruimte.");
	}
}
function CheckFactorA() {
	var opbouwpensioen = parseInt(document.theForm.OpbouwPensioen.value);
	if ((opbouwpensioen == 1) && (document.theForm.FactorA.value <= 0)) {
		window.alert("Let op: u moet bij Factor A een waarde invullen!");
		return false;
	} else {
		return true;
	}
}
function CheckFranchise() {
	var opbouwpensioen = parseInt(document.theForm.OpbouwPensioen.value);
	if ((opbouwpensioen == 2) && (document.theForm.Franchise.value <= 0)) {
		window.alert("Let op: u moet bij Franchise een waarde invullen!");
		return false;
	} else {
		return true;
	}
}
function CheckOpbouwpercentage() {
	var opbouwpensioen = parseInt(document.theForm.OpbouwPensioen.value);
	if ((opbouwpensioen == 2) && (document.theForm.Opbouwpercentage.value <= 0)) {
		window.alert("Let op: u moet bij Opbouwpercentage een waarde invullen!");
		return false;
	} else {
		return true;
	}
}
function CheckBeschikbarepremie() {
	var opbouwpensioen = parseInt(document.theForm.OpbouwPensioen.value);
	if ((opbouwpensioen == 3) && (document.theForm.BeschikbarePremie.value <= 0)) {
		window.alert("Let op: u moet bij Beschikbare Premie een waarde invullen!");
		return false;
	} else {
		return true;
	}
}
function SelecteerOpbouw() {
	if (document.theForm.OpbouwPensioen.value == 0) {
		document.theForm.FactorA.style.backgroundColor='';
		document.theForm.BeschikbarePremie.style.backgroundColor='';
		document.theForm.Franchise.style.backgroundColor='';
		document.theForm.Opbouwpercentage.style.backgroundColor='';
		document.theForm.FactorA.value='';
		document.theForm.BeschikbarePremie.value='';
		document.theForm.Franchise.value='';
		document.theForm.Opbouwpercentage.value='';
	}
	if (document.theForm.OpbouwPensioen.value == 1) {
		document.theForm.FactorA.style.backgroundColor='';
		document.theForm.BeschikbarePremie.style.backgroundColor='silver';
		document.theForm.Franchise.style.backgroundColor='silver';
		document.theForm.Opbouwpercentage.style.backgroundColor='silver';
		document.theForm.FactorA.value='';
		document.theForm.BeschikbarePremie.value='niet invullen';
		document.theForm.Franchise.value='niet invullen';
		document.theForm.Opbouwpercentage.value='niet invullen';
	}
	if (document.theForm.OpbouwPensioen.value == 2) {
		document.theForm.FactorA.style.backgroundColor='silver';
		document.theForm.BeschikbarePremie.style.backgroundColor='silver';
		document.theForm.Franchise.style.backgroundColor='';
		document.theForm.Opbouwpercentage.style.backgroundColor='';
		document.theForm.FactorA.value='niet invullen';
		document.theForm.BeschikbarePremie.value='niet invullen';
		document.theForm.Franchise.value='';
		document.theForm.Opbouwpercentage.value='1,75';
	}
	if (document.theForm.OpbouwPensioen.value == 3) {
		document.theForm.FactorA.style.backgroundColor='silver';
		document.theForm.BeschikbarePremie.style.backgroundColor='';
		document.theForm.Franchise.style.backgroundColor='silver';
		document.theForm.Opbouwpercentage.style.backgroundColor='silver';
		document.theForm.FactorA.value='niet invullen';
		document.theForm.BeschikbarePremie.value='';
		document.theForm.Franchise.value='niet invullen';
		document.theForm.Opbouwpercentage.value='niet invullen';
	}
	if (document.theForm.OpbouwPensioen.value == 4) {
		document.theForm.FactorA.style.backgroundColor='silver';
		document.theForm.BeschikbarePremie.style.backgroundColor='silver';
		document.theForm.Franchise.style.backgroundColor='silver';
		document.theForm.Opbouwpercentage.style.backgroundColor='silver';
		document.theForm.FactorA.value='niet invullen';
		document.theForm.BeschikbarePremie.value='niet invullen';
		document.theForm.Franchise.value='niet invullen';
		document.theForm.Opbouwpercentage.value='niet invullen';
	}
}
function SelecteerVelden1()  {
	if (document.theForm.Privekilometers.value == 1) {
		document.theForm.CO2UitstootAuto.style.backgroundColor='silver';
		document.theForm.CO2UitstootAuto.value='n';
	}
	if ((document.theForm.Privekilometers.value == 0) || (document.theForm.Privekilometers.value == 2)) {
		document.theForm.CO2UitstootAuto.style.backgroundColor='';
		document.theForm.CO2UitstootAuto.value='0';
	}
}
