// $Id: xmlhttp.js,v 1.1 2006/01/15 18:09:15 mikeg Exp $

/* ******** XMLHTTP functions ********** */

/* retrieve the content in "path", and call the function callback with
   a single argument of the retrieved content.

   note: this will append "&" or "?" (as appropriate), followed by
   "dummy##########=1" (where # is a unique integer) to the path
   being retrieved to defeat browser caching mechanisms.
*/
function url_callback(path, callback)
{
  callback_description = new String(callback)
  callback_description = callback_description.substr(0, callback_description.indexOf("("))
  saydebug("<b>url_callback: callback="+callback_description + " url="+path+"</b>")
  /* set up fancy xmlhttp stuff.
     note: for more information about using xmlhttp, see:
       http://jibbering.com/2002/4/httprequest.html
       http://webfx.eae.net/dhtml/xmlextras/xmlextras.html
  */
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     xmlhttp = false;
    }
   }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  // prevent chaching...
  if (path.indexOf('?')>=0) { path += "&" } else { path += "?" }
  path += "dummy" + (new Date()).getTime() + "=1"

  // alert("going to "+path+" and putting content into id "+id)

  // prepare the connection in asynchronous mode
  xmlhttp.open("GET", path, true);
  // when the connection's over with, evaluate this function
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      // capture the content
      content = xmlhttp.responseText
      callback(content)
      // put it in the right location
      // puthtml(id, content)
      // annul the status message
      // puthtml('status', '')
    }
  }

  // go
  xmlhttp.send(null)

  return false;
}

function url_callbackN(stuff)
{
  if (stuff.length>0)
  {
    pathcallback = stuff.shift();
    path     = pathcallback[0];
    callback = pathcallback[1];
  
    callback_description = new String(callback);
    callback_description = callback_description.substr(0, callback_description.indexOf("("));
    saydebug("<b>url_callback2: callback="+callback_description + " callbackpath="+callbackpath + " url="+path+"</b>");
  }
}

function url_callback2(path, callback, callbackpath)
{
  callback_description = new String(callback)
  callback_description = callback_description.substr(0, callback_description.indexOf("("))
  saydebug("<b>url_callback2: callback="+callback_description + " callbackpath="+callbackpath + " url="+path+"</b>")
  /* set up fancy xmlhttp stuff.
     note: for more information about using xmlhttp, see:
       http://jibbering.com/2002/4/httprequest.html
       http://webfx.eae.net/dhtml/xmlextras/xmlextras.html
  */
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     xmlhttp = false;
    }
   }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  // prevent chaching...
  if (path.indexOf('?')>=0) { path += "&" } else { path += "?" }
  path += "dummy" + (new Date()).getTime() + "=1"

  // alert("going to "+path+" and putting content into id "+id)

  // prepare the connection in asynchronous mode
  xmlhttp.open("GET", path, true);
  // when the connection's over with, evaluate this function
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      // capture the content
      content = xmlhttp.responseText
      // but ignore it, call the callback instead
      url_callback(callbackpath, update)
    }
  }

  // go
  xmlhttp.send(null)

  return false;
}

