// JavaScript Document
// Define global start variables
var XMLHttpRequestObject = null
var objectid = null

// Create XMLHttpRequest object
function GetXMLHttpRequestObject(handler){ 
	var objXMLHttp=null
	
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject){
		// All possible IE versions of XMLHTTP
		var msxmlhttp = new Array(
			'Msxml2.XMLHTTP.5.0',
			'Msxml2.XMLHTTP.4.0',
			'Msxml2.XMLHTTP.3.0',
			'Msxml2.XMLHTTP',
			'Microsoft.XMLHTTP');
		// Get the latest version
		for (var i = 0; i < msxmlhttp.length; i++) {
			try {
				objXMLHttp = new ActiveXObject(msxmlhttp[i]);
				break;
			} catch (e) {
				objXMLHttp = null;
			}
		}
	}
	
	if (objXMLHttp==null){
		alert ("Your browser doesn't seem to support the XMLHttpRequest object!")
		return
	}
	
	return objXMLHttp
}

// Decode a URL message from the PHP page
function URLDecode(strString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(strString).replace(lsRegExp, " "));
}

// Get a param value in a string
function getURLParam(strParamName, strURL){
	var strReturn = "";
	var strQueryString = strURL;

		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if(aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}

	return strReturn;
}

// Escape all values and create an URL
function createSafeURL(strURL){
	var strReturn = "";
	var strQueryString = strURL;

	var aQueryString = strQueryString.split("&");
	
	for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	
		var bQueryString = aQueryString[iParam].split("=")
		strReturn += bQueryString[0] + "=" + escape(bQueryString[1]);
		
		if(iParam < aQueryString.length-1){
			strReturn += "&"
		}
	}
	return strReturn;
}

// Prepare the sending of data to the PHP page
function runCodeBehind(strMessage){
	// Check what object who should be updated
	objectid = URLDecode(getURLParam("objectid", strMessage))
	// The URL with a random number to avoid IE cache bug
	var url="admin/getEvento.php?sid=" + Math.random() + "&" + createSafeURL(strMessage)
	
	// Uncomment following rows for debug info
	//alert(getURLParam("action", strMessage)) // What is the action
	//alert(getURLParam("objectid", strMessage)) // What is the object id name
	//alert(url)
	
	// If an output object is defined
	if(!objectid){
		alert('Not output data object is defined. Please define what object who\nshould be updated with objectid=idofobject in your AJAX call!');
		return
	}
	else {
		document.getElementById(objectid).innerHTML = "Processing..."
	}
	
	sendData(url);
}

// Send data to the PHP page
function sendData(url){
	// Create the XMLHttpRequest object
	XMLHttpRequestObject=GetXMLHttpRequestObject()
	// The name of the  function who should recive the data
	XMLHttpRequestObject.onreadystatechange=stateGetData
	// Open the connection
	XMLHttpRequestObject.open("GET",url,true)
	// Send the data
	XMLHttpRequestObject.send(null)
}

// Recieve data from the PHP page
function stateGetData() {
	// If the script is complete and all info have been recieved
	if ((XMLHttpRequestObject.readyState==4 || XMLHttpRequestObject.readyState=="complete") && XMLHttpRequestObject.status == 200){
		// Catch the result
		strResponseText = XMLHttpRequestObject.responseText
		// Get the data and the object who should be updated and URL decode them
		text = URLDecode(getURLParam("text", strResponseText))
		// Output the result to the HTML document
		document.getElementById(objectid).innerHTML = strResponseText
		
		// Reset the settings
		objectid=null
	}
}
