function ajaxQueueHandler() {
  ajaxQueue = this;
  this.queue = new Array();
  this.queueResponseType = new Array();
  this.queueResponseElem = new Array();
  this.ajaxArray = new Array();
  
  this.getHTTPObject = function() {
    if( typeof XMLHttpRequest != "undefined" ) return new XMLHttpRequest();
    try {
      return new ActiveXObject( "Msxml2.XMLHTTP" );
    } catch (e) {
      try {
        return new ActiveXObject( "Microsoft.XMLHTTP" );
      } catch (e) { }
    }
    return false;
  }
  
  this.getAjaxIndex = function() {
    var index = 0; 
    // Find an empty slot in the AJAX array, max 10 calls
    while( index < 10 ) {
      if( !ajaxQueue.ajaxArray[index] || ( ajaxQueue.ajaxArray[index].readyState == 4 && ajaxQueue.ajaxArray[index].status == 200 ) ) {
        ajaxQueue.ajaxArray[index] = ajaxQueue.getHTTPObject();
        break;
      } else index++;
    }
    
    if( index < 10 ) return index;
    else return false;
  }
  
  this.AjaxCall = function( url, responseType, responseElementId ) {
    var index = this.getAjaxIndex();
    
    if( index === false ) return false;
    
    // url = url + "?" + params;
    
    ajaxQueue.ajaxArray[index].open( "GET", url, true );
    ajaxQueue.ajaxArray[index].onreadystatechange = function() {
      if( ajaxQueue.ajaxArray[index].readyState == 4 ) {
        if( ajaxQueue.ajaxArray[index].status == 200 ) {
          // responseType: 0 = no response, 1 = overwrite element contents, 2 = append to element
          if( responseType > 0 ) {
            var temp;
            if( responseType == 1 ) temp = ajaxQueue.ajaxArray[index].responseText;
            if( responseType == 2 ) temp = document.getElementById( responseElementId ).innerHTML + ajaxQueue.ajaxArray[index].responseText;
            document.getElementById( responseElementId ).innerHTML = temp;
          }
          ajaxQueue.ajaxArray[index] = null;
        }
      }
    }
    ajaxQueue.ajaxArray[index].send(null);
    
    return true;
  }
  
  this.handle = function() {
    /*
    if( this.queueElement.innerHTML.length <= 0 ) return false;
    this.queue = this.queueElement.innerHTML.split("|");
    */
    
    var len = this.queue.length;
    for( var i = 0; i < len; ++i ) {
      while( this.queue[i] != false ) {
        if( this.AjaxCall( this.queue[i], this.queueResponseType[i], this.queueResponseElem[i] ) ) this.queue[i] = false;
      }
      // if ajax call fails, leave the rest of the queue as is and wait for the next iteration
      // i.e. re-fill queueElement with this.queue[i] + "|" etc
    }
    
    newQueue = new Array();
    newQueueResponseType = new Array();
    newQueueResponseElem = new Array();
    
    for( var i = 0; i < len; ++i ) {
      if( this.queue[i] !== false ) {
        newQueue.push( this.queue[i] );
        newQueueResponseType.push( this.queueResponseType[i] );
        newQueueResponseElem.push( this.queueResponseElem[i] );
      }
    }
    this.queue = newQueue;
    this.queueResponseType = newQueueResponseType;
    this.queueResponseElem = newQueueResponseElem;
    // this.queueElement.innerHTML = newQueue.join( "|" );
  }
  
  this.queueCommand = function( url, params, responseType, responseElem ) {
    /*
    var text = this.queueElement.innerHTML;
    this.queueElement.innerHTML = text + "|" + value;
    */
    this.queue.push( url + "?" + params );
    this.queueResponseType.push( responseType );
    this.queueResponseElem.push( responseElem );
  }
  
  this.init = function() {
    // check command queue every second
    setInterval( function() { ajaxQueue.handle(); }, 500 );
    // this.queueElement = document.getElementById( "ajaxQueue" );
  }
}

// ajaxQueue.init();
