﻿var _isIE = !!(window.attachEvent && !window.opera);
var _isGecko = navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1;
function XmlDom(xml) {
	var doc = null;
	if (_isIE) {
		var arrSignatures = ["MSXML2.DOMDocument.7.0", 
							"MSXML2.DOMDocument.6.0", 
							"MSXML2.DOMDocument.5.0",
							"MSXML2.DOMDocument.4.0", 
							"MSXML2.DOMDocument.3.0",
							"MSXML2.DOMDocument", 
							"Microsoft.XmlDom"];
		for (var i = 0; i < arrSignatures.length; i++) {
			try {
				doc = new ActiveXObject(arrSignatures[i]);
			} catch (oError){}
		}
	} else if (_isGecko) {
		doc = document.implementation.createDocument("","", null);
		doc.parseError =  {
			valueOf: function() {
				return this.errorCode;
			}, 
			toString: function() {
				return this.errorCode.toString()
			}
		};
		doc.__initError__();
		doc.addEventListener("load", function() {
			this.__checkForErrors__(); 
			this.__changeReadyState__(4);
		}, false);
	}
	if (doc == null)
		throw new Error("Your browser doesn't support an XML DOM object.");
	else{
		if (xml) {
			if (xml.indexOf('<') > -1)
				doc.loadXML(xml);
			else{
				doc.async = false;
				doc.load(xml);
			}
		}
	}
	return doc;
};

/* XML Document patch for Firefox */
if (Browser.Engine.gecko) {
	Document.prototype.readyState = 0;
	Document.prototype.onreadystatechange = null;
	Document.prototype.__changeReadyState__ = function(iReadyState) {
		this.readyState = iReadyState;
		if (typeof this.onreadystatechange =="function") {
			this.onreadystatechange();
		}
	};
	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;
	};
	Document.prototype.__checkForErrors__ = function() {
		if (this.documentElement.tagName =="parsererror") {
			var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/;
			reError.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;
		}
	};
	Document.prototype.loadXML = function(sXml) {
		this.__initError__();
		this.__changeReadyState__(1);
		var oParser = new DOMParser();
		var oXmlDom = oParser.parseFromString(sXml,"text/xml");
		while (this.firstChild) {
			this.removeChild(this.firstChild);
		}
		for (var i = 0; i < oXmlDom.childNodes.length; i++) {
			var oNewNode = this.importNode(oXmlDom.childNodes[i], true);
			this.appendChild(oNewNode);
		}
		this.__checkForErrors__();
		this.__changeReadyState__(4);
	};
	Document.prototype.__load__ = Document.prototype.load;
	Document.prototype.load = function(sURL) {
		this.__initError__();
		this.__changeReadyState__(1);
		this.__load__(sURL);
	};
	Node.prototype.__defineGetter__("xml", function() {
		var oSerializer = new XMLSerializer(); 
		return oSerializer.serializeToString(this,"text/xml");
	});
	XMLDocument.prototype.__proto__.__defineGetter__("text", function() {
		return this.firstChild.textContent;
	});
	Element.prototype.__proto__.__defineGetter__("text", function() {
		return this.textContent;
	});
	Element.prototype.__proto__.__defineSetter__("text", function(value) {
		this.textContent = value;
	});
	XMLDocument.prototype.selectSingleNode = 
	    Element.prototype.selectSingleNode = function(xpath) {
		var x = this.selectNodes(xpath)
		if (!x || x.length < 1) {
			return null;
		}
		return x[0];
	}
	XMLDocument.prototype.selectNodes = 
	    Element.prototype.selectNodes =function(xpath) {
		var xpe = new XPathEvaluator();
		var nsResolver = xpe.createNSResolver(this.ownerDocument == null ? this.documentElement: this.ownerDocument.documentElement);
		var result = xpe.evaluate(xpath, this, nsResolver, 0, null);
		var found = [];
		var res;
		while (res = result.iterateNext()) {
			found.push(res);
		}
		return found;
	}
};
function $xget(node, attribute, defaultValue){ if (attribute){ if (node.getAttribute(attribute)) return node.getAttribute(attribute); else if (defaultValue) return defaultValue;else return "";} else return node.innerText; };
function $xgetI(node, attribute, defaultValue){ var v = $xget(node, attribute, defaultValue); if (v == "") v = 0; return parseInt(v); };
function $xgetB(node, attribute, defaultValue){ var v = $xget(node, attribute, defaultValue); if (v.toLowerCase() == "true") return true; return false; };
function $xset(node, attribute, value){ if (attribute) node.setAttribute(attribute, value.toString()); else node.innerText = value; };

