var OLEDoc  = ['Microsoft.XMLDOM', 'MSXML2.DOMDocument', 'MSXML2.DOMDocument.3.0', 'MSXML2.DOMDocument.4.0', 'MSXML2.DOMDocument.5.0'];
var OLEHTTP = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP', 'MSXML2.XMLHTTP.3.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.5.0'];

/*
use: 
	var Doc  = getXMLDocument()	/for an empty XML Document.
	var Http = getHTTPRequest()	/for a new HTTPRequest object
	var XMLHTTP = getHTTPResponse(URL [,name, value][,name, value] ...) /for a XMLHTTP object
	var Doc  = getHTTPDocument(URL)	/to open a document from a URL.
	var Doc  = getHTTPDocument(URL, name, value, name, value, ...) /to open a document by POSTing name/value pairs to a URL.
	var Doc  = getSOAP(URL, action, message) /to make a SOAP request and get the returned value
	
	W3C DOM's are extended with these methods/properties from the MSXML DOM:
		XMLDOcument.transformNode(XSLDOM) (Document only extended)	
		Element.selectSingleNode(Xpath)
		Element.selectNodes(XPath)
		Element.xml
		Node.text		

*/

function XMLDocument() {
	var xmldoc;
	if (typeof(XMLHttpRequest)=='undefined') {
		for (var i = OLEDoc.length; i >= 0; i--) {
			try {
				xmldoc = new ActiveXObject(OLEDoc[i - 1]);
				break;
			} catch(e) {}
		}
	} else {
		xmldoc = document.implementation.createDocument('', '', null);
	}
	return xmldoc;
}

function getHTTPRequest() {
	var xmlhttp;
	if (typeof(XMLHttpRequest)=='undefined') {
		for (var i = OLEHTTP.length; i >= 0; i--) {
			try {
				xmlhttp = new ActiveXObject(OLEHTTP[i - 1]);
				break;
			} catch(e) {}
		}
	} else {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function getHTTPResponse(URL) {
    var xmlhttp = getHTTPRequest();
    if (xmlhttp) {		      
        var URLEncoded = '';
        for (var i=1; i<arguments.length; i = i+2) {
            if (URLEncoded) {URLEncoded += '&'}
            URLEncoded += (escape(arguments[i]) + '=' + escape(arguments[i+1]));
        }
        try {         
            xmlhttp.open((URLEncoded ? 'POST' : 'GET'), URL, false);             
            if (URLEncoded) {xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')}  
            xmlhttp.send(URLEncoded);            
            return xmlhttp;
        } catch(e) {alert('An error occurred downloading data.\n('+e+')')}
    }
}

function getHTTPDocument(URL) {
	var xmlhttp = getHTTPResponse.apply(this, arguments);
	if (xmlhttp) {return xmlhttp.responseXML}
}

function getSOAP(URL, action, message) {
	var xmlhttp = getHTTPRequest();
	if (xmlhttp) {
		try {
			if (netscape.security.PrivilegeManager.enablePrivilege) {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			}
		} catch (ex) {}
		try {
			xmlhttp.open("POST", URL, false);
			xmlhttp.setRequestHeader('Content-Type', 'text/xml');
			xmlhttp.setRequestHeader('SOAPAction', action);
			var envelope = "<SOAP-ENV:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'><SOAP-ENV:Body SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" + message + "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
			xmlhttp.send(envelope);
			var xmldoc = xmlhttp.responseXML;			
			if (xmldoc) {
				return xmldoc.documentElement.selectSingleNode('/*/*/*');
			}
		} catch(e) {
			alert('This browser cannot currently complete SOAP requests.');
		}
	}
}

function SOAPNSResolver(prefix) {
	switch (prefix) {
		case 'SOAP-ENV' : return 'http://schemas.xmlsoap.org/soap/envelope/';
	}
}

if (typeof(XMLHttpRequest)!='undefined') {	
	XMLDocument.prototype.transformNode = function(stylesheet) {
		var result = document.implementation.createDocument('', '', null);
		try {
			new XSLTProcessor().transformDocument(this, stylesheet, result, null);
		} catch(e) {
			alert(e); // throw(e);
		}
		try {
			return new XMLSerializer().serializeToString(result);
		} catch(e) {
			alert(e); // throw(e);
		}
	}
		
	Element.prototype.selectNodes = function(XPath, NSResolver) 
	{
		var result = [];
		result.item = function(index){return result[index]}
		try {
			var doc = (this.implementation ? this : this.ownerDocument);
			var nodes = doc.evaluate(XPath, this, NSResolver, 5, null);
			var node = nodes.iterateNext();
			while(node) {
				result[result.length] = node;
				node = nodes.iterateNext();
			}
		} catch(e) {
			alert(e); // throw(e);
		}
		return result;
	}
	
	Element.prototype.selectSingleNode = function(x_path, NSResolver) {
		try {
			var result = (this.implementation ? this : this.ownerDocument);
			return result.evaluate(x_path, this, NSResolver, 9, null).singleNodeValue;
		} catch(e) {
			alert(e); // throw(e);
		}
	}
	
	Element.prototype.__defineGetter__("xml", function (){
		try {
			return (new XMLSerializer()).serializeToString(this);
		} catch(e) {
			alert(e); // throw(e);
		}
	});
	
	Node.prototype.__defineGetter__("text", function()
	{
		try {
			var result = '';
			if(this.nodeType==3) {return this.nodeValue}
			if(this.hasChildNodes()) {
				var children = this.childNodes;
				for(var i=0;i<children.length;i++) {
					var n = children[i];
					if(n.nodeType==3) {result+=n.nodeValue}
					if(n.nodeType==1 && n.hasChildNodes()){result+=n.text}
				}
			}
			return result;
		} catch(e) {
			alert(e); // throw(e);
		}
	});					
}
