////////////////////////////////////////////////////////////////////////////////
//      File: XMLHTTPUtils
//       Des: utility file for making backend data requests via XMLHTTP (ie, ajax)
//     Notes:
//      Todo:
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//  Function: issueRequest
//       Des: issue an ajax request
//     Param: method             - either "GET" or "POST"
//     Param: isAsync            - boolean determining if async request or not
//     Param: url                - url (include query string if GET)
//     Param: content            - contents to send to server if POST
//     Param: callbackFunction   - code to be invoked when call returns
//   Returns: void
//     Notes:
//      Bugs:
//      Todo:
////////////////////////////////////////////////////////////////////////////////
function issueRequest(method, isAsync, url, content, callbackFunction)
{
   var xmlHTTP;
   if(window.XMLHttpRequest)
   {
      ///////////////////////////////////////////////////
      // code for IE7+, Firefox, Chrome, Opera, Safari //
      ///////////////////////////////////////////////////
      xmlHTTP = new XMLHttpRequest();
   }
   else
   {
      try
      {
         ///////////////////////
         // code for IE5, IE6 //
         ///////////////////////
         xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
         alert("ERROR:  Your browser does not support XMLHTTP.");
         return(null);
      }
   }

   ////////////////////////////////////////////////////////////
   // put a nochache onto the params to help prevent caching //
   ////////////////////////////////////////////////////////////
   if(url.indexOf("?") > 0)
   {
      url      += "&_no_cache=" + (new Date()).getTime();
      content  += "&_no_cache=" + (new Date()).getTime();
   }
   else
   {
      url += "?_no_cache=" + (new Date()).getTime();
   }

   if(method.toLowerCase() != "get" && method.toLowerCase() != "post")
   {
      alert("XMLHTTPUtils.issueRequest() : invalid method found [" + method + "]");
      return(null);
   }

   ////////////////////////////////
   // setup the response handler //
   ////////////////////////////////
   if(isAsync)
   {
      xmlHTTP.onreadystatechange = function()
      {
         if(xmlHTTP.readyState == 4)
         {
            handleResponse(xmlHTTP, callbackFunction);
         }
      }
   }

   ///////////////////////
   // issue the request //
   ///////////////////////
   try
   {
      xmlHTTP.open(method, url, isAsync);
      xmlHTTP.send(content);
   }
   catch(e)
   {
      alert("An error occurred : " + e);
   }

   ////////////////////////////////////////////////
   // make the callback for syncrhonous requests //
   ////////////////////////////////////////////////
   if(! isAsync)
   {
      handleResponse(xmlHTTP, callbackFunction);
   }
}



////////////////////////////////////////////////////////////////////////////////
//  Function: handleResponse
//       Des: handle the response from an ajax call
//     Param: xmlHTTP
//     Param: callbackFunction
//   Returns: void
//     Notes: Only Meant to be called by issueRequest
//      Bugs:
//      Todo:
////////////////////////////////////////////////////////////////////////////////
function handleResponse(xmlHTTP, callbackFunction)
{
   if(xmlHTTP.status != 200)
   {
      alert("An error occurred making an xml http request : status [" + xmlHTTP.status + "] - " + xmlHTTP.statusText);
      return;
   }

   if(callbackFunction)
   {
      ////////////////////////////////////////////////////////
      // return the xmlHTTP object to the callback function //
      ////////////////////////////////////////////////////////
      callbackFunction(xmlHTTP);
   }
}


