// This class is the base class for XML data island.
// Construction functon
function XMLBaseObject(){
	DeliverXMLBaseObject(this);
}

// The function used for inheritence.
function DeliverXMLBaseObject(toObject){
	toObject.xmlDocument = null;
	toObject.rootElement = null;
	toObject.domParser = null;
	toObject.addAttribute = XMLBOAddAttribute;
	toObject.setXMLDocument = XMLBOSetXMLDocument;
	toObject.getXMLDocument = XMLBOGetXMLDocument;
	toObject.loadXML = XMLBOLoadXML;
	toObject.setRootElement = XMLBOSetRootElement;
	toObject.getInnerXml = XMLBOGetInnerXml;
	toObject.getDomParser = XMLBOGetDomParser;
}

function XMLBOGetInnerXml(){
	return this.rootElement.xml;
}

// Add an attribute to the given element.
function XMLBOAddAttribute(toElement, tsName, tsValue){
	var loAttribute = this.xmlDocument.createAttribute(tsName);
	if(tsValue==null) tsValue = "";
	loAttribute.nodeValue = tsValue;
	toElement.attributes.setNamedItem(loAttribute);
}

// Set a XML DOM document.
function XMLBOSetXMLDocument(toXMLDocument){
	this.xmlDocument = toXMLDocument;
	this.setRootElement();
}

function XMLBOGetDomParser(){
	if(this.domParser==null)
		this.domParser = new DOMParser();
	return this.domParser;
}
// Return the XML DOM Document object. 
function XMLBOGetXMLDocument(){
	if(this.xmlDocument==null){
		if(window.ActiveXObject){
			this.xmlDocument = new ActiveXObject("microsoft.XMLDOM");
			this.xmlDocument.loadXML("<XMLDATA></XMLDATA>");
		}else if(window.DOMParser){
			this.xmlDocument = this.getDomParser().parseFromString("<XMLDATA></XMLDATA>", "text/xml");
			XMLBOImplementXPath();
		}
		this.setRootElement();
	}
	return this.xmlDocument;
}

// Load the XML script to the object.
function XMLBOLoadXML(tsXML){
	if(window.ActiveXObject){
		this.getXMLDocument().loadXML(tsXML);
	}else if(window.DOMParser){
		this.xmlDocument = this.getDomParser().parseFromString(tsXML, "text/xml");
		// check for XPath implementation
		XMLBOImplementXPath();
	}
	this.setRootElement(); 
}

function XMLBOImplementXPath(){
	// check for XPath implementation
	if( document.implementation.hasFeature("XPath", "3.0") )
	{  // prototying the XMLDocument  
		XMLDocument.prototype.selectNodes = function(cXPathString, xNode)  {     
			if( !xNode ) { xNode = this; }      
			var oNSResolver = this.createNSResolver(this.documentElement)     
			var aItems = this.evaluate(cXPathString, xNode, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			var aResult = [];     
			for( var i = 0; i < aItems.snapshotLength; i++){ 
				aResult[i] =  aItems.snapshotItem(i);     
			}     
			return aResult;  
		}  
		// prototying the Element  
		Element.prototype.selectNodes = function(cXPathString){
			if(this.ownerDocument.selectNodes){
				return this.ownerDocument.selectNodes(cXPathString, this);     
			}     
			else{
				throw "For XML Elements Only";
			}  
		}
		Node.prototype.__defineGetter__
		(
			"xml",
			function()
			{
				return (new XMLSerializer).serializeToString(this);
			}
		);
		
	}
}

function XMLBOSetRootElement(){
	if(this.xmlDocument.childNodes!=null){
		for(var index=0; index<this.xmlDocument.childNodes.length; index++){
			if(this.xmlDocument.childNodes[index].nodeType==1){
				this.rootElement = this.xmlDocument.childNodes[index];
				return;
			}
		}
	}
	this.rootElement = null;
}

// Get a query string for an attribute.
function GetQueryItem(tsName, tsValue){
	return "@"+tsName+"=\""+tsValue+"\"";
}

// Get a query string for an boolean attribute.
function GetQueryItemBool(tsName, tbValue){
	var lnValue;
	if(tbValue==true){
		lnValue = 1;
	}else{
		lnValue = 0;
	}
	return "@"+tsName+"="+lnValue+"";
}

