  // holds an instance of XMLHttpRequest
  var xmlHttp = createXmlHttpRequestObject();
  
  // when set to true, display detailed error messages
  var showErrors = true;
  
  // initialize the validation requests cache 
  var cache             = new Array();
  var cache_request_uri = new Array();
  
  var depth = 0;
  
  var request_uri = '';
  var action_type = '';
  
  // creates an XMLHttpRequest instance
  function createXmlHttpRequestObject() 
  {
      // will store the reference to the XMLHttpRequest object
      var xmlHttp;
      // this should work for all browsers except IE6 and older
      try
      {
          // try to create XMLHttpRequest object
          xmlHttp = new XMLHttpRequest();
      }
      catch(e)
      {
          // assume IE6 or older
          var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                          "MSXML2.XMLHTTP.5.0",
                                          "MSXML2.XMLHTTP.4.0",
                                          "MSXML2.XMLHTTP.3.0",
                                          "MSXML2.XMLHTTP",
                                          "Microsoft.XMLHTTP");
          // try every id until one works
          for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
          {
              try 
              { 
                  // try to create XMLHttpRequest object
                  xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
              } 
              catch (e) {} // ignore potential error
          }
      }
      // return the created object or display an error message
      if (!xmlHttp)
        displayError("Error creating the XMLHttpRequest object.");
      else 
        return xmlHttp;
  }
  
  
  /*** function AjaxCall(request_uri, params)
       DESC:
        -  
  */  
  function AjaxCall(i_request_uri, i_action_type, i_params)
  {
      //-- If is new calling or cache is empty
      if(i_request_uri)
      {
          var request_uri = i_request_uri;
          
          try
          {
              if(!request_uri || request_uri == '' || !i_action_type || i_action_type == '')
                throw("Invalid AJAX request!");
          }
          catch(e)
          {
              displayError(e.toString());
          }
          
          // only continue if xmlHttp isn't void
          if (xmlHttp)
          {
              // encode values for safely adding them to an HTTP request query string
              var action_type = encodeURIComponent(i_action_type);
              var params      = '&action_type=' + action_type;
                  
            	if (i_params) 
              {
            			if(typeof(i_params)=="object")
            			{
                      for (i = 0; i<i_params.length; i++)
                			{
                  				value = i_params[i];
                  				//if (typeof(value)=="object") value = objectToXML(value);
                  						
                          params += "&ajax_params[]="+encodeURIComponent(value);
                			}
                	}
                	else params += "&"+i_params;
            	}
            	
              cache.push(params);
              cache_request_uri.push(request_uri);
          }
      }
          
          
      // try to connect to the server
      try
      {
          // continue only if the XMLHttpRequest object isn't busy
          // and the cache is not empty
          if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0)
          {
              // get a new set of parameters from the cache
              var cacheEntry    = cache.shift();
              var cacheEntryURI = cache_request_uri.shift();
              // make a server request to validate the extracted data
              xmlHttp.open("POST", cacheEntryURI, true);
              xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
              xmlHttp.onreadystatechange = handleRequestStateChange;
              xmlHttp.send(cacheEntry);
          }
      }
      //-- display an error when failing to connect to the server
      catch(e) 
      {
          displayError(e.toString());
      }
      
      //-- If cache isn't empty
      if(cache.length > 0) setTimeout("AjaxCall();", 500);
  }
  
  // function that handles the HTTP response
  function handleRequestStateChange() 
  {
      // when readyState is 4, we read the server response
      if (xmlHttp.readyState == 4) 
      {
          // continue only if HTTP status is "OK"
          if (xmlHttp.status == 200) 
          {
              try
              {
                  // read the response from the server
                  readResponse();
              }
              // display error message
              catch(e) 
              {
                  displayError(e.toString());
              }
          }
          //-- display error message
          else 
          {
              displayError(xmlHttp.statusText);
          }
      }
  }
  
  // read server's response 
  function readResponse()
  {
      // retrieve the server's response 
      var response = xmlHttp.responseText;
      // server error?
      if (response.indexOf("ERRNO") >= 0 
          || response.indexOf("error:") >= 0
          || response.length == 0)
        throw(response.length == 0 ? "Server error." : response);
      // get response in XML format (assume the response is valid XML)
      responseXml = xmlHttp.responseXML;
      
      //-- IE & Opera errors
      if(!responseXml || !responseXml.documentElement)
        throw("Invalid XML structure:\n" + xmlHttp.responseText);
      
      //-- Firefox errors
      var rootNodeName = responseXml.documentElement.nodeName;
      if(rootNodeName == "parseerror")
        throw("Invalid XML structure:\n" + xmlHttp.responseText);
      
      //-- Root node
      xmlDoc = responseXml.documentElement;
      
      //-- Check error node
      if(!xmlDoc.getElementsByTagName("error")[0])
        throw("MMSKUJ.sk engine Error encountered: \n\n" + xmlHttp.responseText);
        
      //-- Check errors
      if(xmlDoc.getElementsByTagName("error")[0].firstChild)
      {
          error = xmlDoc.getElementsByTagName("error")[0].firstChild.data;
          throw(error);
      }
      
      //-- Check action_type
      if(!xmlDoc.getElementsByTagName("action_type")[0])
        throw("Doesn't exist action_type in XML structure:\n" + xmlHttp.responseText);
      
      action_type = xmlDoc.getElementsByTagName("action_type")[0].firstChild.data;
      
      try
      {
          if (typeof AjaxResponse=="function") AjaxResponse(xmlDoc, action_type);
          else throw("Function \"AjaxResponse()\" isn't declared or isn\'t correct.");
      }
      catch(e)
      {
          displayError(e.toString());
      } 
  }
  
  // function that displays an error message
  function displayError($message)
  {
      //-- Ignore errors if showErrors is false
      if (showErrors)
      {
          //-- Display error message
          //alert(innerText = "Error encountered: \n\n" + $message);
          alert(innerText = $message);
      }
  }

