function longueur(maxi,champ,affiche) {
	var iLongueur, iLongueurRestante;
	iLongueur = document.getElementById(champ).value.length;
	document.getElementById(affiche).innerHTML = iLongueur;
	/*if (iLongueur > maxi) {
		document.getElementById(champ).value = document.getElementById(champ).value.substring(0,maxi);
		iLongueurRestante = 0;
		}
		else {
		iLongueurRestante = maxi - iLongueur;
		}
	if (iLongueurRestante <= 1)
		document.getElementById(affiche).innerHTML = iLongueurRestante + "&nbsp;caract&egrave;re&nbsp;disponible";
		else
		document.getElementById(affiche).innerHTML = iLongueurRestante + "&nbsp;caract&egrave;res&nbsp;disponibles";*/
	}

// sous-menu
function montre(id_menu) {
	// cache tous les menus
	for (var nb_menu = 1; nb_menu<=10; nb_menu++) {
		if ( document.getElementById("smenu" + nb_menu) ) {
			document.getElementById("smenu" + nb_menu).style.display = "none";
			//document.getElementById("emenu" + nb_menu).style.backgroundColor = "#B80000";
			}
		}
	// affiche le menu choisi	
	if (id_menu) {
		document.getElementById("smenu" + id_menu).style.display = "block";
		//document.getElementById("emenu" + id_menu).style.backgroundColor="#FFF";		
		}
	};

function edite_legende(sObjet,sDir,sFile) {	
	window.status = unescape(query("ajax_legende.php","fichier=" + sDir + "/" + sFile + "&texte=" + escape(document.getElementById(sObjet).value)));
	}

function edite_titre(sFile) {	
	window.status = unescape(query("ajax_legende.php","file=" + sFile + "&texte=" + escape(document.getElementById("textetitre").value)));
	}

function popup(url,larg,haut,barres) {
	var top=(screen.height-haut)/2;
	var left=(screen.width-larg)/2;
	window.open(url,"menu","top="+top+",left="+left+",width="+larg+",height="+haut+",location=no,toolbar=no,status=no,directories=no,menubar=no,scrollbars="+barres+",resizable=no,copyhistory=no");
	};

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

function check_empty(text) {
	return (text.length > 0);
	};

function confirmation(laPhrase) {
	var is_ok = confirm('Etes-vous sûr de vouloir ' + laPhrase + ' ?');
	return is_ok;
	};

// cache le div
function masqueBoite() {
	var cadre = document.getElementById("boxDrag");
	cadre.innerHTML = "";
	cadre.style.visibility="hidden";
	}
	
function modifierPanier(sVin,sQte) {
	document.getElementById("panier_qte" + sVin).value = sQte;
	document.getElementById("modifier" + sVin).submit();
	};
	
// recopie une valeur et va sur le champ si focus=1
function recopie(source,cible,is_focus) {
	var champ_source = document.getElementById(source);
	var champ_cible = document.getElementById(cible);
	if ( !champ_source ||!champ_cible )
		alert("Champs non trouvés");
		else
		{
		champ_cible.value = champ_source.value;
		if ( is_focus == 1 )
			champ_cible.focus();
		};
	};

// renvoie TRUE si au moins une option est cochée
function test_radio(radioname) {
	var is_checked = false;
	radioname = radioname.substring(6,radioname.length);
	for (var i = 0 ; i < document.getElementsByName(radioname).length ; i++) {
	
		if ( document.getElementsByName(radioname).item(i).checked ){
			is_checked = true;
			break;
			};
		};
	return is_checked;
	};


// test un champ, affiche_msg = 0 pour pas de message
function test_champ(nom_champ,libelle,affiche_msg) {
	var nb_erreurs = 0;
	var msg_erreur = "";
	// recherche champ
	var champ = document.getElementById(nom_champ);
	if ( champ == null ) {
		alert("Erreur formulaire : champ " + nom_champ + " non trouvé");			
		return 1;
		};
	// test valeur du champ
	if ( champ.value == "" || champ.value == 0) {		
		if ( affiche_msg )
			alert("Vous devez saisir " + libelle + ".");
			else
			msg_erreur = "Vous devez saisir " + libelle + ". ";
		champ.focus();
		nb_erreurs++;
		}
		else
		{
		// champ spécifique adresse e-mail
		if ( nom_champ.indexOf("adressemail") != -1 && !testAdresse(champ.value) ) {
			if ( affiche_msg )
				alert("L\'adresse e-mail est incorrecte.");
				else
				msg_erreur = "L\'adresse e-mail est incorrecte.";
			champ.focus();
			nb_erreurs++;
			};
		// champ spécifique téléphone
		if ( nom_champ.indexOf("tel",0) != -1 && !testTelephone(champ.value) ) {
			if ( affiche_msg )
				alert("Le numéro de téléphone est incorrect (format : 02 44 55 66 77 ou +33 2 44 55 66 77)");
				else
				msg_erreur = "Le numéro de téléphone est incorrect (format : 02 44 55 66 77 ou +33 2 44 55 66 77)";
			champ.focus();
			nb_erreurs++;
			};
		};
	if ( affiche_msg == 0 )
		return msg_erreur;
		else
		return nb_erreurs;
	};

function isValid(aChamps,aLibelles,autocode) {
	var nb_erreurs = 0;
	var type_champ_autre = false;
	// vérification cohérence champs et libellés associés
	if ( aChamps.length != aLibelles.length ) {
		alert("Problème configuration tableaux (" + aLibelles.length + " champs et " + aChamps.length + " libellés)" );
		return false;
		};
	
	// contrôle de tous les champs
	for ( var i = 0; i < aChamps.length; i++ ) {
		type_champ_autre = false;
		if ( autocode )
			var nom_champ = aChamps[i] + autocode;
			else
			var nom_champ = aChamps[i];
			
		// champ optionnel, à tester uniquement si valorisé
		if ( nom_champ.substring(0,7) == "option_" ) {
			type_champ_autre = true;
			// test si valorisé
			if ( document.getElementById(nom_champ.substring(7,nom_champ.length)).value != "" )
				nb_erreurs += test_champ(nom_champ.substring(7,nom_champ.length),aLibelles[i],1);
			};

		// champ au choix (noms séparés par pipe)
		if ( nom_champ.indexOf("|",0) != -1 ) {
			type_champ_autre = true;
			var liste_champs = nom_champ.split("|");			
			var erreur_choix = "";
			var nb_erreur_choix = 0;
			for ( j = 0 ; j < liste_champs.length ; j++ ) {
				var erreur_choix = test_champ(liste_champs[j],aLibelles[i],0);
				if ( erreur_choix.length > 0 ) {
					nb_erreur_choix++;  }
				};
			
			// erreurs maxi possibles : nombre total de champs-1
			if ( nb_erreur_choix > liste_champs.length-1 ) {
				alert(erreur_choix);
				nb_erreurs++;
				//nom_champ = liste_champs[0];
				};
			};

		// test champ radio
		if ( nom_champ.substring(0,6) == "radio_" ) {
			type_champ_autre = true;
			if ( test_radio(nom_champ) == false ) {
				alert("Vous devez saisir " + aLibelles[i] + ".");
				nb_erreurs++;
				break;
				};
			};

		// champ texte
		if ( type_champ_autre == false )			
			nb_erreurs += test_champ(nom_champ,aLibelles[i],1);
		
		if ( nom_champ.name != "undefined" )
			// si erreur, contour en rouge et stop les contrôles sinon contour couleur d'origine
			if ( nb_erreurs > 0 ) {
				if ( document.getElementById(nom_champ) != null ) {
					document.getElementById(nom_champ).style.borderColor = "#FF0000"; document.getElementById(nom_champ).focus(); }
				break;
				} else {
				if ( document.getElementById(nom_champ) != null )
					document.getElementById(nom_champ).style.borderColor = "#AAAAAA";
				};
		};
	if ( nb_erreurs <= 0 )
		return true;
		else
		return false;
	};

// vérification numéro de téléphone
function testTelephone(numero) {
	// autorise 01 à 09 (sauf 07) ou +33 1-9 et séparateurs ./- et espace
	var masque = /^(0|\+33\s?)[1|2|3|4|5|6|8|9]([\s\.\/\-]?\d{2}){4}$/;
	return masque.test(numero);
	};


function affiche(objet) {
	document.getElementById(objet).style.visibility = "visible";
	document.getElementById(objet).style.position = "relative";
	};

function masque(objet) {
	document.getElementById(objet).style.visibility = "hidden";
	document.getElementById(objet).style.position = "absolute";
	};

function testAdresse(adrMail) {
var adrMailLength=adrMail.length; // longueur de la chaîne adrMail
var caractOk="_.-0123456789abcdefghijklmnopqrstuvwxyz"; // caratères corect d'une adresse mail
var posArrobas=adrMail.indexOf('@',1); // position du premier Arrobas
var posPoint=adrMail.lastIndexOf('.',adrMailLength); // position du dernier point
var posCaractFaux=adrMail.indexOf('..',posArrobas+1); // position d'un caractère faux ('..' ou ' ')
var caract=0; // caractère extrait de la chaîne adrMail (utilisée par les boucles while)
var caractFaux=adrMail.charAt(0)+adrMail.charAt(posArrobas-1)+adrMail.charAt(posArrobas+1)+adrMail.charAt(posPoint-1);
var posCaract=0; // position du caractère à extraire de la chaîne adrMail
var posCaractOk=0; // position du caractère extrait de la chaîne adrMail
var posCaractNon=-1;
var retour = false;

if (posCaractFaux!=-1 || adrMail.indexOf(' ',0)!=-1 || adrMail.indexOf('--',0)!=-1 || adrMail.indexOf('__',0)!=-1) {
   posCaractFaux=0;
}

for (posCaract;posCaract<4;posCaract++) {
    caract=caractFaux.charAt(posCaract);
    posCaractNon=caractOk.lastIndexOf(caract,2);
    if (posCaractNon!=-1) {
       posCaractFaux=0;
       break;
    }
}
if (adrMailLength>0 && posArrobas!=-1 && posPoint!=-1 && posArrobas<posPoint && posCaractFaux==-1 && adrMailLength-posPoint-1>=2 && adrMailLength-posPoint-1<=4) {
   adrMail=adrMail.toLowerCase();
   posCaract=0;
   while (posCaract<posArrobas && posCaractOk!=-1) {
         caract=adrMail.charAt(posCaract);
         posCaractOk=caractOk.indexOf(caract,0);
         posCaract++;
   }
   posCaract++; // on passe l'arobase
   while (posCaract<posPoint && posCaractOk!=-1) {
         caract=adrMail.charAt(posCaract);
         posCaractOk=caractOk.indexOf(caract,1);
         posCaract++;
   }
   posCaract++; // on passe le point
   while (posCaract<adrMailLength && posCaractOk!=-1) {
         caract=adrMail.charAt(posCaract);
         posCaractOk=caractOk.indexOf(caract,13);
         posCaract++;
   } // fin de la boucle while
  
   if (posCaractOk!=-1) {
      retour=true;
   }
}
return retour;
}

/* menu hover IE5-6 */
var sfHover = function() {
	if (document.all && window.print && /MSIE [56]/.test(navigator.userAgent) && document.getElementById("menu") ) {
		var sfEls = document.getElementById("menu").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {	
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";			
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
