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;
 }

/* DEBUG * * * * * * * * * * * * * * * * * * * */

	function loadFragmentInToElementUsingGet(fragment_url, element_id, callback) {
		var element = document.getElementById(element_id);
	
		if(! isValidFragment(fragment_url)) return false;
		document.body.style.cursor = "progress";
	
		element.innerHTML = '<em>Please wait ...</em>';
	
		xmlhttp.open("GET", fragment_url);
	
		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(null);
	}
	
	function loadFragmentUsingGet(fragment_url, callback) {
		var ret;
		alert(fragment_url);
		if(! isValidFragment(fragment_url)) return false;
	
		document.body.style.cursor = "progress";
		xmlhttp.open("GET", fragment_url);
	
		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(null);
	}
	
/* * * * * * * * * * * * * * * * * * * * * * */

 function loadFragmentInToElement(fragment_url, element_id, callback, method) {
	var element = document.getElementById(element_id);

	if(! isValidFragment(fragment_url)) return false;
	document.body.style.cursor = "progress";
	
	if(method == "POST") var queryArray = splitQueryString(fragment_url);
	
	if(!method) method = "POST";
	if(method != "GET" && method != "POST") alert("Error: invalid method when executing remote query. Use GET or POST.");
	
	if(method == "GET") if(! isValidFragment(fragment_url)) return false;

	if(method == "POST") xmlhttp.open("POST", queryArray[0], false);
	if(method == "GET")  xmlhttp.open("GET", fragment_url);

	if(method == "POST") 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");
		}

    }
   if(method == "POST")  xmlhttp.send(queryArray[1]);
   if(method == "GET") 	 xmlhttp.send(null);
}
 
/* Loads the fragment and calls the callback function with
 * response as a parameter
 */
function loadFragment(fragment_url, callback, method) {
	var ret;
	if(!method) method = "POST";
	if(method != "GET" && method != "POST") alert("Error: invalid method when executing remote query. Use GET or POST.");
	
	if(method == "GET") if(! isValidFragment(fragment_url)) return false;

	document.body.style.cursor = "progress";
	
	if(method == "POST") var queryArray = splitQueryString(fragment_url);
	
	xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	if(method == "POST") xmlhttp.open("POST", queryArray[0], false);
	if(method == "GET")  xmlhttp.open("GET", fragment_url);
	
	if(method == "POST") 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");
		}
    }

   if(method == "POST")  xmlhttp.send(queryArray[1]);
   if(method == "GET") 	 xmlhttp.send(null);
}

/* Returns url base */
function GetBaseRef() {
	 var baseref = location.href.substring(0,location.href.lastIndexOf("/") + 1);
	 return baseref;
}

/* 
 * Internet Explorer maximum URL length is 2,083 Characters, 
 * though RFC 2616 that defines HTTP does not set limitations to URL size
 * Ref: http://support.microsoft.com/default.aspx?scid=KB;en-us;q208427
 * This function checks if the fragment length is valid
 * TODO: I DONT KNOW IF THIS APPLIES TO IE6???
 */
function isValidFragment(fragment_url) {
	if(! self.GetBaseRef) {
		alert("Error: GetBaseRef function not included.... ");
		return false;
	}

	var strFullUrl = GetBaseRef() + fragment_url;

	if( strFullUrl.length > 2083) {
		alert("Error: URL length " + strFullUrl.length  + " in remote query exceeds 2083 characters, which is the limit in IE");
		return false;		
	} else {
		return true;	
	}	
}

