/*
*	CLASS XMLHTTP - Creates a wrapper around Microsoft's XMLHTTP Object and
*	Mozilla/Safari/Opera's XMLHttpRequest Object
*	
*	Author: Michael Honaker
*	Date: 11/19/2004
*/

// Constructor
function XMLHTTP()
{
	/* PUBLIC INTERFACE */
	// public ready state constants
	this.UNINITIALIZED = 0;
	this.LOADING = 1;
	this.LOADED = 2;
	this.INTERACTIVE = 3;
	this.COMPLETE = 4;
	
	// public add-on data elements
	this.isIE = (document.all) ? true : false;
	this.isMozilla = (window.XMLHttpRequest) ? true : false;
	
	// public wrapper methods and events
	this.send = xmlhttp_send;
	this.abort = xmlhttp_abort;
	this.open = xmlhttp_open;
	this.getAllResponseHeaders = xmlhttp_getallresponseheaders;
	this.getResponseHeader = xmlhttp_getresponseheader;
	this.setRequestHeader = xmlhttp_setrequestheader;
	this.onreadystatechange = xmlhttp_onreadystatechange;
	
	// public data wrapper members implemented as methods
	this.responseText = xmlhttp_responsetext;
	this.responseXML = xmlhttp_responsexml;
	this.readyState = xmlhttp_readystate;
	this.status = xmlhttp_status;
	this.statusText = xmlhttp_statustext;
	
	// public add-on data members implemented as methods
	this.responseXMLString = xmlhttp_responsexmlstring;
	
	/* PRIVATE MEMBERS */
	this.m_oXHTTP = false;
	
	// Initialize the respective browsers http request object
	if (this.isMozilla) // mozilla or safari
	{
		this.m_oXHTTP = new XMLHttpRequest();	
	} else if(this.isIE) {
		this.m_oXHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

//============================================================
// accessor data members
//============================================================
function xmlhttp_statustext()
{
	if("object" != typeof(this.m_oXHTTP)) return "";
	else return this.m_oXHTTP.statusText;
}
function xmlhttp_status()
{
	if("object" != typeof(this.m_oXHTTP)) return -1;
	else return this.m_oXHTTP.status;
}
function xmlhttp_readystate()
{
	if("object" != typeof(this.m_oXHTTP)) return -1;
	else return this.m_oXHTTP.readyState;
}
function xmlhttp_responsexml()
{
	if("object" != typeof(this.m_oXHTTP)) return "";
	else return this.m_oXHTTP.responseXML;
}
function xmlhttp_responsexmlstring()
{
	if("object" != typeof(this.m_oXHTTP)) return "";
	else return this.m_oXHTTP.responseXML.xml.toString();
}
function xmlhttp_responsetext()
{
	if("object" != typeof(this.m_oXHTTP)) return "";
	else return this.m_oXHTTP.responseText;
}

//============================================================
// events
//============================================================
function xmlhttp_onreadystatechange(fnRef) // expects you to pass a function reference
{
	if("object" != typeof(this.m_oXHTTP)) return false;
	// else
	this.m_oXHTTP.onreadystatechange = fnRef;
	return true;
}

//============================================================
// methods
//============================================================
function xmlhttp_setrequestheader(strLabel,strValue)
{
	if("object" != typeof(this.m_oXHTTP)) return false;
	else return this.m_oXHTTP.setRequestHeader(strLabel,strValue);
}
function xmlhttp_getresponseheader(strHeader)
{
	if("object" != typeof(this.m_oXHTTP)) return false;
	else return this.m_oXHTTP.getResponseHeader(strHeader);
}
function xmlhttp_getallresponseheaders()
{
	if("object" != typeof(this.m_oXHTTP)) return false;
	else return this.m_oXHTTP.getAllResponseHeaders();
}
function xmlhttp_open(sMethod,sURL)
{
	if("object" != typeof(this.m_oXHTTP)) return false;
	var argc = xmlhttp_open.arguments.length;
	var argv = xmlhttp_open.arguments;
	// open method can have up to 5 arguments (asyncflag defaults to true)
	// open(method, URL, asyncFlag, userName, password)
	switch(argc)
	{
		case 3:
			return this.m_oXHTTP.open(sMethod,sURL,argv[2].toString());
			break;
		case 4:
			return this.m_oXHTTP.open(sMethod,sURL,argv[2].toString(),argv[3].toString());
			break;
		case 5:
			return this.m_oXHTTP.open(sMethod,sURL,argv[2].toString(),argv[3].toString(),argv[4].toString());
			break;
		default:
			return this.m_oXHTTP.open(sMethod,sURL);
			break;
	}
}
function xmlhttp_abort()
{
	if("object" != typeof(this.m_oXHTTP)) return false;
	else return this.m_oXHTTP.abort();
}
function xmlhttp_send()
{
	if("object" != typeof(this.m_oXHTTP)) return false;
	// else
	if(xmlhttp_send.arguments.length == 1)
	{
		var strContent = xmlhttp_send.arguments[0].toString();
		return this.m_oXHTTP.send(strContent);
	} else return this.m_oXHTTP.send(null);
}