/******************************************************************************* 
ALSPA AjaxConnection - XMLHTTP Application Prorgam Interface
Copyright (c) 2005. ALSPA Consulting Corp. All Rights Reservered.
Author: Michael S. Kolias - mikek@alspaconsulting.com

///COMMENTS
0 - Uninitialized - open() has not been called yet.
1 - Loading - send() has not been called yet.
2 - Loaded - send() has been called, headers and status are available.
3 - Interactive - Downloading, responseText holds the partial data.
4 - Completed - Finished with all operations.
********************************************************************************/

function AjaxConnection()
{
	var _host = "http://localhost"; //The host to submit the request.
	var _method = "POST";		    //Method of the request (POST or GET);
	var _parameters = "";
	this.ReadyState = null;
	this.Status = null;

	var xmlhttp;
	var complete = false;
	
	//Try to create the XmlHttp object.
	xmlhttp = GetXmlHttpObject();

		if (!xmlhttp) return null; //Failed to create XmlHttp object.
		
		this.Connect = function(fnCallBack)
		{
			if (_method != "POST" && _method != "GET") _method = "POST";

			try
			{
				if (_method == "POST")
				{
					xmlhttp.open(_method, _host, true);
	        		xmlhttp.setRequestHeader("Method", "POST " + _host + " HTTP/1.1");
  	      			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				}
				
				else
				{
					var url = _host + "?" + _parameters;				
					xmlhttp.open(_method, url, true);
					_parameters = "";
				}
				
				xmlhttp.onreadystatechange = function()
				{
					switch(xmlhttp.readyState)
					{
						case 1:
							break;
							
						case 2:
							break;
							
						case 3:
							break;
							
						case 4:
							if (!complete) 
							{ 
								complete = true;
								fnCallBack(xmlhttp);
							}
							break;						
					}
				};
				
				 xmlhttp.send(_parameters);
			}
			
			catch(ex) { alert(ex); return false; }
		};
		
		this.SetHost = function(host) { _host = host };
		this.SetParameters = function(params) { _parameters = params };
		this.SetMethod = function(method) { _method = method };
}




function GetXmlHttpObject()
{
	var theObject;
	
	try { theObject = new ActiveXObject("Msxml2.XMLHTTP"); }
	
	catch (ex)
	{ 
		try { theObject = new ActiveXObject("Microsoft.XMLHTTP"); }
		
		catch (ex) 
		{
			try { theObject = new XMLHttpRequest(); }
	
			catch (ex) { theObject = false; }	
		}
		
	}
	
	return theObject;
}