/**
 * @file
 * AJAX module definitions/utilities.
 *
 * @author Shannon M. Rause <shannon.rause@creativeflavor.com>
 * @version $Revision: 1.4 $
 * @version $Name:  $
 * @version $Id: Ajax.js,v 1.4 2006/09/07 03:26:14 smr Exp $
 *
 * Copyright 2005-2006 Creative Flavor, Inc. All rights reserved.
 * 
 * Please see the LICENSE file for specific licensing information.
 * If you did not receive this file or are unable to locate it, 
 * please contact licensing@creativeflavor.com.
 */
/**
 * Ajax class.
 *
 * @public
 */
function Ajax()
{
   /**
    * Creates and returns a new XMLHttpRequest object.
    *
    * @param cb      User function to call when response is received.
    * @param data    Callback data.
    *
    * @return Newly created request object or null on error.
    *
    * @private
    */
   this._createHttp = function()
   {
      var http;

      /*@cc_on
      // IE only.
      @if (@_jscript_version >= 5)
         try
         {
            http = new ActiveXObject("Msxml2.XMLHTTP");
         } // try
         catch (e)
         {
            try 
            {
               http = new ActiveXObject("Microsoft.XMLHTTP");
            } // try
            catch (e2)
            {
               http = null;
            } // catch
         } // catch
      @else
         http = null;
      @end
      @*/
      
      if ((!http) && 
          (typeof XMLHttpRequest != undefined))
      {
         try
         {
            http = new XMLHttpRequest();
         } // try
         catch (e)
         {
            http = null;
         } // catch
      } // if

      return (http);
   } // _createHttp


   /**
    * Creates a request string.
    *
    * @param data Object data to create request with.
    *
    * @return Generated request string.
    *
    * @private
    */
   this._createReq = function(data)
   {
      var req = '';

      if (data)
      {
         for (var param in data)
         {
            if ((isNaN(data[param])) &&
                (!data[param]))
            {
               data[param] = '';
            } // if

            if (typeof(data[param]) == 'object')
            {
               if (!data[param].length)
               {
                  req += '&' + param + '=';
               }
               else
               {
                  for (var i = 0; i < data[param].length; i++)
                  {
                     req += '&' + param + '[]=' + encodeURIComponent(data[param][i]);
                  } // for
               } // else
            } // if
            else
            {
               req += '&' + param + '=' + encodeURIComponent(data[param]);
            } // else
         } // for
      } // if

      return (req);
   } // _createReq


   /**
    * Sends an xml request.
    *
    * @param url     URL to post/get to.
    * @param data    Data to send (must be an object).
    * @param cb      User function to call when response is received.
    * @param cbData  Callback data.
    * @param state   What state (normally 4) to call callback on.
    *
    * @public
    */
   this.sendXml = function(url,
                           data,
                           cb,
                           cbData,
                           state)
   {
      if (data == undefined)
      {
         data = null;
      } // if

      if (cb == undefined)
      {
         cb = null;
      } // if

      if (cbData == undefined)
      {
         cbData = null;
      } // if

      if ((state == null) || (state == undefined))
      {
         state = 4;
      } // if

      var http = this._createHttp();

      if (!http)
      {
         return (false);
      } // if

      if (data)
      {
         var mode = 'POST';
         var req = this._createReq(data);
      } // if
      else
      {
         var mode = 'GET';
         var req = null;
      } // else

      if (cb)
      {
         http.onreadystatechange = function() { Ajax._callback(http, cb, cbData, state, 'object'); }
      } // if

      http.open(mode,
                url,
                true);
      http.setRequestHeader('Content-Type',
                            'application/x-www-form-urlencoded; charset=UTF-8');
      http.send(req);

      return (true);
   } // sendXml
} // Ajax


/**
 * Callback that will be called when a response is received.
 * Note that this is called before (and actually calls) the user's
 * callback function.
 *
 * @param http    XmlHttpRequest object.
 * @param cb      User function to call when response is received.
 * @param cbData  Callback data.
 * @param state   What state (normally 4) to call callback on.
 * @param cbType  Currently only supports 'object'.
 *
 * @private
 */
Ajax._callback = function(http,
                          cb,
                          cbData,
                          state,
                          cbType)
{
   if ((http) &&
       (http.readyState == state))
   {
      if (cb)
      {
         if (cbData == undefined)
         {
            cbData = null;
         } // if

         switch (cbType)
         {
            case null:
            case undefined:
            case 'object':
               if (http.status == 200)
               {
                  var obj = Utils.xmlToObject(http.responseXML);
               } // if
               else
               {
                  var obj = null;
               } // else

               cb(obj,
                  cbData,
                  http.status,
                  http.statusText);
               break;
         } // switch
      } // if
   } // if
} // Ajax._callback
