var AJAX_REQUESTS=new Array();
var AJAX_METHODS=new Array();
var AJAX_TIMEOUTS=new Array();
var AJAX_RESULT;

/* Calls up a URL to check something, giving any 'feedback' as an error (or if just 'false' then returning false with no message) */
function do_ajax_field_test(url)
{
   if (!ajax_supported()) return true;

   if (window.keep_stub) url=url+keep_stub();
   var xmlhttp=load_XML_doc(url);
   if ((xmlhttp.responseText!='') && (xmlhttp.responseText!=Null))
   {
      if (xmlhttp.responseText!='false') window.alert(xmlhttp.responseText);
      return false;
   }
   return true;
}

function ajax_supported()
{
   // Intentionally not a single line, to help validator
   if ((window.XMLHttpRequest) || (window.ActiveXObject)) return true;
   return false;
}

function load_XML_doc(url,method,post) // Note: 'post' is not an array, it's a string (a=b)
{
   var synchronous=!method;

   var index=AJAX_REQUESTS.length;
   AJAX_METHODS[index]=method;
   if (window.XMLHttpRequest)
   {
      // Branch for none-IE
      AJAX_REQUESTS[index]=new XMLHttpRequest();
      if (!synchronous) AJAX_REQUESTS[index].onreadystatechange=process_request_changes;
      if (post)
      {
         AJAX_REQUESTS[index].open('POST',url,!synchronous);
         AJAX_REQUESTS[index].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
         AJAX_REQUESTS[index].send(post);
      } else
      {
         AJAX_REQUESTS[index].open("GET",url,!synchronous);
         AJAX_REQUESTS[index].send(null);
      }
   }
   else if (window.ActiveXObject)
   {
      // Branch for IE
      AJAX_REQUESTS[index]=new ActiveXObject("Microsoft.XMLHTTP");
      if (AJAX_REQUESTS[index])
      {
         if (!synchronous) AJAX_REQUESTS[index].onreadystatechange=process_request_changes;
         if (post)
         {
            AJAX_REQUESTS[index].open('POST',url,!synchronous);
            AJAX_REQUESTS[index].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            AJAX_REQUESTS[index].send(post);
         } else
         {
            AJAX_REQUESTS[index].open("GET",url,!synchronous);
            AJAX_REQUESTS[index].send();
         }
      }
   }

   var result=AJAX_REQUESTS[index];
   if (synchronous)
   {
      AJAX_REQUESTS[index]=null;
   }
   return result;
}

function process_request_changes()
{
   // If any AJAX_REQUESTS are 'complete'
   var i,ajax_request;
   for (i=0;i<AJAX_REQUESTS.length;i++)
   {
      ajax_request=AJAX_REQUESTS[i];
      if ((ajax_request!=null) && (ajax_request.readyState) && (ajax_request.readyState==4))
      {
         AJAX_REQUESTS[i]=null;

         // If status is 'OK'
         if ((ajax_request.status) && (ajax_request.status==200))
         {
            //Process the result
            handle_errors_in_result(ajax_request);
            if (ajax_request.responseXML)
            {
               ajax_request.responseXML.validateOnParse=false;
               var response=ajax_request.responseXML.documentElement;
               process_request_change(response,i);
            }
         }
         else
         {
            window.alert("There was a problem retrieving the XML data:\n"+ajax_request.status+": "+ajax_request.statusText+".");
         }
      }
   }
}

function handle_errors_in_result(request)
{
   if (request.responseXML==null)
   {
      if ((request.responseText) && (request.responseText!='')/* && (!request.synchronous)*/)
      {
         var error_window=window.open();
         if (error_window)
         {
            error_window.document.write(request.responseText);
            error_window.document.close();
         }
      }
      return false;
   }
   return true;
}

function process_request_change(response,i)
{
   if (!response) return; // Needed for Opera

   if (response.getElementsByTagName("message")[0])
   {
      //Either an error or a message was returned. :(
      var message=response.getElementsByTagName("message")[0].firstChild.data;

      if (response.getElementsByTagName("error")[0])
      {
         //It's an error :|
         window.alert("An error ("+response.getElementsByTagName("error")[0].firstChild.data+") message was returned by the server: "+message);
         return;
      }

      window.alert("An informational message was returned by the server: "+message);
      return;
   }

   AJAX_RESULT=response.getElementsByTagName("result")[0];

   if ((response.getElementsByTagName("method")[0]) || (AJAX_METHODS[i]))
   {
      var method=(response.getElementsByTagName("method")[0])?eval('return '+response.getElementsByTagName("method")[0].firstChild.data):AJAX_METHODS[i];

      if (method.response) method.response();
      else method();

   }// else window.alert("Method required: as it is non-blocking");
}

function create_xml_doc()
{
   var xml_doc;

   if (window.ActiveXObject)
   {
      xml_doc=new ActiveXObject("Microsoft.XMLDOM");
   }
   else if (document.implementation && document.implementation.createDocument)
   {
     xml_doc=document.implementation.createDocument("","",null);
   }
   return xml_doc;
}

function merge_text_nodes(childNodes)
{
   var i,text='';
   for (i=0;i<childNodes.length;i++)
   {
      if (childNodes[i].nodeName=='#text')
      {
         text+=childNodes[i].data;
      }
   }
   return text;
}


