
var Class = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
	}
};

//

function $(id) {  
	if (id == undefined) return false;
	return (document.getElementById(id.toString())) ? document.getElementById(id.toString()) : false; 
}

var divRetour = "";

var typeRetour = "";

var AjaxMe = Class.create();

AjaxMe.prototype = {

	// Constructeur

	initialize: function(sUrl, scope) {

		if (scope == undefined) { 
			this.scope = window; 
		} else {
			this.scope = scope; 
		} 

		this.sUrl = sUrl;
		this.oXmlHttp = false;
		this.sVariables = "";
		this.aArguments = [];

		/*@cc_on
		@if(@_jscript_version >= 5)
			try {
				this.oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					this.oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					this.oXmlHttp = false;
				}
			}
		@else
			this.oXmlHttp = false;
		@end @*/
		if(!this.oXmlHttp && typeof XMLHttpRequest != 'undefined') {
			try {
				this.oXmlHttp = new XMLHttpRequest();
			} catch (e) {
				this.oXmlHttp = false;
			}
		}
		if (!this.oXmlHttp) {
			// XMLHttpRequest n'est pas supporte pas le client
		}
	},
	// Envoi
	envoie: function(fonctionRetour) {

		this.fonctionRetour = fonctionRetour;
		this.oXmlHttp.abort();
		this.oXmlHttp.open('POST', this.sUrl, true);
		this.oXmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		for (var i = 0; i < this.aArguments.length; i++) {
			this.sVariables += "&"+this.aArguments[i].nom+"="+this.aArguments[i].valeur;
		}

		this.oXmlHttp.send(this.sVariables);
		var _this = this;

		this.oXmlHttp.onreadystatechange = function () {
			try {
				if (_this.oXmlHttp.status == 200 && _this.oXmlHttp.readyState == 4) {
					_this.fonctionRetour.apply(_this.scope, [_this.oXmlHttp.responseText]);
				}
			} catch (e) {
				// debug seulement
			}
		}
	},

	// Ajoute
	ajoute: function(nom, valeur) {
		var bExiste = false;
		for (var i = 0; i < this.aArguments.length; i++) {
			if (this.aArguments[i].nom == nom) {
				bExiste = true;
				this.aArguments[i].valeur = valeur;
				break;
			}
		}
		if (!bExiste) {
			this.aArguments.push( {nom: nom, valeur: valeur} );
		}
	}

};


function appelAjax (url,layercible,parametres,typeretour) {

  divRetour = layercible;

  typeRetour = typeretour;

  var oAfficheAjax = new AjaxMe(url);  

  if(parametres) {

    var reg=new RegExp("[&]+", "g");

    var tableau=parametres.split(reg);

    if(tableau.length > 0) {

      for (var i=0; i<tableau.length; i++) {

          var regIn=new RegExp("[=]+", "g");

          var tableauIn=tableau[i].split(regIn);

          oAfficheAjax.ajoute(tableauIn[0], tableauIn[1]);

      }

    } else {

       var regIn=new RegExp("[=]+", "g");

       var tableauIn=tableau[i].split(regIn);

       oAfficheAjax.ajoute(tableauIn[0], tableauIn[1]); 

    }

  }

	oAfficheAjax.envoie(retourAjax);  

}



var retourAjax = function(e) {

  if(typeRetour == "inner")

	 $(divRetour).innerHTML = e;

	else eval(e);

	typeRetour = "";

	divRetour = "";

}