///////////////////////////////////////////////////////////////////////
//
//	javascript functies tbv de postcode controle met en zonder Ajax
//
///////////////////////////////////////////////////////////////////////

function checkPostcode(obj1, obj2, target) {
    var strPostcode;
	if (($F(obj1).length < 4)&&($F(obj2).length > 1)) { //
		//alert('Dit is geen volledige postcode.');
		target.value = '';
		return false;
	} else {
		if ($F(obj1).length > 4) { //single field of buitenlands
			target.value = $F(obj1);
			return true;
		} else { //2 velden invoer nl postcode
			strPostcode = $F(obj1);
			if (strPostcode.length == 4) { //skip naar volgend input veld					
				try{obj2.focus();}catch(e){};
			}
			strPostcode = strPostcode + $F(obj2);
			if (strPostcode.length == 6) {
				obj2.value = $F(obj2).toUpperCase();
				strPostcode = strPostcode.toUpperCase();
				if (isPostcode(strPostcode)) {
					target.value = strPostcode;				
					return true;
				} else {
					$('spanAdresToon').innerHTML = strPostcode+' is geen correcte postcode.';
					target.value = '';
					return false;
				}	
			}
		}
	}
	target.value = '';
	return false;
}

var myPostcodeHandler;
var myTarget = $('spanAdresToon');
var myPostcode;
var myHuisnummer;
var myToevoeging;
var myStraat;
var myPlaats;
var myLabels;

myPostcodeHandler = {
	onCreate: function(){
		myTarget.style.display = 'block';
		myTarget.style.height = '40px';
		myTarget.innerHTML = '<img src="../../img/loading.gif" width="12" height="12" alt="Laden" />';
	},
	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			//myTarget.innerHTML = '';
		}
	}
};
Ajax.Responders.register(myPostcodeHandler);

function checkPostcodeAjax(Postcode, Huisnummer, Toevoeging, target, Labels) {
	myTarget = $(target);
	myPostcode = $F(Postcode);
	myHuisnummer = $F(Huisnummer);
	myToevoeging = $F(Toevoeging);
	myLabels = Labels;
	if (myPostcode.length == 0) {
	    try {
			myPostcode = document.contentForm("txtPostcode1").value + document.contentForm("txtPostcode2").value;
			document.contentForm("txtPostcode").value = myPostcode;
	    } catch(e) {}
	    try{
			myPostcode = document.contentForm("txtPostcode_mede1").value + document.contentForm("txtPostcode_mede2").value;
			document.contentForm("txtPostcode_mede").value = myPostcode;
	    } catch(e) {}
	}
	if ((myPostcode.length == 6)&&((myHuisnummer.length > 0))) { //postcode & huisnummer gevuld		
		var url = '../../Services/PostcodeCheck.asp';
		var pars = 'postcode=' + myPostcode + '&huisnummer=' + myHuisnummer + '&toevoeging=' + myToevoeging;		
		var localAjax = new Ajax.Request(
			url,
			{
				method: 'get', 
				parameters: pars, 
				onComplete: showResponseAdres
			});
	}
}

function clearPostadres(objForm) {
	objForm.txtPostnummer.value = '';
	objForm.txtPostnummer2.value = '';
	objForm.txtPostpostcode1.value = '';
	objForm.txtPostpostcode2.value = '';
	objForm.txtPoststraat.value = '';
	objForm.txtPostplaats.value = '';
}

function showResponseAdres(XML_Http_Request){
	var xmldoc = XML_Http_Request.responseXML;
	var parentNode = xmldoc.getElementsByTagName("body")[0]
	var myError = xmldoc.documentElement.getElementsByTagName('error')[0];	
    var error = '';
    if(myError.hasChildNodes()) {//error is gevuld
		error = myError.childNodes[0].nodeValue; 
    }
    if(error==''){
	    var Adres 		= xmldoc.getElementsByTagName("adres")[0];
	    myStraat 		= Adres.getElementsByTagName("straat")[0];
        myWoonplaats 	= Adres.getElementsByTagName("woonplaats")[0];
	    myStraat 		= myStraat.childNodes[0].nodeValue;
	    myWoonplaats 	= myWoonplaats.childNodes[0].nodeValue;
	    toonAdres(myPostcode, myHuisnummer, myToevoeging, myStraat, myWoonplaats, myTarget, false);
		highLightLabels(myLabels, 'none');
	    try {
		    document.contentForm.txtPlaats.value = myWoonplaats;
		    document.contentForm.txtStraat.value = myStraat;
	    } catch(e) {}
	    try{
		    document.contentForm.txtPlaats_mede.value = myWoonplaats;
		    document.contentForm.txtStraat_mede.value = myStraat;
	    } catch(e) {}
    } else {
	    myTarget.innerHTML = '';
	    highLightLabels(myLabels, 'errorLabel');
	    myTarget.innerHTML = error;
	    try {
		    document.contentForm.txtPostcode.value = '';
	    } catch(e) {}
	    try{
		    document.contentForm.txtPostcode_mede.value = '';
	    } catch(e) {}
    }    
}

function toonAdres(Postcode, Huisnummer, Toevoeging, Straat, Woonplaats, target, getFromForm) {    
	if (getFromForm) {
		myPostcode 		= $F(Postcode);
		myHuisnummer 	= $F(Huisnummer);
		myToevoeging 	= $F(Toevoeging);
		myStraat 		= $F(Straat);
		myWoonplaats 	= $F(Woonplaats);
		myTarget = $(target);
	} else {
		myPostcode 		= Postcode;
		myHuisnummer 	= Huisnummer;
		myToevoeging 	= Toevoeging;
		myStraat 		= Straat;
		myWoonplaats 	= Woonplaats;
		myTarget = target;
	}
	myTarget.style.display = 'block';
	myTarget.style.height = '40px';
	myTarget.innerHTML = '' + myStraat +' '+ myHuisnummer +' '+ myToevoeging +'<br />'+ myPostcode +' '+ myWoonplaats;    
}

