/**
 * ------------------------------------------------------------------------
 *  Made by Jacques Bodin-Hullin
 *     Jardin <jacques@bodin-hullin.net>
 *
 *  Started on July 19, 2007
 *  Last update October 30, 2007
 *
 *  (C) Copyright 2007 - Jacques 'Jardin' Bodin-Hullin
 * ------------------------------------------------------------------------
*/


/**
 * XmlHttpRequest object
 */
function getXhr()
{
   var xhr = false;
   if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
   }
   else {
      //Le navigateur n'est pas compatible avec AJAX.
      //On ne fait rien, on retourne simplement false.
   }

   return xhr;
}

/**
 * Send XmlHttpRequest
 *
 * @param method string ['post'|'get']
 * @param url string filename
 * @param data string data
 * @param flag bool [true|false]
 */
function Xsend(xhr, method, url, data, flag) {
   if (!xhr) {
      alert('JavaScript Error : Xsend, xhr is not a XMLHttpRequest Object');
      return false;
   }

   switch (method) {
      case 'POST':
      case 'post':
         xhr.open("POST", url, flag);
         xhr.setRequestHeader('Content-Type',
            'application/x-www-form-urlencoded');
         xhr.send(data);
         break;

      case 'GET':
      case 'get':
         if (data == null) {
            xhr.open("GET", url, flag);
         }
         else {
            xhr.open("GET", url + '?' + data, flag);
         }
         xhr.send(null); 
         break;

      default:
         alert('JavaScript Error : Xsend function');
         break;
   }

   return xhr;
}

/**
 * Check if responseText is OK
 *
 * @param xhr Object XmlHttpRequest
 */
function Xready(xhr) {
   if (xhr.readyState == 4 && xhr.status == 200) {
      return true;
   }
   else {
      return false;
   }
}

/**
 * Load an XML/XSL file
 *
 * @param url string XML Flename
 */
function loadXML(url) {
   var xhr;

   //Firefox et IE 7
   if (document.implementation.createDocument
      || window.XMLHttpRequest)
   {
      xhr = new XMLHttpRequest();
      Xsend(xhr, 'get', url, null, true);
      if (Xready(xhr)) {
         xhr = xhr.responseXML;
      }
   }
   //IE < 7
   else if (window.ActiveXObject) {
      xhr = xhr = new ActiveXObject("Microsoft.XMLHTTP");
      xhr.async = false;
      xhr.load(url);
   }

   return xhr;
}

/**
 * Load an XSL file
 *
 * Aliase of loadXML(url);
 *
 * @param url string XSL Filename
 */
function loadXSL(url) {
   return loadXML(url);
}
