/**
 * @namespace com.thesis.ajax.XmlDocument
 * @author 钟军锐 August.R@263.net
 * imports com.thesis.base.Detect
 */

/** @id XmlDocument */
function XmlDocument(){
	if (typeof XmlDocument._initialized == "undefined") {
		XmlDocument._initialized = true;
		//make the Mozilla Firefox browser working like IE browser.
		if (Detect.isMoz) {
			Document.prototype.readyState = 0;
			Document.prototype.onreadystatechange = null;
			/** @id __changeReadyState__ */
			Document.prototype.__changeReadyState__ = function(iReadyState){
				this.readyState = iReadyState;
				if (typeof this.onreadystatechange == "function") {
					this.onreadystatechange();
				}
			};
			/** @id __initError__ */
			Document.prototype.__initError__ = function(){
				this.parseError.errorCode = 0;
				this.parseError.filepos = -1;
				this.parseError.line = -1;
				this.parseError.linepos = -1;
				this.parseError.reason = null;
				this.parseError.srcText = null;
				this.parseError.url = null;
			};
			/** @id __checkForError__ */
			Document.prototype.__checkForError__ = function(){
				if (this.documentElement.tagName == "parsererror") {
					/>([\s\s]*?)Location:([\s\s]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\s]*?)(?:\-*\^)/.test(this.xml);
					this.parseError.errorCode = -999999;
					this.parseError.reason = RegExp.$1;
					this.parseError.url = RegExp.$2;
					this.parseError.line = parseInt(RegExp.$3);
					this.parseError.linepos = parseInt(RegExp.$4);
					this.parseError.srcText = RegExp.$5;
				}
			};
			/** @id loadXML */
			Document.prototype.loadXML = function(sXML){
				this.__initError__();
				this.__changeReadyState__(1);
				this.__oParser__ = new DOMParser();
				this.__oXmlDom__ = this.__oParser__.parseFromString(sXML, "text/xml");
				while (this.firstChild) {
					this.removeChild(this.firstChild);
				}
				for (var i = 0; i < this.__oXmlDom__.childNodes.length; i++) {
					this.__oNewNode__ = this.importNode(this.__oXmlDom__.childNodes[i], true);
					this.appendChild(this.__oNewNode__);
				}
				this.__checkForError__();
				this.__changeReadyState__(4);
			};
			/** @id __load__ */
			Document.prototype.__load__ = Document.prototype.load;
			/** @id load */
			Document.prototype.load = function(sURL){
				this.__initError__();
				this.__changeReadyState__(1);
				this.__load__(sURL);
			};
			Node.prototype.__defineGetter__("xml", function(){
				this.__oSerializer__ = new XMLSerializer();
				return this.__oSerializer__.serializeToString(this, "text/xml");
			});
			Element.prototype.selectNodes = function(sXPath){
				this.__oEvaluator__ = new XPathEvaluator();
				this.__oResult__ = this.__oEvaluator__.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
				this.__arrNodes__ = new Array();
				if (this.__oResult__) {
					while (this.__oElement__ = this.__oResult__.iterateNext()) 
						this.__arrNodes__.push(this.__oElement__);
				}
				return this.__arrNodes__;
			};
			Element.prototype.selectSingleNode = function(sXPath){
				this.__oEvaluator__ = new XPathEvaluator();
				this.__oResult__ = this.__oEvaluator__.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
				if (this.__oResult__) 
					return this.__oResult__.singleNodeValue;
				else 
					return null;
			};
		}
		/** @id enumerateIEXMLDocumentDrivers */
		XmlDocument.prototype.enumerateIEXMLDocumentDrivers = function(){
			this.__arrDllName__ = ["MSXML6.DOMDocument", "MSXML5.DOMDocument", "MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "Microsoft.XmlDom"];
			this.__arrDriver__ = new Array();
			for (var i = 0; i < this.__arrDllName__.length; i++) 
				if (i < this.__arrDllName__.length - 1) {
					for (var j = 6; j > 0; j--) {
						if (j != 1) 
							this.__arrDriver__.push(this.__arrDllName__[i] + "." + j + ".0");
						else 
							this.__arrDriver__.push(this.__arrDllName__[i]);
					}
				}
				else {
					this.__arrDriver__.push(this.__arrDllName__[i]);
				}
			return this.__arrDriver__;
		};
	}
	if(window.ActiveXObject){
		//IE-specific code
		this.__arrDriver__ = this.enumerateIEXMLDocumentDrivers();
		for(var i=0;i<this.__arrDriver__.length;i++){
			try{
				XmlDocument.Driver = this.__arrDriver__[i];
				return new ActiveXObject(this.__arrDriver__[i]);
			}
			catch(oError){
				delete XmlDocument.Driver;
			}
		}
		throw new Error("MSXML is not installed on your system.");
	}
	else if(document.implementation && document.implementation.createDocument){
		//DOM-specific code
		this.__oXmlDom__=document.implementation.createDocument("","",null);
		this.__oXmlDom__.parseError = {
			valueOf: function(){ return this.errorCode;},
			toString: function(){return this.errorCode.toString();}
		};
		this.__oXmlDom__.__initError__();
		this.__oXmlDom__.addEventListener("Loaded",function(){
			this.__checkForErrors__();
			this.__changeReadyState__(4);
		},false);
		return this.__oXmlDom__;
	}
	else{
		throw new Error("Your brower doesn't support an XML document object.");
	}
}

