var paraf = 5144;

var toetsrente, inkomen1, inkomen2, leeftijd, maxhyp, maxgar, maxwoon;

function berekenHypotheek() {
        var tr = toetsrente / 100;
        var totaalInkomen = 0;
        maxgar = 0; maxwoon = 0;
        totaalInkomen = inkomen1 + inkomen2;
        if (inkomen1 > inkomen2) {
		iv = inkomen1 / inkomen2;
	} else {
		iv = inkomen2 / inkomen1;
	}
        if (totaalInkomen < 12000) {
		return false;
	}
	maxhyp = 0;
        if (totaalInkomen < 7000 && maxhyp == 0) {
		maxhyp = Math.round((0.369 * totaalInkomen * 0.834) / tr);
	}
	if (totaalInkomen < 32000 && maxhyp == 0) {
		maxhyp = Math.round(((0.343 + 0.0000007 * (totaalInkomen - 7000)) * totaalInkomen * 0.834) / tr);
	}
	if (totaalInkomen < 200000 && maxhyp == 0) {
		maxhyp = Math.round(((0.349 + (1/30000000) * (totaalInkomen - 32000)) * totaalInkomen * 0.834) / tr);
	}
	if (maxhyp == 0) {
		maxhyp = Math.round((0.39 * (totaalInkomen * 0.834)) / tr);
	}
	maxhyp2 = 0;
       	if (totaalInkomen < 12500 && maxhyp2 == 0) {
		maxhyp2 = totaalInkomen + ((iv - 1) * 216666);
		if (maxhyp2 > (totaalInkomen * 3.6)) {
			maxhyp2 = totaalInkomen * 3.6;
		}
	}
	if (totaalInkomen < 16000 && maxhyp2 == 0) {
		maxhyp2 = (1.5 * totaalInkomen) + (iv - 1) * 216666;
		if (maxhyp2 > (totaalInkomen * 3.66)) {
			maxhyp2 = totaalInkomen * 3.66;
		}
	}
	if (totaalInkomen < 20000 && maxhyp2 == 0) {
		maxhyp2 = (2.125 * totaalInkomen) + (iv - 1) * 21666;
		if (maxhyp2 > (totaalInkomen * 3.75)) {
			maxhyp2 = totaalInkomen * 3.75;
		}
	}
	if (totaalInkomen < 25000 && maxhyp2 == 0) {
		maxhyp2 = (3 * totaalInkomen) + (iv - 1) * 13333;
		if (maxhyp2 > (totaalInkomen * 3.8)) {
			maxhyp2 = totaalInkomen * 3.8;
		}
	}
	if (totaalInkomen < 32000 && maxhyp2 == 0) {
		maxhyp2 = (3.67 * totaalInkomen) + (iv - 1) * 5000;
		if (maxhyp2 > (totaalInkomen * 3.917)) {
			maxhyp2 = totaalInkomen * 3.917;
		}
	}
        if (inkomen1 != 0 && inkomen2 != 0) {
                if (totaalInkomen >= 32000) {
			maxhyp2 = totaalInkomen * 4.3;
                        if (iv >= 2 && iv < 3) {
				maxhyp2 = maxhyp2 - 4500;
			}
                        if (iv < 2) {
				maxhyp2 = maxhyp2 - 9000;
			}
                }
                if (maxhyp2 < maxhyp) {
			maxhyp = maxhyp2;
		}
        }
        maxhyp = 2000 * Math.round(maxhyp / 2000);
        maxgar = maxhyp - paraf;
        if (maxhyp < 200000) {
		maxgar = maxhyp;
	} else {
		maxgar = 200000;
	}
	maxwoon = 0
        if (totaalInkomen < 6800) {
		maxwoon = 0.25 * totaalInkomen - paraf * 0.25;
	}
	if (totaalInkomen < 32000 && maxwoon == 0) {
		maxwoon = (0.25 + 0.0000007 * (totaalInkomen-6800)) * (totaalInkomen - (paraf * 0.25));
	}
	if (totaalInkomen < 200000 && maxwoon == 0) {
		maxwoon = (0.288 + (0.0000000333 * (totaalInkomen - 32000))) * (totaalInkomen - (paraf * 0.288));
	}
	if (maxwoon == 0) {
		maxwoon=(0.3 * totaalInkomen * 0.3);
	}
        maxwoon = Math.round(maxwoon / 12);
	return true;
}
function toonResultaten(f) {
	toetsrente = parseFloat("0" + f.toetsrente.value);
	inkomen1 = parseInt("0" + f.inkomen1.value, 10);
	inkomen2 = parseInt("0" + f.inkomen2.value, 10);
	leeftijd = parseInt("0" + f.leeftijd.value, 10);
	f.maxgar.value = (f.maxwoon.value = (f.maxhyp.value = "......"));
	if (inkomen1 < 0 || inkomen2 < 0 || leeftijd < 0) {
		alert("U heeft een ongeldige waarde ingevuld!");
		return false;
	}
	if (!berekenHypotheek()) {
		alert("Gezien de hoogte van uw inkomen kan niet worden bepaald wat een goede indicatie van uw maximale hypotheeksom is.\n" +
                		"Neemt u hiervoor telefonisch contact op met ons kantoor of gebruik het contactformulier.");
		return false;
	}
	if (leeftijd > 40) {
		alert("Deze berekening is gebaseerd op een spaarhypotheek met een looptijd van 30 jaar.\nEr is een kans dat een dergelijke hypotheek u niet verstrekt wordt gezien uw leeftijd.");
	}
	if (maxhyp > 500000) {
		alert("Op basis van uw inkomen schatten wij uw maximale leencapaciteit boven EUR 500.000,-.\nEen betere indicatie geven is niet mogelijk.");
		return false;
	}
	f.maxhyp.value = maxhyp;
	f.maxwoon.value = maxwoon;
	f.maxgar.value = maxgar;
}
