﻿//Array To hold the remote scripting objects
var AJAXGlobal = new Array();

// function that initializes the AJAX Object
function InitializeAJAXObj()
{
	//XMLHttpObject 
	var RemoteObj;
	if(window.XMLHttpRequest) // NON - IE Browsers
	{
		RemoteObj = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) // IE Browser
	{
		RemoteObj = new window.ActiveXObject("Microsoft.XMLHTTP"); 
	}
	return RemoteObj;
}

//Object to store the remote object along with other information
function AJAXObject(AJAXObj, RespFunction, url, TargtId, ErrorFunction, isPST, isXmlval, isFrame, nameValPairArray)
{
	this.AJAXObject = AJAXObj;				// main object
	this.ResponseFunction = RespFunction;	// function to call after it initializes
	this.TargetId = TargtId;				// Target Id if applicable
	this.isPOST = isPST;					// POST/GET
	this.errorHandler = ErrorFunction;		// function to call if something is wrong
	this.Url = url;							// url to query
	this.isFrame = isFrame;
	this.Initialized = false;
	this.Finished = false;
	this.NameValpair = nameValPairArray;
	this.toString = function(){
		var strRet = "URL: "+ this.Url +"\n"+ 
					 "TargetId: "+ this.TargetId +"\n"+ 
					 "isPOST: "+ this.isPOST +"\n"+ 
					 "isXML: "+ this.isXML +"\n"+ 
					 "respFunction: "+ this.ResponseFunction;  
	    return strRet;  
	};
	this.isXML = isXmlval;
}

// function that gets called after the remote object completes the request
function AJAXGetResponse(UniqueId)
{
	// check if we have necessary objects
	if(	AJAXGlobal[UniqueId] &&
		AJAXGlobal[UniqueId].AJAXObject &&
		AJAXGlobal[UniqueId].AJAXObject.readyState == 4 // state becomes 4 when script calls back
		)
	{
		var worked = false;
	//	try
		{
			if(	AJAXGlobal[UniqueId].AJAXObject.status == 200) // there is no error
			{
				//alert(AJAXGlobal[UniqueId].AJAXObject.responseXml.xml);
				
				if(AJAXGlobal[UniqueId].isXML && AJAXGlobal[UniqueId].AJAXObject.getResponseHeader("Content-Type") == "text/xml")
				{
					AJAXGlobal[UniqueId].ResponseFunction(AJAXGlobal[UniqueId].AJAXObject.responseXML, UniqueId);
				}
				else
					AJAXGlobal[UniqueId].ResponseFunction(AJAXGlobal[UniqueId].AJAXObject.responseText, UniqueId);

				worked = true;
			}
		}
	//	catch(e)
		{
		
			
		}
		if(!worked)
		{
			AJAXGlobal[UniqueId].errorHandler(UniqueId);
		}
		delete AJAXGlobal[UniqueId];
	}

}

function HandleError(UniqueId)
{
	
	if(AJAXGlobal[UniqueId] && AJAXGlobal[UniqueId].TargetId != 'SENDING_REMOTE_ERROR') /* make sure we are not sending an error that happened while sending error */ 
	{
	//	var Arr = new Array();
	//	Arr["AJXValue"] = "Remote Scripting Error :\n" + AJAXGlobal[UniqueId].toString();
	//	SendAJAXCommand('RemoteScriptingObject1_Error', 'Errors/CatchJscriptError.aspx', function(str){}, 'SENDING_REMOTE_ERROR', true, Arr)
	//	alert("Errored "+ UniqueId +"\n"+ AJAXGlobal[UniqueId].AJAXObject.responseText);
	}
}
var frameInitialized = false;

function AjaxResultsFromFrame(UniqueId)
{
	if(AJAXGlobal[UniqueId] && AJAXGlobal[UniqueId].Initialized)
	{
		if(window.frames["AJAXHiddenFrame"+ UniqueId])
		{
			var doc = window.frames["AJAXHiddenFrame"+ UniqueId].document;
			AJAXGlobal[UniqueId].ResponseFunction(doc.body.innerHTML, UniqueId);
			AJAXGlobal[UniqueId].Finished = true;
			var frm = document.getElementById("AJAXHiddenFrameId_"+ UniqueId);
			if(frm)
				document.body.removeChild(frm);
		}
		delete AJAXGlobal[UniqueId];
		
	}
	
	
}

var AJAXsavedRequest = new Array();

function CreateAndUseHiddenFrame(UniqueId, url, RespFunction, TargtId, isPost, nameValPairArray, isXML)
{
	var proto = location.href.toLowerCase().indexOf("https:") > -1 ? "https:" : "http:";
	var domain = location.hostname;
	var FrameName = "AJAXHiddenFrame"+ UniqueId;
	
	var strNewFrame = "<IFRAME id='AJAXHiddenFrameId_"+ UniqueId +"' src='"+ proto +"//"+ domain +"/Blank.htm' height=0 width=0 onload='AjaxResultsFromFrame(\""+ UniqueId +"\")' name='"+ FrameName +"'></IFRAME>";
	document.body.insertAdjacentHTML("beforeEnd", strNewFrame);	
	
	url = ((url.indexOf("?") > 0)? url +"&" : url +"?") + "AddBody=<BODY>";
	var doc = window.frames[FrameName].document;
	doc.open();
	doc.write("<body>");
	doc.write("<form id='AJAXMethodForm' method=POST action='"+  url +"'>");
	if(isPost && nameValPairArray != null)
	{
		for(key in nameValPairArray)
		{
			doc.write("<input type='hidden' name='"+ key +"' value='"+ escape(nameValPairArray[key]) +"'>");
		}
		doc.write("<input type='hidden' name='AddBody' value='<body>'>");
	}	
	doc.write("</form>");
	doc.write("</body>");
	doc.close();
	
	var objForm = doc.getElementById("AJAXMethodForm");
	var ajxObj = new AJAXObject(null, RespFunction, url, TargtId, HandleError, true, isXML, true);
	AJAXGlobal[UniqueId] = ajxObj;
	AJAXGlobal[UniqueId].Initialized = true;
	objForm.submit();
	
	
	
}

function SendAJAXCommand(UniqueId, url, RespFunction, TargtId, isPost, nameValPairArray, isXML)
{
	var RemoteObj = null;
	url = ((url.indexOf("?") > 0)? url +"&" : url +"?") + (new Date()).getTime();
	
	try{
		RemoteObj = InitializeAJAXObj();
	}
	catch(e)
	{
		
	}
	if(typeof(isXML) == "undefined")
		isXML = false;

	if(!RemoteObj)
	{
		CreateAndUseHiddenFrame(UniqueId, url, RespFunction, TargtId, isPost, nameValPairArray, isXML);
		return;
	}
	
	if(AJAXGlobal[UniqueId] != null)
	{
	    AJAXGlobal[UniqueId].AJAXObject.abort( );
	    delete AJAXGlobal[UniqueId];
	}
	
	if(RemoteObj && AJAXGlobal[UniqueId] == null)
	{
		var ajxObj = new AJAXObject(RemoteObj, RespFunction, url, TargtId, HandleError, isPost, isXML, false);
		
		AJAXGlobal[UniqueId] = ajxObj;
		AJAXGlobal[UniqueId].AJAXObject.onreadystatechange = new Function("AJAXGetResponse('"+ UniqueId +"')");
		if(isPost)
		{
			var str = "";
			try
			{
				for(key in nameValPairArray)
				{
					str += ((str == "")? "" : "&") + key +"="+ escape(nameValPairArray[key]);
				}
				str += ((str == "")? "" : "&") +"AddBody= ";
			}
			catch(e)
			{
				delete AJAXGlobal[UniqueId];
				return;
			}
			AJAXGlobal[UniqueId].AJAXObject.open("POST", url , true);
			AJAXGlobal[UniqueId].AJAXObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			AJAXGlobal[UniqueId].AJAXObject.send(str);
		}
		else
		{
			AJAXGlobal[UniqueId].AJAXObject.open("GET", url , true);
			AJAXGlobal[UniqueId].AJAXObject.send(null);
		}
	}
	else
	{
		
	}

}
