////////////////////////////////////////////////////////////////////////////
//
// © 11/2007 - Nathanaël Mariani pour PMP CONCEPT 
//
//		LISTE MANAGER :: GESTION JAVASCRIPT DES LISTES DE DONNEES
//
////////////////////////////////////////////////////////////////////////////

/* inclusion des dépendances */
PMP.include("librairiesjs/ajax.js");

PMP.com.liste =
{
	showLoader : function(listeId)
	{
		// recupération du conteneur de la liste
		var liste = PMP.dom.getElement(listeId);
		if(!liste)
			return;
		
		//affichage du template de chargement
		var	loaderContent = document.getElementById(listeId+"loaderContent");
		var	loaderTarget = document.getElementById(listeId+"loaderTarget");
		
		if(loaderContent && loaderTarget)
		{
			// position et taille du template
			loaderContent.style.left = PMP.common.getElementLeft(loaderTarget, liste) + "px";
			loaderContent.style.top = PMP.common.getElementTop(loaderTarget, liste) + "px";
			loaderContent.style.width = PMP.common.getElementWidth(loaderTarget) + "px";
			loaderContent.style.height = PMP.common.getElementHeight(loaderTarget) + "px";
			
			loaderContent.style.visibility = "visible";
			loaderContent.style.display = "block";

			//alert("affiché");
		}
	},

	hideLoader : function(listeId)
	{
		// recupération du conteneur de la liste
		var liste = PMP.dom.getElement(listeId);
		if(!liste)
			return;
		
		//affichage du template de chargement
		var	loaderContent = document.getElementById(listeId+"loaderContent");
		
		if(loaderContent)
		{
			loaderContent.style.visibility = "hidden";
			loaderContent.style.display = "none";

			//alert("masqué");
		}
	},

	refresh : function(listeId, data, module, conf, front)
	{
		if( PMP.common.isUndefined(listeId) )
			return;
		
		// affiche le loader
		//window.setInterval(PMP.com.liste.showLoader.bind(this, listeId), 0);
		
		if( PMP.common.isUndefined(data) )
			data="";
		
		//alert(PMP.dom.getElementProperty('critere1', 'value'));
		// recupération du conteneur de la liste
		var liste = PMP.dom.getElement(listeId);
		if(!liste)
			return;
		
		// si le module n'est pas défini on tente de récupérer le module par défaut	
		if( PMP.common.isUndefined(module) || !module)
			var module = PMP.context.currentModule;
		
		// si le module n'est pas défini on tente de récupérer le module par défaut	
		if( PMP.common.isUndefined(conf) )
			var conf = PMP.context.currentModuleConf;
		
		var ajax = new PMP.util.pmpAjax(PMP.pathracine+"_lib/_composants/liste/liste.ajax", "GET");
		
		if(!ajax)
			return;
		
		ajax.appendData("action", "actualiser");
		
		ajax.appendData("liste_id", listeId);
			
		if(PMP.common.isString(data))
			ajax.appendData(listeId, data);
		else
			ajax.appendData(listeId, PMP.common.serialize(data));
		
		if(PMP.common.isString(module) && module.length > 0)
			ajax.appendData("module", module);
		
		if(PMP.common.isString(conf) && conf.length > 0)
			ajax.appendData("conf", conf);
		
		if(!PMP.common.isUndefined(front))
			ajax.appendData("front", front);
		
		ajax.appendData
		ajax.send();
			
		if(ajax.reponse.etat)
		{
			var listeXml = ajax.reponse.message.search(listeId+"Liste");
			
			if(listeXml<0)
			{
				liste = PMP.dom.getElement(listeId+"Liste");
			}
			 
			if(liste)
			{
				PMP.dom.getElement(listeId).style.display = 'block';
				liste.innerHTML = ajax.reponse.message.trim();				
				liste.style.display = "block";
			}

			return true;
		}
		else if(typeof(NotificationManager)!="undefined")
			NotificationManager.notify({text:ajax.reponse.message, className:"erreur", visibleDuration:0});

		return false;
	},
	
	coche : function(index)
	{
		if(typeof(depart) == 'undefined')		
			depart = 1;
		
		if(typeof(chkClasse) == 'undefined')		
			chkClasse = new Array();
		
		if(typeof(isChecked) == 'undefined')
			isChecked = new Array();
		
		if(typeof(all) == 'undefined')
			all = true;
			
		if(typeof(index) == 'number')
		{
			var check = document.getElementById('checkbox_tbl_' + index);
			var tr = document.getElementById('tr_tbl_' + index);
			
			if(check.checked == true)
			{
				check.checked = false;
				tr.className = chkClasse[index];
			}
			else
			{
				check.checked = true;
				chkClasse[index] = tr.className;				
				isChecked[index] = true;				
				tr.className = chkClasse[index] + " marked";
			}
		}
		else 
		{
			var checkListe = PMP.dom.getElementsByMaskId("checkbox_tbl_%s", "input");
			var trListe = PMP.dom.getElementsByMaskId("tr_tbl_%s", "tr");

			for (i = 0; i < checkListe.length; i++) 
			{
				if(checkListe[i].checked == true)
				{
					if (!isChecked[i]) {
						checkListe[i].checked = false;
						trListe[i].className = chkClasse[i];
					}
					else {
						isChecked[i] = false;
					}
				}
				else
				{					
					if (index == true) {
						checkListe[i].checked = true;
						chkClasse[i] = trListe[i].className;
						trListe[i].className = chkClasse[i] + " marked";
					}
				}
			}
		}
		depart = 0;		
	},
		
	PMA_markRowsInit : function(listeId) {
	    // for every table row ...
	    var rows = PMP.dom.getElementsByMaskId(listeId + "_tr_%s", "tr");
		
		if(typeof(marked_row) == "undefined")
			marked_row = new Array;
		
	    for ( var i = 0; i < rows.length; i++ ) {
	        // ... with the class 'odd' or 'even' ...
	        
			if ( 'odd' != rows[i].className.substr(0,3) && 'even' != rows[i].className.substr(0,4) && 'invisible' != rows[i].className.substr(0,9) && 'rupture' != rows[i].className.substr(0,7) && 'alerte' != rows[i].className.substr(0,6) ) {
	            continue;
	        }
	        // ... add event listeners ...
	        // ... to highlight the row on mouseover ...
	        if ( navigator.appName == 'Microsoft Internet Explorer' ) {
	            // but only for IE, other browsers are handled by :hover in css
	            rows[i].onmouseover = function() {
	                this.className += ' hover';
	            }
	            rows[i].onmouseout = function() {
	                this.className = this.className.replace( ' hover', '' );
	            }
	        }
	        // Do not set click events if not wanted
	        if (rows[i].className.search(/noclick/) != -1) {
	            continue;
	        }
	        
			// ... and to mark the row on click ...
	        rows[i].onmousedown = function() {
	            
				var unique_id;
	            var checkbox;
	
	            checkbox = this.getElementsByTagName( 'input' )[0];
				
				if ( checkbox && checkbox.type == 'checkbox' ) {
	                unique_id = checkbox.name; //+ checkbox.value;
	            } 
				else if ( this.id.length > 0 ) {	                
					unique_id = this.id;					
	            } 
				else {
	                return;
	            }
				
	            if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
	                marked_row[unique_id] = true;
	            } 
				else{
	                marked_row[unique_id] = false;
	            }				
				
				// Classe
	            if ( marked_row[unique_id] ) {
	                this.className += ' marked';
	            }
				else{
	                this.className = this.className.replace(' marked', '');
	            }
	
				// Checkbox				
			    if ( checkbox && checkbox.disabled == false ) 
				{
					checkbox.checked = marked_row[unique_id];
	            }
	        }
	
	        // ... and disable label ...
	        var labeltag = rows[i].getElementsByTagName('label')[0];
	        if ( labeltag ) {
	            labeltag.onclick = function() {
	                return false;
	            }
	        }
	        
			// .. and checkbox clicks	       
			var checkbox = rows[i].getElementsByTagName('input')[0];
	        
			if ( checkbox ) {
	            checkbox.onclick = function() {
	                // opera does not recognize return false;
	                this.checked = ! this.checked;
	            }
	        }
	    }
	},
	
	/**
	 * Marque toutes les lignes de la liste et coche la checkbox associées
	 * 	 *
	 * @param    conteneur    DOM element
	 */
	markAllRows : function( container_id ) {
	   
	    //var rows = document.getElementById(container_id).getElementsByTagName('tr');
	    var rows = PMP.dom.getElementsByMaskId(container_id + "_tr_%s", "tr");
		var unique_id;
	    var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) {
		        
			checkbox = document.getElementById("checkbox_tbl_"+i);

	        if ( checkbox && checkbox.type == 'checkbox' ) {
	            
				unique_id = checkbox.name; //+ checkbox.value;
				
	            if ( checkbox.disabled == false ) {
	                checkbox.checked = true;
	               
				    if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
						rows[i].className += ' marked';	                    
						marked_row[unique_id] = true;
	                }
	            }
	        }
	    }
	
	    return true;
	},

	/**
	 * marks all rows and selects its first checkbox inside the given element
	 * the given element is usaly a table or a div containing the table or tables
	 *
	 * @param    container    DOM element
	 */
	unMarkAllRows : function( container_id ) {
	    
		//var rows = document.getElementById(container_id).getElementsByTagName('tr');
	    var rows = PMP.dom.getElementsByMaskId(container_id + "_tr_%s", "tr");
		var unique_id;
	    var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) {
		        
			checkbox = document.getElementById("checkbox_tbl_"+i);

	        if ( checkbox && checkbox.type == 'checkbox' ) {
	            
				unique_id = checkbox.name;// + checkbox.value;
				
	            if ( checkbox.disabled == false ) {
	                checkbox.checked = false;
					
				    if ( marked_row[unique_id] ) {						
						rows[i].className = 'odd';                  
						marked_row[unique_id] = false;
	                }
	            }				
	        }
	    }
	
	    return true;
		/*
		var rows = document.getElementById(container_id).getElementsByTagName('tr');
	    var unique_id;
	    var checkbox;
	
	    for ( var i = 0; i < rows.length; i++ ) {
	
	        checkbox = rows[i].getElementsByTagName( 'input' )[0];
	
	        if ( checkbox && checkbox.type == 'checkbox' ) {
	           
			    unique_id = checkbox.name + checkbox.value;
	            checkbox.checked = false;
	            rows[i].className = rows[i].className.replace(' marked', '');
	            marked_row[unique_id] = false;
	        }
	    }
	
	    return true;
	    */
	},
	
	actionMulti : function(action, champ){
		var tabSlct = PMP.dom.getElementsByMaskId("checkbox_tbl_%s", "input");		
		var tabRef = PMP.dom.getElementsByMaskId("ref%s", "input");
		var chaineParam = new String();
		
		for(i=0; i< tabSlct.length; i++)
		{
			if (tabSlct[i].checked == true) {				
				if (i != 0) 
					chaineParam += '|';
				chaineParam += tabRef[i].value;
			}			
		}

		switch(action){
			case 'SUPPR':								
				supprimerMulti(chaineParam);
				break;
			case 'ACTIVE':
				modifChamp(chaineParam, champ, 1);
				break;
			case 'DESACTIVE':
				modifChamp(chaineParam, champ, 0);
				break;
			case 'PRIVEE':
				modifChamp(chaineParam, champ, 1);
				break;
			case "PUBLIC":
				modifChamp(chaineParam, champ, 0);
				break;
		}
	}
	
}