// deze is anders.
var postcodeCheck = function(){
	this.validePostcode		= false,
	this.postcodeCache 		= {postcode:'', nummer:''},
	this.postcodeInput		= '',
	this.huisnummerInput	= '',
	this.straatSpan			= '',
	this.plaatsSpan			= '',
	this.ajax				= '',
	
	this.init = function(formId, pInput, hInput, straatDisplay, plaatsDisplay) {
		this.postcodeInput = pInput;
		this.huisnummerInput = hInput;
		this.straatSpan = straatDisplay;
		this.plaatsSpan = plaatsDisplay;
		 
		//document.observe('dom:loaded', this.checkPostcode.bindAsEventListener(this));
		Event.observe(pInput, 'blur', this.checkPostcode.bindAsEventListener(this));
		Event.observe(hInput, 'blur', this.checkPostcode.bindAsEventListener(this));
	},
	
	this.checkPostcode = function() {
		if(this.postcodeCache['postcode'] == $F(this.postcodeInput) && this.postcodeCache['nummer'] == $F(this.huisnummerInput))
			return;
		this.postcodeCache['postcode'] = $F(this.postcodeInput);
		this.postcodeCache['nummer'] = $F(this.huisnummerInput);
		
		_this = this;
		this.ajax = new Ajax.Request('http://www.allesoverhypotheken.nl/ajax.php?type=postcodeCheck&p='+escape($F(this.postcodeInput))+'&n='+escape($F(this.huisnummerInput)), {
		method: 'get',
		onSuccess: function(transport) {
				var json = transport.responseText.evalJSON(true);
				if(json['succes'] == true)	{
					if($(_this.postcodeInput))	$(_this.postcodeInput).value	= json['data']['postcode'].toUpperCase();
					if($(_this.straatSpan))		$(_this.straatSpan).innerHTML	= json['data']['straat'].capitalize() + ' ' + json['data']['nummer'].capitalize();
					if($(_this.plaatsSpan))		$(_this.plaatsSpan).innerHTML	= json['data']['postcode'].toUpperCase() + ' ' + json['data']['plaats'].capitalize();
					_this.validePostcode = true;
				}else{
					if($(_this.plaatsSpan))		$(_this.plaatsSpan).innerHTML = '';
					if($(_this.straatSpan))		$(_this.straatSpan).innerHTML = json['message'];
					_this.validePostcode = false;
				}
			}
		});
	}
};
