/**
*	Alias
*/
function $(id) { return document.getElementById(id) }
function $$(conteneur, type, id) { return (!isNaN(id)) ? ((conteneur.getElementsByTagName(type)) ? conteneur.getElementsByTagName(type)[id] : false) : conteneur.getElementsByTagName(type) }

Array.prototype.in_array = function(valeur){
    var n = this.length;
    for(var i=0; i<n; i++){
	if(this[i] == valeur) return true;
    };
    return false;
}

/**
*	Librairie de fonctions javascript AJAX
*/
function createXHR(){
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {};
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {};
	try { return new XMLHttpRequest(); } catch (e) {};
	alert("XMLHttpRequest non supportÃ© !!!");
	return null;
}

// gestionnaires AJAX

function Moteur(corps, visualisateur, mode, moteur, objEnAttente){		// mode: par défaut: remplacement, 1: ajout, 2: synchrone
    this.corps = corps;
    this.moteur = (moteur) ? moteur : this.moteurDefault;
    this.objEnAttente = (objEnAttente) ? objEnAttente : false;
    this.tampon = false;
    this.visualisateur = visualisateur;
    this.mode = mode;
    this.requete = this.recupRequete();
    this.reponse = '';
    if(this.requete != null){
	try {
	    this.requete.open("POST", this.moteur, (this.mode !== 2));
	    this.requete.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    var courant = this;
	    this.requete.onreadystatechange = function(){
		if(courant.requete.readyState == 4 && courant.requete.status == 200){
		    courant.reponse = courant.requete.responseText;
		    if(courant.mode == 1){
			courant.visualisateur.innerHTML += courant.reponse;
		    } else {
			courant.visualisateur.innerHTML = courant.reponse;
		    };
		    if(courant.objEnAttente){
		    	courant.objEnAttente.removeChild(courant.iconWait);
		    	courant.objEnAttente.innerHTML = courant.tampon;
		    };
		    // interpretation des javascript
		    var filtre = new RegExp("<script[^>]*>([^<]*)<\/script>", "g");
		    var lsScript = courant.reponse.match(filtre);
		    if(lsScript && lsScript.length > 0) {
			var filtre2 = new RegExp(">([^<]*)<");
			var n = lsScript.length;
			for(i=0; i < n; i++){
			    var tabRes = filtre2.exec(lsScript[i]);
			    if(tabRes != null) eval(tabRes[1]);
			};
		    };
		};
	    };
	    this.requete.send(this.corps);
	    if(this.objEnAttente) this.enAttente();
	} catch (exc) {
	    alert(exc);
	};
	return true;
    };
}

Moteur.prototype = {
    
    iconLoadURL: "Media/images/loading.gif",
    moteurDefault: 'engine.php',

    recupRequete: function(){
	var result = this.requete;
	if(result == null){
	    if(window.XMLHttpRequest) {
		result = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
		result = new ActiveXObject("Microsoft.XMLHTTP");
	    };
	};	
	return result;
    },
    
    enAttente: function(){
	this.iconWait = document.createElement('img');
	this.iconWait.src = this.iconLoadURL;
	this.tampon = this.objEnAttente.innerHTML;
	this.objEnAttente.innerHTML = '';
	this.objEnAttente.appendChild(this.iconWait);
    }
}

Moteur.prototype.constructor = Moteur;

