

var $UNINITIALIZED	= 0;
var $LOADING		= 1;
var $LOADED			= 2;
var $COMPLETE		= 4;

///<summary>
/// Mozilla Browser Complement
/// 	Adding some missing 'oddie-goodie' functions to Document & Element Object
/// 		+ loadXML
/// 		+ setProperty
///			+ selectSingleNode
/// 		+ selectNodes
///</summary>
var $MozillaComplement = function()
{
	if (typeof(window.XPathEvaluator)!='undefined')
	{	
		var xpath = new XPathEvaluator();
	
		var element = Element.prototype;
	    element.__proto__ = {__proto__: element.__proto__};
	    element = element.__proto__;
	
		var attribute = Attr.prototype;
	    attribute.__proto__ = {__proto__: attribute.__proto__};
	    attribute = attribute.__proto__;
	
		var txt = Text.prototype;
	    txt.__proto__ = {__proto__: txt.__proto__};
	    txt = txt.__proto__;
	
		var doc = Document.prototype;
	    doc.__proto__ = {__proto__: doc.__proto__};
	    doc = doc.__proto__;
	
		doc.loadXML = function(text)
		{
			var parser = new DOMParser;
			var newDoc = parser.parseFromString(text, "text/xml");
			// modified by Nguyen Anh Tuan
			// check this first, so we cannot remove a null child
			if (this.documentElement)
				this.removeChild(this.documentElement);
	
			// now we can add a new child
			this.appendChild(newDoc.documentElement);
			//this.replaceChild(newDoc.documentElement, this.documentElement);		
		};
	
		doc.setProperty = function(name, value)
		{
			if(name=="SelectionNamespaces"){
				namespaces = {};
				var a = value.split(" xmlns:");
				for (var i=1;i<a.length;i++){
					var s = a[i].split("=");
					namespaces[s[0]] = s[1].replace(/\"/g, "");
				}
				this._ns = {
					lookupNamespaceURI : function(prefix){return namespaces[prefix]}
				}
			}
		};
	
		doc._ns = {
			lookupNamespaceURI : function(){return null}
		};
	
		doc.selectNodes = function (path) {
		   var result = xpath.evaluate(path, this, this._ns, 7, null);
		   var i, nodes = [];
		   for (i=0; i<result.snapshotLength; i++) {nodes[i]=result.snapshotItem(i)}
		   return nodes;
		};
	
		doc.selectSingleNode = function (path) {
		   return xpath.evaluate(path, this, this._ns, 9, null).singleNodeValue;
		};
	
		element.selectNodes = function (path) {
		   var result = xpath.evaluate(path, this, this.ownerDocument._ns, 7, null);
		   var i, nodes = [];
		   for (i=0; i<result.snapshotLength; i++) {nodes[i]=result.snapshotItem(i)}
		   return nodes;
		};
	
		element.selectSingleNode = function (path) {
		   return xpath.evaluate(path, this, this.ownerDocument._ns, 9, null).singleNodeValue;
		};
	
		element.__defineGetter__("text", function(){
			var i, a=[], nodes = this.childNodes, length = nodes.length;
			for (i=0; i<length; i++){a[i] = nodes[i].text}
			return a.join("");
		});
		
		attribute.__defineGetter__("text", function(){return this.nodeValue});
	
		txt.__defineGetter__("text", function(){return this.nodeValue});
	}
};
$MozillaComplement();

function XMLDocument()
{
	var self 				= this;

	self.Name				= "$XMLDocument";
	self.xmlDoc				= null;
	self.documentElement 	= null;
	self.firstChild 		= null;

	self.OnLoad				= function(self) {return null;};
	self.GetNodeAttribute   = function(node, sAttributeName)
	{
		if (null==node.attributes) return null;
		var a = node.attributes.getNamedItem(sAttributeName);
		return a ? a.value : null;
	}

	if (window.ActiveXObject) // IE supports
	{
		try
		{
			self.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}
		catch(e)
		{
			try
			{
				self.xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
			}
			catch(e)
			{
				// No way...
			}
		}

		if (self.xmlDoc!=null)
		{
			self.xmlDoc.onreadystatechange = function()
			{
				self.readyState = self.xmlDoc.readyState;
				if (self.readyState==$COMPLETE)
				{
					self.documentElement 	= self.xmlDoc.documentElement;
					self.firstChild 		= self.documentElement;
					self.OnLoad(self);
				}
			}
		}
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		try
		{
			self.xmlDoc = document.implementation.createDocument("", "", null);
		}
		catch(e)
		{
			// In the middle of nowhere...
		}

		if (this.xmlDoc!=null)
		{
			self.xmlDoc.onload = function()
			{
				self.documentElement 	= self.xmlDoc.documentElement;
				self.firstChild 		= self.documentElement;
				self.OnLoad(self);
			}
		}
	}

	self.Load		= function(url)
	{
		self.xmlDoc.load(url);
	};

	self.LoadXML	= function(xml)
	{
		self.xmlDoc.loadXML(xml);
		self.xmlDoc.setProperty("SelectionLanguage", "XPath");
		self.documentElement 	= self.xmlDoc.documentElement;
		self.firstChild 		= self.documentElement;
	};
}


