
/*
 * checkForm.js
 * require prototype.js
 * call with "cf = new CheckForm(); if(cf.isValid) alert("ok"); else alert("nok");"
 */

var CheckForm = function(){
  this.isValid = 0;

  // binding
  this.regExp               = CheckForm_regExp;
  this.isValidMail          = CheckForm_isValidMail;
  this.isValidMailLaPoste   = CheckForm_isValidMailLaPoste;
  this.isValidPhoneNumber   = CheckForm_isValidPhoneNumber;
  this.isValidZipCode       = CheckForm_isValidZipCode;

  if($("thematique").value == "0"){
    this.isValid = 0;
    alert("Vous devez choisir un th\350me.");
    $("thematique").focus();
    return 0;

  }
  if($("mail1").value == ""){
    this.isValid = 0;
    alert("le champs adresse Laposte.net est obligatoire. (ex: john.doe@laposte.net)");
    $("mail1").focus();
    return 0;
  }

  if(this.isValidMailLaPoste($("mail1").value) == false){
    this.isValid = 0;
    alert("le champs adresse Laposte.net n'est pas valide. (ex: john.doe@laposte.net)");
    $("mail1").select();
    return 0;
  }

  if($("mail2").value != "")
    if(this.isValidMail($("mail2").value) == false){
      this.isValid =  0;
      alert("le champs adresse alternative n'est pas valide. (ex: john.doe@foo.bar)");
      $("mail2").select();
      return 0;
    }

  if($("cp").value != "")
    if(this.isValidZipCode($("cp").value) == false){
      this.isValid =  0;
      alert("le champs code postal n'est pas valide. (ex: 75001)");
      $("cp").select();
      return 0;
    }

  if($("telephone").value != "")
    if(this.isValidPhoneNumber($("telephone").value) == false){
      this.isValid =  0;
      alert("le champs t\351l\351phone n'est pas valide. (ex: 0132672245)");
      $("telephone").select();
      return 0;
    }

  if($("message").value == ""){
    this.isValid = 0;
    alert("le champs message est obligatoire.");
    $("message").focus();
    return 0;
  }

  this.isValid = 1;
  return 1;
}

var CheckForm_regExp = function(regexp, val){
  reg = new RegExp(regexp);
  return reg.test(val);
}

var CheckForm_isValidMailLaPoste = function(val){
  return this.regExp("^[a-zA-Z0-9._-]+@laposte.net", val);
}

var CheckForm_isValidMail = function(val){
  return this.regExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$", val);
}

var CheckForm_isValidPhoneNumber = function(val){
  return this.regExp("^[0-9]{10}$", val);
}

var CheckForm_isValidZipCode = function(val){
  return this.regExp("^[0-9]{5}$", val);
}


