


function GetXHR()
{
var xmlHttp=null;
try// Firefox, Opera 8.0+, Safari
 { 
 xmlHttp = new XMLHttpRequest();
 }
catch (e)
 {
 try  // Internet Explorer 6.0 and later
  {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)	//  Internet Explorer 5.5
  {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


function getToInnerHTML(uri,id,loadingText, callback)
{
var xhr = GetXHR();

xhr.onreadystatechange  = function()
{ 
	 if(xhr.readyState  == 4)
	 {
		  if(xhr.status  == 200)
		  	{
			document.getElementById(id).innerHTML = xhr.responseText;
			if (callback)
				callback(xhr.responseText);
			}
		  else
		  	{
			document.getElementById(id).innerHTML = "Error code " + xhr.status;
			if (callback)
				callback("Error code " + xhr.status);
			}
	 }
};

// loading place holder
if (loadingText != "")
	document.getElementById(id).innerHTML = loadingText;

xhr.open('GET', uri,  true); 
xhr.send(null);
}

function postXMLToInnerHTML(uri,id,loadingText,XML, callback)
{
var xhr = GetXHR();

xhr.onreadystatechange  = function()
{ 
	 if(xhr.readyState  == 4)
	 {
		  if(xhr.status  == 200)
		  	{
			document.getElementById(id).innerHTML = xhr.responseText;
			
			// if a callback function was given call it
			if (callback)
				callback(xhr.responseText);
			}
		  else 
			document.getElementById(id).innerHTML = "Error code " + xhr.status;
	 }
};

// loading place holder
if (loadingText != "")
	document.getElementById(id).innerHTML = loadingText;

xhr.open('POST', uri,  true);
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.send(XML);
}

function postXMLToCallback(uri,XML, callback)
{
var xhr = GetXHR();

xhr.onreadystatechange  = function()
{ 
	 if(xhr.readyState  == 4)
	 {
		  if(xhr.status  == 200)
		  	callback(xhr.responseText);
	 }
};

xhr.open('POST', uri,  true);
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.send(XML);
}

function getTextToCallback(uri, callback)
{
	var xhr = GetXHR();
	
	xhr.onreadystatechange  = function()
	{ 
		 if(xhr.readyState  == 4)
		 {
			  if(xhr.status  == 200)
				callback(xhr.responseText);
			  else 
				callback(false);
		 }
	};
	
	try{
		xhr.open('GET', uri,  true);
		xhr.send(null);
		}
	catch (e)
		{
		alert("Request uri was : " + uri);
		debug = new JAM_D_AnalyzeDialog(e, "e");
		callback(false);
		}
}

function postTextToCallback(uri, callback)
{
	var xhr = GetXHR();
	
	// split the URI from the query string
	var Parts = uri.split("?");
	uri = Parts[0];
	var PostData = Parts[1];
	
	xhr.onreadystatechange  = function()
	{ 
		 if(xhr.readyState  == 4)
		 {
			  if(xhr.status  == 200)
				callback(xhr.responseText);
			  else 
				callback(false);
		 }
	};
	
	try{
		xhr.open('POST', uri,  true);
	    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.send(PostData);
		}
	catch (e)
		{
		alert("request URI was : " + uri);
		alert("request Post Data was : " + uri);
		debug = new JAM_D_AnalyzeDialog(e, "e");
		callback(false);
		}
}

function getXMLToCallback(uri, callback)
{
	var xhr = GetXHR();
	
	xhr.onreadystatechange  = function()
	{ 
		 if(xhr.readyState  == 4)
		 {
			  if(xhr.status  == 200)
			  	{
				// check if the XML returned was an error message
				if (xhr.responseXML.getElementsByTagName("error_message").length > 0)
					{
					var ErrorMessage = xhr.responseXML.getElementsByTagName("error_message")[0];
					
					var Message = ErrorMessage.getAttribute("message");
					Message += "\n Code : " + ErrorMessage.getAttribute("code");
					
					var Msg = new JAM_MsgBox("Ok", Message);
					Msg.Execute();
					callback(false);
					}
				else
					callback(xhr.responseXML);
				}
			  else
			  	{
				var Msg = new JAM_MsgBox("Ok", "XML request failed with HTTP error code " + xhr.status);
				Msg.Execute();
				callback(false);
				}
		 }
	};
	
	try{
		xhr.open('GET', uri,  true);
		xhr.send(null);
		}
	catch (e)
		{
		debug = new JAM_D_AnalyzeDialog(e, "e");
		callback(false);
		}
}


function getToInnerHTMLWithJS(uri,id,loadingText, callback)
{
var xhr = GetXHR();

xhr.onreadystatechange  = function()
{ 
	 if(xhr.readyState  == 4)
	 {
		  if(xhr.status  == 200)
		  	{
			data = xhr.responseXML;
			
			html = "";
			if (xhr.responseXML.getElementsByTagName('html').length != 0)
				{
				if (xhr.responseXML.getElementsByTagName('html')[0].childNodes.length != 0)
					html = xhr.responseXML.getElementsByTagName('html')[0].childNodes[0].nodeValue;
				}
				
			script = "";
			if (xhr.responseXML.getElementsByTagName('script').length != 0)
				{
				if (xhr.responseXML.getElementsByTagName('script')[0].childNodes.length != 0)
					script = xhr.responseXML.getElementsByTagName('script')[0].childNodes[0].nodeValue;
				}
		
			// need to find why this is failing on IE 7 sp2 (answer because there is a JS function definition in the HTML!?!?!?)
			document.getElementById(id).innerHTML = html;
			
			// parse the given Javascript code.
			eval(script);
			
			// if a callback function was given call it
			if (callback)
				callback();
			}
		  else 
			document.getElementById(id).innerHTML = "Error code " + xhr.status;
	 }
};

// loading place holder
if (loadingText != "")
	document.getElementById(id).innerHTML = loadingText;

xhr.open('GET', uri,  true); 
xhr.send(null);
}

var JAM_XML_Entities = [["\"", "&quot;"],
						["'", "&apos;"],
						["<", "&lt;"],
						[">", "&gt;"],
						["&", "&amp;"]];

function XMLSpecialChars(string)
{
var NewString = "";
var i;
var j;

for (i=0; i<string.length; ++i)
	{
	Char = string.substr(i,1);
	
	// find if this character needs translating
	for (j=0; j<JAM_XML_Entities.length; ++j)
		if (Char == JAM_XML_Entities[j][0])
			{
			Char = JAM_XML_Entities[j][1];
			break;
			}
			
	NewString += Char;
	}
	
return NewString;
}

function decodeXMLSpecialChars(string)
{
var newString;
	
if (typeof(string) != "string" && typeof(string) != "object")
	{
	alert("parameter of type \"" + typeof(string) + "\" passed to decodeXMLSpecialChars function.");
	return string;
	}
	
// convert quotes
string.split('&quot').join('"');

//while (string != (newString = string.replace(/&quot;/,'"')))
//	string = newString;
	
// convert apostrophies
while (string != (newString = string.replace(/&apos;/,'\'')))
	string = newString;
	
// convert greater thans
while (string != (newString = string.replace(/&lt;/,'<')))
	string = newString;

// convert less thans
while (string != (newString = string.replace(/&gt;/,'>')))
	string = newString;
	
// convert ampersands. clever matches & but not when followed by amp;
while (string != (newString = string.replace(/&amp;/,'&')))
	string = newString;

return string;
}
