// author: Rob


// Use this function to encode URI components, when uploading data to the server;
function EncodeAjaxUploadData(sTxt)
{
	var aCodes = { "	":"%09", "\n":"%0A", "\r":"%0D", " ":"%20",
		           "#":"%23", "$":"%24", "%":"%25", "&":"%26",            "+":"%2B", ",":"%2C",            "/":"%2F",            ":":"%3A", ";":"%3B", "=":"%3D", "?":"%3F", "@":"%40",
		"€":"%80",            "‚":"%82", "ƒ":"%83", "„":"%84", "…":"%85", "†":"%86", "‡":"%87", "ˆ":"%88", "‰":"%89", "Š":"%8A", "‹":"%8B", "Œ":"%8C",            "Ž":"%8E",
		           "‘":"%91", "’":"%92", "“":"%93", "”":"%94", "•":"%95", "–":"%96", "—":"%97", "˜":"%98", "™":"%99", "š":"%9A", "›":"%9B", "œ":"%9C",            "ž":"%9E", "Ÿ":"%9F",
		           "¡":"%A1", "¢":"%A2", "£":"%A3", "¤":"%A4", "¥":"%A5", "¦":"%A6", "§":"%A7", "¨":"%A8", "©":"%A9", "ª":"%AA", "«":"%AB", "¬":"%AC", "­":"%AD", "®":"%AE", "¯":"%AF",
		"°":"%B0", "±":"%B1", "²":"%B2", "³":"%B3", "´":"%B4", "µ":"%B5", "¶":"%B6", "·":"%B7", "¸":"%B8", "¹":"%B9", "º":"%BA", "»":"%BB", "¼":"%BC", "½":"%BD", "¾":"%BE", "¿":"%BF",
		"À":"%C0", "Á":"%C1", "Â":"%C2", "Ã":"%C3", "Ä":"%C4", "Å":"%C5", "Æ":"%C6", "Ç":"%C7", "È":"%C8", "É":"%C9", "Ê":"%CA", "Ë":"%CB", "Ì":"%CC", "Í":"%CD", "Î":"%CE", "Ï":"%CF",
		"Ð":"%D0", "Ñ":"%D1", "Ò":"%D2", "Ó":"%D3", "Ô":"%D4", "Õ":"%D5", "Ö":"%D6", "×":"%D7", "Ø":"%D8", "Ù":"%D9", "Ú":"%DA", "Û":"%DB", "Ü":"%DC", "Ý":"%DD", "Þ":"%DE", "ß":"%DF",
		"à":"%E0", "á":"%E1", "â":"%E2", "ã":"%E3", "ä":"%E4", "å":"%E5", "æ":"%E6", "ç":"%E7", "è":"%E8", "é":"%E9", "ê":"%EA", "ë":"%EB", "ì":"%EC", "í":"%ED", "î":"%EE", "ï":"%EF",
		"ð":"%F0", "ñ":"%F1", "ò":"%F2", "ó":"%F3", "ô":"%F4", "õ":"%F5", "ö":"%F6", "÷":"%F7", "ø":"%F8", "ù":"%F9", "ú":"%FA", "û":"%FB", "ü":"%FC", "ý":"%FD", "þ":"%FE", "ÿ":"%FF" };

	var c, s = "";
	for (var i=0; i<sTxt.length; i++)
	{
		c = sTxt.charAt(i);
		if (aCodes[c])
			s += aCodes[c];
		else
			s += c;
	}
	return s;
}

// This function is still here for backward compatibility.
// (For AJAX-requests it's better to use EncodeAjaxUploadData than fnEscape!)
var fnEscape = EncodeAjaxUploadData;
//var fnEscape = ((typeof encodeURIComponent == "function") ? encodeURIComponent : escape);
var fnUnescape = ((typeof decodeURIComponent == "function") ? decodeURIComponent : unescape);


function makeHttpRequest(url)
{
	makeAjaxRequest(url); 
}

// if return_xml is true, make sure that the server-side script starts with sending a "Content-type: text/xml" header!
function makeAjaxRequest(url, callback_function, return_xml)
{ 
	var objRequest = CreateHttpRequestObj(callback_function, return_xml); 
	if (!objRequest)
		return false; 

	objRequest.open('GET', url, true); 
	objRequest.send(null);
	return true;
}

function MakeAjaxGetRequest(sUrl, sQuery, sCallbackFunction, bXml) 
{
	return makeAjaxRequest(sUrl + (sQuery ? "?" + sQuery : ""), sCallbackFunction, bXml);
}

function MakeAjaxPostRequest(sUrl, sQuery, sCallbackFunction, bXml)
{
	var objRequest = CreateHttpRequestObj(sCallbackFunction, bXml); 
	if (!objRequest)
		return false; 

	objRequest.open('POST', sUrl, true); 
	objRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	objRequest.setRequestHeader("Content-Length", sQuery.length);
	objRequest.setRequestHeader("Connection", "close");
	objRequest.send(sQuery);
	return true;
}

function CreateHttpRequestObj(sCallbackFunction, bXml) 
{ 
	var objRequest = null; 
	if (window.XMLHttpRequest) // Mozilla, Safari, MSIE7,...
	{ 
		objRequest = new XMLHttpRequest(); 
		if (objRequest.overrideMimeType)
			objRequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject) // IE 
	{
		try
		{ 
			objRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
		}
		catch (e)
		{ 
			try
			{ 
				objRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			catch (e) {} 
		} 
	}

	if (objRequest && sCallbackFunction)
	{
		objRequest.onreadystatechange = function()
		{ 
			if (objRequest.readyState == 4)
			{ 
				if (objRequest.status == 200)
				{ 
					if (bXml)
						eval(sCallbackFunction + '(objRequest.responseXML)'); 
					else 
						eval(sCallbackFunction + '(objRequest.responseText)'); 
				}
				else
					alert('There was a problem with the request.(Code: ' + objRequest.status + ' => ' + objRequest.statusText + ')'); 
			} 
		} 
	}

	return objRequest; 
}

