﻿// FONCTIONS DE TEST DE FORMULAIRE //

var color_error = "#A8B9CB";
var color = "#FFFFFF";




//affiche date et heure
/*function dateheure()
{
	days = new Array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
	months = new Array("Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre");

	var mydate = new Date();
	var year = mydate.getYear();
	if (year < 2000) {
		if (document.all)
			year = "19" + year;
		else
			year += 1900;
	}
	var day = mydate.getDay();
	var month = mydate.getMonth();
	var daym = mydate.getDate();
	if (daym < 10)
		daym = "0" + daym;
	var hours = mydate.getHours();
	var minutes = mydate.getMinutes();
	var seconds = mydate.getSeconds();
	var dn = "AM";*/
	/*if (hours >= 12) {
		dn = "PM";
		hours = hours - 12;
	}
	if (hours == 0)
		hours = 12;*/
	/*if (minutes <= 9)
		minutes = "0" + minutes;
		
	var horloge = days[day]+" "+daym+" "+months[month]+" "+" "+hours+":"+minutes;
	
	
	if(document.getElementById)
	{
		document.getElementById("clock").innerHTML=horloge;
	}
	if(document.layers)
	{
		document.clock.document.write(horloge);
		document.clock.document.close;
	}

	setTimeout("dateheure()",1000);
} window.onload=dateheure;*/


//effectue un trim sur les form pour eviter les champs pseudo-vides remplis par des espaces
function trim(string)
{
return string.replace(/(^\s*)|(\s*$)/g,'');
}

//pour l'utilisation de upload.php et popup_upload => si case décochée, alors bouton parcourir grisé, sinon...
function decoche(bouton, check, texte)
{
	if(check.checked==true)
	{
		bouton.disabled="";
		texte.disabled="";
	}
	else
	{
		bouton.disabled="disabled";
		texte.disabled="disabled";
	}
}



// Confirmation de la validation d'un formulaire (suppression d'un élément)
function confirm_del() {
	if (confirm("Etes vous sûr de vouloir supprimer cet élément ?")) 
		return true;
	else
		return false;
}

function is_checked(myChamp, champ_name)
{
	if (!myChamp.checked)
	{
		myChamp.style.background = color_error;
		alert("La case "+champ_name+" n'est pas cochée");
		return false;
	}
	else
	{
		myChamp.style.background = color;
		return true;
	}
}

// Vérifie si le champ est un nombre et s'il est comprise entre
// les valeurs start et end
// Vérifie si le champ est un nombre et s'il est compris entre
// les valeurs start et end
function check_range(myChamp, start, end, champ_name)
{
	if (check_number(myChamp))
	{
		if (myChamp.value < start || myChamp.value > end)
		{
			myChamp.style.background = color_error;
			myChamp.focus();
			alert(myChamp.value+" n'est pas compris dans l'intervalle "+start+" "+end);
    	   	return false;
		}
		if (myChamp.value >= start && myChamp.value <= end)
		{
			myChamp.style.background = color;
    	   	return true;
		}
		
	}
	else
	{
		myChamp.style.background = color_error;
		myChamp.focus();
       	return false;
	}
	
	return true;
}

// 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
function check_date(myDate, myChamp)
{ 	 
      var amin=1900; // année mini
      var amax=2900; // année maxi
      var separateur="/"; // separateur entre jour/mois/annee
      var j=(myDate.value.substring(0,2));
      var m=(myDate.value.substring(3,5));
      var a=(myDate.value.substring(6,myDate.value.length));
      var ok=true;
	 
	  if (myDate.value.length != 0)
	  {
		  if (((isNaN(j))||(j<1)||(j>31)) && (ok==1) ) {
			 alert("Le jour de "+myChamp+" n'est pas correct."); ok=false;
		  }
		  if (((isNaN(m))||(m<1)||(m>12)) && (ok==1) ) {
			 alert("Le mois de "+myChamp+" n'est pas correct."); ok=false;
		  }
		  if (((isNaN(a))|| (a<amin)||(a>amax)) && (ok==1) ) {		  				
			 alert("L'année de "+myChamp+" n'est pas correcte."); ok=false;					
		  }
		  if (((myDate.value.substring(2,3)!=separateur)||(myDate.value.substring(5,6)!=separateur)) && (ok==1) ) {
			 alert("Les séparateurs de "+myChamp+" doivent être des "+separateur); ok=false;
		  }
		  /*if (ok==1) {
			 var d2=new Date(a,m-1,j);
			 j2=d2.getDate();
			 m2=d2.getMonth()+1;
			 a2=d2.getYear();
			 if (a2<=100) {a2=1900+a2}
			 if ( (j!=j2)||(m!=m2)||(a!=a2) ) {
				alert("La date "+myDate.value+" n'existe pas !");
				ok=false;
			 }
		  }*/
	  }
	  else
	  {
	  	  ok = true;
	  }
	  
	  if (ok == false)
	  {
		myDate.focus();		
		myDate.style.background = color_error;
	  }
	  else
	  {
	  	myDate.style.background = color;
	  }

      return ok;
}

// Vérifie que date_inf est inférieure à date_sup
function check_date_inf(date_inf, date_sup)
{
	var j_inf = (date_inf.value.substring(0,2));
    var m_inf = (date_inf.value.substring(3,5));
    var a_inf = (date_inf.value.substring(6,date_inf.value.length));
	var j_sup = (date_sup.value.substring(0,2));
    var m_sup = (date_sup.value.substring(3,5));
    var a_sup = (date_sup.value.substring(6,date_sup.value.length));

	
	if (a_inf <= a_sup)
	{	
		if (a_inf ==  a_sup)
		{
			if (m_inf <= m_sup)
			{
				if (m_inf == m_sup)
				{	
					if (j_inf <= j_sup)
					{
						date_sup.style.background = color;
						return true;
					}
					else
					{	
						date_sup.style.background = color_error;
						date_sup.focus();
						alert(date_inf.value+" est supérieure à "+date_sup.value);
						return false;
					}
				}
				else
				{
					date_sup.style.background = color;
					return true;
				}				
			}
			else
			{
				date_sup.style.background = color_error;
				date_sup.focus();
				alert(date_inf.value+" est supérieure à "+date_sup.value);			
				return false;
			}
		}
		else
		{
			date_sup.style.background = color;
			return true;
		}
	}
	else
	{
		date_sup.style.background = color_error;
		date_sup.focus();
		alert(date_inf.value+" est supérieure à "+date_sup.value);
		return false;
	}
}

// Vérifie si un champs contient une valeur
function check_length(myChamp, champ_name)
{
	if (myChamp.value.length == 0)
	{
		alert("Le champ "+champ_name+" est vide.");
		myChamp.style.background = color_error;
		myChamp.focus();		
		return false;
	}
	else
	{
		myChamp.style.background = color;
		return true;
	}
}

// Vérifie si le champ est un mail
function check_mail(myMail) 
{
	if (myMail.value.length != 0)
	{
		var reg = new RegExp("^[a-zA-Z0-9\-_]+[a-zA-Z0-9\.\-_]*@[a-zA-Z0-9\-_]+\.[a-zA-Z\.\-_]{1,}[a-zA-Z\-_]+", "g");		
		if (reg.test(myMail.value)) 
		{
    		return true;
    	} else {
       		alert("Mail invalide !");
			myMail.style.background = color_error;
			myMail.focus();
       		return false;
		}
	}
	else
	{
		myMail.style.background = color;
		return true;
	}
}

// Vérifie le numéro de tel
function check_tel(myTel, champ_name)
{
	if (myTel.value.length != 0)
	{
		var reg = new RegExp("^[0-9]{2}?[0-9]{2}?[0-9]{2}?[0-9]{2}?[0-9]{2}?$");		
		if (reg.test(myTel.value)) 
		{
    		return true;
    	} else {
       		myTel.style.background = color_error;
			myTel.focus();
			alert("Le numéro de "+ champ_name+" est invalide ! il ne doit comporter que des chiffres et aucun espace");
       		return false;
		}
	}
	else
	{
		myTel.style.background = color;
		return true;
	}
}

// Vérifie si le champ est un nombre
function check_number(myNum)
{
	if (!isNaN(myNum.value))
	{	
		myNum.style.background = color;
		return 1;
	}
	else
	{
		myNum.style.background = color_error;
		alert(myNum.value+" n'est pas un nombre");
		myNum.focus();
		return 0;
		
	}
}



// Vérifie si le champ est monnaitaire
function check_monnaie(myNum, champ_name)
{
	if (check_number(myNum))
	{
		myNum.style.background = color;	
		return true;
	}
	else
	{	
		myNum.style.background = color_error;	
		//alert("Prix "+myNum.value+" invalide");
		return false;
	}
}

// Vérifie si le champ est un nombre et s'il est compris entre
// les valeurs start et end
function check_cp(myChamp, start, end, champ_name)
{
	if (check_number(myChamp))
	{
		if (myChamp.value < start || myChamp.value > end)
		{
			myChamp.style.background = color_error;
			myChamp.focus();
			alert(myChamp.value+" n'est pas compris dans l'intervalle "+start+" "+end+" des codes postaux enregistrés");
    	   	return false;
		}
		if (myChamp.value >= start && myChamp.value <= end)
		{
			myChamp.style.background = color;
    	   	return true;
		}
		
	}
	else
	{
		myChamp.focus();
		myChamp.style.background = color_error;
       	return false;
	}
	
	return true;
}

//repere si un element de lsite déroulante est sélectionné
function check_liste(Mylist, champ_name)
{
	if(Mylist.selectedIndex==0)
		{
			alert("Choisir une valeur dans la liste " + champ_name);
			Mylist.focus();
			//Mylist.style.background = color_error;	
			return false;
		}
		else
		{
			Mylist.style.background = color;	
			return true;
		}
}


function compter(max_char,champ_degressif,textarea) 
{
	var txt=textarea.value;
	var nb=txt.length;
	if (nb>max_char) 
	{ 
		alert("Pas plus de "+max_char+" caractères dans ce champ");
		textarea.value=txt.substring(0,max_char);
		nb=max_char;
	}
		champ_degressif.value=max_char-nb;
}



//Vérification du formulaire d'inscription
function check_form_inscr(form)
{
	if(!check_length(form.prenom,"prenom")) return false;
	if(!check_length(form.nom,"nom")) return false;

	if(!check_length(form.dob,"date de naissance")) return false;
	if(!check_date(form.dob, "date de naissance")) return false;
	
	if(!check_mail(form.email)) return false;
	if(!check_length(form.email,"e-mail")) return false;
	
	if(!check_length(form.adresse,"adresse")) return false;
	if(!check_length(form.cp,"code postal")) return false;
	if(!check_length(form.ville,"ville")) return false;
	if(!check_liste(form.pays, "Pays")) return false;
	
	if(!check_length(form.password,"mot de passe")) return false;
	if(!check_length(form.password2,"confirmation du mot de passe")) return false;
	if(form.password.value != form.password2.value)
	{
		alert("Le mot de passe et sa confirmation sont différents");
		form.password.style.background = color_error;
		form.password2.style.background = color_error;
		form.password.focus();
		return false;
	}
}


function check_form_modif_compte(form)
{
	if(!check_length(form.nom, "nom")) return false;
	if(!check_length(form.prenom,"prenom")) return false;
	if(!check_length(form.datenaissance,"datenaissance")) return false;	
	if(!check_date(form.datenaissance, "datenaissance")) return false;
	if(!check_length(form.email,"email")) return false;	
	if(!check_length(form.adresse,"adresse")) return false;	
	if(!check_length(form.codepostal,"codepostal")) return false;	
	if(!check_length(form.ville,"ville")) return false;
	
	if(form.password.value.length!=0 || form.password2.value.length!=0 )
	{ 
	
	
		if(form.password.value.length < 5 || form.password2.value.length < 5)
		{
			alert("Le passe doit faire 5 caractères minimum ! ");
		}
		
	}
	
}

//Admin catalogue
function check_form_AjoutPlante(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.soustitre,"sous-titre")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.type,"type")) return false;	
	if(!check_length(form.famille,"famille")) return false;
	if(!check_length(form.description,"description")) return false;
	if(!check_length(form.soleil,"soleil")) return false;
	if(!check_length(form.temperature,"temperature")) return false;
	if(!check_length(form.arrosage,"arrosage")) return false;

}

function check_form_ModifPlante(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.soustitre,"sous-titre")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.type,"type")) return false;	
	if(!check_length(form.famille,"famille")) return false;
	if(!check_length(form.description,"description")) return false;
	if(!check_length(form.soleil,"soleil")) return false;
	if(!check_length(form.temperature,"temperature")) return false;
	if(!check_length(form.arrosage,"arrosage")) return false;	
}


function check_form_ajout_design(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.soustitre1,"sous-titre 1")) return false;
	if(!check_length(form.soustitre2,"sous-titre 2")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}



function check_form_ajout_graines(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;	
	if(!check_length(form.soustitre1,"sous-titre 1")) return false;
	if(!check_length(form.soustitre2,"sous-titre 2")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}

function check_form_ajout_subengrais(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.soustitre1,"sous-titre 1")) return false;
	if(!check_length(form.soustitre2,"sous-titre 2")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}
function check_form_ajout_ustensiles(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.soustitre1,"sous-titre 1")) return false;
	if(!check_length(form.soustitre2,"sous-titre 2")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}



function check_form_ajout_pots(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.soustitre1,"sous-titre 1")) return false;
	if(!check_length(form.soustitre2,"sous-titre 2")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}



function check_form_ajout_livres(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.auteur,"auteur")) return false;
	if(!check_length(form.edition,"edition")) return false;
	if(!check_length(form.divers,"divers")) return false;
	if(!check_length(form.reference,"réference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}


function check_form_ajout_options(form)
{
	
	if(!check_length(form.intitule,"intitulé")) return false;
	if(!check_length(form.soustitre1,"sous-titre 1")) return false;
	if(!check_length(form.soustitre2,"sous-titre 2")) return false;
	if(!check_length(form.soustitre3,"sous-titre 3")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}

function check_form_ajout_carte(form)
{
	if(!check_length(form.intitule,"intitulé")) return false;	
	if(!check_length(form.soustitre1,"sous-titre 1")) return false;
	if(!check_length(form.soustitre2,"sous-titre 2")) return false;
	if(!check_length(form.reference,"reference")) return false;
	if(!check_length(form.stock,"stock")) return false;
	if(!check_number(form.stock, "stock")) return false;
	if(!check_length(form.poids, "poids")) return false;
	if(!check_number(form.poids, "poids")) return false;
	if(!check_length(form.description,"description")) return false;
}

function decochelivraison(form)
{
	if(form.oklivraison.checked==true)
	{
		form.nomlivraison.disabled="";
		form.prenomlivraison.disabled="";
		form.adresselivraison.disabled="";
		form.cptadresselivraison.disabled="";
		form.cplivraison.disabled="";
		form.villelivraison.disabled="";
		form.payslivraison.disabled="";
	}
	else
	{
		form.nomlivraison.disabled="disabled";
		form.prenomlivraison.disabled="disabled";
		form.adresselivraison.disabled="disabled";
		form.cptadresselivraison.disabled="disabled";
		form.cplivraison.disabled="disabled";
		form.villelivraison.disabled="disabled";
		form.payslivraison.disabled="disabled";
		
		form.nomlivraison.value="";
		form.prenomlivraison.value="";
		form.adresselivraison.value="";
		form.cptadresselivraison.value="";
		form.cplivraison.value="";
		form.villelivraison.value="";
	}
}

function check_form_livraison(form)
{
	if(form.oklivraison.checked==true)
	{
		if(!check_length(form.nomlivraison,"Nom")) return false;	
		if(!check_length(form.prenomlivraison,"Prenom")) return false;
		if(!check_length(form.adresselivraison,"Adresse")) return false;
		if(!check_length(form.cplivraison,"code postal")) return false;
		if(!check_length(form.villelivraison,"ville")) return false;
	}
}