// $Id$
var realcosts = {
	add_event_listner: function(obj, ev, listner) {
		if(document.all) {
			obj.attachEvent('on' + ev, listner);
		} else {
			obj.addEventListener(ev, listner, false);
		}
	},
	$: function(id) {
		return document.getElementById(id);
	},
	calculate: function() {
		
		var value = realcosts.$('edit-prijs').value;
		if(value == '') value = 0;
		var prijs = parseFloat(value);
		var ki = parseFloat(realcosts.$('edit-ki').value);
		var kinderen = parseFloat(realcosts.$('edit-kinderen').value);
		var procent = parseFloat(realcosts.$('realcosts-gb').value);
		var klein_bes = false;
		if(realcosts.$('edit-eerste').checked && ki < kinderen) {
			procent = parseFloat(realcosts.$('realcosts-kb').value);
			klein_bes = true;
		}
		
		var vp = prijs;
		var ak = prijs / 100 * procent;
		var nk = realcosts.notary(prijs);
		var an = (prijs < 4000 || klein_bes) ? parseFloat(realcosts.$('realcosts-akte-kb').value) : parseFloat(realcosts.$('realcosts-akte-gb').value);
		prijs += ak + nk + an;
		
		var ihtml = "<table width=\"100%\"><colgroup><col span=\"1\" /><col span=\"1\" style=\"text-align: right;\" /></colgroup><tr><td>Prijs</td><td><strong>&euro; " + realcosts.number_format(vp) + "</strong></td></tr><tr><td>Beschrijf</td><td><strong>&euro; " + 
		realcosts.number_format(ak) + 
		"</strong></td></tr><tr><td>Notaris</td><td><strong>&euro; " + 
		realcosts.number_format(nk) + 
		"</strong></td></tr><tr><td>Akte</td><td><strong>&euro; " + 
		realcosts.number_format(an) + 
		"</strong></td></tr><tr><td>Totaal<td><strong>&euro; " + 
		realcosts.number_format(prijs) + "</strong></td></tr></table>";
		
		
		realcosts.$('realcostsResult').innerHTML = ihtml;
		
	},
	number_format: function(number) {
		return number.toFixed(2).toString().replace('.', ',');
	},
	notary: function(prijs) {
		var table = [
			[0, 4.56], 
			[7500, 2.85], 
			[17500, 2.28], 
			[30000, 1.71], 
			[45495, 1.14], 
			[64095, 0.57], 
			[250095, 0.057]
		];
		var s = 0;
		for(var i = 0;i < table.length;i++) {
			if(table[i][0] < prijs) {
				var k = i + 1;
				var a = table[i][0];
				if(table[k]) {
					var b = table[k][0];
					var p = (prijs < b) ? prijs - a : b - a;
				} else {
					var p = prijs - a;
				}
				s += p / 100 * table[i][1];
			} else {
				break;
			}
		}
		return s;
	},
	info_show: function(id, e) {
		if(!e) var e = window.event;
		var o = realcosts.$(id);
		var offset = realcosts.get_offset(o);
		var coors = realcosts.get_coors(e);
		window.status = coors.x + ' - ' + coors.y;
		o.style.left = ((coors.x - offset.l) - 210) + 'px';
		o.style.top = ((coors.y - offset.t) + 10) + 'px';
		o.style.visibility = 'visible';
	},
	info_hide: function(id, e) {
		var o = realcosts.$(id);
		o.style.left = '0px';
		o.style.top = '0px';
		o.style.visibility = 'hidden';
	},
	get_offset: function(o) {
		var left = 0;
		var top = 0;
		if(o.offsetParent) {
			left = o.offsetLeft;
			top = o.offsetTop;
			while(o = o.offsetParent) {
				left += o.offsetLeft;
				top += o.offsetTop;
			}
		}
		return {l:left, t:top}
	},
	get_coors: function(e) {
		if(!e) var e = window.event;
		if(document.all) {
			return {x:e.clientX + document.body.parentNode.scrollLeft, y:e.clientY + document.body.parentNode.scrollTop}
		} else {
			return {x:e.pageX, y:e.pageY}
		}
	},
	initialize: function() {
		var c = false;
		if(realcosts.$('hidden_price')) {
			realcosts.$('edit-prijs').value = realcosts.$('hidden_price').innerHTML;
			realcosts.$('edit-ki').value = realcosts.$('hidden_ki').innerHTML;
			if(isNaN(parseInt(realcosts.$('hidden_ki').innerHTML))) {
				realcosts.$('edit-ki').value = realcosts.$('hidden_ki_text').innerHTML;
				realcosts.$('edit-kinderen').disabled = true;
				realcosts.$('edit-eerste').disabled = true;
			}
			c = true;
		}
		var inputs = ['edit-prijs', 'edit-ki', 'edit-eerste', 'edit-kinderen'];
		for(var item in inputs) {
			realcosts.add_event_listner(realcosts.$(inputs[item]), 'change', realcosts.calculate);
		}
		if(c) {
			realcosts.calculate();
		}
	}
}
function add_event(o, ev, listner) {
	if(document.all) {
		// ie only
		return o.attachEvent('on' + ev, listner);
	} else {
		// other browsers
		return o.addEventListener(ev, listner, false);
	}
}