
function Ajax()
{

}

//---
// get a new XMLHTTP object, use a seperate object for each request
//---
Ajax.GetHttpRequestObject=function()
{
	try {return new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {};
	try {return new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {};
	try {return new XMLHttpRequest();} catch (e) {};
	alert("No XMLHTTP object available");
	return null;
}

//readyState Status Codes:
var RS_UNINITIALIZED=0;
var RS_LOADING=1;
var RS_LOADED=2;
var RS_INTERACTIVE=3;
var RS_COMPLETE=4;

//---
// retrieves a page and calls a callback function of the form MyCallback(bSuccess,sPageText,oArguments)
//---
Ajax.Get=function(sUrl,oCallback,oArguments)
{
	// Defect #11070 - caching issue causing loop
	// manually add the timestamp since CUrl may not be included
	var sOldUrl = sUrl;
	if(sUrl.indexOf("?") > -1) sUrl += "&"
	else sUrl += "?";
	sUrl += "nTimestamp="+new Date().getTime();
	
	var oHttpRequest = Ajax.GetHttpRequestObject();
	oHttpRequest.open("GET", sUrl,true);
	oHttpRequest.onreadystatechange=function() 
	{
		if (oHttpRequest.readyState==RS_COMPLETE)
		{ 
			if(oHttpRequest.status!=200) 
			{
				oCallback(false,oHttpRequest.responseText,oArguments);
				oHttpRequest=null;
				return;
			}
			oCallback(true,oHttpRequest.responseText,oArguments)
			oHttpRequest=null;
		}
	}
	oHttpRequest.send(null);
	return oHttpRequest;
}

//---
// Do an async RPC call using JSON as the encoding
// callback: function CallbackFunction(oResponse,oArguments)
// input: oRequest can be any JavaScript collection/object to be sent to server
// check oResponse.m_bSuccess for success(true/false)
//---
Ajax.Rpc=function(sUrl,oRequest,oCallback,oArguments)
{
	var oHttpRequest = Ajax.GetHttpRequestObject();
	
	oHttpRequest.open("POST",sUrl,true);
	oHttpRequest.onreadystatechange=function() 
	{
		if (oHttpRequest.readyState==RS_COMPLETE)
		{ 
			if(oHttpRequest.status!=200) 
			{
				var oResponse = new Object();
				oResponse.m_bSuccess=false;
				oResponse.m_sErrorMsg="HTTP Error:" + oHttpRequest.status + "\nURL:" + sUrl;
				oCallback(oResponse,oArguments);
				oHttpRequest=null;
				return;
			}

			var oResponse = JSON.FromString(oHttpRequest.responseText)
			if(oResponse==null || oResponse==false)
			{
				var oResponse = new Object();
				oResponse.m_bSuccess=false;
				oResponse.m_sErrorMsg="Could not complete the operation. Please make sure you have a valid internet connection.";
				oCallback(oResponse,oArguments);
				oHttpRequest=null;
				return;
			}
			oCallback(oResponse,oArguments)
			oHttpRequest=null;
		}
	}
	
	var sText = JSON.ToString(oRequest);
	oHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	oHttpRequest.send("oRequest=" + encodeURIComponent(sText));
	return oHttpRequest;
}

//---
// replace the innerHTML of an element with the specified text
//---
Ajax.SetText=function(sElementID,sText)
{
	var oElement = document.getElementById(sElementID);
	if(oElement==null)
	{
		alert("ERROR: element " + sElementID + " not found on page.");
		return;
	}
	try
	{
		oElement.innerHTML = sText;
	}
	catch(e)
	{
		AlertOnce("Ajax.SetText", "Could not complete the operation. Please make sure you have a valid internet connection.");
		document.location="/NmConsole/";
	}		
}

//---
// this is the callback used by Ajax.LoadElement() when the page finishes loading or when there is an error
//---
Ajax.CallbackSetElement=function(bSuccess,sPageText,sElementID)
{
	if(bSuccess)
	{
		Ajax.SetText(sElementID,sPageText);
		Ajax.ExecuteScripts(sPageText);
		return;
	}
	else
	{
		Ajax.SetText(sElementID,sPageText);
		return;
	}
}

//---
// finds all scripts within SCRIPT tags and calls eval() for each of them
//---
Ajax.ExecuteScripts=function(sHtml)
{
	var oMatch = sHtml.extractScripts();
	for (x = 0; x < oMatch.length; x++) 
	{
      //alert(oMatch[x]);
      eval(oMatch[x]);
    }
}

//---
// asynchronously loads a HTML page and replaces the HTML element with the result
//---
Ajax.LoadElement=function(sElementID,sUrl,sInitialText)
{
	if(sInitialText!=null)
	{
		Ajax.SetText(sElementID,sInitialText);
	}

	Ajax.Get(sUrl,Ajax.CallbackSetElement,sElementID);
}
