/**
 * @author jontj1
 */

 var xmlHttpStats = createXmlHttpRequestObjectStats();
 var serverAddressStat = "../includes/ajaxUpdateStats.php";
 var valueString = '';
 var debugMode = false;
 var debugModePHP = false;
 
 function updateStats(agentId, column){
 	valueString = "agentId="+agentId+"&column="+column;
	sendStatRequest();
 }
 
 function createXmlHttpRequestObjectStats(){
 	var xmlHttp;
	//this should work for all browsers except IE6 and older
	try{
		//try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		//assume IE6 or older when try block fails
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		//try every prog id until one works
		for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
			try{
				//try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}catch(e){};
		}
	}
	//return the created object or display an error message
	if(!xmlHttp){
	//alert('timmy');
		displayError("Error creating the XMLHttpRequest object");
	}else{
		return xmlHttp;
	}
 }
 
 //initiate HTTP request to retrieve suggestions for the current keyword
 function sendStatRequest(){
	//if the keyword isnt in the cache make an HTTP request
	try{
		//dont start another operation if one is already in progress
		if (xmlHttpStats.readyState == 4 || xmlHttpStats.readyState == 0) {
			xmlHttpStats.open("POST", serverAddressStat, true);
			xmlHttpStats.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttpStats.onreadystatechange = handleServerResponseStat;
			xmlHttpStats.send(valueString);
		}
	}catch(e){
		if(debugMode){
			displayError("Cant connect to server:\n"+e.toString());
		}
	}
 }
 
 function handleServerResponseStat(){
 	//when readyState is 4 we are ready to read the server response
	if(xmlHttpStats.readyState==4){
		//continue only if HTTP status is OK
		if(xmlHttpStats.status==200){
			try{
				checkResponseStat();
			}catch(e){
				//display error message
				if(debugMode){
					displayError(e.toString());
				}
			}
		}else{
			//display error message
			if(debugMode){
				displayError("There was a problem retrieving the data:\n"+xmlHttpStats.statusText);
			}
		}
	}	
 }
 
 //function that processes the servers response
 function checkResponseStat(){
 	//retrieve the servers response
	var response = xmlHttpStats.responseText;
	//server error?
	if(response.indexOf("ERRNO")>=0 || response.indexOf("error:")>=0 || response.length==0){
		throw(response.length == 0 ? "Server error." : response);
	}
	//retrieve the document element
	var xmlDocUsername = xmlHttpStats.responseXML.documentElement;
	//initialise the new array of function names
	var resultUsername = xmlDocUsername.getElementsByTagName("msg")[0].firstChild.data;
	displayResultsStat(resultUsername);
 }
 
 function displayResultsStat(msg){
 	//no response required
	if(debugModePHP){
		alert(msg);
	}
 	
 }
 
 //function that displays an error message
 function displayError(message){
 	//display error message, with more technical details if debugMode is true
	alert("Error accessing server!"+(debugMode ? "\n"+message : ""));
 }
