﻿var ctrlToEnable = null;
var ctrlToEnableText = null;

function ASYNC_GetXMLHttpRequest()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		if (window.ASYNC_XMLHttpRequestID)
		{
			return new ActiveXObject(window.ASYNC_XMLHttpRequestID);
		}
		else
		{
			var rgprogID = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
			for (var i=0;i<rgprogID.length;i++)
			{
				var progID = rgprogID[i];
				try
				{
					var x = new ActiveXObject(progID);
					window.ASYNC_XMLHttpRequestID = progID;
					return x;
				}
				catch(e)
				{
				}
			}
		}
	}
}

function ASYNC_Receive(http_request)
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			var xmldoc = http_request.responseXML;
			if (!xmldoc)
			{
				alert('xmldoc is null - '+http_request.responseText);
				return;
			}
			if (ctrlToEnable != null)
			{
				ctrlToEnable.disabled = false;
				if (ctrlToEnableText != null)
				{
					if (ctrlToEnable.nodeName == 'INPUT')
					{
						ctrlToEnable.value = ctrlToEnableText;
 					}
	 				else
		 			{
			 			ctrlToEnable.innerHTML = ctrlToEnableText;
					}
				}
				ctrlToEnable = null;
			}
			var PageRed = xmldoc.getElementsByTagName('Redirect');
			var submitForm = true;
			if (PageRed != null)
			{
				if (PageRed[0] != null)
				{
					if (PageRed[0].firstChild != null)
					{
					    var redVal = PageRed[0].firstChild.xml;
					    if (redVal == null)
					    {
					        redVal = PageRed[0].firstChild.nodeValue;
					    }
						var r1 = /<!\[CDATA\[/g;
						var r2 = /\]\]>/g;
						redVal = redVal.replace(r1,"");
						redVal = redVal.replace(r2,"");
						window.location = redVal;
						submitForm = false;
					}
				}
			}
			var Popup = xmldoc.getElementsByTagName('Popup');
			if (Popup.length > 0)
			{
				var pop = Popup[0];
				var popTitle = pop.getAttribute("name");
				var popURL = pop.firstChild.xml;
				window.open(popURL,popTitle,'height=600,width=640,status=0,toolbar=0,menubar=0,directories=0,location=0');
			}
			var items = xmldoc.getElementsByTagName('item');
			if (items.length == 0)
			{
				if (submitForm == true)
				{
					alert('No Items - '+http_request.responseText);
					document.forms[0].submit();
				}
			}
			if (items.length > 0)
			{
				for(var i=0;i<items.length;i++)
				{
					var item = items[i];
					if (item == null)
					{
						alert('Item was null at position '+i);
						continue;
					}
					var itemName = item.getAttribute("name");
					var itemClear = item.getAttribute("clear");
					// TODO: add ability to Change Item Attributes here!!
					var element = document.getElementById(itemName);
					if (element == null)
					{
						alert('An element named '+itemName+' was not found on the page');
						continue;
					}
					if (item.firstChild == null)
					{
						alert('item does not have a first child at position '+i);
						continue;
					}
					///FIREFOX BREAKS ON THIS STATEMENT!!
					var newValue = item.firstChild.xml;
					if (newValue == null)
					{
						///This statement added to placate Firefox
						newValue = item.firstChild.nodeValue;
					}
					if (newValue != null)
					{
						var r1 = /<!\[CDATA\[/g;
						var r2 = /\]\]>/g;
						newValue = newValue.replace(r1,"");
						newValue = newValue.replace(r2,"");
					}
					if (element.nodeName == 'INPUT')
					{
						element.value = newValue;
					}
					else
					{
						if (newValue == '#BLANK#')
						{
							element.innerHTML=' ';
							element.visible=false;
						}
						else
						{
							element.visible=true;
							element.innerHTML = newValue;
						}
					}
					if (itemClear=='yes')
					{
						document.forms(0).reset();
					}
				}
			}
			var loading = document.getElementById("loading");
			if (loading != null)
			{
				document.body.removeChild(loading);
			}
		}
		else
		{
			var txtResponse = http_request.responseText;	
			if ((txtResponse == null) || (txtResponse.Length=0))
			{
				txtResponse="<h1>System Error</h1>An unrecoverable error has occured, please click the refresh button";
			}
			var ajaxError = document.getElementById("ajaxError");
			if (ajaxError != null)
			{
				ajaxError.innerHTML = txtResponse;
			}
			var loading = document.getElementById("loading");
			if (loading != null)
			{
				document.body.removeChild(loading);
			}
		}
	}
}

function ASYNC_ProcessGet(url)
{
	var http_request = ASYNC_GetXMLHttpRequest();
	if (http_request == null)
	{
		alert("Can't get XMLHttpRequest Object");
		return null;
	}
	//http_request.overrideMimeType('text/xml');
	http_request.onreadystatechange=function(){ASYNC_Receive(http_request);};
	http_request.open("GET",url,true);
	http_request.send(null);
}
function ASYNC_ProcessPostDisable(ctrl,text,url,params)
{
	ctrlToEnable = ctrl;
	if (ctrl.nodeName == 'INPUT')
	{
		if ((ctrl.getAttribute("TYPE").toUpperCase() == "RADIO") || (ctrl.getAttribute("TYPE").toUpperCase() == "CHECKBOX"))
		{
		}
		else
		{
			ctrlToEnableText = ctrl.value;
			ctrl.value = text;
		}
	}
	else if (ctrl.nodeName == 'SELECT')
	{
		ctrlToEnableText = null;
	}
	else
	{
		ctrlToEnableText = ctrl.innerHTML;
		ctrl.innerHTML = text;
	}
	ctrlToEnable.disabled = true;
	ASYNC_ProcessPost(url,params);
}
function CreateWait()
{
	var loading = document.createElement("div");
			loading.id = "loading";
			loading.style.paddingLeft = "5px";
			loading.style.paddingRight = "5px";
			loading.style.position = "absolute";
			loading.style.left = 450;
			loading.style.top = 350;
			loading.style.zIndex = "9999";
			loading.innerHTML = "<img src='Layout/_Images/Loading.gif' alt='Loading...'>";
			document.body.appendChild(loading);
	return loading;
}

function ASYNC_ProcessPostWaitDisable(ctrl,text,url,params)
{
	CreateWait();
	ASYNC_ProcessPostDisable(ctrl,text,url,params);
}
function ASYNC_ProcessPostWait(url,params)
{
	CreateWait();
	ASYNC_ProcessPost(url,params);
}
function ASYNC_ProcessPost(url,params)
{
	var http_request = ASYNC_GetXMLHttpRequest();
	if (http_request == null)
	{
		alert("Can't get XMLHttpRequest Object");
		return null;
	}
	http_request.onreadystatechange=function(){ASYNC_Receive(http_request);};
	try
	{
		http_request.open("POST",url,true);
	}
	catch(e)
	{
		alert('Error Connecting to '+url+': '+eval(e));
		var loading = document.getElementById("loading");
		if (loading != null)
		{
			document.body.removeChild(loading);
		}
		return;
	}	
	//application/x-www-form-urlencoded
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	var encodedData = params;
	var form = document.forms[0];//getElementById("EngineForm");
	if (form == null)
	{
		alert('Cannot find form');
	}
	else
	{
		for (var elementIndex = 0; elementIndex < form.length;elementIndex++)
		{
			var element = form.elements[elementIndex];
			if (element.name)
			{
				var elementValue = null;
				if (element.nodeName=="INPUT")
				{
					var inputType = element.getAttribute("TYPE").toUpperCase();
					if (inputType == "TEXT" || inputType=="PASSWORD")
					{
						elementValue = element.value;
					}
					else if (inputType == "HIDDEN")
					{
						elementValue = element.value;
					}
					else if (inputType == "RADIO" || inputType == "CHECKBOX")
					{
						if (element.checked)
						{
							elementValue = element.value;
						}
					}
				}
				else if (element.nodeName == "SELECT")
				{
					if (element.multiple)
					{
						elementValue = [];
						for (var i=0;i<element.length;i++)
						{
							if (element.options[i].selected)
							{
								elementValue.push(element.options[i].value);
							}
						}
					}
					else
					{
						elementValue = element.value;
						if (elementValue == "")
						{
							elementValue = null;
						}
					}
				}
				else if (element.nodeName == "TEXTAREA")
				{
					elementValue = element.value;
				}
				else if (element.nodeName == "OBJECT")
				{
					elementValue='';
				}
				else
				{
					//alert('Unknown Element Type : '+element.nodeName);
					elementValue='';
				}
				if (elementValue instanceof Array)
				{
					for (var i=0;i<elementValue.length;i++)
					{
						encodedData += "&"+element.name+"="+encodeURIComponent(elementValue);
					}
				}
				else if (elementValue != null)
				{
					encodedData +="&"+element.name + "=" + encodeURIComponent(elementValue);
				}
			}
		}
	}
	http_request.send(encodedData);
}