var request;

function createRequest() {
	//Initiate request object
	try {
    	request = new XMLHttpRequest();
	} catch (trymicrosoft) {
    	try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}
		}
	}
	if (!request) {
    	alert("Error initializing XMLHttpRequest!");
  	}
}


function response_handler() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var response = request.responseXML;

			//Get Type
			var record_type = response.getElementsByTagName('recordset')[0].getAttribute("type");

			//Take action based on type
			if(record_type == "updateHomeBranche") {
				var root = response.getElementsByTagName('item');
				
				//Delete all current values from the selectboxes
				var i;
				for(i=document.getElementById('branche').options.length-1;i>=0;i--) {
					document.getElementById('branche').remove(i);
				}
				var i;
				for(i=document.getElementById('regio').options.length-1;i>=0;i--) {
					document.getElementById('regio').remove(i);
				}
				
				//Add default item to box
				var theOpt = document.createElement("OPTION");
				theOpt.text = "Geen voorkeur";
				theOpt.value = "";
				document.getElementById('branche').options.add(theOpt);

				var theOpt = document.createElement("OPTION");
				theOpt.text = "Geen voorkeur";
				theOpt.value = "";
				document.getElementById('regio').options.add(theOpt);
				
				//Loop items, and fill the selectbox with it
				for (var iNode = 0; iNode < root.length; iNode++) {
					var theOpt = document.createElement("OPTION");
					theOpt.text = root[iNode].childNodes[0].nodeValue;
					theOpt.value = root[iNode].getAttribute("id");
						
					//if(root[iNode].getAttribute("id") == xmlData.getElementsByTagName('recordset')[0].getAttribute("selectedModel")) {
					//	theOpt.selected = true;
					//}
					if(root[iNode].getAttribute("type") == "branche") {
						document.getElementById('branche').options.add(theOpt);
					} else {
						document.getElementById('regio').options.add(theOpt);
					}
				}
			
			
			} else if(record_type == "updateSearchPage") {
				var root = response.getElementsByTagName('item');
				
				//Delete all current values from the selectboxes
				var i;
				for(i=document.getElementById('branche').options.length-1;i>=0;i--) {
					document.getElementById('branche').remove(i);
				}
				var i;
				for(i=document.getElementById('regio').options.length-1;i>=0;i--) {
					document.getElementById('regio').remove(i);
				}
				var i;
				for(i=document.getElementById('prijsklasse').options.length-1;i>=0;i--) {
					document.getElementById('prijsklasse').remove(i);
				}
				var i;
				for(i=document.getElementById('aanbodsinds').options.length-1;i>=0;i--) {
					document.getElementById('aanbodsinds').remove(i);
				}
				
				//Add default item to box
				var theOpt = document.createElement("OPTION");
				theOpt.text = "Geen voorkeur";
				theOpt.value = "";
				document.getElementById('branche').options.add(theOpt);

				var theOpt = document.createElement("OPTION");
				theOpt.text = "Geen voorkeur";
				theOpt.value = "";
				document.getElementById('regio').options.add(theOpt);
				
				var theOpt = document.createElement("OPTION");
				theOpt.text = "Geen voorkeur";
				theOpt.value = "";
				document.getElementById('prijsklasse').options.add(theOpt);
				
				var theOpt = document.createElement("OPTION");
				theOpt.text = "Geen voorkeur";
				theOpt.value = "";
				document.getElementById('aanbodsinds').options.add(theOpt);
				
				//Loop items, and fill the selectbox with it
				for (var iNode = 0; iNode < root.length; iNode++) {
					var theOpt = document.createElement("OPTION");
					theOpt.text = root[iNode].childNodes[0].nodeValue;
					theOpt.value = root[iNode].getAttribute("id");
						
					if(root[iNode].getAttribute("selected") == "yes") {
						theOpt.selected = true;
					}
					if(root[iNode].getAttribute("type") == "branche") {
						document.getElementById('branche').options.add(theOpt);
					} else if(root[iNode].getAttribute("type") == "regio") {
						document.getElementById('regio').options.add(theOpt);
					} else if(root[iNode].getAttribute("type") == "prijs") {
						document.getElementById('prijsklasse').options.add(theOpt);
					} else if(root[iNode].getAttribute("type") == "sinds") {
						document.getElementById('aanbodsinds').options.add(theOpt);
					}
				}
			}
        	
		} else {
        	alert("Invalid server status: " + request.status);
     	}	
	
	}
}

function updateHomeBranche(mt) {
	var url = "libraries/caller.php?type=updateHomeBranche&mt=" + escape(mt);
	createRequest();
	request.open("GET", url, true);
    request.onreadystatechange = response_handler;
    request.send(null);
}
function updateSearchPage(mt, branche, regio, prijs, sinds) {
	var url = "libraries/caller.php?type=updateSearchPage&mt=" + escape(mt) + "&branche=" + escape(branche) + "&regio=" + escape(regio) + "&prijs=" + escape(prijs) + "&sinds=" + escape(sinds);
	createRequest();
	request.open("GET", url, true);
    request.onreadystatechange = response_handler;
    request.send(null);
}
