// *************************************************************
// Controle de l'ensemble des saisies effectuees
// *************************************************************
			
function ctl_saisie() {
	// Verification du nom 
	if (jsTrim(window.document.forms[0].nom.value) == "") {
		alert("Vous devez saisir votre nom.")
		window.document.forms[0].nom.focus()
		return false
	}
	
	// Verification du prénom
	if (jsTrim(window.document.forms[0].prenom.value) == "") {
		alert("Vous devez saisir votre prénom.")
		window.document.forms[0].prenom.focus()
		return false
	}
	
	// Verification du mail
	if (jsTrim(window.document.forms[0].email.value) == "") {
		alert("Vous devez saisir votre adresse E-mail.")
		window.document.forms[0].email.focus()
		return false
	}
	if ( ! isEmail(jsTrim(window.document.forms[0].email.value))) {
		alert("Votre adresse E-mail est invalide.")
		window.document.forms[0].email.focus()
		return false
	}
	
	// Verification des numero de tel et de fax
	if (jsTrim(window.document.forms[0].telephone.value) != "") {
		if ( VerifieNumero(window.document.forms[0].telephone.value) == false ) {
			alert("Vous devez saisir un numero de téléphone valide.")
			window.document.forms[0].telephone.focus()
			return false
		}
		tel = jsTrim(window.document.forms[0].telephone.value)
		if (tel.length < 10 ) {
			alert("Vous devez saisir un numero de téléphone valide.")
		    window.document.forms[0].telephone.focus()
			return false
		}
	}
	if (jsTrim(window.document.forms[0].fax.value) != "") {
		if ( VerifieNumero(window.document.forms[0].fax.value) == false ) {
			alert("Vous devez saisir un numero de fax valide.")
			window.document.forms[0].fax.focus()
			return false
		}
		fax = jsTrim(window.document.forms[0].fax.value)
		if (fax.length < 10 ) {
			alert("Vous devez saisir un numero de fax valide.")
		    window.document.forms[0].fax.focus()
			return false
		}
	}	
	CreerMail()
	return true
}

// *************************************************************
// Creation du mail de contact
// *************************************************************

function CreerMail() {
	window.document.form1.TheFrom.value=window.document.form1.email.value
	window.document.form1.TheSubject.value= "Contact"
	var LeBody = ""
	
	if (jsTrim(window.document.form1.nom.value) != "") {
		LeBody = " Nom : " + window.document.form1.nom.value + "\n"
	}
 
	if (jsTrim(window.document.form1.prenom.value) != "") {
		LeBody =LeBody + " Prénom : " + window.document.form1.prenom.value + "\n"
	}
	
	if (jsTrim(window.document.form1.adresse.value) != "") {
		LeBody =LeBody + " Adresse postale : " + window.document.form1.adresse.value + "\n"
	}
	
	if (jsTrim(window.document.form1.email.value) != "") {
		LeBody =LeBody + " E-mail : " + window.document.form1.email.value + "\n"
	}
	
	if (jsTrim(window.document.form1.telephone.value) != "") {
		LeBody =LeBody + " Téléphone : " + window.document.form1.telephone.value + "\n"
	}
	
	if (jsTrim(window.document.form1.fax.value) != "") {
		LeBody =LeBody + " Fax : " + window.document.form1.fax.value + "\n"
	}
	
	if (jsTrim(window.document.form1.message.value) != "") {
		LeBody =LeBody + " Message : " + window.document.form1.message.value
	}

	window.document.form1.TheBody.value = LeBody
	window.document.form1.action = "mail.asp";
	//window.document.form1.submit();
}

// *************************************************************
// Fonction verifiant si la chaine passe en paramatre est un entier
// *************************************************************

function VerifieNumero(Numaverifie) {
	lenumero = jsTrim(Numaverifie) 
	numok= true
	for (i=0; i<lenumero.length;i++) {
		LeChiffre = parseInt(lenumero.charAt(i))
		if (LeChiffre < 10)
			{}
		else {
			numok = false
		}
	}
	if (numok == false) {
		return false
	}
	return true
}

// *************************************************************
// Fonction otant les espaces autour d'une chaine de caracteres
// *************************************************************

function jsTrim(inChaine) {
	var idxDebut = 0 ;
	var idxFin = inChaine.length ;

	while ((idxDebut < idxFin) && (inChaine.charAt(idxDebut) == ' '))
		idxDebut ++ ;

	if (idxDebut >= idxFin)
		return "" ;
	do
		idxFin -- ;
	while ((idxFin > idxDebut) && (inChaine.charAt(idxFin) == ' ')) ;

	return inChaine.substr(idxDebut, idxFin - idxDebut + 1) ;
}

// *************************************************************
// Verifie qu'une chaine represente syntaxiquement un e-mail
// *************************************************************

function isEmail(chaine)  {
	var tChaine
	tChaine = jsTrim(chaine) ;

	var i = 1;
	var sLength = tChaine.length;

	if (sLength == 0) return false ;

	while ((i < sLength) && (tChaine.charAt(i) != "@"))
	{ i++
	}
	if ((i >= sLength) || (tChaine.charAt(i) != "@"))
		return false ;
	else
		i += 2 ;

	while ((i < sLength) && (tChaine.charAt(i) != "."))
	{ i++
	}
	if ((i >= sLength - 1) || (tChaine.charAt(i) != "."))
		return false;
	else
		return true;
}
