var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {	
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

/* Loads the fragment and inserts response html into given element.
 * If callback function is given, will call it after finished
 */
 
 function splitQueryString(queryString) {
	
	var queryArray = queryString.split("?");
	
	return queryArray;
 }

 function loadQueryInToElement(fragment_url, element_id, callback) {
	var element = document.getElementById(element_id);

	document.body.style.cursor = "progress";
	
	var queryArray = splitQueryString(fragment_url);

	xmlhttp.open("POST", queryArray[0], true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.body.style.cursor = "auto";
			element.innerHTML = xmlhttp.responseText;
			if(callback) callback();
		} if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
			document.body.style.cursor = "auto"
			element.innerHTML = "Warning: Network not available... check your internet connection";
			if(callback) callback("errorno=10&error=Warning: Network not available.. check your internet connection&errlvl=notice");
		}

   }
   xmlhttp.send(queryArray[1]);
}

/* Loads the fragment and calls the callback function with response as a parameter */
function loadQuery(fragment_url, callback) {

	var ret;
	document.body.style.cursor = "progress";
	
	var queryArray = splitQueryString(fragment_url);

	xmlhttp.open("POST", queryArray[0], true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.body.style.cursor = "auto"
			if(callback) callback(xmlhttp.responseText);
		} if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
			document.body.style.cursor = "auto"
			if(callback) callback("errorno=10&error=Warning: Network not available.. check your internet connection&errlvl=notice");
		}
    }
    xmlhttp.send(queryArray[1]);
}

function validateCallBackData(namevaluepairs) {
	if(namevaluepairs == null) {
		alert("Error: Cannot validate callback data, null parameter");
		return false;	
	}
	var oQuery = stripQuerystring(namevaluepairs); 
	if(!oQuery) { 
		namevaluepairs = namevaluepairs.split('\n');
		if(namevaluepairs.length > 5) {
			for(i=0; i < 5; i++) {
				namevaluepairs += namevaluepairs[i];	
			}
		}
		alert("ERROR: Failed to validate call back data :" + namevaluepairs );
		return null;
	}

	if(oQuery["errorno"] != 0 && oQuery["errorno"] != "" )  {
		if(oQuery["errlvl"] == "warning") {
			addComment("WARNING:<br/>" + URLDecode(oQuery["error"]));
		} else if (oQuery["errlvl"] == "debug") { 
			addComment("DEBUG:<br/>" + URLDecode(oQuery["error"]));	
		} else {
			alert(oQuery["error"]);
			//addComment( URLDecode(oQuery["error"]) );
		}

		//doError("Call back failed (validateCallBackData)",oQuery["errorno"],oQuery["error"]);
		return false;
	}	

	//if(document.getElementById("error")) document.getElementById("error").innerHTML = URLDecode(oQuery["error"]); else alert("Error: No div for displaying error");
	//if(document.getElementById("errorno")) document.getElementById("errorno").value= oQuery["errorno"]; else alert("Error: No div for displaying error number");
	return oQuery;
}

/* Strips each name value pair in given string and returns object["name"] = value */ 
function stripQuerystring(str) {
	try {
		var
			aURLQuery=str.split(/[&;]/), 
			re=/(.*)=(.*)/, 
			i=aURLQuery.length, 
			oURLQuery={}, 
			aTemp; 
		while(i-- > 0) { 
			aTemp=re.exec(aURLQuery[i]); 
			oURLQuery[aTemp[1]]=unescape(aTemp[2]); 
		}
		return oURLQuery; 
		
	} catch(er) {
		alert("Error parsing querystring: " + str);	 
	}	
}
