var xmlhttp;
/*------------------------------------------------------------------
This function creates an ajax request
------------------------------------------------------------------*/
function ajaxRequest_JD(params){
	/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	Avalible params --------------------------------------
	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	
	multiple inputs are seperated by a '|'.
	
	REQUIRED
	- script (the path to the required script)
	
	OPTIONAL
	- inputName (the variables used in the request)
	- inputData (the data used in the request)
	- type (GET or POST - GET is default)
	- action (if it isn't set the executuing code will have to wait for a reply before continuing)
	- timeout (default of 2000ms)
	- onError (specify a function to call upon an error)
	_______________________________________________________
	_______________________________________________________*/
	
	if(params.script == undefined){
		alert("JD System Design AJAX API Error! No script has been specified.");
		return;
	}
	
	var script = params.script || "";
	var inputName = params.inputName || "";
	var inputData = params.inputData || "";
	var type = params.type || "GET";
	var action = params.action || "false";
	var timeout = params.timeout || 2000;
	var onError = params.onError || defaultAjaxError;
	
	script = trim_JD(script);
	inputName = trim_JD(inputName);
	type = trim_JD(type);
	timeout = parseInt(timeout);
	
	if(timeout < 1){
		alert("JD System Design AJAX API Error! The timeout is set too low, a minimum of 1ms is accepted");
		return;
	}
	
	var numName = getNumInputs_JD(inputName);
	var numData = getNumInputs_JD(inputData);
	
	if(numName != numData){
		alert("JD System Design AJAX API Error! The number of variables doesn't match the number of inputs.");
		return;
	}
	
	var singleName = new Array;
	var singleData = new Array;
	
	for(var i=0; i<numName; i++){
		singleName[i] = getRequiredParam_JD(inputName,i); 	
		singleData[i] =  encodeURIComponent(getRequiredParam_JD(inputData,i));
	}

	if(type == "GET"){var readyData = "?";}else{var readyData = "";}
	readyData = readyData + singleName[0] + "=" + singleData[0];
	for(var i=1; i<numName; i++){readyData = readyData + "&" + singleName[i] + "=" + singleData[i];}
	if(action == "false"){var synchronous = false;}else{var synchronous = true;}

	xmlhttp=GetXmlHttpObject();
	
	if(type == "GET"){
		xmlhttp.open(type, script+readyData, synchronous);
		xmlhttp.send(null)
		var ajaxT= setTimeout("ajaxTimeout("+onError+");",timeout);
	}else{
		xmlhttp.open(type, script, synchronous);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", readyData.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(readyData);
		var ajaxT = setTimeout("ajaxTimeout("+onError+");",timeout);
	}
		
	if(synchronous == true){
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){clearTimeout(ajaxT);action(xmlhttp.responseText);}
		}
	}else{return xmlhttp.responseText;}
}

function GetXmlHttpObject(){
	if(window.XMLHttpRequest){return new XMLHttpRequest();}
	if(window.ActiveXObject){return new ActiveXObject("Microsoft.XMLHTTP");}
	return null;
}

function ajaxTimeout(onError){
   xmlhttp.abort();
	onError();	   
}

function defaultAjaxError(){
	alert("JD System Design AJAX API Error! The connection to the server timed out! (timeout too low?)");  
}
