/**
 * From http://webfx.eae.net/dhtml/xmlextras/xmlextras.html
 * 
*/
/********************************************************************************/
/**
 * Handle DOM XML model quirk flags
 * 
*/
var _isIEmodel = false;
var _isMOZmodel = false;
var _isOP8model = false;
var _isKonqmodel = false;
var _isSafarimodel=false;
if ((document.all) && (document.getElementById) && (window.XMLDocument))
  _isIEmodel = true;
if ((!document.all) && (document.getElementById) && (window.XMLDocument))
  _isMOZmodel = true;
if ((navigator.userAgent.indexOf("Opera") > -1) && (document.implementation.createLSParser) && (document.implementation.createLSSerializer))
  _isOP8model = true;
var kxstest = null;
try { kxstest = new XMLSerializer(); } catch(e) {}
if ((navigator.userAgent.indexOf('KHTML') > -1) && (document.loadXML) && (typeof(kxstest)=="object"))
  _isKonqmodel = true;
if ((navigator.userAgent.indexOf('AppleWebKit') > -1) && (navigator.userAgent.indexOf('Safari')>-1) && (typeof(kxstest)=="object"))
  _isSafarimodel = true;
/********************************************************************************/
/**
 * Add two useful functions to mozilla if it doesn't already have them
 * 
*/
document.sharedinstances = new Object();  // recycle, reduce, reuse
if (_isSafarimodel)
{
  document.sharedinstances.domparser = new DOMParser();
  document.sharedinstances.xmlserializer = new XMLSerializer();
}
if (_isOP8model)
{
  document.sharedinstances.domparser = document.implementation.createLSParser(document.implementation.MODE_SYNCHRONOUS,'http://www.w3.org/TR/REC-xml'); // creating a new one each time is expensive
  document.sharedinstances.xmlserializer = document.implementation.createLSSerializer();
}
if (_isKonqmodel)
{
  //document.sharedinstances.domparser = document.implementation.createLSParser(document.implementation.MODE_SYNCHRONOUS,'http://www.w3.org/TR/REC-xml'); // creating a new one each time is expensive
  document.sharedinstances.xmlserializer = new XMLSerializer();
}
if ((_isMOZmodel) && (!_isOP8model))
{
  if (!Document.prototype.loadXML)
  {
    document.sharedinstances.domparser = new DOMParser(); // creating a new one each time is expensive
	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both! -- some versions? all versions!
	XMLDocument.prototype.loadXML = 
	Document.prototype.loadXML = function (s) {
		// parse the string to a new doc	
		var doc2 = document.sharedinstances.domparser.parseFromString(s, "text/xml");
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
		// insert and import nodes
		childNodeCount = doc2.childNodes.length;
		for (var i = 0; i < childNodeCount; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	};
  }
  if (!Document.prototype.xml)
  {
	  /**
	   * xml getter
	   * This serializes the DOM tree to an XML String
	   * Usage: var sXml = oNode.xml
	   *
	   */
	  // XMLDocument did not extend the Document interface in some versions
	  // of Mozilla. Extend both! -- some versions? all versions!
    document.sharedinstances.xmlserializer = new XMLSerializer(); // creating a new one each time is expensive
    if (window.navigator.userAgent.search(/FireFox/g)!=-1)  //this causes problems in Firefox 1.0.3+
    {
      XMLDocument.prototype.__defineGetter__("xml", function () {
          return document.sharedinstances.xmlserializer.serializeToString(this);
	    });
    }
    Element.prototype.__defineGetter__("xml", function () {
      return document.sharedinstances.xmlserializer.serializeToString(this);
    });
    Document.prototype.__defineGetter__("xml", function () {
      return document.sharedinstances.xmlserializer.serializeToString(this);
	  });

  }
};
/********************************************************************************/
function getControlPrefix() {
   if (getControlPrefix.prefix)
      return getControlPrefix.prefix;

   var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
   var o, o2;
   for (var i = 0; i < prefixes.length; i++) {
      try {
         // try to create the objects
         o = new ActiveXObject(prefixes[i] + ".XmlHttp");
         o2 = new ActiveXObject(prefixes[i] + ".XmlDom");
         /* Clean up after our little experiment */
         delete o;
         delete o2;
         return getControlPrefix.prefix = prefixes[i];
      }
      catch (ex) {};
   }
   throw new Error("Could not find an installed XML parser");
};
/********************************************************************************/
// XmlHttp factory
function XmlHttp() {}
XmlHttp.create = function () {
   try {
      if (window.XMLHttpRequest) {
         var req = new XMLHttpRequest();
         
         // some older versions of Moz did not support the readyState property
         // and the onreadystate event so we patch it!
         if (req.readyState == null) {
            req.readyState = 1;
            req.addEventListener("load", function () {
               req.readyState = 4;
               if (typeof req.onreadystatechange == "function")
                  req.onreadystatechange();
            }, false);
         }
         
         return req;
      }
      if (window.ActiveXObject) {
         return new ActiveXObject(getControlPrefix() + ".XmlHttp");
      }
   }
   catch (ex) {}
   // fell through
   return null;
};
   /*******************************************************/
XmlHttp.loadSync = function(sUri)  // may leak, need to be able to get rid of original xmlHttp ?
{
  var xmlHttp = XmlHttp.create();
  var async = false;
  xmlHttp.open("GET", sUri, async);
  xmlHttp.send(null);
  if (_isOP8model)
  {
    xmlHttp.responseXML.loadXML = XmlDocument.loadXML;
    xmlHttp.responseXML.xml = XmlDocument.xml;
  }
  if (_isKonqmodel)
    xmlHttp.responseXML.xml = XmlDocument.xml;
  return (xmlHttp.responseXML);
};
   /*******************************************************/
XmlHttp.loadAsync = function (sUri,callback) 
{
  var xmlHttp = XmlHttp.create();
  var async = true;
  xmlHttp.open("GET", sUri, async);
  xmlHttp.onreadystatechange = function () 
  {
    if (xmlHttp.readyState == 4)
    {
      if (_isOP8model)
      {
        xmlHttp.responseXML.loadXML = XmlDocument.loadXML;
        xmlHttp.responseXML.xml = XmlDocument.xml;
      }
      if (_isKonqmodel)
        xmlHttp.responseXML.xml = XmlDocument.xml;
      callback(xmlHttp.responseXML); // responseXML : XmlDocument
      delete xmlHttp;
    }
  };
  xmlHttp.send(null);
};
   /*******************************************************/
XmlHttp.loadTextAsync2 = function (sUri,newCallback, request_type) 
{
  var xmlHttp = XmlHttp.create();
  var async = true;
  xmlHttp.open("GET", sUri, async);
  xmlHttp.onreadystatechange = function () 
  {
    if (xmlHttp.readyState == 4)
    {
      if (DataTypeOf(newCallback) == 'function')
        newCallback(xmlHttp.responseText, request_type);
      else
        newCallback.callback(xmlHttp.responseText);
      delete xmlHttp;
    }
  };
  xmlHttp.send(null);
};
   /*******************************************************/
XmlHttp.postSync = function(sUri, xmlDoc)  // may leak, need to be able to get rid of original xmlHttp ? 
{
  var xmlHttp = XmlHttp.create();
  var async = false;
  xmlHttp.open("POST", sUri, async);
  if (_isSafarimodel)
    xmlHttp.setRequestHeader('Content-Type','text/xml');
  xmlHttp.send(xmlDoc);
  if (_isOP8model)
  {
    xmlHttp.responseXML.loadXML = XmlDocument.loadXML;
    xmlHttp.responseXML.xml = XmlDocument.xml;
  }
  if (_isKonqmodel)
    xmlHttp.responseXML.xml = XmlDocument.xml;
  return (xmlHttp.responseXML);
};

XmlHttp.postAsync = function(sUri, xmlDoc, newCallback, request_type) 
{
  var xmlHttp = XmlHttp.create();
  var async = true;
  xmlHttp.open("POST", sUri, async);
  if (_isSafarimodel)
    xmlHttp.setRequestHeader('Content-Type','text/xml');
  xmlHttp.onreadystatechange = function () 
  {
    if (xmlHttp.readyState == 4)
    {
      if (_isOP8model)
      {
        xmlHttp.responseXML.loadXML = XmlDocument.loadXML;
        xmlHttp.responseXML.xml = XmlDocument.xml;
      }
      if (_isKonqmodel)
        xmlHttp.responseXML.xml = XmlDocument.xml;
      if (DataTypeOf(newCallback) == 'function')
        newCallback(xmlHttp.responseXML, request_type);
      else
        newCallback.callback(xmlHttp.responseXML, request_type); // responseXML : XmlDocument
      delete xmlHttp;
    }
  };
  xmlHttp.send(xmlDoc);
};

   /*******************************************************/
//XmlHttp.postAsync = function(sUri, xmlDoc, newCallback) 
//  9-09:  modified to accept a request_type parameter

XmlHttp.postAsync_ = function(sUri, xmlDoc, newCallback) 
{
  var xmlHttp = XmlHttp.create();
  var async = true;
  xmlHttp.open("POST", sUri, async);
  if (_isSafarimodel)
    xmlHttp.setRequestHeader('Content-Type','text/xml');
  xmlHttp.onreadystatechange = function () 
  {
    if (xmlHttp.readyState == 4)
    {
      if (_isOP8model)
      {
        xmlHttp.responseXML.loadXML = XmlDocument.loadXML;
        xmlHttp.responseXML.xml = XmlDocument.xml;
      }
      if (_isKonqmodel)
        xmlHttp.responseXML.xml = XmlDocument.xml;
      newCallback(xmlHttp.responseXML);
      delete xmlHttp;
    }
  };
  xmlHttp.send(xmlDoc);
};


XmlHttp.loadTextSync = function(sUri)  // may leak, need to be able to get rid of original xmlHttp ?
{
  var xmlHttp = XmlHttp.create();
  var async = false;
  xmlHttp.open("GET", sUri, async);
  xmlHttp.send(null);
  return (xmlHttp.responseText);
};
   /*******************************************************/
XmlHttp.loadTextASync = function(sUri,callback)
{
  var xmlHttp = XmlHttp.create();
  var async = true;
  xmlHttp.open("GET", sUri, async);
  xmlHttp.onreadystatechange = function () 
  {
    if (xmlHttp.readyState == 4)
    {
      callback(xmlHttp.responseText); // responseXML : XmlDocument
    }
  };
  xmlHttp.send(null);
};
   /*******************************************************/
XmlHttp.postTextSync = function(sUri,xmlDoc)  // may leak, need to be able to get rid of original xmlHttp ?
{
  var xmlHttp = XmlHttp.create();
  var async = false;
  xmlHttp.open("POST", sUri, async);
  xmlHttp.send(xmlDoc);
  return (xmlHttp.responseText);
};
/********************************************************************************/
// XmlDocument factory
function XmlDocument() {};
           XmlDocument.loadXML = function(xmlstr)
           {
             var xinput = document.implementation.createLSInput();
             xinput.stringData = xmlstr;
		         // parse the string to a new doc	
		         var doc2 = document.sharedinstances.domparser.parse(xinput);
		         // remove all initial children
             if(this.documentElement)
               this.removeChild(this.documentElement);
		         // insert and import nodes
		         var childNodeCount = doc2.childNodes.length;
		         for (var i = 0; i < childNodeCount; i++)
             {
			         this.appendChild(this.importNode(doc2.childNodes[i], true));
		         }
           };
           XmlDocument.xml = function()
           {
             if (arguments.length==1) // is set
             {
               this.loadXML(arguments[0]);
             }
             else // is get
             {
               if (_isOP8model)
                 return(document.sharedinstances.xmlserializer.writeToString(this));
               if (_isKonqmodel||_isSafarimodel)
               {
                 var timpstr = document.sharedinstances.xmlserializer.serializeToString(this);
                 return(timpstr);
               }
             }
           };
XmlDocument.create = function () {
   try {
      // DOM2
      if (document.implementation && document.implementation.createDocument) {
         var doc = document.implementation.createDocument("", "", null);
         // some versions of Moz do not support the readyState property
         // and the onreadystate event so we patch it!
         if (doc.readyState == null) {
            doc.readyState = 1;
            doc.addEventListener("load", function () {
               doc.readyState = 4;
               if (typeof doc.onreadystatechange == "function")
                  doc.onreadystatechange();
            }, false);
         }
         if (_isOP8model)
         {
           doc.loadXML = XmlDocument.loadXML;
           doc.xml = XmlDocument.xml;
         }
         if (_isKonqmodel)
           doc.xml = XmlDocument.xml;
         if (_isSafarimodel)
         {
           if(typeof(doc.loadXML)=='undefined'){
             doc.loadXML = function (s) {
               // parse the string to a new doc	
               var doc2 = document.sharedinstances.domparser.parseFromString(s, "text/xml");
               // remove all initial children
               while (this.hasChildNodes())
                 this.removeChild(this.lastChild);
               // insert and import nodes
               childNodeCount = doc2.childNodes.length;
               for (var i = 0; i < childNodeCount; i++) {
                 this.appendChild(this.importNode(doc2.childNodes[i], true));
               }
             };
           }
           if(typeof(doc.xml)=='undefined')
             doc.xml = XmlDocument.xml;
         }
         return doc;
      }
      else
      if (window.ActiveXObject)
      {
         return new ActiveXObject(getControlPrefix() + ".XmlDom");
      }
   }
   catch (ex) { alert('XmlDocument.create error: '+ex); }
   return null;
};
