/*
 *
 * http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype
 * Herve CARTRON
 * Version 1.0 (2007-01-08)
 *
 * Nécessite :
 * - prototype.js (version 1.5.0_rc2 ou sup)
 * - validation.js : Really easy field validation with Prototype http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype
 *
 */

/*
Validation.add('toto', 'Error message text', {
     pattern : new RegExp("^[a-zA-Z]+$","gi"), // only letter allowed
     minLength : 6, // value must be at least 6 characters
     maxLength : 13, // value must be no longer than 13 characters
     min : 5, // value is not less than this number
     max : 100, // value is not more than this number
     notOneOf : ['password', 'PASSWORD'], // value does not equal anything in this array
     oneOf : ['fish','chicken','beef'], // value must equal one of the values in this array
     is :  '5', // value is equal to this string
     isNot : 'turnip', //value is not equal to this string
     equalToField : 'password', // value is equal to the form element with this ID
     notEqualToField : 'username', // value is not equal to the form element with this ID
     include : ['validate-alphanum'] // also tests each validator included in this array of validator keys (there are no sanity checks so beware infinite loops!)
});
*/

Validation.addAllThese([
     ['validate-limit-min', 'La valeur de ce champ est trop petite',
            function(v,elt) {
                              var limitmin = elt.getAttribute('min');
                              if(limitmin == null) {alert('Erreur : vous n\'avez pas défini l\'attribut > min < dans <element name="' + elt.name + '" />');return;}
                              limitmin = ($(limitmin) != null) ? $(limitmin).value : limitmin;
                              return Validation.get('IsEmpty').test(v) || (Validation.get('validate-number').test(v) && (parseFloat(v) > parseFloat(limitmin)));
     }],
     ['validate-limit-max', 'La valeur de ce champ est trop grande',
            function(v,elt) {
                              var limitmax = elt.getAttribute('max');
                              if(limitmax == null) {alert('Erreur : vous n\'avez pas défini l\'attribut > max < dans <element name="' + elt.name + '" />');return;}
                              limitmax = ($(limitmax) != null) ? $(limitmax).value : limitmax;
                              return Validation.get('IsEmpty').test(v) || (Validation.get('validate-number').test(v) && (parseFloat(v) < parseFloat(limitmax)));
     }],
     ['validate-limit-between', 'Vous n\'êtes pas dans le bon intervalle',
            function(v,elt) {
                              return Validation.get('IsEmpty').test(v) || (Validation.get('validate-number').test(v) && Validation.get('validate-limit-min').test(v,elt) && Validation.get('validate-limit-max').test(v,elt));
     }],
	['validate-int', 'Veuillez renseigner un nombre entier.', function(v) {
				return Validation.get('IsEmpty').test(v) || ((String(parseInt(v,10))) === (v.replace(/^\s+|\s+$/g,'')));
			}],
	['validate-float', 'Veuillez renseigner un nombre décimal.', function(v) {
				return Validation.get('IsEmpty').test(v) || ((String(parseFloat(v))) === (v.replace(/^\s+|\s+$/g,'')));
			}],
   ['validate-int-pos', 'Veuillez renseigner un nombre entier positif.', function(v) {
            while(v.substring(0,1)=='0') v=v.substring(1);
            return Validation.get('IsEmpty').test(v) || ((String(parseInt(v,10))) === (v.replace(/^\s+|\s+$/g,'')) && (parseInt(v,10) >= 0) );
         }],
	['validate-float-pos', 'Non valide', function(v) {
				return Validation.get('IsEmpty').test(v) || ( ((String(parseFloat(v))) === (v.replace(/^\s+|\s+$/g,'')) || (String(parseFloat(v))) === (String(parseInt(v)))) && (parseFloat(v) >= 0) );
			}],
	['validate-input-radio', '!', function(v,obj) {
				var name = obj.name;
				var tampon = false;
				$$('input[name="'+name+'"]').each(function(object){
               if(object.checked) tampon = true;
            });
            return tampon;
			}],
	['validate-date-annee', '!', function(v,obj) {
            // on remet l'année sur deux caractéres
            if(v.length==2)
            {
               if(v>30)
                  obj.value='19'+v;
               else
                  obj.value='20'+v;
            }

            if(v.length<4)
               return false;

				return true;
			}],
   ['validate-check-box', '!', function(v,obj) {
            return obj.checked;
			}],
   ['validate-size-password', 'Votre mot de passe doit contenir au minimum 6 caractères alphanumérique', function(v) {

      if($('motdepasse').value.length<6  && $('motdepasseConf').value.length<6 || $('motdepasse').value != $('motdepasseConf').value) return false;
      else return true;
   }],
   ['validate-birthdate', 'Merci de saisir votre date de naissance', function(v) {
      if(Validation.get('IsEmpty').test($('birthdate_j').value) || Validation.get('IsEmpty').test($('birthdate_m').value)) return false;
      else if(/[^\d]/.test($('birthdate_j').value) || /[^\d]/.test($('birthdate_m').value)) return false;
      else return true;

   }],
	['validate-quickSearch', 'Ce champ est obligatoire', function(v) {
	   return !Validation.get('IsEmpty').test(v) && v!='Recherche rapide';
   }]
]);