function GetXmlHttpObject() {  
  var xmlHTTP=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHTTP=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHTTP=new ActiveXObject("Msxml2.XMLHTTP");
	}
    catch (e) {
      xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP");
	  
     }
   }
  return xmlHTTP;
}

function getXMLData(wat) {
	
	xmlHttp=GetXmlHttpObject()
	xmlHttp2 = GetXmlHttpObject()
	if (xmlHttp==null) {
  		alert ("Uw browser ondersteunt geen AJAX!");
  		return;
  	} 
	var str;
	var url="ajx/ajx_zoekschool.asp";
	switch (wat) 
	{
		case 'pc':  		
  		str = document.inschrijfform.postcode.value;	
		url=url+"?pc="+str;
		xmlHttp.onreadystatechange=pcChanged;
		break;
	
		case 'school':		
  		str = document.inschrijfform.dropdownlist_scholen.options[document.inschrijfform.dropdownlist_scholen.selectedIndex].value;
		url=url+"?accountid="+str;
		xmlHttp.onreadystatechange=schoolChanged;
		break;
		
		case 'contact':
		str = document.inschrijfform.dropdownlist_contacts.options[document.inschrijfform.dropdownlist_contacts.selectedIndex].value;
		url = "ajx/ajx_zoekcontacts.asp?ContactId="+str;
		xmlHttp.onreadystatechange=contactChanged;
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function pcChanged() { 
	if (xmlHttp.readyState==4) {
		var i;
		var displayschoolnaam;
		
		//dropdownlijst eerst leegmaken
		for (i=document.inschrijfform.dropdownlist_scholen.length-1; i>0; i--) {
			document.inschrijfform.dropdownlist_scholen.remove(i);
		}		
		
		//check if browser = mozilla-achtig
		var moz = (typeof document.implementation != 'undefined') && (typeof
		document.implementation.createDocument != 'undefined');		
		
		if (moz) {
			
			var xmlDoc = xmlHttp.responseXML.getElementsByTagName("scholen");
			var myschools = xmlDoc[0].getElementsByTagName("School");
			
			if (myschools.length > 0) {
				var aantal = myschools.length;
				var j;
				document.inschrijfform.dropdownlist_scholen.options[0] = new Option("Kies uw school...");
												
				for (i=0; i<(aantal); i++) {
						j = i + 1;
						displayschoolnaam = myschools[i].childNodes[0].firstChild.nodeValue + " - " + myschools[i].childNodes[1].firstChild.nodeValue;
						document.inschrijfform.dropdownlist_scholen.options[j] = new Option(displayschoolnaam, myschools[i].childNodes[8].firstChild.nodeValue);
						document.inschrijfform.dropdownlist_scholen.disabled=false;					
				}
				
					//indien geen gegevens, schrijf pro forma dropdownlijst
			} else {
				document.inschrijfform.dropdownlist_scholen.options[0] = new Option("Geen scholen met deze postcode gevonden...");
				document.inschrijfform.dropdownlist_scholen.disabled=true;
			}
							
		} else {				
			var xmlDoc=xmlHttp.responseXML.documentElement;			
				
			//en vervolgens opnieuw vullen
			if (xmlDoc.hasChildNodes() == true) {
				var aantal = xmlDoc.childNodes.length;
				var j;
				document.inschrijfform.dropdownlist_scholen.options[0] = new Option("Kies uw school...");
				for (i=0; i<aantal; i++) {
					j = i + 1;
					displayschoolnaam = xmlDoc.childNodes[i].firstChild.text + " - " + xmlDoc.childNodes[i].getElementsByTagName("Adreslijn1")[0].firstChild.text;
					document.inschrijfform.dropdownlist_scholen.options[j] = new Option(displayschoolnaam, xmlDoc.childNodes[i].getElementsByTagName("AccountId")[0].firstChild.text);
					document.inschrijfform.dropdownlist_scholen.disabled=false;
				}
				//indien geen gegevens, schrijf pro forma dropdownlijst
			} else {
				document.inschrijfform.dropdownlist_scholen.options[0] = new Option("Geen scholen met deze postcode gevonden...");
				document.inschrijfform.dropdownlist_scholen.disabled=true;
			}				
		}				
	}
}
function schoolChanged() {
	
	if (xmlHttp.readyState==4) {
		var i;
		
		//check if browser = mozilla-achtig
		var moz = (typeof document.implementation != 'undefined') && (typeof
		document.implementation.createDocument != 'undefined');		
		
		if (moz) {
			
			var xmlDoc = xmlHttp.responseXML.getElementsByTagName("scholen");
			var myschools = xmlDoc[0].getElementsByTagName("School");
			document.inschrijfform.dropdownlist_scholen.options[document.inschrijfform.dropdownlist_scholen.selectedIndex].text = myschools[0].childNodes[0].firstChild.nodeValue;
			document.inschrijfform.adres.value = myschools[0].childNodes[1].firstChild.nodeValue;
			document.inschrijfform.plaats.value = myschools[0].childNodes[3].firstChild.nodeValue;
			document.inschrijfform.email.value = myschools[0].childNodes[6].firstChild.nodeValue;
			document.inschrijfform.tel.value = myschools[0].childNodes[4].firstChild.nodeValue;
			document.inschrijfform.accountId.value = myschools[0].childNodes[8].firstChild.nodeValue;
			document.inschrijfform.schoolnaam.value = myschools[0].childNodes[0].firstChild.nodeValue;
													
		} else {				
			var xmlDoc=xmlHttp.responseXML.documentElement;
			document.inschrijfform.dropdownlist_scholen.options[document.inschrijfform.dropdownlist_scholen.selectedIndex].text = xmlDoc.childNodes[0].getElementsByTagName("Naam1")[0].firstChild.text;
			document.inschrijfform.adres.value = xmlDoc.childNodes[0].getElementsByTagName("Adreslijn1")[0].firstChild.text;
			document.inschrijfform.plaats.value = xmlDoc.childNodes[0].getElementsByTagName("Plaats")[0].firstChild.text;
			document.inschrijfform.email.value = xmlDoc.childNodes[0].getElementsByTagName("Email")[0].firstChild.text;
			document.inschrijfform.tel.value = xmlDoc.childNodes[0].getElementsByTagName("Tel")[0].firstChild.text;
			document.inschrijfform.accountId.value = xmlDoc.childNodes[0].getElementsByTagName("AccountId")[0].firstChild.text;
			document.inschrijfform.schoolnaam.value = xmlDoc.childNodes[0].getElementsByTagName("Naam1")[0].firstChild.text;
		}
		
		var str = document.inschrijfform.accountId.value;
		var url = "ajx/ajx_zoekcontacts.asp";
		url=url+"?accountid="+str;
		xmlHttp2.onreadystatechange=changeContactList;
		xmlHttp2.open("GET",url,true);
		xmlHttp2.send(null);
	}
}

function contactChanged() {
	
	if (xmlHttp.readyState==4) {
		var i;
		
		//check if browser = mozilla-achtig
		var moz = (typeof document.implementation != 'undefined') && (typeof
		document.implementation.createDocument != 'undefined');		
		
		if (moz) {
			
			var xmlDoc = xmlHttp.responseXML.getElementsByTagName("contacts");
			var mycontacts = xmlDoc[0].getElementsByTagName("contact");
			
			if (mycontacts[0].childNodes[3].getAttribute("xsi:nil") == 1) {
				document.inschrijfform.aanspreking.value = "";
			} else
			{
				document.inschrijfform.aanspreking.value = mycontacts[0].childNodes[3].firstChild.nodeValue;
			}
			document.inschrijfform.voornaam.value = mycontacts[0].childNodes[1].firstChild.nodeValue;
			document.inschrijfform.achternaam.value = mycontacts[0].childNodes[2].firstChild.nodeValue;
			document.inschrijfform.contactId.value = mycontacts[0].childNodes[0].firstChild.nodeValue;
			
			if (mycontacts[0].childNodes[5].getAttribute("xsi:nil") == 1) {
				document.inschrijfform.email.value="";
			} else {
			
				document.inschrijfform.email.value = mycontacts[0].childNodes[5].firstChild.nodeValue;
			}
			
			if (mycontacts[0].childNodes[6].getAttribute("xsi:nil") == 1) {
				document.inschrijfform.tel.value="";
			} else {
			
				document.inschrijfform.tel.value = mycontacts[0].childNodes[6].firstChild.nodeValue;
			}
			
			for (i=0; i<document.inschrijfform.dropdownlist_functies.length; i++) {
			//document.inschrijfform.dropdownlist_functies.remove(i);
				if (document.inschrijfform.dropdownlist_functies.options[i].text == mycontacts[0].childNodes[7].firstChild.nodeValue) {
					document.inschrijfform.dropdownlist_functies.options[i].selected = true;
				}
			}
			document.inschrijfform.functieLabel.value = mycontacts[0].childNodes[7].firstChild.nodeValue;
			document.inschrijfform.functieValue.value = mycontacts[0].childNodes[9].firstChild.nodeValue;
			
			if (mycontacts[0].childNodes[4].firstChild.nodeValue ==1) {
				document.inschrijfform.dropdownlist_geslacht.options[1].selected = true;
			} else {
				document.inschrijfform.dropdownlist_geslacht.options[2].selected = true;
			}
				
			
			
													
		} else {				
			var xmlDoc=xmlHttp.responseXML.documentElement;
			
			if (xmlDoc.childNodes[0].getElementsByTagName("Aanspreking")[0].getAttribute("xsi:nil") == 1) {
				document.inschrijfform.aanspreking.value = "";
			} else
			{document.inschrijfform.aanspreking.value = xmlDoc.childNodes[0].getElementsByTagName("Aanspreking")[0].firstChild.text;
			}
			document.inschrijfform.voornaam.value = xmlDoc.childNodes[0].getElementsByTagName("Voornaam")[0].firstChild.text;
			document.inschrijfform.achternaam.value = xmlDoc.childNodes[0].getElementsByTagName("Achternaam")[0].firstChild.text;
			document.inschrijfform.contactId.value = xmlDoc.childNodes[0].getElementsByTagName("ContactId")[0].firstChild.text;
			
			if (xmlDoc.childNodes[0].getElementsByTagName("Email")[0].getAttribute("xsi:nil") == 1) {
				document.inschrijfform.email.value = "";
			} else {
						
				document.inschrijfform.email.value = xmlDoc.childNodes[0].getElementsByTagName("Email")[0].firstChild.text;
			}
			
			if (xmlDoc.childNodes[0].getElementsByTagName("Tel1")[0].getAttribute("xsi:nil") == 1) {
				document.inschrijfform.tel.value = "";
			} else {
						
				document.inschrijfform.tel.value = xmlDoc.childNodes[0].getElementsByTagName("Tel1")[0].firstChild.text;
			}
						
			var i;
			for (i=0; i<document.inschrijfform.dropdownlist_functies.length; i++) {
			//document.inschrijfform.dropdownlist_functies.remove(i);
				if (document.inschrijfform.dropdownlist_functies.options[i].text == xmlDoc.childNodes[0].getElementsByTagName("Functie")[0].firstChild.text) {
					document.inschrijfform.dropdownlist_functies.options[i].selected = true;
				}
			}
			document.inschrijfform.functieLabel.value = xmlDoc.childNodes[0].getElementsByTagName("Functie")[0].firstChild.text;
			document.inschrijfform.functieValue.value = xmlDoc.childNodes[0].getElementsByTagName("FunctieId")[0].firstChild.text;
			
			if (xmlDoc.childNodes[0].getElementsByTagName("Gendercode")[0].firstChild.text =='1') {
				document.inschrijfform.dropdownlist_geslacht.options[1].selected = true;
			} else {
				document.inschrijfform.dropdownlist_geslacht.options[2].selected = true;
			}
			
		}
	}
	
}

function changeContactList() {
		
	if (xmlHttp2.readyState==4) {
		var i;
		
		//dropdownlijst eerst leegmaken
		for (i=document.inschrijfform.dropdownlist_contacts.length-1; i>0; i--) {
			document.inschrijfform.dropdownlist_contacts.remove(i);
		}		
		
		//check if browser = mozilla-achtig
		var moz = (typeof document.implementation != 'undefined') && (typeof
		document.implementation.createDocument != 'undefined');		
		
		if (moz) {
			
			var xmlDoc = xmlHttp2.responseXML.getElementsByTagName("contacts");
			var mycontacts = xmlDoc[0].getElementsByTagName("contact");
			
			if (mycontacts.length > 0) {
				var aantal = mycontacts.length;
				document.inschrijfform.dropdownlist_contacts.options[0] = new Option("Kies...", "");
				var j;
				for (i=0; i<aantal; i++) {
						j = i + 1;
						displaycontact = mycontacts[i].childNodes[1].firstChild.nodeValue + " " + mycontacts[i].childNodes[2].firstChild.nodeValue;
						document.inschrijfform.dropdownlist_contacts.options[j] = new Option(displaycontact, mycontacts[i].childNodes[0].firstChild.nodeValue);
				}
				document.inschrijfform.dropdownlist_contacts.disabled=false;
					//indien geen gegevens, schrijf pro forma dropdownlijst
			} else {
				document.inschrijfform.dropdownlist_contacts.options[0] = new Option("Geen contactpersonen gevonden...");
				document.inschrijfform.dropdownlist_contacts.disabled=true;
			}
							
		} else {				
			var xmlDoc=xmlHttp2.responseXML.documentElement;			
				
			//en vervolgens opnieuw vullen
			if (xmlDoc.hasChildNodes() == true) {
				var aantal = xmlDoc.childNodes.length;
				document.inschrijfform.dropdownlist_contacts.options[0] = new Option("Kies...", "");
				var j;
				for (i=0; i<aantal; i++) {
					j = i + 1;
					displaycontacts = xmlDoc.childNodes[i].getElementsByTagName("Voornaam")[0].firstChild.text + " " + xmlDoc.childNodes[i].getElementsByTagName("Achternaam")[0].firstChild.text;
					document.inschrijfform.dropdownlist_contacts.options[j] = new Option(displaycontacts, xmlDoc.childNodes[i].getElementsByTagName("ContactId")[0].firstChild.text);
					document.inschrijfform.dropdownlist_contacts.disabled=false;
				}
				//indien geen gegevens, schrijf pro forma dropdownlijst
			} else {
				document.inschrijfform.dropdownlist_contacts.options[0] = new Option("Geen contactpersonen gevonden...");
				document.inschrijfform.dropdownlist_contacts.disabled=true;
			}				
		}				
	}
	
}

