
/*** Fonctions Javascript communes de l'Interface ********************************/

	function JS_Interface() {
	
		this.filtrerSources = 	function Interface_filtrerSources(NomModule, IdFiltre){
									frmSearchMask = document.forms['Frm'+NomModule];		
									indexCheck = 0;
									var tabIDSourcesChecked = Array();
									var cumulMatches = 0;
									var cumulNoSelect = 0;
									var NombreFiltres =0;
									NombreFiltres =  tabSources[NomModule][0]["FILTRES"].length;
									
									// on vérifie le nombre de filtres sollicités
									for (i=0; i <= NombreFiltres; i++) {
										var oFiltre = document.getElementById('REQ_'+NomModule+'_'+i);
										if (oFiltre && oFiltre.value ==-1) {
											++cumulNoSelect;
										}
									}
									
									// si aucunfiltre n'est sollicité on ne checke rien
									if (cumulNoSelect == NombreFiltres) {
										return;
									}
									else {
										cumulNoSelect = 0;
										// Parcours de la liste de sources ----------
										for (i=0;i<tabSources[NomModule].length;i++){	
											var IdSource = tabSources[NomModule][i]["ID"];
											
											NombreFiltres =  tabSources[NomModule][i]["FILTRES"].length;
											
											for (var IdFiltre=0; IdFiltre < tabSources[NomModule][i]["FILTRES"].length; IdFiltre ++) {
											
												var IdMapping = tabSources[NomModule][i]["FILTRES"][IdFiltre];
												var oFiltre = document.getElementById('REQ_'+NomModule+'_'+IdFiltre);	
												
												if (oFiltre) {
													var IdFiltreSelectionne = oFiltre.options[oFiltre.selectedIndex].value;
													
													if (IdMapping == IdFiltreSelectionne || IdFiltreSelectionne ==-1) {
														++cumulMatches;
														if (IdFiltreSelectionne ==-1) {
															++cumulNoSelect;
														}
													}
												}
												else {
													++cumulMatches;
												}
												
											}
											
											if (cumulMatches == NombreFiltres) {
												tabIDSourcesChecked[++indexCheck] = IdSource;
											}	
											cumulMatches =0;
											cumulNoSelect =0;	
											NombreFiltres = 0;
										}
												
										// Il ne faut que l'on ai au moins une des sources
										if (cumulNoSelect <= 1){
											checkedSources(tabIDSourcesChecked, NomModule);
										}
									}
									
										
								}// end function
	
	
		this.toutCocher = 		function(NomModule) {
									frmSearchMask = document.forms['Frm'+NomModule];		
									var choix = frmSearchMask.elements['REQ_'+NomModule+'_SOURCE[]'];	
									if (choix.length ==undefined) {
										if (document.getElementById('REQ_'+NomModule+'_SOURCE[]')) {
											document.getElementById('REQ_'+NomModule+'_SOURCE[]').checked =true;
										}
									}
									else {
										for (i = 0; i<choix.length; i++) {					
											choix[i].checked = true;
										} // end for
									}
								}
		
		this.toutDecocher = 	function(NomModule) {
									frmSearchMask = document.forms['Frm'+NomModule];		
									var choix = frmSearchMask.elements['REQ_'+NomModule+'_SOURCE[]'];
									if (choix.length ==undefined) {
										if (document.getElementById('REQ_'+NomModule+'_SOURCE[]')) {
											document.getElementById('REQ_'+NomModule+'_SOURCE[]').checked =false;
										}
									}
									else {	
										for (i = 0; i<choix.length; i++) {					
											choix[i].checked = false;
										} // end for
									}
									
								}
		
		
		this.ajouterSourcesSelectionnees =		function(NomModule) {
											  		frmSearchMask = document.forms['Frm'+NomModule];		
													var choix = frmSearchMask.elements['REQ_'+NomModule+'_SOURCE[]'];	
													var nbSourcesChoisies = 0;
													
													if (choix.length != undefined) {
														for (i = 0; i<choix.length; i++) {	
															if (choix[i].checked ==true) {
																++nbSourcesChoisies;
															}
														} // end for
													}
													else {
														if (document.getElementById('REQ_'+NomModule+'_SOURCE[]') &&
															document.getElementById('REQ_'+NomModule+'_SOURCE[]').checked) {
															++nbSourcesChoisies;
														}
													}
													
													if ( nbSourcesChoisies >0) {
														document.forms['Frm'+NomModule].action += "&ACTION"+NomModule+"=AJOUTER_SELECTION" ;
														document.forms['Frm'+NomModule].submit();
													}
													else {
														alert(Message["AJOUTER_SELECTION"]);
														return;
													}
												}
		
		
		this.supprimerSourcesSelectionnees = 	function(NomModule) {
											  		frmSearchMask = document.forms['Frm'+NomModule];		
													var choix = frmSearchMask.elements['REQ_'+NomModule+'_SOURCE[]'];	
													var nbSourcesChoisies = 0;
													
													if (choix.length != undefined) {
														for (i = 0; i<choix.length; i++) {	
															if (choix[i].checked ==true) {
																++nbSourcesChoisies;
															}
														} // end for
													}
													else {
														if (document.getElementById('REQ_'+NomModule+'_SOURCE[]') &&
															document.getElementById('REQ_'+NomModule+'_SOURCE[]').checked) {
															++nbSourcesChoisies;
														}
													}
													
													if ( nbSourcesChoisies >0) {
														document.forms['Frm'+NomModule].action += "&ACTION"+NomModule+"=SUPPRIMER_SELECTION" ;
														document.forms['Frm'+NomModule].submit();
													}
													else {
														alert(Message["SUPPRIMER_SELECTION"]);
														return;
													}
												}
	
												
		this.ouvrirPopupDeverrouillerSource	=		function(NomModule) {
														document.forms['Frm'+NomModule].action += "&ACTION"+NomModule+"=DEVERROUILLER_SOURCE" ;
														document.forms['Frm'+NomModule].submit();
													}
												
		this.ouvrirPopup = 		function(Url,Title,Param,Width,Heigth) {
									
									if (Param == "") {
										Param = "width=" + Width + ",height=" + Heigth;
									}
									else {
										Param += ",width=" + Width + ",height=" + Heigth;
									}
									var Top=Math.round((screen.availHeight-Heigth)/2);
									var Left=Math.round((screen.availWidth-Width)/2);
										
									Param += ",top="+Top+", left="+Left;
									
									if (Title =='') {
										TitrePopup = 'SistPopup';
									}
									else {
										TitrePopup = Title;
									}
									
									window.open(Url,TitrePopup,Param);
								}	
	
		this.envoyerRecherche = 	function(NomModule) {
										frmSearchMask = document.forms['Frm'+NomModule];		
										
										// Si aucun source n'est selectionnée, il faut toutes les selectionner
										var choix = frmSearchMask.elements["REQ_"+NomModule+"_SOURCE[]"];
										var tabIDSourcesChecked = Array();
										var isStop = false;
										
										if (choix.length !=undefined) { 
											for (i = 0;((i<choix.length) && (!isStop)); i++) {
												tabIDSourcesChecked[i] = choix[i].value;
												if (choix[i].checked == true) isStop = true;
											} // end for
										}
										else {
											if (document.getElementById("REQ_"+NomModule+"_SOURCE[]") && document.getElementById("REQ_"+NomModule+"_SOURCE[]").checked) {
												tabIDSourcesChecked[0] = document.getElementById("REQ_"+NomModule+"_SOURCE[]").value;
												isStop = true;
											}
										}
										
										if (!isStop){	// Aucun source n'est selectionnï¿½ 
												alert(Message["SOURCE_VIDE"]);
												return;
										}
										else  {
											// Mise à jour de l'action
											//frmSearchMask.REQ_ACTION.value = 'ENVOYER';
											frmSearchMask.action += "&ACTION"+NomModule+"=RECHERCHER";	
											// On soumet le formulaire
											frmSearchMask.submit(); 
										}
									}

									
		this.toggleBloc	= 			function(NomModule, NomBloc) {
			
										var bloc = document.getElementById(NomModule+NomBloc);
										var bouton = document.getElementById(NomModule+'Bouton'+NomBloc).getElementsByTagName("img")[0];
			
										if (bloc.style.display =="none") {
											bloc.style.display= "block";
											bouton.src = "afficher/images/moins.gif";
											
										}
										else if (bloc.style.display =="block") {
											bloc.style.display= "none";
											bouton.src = "afficher/images/plus.gif";
											
										}
									}
									
		this.exporterResultats =	function(NomModule, Url, ObjTypeExport) {
										
										// On mémorise les paramètres initiaux du formulaire
										var MemoUrl = document.forms['Frm'+NomModule].action;
										var MemoTarget = document.forms['Frm'+NomModule].target;
										
										// On modifie les parametres du formulaire pour l'envoi a la pseudo popup
										document.forms['Frm'+NomModule].target = "_blank";
										document.forms['Frm'+NomModule].action = Url + "?ACTION"+NomModule+"=EXPORTER_RESULTATS&"+NomModule+"_TYPE_EXPORT="+ObjTypeExport.value+"&NOM_MODULE="+NomModule;
										document.forms['Frm'+NomModule].submit();
										
										// On remet les parametres initiaux du formulaire
										document.forms['Frm'+NomModule].target = MemoTarget;
										document.forms['Frm'+NomModule].action = MemoUrl;
										//this.ouvrirPopup(Url,'' ,'scrollbars=yes,resizable=yes',300,50);
									}

		
		this.afficherAdresseMail = 	function(Login, Domaine, Extension, Link, Libelle, Subject, Body) {

										var Arobace = "@";
										var Email = Login + Arobace + Domaine + "." + Extension;
										var Html = "";
										var Mailto = "";
										
										// Pas de lien : mail affiché en texte simple	
										if (!Link) {
											Html = Email;
										}
										// Si ya un lien, on le compose
										else {
											Html = "<a href=\"mailto:" +Email;
											
											// On complete le mailto en fonction du subjet et /ou body renseignés
											// Soit les 2 sont remplis et on les concatène
											if (Subject !='' && Body !='') {
												Mailto = "?subject="+ Subject +"&body=" + Body;
											}
											// Soi un seul est rempli et on concatène que celui la.
											else {
												if (Subject !='') {
													Mailto = "?subject=" + Subject;
												}
												else {
													Mailto = "?body=" + Body;
												}
											}
											
											Html += Mailto +"\">";
											Html += (Libelle !='') ? Libelle : Email;
											Html += "</a>";
										}
										document.write(Html);
									}
	

		this.creerAlerte =			function(NomModule, Url) {
			
										var ListeCriteres = "&NOM_MODULE="+NomModule;
										var TailleForm = document.forms['Frm'+NomModule].elements.length-1;	
										
										for (var i=0; i<= TailleForm; i++) {
											var Champs = document.forms['Frm'+NomModule].elements[i];
											
											if (Champs.name.indexOf(NomModule+'_SEARCH') >= 0) {
												
												if ( (Champs.type !="checkbox" && trim(Champs.value) !="" && trim(Champs.value) != "-1") || (Champs.type=='checkbox' && Champs.checked) ) {
													ListeCriteres += "&"+Champs.name+"="+Champs.value;
												}
											}
										}
										
										if (ListeCriteres != "&NOM_MODULE="+NomModule) {
											this.ouvrirPopup(Url+ListeCriteres,'','scrollbars=yes,resizable=no',700,600);
										}
									}
									
}
	

// Instanciation de la classe Javascript d'Interface
var Interface = new JS_Interface; 

function buildQueryContext(querystring, param ) {
	indexPEq 		= param.indexOf("=");
	isFirstParam	= (querystring.indexOf("?")== -1);
	
	// On cherche la clef du PÃ„RAM
	if (indexPEq == -1)	return querystring; 
	key = param.substring(0,indexPEq+1);

	// On ajoute simplement la clef
	indexKey = querystring.indexOf(key);
	if (indexKey == -1) {
		if (isFirstParam) return querystring+"?"+param;
		else return querystring+"&"+param;
	}// end if

	// On remplace la valeur
	indexKeyFin = querystring.indexOf("&",indexKey);
	newQuery =	querystring.substring(0, indexKey);
	newQuery += param;
	if(indexKeyFin != -1) newQuery += querystring.substring(indexKeyFin,querystring.length);
	
	return newQuery;
}

function ChkWebSite(siteStr) {
	var emailPat=/^(.+)\.(.+)\.(.+)$/
	var matchArray=siteStr.match(emailPat)
	if (matchArray==null) {
		return false
	}		
	return true;
}
	

function checkedSources(tabIDSources, NomModule){
		frmSearchMask = document.forms['Frm' + NomModule];		
		var choix = frmSearchMask.elements['REQ_'+NomModule+'_SOURCE[]'];	
		for (i = 0; i<choix.length; i++) {					
			choix[i].checked = false;
			for (j = 0; j < tabIDSources.length ; j++) {						
				if (choix[i].value == tabIDSources[j]) choix[i].checked = true;
			} // end for
		} // end for
	}// end function



function trim(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '');
}	
	
/***********************************************************************************/	


