/*
AjaxCall.js v1.0, Wed Nov 01 10:25am CST 2006
Copyright AllenPages.com 2006 (www.allenpages.com).

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

function AjaxCall()
{
  this.async = async;
  this.sync = sync;
  
  /*
  * function async(myUrlWithArgs, postData, callbackFunction, failureFunction)
  * Parameters:
  *   myUrlWithArgs: The absolute or relative URL to the document you wish to retrieve.
  *   postData: The data you wish to send via the POST method. You must format this 
  *             in the proper URL encoded scheme. If you pass NULL instead, the method
  *             is switched to GET and no data will be sent. If you wish to use the GET
  *             method, pass the arguements through the myUrlWithArgs parameter.
  *   callbackFunction: The name of a function to be called when the data has been
  *             retrieved. This string gets run though eval() which causes the function
  *             to execute. The function must take an argument to receive the xml from
  *             the responce (i.e., function myFunction(xmlResponse){}). If the 
  *             callbackFunction is not specified, it is assumed we are just pushing 
  *             data to the server and don't need anything else to happen.
  *   failureFunction: The name of a function to be called if the data can not be 
  *             retrieved. This string will be run through eval, causing the function
  *             to execute. If the failureFunction is not specified, a generic function
  *             will be called.
  *
  * Return: void
  */
  function async(myUrlWithArgs, postData, callbackFunction, failureFunction)
  { 
    var reqAsync = null;
    var methodType = "";
    
    //check arguments
    if(myUrlWithArgs == null || myUrlWithArgs == "")
    { alert("FAILURE: No url specified. Please report this problem to the Help Desk"); return; }
    
    if(postData == null)
    { methodType = "GET"; }
    else
    { methodType = "POST"; }
    
    if(callbackFunction == null)
    { callbackFunction = genericAjaxCallbackFunction; }
    
    if(failureFunction == null)
    { failureFunction = genericAjaxFailureFunction; }
    
    //build the URL
    var urlArgBaseSeparator = "&";
    if(myUrlWithArgs.indexOf("?") == -1)
    { urlArgBaseSeparator = "?"; }
        
    var dateMillis = (new Date()).getMilliseconds(); //force url to refresh, no caching
    var url = (myUrlWithArgs + urlArgBaseSeparator + "noCaching=" + dateMillis);
    
    //create an XMLHttpRequest object
    try 
    {
      reqAsync=new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
      try 
      {
        reqAsync=new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch (oc) 
      {
        reqAsync=null;
      }
    }
    
    if(!reqAsync && typeof XMLHttpRequest != "undefined")
    { reqAsync = new XMLHttpRequest(); }
    
    if (!reqAsync)
    { alert("FAILURE: You must be using IE 5.5+, Netscape 7.0+, or Firefox 1.0+."); return; }

    //open the connection to the url
    reqAsync.open(methodType, url, true);
    
    //detect state changes
    reqAsync.onreadystatechange = function()
      { 
        if (reqAsync.readyState == 4) 
        {
          if (reqAsync.status == 200) 
          {
            var xmlResponse = reqAsync.responseXML;
            callbackFunction(reqAsync);
          }
          else
          {
            failureFunction(reqAsync.status);
          }
          reqAsync = null;
        }
      }
    
    //send the request
    if(methodType == "GET")
    { reqAsync.send(null); }
    else if(methodType == "POST")
    {
      reqAsync.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      reqAsync.send(postData);
    }

  }

  /*
  * function sync(myUrlWithArgs, postData)
  * Parameters:
  *   myUrlWithArgs: The absolute or relative URL to the document you wish to retrieve.
  *   postData: The data you wish to send via the POST method. You must format this 
  *             in the proper URL encoded scheme. If you pass NULL instead, the method
  *             is switched to GET and no data will be sent. If you wish to use the GET
  *             method, pass the arguements through the myUrlWithArgs parameter.
  *
  * Return: xml  //null if there is an error
  *
  * WARNING: Using the sync method will cause the JavaScript engine to be blocked until 
  *          the interaction with the server is complete. It works particularly well 
  *          when the server is on the same machine, or nearby on the LAN. Unfortunately,
  *          it can perform very badly if the server is under heavy load, or if the 
  *          browser is connected to the server over a slow link. Because the JavaScript
  *          engine is blocked until the request completes, the browser will be frozen. 
  *          The user cannot cancel the request, cannot click away, cannot go to another
  *          tab. Strong consideration must be used when deciding to use this method.
  *
  */
  function sync(myUrlWithArgs, postData)
  {
    var reqSync = null;
    var methodType = "";
    
    //check arguments
    if(myUrlWithArgs == null || myUrlWithArgs == "")
    { alert("FAILURE: No url specified. Please report this problem to the Help Desk"); return null; }
    
    if(postData == null)
    { methodType = "GET"; }
    else
    { methodType = "POST"; }
    
    //build the URL
    var urlArgBaseSeparator = "&";
    if(myUrlWithArgs.indexOf("?") == -1)
    { urlArgBaseSeparator = "?"; }
        
    var dateMillis = (new Date()).getMilliseconds(); //force url to refresh, no caching
    var url = (myUrlWithArgs + urlArgBaseSeparator + "noCaching=" + dateMillis);
    
    //create an XMLHttpRequest object
    try 
    {
      reqSync=new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
      try 
      {
        reqSync=new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch (oc) 
      {
        reqSync=null;
      }
    }
    
    if(!reqSync && typeof XMLHttpRequest != "undefined")
    { reqSync = new XMLHttpRequest(); }
    
    if (!reqSync)
    { alert("FAILURE: You must be using IE 5.5+, Netscape 6.0+, or Firefox 1.0+."); return null; }
    
    
    //open the connection to the url
    reqSync.open(methodType, url, false);
    
    //send the request
    if(methodType == "GET")
    { reqSync.send(null); }
    else if(methodType == "POST")
    {
      reqSync.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      reqSync.send(postData);
    }
    
    //get the response
    if (reqSync.status == 200) 
    {
      return reqSync;
    }
    else
    {
      return null;
    }
  }
}  

/*
* function genericAjaxCallbackFunction()
* Parameters: none
* Return: void
*/
function genericAjaxCallbackFunction()
{
  //do nothing
}

/*
* function genericAjaxFailureFunction(statusCode)
* Parameters:
*   statusCode: The HTTP error code.
*
* Return: void
*
*/
function genericAjaxFailureFunction(statusCode)
{
  alert("FAILURE: A problem was encountered.\nHTTP Error Code: " + statusCode);
}

