// Module AJAX

// Ce module comporte les fonctions publiques suivantes :
// LoadToDiv(url, div) : Charge le contenu de l'url et le copie dans le div
// LoadStyleToDiv(url, div) : Récupère le nom de la classe à passer au div
// LoadToFunction(url, fn) : charge le contenu de l'url et renvoie le résultat à la fonction fn
// LoadJS(url) : charge le contenu de l'url et l'exécute

var ajaxObject=new Object();
ajaxObject.READY_STATE_UNINITIALIZED=0;
ajaxObject.READY_STATE_LOADING=1;
ajaxObject.READY_STATE_LOADED=2;
ajaxObject.READY_STATE_INTERACTIVE=3;
ajaxObject.READY_STATE_COMPLETE=4;
ajaxObject.ContentLoader=function(url, onload, data, onerror){
	this.url=url;
	this.req=null
	this.onload=onload;
	this.data=(data)?data:null;
	this.onerror=(onerror)?onerror:this.defaultError;
	this.loadXMLDoc(url, data);
}
ajaxObject.ContentLoader.prototype={
	loadXMLDoc:function(url, data){
		if (window.XMLHttpRequest){
			this.req=new XMLHttpRequest();
		}
		else if (window.ActiveXObject){
			this.req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (this.req){
			try{
				var loader=this;
				this.req.onreadystatechange=function(){
					loader.onReadyState.call(loader);
				}
				//Création de la requête POST asynchrone sur l'url passée en paramètre
				this.req.open("POST",url,true);
				//Necessaire pour l'envoi de requête POST, sinon les datas ne passent pas!
				this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
				//Passage des paramètres
				this.req.send(data);
			}
			catch(err){
				alert(err);
				this.onerror.call(this);
			}
		}
	},
	onReadyState:function(){
		var req=this.req;
		var ready=req.readyState;
		if (ready==ajaxObject.READY_STATE_COMPLETE){
			var httpStatus=req.status;
			if (httpStatus==200||httpStatus==0){
				this.onload.call(this);
			}else{
				this.onerror.call(this);
			}
		}
	},
	defaultError:function(){
		alert(	"Erreur de traitement\n\nEtat : " + this.req.readyState +
				"\nStatus : " + this.req.status +
				"\nHeaders : " + this.req.getAllResponseHeaders());	
	}

}

ajaxObject.LoadToDiv=function(url, div, data){
	var tmp=new this.ContentLoader(url,this.toDiv, data);
	tmp.div=div;
}
ajaxObject.toDiv=function(){
	window.document.getElementById(this.div).innerHTML=this.req.responseText;
}
ajaxObject.LoadStyleToDiv=function(url, div){
	var tmp=new this.ContentLoader(url,this.StyletoDiv);
	tmp.div=div;
}
ajaxObject.StyletoDiv=function(){
	window.document.getElementById(this.div).className=this.req.responseText;
}
ajaxObject.LoadToFunction=function(url, fn){
	var tmp=new this.ContentLoader(url,fn);
}
ajaxObject.LoadJS=function(url){
	var tmp=new this.ContentLoader(url,this.inscrireJS);
}
ajaxObject.inscrireJS=function(){
	eval(this.req.responseText);
}
ajaxObject.LoadToDivAndJS=function(url, div, data){
	var tmp=new this.ContentLoader(url,this.toDivAndJS, data);
	tmp.div=div;
}
ajaxObject.toDivAndJS=function(){

	window.document.getElementById(this.div).innerHTML=this.req.responseText;
	db=this.req.responseText.indexOf('<!--//JAVASCRIPT_DB-->',0);
	if (db>=0){
		fn=this.req.responseText.indexOf('<!--//JAVASCRIPT_FN-->',db);
		if (fn<db) fn=this.req.responseText.length;
		js=this.req.responseText.substring(db,fn);
		eval(js);
	}
}

