/*
  Java Script file: ajax.js
       Description: This JS file contains JavaScript functions that can be
                    be used to develop AJAX functionality


  Modification History:
  Date         By   Version   Description
  Sep 02 2008  GL   v1.0      Created functions to handle processing of
                              async call back from a web server
*/

  var ajaxHTTP;

  /*
     Function: AJAXGetBrowser
      Purpose: Identifies the type of browser currently in use

    Called by: AJAXGetXMLHTTP

  */
  function AJAXGetBrowser(){
    var browserTypeText;
    var isIE;
    var isIE5;
    var isOpera;
    var isNetscape;

    browserTypeText = "";
    isIE = 0;
    isIE5 = 0;
    isOpera = 0;
    isNetscape = 0;

    if (navigator.userAgent.indexOf('MSIE') >= 0){
      isIE = 1;
      browserTypeText = "IE";
    }
    if (navigator.appVersion.indexOf("MSIE 5.5")!=-1){
     isIE5 = 1;
     browserTypeText = "IE5";
    }
    if ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)){
      isOpera = 1;
      browserTypeText = "OPERA";
    }
    if (navigator.userAgent.indexOf('Netscape') >= 0){
      isNetscape = 1;
      browserTypeText = "NETSCAPE";

    }

    if (navigator.userAgent.indexOf('Firefox') >= 0){
      isNetscape = 1;
      browserTypeText = "FIREFOX";
    }
    if (navigator.userAgent.indexOf('Chrome') >= 0){
      isNetscape = 1;
      browserTypeText = "CHROME";
    }
    

    return browserTypeText
  } // End of AJAXGetBrowser function

  /*
     Function: AJAXGetXMLHTTP
      Purpose: Creates a HTTP messaging object that is mapped to the given
               event handler. When this HTTP object has finished processing
               a request (such as GET) the mapped event handler method will
               be invoked.

       Calls: AJAXGetBrowser
  */
  function AJAXGetXMLHTTP(handler) {
     var objXmlHttp;    //Holds the local xmlHTTP object instance
     var browserType;
     var xmlHTTPObjectName;
     var errorMsg;

     objXMLHttp = null;
     browserType = AJAXGetBrowser();

     if (browserType == "IE"){
       xmlHTTPObjectName = "Msxml2.XMLHTTP";
       try{
           objXmlHttp = new ActiveXObject(xmlHTTPObjectName);
           objXmlHttp.onreadystatechange = handler;
        }
        catch(e){
          errorMsg = "Unable to set up the web page to support the IE browser. " +
                     "Please check that active scripting and ActiveX controls are enabled " +
                     "for your browser.";
          alert(errorMsg);
          return;
        }
     } // Determined that the browser is IE
     else if (browserType =="IE5"){
       xmlHTTPObjectName = "Microsoft.XMLHTTP";
       try{
           objXmlHttp = new ActiveXObject(xmlHTTPObjectName);
           objXmlHttp.onreadystatechange = handler;
        }
        catch(e){
          errorMsg = "Unable to set up the web page to support the IE5 browser. " +
                     "Please check that active scripting and ActiveX controls are enabled " +
                     "for your browser.";
          alert(errorMsg);
          return;
        }

     } // Determined that the browser is IE5
     else if (browserType == "NETSCAPE" || browserType == "FIREFOX" || browserType == "CHROME"){
        objXmlHttp = new XMLHttpRequest();
        objXmlHttp.onload = handler;
        objXmlHttp.onerror = handler;
     } // Determined that the browser is Netscape or Firefox
     else if (browserType == "OPERA"){
       errorMsg = "It is most likely that you are using the Opera browser." +
                  " Unfortunately this web page does not support this type of browser.";
       alert(errorMsg);
     }
     return objXmlHttp;

  } // End of AJAXGetXMLHTTP


  /*
     Function: AJAXSendHTTPRequest
      Purpose: Sets up the given HTTP message object to make a request (GET or POST)
               to a web server (at the given URL).


    Parameter:
      xmlHTTP .......... HTTP messaging object that is presumably mapped to
                         an event handler (for handling the the async call back
                         from a web server)
      url .............. URL of web server
      httpRequestType .. Typically a GET request
      textData ......... Text data to send if using POST method

  */
  function AJAXSendHTTPRequest(xmlHTTP, url, httpRequestType, textData) {
    if (xmlHTTP != null && url != null && httpRequestType != null){
      try{
        if (httpRequestType == "GET"){
          xmlHTTP.open(httpRequestType, url, true);
          xmlHTTP.send(null);
        }
        else if (httpRequestType =="POST"){
          xmlHTTP.open(httpRequestType, url, true);
          xmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          xmlHTTP.setRequestHeader("IsAJAXPostRequest", "true");

          xmlHTTP.send(textData);
        }
      }
      catch(exp){
        AJAXShowError(exp,
                      'AJAXSendHTTPRequest',
                      'sending a HTTP request');

      }
    } // Check if the HTTP object is defined
  } // End of AJAXSendHTTPRequest method


  /*
     Function: HTTPSubmitGetRequest
      Purpose: Submits an asynchronous HTTP request back to the web page
               with the given query string. Upon call back from the
               asynchronous request, the given handler method will be
               invoked

        Calls: AJAXGetXMLHTTP
               AJAXSendHTTPRequest
               
    Parameter:
      url .............. URL of web server
      query ............ Query string to be appended to the URL when submitting a
                         HTTP GET request
      handler .......... JavaScript function that will be invoked upon an
                         AJAX call back

  */
  function HTTPSubmitGetRequest(url, query, handler){
    var isSubmitSuccessful;
    var submitQuery;
    var errorMessage;
    var process;

    isSubmitSuccessful = false;
    errorMessage = "Fail to submit a HTTP GET request. ";
    process = "submitting an asynchronous (AJAX) HTTP GET request";
    if (url != null){
      try{
        // NOTE: ajaxHTTP is already declared in ajax.js
        ajaxHTTP = AJAXGetXMLHTTP(handler);
        submitQuery = "";
        if (query!= null){
          submitQuery = query;
        }
        AJAXSendHTTPRequest(ajaxHTTP, url+submitQuery, "GET", "");
        isSubmitSuccessful = true;
      }
      catch(exp){
        document.body.style.cursor="default";
        errorMessage += exp;
        AJAXShowError(errorMessage,
                      'HTTPSubmitGetRequest',
                      process);

      }
    }
    else{
      errorMessage += "The URL for submitting the HTTP request is not defined.";
      AJAXShowError(errorMessage,
                    'HTTPSubmitGetRequest',
                    process);

    } // Check if the parameter is defined
    return isSubmitSuccessful;

  } // End of HTTPSubmitGetRequest function


  /*
     Function: HTTPSubmitPostRequest
      Purpose: Submits an asynchronous HTTP POST request back to the web page,
               sending the given text data. Upon call back from the asynchronous
               request, the given handler method will be invoked

        Calls: AJAXGetXMLHTTP
               AJAXSendHTTPRequest

    Parameter:
      url .............. URL of web server
      textData ......... Data to be sent to the web server in the HTTP POST
                         request
      handler .......... JavaScript function that will be invoked upon an
                         AJAX call back

  */
  function HTTPSubmitPostRequest(url, textData, handler){
    var isSubmitSuccessful;
    var submitQuery;
    var errorMessage;
    var process;

    isSubmitSuccessful = false;
    errorMessage = "Fail to submit a HTTP POST request. ";
    process = "submitting an asynchronous (AJAX) HTTP POST request";
    if (url != null){
      try{
        // NOTE: ajaxHTTP is already declared in ajax.js
        ajaxHTTP = AJAXGetXMLHTTP(handler);
        AJAXSendHTTPRequest(ajaxHTTP, url, "POST", textData);
        isSubmitSuccessful = true;
      }
      catch(exp){
        document.body.style.cursor="default";
        errorMessage += exp;
        AJAXShowError(errorMessage,
                      'HTTPSubmitPostRequest',
                      process);
      }
    }
    else{
      errorMessage += "The URL for submitting the HTTP request is not defined.";
      AJAXShowError(errorMessage,
                    'HTTPSubmitPostRequest',
                    process);

    } // Check if the parameter is defined
    return isSubmitSuccessful;

  } // End of HTTPSubmitGetRequest function

  /*
      Function: AJAXShowError
       Purpose: Displays an error message reporting the error that
                occured

      Parameter:
        exp ................. Exception object
        jsMethod ............ JS function in which error occured
        process ............. Text description of the process under way when
                              the error occured
  */
  function AJAXShowError(exp, jsMethod, process){
    var errorMessage;

    if (exp != null && jsMethod!= null && process != null){
      errorMessage = "Fail to complete a certain AJAX process. An error has occured while " + process + ".\n";
      errorMessage += "This error occured within the method, " + jsMethod + "\n\n";
      errorMessage += "Error message: \n";
      errorMessage += exp;
      alert(errorMessage);
    } // Check that parameters are defined

  }
