





function cback_ajaxMessageReceived()
{
   var idx = this.ajaxIDX;

   switch (ajaxRequests[idx].ajaxHTTPRequest.readyState)
   {
      case 4:
      {
         // everything is good, the response is received
         if (ajaxRequests[idx].ajaxHTTPRequest.status == 200)
         {
            // Load the XML
            if (ajaxRequests[idx].ajaxMessageXMLRequired)
            {
               var doc = null;
               if (window.ActiveXObject)
               {
                  // code for IE
                  try
                  {
                     doc = new ActiveXObject("Microsoft.XMLDOM");
                  }
                  catch (ignore)
                  {
                     alert("Error: " + ignore.description + "\n\nThis browser will not allow an ActiveXObject (Microsoft.XMLDOM) to be created\nto power AJAX communication with the server.\n\nThis is most likely to be a security setting in the browser configuration.");
                     return;
                  }
                  doc.async = "false";
                  doc.loadXML(ajaxRequests[idx].ajaxHTTPRequest.responseText);
               }
               else
               {
                  // code for Mozilla, Firefox, Opera, etc.
                  var parser = new DOMParser();
                  doc = parser.parseFromString(ajaxRequests[idx].ajaxHTTPRequest.responseText,"text/xml");
               }
               ajaxRequests[idx].ajaxMessageXMLDoc = doc.documentElement;

               if (ajaxRequests[idx].ajaxMessageXMLDoc.nodeName == "Exception")
               {
                  var clss = ajaxRequests[idx].ajaxMessageXMLDoc.getElementsByTagName('Class')[0].childNodes[0].nodeValue;
                  var message = "Unknown reason";
                  if (ajaxRequests[idx].ajaxMessageXMLDoc.getElementsByTagName('Message')[0].childNodes.length >= 1)
                     message = ajaxRequests[idx].ajaxMessageXMLDoc.getElementsByTagName('Message')[0].childNodes[0].nodeValue;
                  alert("Error (" + clss + "): " + message);
                  ajaxRequests[idx].ajaxMessageCallback = null;
               }
            }
            else
               ajaxRequests[idx].ajaxMessageXMLDoc = null;

            if (ajaxRequests[idx].ajaxMessageCallback != null)
            {
               var cback = ajaxRequests[idx].ajaxMessageCallback.replace(/ajaxMessageXMLDoc/, "ajaxRequests[idx].ajaxMessageXMLDoc");
               cback = cback.replace(/ajaxMessageUserContext/, "ajaxRequests[idx].userContext");
               eval(cback);
            }
            ajaxRequests[idx].ajaxMessageCallback = null;
         }
         else
         {
            // there was a problem with the request,
            // for example the response may be a 404 (Not Found)
            // or 500 (Internal Server Error) response codes
            alert("AJAX request error, status=" + ajaxRequests[idx].ajaxHTTPRequest.status + "\nURL=" + ajaxRequests[idx].ajaxURL);
         }

         ajaxRequests[idx] = null;
         return;
      }

      default:
         return;
   }
}

function AjaxRequest(idx)
{
   this.ajaxIDX = idx;
   this.ajaxHTTPRequest = null;
   this.ajaxMessageXMLDoc = null;
   this.ajaxMessageCallback = null;
   this.ajaxMessageXMLRequired = false;
   this.ajaxMessageReceived = cback_ajaxMessageReceived;
   this.ajaxURL = null;
}

var ajaxRequests = new Array();
var ajaxRequestsIndex = 0;
function ajaxMessageSend(method, args, callback, xml_required, userContext, url, httpMethod)
{
   if (httpMethod == null || httpMethod == "undefined") httpMethod = "POST";

   // Generate the URL
   if (url == null || url == "undefined")
   {
      url = document.location.toString();
      var re = new RegExp("^(http(s){0,1}\:\/\/[^\?]{1,})");
      var m = re.exec(url);
      url = m[1];
   }
   else if (url.match(/^http.*$/) == null)
   {
      var re = new RegExp("^(http(s){0,1}\:\/\/[^\/]{1,})");
      var m = re.exec(document.location.toString());
      url = m[1] + url;
   }

   // Get session authenticators
   var cookie = getCookie();
   var sessionApplication = getSessionApplication();

   // Create a new AjaxRequest





   var idx = ajaxRequestsIndex++;
   ajaxRequests[idx] = new AjaxRequest(idx);
   ajaxRequests[idx].ajaxMessageCallback = callback;
   ajaxRequests[idx].ajaxMessageXMLRequired = xml_required;
   ajaxRequests[idx].userContext = (userContext == null || userContext == "undefined" ? null : userContext);
   ajaxRequests[idx].ajaxURL = url;

   if (window.XMLHttpRequest)
   {
      // Mozilla, Safari, ...
      ajaxRequests[idx].ajaxHTTPRequest = new XMLHttpRequest();
      if (ajaxRequests[idx].ajaxHTTPRequest.overrideMimeType) ajaxRequests[idx].ajaxHTTPRequest.overrideMimeType('text/xml');
   }
   else if (window.ActiveXObject)
   {
      // IE
      try
      {
         ajaxRequests[idx].ajaxHTTPRequest = new ActiveXObject("MSXML2.XMLHTTP");
      }
      catch (e)
      {
         try
         {
            ajaxRequests[idx].ajaxHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (ignore)
         {
            alert("Error: " + ignore.description + "\n\nThis browser will not allow an ActiveXObject (MSXML2.XMLHTTP / Microsoft.XMLHTTP) to be created\nto power AJAX communication with the server.\n\nThis is most likely to be a security setting in the browser configuration.");
            return(false);
         }
      }
   }

   if (!ajaxRequests[idx].ajaxHTTPRequest)
   {
      alert('Failed to create an XMLHTTP instance');
      return(false);
   }

   try
   {
      var qs = "jaction=" + method + "&cookie=" + cookie + "&session.application=" + sessionApplication + "&" + args;
      if (httpMethod == "GET") url = url + "?" + qs;

      var self = ajaxRequests[idx]; // Fix loss-of-scope in inner function 
      ajaxRequests[idx].ajaxHTTPRequest.open(httpMethod, url, true);
      ajaxRequests[idx].ajaxHTTPRequest.onreadystatechange = function() { self.ajaxMessageReceived(); }
      ajaxRequests[idx].ajaxHTTPRequest.setRequestHeader('X-Reference-Index', idx);
      ajaxRequests[idx].ajaxHTTPRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      ajaxRequests[idx].ajaxHTTPRequest.send(httpMethod == "GET" ? "" : qs);
   }
   catch (e)
   {
      alert("Failed to send AJAX request.\nURL: " + url + "\n\nError type: " + e.name + "\nError message: " + e.message);
   }

   return(true);
}

