/**
 *  AJAX Functionality
 *
**/

var ajaxIsEnabled;
var ajaxID          = 0;
var ajaxRequests    = new Array();
var ajaxData        = new Array();
var ajaxFunctions   = new Array();
var ajaxBuffers     = new Array();

var ajaxListeners   = new Array();

// This is a basic AJAX script template
function runAjaxCode(fileRequest, syncRequest, requestMethod, requestParams, dataFunction) {

  if( ajaxIsEnabled == false ) return false;

  // Set up an entry in the AJAX data array that will hold the response
  ajaxID++; // increment the ID for the next request

  ajaxRequests[ajaxID]  = null;
  ajaxData[ajaxID]      = null;

  try {
  // Try for Opera, Firefox, and Safari
    ajaxRequests[ajaxID] = new XMLHttpRequest();

  } catch(e) {
    try {
    // Try for Internet Explorer
      ajaxRequests[ajaxID] = new ActiveXObject("Msxml2.XMLHTTP");

    } catch(e) {
      try {
      // Alternate method for Internet Explorer
        ajaxRequests[ajaxID] = new ActiveXObject("Microsoft.XMLHTTP");

      } catch(e) {
      // Browser does not support AJAX
        ajaxIsEnabled = false;
        return false;
      }
    }
  }

  // Determine the request method used for this request and
  // check if there are any parameters to be passed along
  requestMethod   = requestMethod == 'POST' ? requestMethod : 'GET';
  requestParams   = !requestParams ? null : requestParams;

  // Detect what sync-mode should be used
  // false means that the JS will continue while the request is running
  // anything else (including undefined) means JS will wait for the request
  syncRequest     = syncRequest != false ? true : false;



  // Start an alternate listener in case the browser does not
  // support the normal one. This step is only needed in FF.
  if( userBrowser == 'FF' ) {

    if( dataFunction != undefined && window[dataFunction] ) {
      ajaxFunctions[ajaxID] = dataFunction;
    }

    ajaxBuffers[ajaxID]     = 0;

    ajaxListeners[ajaxID]   = setInterval('ajaxListener(' + ajaxID + ')', 100);

  }
  // ! alternate listener


  ajaxRequests[ajaxID].open(requestMethod, fileRequest, syncRequest);

  if( requestMethod == 'POST' ) {
  // Add some additional headers to the request if POST is used
    ajaxRequests[ajaxID].setRequestHeader('Content-Type',  'application/x-www-form-urlencoded;');
    ajaxRequests[ajaxID].setRequestHeader('Connection',    'close');
  }

  if( syncRequest == false ) {
  // For asynchronous requests, keep the status updated
    // 0 - Not Initialized
    // 1 - Loading
    // 2 - Loaded
    // 3 - Interactive
    // 4 - Complete
    ajaxRequests[ajaxID].onreadystatechange = function() {

      switch(ajaxRequests[ajaxID].readyState) {
      case 1: // Loading

      break;
      case 2: // Loaded

      break;
      case 3: // Interactive

      break;
      case 4: // Complete

        switch(ajaxRequests[ajaxID].status) {
        case 200:
          ajaxData[ajaxID]      = ajaxRequests[ajaxID].responseText;

          // Run a function on the returned content?
          if( dataFunction != undefined && window[dataFunction] ) {

            var functionCode    = dataFunction + '(ajaxData[' + ajaxID + ']);';
            setTimeout(functionCode, 0);
            // eval(functionCode);

          } // ! function?

        break;
        } // ! switch (request status)

      break;
      } // ! switch (request state)

    } // ! monitor ready state
  } // ! asynchronous?

  // Send the AJAX request
  ajaxRequests[ajaxID].send(requestParams);

  return ajaxID;

} // end of AJAX script





/** ajaxListener()
 *
 *  Alternate listener
 *  Only works when calling a function through the AJAX request as
 *  normal execution continues after starting the listener. 
 *
**/
function ajaxListener(ajaxID) {

  if( ajaxRequests[ajaxID].responseText ) {

    var ajaxBufferLength  = ajaxRequests[ajaxID].responseText.length;

    if( ajaxBufferLength != ajaxBuffers[ajaxID] ) {
      ajaxBuffers[ajaxID] = ajaxBufferLength;
      return false;
      // Still receiving data...
    }

    ajaxData[ajaxID]    = ajaxRequests[ajaxID].responseText;

    clearInterval(ajaxListeners[ajaxID]);

    if( ajaxFunctions[ajaxID] ) {

      var functionCode  = ajaxFunctions[ajaxID] + '(ajaxData[' + ajaxID + '])';
      setTimeout(functionCode, 0);

    }

    return true;

  }

  return false;

} // ! ajaxListener()



