// XHI - Xml Http Interface
// Version: 1.1 - 2007-03-01
// Ástţór Ingi Pétursson (aip@aip.is)

// Originally by: Brad Fults (bfults@gmail.com)
// XHConn - http://xkr.us/code/javascript/XHConn/

// Code licensed under Creative Commons Attribution-ShareAlike License
// http://creativecommons.org/licenses/by-sa/3.0/

function XHI()
{
	// Public variables
	var url;
	var method;
	var parameters;
	var postback;

	// Private variables	
	var m_xhi;
	var m_complete = false;
	
	// Initalize the XMLHTTP object
	try { m_xhi = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { m_xhi = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { m_xhi = new XMLHttpRequest(); }
	catch (e) { m_xhi = false; }}}
	
	// The browser doesn't support XMLHTTP
	if (!m_xhi) return null;

	// connection function
	// Initalizes the connection
	this.connect = function()
	{
		// Don't do anything if the object hasn't been created
		if (!m_xhi) return false;
		
		// The request is not complete
		m_complete = false;

		// Format the method variable
		var method_mod = this.method.toUpperCase();
		
		try {
			if (method_mod == "GET")
			{
				var url_mod = this.url;
				
				if (this.parameters != "")
				{
					url_mod += "?" + this.parameters;
				}
				
				m_xhi.open(method_mod, url_mod, true);
			}
			else
			{
				m_xhi.open(method_mod, this.url, true);
				m_xhi.setRequestHeader("Method", "POST " + this.url + " HTTP/1.1");
				m_xhi.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}

			// Move the private variable into a local var
			// to be able to use it in the onreadystatechange function
			var postback_func = this.postback;
			
			m_xhi.onreadystatechange = function(){
				if (m_xhi.readyState == 4 && !m_complete)
				{
					m_complete = true;
					postback_func(m_xhi);
				}
			};

			m_xhi.send(this.parameters);
		}
		catch(z)
		{
			return false;
		}
		
		return true;
	};
	
	
	return this;
}

