/*
 * CLASS APPEL AJAX
 * @author	: GSP
 * @since	: juin 2007
 * @version	: 1.0
 */

var MSG_NO_ACTIVEX = "Pas d activeX trouvé dans UA pour XMLHttpRequest";
var MSG_NO_XMLHTTPREQUEST = "Votre navigateur ne supporte pas l'objet XMLHttpRequest";
var MSG_PROBLEME_RECUPERATION_DONNEES="There was a problem retrieving the XML data:\n";

/*
 * constructeur
 * url = url de l'action
 * elt = id de l'élément à 'remplir'
 * js = javascript
 * isASynchrone = true pour asynchrone, false pour synchrone
 * isReplaceContenuElt = true pour remplacer le contenu de elt par le résultat, false pour mettre à la suite
 */

function AppelAjax(url,elt,js,isASynchrone,isReplaceContenuElt)
{
  /*debug*/
  //document.body.appendChild(document.createElement("br"));
  //document.body.appendChild(document.createTextNode(url));
	this.url = url;
	this.elt = elt;
	this.js = js;
	this.isASynchrone = isASynchrone;
	this.isReplaceContenuElt = isReplaceContenuElt;
	this.xmlhttp = null;
	this.xmlhttpChangeAjax = xmlhttpChangeAjax;
	loadDocAjax(this);
}


function loadDocAjax(appelAjax)
{
    document.body.style.cursor = "wait";
    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
    {
            appelAjax.xmlhttp = new XMLHttpRequest();
            appelAjax.xmlhttp.onreadystatechange=function anonymous() {appelAjax.xmlhttpChangeAjax()};
            appelAjax.xmlhttp.open("GET",appelAjax.url,appelAjax.isASynchrone);
            appelAjax.xmlhttp.send(null);
    }
    // code for IE
    else if (window.ActiveXObject)
    {
      try
      {
        appelAjax.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }catch (e)
      {
           try
           {
              appelAjax.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (E)
           {
              alert(MSG_NO_ACTIVEX);
              appelAjax.xmlhttp = false;
           }
      }
      if (appelAjax.xmlhttp)
      {
          appelAjax.xmlhttp.onreadystatechange=function anonymous() {appelAjax.xmlhttpChangeAjax()};
          appelAjax.xmlhttp.open("POST",appelAjax.url,appelAjax.isASynchrone);
          appelAjax.xmlhttp.send();
      }
    }
    else
    {
            alert(MSG_NO_XMLHTTPREQUEST);
    }
    if(!appelAjax.isASynchrone)
    {
      appelAjax.xmlhttpChangeAjax();
      document.body.style.cursor = "auto";
    }
}

function xmlhttpChangeAjax() {
	// only if req shows "loaded"
	if (this.xmlhttp.readyState == 4) {
		// only if "OK"
		if (this.xmlhttp.status == 200) {
			// ...processing statements go here...
			if(this.elt!=null)
			{
          if(this.isReplaceContenuElt)
          {
              try
              {
                var element_cible = document.getElementById(this.elt);
                var reponse = ""+this.xmlhttp.responseText;
                reponse=trim(reponse);
                element_cible.innerHTML = ""+reponse;
              }catch(ex)
              {
                alert(ex.message);
              }
          }
          else
          {
          document.getElementById(this.elt).innerHTML += this.xmlhttp.responseText;
          }
				if (this.js)
				{
					eval(this.js);
				}
			}

		} else {
			alert(MSG_PROBLEME_RECUPERATION_DONNEES +
				this.xmlhttp.statusText);
		}
	}
	document.body.style.cursor = "auto";
}

/**
 * Equivalent du trim
 */

function trim(chaine)
{
   valueTmp = chaine.replace(/(^\s*)|(\s*$)/g,'');

   return(valueTmp);
}
