
function XMLHttp()
{//CR 1668872 V82 Evaluate IE7
	if (window.XMLHttpRequest){
         // If IE7, Mozilla, Safari, etc: Use native object
         	return new XMLHttpNS();
    }
   else {
					if (window.ActiveXObject){
	     			// ...otherwise, use the ActiveX control for IE5.x and IE6
                return new XMLHttpIE();
             }
   }

}

//CR 1668872 V82 Evaluate IE7 : Ie7 don't support Domparser object. 
function createXMLFromStringByActiveX (string)
	{
  		var xmlDocument;
  		try 
		{
    		xmlDocument = new ActiveXObject('MSXML.DOMDocument');
    		xmlDocument.async = false;
    		xmlDocument.loadXML(string);
    		return xmlDocument;
  		}
 		catch (e)
	 	{
    		alert("Can't create XML document:\nMSXML.DOMDocument\n"+e.description);
    		return null;
  		}
	}
	
function XMLHttpIE()
{
	this.fnCallback=null;
	this.resultBuf=null;
	this.httpRequest=null;
	this.callerObject=null;
	
  XMLHttpIE.prototype.createXMLFromString = function (string)
	{
  		createXMLFromStringByActiveX (string);
	}
	
	XMLHttpIE.prototype.serializeXML = function  (xmlDocument)
	{
  		return xmlDocument.xml;
	}


   XMLHttpIE.prototype.postXML = function (url, xmlDocument, async)
	{
  		var httpRequest;
  		try
		{
    		httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
    		httpRequest.open('POST', url, async);
    		httpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
 			httpRequest.send(xmlDocument);
    		return httpRequest;
  		}
  		catch (e)
		{
			if (this.callerObject && this.callerObject.errorHandler) {
				var errMsg = new Error ("Can't post XML document:\nMicrosoft.XMLHTTP\n"+e.description);
				this.callerObject.errorHandler(errMsg);	
			} else {
				alert("Can't post XML document:\nMicrosoft.XMLHTTP\n"+e.description);
			}
    		return null;
  		}
	}	
	
	XMLHttpIE.prototype.quickCall = function (url, param)
	{
		var httpRequest = this.postXML(url, this.createXMLFromString("<param>"+param+"</param>"), false);
		var xmlResponse = httpRequest.responseXML;
		
		try
		{
			return xmlResponse.documentElement.childNodes[0].data;
		}catch(ex)
		{
			return null;
		}
	}	
	
	XMLHttpIE.prototype.quickCallAsync = function (url, param, callback, callerObject)
	{
		this.fnCallback=callback;
		this.callerObject=callerObject;
		this.httpRequest = this.postXML(url, this.createXMLFromString("<param>"+param+"</param>"), true);
		this.id=ao_xmlHttp.length;
		ao_xmlHttp.push(this);
		_xmlWaitResult(this.id);
	}	
	
	// Stop an async request already in progress.
	XMLHttpIE.prototype.cancel = function()
	{
		ao_xmlHttp[this.id]=null;
	}	
	
	XMLHttpIE.prototype.getXML = function (justData)
	{
		var response = this.httpRequest.responseXML;
		debug("getXML() - this.httpRequest.responseText =\n"+this.httpRequest.responseText+"\nEND");
		try
		{
			if (justData)
				response = response.documentElement.childNodes[0].data;
	
			return response;
		}
		catch(ex)
		{
			return null;
		}
	}	
}

function XMLHttpNS()
{
	
	XMLHttpNS.prototype.createXMLFromString = function  (string) 
	{
  		var xmlParser, xmlDocument;
  		
  		try 
		{
		    if (browser.isSafari) {
		        var tagName = 'param';
                if (document.implementation && document.implementation.createDocument &&
                    (xmlDocument = document.implementation.createDocument('', tagName, null))) {
                        if (xmlDocument.documentElement == null) { // Safari 1.2 bug 
                            // add documentElement
                            xmlDocument.appendChild(xmlDocument.createElement(tagName));
                        }
                        return xmlDocument;
              }
              else {
                return null;
              }
		    }
				if(!window.DOMParser){
						//CR 1668872 
						//Even if XMLHttpRequest is a native object in IE7, we still using activeX to create Xml document
						createXMLFromStringByActiveX(string);
				}
    		else{ 
    		xmlParser = new DOMParser();
    		xmlDocument = xmlParser.parseFromString(string, 'text/xml');
						}
    		return xmlDocument;
  		}
  		catch (e)
	 	{
			if (this.callerObject && this.callerObject.errorHandler) {
				var errMsg = new Error ("Can't create XML document: "+e.description);
				this.callerObject.errorHandler(errMsg);	
			} else {
				alert("Can't create XML document: "+e.description);
			}
    		return null;
  		}
	}	

	XMLHttpNS.prototype.serializeXML = function (xmlDocument) 
	{
  		var xmlSerializer;
  		try
		{
    		xmlSerializer = new XMLSerializer();
    		return xmlSerializer.serializeToString(xmlDocument);
  		}
  		catch (e)
		{
			if (this.callerObject && this.callerObject.errorHandler) {
				var errMsg = new Error ("Can't serialize XML document.");
				this.callerObject.errorHandler(errMsg);	
			} else {
				alert("Can't serialize XML document.");
			}
    		return '';
  		}
	}

	XMLHttpNS.prototype.postXML = function (url, xmlDocument, async) 
	{
  		try 
		{
    		var httpRequest = new XMLHttpRequest();
		    httpRequest.open('POST', url, async);
            // work around Safari 1.2 bug that Safari doesn't set the Content-Type
            // to text/xml
            if (httpRequest.setRequestHeader) {
              httpRequest.setRequestHeader('Content-Type', 'text/xml');
            }
            httpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    		httpRequest.send(xmlDocument);
    		return httpRequest;
  		}
  		catch (ex) 
		{
			if (this.callerObject && this.callerObject.errorHandler) {
				var errMsg = new Error ("Can't post XML document");
				this.callerObject.errorHandler(errMsg);	
			} else {
				alert("Can't post XML document");
			}
    		return null;
  		}
	}
	
	XMLHttpNS.prototype.quickCall = function (url, param)
	{
		var httpRequest = this.postXML(url, this.createXMLFromString("<param>"+param+"</param>"), false);
		var xmlResponse = httpRequest.responseXML;

		// Netscape limits data to 4096 bytes...
		var buf="";
		for(var i=0; i < xmlResponse.documentElement.childNodes.length; i++)
		{
			buf += xmlResponse.documentElement.childNodes[i].data;
		}
		return buf;
	}	
	
	XMLHttpNS.prototype.quickCallAsync = function (url, param, callback, callerObject)
	{
		this.fnCallback=callback;
		this.callerObject=callerObject;
		this.httpRequest = this.postXML(url, this.createXMLFromString("<param>"+param+"</param>"), true);
		
		this.id=ao_xmlHttp.length;
		ao_xmlHttp.push(this);
		
		//debug("call="+this.id);
		_xmlWaitResult(this.id);
		return this.id;
	}	
	
	// Stop an async request already in progress.
	XMLHttpNS.prototype.cancel = function()
	{
		ao_xmlHttp[this.id]=null;
	}	
	
	XMLHttpNS.prototype.getXML = function (justData)
	{
		var xmlResponse = this.httpRequest.responseText;
		debug("getXML() - this.httpRequest.responseText =\n"+this.httpRequest.responseText+"\nEND");
		try
		{
			if (justData)
			{
				//debug("getXML:"+xmlResponse.documentElement.childNodes.length);
				var response="";
				for(var i=0; i < xmlResponse.documentElement.childNodes.length; i++)
				{
					response += xmlResponse.documentElement.childNodes[i].data;
				}
			}
			return response;
		}
		catch(ex)
		{
			//alert("Exception:"+ex);
			return null;
		}
	}
}

// this is to keep track of concurrent async calls;
var ao_xmlHttp = new Array();

// This function calls itself recursively until some data is returned.
function _xmlWaitResult(i)
{
	var oXmlHttp=ao_xmlHttp[i];
	if (!oXmlHttp || !oXmlHttp.httpRequest) // cancel method called..
		return;

	//debug("Ready="+oXmlHttp.httpRequest.readyState);
	
	if (oXmlHttp.httpRequest.readyState != 4)
		setTimeout("_xmlWaitResult('"+i+"')", 200);
	else
		oXmlHttp.fnCallback(oXmlHttp);
}
