// FONCTIONS DE TEST DE FORMULAIRE //

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

//centrage des popup
function ouvre(fichier,w,h) {
  ff=window.open(fichier,"pop","width="+w+",height="+h
	                 +",left=10,top=10,scrollbars=no") ;
  titre="Galerie";
  if (ff) {
  ff.document.open(); 
  ff.document.write('<head><title>' + titre + '<\/title><\/head>');
  ff.document.write('<body background=" '+ fichier 
                               + ' " onBlur="top.close()"><\/body>');
  ff.document.close();
         }
  }
// Enlève les espaces en début et fin de chaine
function trim(myString)
{
	return myString.replace(/(^\s*)|(\s*$)/g, "");
}

// Confirmation de la validation d'un formulaire (suppression d'un élément)

function confirm_send_mail(champ_activation, champ_mail,champ_cache)
{
	if(champ_mail.value.length != 0 & champ_activation.checked==true )
	{
		if (confirm("Voulez-vous envoyer un e-mail confirmant l'activation ?")) 
		{
			//return true;
			champ_cache.value=1;
			return true;
		}
		else
		{
			//return false;
			champ_cache.value=0;
			return true;
		}
	}
}

// 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 check_form_agenda(form)
{
	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.date_debut,"date début")) return false;
	if(!check_date(form.date_debut, "date début")) return false;	
	
	if(form.date_fin.value.length!=0)
	{
		if(!check_date(form.date_fin, "date fin")) return false;
		if(!check_date_inf(form.date_debut, form.date_fin)) return false;	
	}
	if(form.formdescription.value.length==0)
	{
			alert("Vous devez rentrer une description");
			return false;
	}
	
}

function check_form_alaune(form)
{
	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.date_moment,"date")) return false;
	if(!check_length(form.desc_rap,"description rapide")) return false;
	if(!check_date(form.date_moment, "date")) return false;	
	
	if(form.en_ce_moment.value.length==0)
	{
			alert("Vous devez rentrer une description");
			return false;
	}
	
}

function check_form_commerce(form)
{
	if(!check_length(form.nom,"nom")) return false;
	if(!check_length(form.prenom,"prénom")) return false;
	if(!check_length(form.raison,"raison sociale")) return false;
	//if(!check_length(form.type,"activité")) return false;
	if(!check_length(form.tel,"téléphone")) return false;
	if(!check_tel(form.tel,"téléphone")) return false;
	
	if(form.fax.value.length!=0)
	{
		if(!check_length(form.fax,"fax")) return false;
		if(!check_tel(form.fax,"fax")) return false;
	}
	
	if(form.portable.value.length!=0)
	{
		if(!check_length(form.portable,"portable")) return false;
		if(!check_tel(form.portable,"portable")) return false;
	}
	
	if(form.mail.value.length!=0)
	{
		if(!check_mail(form.mail)) return false;
	}
	
	if(form.adresse.value.length==0)
	{
			alert("Vous devez rentrer une adresse");
			return false;
	}
	
}

function check_form_conseil(form)
{
	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.date_moment,"date")) return false;
	if(!check_date(form.date_moment, "date")) return false;
	if(!check_length(form.fichier,"fichier .pdf ou .doc")) return false;
	
}

function check_form_etatcivil(form)
{
	if(form.contenuetatcivil.value.length==0)
	{
			alert("Vous devez rentrer une description");
			return false;
	}
}

function check_form_geo(form)
{
	if(form.contenugeo.value.length==0)
	{
			alert("Vous devez rentrer une description");
			return false;
	}
}

function check_form_histoire(form)
{
	if(form.contenuhsitoire.value.length==0)
	{
			alert("Vous devez rentrer une description");
			return false;
	}
}

function check_form_infoslegales(form)
{
	if(form.description.value.length==0)
	{
			alert("Vous devez rentrer une description");
			return false;
	}
}

function check_form_liens(form)
{
	if(!check_length(form.newurl,"adresse")) return false;
	if(!check_length(form.newintitule,"intitulé")) return false;
}

function check_form_loisirs(form)
{
	if(!check_length(form.nom,"nom")) return false;
	if(!check_length(form.prenom,"prénom")) return false;
	if(!check_length(form.raison,"raison sociale")) return false;
	//if(!check_length(form.type,"activité")) return false;
	if(!check_length(form.tel,"téléphone")) return false;
	if(!check_tel(form.tel,"téléphone")) return false;
	
	if(form.fax.value.length!=0)
	{
		if(!check_length(form.fax,"fax")) return false;
		if(!check_tel(form.fax,"fax")) return false;
	}
	
	if(form.portable.value.length!=0)
	{
		if(!check_length(form.portable,"portable")) return false;
		if(!check_tel(form.portable,"portable")) return false;
	}
	
	if(form.mail.value.length!=0)
	{
		if(!check_mail(form.mail)) return false;
	}
	
	if(form.adresse.value.length==0)
	{
			alert("Vous devez rentrer une adresse");
			return false;
	}
	
}

function check_form_marches(form)
{
	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.date_moment,"date")) return false;
	if(!check_date(form.date_moment, "date")) return false;	
	if(!check_length(form.fichier,"fichier .pdf ou .doc")) return false;
	
}

function check_form_new_galerie(form)
{
	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.date_moment,"date")) return false;
	if(!check_date(form.date_moment, "date")) return false;
	if(!check_length(form.description,"description")) return false;
	
}

function check_form_services(form)
{
	if(!check_length(form.titre,"intitulé")) return false;
	if(!check_length(form.responsable,"responsable")) return false;

	if(!check_length(form.tel,"téléphone")) return false;
	if(!check_tel(form.tel,"téléphone")) return false;
		
	if(form.fax.value.length!=0)
	{
		if(!check_length(form.fax,"fax")) return false;
		if(!check_tel(form.fax,"fax")) return false;
	}
	
	if(form.adresse.value.length==0)
	{
			alert("Vous devez rentrer une adresse");
			return false;
	}	
}

function check_form_travaux(form)
{
	if(!check_length(form.titre,"titre")) return false;
	if(!check_length(form.date_moment,"date")) return false;
	if(!check_date(form.date_moment, "date")) return false;	
	if(!check_length(form.fichier,"fichier .pdf ou .doc")) return false;
	
}

function check_form_mail(form)
{
	if(!check_length(form.mail,"e-mail")) return false;
	if(!check_mail(form.mail)) return false;
	//var txt=form.description.value;
	//var nb=txt.length;
	/*if(nb<30)
	{
		alert("La description doit faire au moins 30 caractères");	
		//form.description.focus();
		return false;	
	}*/
	//if(!check_length(form.email,"e-mail")) return false;			
	//if(!check_mail(form.email)) return false;

}

function check_form_xlagenda_culturel(form)
{
	if(!check_length(form.date_debut,"date début")) return false;
	if(!check_date(form.date_debut, "date début")) return false;	
	
	if(form.date_fin.value.length!=0)
	{
		if(!check_date(form.date_fin, "date fin")) return false;
		if(!check_date_inf(form.date_debut, form.date_fin)) return false;	
	}	
	
	if(!check_length(form.nom,"nom")) return false;
	
	if(form.description.value.length==0)
	{
		alert("Vous devez rentrer une description");
		return false;
	}
	
	//if(!check_liste(categorie, "catégorie")) return false;
	
	if(form.categorie.selectedIndex==0)
	{
		alert ("Veuillez sélectionner une catégorie dans la liste");
		//form.categorie.focus();
		return false;
	}
	if(form.email.value.length!=0)
	{
		if(!check_mail(form.email)) return false;
	}
	if(form.telephone.value.length!=0)
	{
		if(!check_tel(form.telephone,"téléphone")) return false;
	}
	
	if(form.fax.value.length!=0)
	{
		if(!check_tel(form.fax,"fax")) return false;
	}
	
}

function popup_upload(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../includes/upload.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_upload_agenda(form, remote_path, db_path, mask, size_max, width_max, height_max)
{	
	chemin="../../includes/upload.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");	
}


// Sélection de ville
function popupmessage(id_exp, id_dest)
{
		chemin="popupmess.php?id_exp="+id_exp+"&id_dest="+id_dest;
		window.open(chemin,'',"target=_self, toolbar=no, location=no, directories=no, status=yes, scrollbars=no, resizable=no, copyhistory=no, width=400, height=250, left=300, top=50");
}

function popupreadmess(id_mess)
{
		chemin="popupreadmess.php?id_mess="+id_mess;
		window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=no, resizable=no, copyhistory=no, width=400, height=250, left=300, top=50");
}
function popupprofil(id_exp)
{
		chemin="popupprofil.php?id_exp="+id_exp;
		window.open(chemin,'',"location=no, directories=no, status=yes, scrollbars=no, resizable=no, copyhistory=no, width=400, height=250, left=300, top=50");
}
/*function popupville(cp,ville,id_ville)
{
	chemin="includes/popupville.php?cp="+cp+"&ville="+ville+"&id_ville="+id_ville;
	window.open(chemin,'',"toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, resizable=no, copyhistory=no, width=300, height=400, left=300, top=50");
}*/

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