function isANumber(number) {
	answer = 1;
	if (!parseFloat(number))
	{ 
		answer = 0;
	}
	else {
		for (var i=0; i<number.length; i++) {
			if ((number.charAt(i) != "0") && (!parseFloat(number.charAt(i)))) {
				answer = 0;				
				break; 
			} 
		} 
	}
	return answer; 
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



//********** teste la validite de la date *****************************************/ 
function testJour()
{
var jourmaxfevrier;
if (document.inscription.annee.value%4!=0)
	jourmaxfevrier=28;
else
	jourmaxfevrier=29;

var tab=new Array(30,jourmaxfevrier,31,30,31,30,31,30,31,30,31,30);
var mois=document.inscription.mois.value;

if (document.inscription.jour.value > tab[mois-1])
	{
	alert("Date incorrecte");
	document.inscription.jour.value=tab[mois-1];
	}

}


//****************met la chaine en majuscule***************************************/
function maj(chaine){
chaine.value=(chaine.value).toUpperCase();
}

//****************met la chaine en minuscule sauf la premiere lettre***************************************/
function minus(chaine){
var c=(chaine.value).charAt(0);
c=c.toUpperCase();
var s=(chaine.value).substring(1,chaine.value.length);
s=s.toLowerCase();
chaine.value=c+s;
}


//********************** test email ********************************/
function testEmail(email)
{
var emailPartie2;
var posPoint;
var emailPartie3;
var posArobas=email.indexOf('@');

if (posArobas!=-1)
	{
	if (email.charAt(0)!='@')
		{
		emailPartie2=email.substring(posArobas+1,email.length);
		posPoint=emailPartie2.indexOf('.');
		if (posPoint!=-1)	
			{
			if (emailPartie2.charAt(0)!='.')	
				{
				emailPartie3=emailPartie2.slice(posPoint+1,email.length);
				if (emailPartie3.length>=2)
					return true;
	
				}
			}
		}
	}
return false;
}

function validateForm(){
	var chp_nom = trim(document.inscription.nom.value); 
	var taille= chp_nom.length;

	if (taille <2 || chp_nom == "")
		{
		alert("Veuillez remplir le champ Nom !");
		document.inscription.nom.value = "";
		document.inscription.nom.focus();
		return false;
		}
	
	var chp_prenom = trim(document.inscription.prenom.value);
	taille = chp_prenom.length;
	
	if (taille <2 || chp_prenom == "")
		{
		alert("Veuillez remplir le champ Prénom !");
		document.inscription.prenom.value = "";
		document.inscription.prenom.focus();
		return false;
		}
	
	if (document.inscription.jour.selectedIndex=="0" ||document.inscription.mois.selectedIndex=="0" || document.inscription.annee.value=="")
	{
		alert("Date de naissance incorrecte !");
		document.inscription.jour.focus();
		return false;
	}
		
	if (document.inscription.annee.value > 2000 || document.inscription.annee.value <1900)
	{
		alert("Année de naissance incorrecte !");
		document.inscription.annee.focus();
		return false;
	}
	if (document.inscription.annee.value=="")
	{
		alert("Veuillez remplir le champ Année de naissance !");
		document.inscription.annee.focus();
		return false;
	}
	
	if((! parseInt(document.inscription.annee.value)) || document.inscription.annee.value.length != 4
				   || isNaN(document.inscription.annee.value) || (! isANumber(document.inscription.annee.value)))
	{
		alert("Année de naissance incorrecte !");
		document.inscription.annee.focus();
		return false;
	}

	var chp_adr = trim(document.inscription.adresse.value);
	taille = chp_adr.length;
	
	if (taille < 10 || chp_adr =="")
	{
		alert("Veuillez remplir le champ Adresse !");
		document.inscription.adresse.value = "";
		document.inscription.adresse.focus();
		return false;
	}
	
	if (document.inscription.code_postal.value=="")
	{
		alert("Veuillez remplir le champ Code postal !");
		document.inscription.code_postal.focus();
		return false;
	}
	
	if(document.inscription.code_postal.value.length != 5  )
	{
		alert("Code postal incorrect !");
		document.inscription.code_postal.focus();
		return false;
	}
	
	var chp_ville = trim(document.inscription.ville.value);
	taille = chp_ville.length;
	
	if (taille < 2 || chp_ville.value=="")
	{
		alert("Veuillez remplir le champ Ville !");
		document.inscription.ville.value = "";
		document.inscription.ville.focus();
		return false;
	}
	
	if (document.inscription.tel.value=="")
	{
		alert("Veuillez remplir le champ Téléphone !");
		document.inscription.tel.focus();
		return false;
	}
	
	if((! parseInt(document.inscription.tel.value)) || document.inscription.tel.value.length != 10
				   || isNaN(document.inscription.tel.value) || (! isANumber(document.inscription.tel.value)) )
	{
		alert("Numéro de téléphone incorrect !");
		document.inscription.tel.focus();
		return false;
	}

	if (!testEmail(document.inscription.email.value))
	{
		alert("E-mail incorrect !");
		document.inscription.email.focus();
		return false;
	}
}

function validateNewsletterForm(inscription){
	if (!testEmail(document.newsletter.mail.value))
	{
		alert("E-mail incorrect !");
		document.newsletter.email.focus();
		return false;
	}
	
	if(inscription == 1)
		document.newsletter.action = '?action=1';
	else
		document.newsletter.action = '?action=0';
		
	document.newsletter.submit();
}