
function initXml()
{
	if( !isIEBrowser || isOperaBrowser )
		emulateDOMDocument();
}

function getXML( node )
{
	if( isIEBrowser )
		return node.xml;
	else
		return  (new XMLSerializer()).serializeToString( node );
}
function setNSText(node, value)
{ 
    if( node.firstChild )
		node.removeChild(node.firstChild);
    node.appendChild( node.ownerDocument.createTextNode(value) );
}

function chkXmlError( xmlDoc )
{
	var msg = null;
	if( isIEBrowser && xmlDoc.parseError.errorCode != 0 )
	{
		msg = xmlDoc.parseError.reason + " at [" + xmlDoc.parseError.line + ":" + xmlDoc.parseError.linepos + "]";
	}
	else if( xmlDoc.documentElement.nodeName == "parsererror" )
	{
		msg = "";
		//msg += xmlDoc.selectSingleNode( "parsererror" ).text;		
		msg += getXML( xmlDoc );
	}

	if( msg != null )
		alert( msg );
	return msg != null;
}
function emulateDOMDocument()
{
  Document.prototype.readyState = "complete";

  //Node.prototype.__defineGetter__("xml",
  Node.prototype.xml =
    function ()
    {    
      //create a new XMLSerializer
      var objXMLSerializer = new XMLSerializer;
    
      //get the XML string
      var strXML = objXMLSerializer.serializeToString(this);
    
      //return the XML string
      return strXML;
    };

	Document.prototype.recalcSizes = function recalcSizes()
	{
	  this.documentElement.recalcSizes();
	}

  //Document.prototype.__defineGetter__("xml", 
  Document.prototype.xml =
    function () 
    {    
      if(this.documentElement)
        return this.documentElement.xml;
      else
        return "";
    };

  Document.prototype.loadXML = function(strXML) 
    {    
        //change the readystate
        //this.readyState = 1;
 
        //create a DOMParser
        var objDOMParser = new DOMParser();
        
        //create new document from string
        var objDoc = objDOMParser.parseFromString(strXML, "text/xml");

        //make sure to remove all nodes from the document
        while( this.hasChildNodes() )
            this.removeChild(this.lastChild);

        //add the nodes from the new document
        for (var i=0; i < objDoc.childNodes.length; i++) {
            
            //import the node
            var objImportedNode = this.importNode(objDoc.childNodes[i], true);
            
            //append the child to the current document
            this.appendChild(objImportedNode);
        
        } //End: for
        
        //change the readystate
        //this.readyState = 4;
        
    } //End: function

  Node.prototype.selectSingleNode = function(xPath) 
    {
      var xpathResult = this.ownerDocument.evaluate(xPath, this, null, 0, null);
      var node = xpathResult.iterateNext();
      return node;
    }

  Document.prototype.selectSingleNode = function (xPath) 
    {
      var xpathResult = this.evaluate(xPath, this, null, 0, null);
      //alert( typeof(xpathResult) );
      var node = xpathResult.iterateNext();
      return node;
    }

  Node.prototype.selectNodes = function(xPath) 
    {
      var tmp = [];
      var xpathResult = this.ownerDocument.evaluate(xPath, this, null, 0, null);
      var node;
      while ((node = xpathResult.iterateNext())) 
      {
        tmp[tmp.length] = node;          
      }
      return tmp;
    }        

  Document.prototype.selectNodes = function (xPath) 
    {    
      var tmp = [];
      var xpathResult = this.evaluate(xPath, this, null, 0, null);
      var node;
      while ((node = xpathResult.iterateNext())) 
      {
        tmp[tmp.length] = node;          
      }
      return tmp;
    }

  //Node.prototype.__defineGetter__("text",
  Node.prototype.getText =
    function ()
    {    
      var text = "";
      for(var i = 0; i < this.childNodes.length; i++)    
      {
        if(this.childNodes[i].nodeValue) text += this.childNodes[i].nodeValue;
      }
      return text;
    };

  //Node.prototype.__defineSetter__("text", 
  Node.prototype.setText =
    function (value)
    { 
      if(this.firstChild)
        this.removeChild(this.firstChild);
      this.appendChild(this.ownerDocument.createTextNode(value));      
    };

  //XMLDocument.prototype.__defineSetter__("documentElement", function(xmlNode) {
  /*XMLDocument.prototype.setDocumentElement = function( xmlNode ) {
    if(this.documentElement != null)
      this.removeChild(this.documentElement);
	  return this.appendChild(xmlNode);
	};

	XMLDocument.prototype.createNode = function(type, name, arg) {	  
		switch(type)
		{
		case 2: return this.createAttribute(name);
		}    
	};*/


	//Attr.prototype.__defineSetter__("text", function(value) {	  
	Attr.prototype.setText = function(value) {
		this.nodeValue = value;
	};
	
	//Attr.prototype.__defineGetter__("text", function() {	  
	Attr.prototype.getText = function() {
		return this.nodeValue;
	};
}


function createXmlDocument()
{
	var res = null;

	if( document.implementation && document.implementation.createDocument )
	{
		res = document.implementation.createDocument( "", "", null );
	}
	else //IE
	{
		try
		{
			res = new ActiveXObject( "MSXML2.FreeThreadedDOMDocument.4.0" );
		}
		catch(e)
		{
		}
		try
		{
			if( !res )
				res = new ActiveXObject( "MSXML2.FreeThreadedDOMDocument" );
		}
		catch(e)
		{
		}
		if( !res )
			res = new ActiveXObject( "MSXML.FreeThreadedDOMDocument" );
	}
	return res;
}

function reportXMLError( doc )
{
	var e = doc.parseError;
	alert( e.reason + " at [" + e.line + ":" + e.linepos + "]" );
}
function removeCDATA( str )
{
	var i1 = str.indexOf( "<!--" );
	var i2 = str.lastIndexOf( "-->" );
	if( i1 != -1 && i2 != -1 )
		return "<" + str.substring( i1 + "<!--".length, i2 ) + ">";
	else
		return str;
}

function rndVal( lower, upper )
{
	var res = Math.random();
	return Math.floor( (upper - lower + 1) * res + lower );
}
