// ajax.js

// XML HTTP request object
var http = null;
var domein = return_website();

/***********************************************************************************
	XML HTTP request object aanmaken
***********************************************************************************/
function maakRequestObject(){
	var requester;
	
	try{
		requester = new XMLHttpRequest();
	}
	catch(error){
		try{
			requester = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(error){
			requester = null;
			return false;
		}
	}
	
	return requester;
}

/***********************************************************************************
 bestelling toon details
 bij klikken op icoontje
***********************************************************************************/
function ad_bestelling_toon_details(value){
	if(http != null && http.readyState != 0 && http.readyState != 4){
		http.abort();
	}	
	http = maakRequestObject();
	
	//http.open("GET","../../../../ajax/naam_url.php?value="+encodeURIComponent(value));	
	
	http.open("POST","http://"+domein+"/ajax/ad_bestelling_toon_details.php",true);
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http.setRequestHeader("X_USERAGENT", "dArtAjax");
	http.setRequestHeader('Connection', 'close');
	http.send('value='+value);

	divid = 'res-'+value;
	if(document.getElementById(divid).style.display != 'block'){ 
		document.getElementById(divid).style.visibility = 'visible'; 
		document.getElementById(divid).style.display = 'block'; 
		document.getElementById(divid).style.height = 'auto';
		
		//document.getElementById('res-'+bestel_id).innerHTML = bestelling;
	}
	else {
		document.getElementById(divid).style.height = 0 + 'px';
		document.getElementById(divid).style.visibility = 'hidden'; 
		document.getElementById(divid).style.display = 'none';
	}

	http.onreadystatechange = onreadystatechange_ad_bestelling_toon_details;
	//http.send(null);
	return true;
}

function onreadystatechange_ad_bestelling_toon_details(){
	// if  XMLHR object has finished retrieving the data
	
	if(http.readyState == 4){
		// if the data was retreived succesfully
		if(http.status == 200){
			
			if(http.responseText.charAt(0) == "<"){
				var antwoord = http.responseXML;
				
				var tagBestel_id			= antwoord.getElementsByTagName('bestel_id');
				var bestel_id					= tagBestel_id[0].childNodes[0].nodeValue;
				
				var tagBestelling			= antwoord.getElementsByTagName('bestelling');
				//var bestelling				= tagBestelling[0].childNodes[0].nodeValue;
				
				var bestelling 					= '';
				// door grote aantal karakters wordt dit in Firefox opgesplitst in blokken van 4096 karakters
				// daarom moeten we deze terug aan elkaar zetten
				var i=0;
				while(i<tagBestelling[0].childNodes.length){
					bestelling += tagBestelling[0].childNodes[i].nodeValue;
					i++;
				}
				
				divid = 'res-'+bestel_id;
				
				document.getElementById('res-'+bestel_id).innerHTML = bestelling;
				if(document.getElementById(divid).style.display != 'block'){ 
					//document.getElementById(divid).style.visibility = 'visible'; 
					//document.getElementById(divid).style.display = 'block'; 
					//document.getElementById(divid).style.height = 'auto';
					
					
				}
				else {
					//document.getElementById(divid).style.height = 0 + 'px';
					//document.getElementById(divid).style.visibility = 'hidden'; 
					//document.getElementById(divid).style.display = 'none';
					
					
				}
				
			}
			else{
				// alert("geen XML "+http.responseText);		
			}
		}
		// IE returns a status code of 0 on some occasions, so ignore this case
		else if(http.status != 0){
			alert("There was an error while retreiving the URL: " + http.statusText);
		}
	}
	
	return true;
}


/***********************************************************************************
 bestelling nof
 bij klikken op icoontje
***********************************************************************************/
function ad_bestelling_nof(value, nof){
	if(http != null && http.readyState != 0 && http.readyState != 4){
		http.abort();
	}	
	http = maakRequestObject();
	
	//http.open("GET","../../../../ajax/naam_url.php?value="+encodeURIComponent(value));	
	
	http.open("POST","http://"+domein+"/ajax/ad_bestelling_nof.php",true);
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http.setRequestHeader("X_USERAGENT", "dArtAjax");
	http.setRequestHeader('Connection', 'close');
	http.send('value='+value+'&nof='+nof);

	http.onreadystatechange = onreadystatechange_ad_bestelling_nof;
	//http.send(null);
	return true;
}

function onreadystatechange_ad_bestelling_nof(){
	// if  XMLHR object has finished retrieving the data
	
	if(http.readyState == 4){
		// if the data was retreived succesfully
		if(http.status == 200){
			
			if(http.responseText.charAt(0) == "<"){
				var antwoord = http.responseXML;
				
				var tagBestel_id			= antwoord.getElementsByTagName('bestel_id');
				var bestel_id					= tagBestel_id[0].childNodes[0].nodeValue;
				
				var tagNof						= antwoord.getElementsByTagName('nof');
				var nof								= tagNof[0].childNodes[0].nodeValue;
				
				var imgid = 'nof-'+bestel_id;
				var afbeelding;
				
				var afb_0 = new Image();	afb_0.src = "http://"+domein+"/images/icons/icon_book_open.gif";
				var afb_1 = new Image();	afb_1.src = "http://"+domein+"/images/icons/icon_book.gif";
				
				if(nof=='0'){
					document.getElementById(imgid).src = afb_0.src;
					document.getElementById(imgid).attributes['onclick'].value = "ad_bestelling_nof("+bestel_id+",1)";
					//document[imgid].src = afb_0.src;
				}
				else{
					document.getElementById(imgid).src = afb_1.src;
					document.getElementById(imgid).attributes['onclick'].value = "ad_bestelling_nof("+bestel_id+",0)";
					//document[imgid].src = afb_1.src;
				}
				
				
				/*
				var tagBestelling			= antwoord.getElementsByTagName('bestelling');
				//var bestelling				= tagBestelling[0].childNodes[0].nodeValue;
				
				var bestelling 					= '';
				// door grote aantal karakters wordt dit in Firefox opgesplitst in blokken van 4096 karakters
				// daarom moeten we deze terug aan elkaar zetten
				var i=0;
				while(i<tagBestelling[0].childNodes.length){
					bestelling += tagBestelling[0].childNodes[i].nodeValue;
					i++;
				}
				
				divid = 'res-'+bestel_id;
				if(document.getElementById(divid).style.display != 'block'){ 
					document.getElementById(divid).style.visibility = 'visible'; 
					document.getElementById(divid).style.display = 'block'; 
					document.getElementById(divid).style.height = 'auto';
					
					document.getElementById('res-'+bestel_id).innerHTML = bestelling;
				}
				else {
					document.getElementById(divid).style.height = 0 + 'px';
					document.getElementById(divid).style.visibility = 'hidden'; 
					document.getElementById(divid).style.display = 'none';
				}
				*/
			}
			else{
				// alert("geen XML "+http.responseText);		
			}
		}
		// IE returns a status code of 0 on some occasions, so ignore this case
		else if(http.status != 0){
			alert("There was an error while retreiving the URL: " + http.statusText);
		}
	}
	
	return true;
}


/***********************************************************************************
 bestelling maps
 bij klikken op image
***********************************************************************************/
function ad_bestelling_maps(value, checked){
	if(http != null && http.readyState != 0 && http.readyState != 4){
		http.abort();
	}	
	http = maakRequestObject();
	
	//http.open("GET","../../../../ajax/naam_url.php?value="+encodeURIComponent(value));	
	
	http.open("POST","http://"+domein+"/ajax/ad_bestelling_maps.php",true);
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http.setRequestHeader("X_USERAGENT", "dArtAjax");
	http.setRequestHeader('Connection', 'close');
	http.send('value='+value+'&checked='+checked);

	http.onreadystatechange = onreadystatechange_ad_bestelling_maps;
	//http.send(null);
	return true;
}

function onreadystatechange_ad_bestelling_maps(){
	// if  XMLHR object has finished retrieving the data
	
	if(http.readyState == 4){
		// if the data was retreived succesfully
		if(http.status == 200){
			
			if(http.responseText.charAt(0) == "<"){
				var antwoord = http.responseXML;
				
				var tagBestel_id			= antwoord.getElementsByTagName('bestel_id');
				var bestel_id					= tagBestel_id[0].childNodes[0].nodeValue;
				
				var tagCheck					= antwoord.getElementsByTagName('checked');
				var check							= tagCheck[0].childNodes[0].nodeValue;
				
				var imgid = 'maps-'+bestel_id;
				var afbeelding;
				
				var afb_0 = new Image();	afb_0.src = "http://"+domein+"/images/icons/icon_maps_rood.gif";
				var afb_1 = new Image();	afb_1.src = "http://"+domein+"/images/icons/icon_maps_groen.gif";
				
				if(check=='0'){
					document.getElementById(imgid).src = afb_0.src;
					document.getElementById(imgid).attributes['onclick'].value = "ad_bestelling_maps("+bestel_id+",1)";
					//document[imgid].src = afb_0.src;
				}
				else{
					document.getElementById(imgid).src = afb_1.src;
					document.getElementById(imgid).attributes['onclick'].value = "ad_bestelling_maps("+bestel_id+",0)";
					//document[imgid].src = afb_1.src;
				}
			}
			else{
				// alert("geen XML "+http.responseText);		
			}
		}
		// IE returns a status code of 0 on some occasions, so ignore this case
		else if(http.status != 0){
			alert("There was an error while retreiving the URL: " + http.statusText);
		}
	}
	
	return true;
}

/***********************************************************************************
 bestelling maps clear
 bij klikken op image
***********************************************************************************/
function ad_bestelling_maps_clear(value){
	if(http != null && http.readyState != 0 && http.readyState != 4){
		http.abort();
	}	
	http = maakRequestObject();
	
	//http.open("GET","../../../../ajax/naam_url.php?value="+encodeURIComponent(value));	
	
	http.open("POST","http://"+domein+"/ajax/ad_bestelling_maps_clear.php",true);
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http.setRequestHeader("X_USERAGENT", "dArtAjax");
	http.setRequestHeader('Connection', 'close');
	http.send('value='+value);

	http.onreadystatechange = onreadystatechange_ad_bestelling_maps_clear;
	//http.send(null);
	return true;
}

function onreadystatechange_ad_bestelling_maps_clear(){
	// if  XMLHR object has finished retrieving the data
	
	if(http.readyState == 4){
		// if the data was retreived succesfully
		if(http.status == 200){
			
			if(http.responseText.charAt(0) == "<"){
				var antwoord = http.responseXML;
				
				var tagBestel_id			= antwoord.getElementsByTagName('bestel_id');
				var sID								= tagBestel_id[0].childNodes[0].nodeValue;
				
				var aID = sID.split(',');
				for(i in aID){
					var imgid = 'maps-'+aID[i];
				
					var afb_0 = new Image();	afb_0.src = "http://"+domein+"/images/icons/icon_maps_rood.gif";
					if(document.getElementById(imgid)!=null){
						document.getElementById(imgid).src = afb_0.src;
						document.getElementById(imgid).attributes['onclick'].value = "ad_bestelling_maps("+aID[i]+",1)";
					}
				}
			}
			else{
				// alert("geen XML "+http.responseText);		
			}
		}
		// IE returns a status code of 0 on some occasions, so ignore this case
		else if(http.status != 0){
			alert("There was an error while retreiving the URL: " + http.statusText);
		}
	}
	
	return true;
}