/**
* Exception Parametre invalide	
* @member AjaxOption
*/
EXCEP_invalidParameter = " EXCEP_invalidParameter : le parametre en entrée est invalide";
/********************************************************************/
/*                            CLASSE AJAX                           */
/********************************************************************/

/** 
* @class Permet de définir les options de la classe Ajax
* @param methode : null, post ou get
* @param asynchro :  true -> mode asynchrone
* @param param : parametres supplementaires
* @author : ote
* @version : 1.0
* @exception : EXCEP_invalidParameter
*/
function AjaxOption (methode, asynchro, param){
	this.OPT_method='POST';
	this.OPT_asynchronous=true;
	this.OPT_parameters='';
	this.OPT_sablier=true;
	this.OPT_gifDiv=null;
	
	// definition de la methode post ou get 
	if (methode) 
		if (methode=='POST' || methode=='GET') this.OPT_method=method;
		else throw EXCEP_invalidParameter;
	else this.OPT_method='POST';

	//Definition du mode asynchrone
	if (asynchro==false) this.OPT_asynchronous=false;
	else this.OPT_asynchronous=true;
	
	//definition des parametres
	if (param) this.OPT_parameters = param;
	else this.OPT_parameters='';
	
/**
* Affiche les options
* @return :  chaine contenant toute les options définie
* @author : ote
* @version 1.1
*/
	this._toString = function(){
		return "method : "+ this.OPT_method+"\n"
			  +"asynchronous : "+this.OPT_asynchronous+"\n"
			  +"parameters : "+this.OPT_parameters+"\n";
			  +"sablier : "+this.OPT_sablier+"\n";
			  +"gifDiv :"+this.OPT_gifDiv+"\n";
	}
}

/** 
@class Classe ajax <br>
* utilisation : <br>
*    - Instancier un objet ajax (var a = new AjaxClass();)<br>
*    - Modifiier éventuellement les options (a.option.OPT_METHOD='GET');<br>
*    - Appeler une des fonctions publique (a._sendAndReplaceDiv('mapage.jsp','prenom=CLAUDIA&nom=CHOUFLEUR', 'monDivAjax));<br>
@author : ote
@version : 1.0
*/
function AjaxClass(){
	
/**
* permet de choisir le transporteur en fonction du navigateur
* @private
* @author : ote
* @version : 1.0
*/
	this._getTransporteur = function (){
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		   XmlHttp = new XMLHttpRequest();
		} 
	  	else if (window.ActiveXObject) { // IE
			try {
				XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			  }
		}	
		return XmlHttp;
	};
	
	
	//OBJETS

/**
* Transporteur
* @private
*/ 
this.transporteur=this._getTransporteur();
/**
* Option ajax (objet du type AjaxOption)
*/ 
	this.option=new AjaxOption();
/**
* curseur a utiliser
* @private
*/ 
	this.cursor=null;


/**
* Variable contenant le transporteur
* @member AjaxClass
* @private
*/
this.transporteurCommun=null
/**
* objet pouvant contenir une fonction lorsque Ajax commence a se charge
*/
this.onAjaxLoading=null;
/**
* objet pouvant contenir une fonction lorsque Ajax est pret mais que les données ne sont pas encore chargées <br>
* voir l'exemple sur onAjaxLoading
* @member AjaxClass
*/
this.onAjaxLoaded=null;
/**
* objet pouvant contenir une fonction lorsque Ajax est pret mais que les données sont chargées partiellement br>
* voir l'exemple sur onAjaxLoading
* @member AjaxClass
*/
this.onAjaxInteractive =null;
/**
* objet pouvant contenir une fonction lorsque Ajax est pret mais que les données sont chargées totalement<br>
* voir l'exemple sur onAjaxLoading
* @member AjaxClass
*/
this.onAjaxReady=null;



/** permet de faire l'envoi des données data au serveur a la page "page"
* @private 
* @param page : page du serveur a appeler
* @param data : données a envoyer
* @author : ote
* @version : 1.0
*/	
	this._sendData = function(page,data){
		transporteurCommun = this.transporteur;
	   	transporteurCommun.open(this.option.OPT_method, page, this.option.OPT_asynchronous);
   		transporteurCommun.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		setTimeout('transporteurCommun.send("'+data+'")',0);
	}

/**
* ecrit la réponse dans le div divId 
* @private
* @param divId : id du div
* @author :  ote
* @version : 1.0
*/
	this._putResponseInDiv = function(divId){
      OptSablier = this.option.OPT_sablier;
      OptGifDiv = this.option.OPT_gifDiv;
      onAjaxLoading = this.onAjaxLoading;
   		onAjaxLoaded = this.onAjaxLoaded;
   		onAjaxInteractive = this.onAjaxInteractive;
   		onAjaxReady = this.onAjaxReady;

	    transporteurCommun.onreadystatechange = function(){
/*	      if(transporteurCommun.readyState == 1)  {
	    	   //document.getElementById(divId).innerHTML="<center><IMG src=/images/wait.gif align='center'/></center>";
	    	   document.getElementById(divId).innerHTML="Veuillez patienter ...";
		    }
*/	    if(transporteurCommun.readyState == 1)  {
					 if (OptSablier !=null){
					 		 this.cursor=document.getElementById(divId).style.cursor;
					 	document.getElementById(divId).style.cursor="wait"
					}
					if (OptGifDiv !=null){
						document.getElementById(OptGifDiv).innerHTML="<center><IMG src=/images/wait.gif align='center'/></center>";
					}
		    	if (onAjaxLoading != null) onAjaxLoading();
		    }
		    if(transporteurCommun.readyState == 2)  {
		    	 if (onAjaxLoaded != null) onAjaxLoaded();
		    }
		    if(transporteurCommun.readyState == 3)  {
		    	 if (onAjaxInteractive != null) onAjaxInteractive();
		    }
    		if(transporteurCommun.readyState == 4)  {
    			 if (OptSablier){ 
    			 		document.getElementById(divId).style.cursor=this.cursor
    			 }
    			 if (OptGifDiv !=null){
						document.getElementById(OptGifDiv).innerHTML="<center><IMG src=/images/waitBlank.gif align='center'/></center>";
					}
    			 document.getElementById(divId).innerHTML=transporteurCommun.responseText;
    			 var tscr = document.getElementById(divId).getElementsByTagName('script');
    			 for(var i=0;i<tscr.length;i++)
    			 {
    			 //alert(tscr[i].text);
    				eval(tscr[i].text);
    			 }
	    	     if (onAjaxReady != null) onAjaxReady();
		    }
	    }
 
	}
/** envoi au serveur les données data a la page page, et remplace le div divId par la reponse
* @param page : page du serveur a appeler
* @param data : données a envoyer au serveur
* @param divId : id du div
* @author : ote
* @version : 1.0
*/
  	this._sendAndReplaceDiv = function(page,data,divId){
			this._sendData(page,data);
			this._putResponseInDiv(divId);
  	}
  	
/** permet de faire l'envoi des données data au serveur a la page "page" et de retourner la réponse
* @public 
* @param page : page du serveur a appeler
* @param data : données a envoyer
* @author : ote
* @version : 1.0
*/	
	this._sendDataAndGetResponse = function(page,data){
		this._sendData(page,data);
		onAjaxLoading = this.onAjaxLoading;
		onAjaxLoaded = this.onAjaxLoaded;
 		onAjaxInteractive = this.onAjaxInteractive;
 		onAjaxReady = this.onAjaxReady;

    if(transporteurCommun.readyState == 1)  {
	  	if (OptSablier !=null){
			 		 this.cursor=document.getElementById(divId).style.cursor;
			 	document.getElementById(divId).style.cursor="wait"
			}
			if (OptGifDiv !=null){
					document.getElementById(OptGifDiv).innerHTML="<center><IMG src=/images/wait.gif align='center'/></center>";
			}
		   	if (onAjaxLoading != null) onAjaxLoading();
    }
    if(transporteurCommun.readyState == 2)  {
    	 if (onAjaxLoaded != null) onAjaxLoaded();
    }
    if(transporteurCommun.readyState == 3)  {
    	 if (onAjaxInteractive != null) onAjaxInteractive();
    }
		if(transporteurCommun.readyState == 4)  {
			 if (OptSablier){ 
			 		document.getElementById(divId).style.cursor=this.cursor
			 }
			 if (OptGifDiv !=null){
				document.getElementById(OptGifDiv).innerHTML="<center><IMG src=/images/waitBlank.gif align='center'/></center>";
			}
			 return transporteurCommun.responseText;
  	   if (onAjaxReady != null) onAjaxReady();
    }
	}
  	
}

function callAjax(_div, _aspx, _params)
{
	var a = new AjaxClass() ;
		//a.option.OPT_gifDiv =_div;	
		a._sendAndReplaceDiv(_aspx, _params, _div);
}

function callsAjax(prefixe_div)
{
	encadrer=document.getElementById('cms_bloc_encadrer');
	var sec=0;
	var divs = document.getElementsByTagName('div');
	for (i=0; i<divs.length; i++) 
	{
		if(divs[i].id.indexOf(prefixe_div)==0)
		{
			idBloc=divs[i].id.substring(prefixe_div.length);
			if(encadrer!=null)
			{
				//alert("callAjaxBloc_"+idBloc+"('"+encadrer.value+"');");
				setTimeout("callAjaxBloc_"+idBloc+"('"+encadrer.value+"')", sec);
				sec+=100;
			}
		}
	}
	if(encadrer!=null)
	{
		encadrer.value="true"==encadrer.value?"false":"true";
	}
}