/**
 * @file al_valform_fr.js
 *  
 *  Bibliothèque de fonctions de validation de Formulaire.
 *
 *  => Version Française.
 *
 * <!----------------------------> <p><hr></p> <!---------------------------->
 * @author       F.PELLEAU
 * @version      1.5
 * copyright    2004-2006 ALTIDEV. All rights reserved.
 * <!----------------------------> <p><hr></p> <!---------------------------->
 * Historique des versions :
 *  - v1.0 : 07.2004 : FP : Version Initiale
 *  - v1.1 : 07.2004 : FP : ajout de la validation des dates
 *  - v1.2 : 08.2004 : FP : ameliorations
 *           ajout de la validation des heures
 *           protection de l'utilisation de select() et focus() pour éviter les bugs sur les champs spéciaux
 *           avant cette modification, les champs n'ayant pas les méthodes select() ou focus() ne fonctionnaient pas
 *  - v1.3 : 09.2004 : FP : ajout de la possibilité d'utiliser 'h' ou 'H' comme séparateur des heures et des minutes
 *  - v1.4 : 10.2004 : FP : modification de check_mail pour autoriser les domaines à 4 lettres (ex: .info)
 *  - v1.5 : 02.2005 : FP : ajout de la validation des codes postaux
 * <!----------------------------> <p><hr></p> <!---------------------------->
 */

var memo_al_valform_loader;
if ( memo_al_valform_loader!='done' ) {
  memo_al_valform_loader = 'done';

  function focusAndExit(champ) {
	if (champ.select) champ.select();
	if (champ.focus)  champ.focus();
	return false;
  }

  function conv_toLowerCase(champ) {
	  if (champ.value) { champ.value=champ.value.toLowerCase(); }
	  return true;
  }

  function conv_toUpperCase(champ) {
	  if (champ.value) { champ.value=champ.value.toUpperCase(); }
	  return true;
  }

  function check_pwd(champ1,champ2) {
	if (champ1.value==champ2.value) return true;
	else if (champ1.value=='')
	  if (champ2.value=='')  return true;
	  else {
		alert('Vous devez saisir deux fois le mot de passe pour le confirmer');
		return focusAndExit(champ1);
	  }
	else if (champ2.value=='') {
	  alert('Vous devez saisir deux fois le mot de passe pour le confirmer');
	  return focusAndExit(champ2);
	} else {
	  alert('La confirmation du mot de passe n\'est pas valide (les deux champs sont differents)');
	  return focusAndExit(champ1);
	}
  }

  function check_isset(champ,nomchamp) {
	if (champ.value=='') {
		alert('Vous devez remplir le champ > '+nomchamp+' <');
		return focusAndExit(champ);
	} else return true;
  }

  function check_email(champ) {
	with (champ)
	{
	  if (value=='') return true;
	  apos=value.indexOf('@');
	  dotpos=value.lastIndexOf('.');
	  lastpos=value.length-1;
	  if (apos<1 || dotpos-apos<2 || lastpos-dotpos>4 || lastpos-dotpos<2) {
		alert('Adresse email non valide');
		return focusAndExit(champ);
	  } else return true;
	}
  }

  function check_date(champ) {
	// formats autorisés : j/m/aaaa | j/mm/aaaa | jj/mm/aaaa | jj/m/aaaa
	var filter=/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$/i
	if (champ.value=='') return true;
	if ( result = champ.value.match(filter) ) {
		if (result[1]>0 && result[1]<32) {
			if (result[2]>0 && result[2]<13) {
				if (result[3]>1000 && result[3]<3000) {
					return true;
				}
			}
		}
	}
	alert('Date non valide !\n\nformat souhaité = jj/mm/aaaa');
	return focusAndExit(champ);
  }

  function check_codepostal(champ) {
	// formats autorisés : 5 chiffres
	var filter=/^([0-9][0-9][0-9][0-9][0-9])$/i
	if (champ.value=='') return true;
	if ( result = champ.value.match(filter) ) {
		return true;
	}
	alert('Code postal non valide !\n\nformat souhaité = 5 chiffres');
	return focusAndExit(champ);
  }

  function check_heure(champ) {
	// formats autorisés : h:m | h:mm | hh:m | hh:mm
	var filter=/^([0-2]{0,1}[0-9]):([0-5]{0,1}[0-9])$/i
	if (champ.value=='') return true;
	champ.value = champ.value.replace('h',':');
	champ.value = champ.value.replace('H',':');
	if ( result = champ.value.match(filter) ) {
		if (result[1]>=0 && result[1]<=23) {
			if (result[2]>=0 && result[2]<=59) {
				return true;
			}
		}
	}
	alert('Heure non valide !\n\nformat souhaité = hh:mm');
	return focusAndExit(champ);
  }

  function check_numeric(champ,nomchamp,defval) {
	if (champ.value=='') {
		if (defval!='') {
			champ.value=defval;
		}
		return true;
	}
	champ.value = champ.value.replace(',','.');
	conv  = parseFloat(champ.value);
	if (champ.value != ''+conv &&
		champ.value != ''+conv+'0' && champ.value != ''+conv+'00' && champ.value != ''+conv+'000' &&
		champ.value != ''+conv+'.0' && champ.value != ''+conv+'.00' && champ.value != ''+conv+'.000' ) {
		alert('Le champ > '+nomchamp+' < doit avoir une valeur numerique');
		return focusAndExit(champ);
	} else return true;
  }

  function check_integer(champ,nomchamp,defval) {
	if (champ.value=='') {
		if (defval!='') {
			champ.value=defval;
		}
		return true;
	}
	conv  = parseInt(champ.value);
	if (champ.value != ''+conv) {
		alert('Le champ > '+nomchamp+' < doit avoir une valeur numerique entière');
		return focusAndExit(champ);
	} else return true;
  }

  function check_alpha(champ,nomchamp) {
	if ( champ.value.search('[^A-Za-z]') != -1 ) {
		alert('Le champ > '+nomchamp+' < ne doit contenir que des lettres');
		return focusAndExit(champ);
	} else return true;
  }

  function check_alphanum(champ,nomchamp) {
	if ( champ.value.search('[^A-Za-z0-9_]') != -1 ) {
		alert('Le champ > '+nomchamp+' < doit être strictement Alpha-Numérique');
		return focusAndExit(champ);
	} else return true;
  }

  function check_valnum(champ,nomchamp,vmin,vmax,vdef) {
	if ( !check_numeric(champ,nomchamp,vdef) ) {
		return false;
	}
	if ( (champ.value < vmin) || (champ.value > vmax) ) {
		alert('Le champ > '+nomchamp+' < doit être compris entre '+vmin+' et '+vmax);
		return focusAndExit(champ);
	} else return true;
  }

  function check_valint(champ,nomchamp,vmin,vmax,vdef) {
	if ( !check_integer(champ,nomchamp,vdef) ) {
		return false;
	}
	if ( (champ.value < vmin) || (champ.value > vmax) ) {
		alert('Le champ > '+nomchamp+' < doit être compris entre '+vmin+' et '+vmax);
		return focusAndExit(champ);
	} else return true;
  }

  function check_size(champ,nomchamp,lmin,lmax) {
	if ( (champ.value.length < lmin) || (champ.value.length > lmax) ) {
		alert('Le champ > '+nomchamp+' < doit contenir entre '+lmin+' et '+lmax+' caractères');
		return focusAndExit(champ);
	} else return true;
  }

} // end if (memo_al_valform_loader!='done')

