/*
  -------------------------------------------------------------------------
	                    JavaScript Form Validator 
                                Version 2.0.2
	Copyright 2003 JavaScript-coder.com. All rights reserved.
	You use this script in your Web pages, provided these opening credit
    lines are kept intact.
	The Form validation script is distributed free from JavaScript-Coder.com

	You may please add a link to JavaScript-Coder.com, 
	making it easy for others to find this script.
	Checkout the Give a link and Get a link page:
	http://www.javascript-coder.com/links/how-to-link.php

    You may not reprint or redistribute this code without permission from 
    JavaScript-Coder.com.
	
	JavaScript Coder
	It precisely codes what you imagine!
	Grab your copy here:
		http://www.javascript-coder.com/
    -------------------------------------------------------------------------  
*/
function Validator(frmname,nErrorThrowType) {
  this.formobj=document.forms[frmname];
  if(!this.formobj) {
	 alert("BUG: l'objet Form " + frmname + " est introuvable !");
	 return;
  }
  this.formobj.nErrorThrowType    = nErrorThrowType;
  this.formobj.objFirstError      = null;
  if(this.formobj.onsubmit) {
    this.formobj.old_onsubmit     = this.formobj.onsubmit;
    this.formobj.onsubmit         = null;
  } else {
    this.formobj.old_onsubmit     = null;
  }
  this.formobj.onsubmit           = form_submit_handler;
  this.addValidation              = add_validation;
  this.setAddnlValidationFunction = set_addnl_vfunction;
  this.clearAllValidations        = clear_all_validations;
}

function set_addnl_vfunction(functionname) {
  this.formobj.addnlvalidation = functionname;
}

function clear_all_validations() {
  for(var itr=0;itr < this.formobj.elements.length;itr++) {
    this.formobj.elements[itr].validationset = null;
  }
}

function form_submit_handler() {
  var strError = "";
  var tmpError = "";

  for(var itr=0 ; itr < this.elements.length ; itr++) {
    if(this.elements[itr].validationset) {
      tmpError = this.elements[itr].validationset.validate();
      if (tmpError != '') {
        if (this.nErrorThrowType == 0) {
          strError = tmpError;
          alert(strError);
          this.elements[itr].focus();
          return false;
        } else {
          strError += ((strError != '') ? "\r\n":"") + tmpError;
          if (this.objFirstError == null) {
            this.objFirstError = this.elements[itr];
          }
        }
      }
    }
  }

  if(this.addnlvalidation) {
    str = " var ret = " + this.addnlvalidation + "()";
    eval(str);
    if (ret != '') {
      strError += ((strError != '') ? "\r\n":"") + ret;
    }
  }
  
  if(strError != '') {
    alert(strError);
    if(this.objFirstError != null) this.objFirstError.focus();
    return false;
  } else {
    if (this.old_onsubmit) {
      this.old_onsubmit();
    }
    return true;
  }
}
 
function add_validation(itemname,descriptor,errstr) {
  if(!this.formobj) {
    alert("BUG: l'objet Form n'est pas défini correctemment");
    return;
  }
  var itemobj = this.formobj[itemname];
  if(!itemobj) {
    alert("BUG: l'élément de formulaire nommé '" + itemname + "' est introuvable !");
    return;
  }
  if(!itemobj.validationset) {
    itemobj.validationset = new ValidationSet(itemobj);
  }
  itemobj.validationset.add(descriptor,errstr);
}

function ValidationDesc(inputitem,desc,error) {
  this.desc     = desc;
  this.error    = error;
  this.itemobj  = inputitem;
  this.validate = vdesc_validate;
}

function vdesc_validate() {
  var strError = V2validateData(this.desc,this.itemobj,this.error);
  if(strError != '') {
	 return strError;
  }
  return '';
}

function ValidationSet(inputitem) {
  this.vSet     = new Array();
  this.add      = add_validationdesc;
  this.validate = vset_validate;
  this.itemobj  = inputitem;
}

function add_validationdesc(desc,error) {
  this.vSet[this.vSet.length]= new ValidationDesc(this.itemobj,desc,error);
}

function vset_validate() {
  for(var itr=0;itr<this.vSet.length;itr++) {
    var strError = this.vSet[itr].validate();
    if(strError != '') {
      return strError;
    }
  }
  return '';
}

function validateEmailv2(email) {
  if(email.length <= 0) {
    return true;
  }
  //var regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/;
  var regexp = /^[^@\s]+@([-a-z0-9]+\.)+([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum)$/i;
  if (!email.match(regexp)) return false;
  return true;
}

function validatePostalZipv2(code) {
  if(code.length <= 0) {
    return true;
  }
  var regexp = /^\d{5}$/;
  if (!code.match(regexp)) return false;
  return true;
}

function validateTelv2(tel) {
  if(tel.length <= 0) {
    return true;
  }
  var regexp = /^[0-9]{2}(\.|-|\s)[0-9]{2}(\.|-|\s)[0-9]{2}(\.|-|\s)[0-9]{2}(\.|-|\s)[0-9]{2}$/;
  if(!tel.match(regexp)) return false;
  return true;
}

function V2validateData(strValidateStr,objValue,strError) { 
  var epos      = strValidateStr.search("="); 
  var  command  = ""; 
  var  cmdvalue = ""; 
  if(epos >= 0) { 
    command  = strValidateStr.substring(0,epos); 
    cmdvalue = strValidateStr.substr(epos+1); 
  } else { 
    command  = strValidateStr; 
  } 
  switch(command) { 
    case "req": 
    case "required": 
      if(eval(objValue.value.length) == 0) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name + " : champ requis"; 
        } 
        return strError; 
      } 
      break;             
    case "maxlength": 
    case "maxlen": 
      if(eval(objValue.value.length) > eval(cmdvalue)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name + " : "+cmdvalue+" charactères maximum "; 
        }
        return strError + "\n[longueur actuelle = " + objValue.value.length + " ]"; 
      }
      break; 
    case "minlength": 
    case "minlen": 
      if(eval(objValue.value.length) < eval(cmdvalue)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name + " : " + cmdvalue + " charactères minimum  "; 
        }
        return strError + "\n[longueur actuelle = " + objValue.value.length + " ]"; 
      }
      break; 
    case "length":
    case "len":
      if(eval(objValue.value.length) != eval(cmdvalue)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name + " : " + cmdvalue + " charactères  "; 
        }
        return strError + "\n[longueur actuelle = " + objValue.value.length + " ]"; 
      }
      break;
    case "alnum": 
    case "alphanumeric": 
      var charpos = objValue.value.search("[^A-Za-z0-9]"); 
      if(objValue.value.length > 0 &&  charpos >= 0) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": seuls les caractères alpha-numériques sont autorisés "; 
        }
        return strError + "\n [caractère non autorisé à la podition " + eval(charpos+1)+"]"; 
      } 
      break; 
    case "num": 
    case "numeric": 
      var charpos = objValue.value.search("[^0-9]"); 
      if(objValue.value.length > 0 &&  charpos >= 0) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": seuls les caractères numériques sont autorisés "; 
        }
        return strError + "\n [caractère non autorisé à la position " + eval(charpos+1)+"]"; 
      }
      break;               
    case "alphabetic": 
    case "alpha": 
      var charpos = objValue.value.search("[^A-Za-z]"); 
      if(objValue.value.length > 0 &&  charpos >= 0) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": seuls les caractères alphabétiques sont autorisés "; 
        }
        return strError + "\n [caractère non autorisé à la position " + eval(charpos+1)+"]"; 
      }
      break; 
	 case "alnumhyphen":
      var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 
      if(objValue.value.length > 0 &&  charpos >= 0) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": les caractères autorisés sont A-Z,a-z,0-9,- et _"; 
        }
        return strError + "\n [Error character position " + eval(charpos+1)+"]"; 
      }
		break;
    case "email": 
      if(!validateEmailv2(objValue.value)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": entrez une adresse email valide "; 
        }
        return strError; 
      }
      break; 
    case "postalzip": 
      if(!validatePostalZipv2(objValue.value)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": entrez une adresse email valide "; 
        }
        return strError; 
      }
      break;    case "lt": 
    case "tel": 
      if(!validateTelv2(objValue.value)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": entrez une adresse email valide "; 
        }
        return strError; 
      }
      break;
    case "lt":
    case "lessthan": 
      if(isNaN(objValue.value)) { 
        return objValue.name + ": doit être un nombre "; 
      } 
      if(eval(objValue.value) >= eval(cmdvalue)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name + " : cette valeur doit être inférieur à "+ cmdvalue; 
        }
        return strError; 
      }
      break; 
    case "gt": 
    case "greaterthan": 
      if(isNaN(objValue.value)) { 
        return objValue.name+": doit être un nombre "; 
      }
      if(eval(objValue.value) <=  eval(cmdvalue)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name + " : cette valeur doit être supérieur à "+ cmdvalue; 
        }
        return strError; 
      }//if             
      break; 
    case "regexp": 
		if(objValue.value.length > 0) {
	     if(!objValue.value.match(cmdvalue)) { 
	       if(!strError || strError.length ==0) { 
            strError = objValue.name+": caractère non valide trouvé "; 
	       }
	       return strError; 
	     }
      }
      break; 
    case "dontselect": 
      if(objValue.selectedIndex == null) { 
        return "BUG: pas d'item sélectionné "; 
      } 
      if(objValue.selectedIndex == eval(cmdvalue)) { 
        if(!strError || strError.length ==0) { 
          strError = objValue.name+": choisissez une option "; 
        }
        return strError; 
      } 
      break; 
    }//switch 
    return ''; 
}
/*
	Copyright 2003 JavaScript-coder.com. All rights reserved.
*/