// support functions for using AJAX
// mainly initialise the xml request and copy resulting info into 
// the AJAX div element for display..

// a global that contains the xmlRequest object
var xmlhttp;
var koopId=-1;

// initialise the xml object and return success or failure.
function initXmlhttp(jsFunc)
{
	xmlhttp=null;
	
	if (!xmlhttp){
		// only construct a new xml object if it hasn't already been created 
		if (window.XMLHttpRequest)
		{
			// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
			
			if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType("text/html; charset=ISO-8859-1");
			
			// if a function name has been passed then set that
			if (jsFunc!=null) xmlhttp.onreadystatechange=jsFunc;

			return true;
  		}else{
			// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			
			if (jsFunc!=null) xmlhttp.onreadystatechange=jsFunc;

			return true;
		}
		
		
		return false;
	}else{
		// if a function name has been passed then set that as well
		if (jsFunc!=null){ xmlhttp.onreadystatechange=jsFunc;}else{alert("jsFunc is NULL");}
	}
	
	return true;
}

// moves the popup to the correct position
// and displays it.
function doPopup(jsLeft,jsTop,jsAjax)
{
	var jsPop=document.getElementById("ajaxInhoud");	

	// if we found the popup element then fill it and show it	
	if ((jsPop!=null)&&(jsAjax.length>0)){
		// set the size and position
		jsPop.style.left=jsLeft+"px";
		jsPop.style.top=jsTop+"px"; 
		jsPop.style.width="auto";
		jsPop.style.height="auto";

		// fill with links
		jsPop.innerHTML=jsAjax.valueOf();
		
		// show the popup
		showPopup();
	}else{
		hidePopup();
	}
}

function doSuggest(jsLeft,jsTop,jsAjax)
{
	var jsPop=document.getElementById("ajaxPopup");	

	// if we found the popup element then fill it and show it	
	if ((jsPop!=null)&&(jsAjax.length>0)){
		// set suggest class
		jsPop.className="ajaxSuggest";
		
		// set the size and position
		jsPop.style.left=jsLeft+"px";
		jsPop.style.top=jsTop+"px"; 
		jsPop.style.width="auto";
		jsPop.style.height="auto";

		// fill with links
		jsPop.innerHTML=jsAjax.valueOf();
		
		// show the popup
		showPopup();
	}else{
		hidePopup();
	}
}

// check whether th passed value is 'auto'
function isAuto(jsM){
	if(jsM){return jsM.toLowerCase()=="auto";}else{return false;}
}
// moves the popup to the correct position
// and displays it.
function doPopupEx(jsAjax,jsClass)
{
	var jsPop=document.getElementById("ajaxInhoud");	
	//if(!isAuto(jsLeft)){jsLeft=jsLeft+"px";}
	//if(!isAuto(jsTop)){jsTop=jsTop+"px";}
	
	// if we found the popup element then fill it and show it	
	if ((jsPop!=null)&&(jsAjax.length>0)){
		// set the size and position
		jsPop.className=jsClass;
		//jsPop.style.left=jsLeft;
		//jsPop.style.top=jsTop; 

		// fill with links
		jsPop.innerHTML=jsAjax.valueOf();
		
		// show the popup
		showPopup();
	}else{
		hidePopup();
	}
}

// hide the AJAX layer 
function hidePopup()
{
	var jsPop=document.getElementById("ajaxPopup");	
	
	if (jsPop!=null){
		// hide the popup
		jsPop.style.display="none";
		jsPop.style.visibility="hidden";
	}else{alert("No popup.");}

}

// show the AJAX layer 
function showPopup()
{
	var jsPop=document.getElementById("ajaxPopup");	
	
	if (jsPop!=null){
		// hide the popup
		jsPop.style.display="block";
		jsPop.style.visibility="visible";
	}else{alert("No popup.");}
}

// Artikel list functions
function updateMandje()
{
	// processes the reply from the XML call
	if(xmlhttp.readyState!=4) return;
	
	// fetch the reply from the server
	jsAjax=xmlhttp.responseText;
	
	// alert("Maandje returned:"+jsAjax);
	
	// Insert it into the basket layer if there is a repsonse..
	if (jsAjax.length==0) return;
	
	jsTot=document.getElementById("totaal")
	if (jsTot){
		// if we can find the totaal element change it's innerText
		jsTot.innerHTML=jsAjax;
	}
	
	// now open the info screen
	if(koopId>0)bestelInfo(koopId);
}
function addToMandje(jsId){
	// check the ID is valid
	if ((jsId==null)||(jsId=="")) return;koopId=jsId;if(initXmlhttp(updateMandje)){xmlhttp.open("GET","/cmsexec/mandje.asp?art="+jsId);xmlhttp.send(null);}
}

function bestelInfo(jsId){
	// use the ajaxPopup for this ..
	if ((jsId==null)||(jsId=="")) return;if (initXmlhttp(showProduct)){xmlhttp.open("GET","/product.asp?artikel="+jsId+"&koop=2");xmlhttp.send(null);}
}
function confirmOrder(jsId){
	// use the ajaxPopup for this ..
	//if ((jsId==null)||(jsId=="")) return;if (initXmlhttp(showConfirm)){xmlhttp.open("GET","/product.asp?artikel="+jsId+"&koop=1");xmlhttp.send(null);}
	if ((jsId==null)||(jsId=="")) return;koopId=jsId;if(initXmlhttp(updateMandje)){xmlhttp.open("GET","/cmsexec/mandje.asp?art="+jsId);xmlhttp.send(null);}
}
function showConfirm(){
	if(xmlhttp.readyState==4){jsAjax=xmlhttp.responseText;if(jsAjax.length==0){return;}
	doPopupEx( jsAjax,"confirmOrder");}
}
function leesMeer(jsId){
	if ((jsId==null)||(jsId=="")) return;if (initXmlhttp(showProduct)){xmlhttp.open("GET","/product.asp?artikel="+jsId);xmlhttp.send(null);}
}
function showProduct(){
	if(xmlhttp.readyState==4){jsAjax=xmlhttp.responseText;if(jsAjax.length==0){return;}
	doPopupEx( jsAjax,"prodAjax");}
}
// a function to hide the menu if a level 2 menu item is selected, this makes he menu disappear after it has been clicked
// not a great way to do it but all I have right now..
function hideLevel2Menu(){
		var jsUl=document.getElementById("level2menu");
		if (jsUl){
			// now set it to hidden
			jsUl.style.visibility="hidden";
			jsUl.style.display="none";
		}/*else{alert("couldn't find UL element");}*/
}