/**
 * Really easy field validation with Prototype
 * http://tetlaw.id.au/view/javascript/really-easy-field-validation
 * Andrew Tetlaw
 * Version 1.5.4.1 (2007-01-05)
 *
 * Copyright (c) 2007 Andrew Tetlaw
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
var Validator = Class.create();

Validator.prototype = {
  initialize : function(className, error, test, options) {
    if(typeof test == 'function'){
      this.options = $H(options);
      this._test = test;
    } else {
      this.options = $H(test);
      this._test = function(){return true};
    }
    this.error = error || 'Validation failed.';
    this.className = className;
  },
  test : function(v, elm) {
    return (this._test(v,elm) && this.options.all(function(p){
      return Validator.methods[p.key] ? Validator.methods[p.key](v,elm,p.value) : true;
    }));
  }
}
Validator.methods = {
  pattern : function(v,elm,opt) {return Validation.get('IsEmpty').test(v) || opt.test(v)},
  minLength : function(v,elm,opt) {return v.length >= opt},
  maxLength : function(v,elm,opt) {return v.length <= opt},
  min : function(v,elm,opt) {return v >= parseFloat(opt)},
  max : function(v,elm,opt) {return v <= parseFloat(opt)},
  notOneOf : function(v,elm,opt) {return $A(opt).all(function(value) {
    return v != value;
  })},
  oneOf : function(v,elm,opt) {return $A(opt).any(function(value) {
    return v == value;
  })},
  is : function(v,elm,opt) {return v == opt},
  isNot : function(v,elm,opt) {return v != opt},
  equalToField : function(v,elm,opt) {return v == $F(opt)},
  notEqualToField : function(v,elm,opt) {return v != $F(opt)},
  include : function(v,elm,opt) {return $A(opt).all(function(value) {
    return Validation.get(value).test(v,elm);
  })}
}

var Validation = Class.create();

Validation.prototype = {
  initialize : function(form, options){
    this.options = Object.extend({
      onSubmit : true,
      stopOnFirst : false,
      immediate : false,
      focusOnError : false,
      appearEffect : false,
      useTitles : true,
      onFormValidate : function(result, form) {},
      onElementValidate : function(result, elm) {}
    }, options || {});
    this.form = $(form);
    if(this.options.onSubmit) Event.observe(this.form,'submit',this.onSubmit.bind(this),false);
    if(this.options.immediate) {
      var useTitles = this.options.useTitles;
      var appearEffect = this.options.appearEffect;
      var callback = this.options.onElementValidate;
      Form.getElements(this.form).each(function(input) { // Thanks Mike!
        Event.observe(input, 'blur', function(ev) { Validation.validate(Event.element(ev),{useTitle : useTitles, appearEffect : appearEffect, onElementValidate : callback}); });
      });
    }
  },
  onSubmit :  function(ev){
    if(!this.validate()) Event.stop(ev);
  },
  validate : function() {
    var result = false;
    var useTitles = this.options.useTitles;
    var appearEffect = this.options.appearEffect;
    var callback = this.options.onElementValidate;
    if(this.options.stopOnFirst) {
      result = Form.getElements(this.form).all(function(elm) { return Validation.validate(elm,{useTitle : useTitles, appearEffect : appearEffect, onElementValidate : callback}); });
    } else {
      result = Form.getElements(this.form).collect(function(elm) { return Validation.validate(elm,{useTitle : useTitles, appearEffect : appearEffect, onElementValidate : callback}); }).all();
    }
    if(!result && this.options.focusOnError) {
      var element = Form.getElements(this.form).findAll(function(elm){return $(elm).hasClassName('validation-failed')}).first();
      if (element.type != 'hidden') element.focus();
    }
    this.options.onFormValidate(result, this.form);
    return result;
  },
  reset : function() {
    Form.getElements(this.form).each(Validation.reset);
  }
}

Object.extend(Validation, {
  validate : function(elm, options){
    options = Object.extend({
      useTitle : false,
      appearEffect : false,
      onElementValidate : function(result, elm) {}
    }, options || {});
    elm = $(elm);
    var cn = elm.classNames();
    return result = cn.all(function(value) {
      var test = Validation.test(value,elm,options.useTitle,options.appearEffect);
      options.onElementValidate(test, elm);
      return test;
    });
  },
  test : function(name, elm, useTitle, appearEffect) {
    var v = Validation.get(name);
    var prop = '__advice'+name.camelize();
    try {
    var conditions = elm.className.split(" ").grep(/^if-/).collect(function (v) { return new Validation.Condition(v); });
    if(Validation.isVisible(elm) && conditions.all(function(c) {return c.test()}) && !v.test($F(elm), elm)) {
      if(!elm[prop]) {
        var advice = Validation.getAdvice(name, elm);
        if(advice == null) {
          var errorMsg = useTitle ? ((elm && elm.alt) ? elm.alt : v.error) : v.error;
          $A(errorMsg.split("<->")).each(function(errorElm) {
            var errorText = "*" + name.toUpperCase() + "*";
            if (errorElm.strip().startsWith(errorText)) {
              errorMsg = errorElm.replace(errorText, "").strip();
            }
          });
          advice = '<label class="validation-advice" id="advice-' + name + '-' + Validation.getElmID(elm) +'"  for="' + elm.id + '" style="display:none">' + errorMsg + '</label>';
          if(!appearEffect) {
            $$("label:[id^='advice-']").each(function(ad) {
              if (ad.id.startsWith('advice-') && ad.id.endsWith('-' + Validation.getElmID(elm))) {
                Try.these(function() {
                  elm.parentNode.removeChild(ad)
                  Validation.resetAdvice(elm);
                });
              }
            });
          }
          switch (elm.type.toLowerCase()) {
            case 'checkbox':
            case 'radio':
              var p = elm.parentNode;
              if(p) {
                new Insertion.Bottom(p, advice);
              } else {
                new Insertion.After(elm, advice);
              }
              break;
            default:
              // Se comprueba si ha saltado la validación de un DatePicker o
              // algún componente compuesto por un input seguido de una imagen
              var nameImage = document.getElementById(elm.id +"_img");
              // Si existe xxx_img -> se trata de dataPicker
              if (nameImage) {
                // Se inserta el texto de validación detrás de la imagen
                new Insertion.After(nameImage, advice);
              }
              else {
                // Si no existe xx_img, se trata de un componente normal
                // Se inserta el texto de validación detrás de ese componente
                new Insertion.After(elm, advice);
              }
          }
          advice = Validation.getAdvice(name, elm);
        }
        if(appearEffect) {
          if(typeof Effect == 'undefined') {
            advice.style.display = 'block';
          } else {
            new Effect.Appear(advice, {duration : 1 });
          }
        }
      }
      elm[prop] = true;
      elm.removeClassName('validation-passed');
      elm.addClassName('validation-failed');
      return false;
    } else {
      var advice = Validation.getAdvice(name, elm);
      if(advice != null) advice.hide();
      elm[prop] = '';
      elm.removeClassName('validation-failed');
      elm.addClassName('validation-passed');
      return true;
    }
    } catch(e) {
      throw(e)
    }
  },
  isVisible : function(elm) {
    while(elm.tagName != 'BODY') {
      if(!$(elm).visible()) return false;
      elm = elm.parentNode;
    }
    return true;
  },
  getAdvice : function(name, elm) {
    return $('advice-' + name + '-' + Validation.getElmID(elm)) || $('advice-' + Validation.getElmID(elm));
  },
  getElmID : function(elm) {
    return elm.id ? elm.id : elm.name;
  },
  reset : function(elm) {
    elm = $(elm);
    var cn = elm.classNames();
    cn.each(function(value) {
      var prop = '__advice'+value.camelize();
      if(elm[prop]) {
        var advice = Validation.getAdvice(value, elm);
        advice.hide();
        elm[prop] = '';
      }
      elm.removeClassName('validation-failed');
      elm.removeClassName('validation-passed');
    });
  },
  resetAdvice : function(elm) {
    elm = $(elm);
    var cn = elm.className.split(" ");
    for (var i=0; i<cn.size(); i++) {
      var prop = '__advice'+cn[i].camelize();
      if(elm[prop]) {
        elm[prop] = '';
      }
    }
  },
  add : function(className, error, test, options) {
    var nv = {};
    nv[className] = new Validator(className, error, test, options);
    Object.extend(Validation.methods, nv);
  },
  addAllThese : function(validators) {
    var nv = {};
    $A(validators).each(function(value) {
        nv[value[0]] = new Validator(value[0], value[1], value[2], (value.length > 3 ? value[3] : {}));
      });
    Object.extend(Validation.methods, nv);
  },
  get : function(name) {
    return  Validation.methods[name] ? Validation.methods[name] : Validation.methods['_LikeNoIDIEverSaw_'];
  },
  methods : {
    '_LikeNoIDIEverSaw_' : new Validator('_LikeNoIDIEverSaw_','',{})
  }
});

Validation.Condition = Class.create()

Validation.Condition.prototype = {
  initialize: function (cn) {
    var tokens = cn.split('-').slice(1); // split on "-" and delete the "if"
    var validations = $H(Validation.methods).keys();
    this.validation = validations.select(function (v) { return cn.indexOf(v) > -1 }).sortBy(function(v) { return v.length; })[0]; // grabs the longes validation we can get
    this.value = (!this.validation)?tokens[tokens.length-1]:null;
    this.negative = tokens[tokens.length - 1 - (this.validation?this.validation.split("-").length:1)] == "not"; // is there a "not" before the validation
    this.field = tokens.slice(0, tokens.length - (this.validation?this.validation.split("-").length:1) - (this.negative?1:0)).join("-"); // the rest should be the field name
  },
  test: function () {
    var ret = true;
    if (this.validation) {
      var v = Validation.get(this.validation);
      ret = v.test($F(this.field), $(this.field));
    } else {
      ret = $F(this.field) == this.value;
    }
    return (this.negative)?!ret:ret;
  }
}

Validation.add('IsEmpty', '', function(v) {
  return  ((v == null) || (v.length == 0)); // || /^\s+$/.test(v));
});

Validation.addAllThese([
  ['required', 'This is a required field.', function(v) {
    return !Validation.get('IsEmpty').test(v);
    }],
  ['validate-number', 'Please enter a valid number in this field.', function(v) {
    return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
    }],
  ['validate-digits', 'Please use numbers only in this field. please avoid spaces or other characters such as dots or commas.', function(v) {
    return Validation.get('IsEmpty').test(v) ||  !/[^\d]/.test(v);
    }],
  ['validate-alpha', 'Please use letters only (a-z) in this field.', function (v) {
    return Validation.get('IsEmpty').test(v) ||  /^[a-zA-Z]+$/.test(v)
    }],
  ['validate-alphanum', 'Please use only letters (a-z) or numbers (0-9) only in this field. No spaces or other characters are allowed.', function(v) {
    return Validation.get('IsEmpty').test(v) ||  !/\W/.test(v)
    }],
  ['validate-date', 'Please enter a valid date.', function(v) {
    var test = new Date(v);
    return Validation.get('IsEmpty').test(v) || !isNaN(test);
    }],
  ['validate-email', 'Please enter a valid email address. For example fred@domain.com .', function (v) {
    return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
    }],
  ['validate-url', 'Please enter a valid URL.', function (v) {
    return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
    }],
  ['validate-date-au', 'Please use this date format: dd/mm/yyyy. For example 17/03/2006 for the 17th of March, 2006.', function(v) {
    if(Validation.get('IsEmpty').test(v)) return true;
    var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
    if(!regex.test(v)) return false;
    var d = new Date(v.replace(regex, '$2/$1/$3'));
    return ( parseInt(RegExp.$2, 10) == (1+d.getMonth()) ) &&
        (parseInt(RegExp.$1, 10) == d.getDate()) &&
        (parseInt(RegExp.$3, 10) == d.getFullYear() );
    }],
  ['validate-currency-dollar', 'Please enter a valid $ amount. For example $100.00 .', function(v) {
    // [$]1[##][,###]+[.##]
    // [$]1###+[.##]
    // [$]0.##
    // [$].##
    return Validation.get('IsEmpty').test(v) ||  /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/.test(v)
    }],
  ['validate-selection', 'Please make a selection', function(v,elm){
    return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
    }],
  ['validate-one-required', 'Please select one of the above options.', function (v,elm) {
    var p = elm.parentNode;
    var options = p.getElementsByTagName('INPUT');
    return $A(options).any(function(elm) {
      return $F(elm);
    });
    }]
]);

Validation.addAllThese([
  ['requerido', 'Este campo es obligatorio.', function(v) {
      return !Validation.get('IsEmpty').test(v);
  }],
  ['blanco', 'Este campo deber&iacute;a estar vac&iacute;o.', function (v) {
    return Validation.get('IsEmpty').test(v);
  }],
  ['validar-numero', 'Introduce un n&uacute;mero v&aacute;lido en este campo.', function(v) {
    return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
  }],
  ['validar-digitos', 'Usa solamente n&uacute;meros en este campo.', function(v) {
    return Validation.get('IsEmpty').test(v) ||  !/[^\d]/.test(v);
  }],
  ['validar-alfa', 'Usa solamente letras en este campo.', function (v) {
    return Validation.get('IsEmpty').test(v) ||  /^[a-zA-Z]+$/.test(v)
  }],
  ['validar-alfanum', 'Usa letras o n&uacute;meros. No se permiten otros caracteres, ni espacios.', function(v) {
    return Validation.get('IsEmpty').test(v) ||  !/\W/.test(v)
  }],
  ['validar-email', 'Introduce un email v&aacute;lido.', function (v) {
    return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
  }],
  ['validar-url', 'Introduce una URL v&aacute;lida.', function (v) {
    return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
  }],
  ['validar-fecha', 'Por favor usa el siguiente formato: dd/mm/yyyy.', function(v) {
    if(Validation.get('IsEmpty').test(v)) return true;
    var regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    if(!regex.test(v)) return false;
    var d = new Date(v.replace(regex, '$2/$1/$3'));
    return ( parseInt(RegExp.$2, 10) == (1+d.getMonth()) ) &&
          (parseInt(RegExp.$1, 10) == d.getDate()) &&
          (parseInt(RegExp.$3, 10) == d.getFullYear() );
  }],
  ['validar-seleccion', 'Selecciona una opci&oacute;n.', function(v,elm){
    return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
    }],
  ['validar-uno-requerido', 'Selecciona una opci&oacute;n.', function (v,elm) {
    var p = elm.parentNode;
    var options = p.getElementsByTagName('INPUT');
    return $A(options).any(function(elm) {
      return $F(elm);
    });
  }],
  ['validar-marcado', 'Debes seleccionar la opci&oacute;n asociada.', function (v,elm) {
    var p = elm.up().parentNode;
    var all = p.getElementsByTagName('INPUT');
    var options = new Array;
    for (var i=0, l = all.length; i < l; i++) {
      var type = all[i].getAttribute('type');
      if (type) {
        if ((type.indexOf('checkbox') > -1) || (type.indexOf('radio') > -1)) {
          options.push(all[i]);
        }
      }
    }
    return $A(options).any(function(opt) {
      return opt.checked ? true : false;
    });
  }],
  ['validar-nif', 'No es un NIF v&aacute;lido.', function (v) {
    var letra = v.substring(v.length-1).toUpperCase();
    var compStr = "TRWAGMYFPDXBNJZSQVHLCKEU";
    v = v.substring(0, v.length-1);
    if (v.charAt(0)=="0") {
      v = v.substring(1, v.length);
    }
    if (v > 0 && v < 99999999) {
      var resto = v % 23;
      if (compStr.charAt(resto) == letra) {
        return true;
      }
    }
    return false;
  }],
  ['validar-cif', 'No es un CIF v&aacute;lido.', function (v) {
    var pares = 0, impares = 0;
    var suma, ultima, unumero;
    var uletra = "JABCDEFGHI";
    var tmp;
    v = v.toUpperCase();
    var regex = /^[ABCDEFGHKLMNPQS]\d{7}[0-9,A-J]$/g;
    if (!regex.test(v)) return false;
    ultima = v.substr(8,1);
    for (var cont = 1 ; cont < 7 ; cont ++){
      tmp = (2 * parseInt(v.substr(cont++,1))).toString() + "0";
      impares += parseInt(tmp.substr(0,1)) + parseInt(tmp.substr(1,1));
      pares += parseInt(v.substr(cont,1));
     }
     tmp = (2 * parseInt(v.substr(cont,1))).toString() + "0";
     impares += parseInt(tmp.substr(0,1)) + parseInt(tmp.substr(1,1));
     suma = (pares + impares).toString();
     unumero = parseInt(suma.substr(suma.length - 1, 1));
     unumero = (10 - unumero).toString();
     if(unumero == 10) unumero = 0;
     return ((ultima == unumero) || (ultima == uletra.charAt(unumero)))
  }],
  ['validar-cod-postal', 'No es un c&oacute;digo postal espa&ntilde;ol v&aacute;lido.', function (v) {
    return Validation.get('IsEmpty').test(v) || /\d{5}/.test(v);
  }]
]);

Validation.addAllThese([
  ['obrigatorio', 'Este campo &eacute; obrigat&oacute;rio.', function(v) {
      return !Validation.get('IsEmpty').test(v);
  }],
  ['alvo', 'Este campo deveria estar esvaziamento.', function (v) {
    return Validation.get('IsEmpty').test(v);
  }],
  ['comprovar-numero', 'Introduza um n&uacute;mero v&aacute;lido neste campo.', function(v) {
    return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
  }],
  ['comprovar-digitos', 'Utilize somente n&uacute;meros neste campo.', function(v) {
    return Validation.get('IsEmpty').test(v) ||  !/[^\d]/.test(v);
  }],
  ['comprovar-alfa', 'Utilize somente letras neste campo.', function (v) {
    return Validation.get('IsEmpty').test(v) ||  /^[a-zA-Z]+$/.test(v)
  }],
  ['comprovar-alfanum', 'Utilize letras ou n&uacute;meros. N&atilde;o se permitem outros carateres, nem espa&ccedil;os.', function(v) {
    return Validation.get('IsEmpty').test(v) ||  !/\W/.test(v)
  }],
  ['comprovar-email', 'Introduza um email v&aacute;lido.', function (v) {
    return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
  }],
  ['comprovar-url', 'Introduza uma URL v&aacute;lida.', function (v) {
    return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
  }],
  ['comprovar-data', 'Por favor utilize o seguinte formato: dd/mm/yyyy.', function(v) {
    if(Validation.get('IsEmpty').test(v)) return true;
    var regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    if(!regex.test(v)) return false;
    var d = new Date(v.replace(regex, '$2/$1/$3'));
    return ( parseInt(RegExp.$2, 10) == (1+d.getMonth()) ) &&
          (parseInt(RegExp.$1, 10) == d.getDate()) &&
          (parseInt(RegExp.$3, 10) == d.getFullYear() );
  }],
  ['comprovar-selecao', 'Selecione uma opci&oacute;n.', function(v,elm){
    return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
    }],
  ['comprovar-um-requerido', 'Selecione uma opci&oacute;n.', function (v,elm) {
    var p = elm.parentNode;
    var options = p.getElementsByTagName('INPUT');
    return $A(options).any(function(elm) {
      return $F(elm);
    });
  }],
  ['comprovar-marcado', 'Deve selecionar a opci&oacute;n associado.', function (v,elm) {
    var p = elm.up().parentNode;
    var all = p.getElementsByTagName('INPUT');
    var options = new Array;
    for (var i=0, l = all.length; i < l; i++) {
      var type = all[i].getAttribute('type');
      if (type) {
        if ((type.indexOf('checkbox') > -1) || (type.indexOf('radio') > -1)) {
          options.push(all[i]);
        }
      }
    }
    return $A(options).any(function(opt) {
      return opt.checked ? true : false;
    });
  }],
  ['comprovar-cod-postal', 'N&atilde;o &eacute; um c&oacute;digo postal portugu&ecirc;s v&aacute;lido.', function (v) {
    return Validation.get('IsEmpty').test(v) || /\d{4}-\d{3}/.test(v);
  }],
  ['comprovar-designac-postal', 'N&atilde;o &eacute; uma designa&ccedil;&atilde;o postal portugu&ecirc;s v&aacute;lida.', function (v) {
    return Validation.get('IsEmpty').test(v) || /^[\w\s]*$/.test(v);
  }]
]);

