
var HypotheekBerekening = Class.create();

HypotheekBerekening.prototype = {

	
	initialize: function( form , message  ){
		
		this.form = $(form);
		this.message = $(message); 
		
		this.maxhyp  = 0;
		this.maxhyp2 = 0;
		this.maxgar  = 0;
		this.maxwoon = 0;
		
		this.properties =  {
			paraf: 5144,
			toetsrente: 0,
			inkomen1: 0,
			inkomen2: 0,
			leeftijd: 0
		};
		
	},
	berekenHypotheek: function() {
		
		this.properties.toetsrente = parseFloat($F('toetsrente'));
		this.properties.inkomen1 = parseInt($F('inkomen1'));
		
		if($F('inkomen2'))
			this.properties.inkomen2 = parseInt($F('inkomen2'));
		
		this.properties.leeftijd = parseInt($F('leeftijd'));
		
		var tr = this.properties.toetsrente / 100;
		var totaalInkomen = this.properties.inkomen1 + this.properties.inkomen2;
		
		if( this.properties.inkomen1 > this.properties.inkomen2 )
			iv = this.properties.inkomen1 / this.properties.inkomen2;
		else
			iv = this.properties.inkomen2 / this.properties.inkomen1;
		
		if( totaalInkomen < 15000 )
			this.maxhyp = Math.round((0.25 * totaalInkomen * 0.834) / tr);
		else if( totaalInkomen < 70000 )
			this.maxhyp = Math.round(((0.25 + 0.0000007 * (totaalInkomen - 15000)) * totaalInkomen * 0.834) / tr);
		else if(totaalInkomen < 420000)
			this.maxhyp = Math.round(((0.288 + (1/30000000) * (totaalInkomen - 70000)) * totaalInkomen * 0.834) / tr);
		else 
			this.maxhyp = Math.round((0.3 * (totaalInkomen * 0.834)) / tr);
			
		if(totaalInkomen < 27500) {
			this.maxhyp2 = totaalInkomen + ((iv - 1) * 216666);
			if( this.maxhyp2 > (totaalInkomen * 3.66) ) 
				this.maxhyp2 = totaalInkomen * 3.66;
		} else if(totaalInkomen < 35000) {
			this.maxhyp2 = (1.5 * totaalInkomen) + (iv - 1) * 216666;
			if (this.maxhyp2 > (totaalInkomen * 3.66))
				this.maxhyp2 = totaalInkomen * 3.66;
		} else if (totaalInkomen < 45000 ) {
			this.maxhyp2 = (2.125 * totaalInkomen) + (iv - 1) * 21666;
			if (this.maxhyp2 > (totaalInkomen * 3.75))
				this.maxhyp2 = totaalInkomen * 3.75;
		} else if (totaalInkomen < 55000 ) {
			this.maxhyp2 = (3 * totaalInkomen) + (iv - 1) * 13333;
			if (this.maxhyp2 > (totaalInkomen * 3.8)) 
				this.maxhyp2 = totaalInkomen * 3.8;
		} else if (totaalInkomen < 70000 ) {
			this.maxhyp2 = (3.67 * totaalInkomen) + (iv - 1) * 5000;
			if (this.maxhyp2 > (totaalInkomen * 3.917)) 
				this.maxhyp2 = totaalInkomen * 3.917;			
		}
		
		if (this.properties.inkomen1 != 0 && this.properties.inkomen2 != 0) {
		 	if (totaalInkomen >= 70000) {
		 		this.maxhyp2 = totaalInkomen * 4;
		 		if(iv >= 2 && iv < 3) 
		 			this.maxhyp2 = this.maxhyp2 - 10000;
		 		if (iv < 2) 
		 			this.maxhyp2 = this.maxhyp2 - 20000;
		 	}
		 	if (this.maxhyp2 < this.maxhyp)
		 		this.maxhyp = this.maxhyp2;
		 }
		 this.maxhyp = 2000 * Math.round(this.maxhyp / 2000);
		 this.maxgar = this.maxhyp - this.properties.paraf;
		
		 if (this.maxhyp < 315000)
		 	this.maxgar = this.maxhyp;
		 else 
		 	this.maxgar = 315000;
		 	

        if (totaalInkomen < 15000) {
			this.maxwoon = 0.25 * totaalInkomen - this.properties.paraf * 0.25;
		}else if (totaalInkomen < 70000 ) {
			this.maxwoon = (0.25 + 0.0000007 * (totaalInkomen-15000)) * (totaalInkomen - (this.properties.paraf * 0.25));
		}else if (totaalInkomen < 420000 ) {
			this.maxwoon = (0.288 + (0.0000000333 * (totaalInkomen - 70000))) * (totaalInkomen - (this.properties.paraf * 0.288));
		} else {
			this.maxwoon=(0.3 * totaalInkomen * 0.3);
		}
		this.maxwoon = Math.round(this.maxwoon / 12);
		return true;
		 
	},
	toonResultaten: function(formElement) {
		
		this.berekenHypotheek();
		
		var message1 = "";
		
		if (this.properties.leeftijd > 40) 
			message1 += "<p>Deze berekening is gebaseerd op een spaarhypotheek met een looptijd van 30 jaar.<br />Er is een kans dat een dergelijke hypotheek u niet verstrekt wordt gezien uw leeftijd.</p>"
		
		message1 += "<p>Indicatie maximale hypotheek (afgerond):<strong> &euro;" + this.maxhyp + ",-</strong></p>";
		message1 += "<p>Indicatie maximale toegestane bruto woonlasten (per maand):<strong> &euro;" + this.maxwoon + ",-</strong></p>";
		message1 += "<p>Maximale hypotheek garantie:<strong> &euro;" + this.maxgar + ",-</strong></p>";
		
		this.message.update(message1);
	}
	
	
	
}
