PostCodeTimer = null;
Check_Postcode = null;
Check_Huisnummer = null;
Check_Straat = null;
Check_Plaats = null;
Check_Target = null;

function ValidatePostCode( PostCode,  Huisnummer , Straat , Plaats , Land , Target )
{
	if ( PostCode == null || Huisnummer == null || Straat == null || Plaats == null || Land == null )
		return false;
	
	PostCode.onchange = Huisnummer.onchange = function( )
	{
		if ( PostCodeTimer != null ) { clearTimeout( PostCodeTimer ); }
		
		if ( Land.value != 'Nederland' ) return false;
		
		var allfilled = 1;
		var objs = new Array( PostCode , Huisnummer );
		for ( var j=0; j<2; j++ )
		{
			var sobj = objs[ j ];
			if ( sobj.value == '' ) allfilled = 0;
		}
		
		if ( allfilled == 1 )
		{
			Straat.value = '';
			Plaats.value = '';
			if ( Target != null )
			{
				//alert( Target );
				Target.innerHTML = 'Adres gegevens worden opgezocht.';
			}
			
			Check_Postcode = PostCode;
			Check_Huisnummer = Huisnummer;
			Check_Straat = Straat;
			Check_Plaats = Plaats;
			Check_Land = Land;
			Check_Target = Target;
			
			PostCodeTimer = setTimeout( 'CheckPC( );' , 750 );
		} else
		{
			if ( Target != null )
			{
				Target.innerHTML = 'Postcode en/of nummer niet correct ingevuld';
			}
		}
	}
}

function CheckPC( )
{
	var pars = '?postcode=' + Check_Postcode.value + '&huisnummer=' + Check_Huisnummer.value;
		
	new Ajax.Request( '/postcode_new.php' , { method: 'get' , parameters: pars ,
	onComplete: function( txt )
	{
		//tst = txt.responseText;
		tst = eval("("+txt.responseText+")");
		Check_Straat.value = tst[ 0 ];
		Check_Plaats.value = tst[ 1 ];
		Check_Postcode.value = tst[ 2 ] + ' ' + tst[ 3 ];
		Check_Target.innerHTML = '';
		//alert( tst );
	},
	onFailure: function( )
	{
		Check_Target.innerHTML = 'Adres niet gevonden!';
	}
	} );
	//alert( Check_Postcode.value );
}
