//	'	<?xml version=""1.0"" encoding=""utf-8""?>
//	'	<ProtocolName action = "">
//	'	<error></error>
//	'	<errorDesc><![CDATA[]]></errorDesc>
//	'	</ProtocolName>

function clsXmlHttp()
{
	this.maxRequest = 10;

	this.arrXmlRequest = new Array();

	this.getXmlDom = function()
	{
    var xDoc = null;
    if (document.implementation && document.implementation.createDocument) {
        xDoc = document.implementation.createDocument("", "", null);
    } else {
        if ((typeof ActiveXObject) != "undefined") {
            var msXmlAx = null;
            try {
                msXmlAx = new ActiveXObject("Msxml2.DOMDocument");
            }
            catch (e) {
                msXmlAx = new ActiveXObject("Msxml.DOMDocument");
            }
            xDoc = msXmlAx;
        }
    }
    if (xDoc == null || typeof xDoc.load == "undefined") {
        xDoc = null;
    }
    return xDoc;
	};

	this.getXmlHttp = function()
	{
		for(var i=0;i<this.arrXmlRequest.length;i++)
		{
			if(typeof(this.arrXmlRequest[i]) == "" && this.arrXmlRequest[i].readyState == 4 && this.arrXmlRequest[i].status == 200)
			{
				return(this.arrXmlRequest[i]);
			}
		}
		if(this.arrXmlRequest.length > this.maxRequest)
		{
			return(null);
		}
		else
		{
			this.arrXmlRequest[this.arrXmlRequest.length] = this.createXMLHttpRequest();
			return(this.arrXmlRequest[this.arrXmlRequest.length-1]);
		}
	};

	this.createXMLHttpRequest = function()
	{
		var xmlHttp = null;
		if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		} else {
			var msxml = new Array("Msxml2.XMLHTTP.5.0", 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP');
			for(var n = 0; n < msxml.length; n++)
			{
				try
				{
					xmlHttp = new ActiveXObject(msxml[n]);
					break;
				}
				catch(e)
				{
					xmlHttp = null;
				}
			}
		}
		return(xmlHttp);
	};

	this.fetchUrl = function(strUrl,strUser,strPassword)
	{
		return(this.sendMessage("GET",strUrl,null,false,strUser,strPassword));
	};

	this.sendMessage = function(strMethod,strUrl,objMessageDom,blnAsync,strUser,strPassword,strReveiveFunction)
	{
		if(typeof(strUrl) == "undefined" || strUrl == null || strUrl.length == 0)
		{
			return(this.getResultDom(strMessageTitle,"1","Url Error"));
		}

		if(typeof(blnAsync) == "undefined" || blnAsync == null)
		{
			blnAsync = false;
		}

		if(blnAsync)
		{
			return(this.sendAsyncMessage(strMethod,strUrl,objMessageDom,strReveiveFunction,strUser,strPassword));
		}
		else
		{
			return(this.sendSyncMessage(strMethod,strUrl,objMessageDom,strUser,strPassword));
		}
	};

	this.sendAsyncMessage = function(strMethod,strUrl,objMessageDom,strReveiveFunction,strUser,strPassword)
	{
		var objXmlHttp = this.getXmlHttp();
		try
		{
			if(typeof(strUser) == "undefined" || strUser == null || strUser.length ==0)
			{
				objXmlHttp.open(strMethod, strUrl, true);
			}
			else
			{
				objXmlHttp.open(strMethod, strUrl, true,strUser,strPassword);
			}

			objXmlHttp.onreadystatechange = function()
		  {
		  	if(objXmlHttp.readyState == 4)
		  	{
			  	if(objXmlHttp.status == 200)
					{
						strReveiveFunction(objXmlHttp);
					}
				}
		  };

			if(typeof(objMessageDom) == "undefined" || objMessageDom == null)
			{
				objXmlHttp.send(null);
			}
			else
			{
				objXmlHttp.send(objMessageDom);
			}
		}
		catch(e)
		{
			objXmlHttp = null;
		}
	};

	this.sendSyncMessage = function(strMethod,strUrl,objMessageDom,strUser,strPassword)
	{
		var strMessageTitle = "protocol";

		var objXmlHttp = this.getXmlHttp();
		try
		{
			if(typeof(strUser) == "undefined" || strUser == null || strUser.length ==0)
			{
				objXmlHttp.open(strMethod, strUrl, false);
			}
			else
			{
				objXmlHttp.open(strMethod, strUrl, false,strUser,strPassword);
			}

			if(typeof(objMessageDom) == "undefined" || objMessageDom == null)
			{
				objXmlHttp.send(null);
			}
			else
			{
				objXmlHttp.send(objMessageDom);
				strMessageTitle = objMessageDom.documentElement.nodeName;
			}
			return(objXmlHttp.responseXML);
			objXmlHttp = null;
		}
		catch(e)
		{
			objXmlHttp = null;
			return(this.getResultDom(strMessageTitle,"1",e.toString()));
		}

		return(objReturnDom);
	};

	this.getResultDom = function(strMessageTitle,strError,strErrorDesc)
	{
		var objResultDom = this.getProtocolDom(strMessageTitle);
		objResultDom.documentElement.setAttribute("action","result");
		objResultDom.documentElement.selectSingleNode("error").text = strError;
		objResultDom.documentElement.selectSingleNode("errorDesc").childNodes[0].text = strErrorDesc;
		return(objResultDom);
	};

	this.getProtocolDom = function(strMessageTitle)
	{
		var objProtocolDom = this.getXmlDom();
		var strProtocalStr = '<?xml version="1.0" encoding="gb2312"?>';
		strProtocalStr += '<'+strMessageTitle+' action=""><error>0</error><errorDesc><![CDATA[]]></errorDesc></'+strMessageTitle+'>';
		objProtocolDom.loadXML(strProtocalStr);
		return(objProtocolDom);
	};
}
