/*
 * Filename: ajaxUtils.js
 * Description: Utility functions for handling AJAX and
 *   database-driven web content.
 *
 * Last Modified: 2007-10-10 12:00 PDT
 * Author: Paul Reuter
 * Version: 1.2
 *
 * Reference: 
 *   John Wiseman
 *   http://www.johnwiseman.ca/
 *
 */


/*
 * Function: createXMLHttpRequest()
 * Description: Returns an XMLHttpRequest object
 *  that is best suited to the browser being used.
 */
function createXMLHttpRequest() {
  var ua;
  if(window.XMLHttpRequest) {
    try {
      ua = new XMLHttpRequest();
    } catch(e) {
      ua = false;
    }
  } else if(window.ActiveXObject) {
    try {
      ua = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ua = false;
    }
  }
  return ua;
}




/*
 * Function: readyStateLookup(state) 
 * Description: returns text representation of what the 
 *  loading code represents.
 */
function readyStateLookup(state) { 
  switch(state) { 
    case 0: 
      return "uninitialized";
    case 1:
      return "loading";
    case 2:
      return "loaded";
    case 3:
      return "interactive";
    case 4:
      return "complete";
    default:
      return "error";
  }
}


/*
 * Function: ajaxRequest(url,func)
 * Description: Performs a remote ajax request on the url
 *  and calls function func when completed successfully.
 * Params: 
 *  url -- a complete url to some document to read
 *  func -- a function that accepts data as its parameter.
 */
function ajaxRequest(url,func,asyc) {
  if( typeof(asyc) == "undefined" ) { 
    asyc = true;
  } 

  var req = createXMLHttpRequest();
  if(!req) {
    return false;
  }
  req.onreadystatechange = function() {
    if(req.readyState==4) { 
      func(req.responseText,req.status);
    }
  }
  req.open("get",url,asyc);
  req.send(null);
}

