/*
 *
 * InfoWest Simple AJAX Handler Class
 *
 * Written by Aaron D. Gifford - http://www.adg.us/
 * Copyright (c) 2005 by InfoWest, Inc. - All Rights Reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, the above list of authors and contributors, this list of
 *    conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author(s) or copyright holder(s) nor the
 *    names of any contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S), AUTHOR(S) AND
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S), AUTHOR(S), OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * DCONSEQUENTIAL AMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

// Global static array of outstanding asynchronous XML requests:
window.IWAqueue = new Array();

function IWAReqDoc(addr, postdata, func) {
  var req = new Object();
  req.xmlobj = null;
  req.callback = func;

  // Does the browser have native support for XMLHttpRequest?
  if (window.XMLHttpRequest) {
    // Yes, so use it!
    req.xmlobj = new XMLHttpRequest();
  // Otherwise, try for MSIE ActiveX:
  } else if (window.ActiveXObject) {
    req.xmlobj = new ActiveXObject('Microsoft.XMLHTTP');
  }
  // Hopefully this worked and we have a request object...
  if (req.xmlobj) {
    req.xmlobj.onreadystatechange = IWAGotDoc;
    window.IWAqueue.push(req);
    if (postdata) {
      req.xmlobj.open('POST', addr, true);
      req.xmlobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      req.xmlobj.send(postdata);
    } else {
      req.xmlobj.open('GET', addr, true);
      req.xmlobj.send(null);
    }
  }
}

function IWAGotDoc() {
  var newq;
  var changed;
  var i;

  newq = new Array();
  changed = 0;

  // Scan all outstanding queries to see if any are complete:
  for (i = 0; i < window.IWAqueue.length; i++) {
    var req = window.IWAqueue[i];

    // Wait for "complete" state (4):
    if (req.xmlobj.readyState == 4) {
      changed = 1;
      if (req.xmlobj.status == 200) {
        // Success!
        req.callback(req.xmlobj.responseText);
      }
    } else {
      newq.push(req);
    }
  }
  if (changed) {
    // Update the list of outstanding queries:
    window.IWAqueue = newq;
  }
}

// Utility for taking an associative array and encoding it for posting:
function IWAEncodeAssoc(a) {
  var data = '';
  var key;
  for (key in a) {
    data += '&' + encodeURI(key) + '=' + a[key];
  }
  return data.substring(1);
}

/*
 * Example usage:
 *   function myCallbackHandler(data) {
 *     ... handle data here ...
 *   }
 *   // Make a GET request:
 *   IWAReqDoc('some.url.here',null,myCallbackHandler);
 *   // Make a POST request:
 *   IWAReqDoc('some.url.here',IWAEncodeAssoc(assoc_array_containg_key_value_data),myCallbackHandler);
 */

