

/**
 * VERSION 1.1
 * ADAPTATION PROTOTYPE
 */


/*
* Constructeur
*
* idname 		: ID de l'objet, utilisé pour le div généré et le nom de l'objet javascript
* jspUrl		: Url à lancer vers le serveur
* jspForm		: Nom du formulaire à soumettre
* onStartup		: Script à lancer à l'initialisation
* onBeforeReload: Script à lancer avant un reload soit rechargement
* onAfterReload	: Script à lancer après un reload soit rechargement
* divTarget		: On peut dire que le div à raffraichir est un DIV existant
* nodiv			: On ne veux pas mettre à jour de div, utile pour des requête/reponse sans affichage. On exploitera this.stream manuellement.
* isSynchr		: Définit le mode synchrone ou assynchrone
*
*/

function panel( idname, jspUrl, jspForm, onStartup, onBeforeReload, onAfterReload, divTarget, nodiv, isSynchr ) 	{
		
	this.idname				= idname;					// ID de l'objet, utilisé pour le div généré et le nom de l'objet javascript
	this.jspUrl				= jspUrl;					// Url à lancer vers le serveur
	this.jspForm			= jspForm;					// Nom du formulaire à soumettre
	this.onStartup			= onStartup;				// Script à lancer à l'initialisation
	this.onBeforeReload		= onBeforeReload;			// Script à lancer avant un reload soit rechargement
	this.onAfterReload		= onAfterReload;			// Script à lancer après un reload soit rechargement
	this.divTarget			= divTarget;

	this.isSynchr			= false;					// Définit le mode synchrone ou assynchrone
	if (isSynchr==null) 	this.isSynchr= false;
	if (isSynchr=="false")  this.isSynchr= false;
	if (isSynchr=="true")	this.isSynchr= true;	

	if (nodiv==null)		this.nodiv= false;			// On ne veux pas mettre à jour de div, utile pour des requête/reponse sans affichage
	if (nodiv=="false")		this.nodiv= false;
	if (nodiv=="true")		this.nodiv= true;	

	this.formSubmitted		= false;
	if (jspForm==null)		this.formSubmitted = false;
	if (jspForm!=null)		this.formSubmitted = true;

	this.httpLoader = new HttpLoader();
	
	
	this.paramsPanelLabels = new Array();
	this.paramsPanelValues = new Array();
	this.paramsPanelPos	   = 0;
	
	
	this.panel = null;
	this.processor = null;
	this.doc = null;
	this.root = null;
	this.counter = 0;
	this.formatter = null;
	this.encoding = null;
	this.stream = null;
	this.commentRegExpr = new RegExp("<!--.+?-->","\g");
	
	//On défini le div qui sera raffraichi si nodiv==false, sinon pas de div à raffraichir, on exploitera le contenu de stream
	if (this.nodiv==false) {	
		if (divTarget==null) {
			document.write("<DIV id=\""+this.idname+"\"> </DIV>");	
			this.divElm=document.getElementById(this.idname);		//Le div raffraichi sera un nouveau div id=idname
		} else {
			this.divElm=document.getElementById(divTarget);			//Le div raffraichi sera un div existant
		}
	}
}




/*
	Réinitialisation d'un panel
*/
panel.prototype.reset = function() {
	this.httpLoader = new HttpLoader();
	this.paramsPanelLabels = new Array();
	this.paramsPanelValues = new Array();
	this.panel = null;
	this.processor = null;
	this.doc = null;
	this.root = null;
	this.counter = 0;
	this.formatter = null;
	
	if (this.jspForm==null)		this.formSubmitted = false;
	if (this.jspForm!=null)		this.formSubmitted = true;
	
	this.encoding = null;
}

/*
	A l'initialisation, si onStartup renseigné alors on lance le script par exemple dans un onAfterReload.
*/
panel.prototype.init = function()	{
	if(this.onStartup) {
		eval(this.onStartup);
	}
}

/*
	Rajout d'un paramètre à envoyer au serveur, on peut néanmons les réécrires dans l'URL
*/
panel.prototype.setJspParam = function(name,value) {
	
	this.paramsPanelLabels[this.paramsPanelPos]=name;
	this.paramsPanelValues[this.paramsPanelPos]=value;
	this.paramsPanelPos++;
	return this;
}

/*
 	Redraw d'un bloc, c'est là que l'on doit vérifier la compatibilité sur tous les navigateurs, le
	paragragraphe <p> invisible est creer pour que le bloc se raffraichise (div). Par la suite,
	on execute tous le code javascript contenu dans le div
*/
panel.prototype.redraw = function(content) {
	
	//Supression des commentaires html :
	//content = content.replace(this.commentRegExpr,' ');
	
	if (content.indexOf("AJAX_ERROR")<0) {
		this.divElm.innerHTML="<p style='visibility:hidden; position:absolute;'>&nbsp;</p>"+content+"<p style='visibility:hidden; position:absolute;'>&nbsp;</p>";
		var elm = document.getElementById(this.idname);
		if(elm && elm.childNodes) {
   			for(var i=0;i<elm.childNodes.length;i++) {
        		if ( elm.childNodes[i].nodeName == "SCRIPT" ) {
         			eval(elm.childNodes[i].innerHTML);
        		}
   	 		}
    	}
    }
}


/*
 	Fonction appellé pour un reload, celle ci va déterminer si le reload se fera de manière synchrone ou assynchrone
*/


panel.prototype.resetReload = function() {
	this.paramsPanelLabels = new Array();
	this.paramsPanelValues = new Array();
	this.paramsPanelPos	   = 0;
}

panel.prototype.reload = function() {		
		
	//si script d'avant reload, on l'execute
	if(this.onBeforeReload) {
		eval(this.onBeforeReload);
	}
	
	//On charge l'url et ses paramètres dans le HttpLoader
	this.httpLoader.setUrl(this.jspUrl);
	this.httpLoader.appendMapData(this.paramsPanelLabels,this.paramsPanelValues);
	if(this.formSubmitted) this.httpLoader.appendFormData(this.jspForm);
	
	//Si on est en synchrone
	if (this.isSynchr) {
		this.stream = this.httpLoader.sendRequest();
		if(this.stream == null) {
			this.redraw("ERROR");
			this.resetReload();
			return;
		}
		//Une fois le flux récupéré, on raffraichi le div
		if(this.nodiv==false) {
			this.redraw(this.stream);
		}
		
		//On effectue les traitements d'afterReload
		if(this.onAfterReload) {
			eval(this.onAfterReload);
		}
		this.resetReload();
	//sinon en assynchrone
	} else {
		this.httpLoader.sendAssyncRequest(this.idname+".assyncCalledFunction();"+this.idname+".resetReload();");	
	}
}

/*
 	Fonction appellé de manière assynchrone une fois le flux récupéré
*/
panel.prototype.assyncCalledFunction = function(idname) {
		if (this.httpLoader.xmlhttp.readyState == 4) { 
			if (this.httpLoader.xmlhttp.status == 200) { 
				this.stream = this.httpLoader.xmlhttp.responseText;
				
				//Une fois le flux récupéré, on raffraichi le div
				if(this.nodiv==false)  { this.redraw(this.stream);  }
				if(this.onAfterReload) { eval(this.onAfterReload); }
			} 
		} 
}