/**
 * The ActiveX objects to use when we want to convert an xml string into a DOM
 * object.
 */
activeX_DOMDocument = ["MSXML.DOMDocument", "Microsoft.XMLDOM", "Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument"];

/**
 * The ActiveX objects to use when we want to do an XMLHttpRequest call.
 */
activeX_XMLHTTP = ["Microsoft.XMLHTTP","MSXML2.XMLHTTP", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0"];


/**
 * Helper to find an ActiveX object that works.
 * @param axarray An array of strings to attempt to create ActiveX objects from
 * @return An ActiveX object from the first string in the array not to die
 */
function _newActiveXObject(axarray) {
  if (typeof ActiveXObject == 'undefined') {
      return null;
  }
  var returnValue = null; 
  for (var i = 0; i < axarray.length; i++) {
    try {
      returnValue = new ActiveXObject(axarray[i]);
      break;
    }
    catch (ex) {
    }
  }
  return returnValue;
};

 function createXMLHttpRequest() {
   var returnValue = _newActiveXObject(activeX_XMLHTTP); 
   if (returnValue == null) {
      try { return new XMLHttpRequest(); } catch(e) {}
   } else {
     return returnValue;
   }
   alert("XMLHttpRequest not supported");
   return null;
 }

 function createXMLHttpRequestXXX() {
     var returnValue = _newActiveXObject(activeX_XMLHTTP); 
     if (returnValue == null) {
        try { return new XMLHttpRequest(); } catch(e) {}
     } else {
       return returnValue;
     }
     alert("XMLHttpRequest not supported");
     return null;
   }

function createXMLActiveXDomParser() {
   return _newActiveXObject(activeX_DOMDocument); 
}


function DtbXmlHttpRequest(callback) {
  this.xmlhttp = null;
  this.callback = callback;
  this.loadingURI = '';
}

DtbXmlHttpRequest.prototype.getXmlHttpRequest = function() {
   if (this.xmlhttp == null) {
      this.xmlhttp = createXMLHttpRequest();
      // Force it to attempt to parse all documents as XML.
      if (this.xmlhttp.overrideMimeType) {
          this.xmlhttp.overrideMimeType('text/xml');
      }
   }
   return this.xmlhttp;
}

DtbXmlHttpRequest.prototype.xmlHttpSend = function (xmlhttp, uri, formStr, callback, context) {

 // Use XMLHttpRequest to asynchronously open a URI, and optionally POST a provided
 // form string if any (otherwise, performs a GET).

 xmlhttp.open(formStr ? 'POST' : 'GET', uri, true);
 xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4)
  {
   var doc = xmlhttp.responseXML;
   if (window.ActiveXObject) { // can't get ie to parse so explicitly calling its dom parser.
//    if (!this.dom) {
//        this.dom = createXMLActiveXDomParser();
//    }
    var dom = createXMLActiveXDomParser();
    var xxx = dom.loadXML(xmlhttp.responseText);
    // What happens on parse fail with IE?
    doc = dom;
    if (doc==null) alert("doc==null")
  }

   // If you are getting an error where 'doc' is null in your own code, try changing
   // the MIME type returned by the server: setting it to text/xml usually works well!
   if (callback) 
       callback(doc, uri, xmlhttp.responseText, context);
  }
 };
 if (formStr && xmlhttp.setRequestHeader)
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 xmlhttp.send(formStr);
 return true;
}



DtbXmlHttpRequest.prototype.load = function(uri, formStr, context) {
   this.loadingURI = uri;
   this.xmlHttpSend(this.getXmlHttpRequest(), uri, formStr, this.callback, context);
   this.loadingURI = '';
};


