/*
this.parentNode=Johan
*/
/*TO INVESTIGATE
*cross-domain acces
*function uploadFile()
-->HTTP method "put"
*function deletefile()
-->HTTP method "delete"

*bookmark & back/forwardbutton
*window location vanuit php of meegegeven met functie
*preloader
*chache variable enkel voor IE6 laten gelden?? no-chache header???
*safer JSON
*destination as primary instead of data
*wat als er een 0 terugkomt van Atalanta, data of geen data??
*/



/*VARIABLES*/
var req;
var activeRequest=false;
var method=false;
var file=false;
var format=false;
var destination=false;
/*END VARIABLES*/



/*INITIALIZE REQUEST*/
function loadFile(HTTPmethod, fileURL, varString, responseFormat, destID)
{
	document.body.style.cursor = "wait";
	if (activeRequest==false)
	{
		HTTPmethod=HTTPmethod.toUpperCase();

		if(HTTPmethod=="GET" || HTTPmethod=="POST" || HTTPmethod=="HEAD")
		{
			if(fileURL.indexOf(".")!=-1)
			{
				req = createObject();
				if(req!=null)
				{
					if(!isSet(responseFormat))
					{
						responseFormat=false;
					}
					else
					{
						responseFormat=responseFormat.toLowerCase();
					}
					if(responseFormat=="text" || responseFormat=="xml" || responseFormat=="json" || !responseFormat)
					{
						if(!isSet(varString))
						{
							varString="";
						}
						if(!isSet(destID))
						{
							destID=false;
						}
						activeRequest=true;
						method=HTTPmethod;
						file=fileURL;
						format=responseFormat;
						destination=destID;

						if(method!="GET")
						{
							req.open(method, file, true);
							req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
						}
						else
						{
							var rand=Math.floor(Math.random()*10000000);
							var url=file+"?cache="+rand;
							if (varString!="")
							{
								url+="&"+varString;
							}
							req.open(method,encodeURI(url), true);
						}
						req.onreadystatechange = processReqChange;
						switch(method)
						{
							case "GET":
							case "HEAD":req.send(null);break;
							case "POST":req.send(varString);break;
						}
					}
					else
					{
						alert("'"+responseFormat+"' is not a valid response type.\n The request will be aborted.");
					}
				}
				else
				{
					alert("Your browser does not support AJAX.");
				}
			}
			else
			{
				alert("'"+fileURL+"' is not a valid file.\n The request will be aborted.");
			}
		}
		else
		{
			alert("'"+HTTPmethod+"' is not a valid method.\n The request will be aborted.");
		}
	}
	else
	{
		window.setTimeout(function () { loadFile(HTTPmethod, fileURL, varString, responseFormat, destID); }, 10);
	}
}
/*END INITIALIZE REQUEST*/



/*CREATE EMPTY REQUEST*/
function createObject()
{
	req = null;
	if(window.XMLHttpRequest)
	{
		try
		{
			req = new XMLHttpRequest();
		}
		catch(e)
		{
			req = null;
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				req = null;
			}
		}
	}
	if(req)
	{
		return req;
	}
}
/*END CREATE EMPTY REQUEST*/



/*ISSET CHECK*/
function isSet(variable)
{
	if(typeof(variable)=="undefined" || variable=="undefined" || variable==false || variable==null)
	{
		return false;
	}
	else
	{
		return true;
	}
}
/*END ISSET CHECK*/



/*FETCH DATA*/
function processReqChange()
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			var data=false;
			if(method=="HEAD")
			{
				data = req.getAllResponseHeaders();
			}
			else
			{
				switch(format)
				{
					case "json":data = eval('(' + req.responseText + ')');break;
					case "text":data = req.responseText;break;
					case "xml": data = req.responseXML;break;
					default:    data = req.responseText;break;
				}
			}
/*DATA HANDLING*/
			if(data)
			{
				handleData(data);
			}
/*DATA HANDLING*/
/*NO DATA HANDLING*/
			else
			{
				handleNoData();
			}
/*END NO DATA HANDLING*/
		}
/*STATUS ERROR*/
		else
		{
			alert("There was a problem retrieving the requested page:\n" + req.statusText);
			clearRequest();
		}
/*END STATUS ERROR*/
	}
}
/*END FETCH DATA*/



/*DATA HANDLING*/
function handleData(data)
{
	// User-defined callback.
	if (destination && Object.prototype.toString.call(destination) === "[object Function]")
	{
		destination(data);
		clearRequest();
	}
/*SPECIAL DATA ACTIONS*/
/***TYPE ONLY HERE***/
/*LOGIN*/
	else if(file.indexOf("logincheck")!=-1)
	{
		clearRequest();
		if(data!=0 && !isNaN(data))
		{
			switch(data)
			{
				case "20":data=31;break;
				case "21":data=32;break;
			}
			startAlert(data);
		}
		else if(data == 'x')
		{
			window.location.href="/login.php";
		}
		else
		{
			window.location.reload();
		}
	}
/*END LOGIN*/
/*ADVANTAGE CODE*/
	else if(file.indexOf("checkCode")!=-1)
	{
		clearRequest();
		if(data!=0 && !isNaN(data))
		{
			startAlert(data);
		}
		else
		{
			window.location.reload();
		}
	}
/*END ADVANTAGE CODE*/
	else if(destination && destination.indexOf("ordercont")!=-1)
	{
		placeData(data);
		clearRequest();
		fillOrderInfo();
	}
/*LANDINGSPAGES*/
	else if (file.indexOf("landingsPages")!= -1)
	{
		landingsPagesHandler(data,destination);
		clearRequest();
	}
/*END LANDINGSPAGES*/
	else if(file.indexOf("get_registration_info")!=-1 && format=="xml")
	{
		clearRequest();
		validateCompanySelection_Handler(data);
	}
/***END TYPE ONLY HERE***/
/*END SPECIAL DATA ACTIONS*/
/*DEFAULT DATA HANDLER*/
	else
	{
		if(isSet(destination))
		{
			if(isSet(document.getElementById(destination)))
			{
				placeData(data);
			}
			else if(destination.indexOf(".")!=-1)
			{
				window.location.href=encodeURI(destination);
			}
		}
		clearRequest();
	}
/*END DEFAULT DATA HANDLER*/
}
/*END DATA HANDLING*/



/*HANDLING EMPTY REQUEST*/
function handleNoData()
{
	if(!format)
	{
		if(destination)
		{
			if(Object.prototype.toString.call(destination) === "[object Function]")
			{
				destination();
				clearRequest();
			}
			else if(destination.indexOf(".")!=-1)
			{
				window.location.href=encodeURI(destination);
				clearRequest();
			}
			else
			{
				if(isSet(document.getElementById(destination)))
				{
					placeData();
				}
				else
				{
					alert("Invalid destination: "+destination);
				}
				clearRequest();
			}
		}
		else
		{
		clearRequest();
		}
	}
	else
	{
		if((destination && isSet(document.getElementById(destination))))
		{
			placeData();
		}
		clearRequest();
	}
}
/*END HANDLING EMPTY REQUEST*/



/*EMPTIES THE REQUEST*/
function clearRequest()
{
	activeRequest=false;
	method=false;
	file=false;
	format=false;
	destination=false;
	document.body.style.cursor = "default";
}
/*END EMPTIES THE REQUEST*/



/*PLACING THE DATA*/
function placeData(data)
{
	if (!isSet(data))
	{
		data="";
	}
	var node=document.getElementById(destination);
	if(node.value && node.tagName!="LI")
	{
		node.value=data;
	}
	else
	{
		node.innerHTML=data;
	}
}
/*END PLACING THE DATA*/