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


function popup_upload1(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../includes/upload1.php?form="+form+"&remote_path="+remote_path+"&db_path="+db_path+"&mask="+mask+"&size_max="+size_max+"&width_max="+width_max+"&height_max="+height_max;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=150, left=300, top=200");	
}

function popup_upload2(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../includes/upload2.php?form="+form+"&remote_path="+remote_path+"&db_path="+db_path+"&mask="+mask+"&size_max="+size_max+"&width_max="+width_max+"&height_max="+height_max;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=150, left=300, top=200");	
}

function popup_upload3(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../includes/upload3.php?form="+form+"&remote_path="+remote_path+"&db_path="+db_path+"&mask="+mask+"&size_max="+size_max+"&width_max="+width_max+"&height_max="+height_max;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=150, left=300, top=200");	
}


function popup_uploadnews1(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../includes/uploadnews1.php?form="+form+"&remote_path="+remote_path+"&db_path="+db_path+"&mask="+mask+"&size_max="+size_max+"&width_max="+width_max+"&height_max="+height_max;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=150, left=300, top=200");	
}

function popup_uploadnews2(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../includes/uploadnews2.php?form="+form+"&remote_path="+remote_path+"&db_path="+db_path+"&mask="+mask+"&size_max="+size_max+"&width_max="+width_max+"&height_max="+height_max;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=150, left=300, top=200");	
}

function popup_uploadnews3(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../includes/uploadnews3.php?form="+form+"&remote_path="+remote_path+"&db_path="+db_path+"&mask="+mask+"&size_max="+size_max+"&width_max="+width_max+"&height_max="+height_max;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=150, left=300, top=200");	
}


function popup_uploadpdf(form, remote_path, db_path, mask)
{	
	chemin="../includes/uploadpdf.php?form="+form+"&remote_path="+remote_path+"&db_path="+db_path+"&mask="+mask;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=600, height=150, left=300, top=200");	
}

//Vérifie le formulaire d'ajout dactu
function check_form_ajout_actu(form)
{

	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.contenu,"contenu")) return false;	
	
	//si le champ url est rempli, il faut obligatoirement un texte 
	if(form.lien.value.length!=0)
	{
		if(!check_length(form.texte_lien,"texte lien")) return false;
	}
	//Si le champ texte est rempli, il faut forcement une url	
	if(form.texte_lien.value.length!=0)
	{
		if(!check_length(form.lien,"lien")) return false;
	}
	
	if(!check_length(form.date,"date")) return false;
	if(!check_date(form.date, "date")) return false;		
	if(form.fichier_server1.value=="")
	{
		alert("Attention ! Vous n'avez pas choisi d'image par défaut pour ce produit !");
		return false;	
	}	
}

//Vérifie le formulaire de modification de actu
function check_form_modif_actu(form)
{
	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.contenu,"contenu")) return false;	
	
	//si le champ url est rempli, il faut obligatoirement un texte 
	if(form.lien.value.length!=0)
	{
		if(!check_length(form.texte_lien,"texte lien")) return false;
	}
	//Si le champ texte est rempli, il faut forcement une url	
	if(form.texte_lien.value.length!=0)
	{
		if(!check_length(form.lien,"lien")) return false;
	}
	
	if(!check_length(form.date,"date")) return false;
	if(!check_date(form.date, "date")) return false;
	
	//si on a coché la case pour supprimer mais qu'on a pas remplacé l'image, message alerte. Chaque produit doit avori OBLIGATOIREMENT UNE IMAGE
	if(form.suppr_image_actu1.checked==true && form.fichier_server1.value=="")
	{
		alert("Vous n'avez pas choisi de nouvelle image par défaut ! Si c'est une erreur, décochez la case !");
		return false;
	}
}

function check_form_modif_singleform(form)
{
	//if(!check_length(form.contenu,"contenu")) return false;
}

//Vérifie le formulaire de modification de produitnouveaute
function check_form_modif_pubs(form)
{
				
	//si on a coché la case pour supprimer mais qu'on a pas remplacé l'image, message alerte. Chaque produit doit avori OBLIGATOIREMENT UNE IMAGE
	if(form.suppr_image_produit1.checked==true && form.fichier_server1.value=="")
	{
		alert("Vous n'avez pas choisi de nouvelle image par défaut ! Si c'est une erreur, décochez la case !");
		return false;
	}
	
	/*form.fichier.value = trim(form.fichier.value);
	if(!check_length(form.fichier,"fichier")) return false;*/
}
//vérifie le formulaire de créations de la newsletter. 3 liens + 3 images obligatoires
function check_form_create_newsletter(form)
{
	if(!check_length(form.fichier_server1,"image 1")) return false;
	if(!check_length(form.lien1,"lien image 1")) return false;
	if(!check_length(form.fichier_server2,"image 2")) return false;
	if(!check_length(form.lien2,"lien image 2")) return false;
	if(!check_length(form.fichier_server3,"image 3")) return false;
	if(!check_length(form.lien3,"lien image 3")) return false;
}

//vérifie la chaine mail pour l'inscription newsletter
function check_form_mail(form)
{
	if(!check_length(form.mail,"e-mail")) return false;
	if(!check_mail(form.mail)) return false;
}

//vérifie les champs du formulaire de contact coté client
function check_form_contact(form)
{
	if(!check_length(form.nom,"nom")) return false;
	if(!check_length(form.prenom,"prénom")) return false;
	if(!check_length(form.adresse, "adresse")) return false;	
	if(!check_length(form.tel,"téléphone")) return false;
	if(!check_length(form.mail, "e-mail")) return false;	
	if(!check_mail(form.mail)) return false;
	//if(!check_length(form.motif,"motif")) return false;
	if(!check_length(form.contenu_message, "message")) return false;	
	if(!check_length(form.code, "code")) return false;
}

//vérification du formaulire de commande infos clients
function check_form_infos_clients(form)
{
	if(!check_length(form.mail,"e-mail")) return false;
	if(!check_mail(form.mail)) return false;
	
	if(!check_length(form.conf_mail,"e-mail de confirmation")) return false;
	if(!check_mail(form.conf_mail)) return false;
	
	if(form.mail.value != form.conf_mail.value)
	{
		alert("Merci de vérifier l'adresse mail et sa confirmation !");
		form.mail.style.background = color_error;
		form.conf_mail.style.background = color_error;
		form.mail.focus();
		return false;
		
	}
	
	if(!check_length(form.nom_client,"nom")) return false;
	if(!check_length(form.prenom,"prenom")) return false;
	if(!check_length(form.adresse_fact,"adresse de facturation")) return false;
	if(!check_length(form.cp_fact,"code postal de l'adresse de facturation")) return false;
	if(!check_number(form.cp_fact)) return false;
	if(!check_length(form.ville_fact,"ville de l'adresse de facturation")) return false;
	
	//au moins un numéro de tél doit être renseigné
	if(form.tel_fixe.value.length==0 && form.tel_gsm.value.length==0)
	{
		alert("Merci de préciser au moins 1 numéro de téléphone (fixe ou GSM) !");
		form.tel_fixe.style.background = color_error;
		form.tel_gsm.style.background = color_error;
		form.tel_fixe.focus();
		return false;
	}
	if(!check_number(form.tel_fixe)) return false;
	if(form.tel_gsm.value.length!=0)
	{
		if(!check_number(form.tel_gsm)) return false;
	}
	
	
	if(form.cgvok.checked!=true)
	{
		alert("Vous devez comprendre et accepter les CGV pour effectuer votre paiement !");
		return false;
	}
	
	//form.submit();
}

