// JavaScript Document
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
<!--

function trim(str) {
   return str.replace(/(^\s*)|(\s*$)/g,"");
}

//fonction pour la partie connexion
function verif(){
	if(document.frm.login.value=="Nom d'utilisateur"){
		alert('Vous devez renseigner votre nom d\'utilisateur.');
		return false;
	}
	if(document.frm.login.value==''){
		alert('Vous devez renseigner votre nom d\'utilisateur.');
		return false;
	}
	if(document.frm.mdp.value==''){
		alert('Vous devez renseigner votre mot de passe.');
		return false;
	}
	if(document.frm.mdp.value.length<6){
		alert('Mot de passe incorrect.');
		document.frm.mdp.focus();
		return false;
	}
	return true; 
}
function connect(){
	if(verif()==true){
		document.frm.submit();
	}
}

//fonction pour la recherche
function recherche(){
	if(document.frm_search.searchs.value==''){
		alert('Vous devez saisir un mot pour effectuer une recherche.');
		document.frm_search.searchs.focus();
	}else{
		document.frm_search.submit();
	}
}


function checkFormCP(cp){
	var codep     = eval(cp).value 
	var verif     = /^[0-9]{5}$/
	//avec le cas des CP corses  /^((2[A-B])|([0-9]{2}))[0-9]{3}$/
	if (verif.exec(codep) == null){
		alert("Le code postal n\'est pas valide");
		eval(cp).value="";
		eval(cp).focus();
		return false;
	}else{
		return true;
	} 
}

function checkFormTel(tel,type,msg,obli){
	var ntel = trim(eval(tel).value);
	ntel = ntel.replace(/[^0-9]/gi,"");
	
	
	
	var i=0;
	var telF="";
	
	if(ntel.length==10){
		while(i<10)   { 
			telF=telF + ntel.substr(i, 2) + " "; 
			i=i+2;
		} 
	}

	
	
	telF=trim(telF);
	eval(tel).value=telF;
	
	if((telF.length==0)&&(obli==0))	return true;	//Si champ non obligatoire
	
	var verif="";
	//0-25/56+23 8_4_4

	switch (type){
		case "F" :
			verif     = /^0[1-58]( [0-9]{2}){4}$/	//N° de tel fixe
			break;
		case "P" :
			verif     = /^06( [0-9]{2}){4}$/		//N° de tel portable
			break;
		default :
			verif     = /^0[1-68]( [0-9]{2}){4}$/	//N° de tel fixe et portable
			break;
	}
	
	
	if (verif.exec(telF) == null){
		alert("Le numéro de " + msg + " n\'est pas valide");
		eval(tel).focus();
		eval(tel).select();
		return false;
	}else{
		eval(tel).value=telF;
		return true;
	} 
}

//teste le champ de formulaire : non vide (nom de champ de formulaire, libellé
function checkFormTexte(txt, libelle,len){
	if(trim(eval(txt).value).length<len){
		alert(libelle + " est un champ obligatoire!");
		eval(txt).focus();
		return false;
	}
	return true;
}

//teste le champ de formulaire : année actuelle ou à venir
function checkFormAnneeActuelle(an){
	var annee = trim(eval(an).value);
	annee = annee.replace(/[^0-9]/gi,"");
	
	Today = new Date;
	if(annee<Today.getFullYear()){
		alert("L\'année est un champ obligatoire!");
		eval(an).focus();
		return false;
	}
	return true;
}

//teste la validité d'une adresse mail
function verifmail(email,obli){
	var verif = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-zA-Z]{2,3}$/
	
	if((obli==0)&&(eval(email).value == ""))	return true
	if ((verif.exec(eval(email).value) == null)||(eval(email).value == "")){
		alert("Cet email n'est pas valide!");
		eval(email).focus();
		return false;
	}
	return true;
}


//Vérification d'un champ date
function checkDate(df,obli) {
   //obli=1 : champ obligatoire ; =0 : champ non obligatoire
   // Cette fonction vérifie le format JJ/MM/AAAA saisi et la validité de la date.
   // Le séparateur est défini dans la variable separateur

   var d=trim(eval(df).value);
   var amin=1999; // année mini
   var amax=2070; // année maxi
   var separateur="/"; // separateur entre jour/mois/annee
   var j=(d.substring(0,2));
   var m=(d.substring(3,5));
   var a=(d.substring(6));
   var ok=true;

if((obli==1)||(d!="")){
	   var tmpMsg="\nLa date doit être au format jj\/mm\/aaaa.";
	   if ( ((isNaN(j))||(j<1)||(j>31)) && (ok==true) ) {
		  alert("Le jour n'est pas correct."+tmpMsg); eval(df).focus();ok=false;
	   }
	   if ( ((isNaN(m))||(m<1)||(m>12)) && (ok==true) ) {
		  alert("Le mois n'est pas correct."+tmpMsg); eval(df).focus();ok=false;
	   }
	   if ( ((isNaN(a))||(a<amin)||(a>amax)) && (ok==true) ) {
		  alert("L'année n'est pas correcte."+tmpMsg); ok=false;eval(df).focus();
	   }
	   if ( ((d.substring(2,3)!=separateur)||(d.substring(5,6)!=separateur)) && (ok==true) ) {
		  alert("Les séparateurs doivent être des "+separateur+tmpMsg);eval(df).focus(); ok=false;
	   }
	   if (ok==true) {
		  var d2=new Date(a,m-1,j);
		  j2=d2.getDate();
		  m2=d2.getMonth()+1;
		  a2=d2.getFullYear();
		  if (a2<=100) {a2=1900+a2}
		  if ( (j!=j2)||(m!=m2)||(a!=a2) ) {
			 alert("La date "+d+" n'existe pas !");
		 eval(df).focus();
			 ok=false;
		  }
	   }
   }
   return ok;
}

//Boutons radios
function testerRadio(radio, libelle) {
	var sel=false;
    for (var i=0; i<eval(radio).length;i++) {
         if (eval(radio)[i].checked) {
            sel=true;
         }
    }
	if(sel==false){
		alert(libelle+" est obligatoire");
	}
	return sel;
}

	function blink_show()
	{
			for (i=0; i<document.getElementsByTagName("span").length; i++) {
				if (document.getElementsByTagName("span").item(i).className == "blink"){
					document.getElementsByTagName("span").item(i).style.display='';
				}
			}
		
		window.setTimeout( 'blink_hide()', 700 );
	}

	function blink_hide()
	{
			for (i=0; i<document.getElementsByTagName("span").length; i++) {
				if (document.getElementsByTagName("span").item(i).className == "blink"){
					document.getElementsByTagName("span").item(i).style.display='none'
				}
			}
		
		window.setTimeout( 'blink_show()', 250 );
	}

function ajout(a){
	document.frmajout.id_fiche.value=a;
	document.frmajout.submit();
}
function openssg(a){
	closessg();
	if(document.getElementById('sg'+a).style.display=='block') etat='none';
	else etat='block';
	document.getElementById('sg'+a).style.display=etat;
	
	for (i=0; i<document.getElementsByTagName("img").length; i++) {
		if (document.getElementsByTagName("img").item(i).className == "test"+a){
			id=document.getElementsByTagName("img").item(i).id;
			tmp=id.split('##');
			imagetmp=tmp[1];
			document.getElementsByTagName("img").item(i).src='images/articles/'+imagetmp;
		}
	}
}

function closessg(){
	for (i=0; i<document.getElementsByTagName("div").length; i++) {
		if (document.getElementsByTagName("div").item(i).className == "ssGamme"){
			document.getElementsByTagName("div").item(i).style.display='none';
		}
	}
}
function condition(){
	page='cgv.php';
	window.open(page,"","menubar=no, status=no, scrollbars=yes, menubar=no, width=800, height=500");
}
function plus(a){
	document.frm_modif.id.value=a;
	document.frm_modif.sens.value=1;
	document.frm_modif.submit();
}
function moin(a){
	document.frm_modif.id.value=a;
	document.frm_modif.sens.value=2;
	document.frm_modif.submit();
}
function suppre(a){
	if(confirm('Etes-vous sur de vouloir supprimer cet article ?')==true){	
		document.frm_panier.supp_art.value=a;
		document.frm_panier.submit();
	}
}
function ajoutFiche(a){
	if(test_quant(a)==true){
		document.frm_fiche.quantite_fiche.value=document.getElementById('nombre').value;
		document.frm_fiche.submit();
	}
}

function test_quant(a){
	if(document.getElementById('nombre').value==''){
		alert('Vous devez saisir la quantité de piece souhaitée.');
		document.getElementById('nombre').focus();
		return false;
	}
	
	if(isNaN(document.getElementById('nombre').value)){
		alert('Vous devez saisir un entier pour la quantité.');
		document.getElementById('nombre').focus();
		return false;
	}
	var chkZ = 1;
	for(i=0;i<document.getElementById('nombre').value.length;++i){
		if((document.getElementById('nombre').value.charAt(i) < "0")	|| (document.getElementById('nombre').value.charAt(i) > "9"))
		chkZ = -1;
	}
	if(chkZ == -1) {
		alert("Vous devez saisir un entier pour la quantité.");
		document.getElementById('nombre').focus();
		return false;
	}
	
	if(document.getElementById('nombre').value<1){
		alert('Vous devez saisir une quantité supérieure a 0');
		return false;
	}
	if(document.getElementById('nombre').value>a){
		alert('La quantité souhaitée est trop grande.');
		return false;
	}
	return true;
}

function validationContact(){
	if(checkFormTexte('document.formContact.Nom', 'Le nom',2)==false) return false;
	if(checkFormTexte('document.formContact.Prenom', 'Le prénom',2)==false) return false;
	if(checkFormTexte('document.formContact.Adresse', 'La saisie de votre adresse',2)==false) return false;
	if(checkFormCP('document.formContact.Code_postal')==false) return false;
	if(checkFormTexte('document.formContact.Ville', 'La ville',2)==false) return false;
	if(checkFormTexte('document.formContact.Pays', 'Le pays',2)==false) return false;
	if(checkFormTel('document.formContact.Telephone','', 'téléphone')==false) return false;
	if(verifmail('document.formContact.Email')==false) return false;
	if(checkFormTexte('document.formContact.Message', 'La saise de votre message',2)==false) return false;
	return true;
}
function valid(){
	if(validationContact()!=false) document.formContact.submit();
}

function loadFamille(){
	for (i=0; i<document.getElementsByTagName("div").length; i++) {
		if (document.getElementsByTagName("div").item(i).className == "imgTmp"){
			id=document.getElementsByTagName("div").item(i).id;
			tmp=id.split('##');
			imagetmp=tmp[1];
			var img = new Image();
			img.src='images/articles/'+id;
			document.getElementById('img'+id).src='images/articles/v2_'+imagetmp;
			document.getElementById('img'+id).style.display='';
		}
	}	
}

