// JavaScript Document
/**************** LES COOKIES **********************/
	function EcrireCookie(nom, valeur) {
		var argv=EcrireCookie.arguments;
		var argc=EcrireCookie.arguments.length;
		var expires=(argc > 2) ? argv[2] : null;
		var path=(argc > 3) ? argv[3] : null;
		var domain=(argc > 4) ? argv[4] : null;
		var secure=(argc > 5) ? argv[5] : false;
		document.cookie=nom+"="+escape(valeur)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
	}
	function getCookieVal(offset) {
		var endstr=document.cookie.indexOf (";", offset);
		if (endstr==-1) endstr=document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr)); 
	}
	function LireCookie(nom) {
		var arg=nom+"=";
		var alen=arg.length;
		var clen=document.cookie.length;
		var i=0;
		while (i<clen) {
			var j=i+alen;
			if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
			i=document.cookie.indexOf(" ",i)+1;
			if (i==0) break;
		}
		return null; 
	}
	function EffaceCookie(nom) {
		date=new Date;
		date.setFullYear(date.getFullYear()-1);
		EcrireCookie(nom,null,date); 
	}
/***************************************************/
function TitreClic(NumTitre) {
	if (!NumTitre) {
		for (i=1;i<=10;i++) {
			if ( $('SSTITRE'+i) ) {
				new Effect.BlindUp($('SSTITRE'+i),{duration:0.5});
				$('FLECHE'+i).src = 'admin/images/fd_sstitre1.jpg';
			}
		}
	} else {
		if ( $('SSTITRE'+NumTitre).visible() ){
			new Effect.BlindUp($('SSTITRE'+NumTitre),{duration:0.5});
			$('FLECHE'+NumTitre).src = 'admin/images/fd_sstitre1.jpg';
		} else {
			new Effect.BlindDown($('SSTITRE'+NumTitre),{duration:0.5});
			$('FLECHE'+NumTitre).src = 'admin/images/fd_sstitre1_1.jpg';
		}
	}
}

function OuvreFermeMenu(numRub){
	var img = $('ADMIMGRUB'+numRub).src;
	if (img.substr(img.length-10,5)=='moins'){
		var NumImg = img.substr(img.length-5,1);
		$('ADMIMGRUB'+numRub).src = 'admin/images/plus'+NumImg+'.gif';
		new Effect.BlindUp($('ADMRUB'+numRub),{duration:0.3});
	} else if (img.substr(img.length-9,4)=='plus'){
		var NumImg = img.substr(img.length-5,1);
		$('ADMIMGRUB'+numRub).src = 'admin/images/moins'+NumImg+'.gif';
		new Effect.BlindDown($('ADMRUB'+numRub),{duration:0.3});
	}
}
/*************** AJOUT MENUS *********************/
	function CacheTousMenus(){
		var numImg;
		var Img;
		for (i=1;i<=100;i++){
			if ($('ADMRUB'+i)) {
				$('ADMRUB'+i).hide();
				Img = $('ADMIMGRUB'+i).src;
				numImg = Img.substr(Img.length-5,1);
				$('ADMIMGRUB'+i).src = "admin/images/plus"+numImg+".gif";
			}
		}
	}
	function AffichLink(TypeLink, IdRubLink, IdPageLink){
		if (!IdPageLink) IdPageLink = 0;
		for (i=1;i<=1000;i++) {
			if ( $('RUBDIV'+i) ) $('RUBDIV'+i).style.backgroundColor='';
			if ( $('SSRUBDIV'+i) ) $('SSRUBDIV'+i).style.backgroundColor='';
		}
		EcrireCookie('Menu_TypeLien',TypeLink);
		EcrireCookie('Menu_IdRubLien', IdRubLink);
		EcrireCookie('Menu_IdPageLien', IdPageLink);
	}
	function CheckLinkColor(TypeLink, IdLink, CouleurOut, CouleurOver){
		if ( TypeLink == 'Rubrique' ){
			if ( LireCookie('Menu_IdPageLien')==0 ){
				if (LireCookie('Menu_IdRubLien')==IdLink) return CouleurOut; else return CouleurOver;
			} else {
				return CouleurOver;
			}
		} else if ( TypeLink == 'Page' ){
			if (LireCookie('Menu_IdPageLien')==IdLink) return CouleurOut; else return CouleurOver;
		}
	}

/*************** FICHE RUBRIQUE*******************/
	function AffichCibleRubrique(Num){
		if ($('CIBLES').visible()) {
			if ($('CIBLE_PAGE').visible) new Effect.Fade($('CIBLE_PAGE'),{duration:0.3});
			if ($('CIBLE_URL').visible) new Effect.Fade($('CIBLE_URL'),{duration:0.3});
			if ($('CIBLE_POPUP').visible) new Effect.Fade($('CIBLE_POPUP'),{duration:0.3});
			if ($('CIBLE_MODULE').visible) new Effect.Fade($('CIBLE_MODULE'),{duration:0.3});
			if ($('CIBLE_FICHIER').visible) new Effect.Fade($('CIBLE_FICHIER'),{duration:0.3});
			new Effect.BlindUp($('CIBLES'),{duration:0.3,delay:0.1});
			$('ouvre_page').selectedIndex = 0;
			$('url').value = '';
			$('popup').value = '';
			$('popup_width').value = '';
			$('popup_height').value = '';
		}
		if (Num==1){
			new Effect.BlindDown($('CIBLES'),{delay:0.4,duration:0.3});
			new Effect.Appear($('CIBLE_PAGE'),{delay:0.4});
		} else if (Num==2){
			new Effect.BlindDown($('CIBLES'),{delay:0.4,duration:0.3});
			new Effect.Appear($('CIBLE_URL'),{delay:0.4});
		} else if (Num==3) {
			new Effect.BlindDown($('CIBLES'),{delay:0.4,duration:0.3});
			new Effect.Appear($('CIBLE_MODULE'),{delay:0.4});
		} else if (Num==4){
			new Effect.BlindDown($('CIBLES'),{delay:0.4,duration:0.3});
			new Effect.Appear($('CIBLE_FICHIER'),{delay:0.4});
		} else if (Num==5){
			new Effect.BlindDown($('CIBLES'),{delay:0.4,duration:0.3});
			new Effect.Appear($('CIBLE_POPUP'),{delay:0.4});
		}
	}
	function AjoutRubrique(){
		new Ajax.Updater($('CADRE_MENU_FICHE'),'admin/menu_rubrique.php',
				{
					method:'GET',
					parameters:{
						type: LireCookie('Menu_TypeLien'),
						id_rub: 0,
						id_page: 0
					},
					onLoading:Wait('CADRE_MENU_FICHE')
				}
			);
	}

/*************** FICHE NEWSLETTER *******************/
	//Appel page de modification d'un groupe
	function modif_groupe(id_elt)
	{
		new Ajax.Updater('CADRE','modules/newsletter/groupes_contacts.php',
			{
				method		: 'GET',
				parameters	: { modif_groupe: id_elt },
				onLoading	: Wait('CADRE')
			}
		);	
	}
	//Appel page de suppression d'un groupe
	function suppr_groupe(id_elt, nom_grp)
	{
		if (confirm('Souhaitez-vous supprimer le groupe '+nom_grp+' ?')) {
			new Ajax.Updater('CADRE','modules/newsletter/groupes_contacts.php',
				{
					method 		: 'GET',
					parameters 	: { suppr_groupe: id_elt },
					onLoading	: Wait('CADRE'),
					onComplete	: alert('Le groupe a été supprimé.')
				}
			)
		}
	}	
	//Modification des icones et valeur d'association membres...
	function asso_membre(idmembre)
	{
		if($F('etat_membre'+idmembre)==0) {
			$('img'+idmembre).src = 'admin/images/user01.gif'
			$('etat_membre'+idmembre).value = 1;
		} else {
			$('img'+idmembre).src = 'admin/images/user02.gif'
			$('etat_membre'+idmembre).value = 0;			
		}
	}
	//Sélection des groupes pour envoi de la Newsletter...
	function selection_groupe(idgrp)
	{
		if($('check'+idgrp).checked) {
			$('check'+idgrp).checked = false
		} else {
			$('check'+idgrp).checked = true		
		}
	}	
	//Enregistrement de la crétion du groupe et de ses membre
	function valid_creation_groupe()
	{
		var liste_m = new Array();
		var tab = new Array();
		nom_grp = $F('nom_groupe');
		description_grp = $F('description_groupe');
		if(nom_grp=='')
			alert('Le nom du groupe est obligatoire.')
		else {
			tab = $$('input');
			if(tab.length!=0) {
				j = 0;
				for(i=0; i<tab.length; i++) {
					if(tab[i].id.substring(0, 11) == 'etat_membre' && tab[i].value==1) { 
						liste_m[j] = tab[i].id.replace('etat_membre', '');
						j++;
					}
				}
				if(liste_m.length==0)
				{
					alert('Vous devez sélectionner au moins 1 membre');
				}
				else
				{
					new Ajax.Updater('CADRE','modules/newsletter/groupes_contacts.php',
						{
							method 		: 'GET',
							parameters 	: { nom_grp_get: nom_grp, description_grp_get : description_grp, liste_mbr : liste_m.join('|') },
							onLoading	: Wait('CADRE')
						}
					)
				}
			}
		}
	}

	function valid_update_groupe(grp_to_update)
	{
		var liste_m = new Array();
		var tab = new Array();
		nom_grp = $F('nom_groupe');
		description_grp = $F('description_groupe');
		if(nom_grp=='')
			alert('Le nom du groupe est obligatoire.')
		else {
			tab = $$('input');
			if(tab.length!=0) {
				j = 0;
				for(i=0; i<tab.length; i++) {
					if(tab[i].id.substring(0, 11) == 'etat_membre' && tab[i].value==1) { 
						liste_m[j] = tab[i].id.replace('etat_membre', '');
						j++;
					}
				}
				if(liste_m.length==0)
				{
					alert('Vous devez sélectionner au moins 1 membre');
				}
				else
				{
					new Ajax.Updater('CADRE','modules/newsletter/groupes_contacts.php',
						{
							method 		: 'GET',
							parameters 	: { nom_grp_get : nom_grp, description_grp_get : description_grp, liste_mbr : liste_m.join('|'), grp_update : grp_to_update },
							onLoading	: Wait('CADRE')
						}
					)
				}
			}
		}
	}
	//Envoi de la newsletter
	function valid_envoi_newsletter(id_news)
	{
		var liste_g = new Array();
		var liste_p = new Array();
		var liste_c = new Array();
		var tab = new Array();
		liste_p = $F('liste_ponctuelle').split('\r\n');
		tab = $$('input[type=checkbox]');
		if(tab.length!=0) {
			j = 0;
			for(i=0; i<tab.length; i++) {
				if(tab[i].checked) { 
					liste_g[j] = tab[i].id.replace('check', '');
					j++;
				}
			}
		}
		if(liste_p.length!=0) {
			j=0;
			for(i=0; i<liste_p.length; i++) {
				liste_c[j] = liste_p[i];
				j++;
			}
		}
		if((parseInt(liste_g.length) + parseInt(liste_c.length))==0)
			alert('Votre sélection ne contient pas de destinataire, veuillez vérifier.')
		else
		{
			//alert("envoi : 1, id_newsletter : "+id_news+", groupe_destinataire : "+liste_g.join('|')+", email_complementaire : "+liste_c.join('|'))
			new Ajax.Request('modules/newsletter/envoi_newsletter.php',
				{
					method 		: 'GET',
					parameters 	: { envoi : 1, id_newsletter : id_news, groupe_destinataire : liste_g.join('|'), email_complementaire : liste_c.join('|') },
					onLoading	: Wait('CADRE'),
					onComplete	: function(xhr) {
							alert(xhr.responseText);
							new Ajax.Updater('CADRE','modules/newsletter/newsletter_liste.php')
					}
				}
			)
		}
	}	
	
	function renvoyer(id_de_envoi)
	{
		new Ajax.Updater('CADRE','modules/newsletter/envoi_newsletter.php',
			{
				method 		: 'GET',
				parameters 	: { idenvoi : id_de_envoi  },
				onLoading	: Wait('CADRE')
			}
		)		
	}

	//test email Newsletter
	function verif(email) { 
	  var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
	   return (reg.exec(email)!=null)
	} 
	
	
	function envoyer_inscription()
	{
		if($F('nom_inscription')=='' || $F('prenom_inscription')=='' || $F('mail_inscription')=='')
			alert("Les champs avec '*' sont obligatoires")
		else {
			if(verif($F('mail_inscription'))== false) {	
				alert("Votre e-mail est incorrect, vérifiez votre saisie !");
			} else { 
				nom = $F('nom_inscription')
				prenom = $F('prenom_inscription')
				mail =  $F('mail_inscription')
				new Ajax.Updater('PRINCIPAL','modules/newsletter/inc_inscription.php',
					{
						method 		: 'POST',
						parameters 	: { nom_inscrit : nom, prenom_inscrit : prenom, mail_inscrit : mail},
						onLoading	: Wait('PRINCIPAL'),
						onComplete	: function(xhr) {
							//alert(xhr.responseText)
						}
					}
				)		
			}
		}
	}	
	
	function return_inscription()
	{
		new Ajax.Updater('PRINCIPAL','modules/newsletter/inc_inscription.php');
	}
	
	function inscrit_desinscrit(id_util, rech)
	{
		new Ajax.Updater('CADRE','modules/newsletter/groupes_contacts.php', 
			 {
				method 		: 'GET',
				parameters 	: { recherche : rech, id_ins_des : id_util},
				onLoading	: Wait('CADRE')				 
			 }
		);
	}
	function desinscrire() {
		if($F('nom_inscription')=='' || $F('prenom_inscription')=='' || $F('mail_inscription')=='')
			alert("Les champs avec '*' sont obligatoires")
		else {		
				nom = $F('nom_inscription')
				prenom = $F('prenom_inscription')
				mail =  $F('mail_inscription')
				new Ajax.Updater('PRINCIPAL','modules/newsletter/desinscription.php',
					{
						method 		: 'POST',
						parameters 	: { nom_desinscrit : nom, prenom_desinscrit : prenom, mail_desinscrit : mail},
						onLoading	: Wait('PRINCIPAL')
					}
				)
		}
	}
//---- PARAMETRES "GESTION DES PLIGINS"
	function specif_accueil_use(CheckId){
		if ( CheckId.checked ) $('specif_accueil_num').disabled = false
		else $('specif_accueil_num').disabled = true
	}
//---- RESIZE DE "pages_details" (MAXIMIZE / MINIMIZE"
	function pages_details_resize(Fenetre, elt1, val1, mode_ouverture){
		
		if ( !mode_ouverture) mode_ouverture = '';
		if ( !elt1 ) elt1 = '';
		if ( !val1 ) val1 = '';

		var W 	 = $('PRINCIPAL').getWidth(); //document.body.offsetWidth
		var H 	 = document.body.offsetHeight;
		var F4_W = $(Fenetre).getWidth();
		var F4_H = $(Fenetre).getWidth();
		
		if ( basename($('img_maxmin').src)=='img_maximize.gif' ){
		// Agrandir
			new Effect.Morph(Fenetre, {
						style: {
							top: '5px',
							width: W + 'px',
							height: (H-15) +'px',
							marginLeft: - (W /2) + 'px'
						},
						duration: 0.5
			})
			window.setTimeout(function() {
				switch(mode_ouverture) {
					case '':
						$(elt1 + '___Frame').style.height = (parseInt(H-20)+parseInt(val1)) + 'px'
						break;
					case 'GONDOLE':
						break;
					
				}
			},500);

			$('img_maxmin').src = 'admin/images/img_restore.gif'
		} else {
		// Rétrécir
			new Effect.Morph(Fenetre, {
						style: {
							top: '40px',
							width: '750px',
							height: '500px',
							marginLeft: '-375px'
						},
						duration: 0.5
			})
			switch(mode_ouverture) {
				case '':
					$(elt1 + '___Frame').style.height = '408px'
					break;
				case 'GONDOLE':
					break;
				
			}			
			$('img_maxmin').src = 'admin/images/img_maximize.gif'
		}
	}
	
//Fonction d'affichage des pages disponibles sur le site dans le module d'admin
function affiches_pages_recherche() {
	new Ajax.Updater('liste_pages_internes', 'admin/pages_liste_contenu.php', {
		method: 'get',
		parameters : { recherche : $F('recherche') },
		evalScripts: true,
		onLoading : Wait('liste_pages_internes')
	});
}

function selection_type(cat, sscat) {
	if(!cat) cat = '';
	if(!sscat) sscat = '';
	new Ajax.Updater('complement_select', 'modules/catalogue/liste_gondole_detail_type.php', {
		method: 'get',
		parameters : { type : $F('type'), categorie : cat, sscategorie: sscat },
		evalScripts: true,
		onComplete : function affiche_rech_art_gondole() {
			type_gondole = $F('type')
			if(type_gondole==0) {
				niveau_gondole = $F('selection_categorie');	
				new Ajax.Updater('art_dispo', 'modules/catalogue/liste_gondole_v2_recherche.php', {
					evalScripts: true
				});
			} 
			else {
				if(type_gondole==1) {
					niveau_gondole = $F('selection_categorie');	
					new Ajax.Updater('art_dispo', 'modules/catalogue/liste_gondole_v2_recherche.php', {
						method : 'get',
						parameters : { cat_select : niveau_gondole },
						evalScripts: true
					});
				} 
				if(type_gondole==2) {
					niveau_gondole = $F('selection_categorie');	
					tab_niveau_gondole = niveau_gondole.split('|');
					niveau_gondole_1 = tab_niveau_gondole[0];
					niveau_gondole_2 = tab_niveau_gondole[1];
					new Ajax.Updater('art_dispo', 'modules/catalogue/liste_gondole_v2_recherche.php', {
						method : 'get',
						parameters : { cat_select : niveau_gondole_1, scat_select : niveau_gondole_2 },
						evalScripts: true						
					});
				} 				
			}
		}
	});

}

function recherche_article() {
	type_gondole = $F('type')
	critere_recherche = $F('recherche_art');
	if(type_gondole==0) {
		new Ajax.Updater('art_dispo', 'modules/catalogue/liste_gondole_v2_recherche.php', {
			method : 'get',
			parameters : { recherche : critere_recherche },						 
			evalScripts: true
		});
	} 
	else {
		if(type_gondole==1) {
			niveau_gondole = $F('selection_categorie');	
			if(niveau_gondole!='') {
				new Ajax.Updater('art_dispo', 'modules/catalogue/liste_gondole_v2_recherche.php', {
					method : 'get',
					parameters : { cat_select : niveau_gondole, recherche : critere_recherche },
					evalScripts: true
				});
			}
		} 
		if(type_gondole==2) {
			niveau_gondole = $F('selection_categorie');	
			tab_niveau_gondole = niveau_gondole.split('|');
			niveau_gondole_1 = tab_niveau_gondole[0];
			niveau_gondole_2 = tab_niveau_gondole[1];
			if(niveau_gondole_1!='' && niveau_gondole_2!='') {
				new Ajax.Updater('art_dispo', 'modules/catalogue/liste_gondole_v2_recherche.php', {
					method : 'get',
					parameters : { cat_select : niveau_gondole_1, scat_select : niveau_gondole_2, recherche : critere_recherche },
					evalScripts: true						
				});
			}
		} 				
	}	
}
function affiche_art_gondole() {
	new Ajax.Updater('art_select', 'modules/catalogue/liste_gondole_ent_art_dispos.php');
}
function select_art_gondole(id_art) {
	new Ajax.Updater('art_select', 'modules/catalogue/liste_gondole_ent_art_dispos.php', {
		method : 'get',
		parameters : { art_add : id_art },
		evalScripts: true 					
	});
}
function del_art_gondole(id_art) {
	new Ajax.Updater('art_select', 'modules/catalogue/liste_gondole_ent_art_dispos.php', {
		method : 'get',
		parameters : { art_del : id_art },
		evalScripts: true						
	});
}

function monte_art(id_art) {
	new Ajax.Updater('art_select', 'modules/catalogue/liste_gondole_ent_art_dispos.php', {
		method : 'get',
		parameters : { art_monte : id_art },
		evalScripts: true						
	}); 
}

function descend_art(id_art) {
	new Ajax.Updater('art_select', 'modules/catalogue/liste_gondole_ent_art_dispos.php', {
		method : 'get',
		parameters : { art_descend : id_art },
		evalScripts: true						
	}); 
}

function monte_actualite(id_actu) {
	new Ajax.Updater('CADRE', 'modules/actualites/liste.php', {
		method : 'get',
		parameters : { actu_monte : id_actu },
		evalScripts: true,
		onLoading	: Wait('CADRE')						
	}); 
}

function descend_actualite(id_actu) {
	new Ajax.Updater('CADRE', 'modules/actualites/liste.php', {
		method : 'get',
		parameters : { actu_descend : id_actu },
		evalScripts: true,
		onLoading	: Wait('CADRE')
	}); 
}
function basename(path, suffix) {

	var b = path.replace(/^.*[\/\\]/g, '');
    
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    
    return b;
}
