/**
* 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 = null;
	this.OPT_AttenteDiv = null;
	this.OPT_Attente = null;
	this.OPT_Attenteblank = null;
	this.OPT_div = 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)
	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 = '';
	
	this._toString = function()
		{
		return "method : " + this.OPT_method + "\n"
			+ "asynchronous : " + this.OPT_asynchronous + "\n"
			+ "parameters : " + this.OPT_parameters + "\n";
			+ "sablier : " + this.OPT_sablier + "\n";
			+ "AttenteDiv :" + this.OPT_AttenteDiv + "\n";
			+ "Attente :" + this.OPT_Attente + "\n";
			+ "Attenteblank :" + this.OPT_Attenteblank + "\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
	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;
		};
	
	this.transporteur=this._getTransporteur();
	this.option=new AjaxOption();
	// curseur a utiliser
	this.cursor=null;
	// Variable contenant le transporteur
	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>
	this.onAjaxLoaded=null;
	// objet pouvant contenir une fonction lorsque Ajax est pret mais que les données sont chargées partiellement br>
	this.onAjaxInteractive =null;
	// objet pouvant contenir une fonction lorsque Ajax est pret mais que les données sont chargées totalement<br>
	this.onAjaxReady=null;
	this.onAjaxReadyArg=null;

	// permet de faire l'envoi des données data au serveur a la page "page"
	this._sendData = function(page, data)
		{
		transporteurCommun = this.transporteur;
	   	transporteurCommun.open(this.option.OPT_method, page + '&AJAX=OK&UTF=OK', this.option.OPT_asynchronous);
   		//transporteurCommun.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		transporteurCommun.setRequestHeader('Content-Type','text/html; charset=ISO-8859-15');
		//transporteurCommun.overrideMimeType('text/html; charset=ISO-8859-15');
		//transporteurCommun.setRequestHeader("Content-length", data.length);
		//xmlHttp.send(escape(data));
		setTimeout('transporteurCommun.send("' + data + '")', 0);
		}
	// permet de faire l'envoi des données data au serveur a la page "page"
	this._sendDataUpload = function(page, data, FileName)
		{
	    var adoStream = new ActiveXObject("ADODB.Stream");
    	adoStream.Mode = 3; // read write
	    adoStream.Type = 1; // adTypeBinary
    	adoStream.Open();
	    adoStream.LoadFromFile(FileName);
	    FileName = FileName.substr(FileName.lastIndexOf("\\")+1);

		transporteurCommun = this.transporteur;
	   	transporteurCommun.open(this.option.OPT_method, page + '&FileName=' + FileName + '&AJAX=OK&UTF=OK', this.option.OPT_asynchronous);
   		transporteurCommun.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//transporteurCommun.setRequestHeader('Content-Type','text/html; charset=ISO-8859-15');
	    transporteurCommun.setRequestHeader("Content-Length", adoStream.Size);
		//xmlHttp.send(escape(data));
		setTimeout('transporteurCommun.send(' + adoStream.Read(adoStream.Size) + ')', 0);
		}

	// ecrit la réponse dans le div OPT_div 
	this._putResponseInDiv = function()
		{
		OptSablier = this.option.OPT_sablier;
		OptAttenteDiv = this.option.OPT_AttenteDiv;
		OptAttente = this.option.OPT_Attente;
		OptAttenteBlank = this.option.OPT_Attenteblank;
		OptDiv = this.option.OPT_div;
		onAjaxLoading = this.onAjaxLoading;
		onAjaxLoaded = this.onAjaxLoaded;
		onAjaxInteractive = this.onAjaxInteractive;
		onAjaxReady = this.onAjaxReady;
		onAjaxReadyArg = this.onAjaxReadyArg;
	
		transporteurCommun.onreadystatechange = function()
			{
			if (transporteurCommun.readyState == 1)
				{
				if (OptSablier != null)
					{
					this.cursor = document.getElementById(OptDiv).style.cursor;
					document.getElementById(OptDiv).style.cursor = "wait";
					}
				if (OptAttenteDiv != null)
					document.getElementById(OptAttenteDiv).innerHTML = OptAttente;
				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(OptDiv).style.cursor = this.cursor;
				if (OptAttenteDiv != null)
					document.getElementById(OptAttenteDiv).innerHTML = OptAttenteBlank;

				RESULT = transporteurCommun.responseText;
				Scripts = "";
				while (RESULT.indexOf("<SCRIPT>") != -1)
					{
					Avant = RESULT.substr(0, RESULT.indexOf("<SCRIPT>"));
					Scripts = Scripts + RESULT.substr(RESULT.indexOf("<SCRIPT>") + 9, RESULT.indexOf("</SCRIPT>") - RESULT.indexOf("<SCRIPT>") - 9);
					Apres = RESULT.substr(RESULT.indexOf("</SCRIPT>") + 9, RESULT.length);
					RESULT = Avant + Apres;
					//alert(Scripts);
					//alert(RESULT);
					}
				while (RESULT.indexOf("<script>") != -1)
					{
					Avant = RESULT.substr(0, RESULT.indexOf("<script>"));
					Scripts = Scripts + RESULT.substr(RESULT.indexOf("<script>") + 9, RESULT.indexOf("</script>") - RESULT.indexOf("<script>") - 9);
					Apres = RESULT.substr(RESULT.indexOf("</script>") + 9, RESULT.length);
					RESULT = Avant + Apres;
					//alert(RESULT);
					}

				//alert(RESULT);
				document.getElementById(OptDiv).innerHTML = RESULT;

				if (Scripts != "")
					{
					//alert(Scripts);
					eval(Scripts);
					}
				
				if (onAjaxReady != null)
					{
					if (onAjaxReadyArg == null)
						onAjaxReady();
					else
						onAjaxReady(onAjaxReadyArg);
					}
				}
			}
		}

	// envoi au serveur les données data a la page page, et remplace le div OPT_div par la reponse
  	this._sendAndReplaceDiv = function(page, data)
		{
		this._sendData(page, data);
		this._putResponseInDiv();
		}
  	
	// permet de faire l'envoi des données data au serveur a la page "page" et de retourner la réponse
	this._sendDataAndGetResponse = function(page, data)
		{
		this._sendData(page, data);
		OptSablier = this.option.OPT_sablier;
		OptAttenteDiv = this.option.OPT_AttenteDiv;
		OptAttenteBlank = this.option.OPT_Attenteblank;
		OptAttente = this.option.OPT_Attente;
		onAjaxLoading = this.onAjaxLoading;
		onAjaxLoaded = this.onAjaxLoaded;
 		onAjaxInteractive = this.onAjaxInteractive;
 		onAjaxReady = this.onAjaxReady;

		transporteurCommun.onreadystatechange = function()
			{
			if (transporteurCommun.readyState == 1)
				{
			  	if (OptSablier != null)
					{
				 	this.cursor = document.body.style.cursor;
			 		document.body.style.cursor = "wait";
					}
				if (OptAttenteDiv != null)
						document.getElementById(OptAttenteDiv).innerHTML = OptAttente;
				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.body.style.cursor = this.cursor;
				if (OptAttenteDiv != null)
					document.getElementById(OptAttenteDiv).innerHTML = OptAttenteBlank;
				
				if (onAjaxReady != null)
					{
					if (onAjaxReadyArg == null)
						onAjaxReady();
					else
						onAjaxReady(onAjaxReadyArg);
					}
				return transporteurCommun.responseText;
				}
			}
		}
		
	// creation d une chaine d arguement pour un formulaire
	this._createFormArgs= function(nomFormulaire)
		{
		var formulaire = document.getElementById(nomFormulaire);
		var retour = '';
		// parcour de tous les elements du formulaire
		for (i = 0; i < formulaire.length; i++)
			{
			if (formulaire.elements[i].name != '') // on ne prend que les elements qui on un nom
				{
				if ((formulaire.elements[i].type == "radio" && formulaire.elements[i].checked) || (formulaire.elements[i].type == "checkbox" && formulaire.elements[i].checked) || (formulaire.elements[i].type != "radio" && formulaire.elements[i].type != "checkbox"))
					{
					if (retour!='')
						retour = retour + '&';
					TMP = formulaire.elements[i].value;
					reg = new RegExp("\r\n", "gi");
					TMP = TMP.replace(reg, '@CR@');
					reg = new RegExp("&", "gi");
					TMP = TMP.replace(reg, '\\x26');
					retour = retour + formulaire.elements[i].name + '=' + encodeURI( TMP );
					}
				}
			}
		return retour;
		}
	
	// envoi au serveur les données data et formulaire a la page page, et remplace le div OPT_div par la reponse
	this._sendAndReplaceDivDataForm = function(page,data,formulaire)
		{
		this._sendData(page, data + '&' + this._createFormArgs(formulaire));
		this._putResponseInDiv();
		}

	// envoi au serveur les données formulaire a la page page, et remplace le div OPT_div par la reponse
	this._sendAndReplaceDivForm = function(page, formulaire)
		{
		this._sendData(page, this._createFormArgs(formulaire));
		this._putResponseInDiv();
		}

	// envoi au serveur les données formulaire a la page page, et remplace le div OPT_div par la reponse
	this._sendAndReplaceDivFormUpload = function(page, formulaire, FileName)
		{
		this._sendDataUpload(page, this._createFormArgs(formulaire), FileName);
		this._putResponseInDiv();
		}
	}

	
	



// Fonctions INNOVNET

var Ajax = new AjaxClass();
var URL_TraitementDesJS = "";
Ajax.option.OPT_AttenteBlank = "";

function TraitementDesJS()
	{
	TMP = Ajax.option.OPT_AttenteDiv;
	Ajax.option.OPT_AttenteDiv = null;
	Ajax.onAjaxReady = null;
	Ajax._sendDataAndEvalResponse(URL_TraitementDesJS, '');
	Ajax.option.OPT_AttenteDiv = TMP;
	}

function AJAX(URL, MenuItem, onAjaxReady)
	{
	if (document.all['AJAXContenu'])
		Ajax.option.OPT_div = "AJAXContenu";
	if (document.all['AJAXAttente'])
		{
		if (document.all['AJAXContenu'])
			Ajax.option.OPT_AttenteDiv = "AJAXContenu";
		Ajax.option.OPT_Attente = document.all['AJAXAttente'].innerHTML;
		}

	if (onAjaxReady)
		{
		URL_TraitementDesJS = onAjaxReady;
		Ajax.onAjaxReady = TraitementDesJS;
		}
	else
		{
		URL_TraitementDesJS = "";
		Ajax.onAjaxReady = null;
		}

	Ajax._sendAndReplaceDiv(URL, "");

	if (MenuItem)
		for (var i = 1; i <= 10; i++) 
			if (document.all['M' + i])
				{
				if (MenuItem == i)
					document.all['M' + i].className = "ongletmenu2";
				else
					document.all['M' + i].className = "ongletmenu1";
				}
	}
function AJAXForm(Form, URL, MenuItem, onAjaxReady)
	{
	if (document.all['AJAXContenu'])
		Ajax.option.OPT_div = "AJAXContenu";
	if (document.all['AJAXAttente'])
		{
		if (document.all['AJAXContenu'])
			Ajax.option.OPT_AttenteDiv = "AJAXContenu";
		Ajax.option.OPT_Attente = document.all['AJAXAttente'].innerHTML;
		}

	if (onAjaxReady)
		{
		URL_TraitementDesJS = onAjaxReady;
		Ajax.onAjaxReady = TraitementDesJS;
		}
	else
		{
		URL_TraitementDesJS = "";
		Ajax.onAjaxReady = null;
		}

	Ajax._sendAndReplaceDivForm(URL, Form);

	if (MenuItem)
		for (var i = 1; i <= 10; i++) 
			if (document.all['M' + i])
				{
				if (MenuItem == i)
					document.all['M' + i].className = "ongletmenu2";
				else
					document.all['M' + i].className = "ongletmenu1";
				}
	}
function AJAX2(URL)
	{
	if (document.all['AJAXContenu2'])
		Ajax.option.OPT_div = "AJAXContenu2";
	if (document.all['AJAXContenu2'])
		Ajax.option.OPT_AttenteDiv = "AJAXContenu2";
	if (document.all['AJAXAttente'])
		Ajax.option.OPT_Attente = document.all['AJAXAttente'].innerHTML;

	Ajax.onAjaxReady = null;

	Ajax._sendAndReplaceDiv(URL, "");
	}
function AJAX2Form(Form, URL, onAjaxReady)
	{
	if (document.all['AJAXContenu2'])
		Ajax.option.OPT_div = "AJAXContenu2";
	if (document.all['AJAXContenu2'])
		Ajax.option.OPT_AttenteDiv = "AJAXContenu2";
	if (document.all['AJAXAttente'])
		Ajax.option.OPT_Attente = document.all['AJAXAttente'].innerHTML;

	if (onAjaxReady)
		Ajax.onAjaxReady = onAjaxReady;
	else
		Ajax.onAjaxReady = null;
		
	Ajax._sendAndReplaceDivForm(URL, Form);
	}

function AJAX2FormUpload(Form, URL, onAjaxReady)
	{
	if (document.all['AJAXContenu2'])
		Ajax.option.OPT_div = "AJAXContenu2";
	if (document.all['AJAXContenu2'])
		Ajax.option.OPT_AttenteDiv = "AJAXContenu2";
	if (document.all['AJAXAttente'])
		Ajax.option.OPT_Attente = document.all['AJAXAttente'].innerHTML;

	if (onAjaxReady)
		Ajax.onAjaxReady = onAjaxReady;
	else
		Ajax.onAjaxReady = null;
		
    //var fso = new ActiveXObject("Scripting.FileSystemObject");
	//alert(Form.FileName.value);
	//if (fso.FileExists(Form.FileName.value))
			{
			alert(document.getElementById(Form).FileName.value + ' exists');
			Ajax._sendAndReplaceDivForm(URL, Form, document.getElementById(Form).FileName.value);
			}
	}
