// File: EmailCheck.js
//
// sample of use:
//
// <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" src="EmailCheck.js"></SCRIPT>
//
// <form name="EmailForm" method=POST onsubmit="return IsEmailObjectValid(this.email,False);" action="EmailCheck.php?email=">
//   <INPUT type="text" name="email" value="email" onFocus="this.select()"><br>
//   <input type=submit value="Envoyer">
// </form>
//


function IsEmailObjectValid(emailObject, emptyEnabled,verbose)
{
// pb avec junk@..com
var str_answerInvalid="Email invalide !";
var res = true;
var checkThisEmail = emailObject.value;
var myAtSymbolAt = checkThisEmail.indexOf('@');
var myLastDotAt = checkThisEmail.lastIndexOf('.');
var mySpaceAt = checkThisEmail.indexOf(' ');
var myLength = checkThisEmail.length;

  if (checkThisEmail == "") {
    if ( emptyEnabled ) {
      return true;
    } else {
      alert("Saisir une adresse email !");
      emailObject.focus();
      return false;
    }
  }

// at least one @ must be present and not before position 2
// @yellow.com : NOT valid
// x@yellow.com : VALID

if ( res && (myAtSymbolAt < 1 ) )
 {res = false; str_answerInvalid="Email invalide : il faut un @ dans l'email !"}

// more than one @ : INVALID
if ( res && (myAtSymbolAt < checkThisEmail.lastIndexOf('@') )) 
 {res = false; str_answerInvalid="Email invalide : trop de @ dans l'email !";}

// at least one . (dot) afer the @ is required
// x@yellow : NOT valid
// x.y@yellow : NOT valid
// x@yellow.org : VALID

if ( res && (myLastDotAt <= myAtSymbolAt + 1) )
 {res = false; str_answerInvalid="Email invalide : il manque dom.com ou dom.fr ou ...";}

// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
// x.y@yellow. : NOT valid
// x.y@yellow.a : NOT valid
// x.y@yellow.ca : VALID

if ( res && (myLength - myLastDotAt <= 2) )
 {res = false; str_answerInvalid="Email invalide : terminaison incorrecte (.com .fr etc) !";}

if ( res && (myLength - myLastDotAt > 4) )
 {res = false; str_answerInvalid="Email invalide : terminaison incorrecte (.com .fr etc) !";}


// no empty space " " is permitted (one may trim the email)
// x.y@yell ow.com : NOT valid

if ( res && (mySpaceAt != -1) )
 {res = false; str_answerInvalid="Email invalide : il ne faut pas de caractère blanc dans l'email !";}


if (res == true) {
//  alert("email is VALID");
} else {
  if (verbose) { alert(str_answerInvalid); }
  emailObject.focus();
}

return res;
}

function CheckEmail(emailObject,emptyEmail,verbose) {
// emptyEmail :
// 0 = not allowed
// 1 = NEANT allowed
// 2 = <empty> allowed
// 3 = <empty>or NEANT allowed
  var email=emailObject.value;

 if ( (email == "" ) || (email == "votre email")) {
  if ( emptyEmail == 0 ) {
	if (verbose) { self.alert("Merci de saisir votre email"); }
	emailObject.focus();
	return false;
  }
  if ( emptyEmail == 1 ) {
	if (verbose) { self.alert("Merci d'indiquer votre email !\nSi vous n'en disposez pas, indiquez NEANT."); }
	emailObject.focus();
	return false;
  }
  return true;
 }
  
 if ( (emptyEmail==1) || (emptyEmail==3) ){
   if ( email == "NEANT" ) { return true; }
   if ( email == "neant" ) { return true; }
   if ( email == "néant" ) { return true; }
 }

  return IsEmailObjectValid(emailObject, false, verbose);
}

function CheckEmailObjectValidOrEmpty(emailObject)
{
  if ( emailObject.value == "") {
    if ( confirm("Soumettre sans email ?") ) {
      return true;
    } else { return talse; }
  }
  return IsEmailObjectValid(emailObject,false,true);
}

