//(c) S.A. Lowell - http://www.DarkStormWorld.com

var g_XMLHTTP_List = [];

//*******************
function XMLHTTP()
{
	var m_TargetElement   = null;
	var m_TargetFileName  = null;
	var m_URL_Value       = "";
	var m_xmlhttp         = null;
	var m_ErrorCodeLength = 0;
	var m_ErrorCodeNumber = null;
	var xmlAtoLst         = [];
	var XMLHTTP_POST      = false;

	setXMLHTTP_Obj();
	//*******************
	function setXMLHTTP_Obj()
	{
		if(window.XMLHttpRequest)
		{
			m_xmlhttp = new XMLHttpRequest();
		}
		else
		{
			m_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	//*******************
	this.abort = function()
	{
		m_xmlhttp.abort();
	}
	//*******************
	this.addObject = function(objToAdd, funcCall, readyStateCall, numberCodeCall)
	{
		var XML_ToAdd               = [];
		XML_ToAdd[0]                = readyStateCall;
		XML_ToAdd[1]                = numberCodeCall;
		XML_ToAdd[2]                = objToAdd;
		XML_ToAdd[3]                = funcCall;
		xmlAtoLst[xmlAtoLst.length] = XML_ToAdd;
	}
	//*******************
	this.addCode = function(codeToExecute, readyStateCall, numberCodeCall)
	{
		var codeToAdd               = [];
		codeToAdd[0]                = readyStateCall;
		codeToAdd[1]                = numberCodeCall;
		codeToAdd[2]                = codeToExecute;
		xmlAtoLst[xmlAtoLst.length] = codeToAdd;
	}
	//*******************
	function exeXML_List(readyState)
	{
		for(var index = 0; index < xmlAtoLst.length; ++index)
		{
			if(xmlAtoLst[index][0] === readyState)
			{
				if(xmlAtoLst[index][1] === false || xmlAtoLst[index][1] == m_ErrorCodeNumber)
				{
					if(dataType(xmlAtoLst[index][2]) === "String")
					{
						eval(xmlAtoLst[index][2]);
					}
					else if(dataType(xmlAtoLst[index][2]) !== "null" && dataType(xmlAtoLst[index][2]) !== "undefined")
					{
						eval("xmlAtoLst[index][2]." + xmlAtoLst[index][3]);
					}
				}
			}
		}
	}
	//*******************
	function XMLHTTP_State()
	{
		var responseText  = "";
		m_ErrorCodeNumber = "";

		if(m_xmlhttp.readyState === 4 && m_xmlhttp.status === 200)
		{
			for(var index = 0; index < m_xmlhttp.responseText.length; ++index)
			{
				if(m_xmlhttp.responseText.charAt(index) === "/" && m_xmlhttp.responseText.charAt(index + 1) === "!" && m_xmlhttp.responseText.charAt(index + 2) === "/")
				{
					m_ErrorCodeLength = index;
					break;
				}
			}
 
			for(var index = 0; index < m_ErrorCodeLength; ++index)
			{
				m_ErrorCodeNumber += m_xmlhttp.responseText.charAt(index);
			}

			if(m_ErrorCodeLength > 0)
			{
				for(var index = m_ErrorCodeLength + 3; index < m_xmlhttp.responseText.length; ++index)
				{
					responseText += m_xmlhttp.responseText.charAt(index);
				}
			}
			else
			{
				responseText = m_xmlhttp.responseText;
			}

			document.getElementById(m_TargetElement).innerHTML = responseText;
			exeXML_List(m_xmlhttp.readyState);
		}
		else if(m_xmlhttp.readyState === 3)
		{
			exeXML_List(m_xmlhttp.readyState);
		}
		else if(m_xmlhttp.readyState === 2)
		{
			exeXML_List(m_xmlhttp.readyState);
		}
		else if(m_xmlhttp.readyState === 1)
		{
			exeXML_List(m_xmlhttp.readyState);
		}
		else if(m_xmlhttp.readyState === 0)
		{
			exeXML_List(m_xmlhttp.readyState);
		}
	}
	//*******************
	this.addToURL_Send = function(arraySend)
	{
		var urlValue = "";

		if(m_URL_Value !== "")
		{
			if(m_URL_Value.length > 0)
			{
				m_URL_Value += "&";
			}
		}

		if(arraySend.length > 0)//Detect if the array is indexed via int!
		{
			for(var index = 0; index < arraySend.length; ++index)
			{
				if(arraySend[index])
				{
					if("name" in arraySend[index] && "value" in arraySend[index])
					{
						if(urlValue !== "")
						{
							if(urlValue.length > 0)
							{
								urlValue += "&";
							}
						}

						if(arraySend[index].type === "checkbox")
						{
							if(arraySend[index].checked === true)
							{
								urlValue += arraySend[index].name.replace(/&/g, "%26");
								urlValue += "=" + arraySend[index].value.replace(/&/g, "%26");
							}
						}
						else
						{
							urlValue += arraySend[index].name.replace(/&/g, "%26");
							urlValue += "=" + arraySend[index].value.replace(/&/g, "%26");
						}
					}
				}
			}
		}
		else//array is indexed associatively!
		{
			for(index in arraySend)
			{
				if(urlValue !== "")
				{
					if(urlValue.length > 0)
					{
						urlValue += "&";
					}
				}

				urlValue += index.replace(/&/g, "%26");
				urlValue += "=" + arraySend[index].replace(/&/g, "%26");
			}
		}

		if(m_URL_Value !== "")
		{
			if(m_URL_Value.length > 0)
			{
				m_URL_Value += "&";
			}
		}

		m_URL_Value += urlValue;
	}
	//*******************
	//filename: the file to send all the data to
	//targetElement: the ID of the HTML element to send all the responseText data to.
	//arrayPost: the array of data to post to fileName.
	this.setXMLHTTP_Post = function(fileName, targetElement, arrayPost)
	{
		m_TargetElement  = targetElement;
		m_TargetFileName = fileName;

		this.addToURL_Send(arrayPost);
	}
	//*******************
	this.exeXMLHTTP_Post = function()
	{
		XMLHTTP_State();//Force state 0.  This shouldn't be necessary. =/
		m_xmlhttp.open("POST", m_TargetFileName, true);
		//m_xmlhttp.onreadystatechange = XMLHTTP_State;

		m_xmlhttp.onreadystatechange = function()
		{
			XMLHTTP_State();
		}

		m_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		m_xmlhttp.setRequestHeader("Content-length", m_URL_Value.length);
		m_xmlhttp.setRequestHeader("Connection", "close");
		m_xmlhttp.send(m_URL_Value);

		XMLHTTP_State();//Force state 1.  This shouldn't be necessary. =/
	}
}
