/*############################################################################################
##									HTTPRequest  CLASS  									##
############################################################################################*/

// Constructor -------------------------------------------------------------------
function HTTPRequest ()
{
	this.CreateXMLHTTPRequest();
	this.m_RequestID = ++HTTPRequest.RequestID;
	
	if(HTTPRequest.DEBUG)
	{
		this.m_DebugLayer						= document.createElement("div");
		this.m_DebugLayer.id					= this.m_RequestID;
		this.m_DebugLayer.className				= "label";
		this.m_DebugLayer.style.backgroundColor = "#EFEFEF";
		this.m_DebugLayer.style.zIndex			= this.m_RequestID+1;
		this.m_DebugLayer.style.width			= 450;
		this.m_DebugLayer.style.height			= 20;
		this.m_DebugLayer.style.position		= "absolute";
		this.m_DebugLayer.style.left			= 460;
		this.m_DebugLayer.style.top				= HTTPRequest.RequestID*45;
		document.body.appendChild(this.m_DebugLayer);
		this.Trace("Ready");
	}
};

// Static properties -------------------------------------------------------------
HTTPRequest.RequestID		= 0;
HTTPRequest.DEBUG			= false;

// Instance properties -----------------------------------------------------------
HTTPRequest.prototype.m_XHR			= null;
HTTPRequest.prototype.m_DebugLayer	= null;
HTTPRequest.prototype.m_RequestID	= 0;
HTTPRequest.prototype.m_PostID		= 0;
HTTPRequest.prototype.m_URL			= "";
HTTPRequest.prototype.m_Method		= "POST";
HTTPRequest.prototype.m_UseXML		= true;

//error Handler
HTTPRequest.prototype.m_ErrorHandlerScope = null;
HTTPRequest.prototype.m_ErrorHandlerMethod	= null;

// Instance methods --------------------------------------------------------------
HTTPRequest.prototype.Trace = function (s)
{
	this.m_DebugLayer.innerHTML += "[SR:"+this.m_RequestID+"_"+this.m_PostID+"] : "+s;
};

HTTPRequest.prototype.GetStatus = function (readySate)
{
	switch(readySate)
	{
		case 0:
			return "Uninitialised";
		case 1:
			return "Loading";
		case 2:
			return "Loaded";
		case 3:
			return "Interactive";
		case 4:
			return "Complete";
		default:
			return "Unknown state";
	}
};

HTTPRequest.prototype.Send	= function(){alert("Could not create XMLHTTPRequest");};

HTTPRequest.prototype.SendIE = function (url,data,scope,handler)
{
	this.m_XHR.Abort();
	if(HTTPRequest.DEBUG) this.Trace("Send:"+url);
	this.m_PostID++;
	this.m_URL = url;
	this.m_XHR.Open(this.m_Method,url,true);
	this.m_XHR.onreadystatechange = this.GetResponseHandler(this.m_XHR,this,scope,handler);
	this.m_XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	this.m_XHR.Send(this.SerializeObject(data));
};
HTTPRequest.prototype.SendMZ = function (url,data,scope,handler)
{
	delete this.m_XHR.onreadystatechange;
	this.m_XHR.abort();
	if(HTTPRequest.DEBUG) this.Trace("Send:"+url);
	this.m_PostID++;
	this.m_URL = url;
	this.m_XHR.open(this.m_Method,url,true);
	this.m_XHR.onreadystatechange = this.GetResponseHandler(this.m_XHR,this,scope,handler);
	this.m_XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	this.m_XHR.send(this.SerializeObject(data));
};
HTTPRequest.prototype.Dispose = function ()
{
	this.m_XHR = null;
	delete this.m_XHR;
};
HTTPRequest.prototype.GetResponseHandler = function (req,sr,scope,handler)
{
	return function ()
	{
		if(HTTPRequest.DEBUG) sr.Trace(sr.GetStatus(req.readyState));
		if(req.readyState==4)
		{
			if(HTTPRequest.DEBUG) sr.Trace("HTTP Status:"+req.status);
			if(req.status==200)
			{
				var r = sr.m_UseXML ? req.responseXML : req.responseText;
				//var r = this.m_UseXML ? req.responseXML : req.responseText;
				if(HTTPRequest.DEBUG) sr.Trace("Complete:"+typeof(r));
				scope[handler](r);
			} else {
				if(sr.m_ErrorHandlerScope!=null)sr.m_ErrorHandlerScope[sr.m_ErrorHandlerMethod](req.status);
				if(HTTPRequest.DEBUG) sr.Trace(req.status+ " error");
			}
		}
	}
}

HTTPRequest.prototype.GetResponseXML = function ()
{
	return this.m_XHR.status==200? this.m_XHR.responseXML : null;
}

HTTPRequest.prototype.SerializeObject = function (o)
{
	if(o==null)return null;
	var s = "";
	var d = "";
	for(var each in o)
	{
		s+=d+each+"="+o[each];
		d = "&";
	}
	return s;
};
HTTPRequest.prototype.CreateXMLHTTPRequest = function()
{
	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;

	if(isIE&&isWin)
	{
		try
		{
			this.m_XHR = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			this.Send	= this.SendIE;
		}
		catch(e)
		{
			try
			{
				this.m_XHR	= new ActiveXObject("Microsoft.XMLHTTP");
				this.Send	= this.SendIE;
			}
			catch(e)
			{
			}
		}
	}
	else
	{
		try
		{
			//netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			this.m_XHR	= new XMLHttpRequest();
			this.Send	= this.SendMZ;
		}
		catch(e)
		{
			alert(e);
		}
	}
};

HTTPRequest.prototype.SetErrorHandler = function (scope,handler)
{
	this.m_ErrorHandlerScope = scope;
	this.m_ErrorHandlerMethod = handler;
}