/**
* Requiere que se defina el mensaje de error en el atributo "title" de cada campo a ser validado
* Se debe asignar el tipo de validación en el atributo class del elemento a validar
*/
jQuery.fn.validacion = function() {

        var no_validados = new Array();
        var clase_error = "errorForm";
        var clases = new Array("vrequerido","vemail","vnumero","valfa","valfanumerico","vdecimal","vunosolo");
        var title = true; // Debe o no enseñar la etiqueta title como parte del error        
        var form_actual = "";

        return this.each(function() {
			form_actual = this;
			jQuery(this).submit(function() {
				limpiarFormulario(this);
				no_validados = new Array();
				return (buscaElementos(this)==false) ? false : true;
			});
        });

        function buscaElementos(form) {
			$.each(clases,function(pos, clase) { 
				jQuery("."+clase, form).each(function() { 
					var elemento = this;
					if (validar(elemento, clase, pos)==false) {
						no_validados.push(elemento.name);
					}
				});
			});
			return (no_validados.length>0) ? false : true;
        };

        function limpiarFormulario(form) {
                jQuery("div ."+clase_error,form.elements).remove();
        };

        function avisarError(elemento, pos_tipo) {
                var descripcion = (elemento.title && title) ? elemento.title : "Error";                
                var id = elemento.id ? elemento.id : elemento.name;
                var id_insercion = id+"-error";
                if (jQuery("#"+id_insercion).attr("class")==clase_error) {
                } else {
                        var insercion = "<span style=\"color:red\" class=\""+clase_error+"\" id=\""+id_insercion+"\">"+descripcion+"</span>";
                        jQuery(elemento).after(insercion);
                }
                jQuery("#"+id_insercion).fadeIn("slow");
        };

        function validar (elemento, tipo, pos_tipo) {
                var v_elemento = jQuery(elemento).val();
                var dev = true;
                if (typeof v_elemento=="undefined" || v_elemento==null) {
                        v_elemento = "";
                }
                if (!isArray(v_elemento)) {
                        v_elemento = new Array (v_elemento);
                } else {
                        if (v_elemento.length==0) {
                                if (!validarValor(elemento, tipo, pos_tipo, "")) {
                                        dev = false;
                                }
                        }
                }
                for (var i=0; i<v_elemento.length; i++) {
                        if (!validarValor(elemento, tipo, pos_tipo, v_elemento[i])) {
                                dev = false;
                        }
                }
                return dev;
        };

        function validarValor (elemento, tipo, pos_tipo, valor) {
                if (eval(tipo+"(valor)")==false) {
                        avisarError(elemento, pos_tipo);
                        return false;
                } else {
                        return true;
                }
        };

        function valfa (valor) {
                return (vrequerido(valor)==false) ? true : evaluar(valor, "^[0-9]+$");
        };

        function valfanumerico(valor) {
                return (vrequerido(valor)==false) ? true : evaluar(valor, "^[a-zA-Z]+$");
        };

        function vnumero(valor) {
                return (vrequerido(valor)==false) ? true : evaluar(valor, "^[0-9]+$");
        };

        function vdecimal(valor) {
                return (vrequerido(valor)==false) ? true : evaluar(valor, "^[0-9\,]+$");
        };

        function vrequerido (valor) {
                return !evaluar(valor, /^\s*$/);
        };

        function vemail(valor) {
                return (vrequerido(valor)==false) ? true : evaluar(valor, "^[a-z0-9]+([_\\.-][a-z0-9]+)*"+"@([a-z0-9]+([\.-][a-z0-9]{1,})+)*$");
        };

        function vunosolo(valor) {
                var cant = 0;
                jQuery(".vunosolo",form_actual).each(function() {
                        var texto = jQuery(this).val();
                        cant = (texto.search(/^\s*$/)>-1!=false) ? cant : cant+1;
                });
                return cant==1 ? true : false;
        };

        function evaluar(valor, expresion) {
                return valor.search(expresion)>-1 ? true : false;
        };

        function isArray(obj) {
                if (obj.constructor.toString().indexOf("Array") == -1) {
                        return false;
                } else {
                        return true;
                }
        };
};

jQuery(function($) {
	$("form").validacion();
});