function testEmail(sMode,sAdresse) {
	loading("wait" + sMode,"Vérification de l'adresse en cours...");	
	var result = unescape(query("/ajax_email.php","adresse=" + sAdresse + "&mode=" + sMode));	
	if (result!="")
		alert(result);
	loading("wait" + sMode,"");
	}

// roue de chargement
function loading(nom_image,texte) {
	if ( document.getElementById(nom_image).style.display == "none" ) {
		document.getElementById(nom_image).style.display = "inline";
		window.status = texte;
		} else {
		document.getElementById(nom_image).style.display = "none";
		window.status = texte;
		};
	};

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";*/
	}


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();
		};
	};


// 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 testMax(qtemax) {
	ok = false;
	if ( document.addtocart.panier_qte.value > qtemax )
		{
		alert("La quantité demandée est supérieure au maximum autorisé.");
		ok = false;
		document.addtocart.panier_qte.value = qtemax;
		}
		else 
		{ ok = true; }
	if ( document.addtocart.panier_qte.value <= 0 )
		ok = false;
	if ( ok )
		return true;
		else
		return false;
	};
	
function copie_adresse() {
	document.getElementById("genre_liv").value = document.getElementById("genre").value;
	document.getElementById("nom_liv").value = document.getElementById("nom").value;
	document.getElementById("prenom_liv").value = document.getElementById("prenom").value;
	document.getElementById("societe_liv").value = document.getElementById("societe").value;
	document.getElementById("adresse1_liv").value = document.getElementById("adresse1_fac").value;
	document.getElementById("adresse2_liv").value = document.getElementById("adresse2_fac").value;
	document.getElementById("cp_liv").value = document.getElementById("cp_fac").value;
	document.getElementById("ville_liv").value = document.getElementById("ville_fac").value;
	document.getElementById("pays_liv").options.selectedIndex = document.getElementById("pays_fac").options.selectedIndex;
	};

function valid_commande(points,montant) {
	var commande = false;
	for ( var i=0 ; i < document.validation.paiement.length ; i++) {
		 if (document.validation.paiement[i].checked) {
			commande = true;
			};
		};
	points = parseFloat(points);
	montant = parseFloat(montant);
	if ( document.getElementById("utiliser_points") != null )
		utiliser_points = document.getElementById("utiliser_points").checked;
		else
		utiliser_points = false;
	if ( utiliser_points && points > montant ) {
		commande = true; }
	if ( commande == false ) {
		/* points insuffisants : mdp pour complément */
		if ( utiliser_points && points < montant )
			alert("Vous devez choisir un moyen de paiement pour compléter le paiement de la commande.");
		/* pas d'utilisation des points : mdp requis */
		if ( !utiliser_points )
			alert("Vous devez choisir un moyen de paiement pour valider la commande.");
		} else {
		// moyen de paiement OK
		if ( document.getElementById("genre_liv").value == ""
			|| document.getElementById("nom_liv").value == ""
			|| document.getElementById("prenom_liv").value == ""
			|| document.getElementById("adresse1_liv").value == ""
			|| document.getElementById("cp_liv").value == ""
			|| document.getElementById("ville_liv").value == ""
			|| document.getElementById("pays_liv").value == ""
			) {
			alert("Vous devez compléter les informations de livraison.");
			commande = false;
			} else {
				if ( !document.getElementById("cgv").checked ) {
					alert("Vous devez accepter les conditions générales de vente pour valider la commande.");
					commande = false;
				};
			};
		};
	if ( commande == true ) {
		if ( confirm("Confirmez-vous la validation définitive de cette commande ?") )
			return true;
			else
			return false;
		} else return false;
	return false;
	};

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;
}

var sfHover = function() {
	if ( document.getElementById("cadremenu") ) {
		var sfEls = document.getElementById("cadremenu").getElementsByTagName("span");	
	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);
