
/**
* 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","vfecha");
    var title = true; // Debe o no enseñar la etiqueta title como parte del error
    var form_actual = "";
    var tipo = "despues";
    var alerta = false;
    var alerta_string = "";

    return this.each(function() {
	form_actual = this;
	jQuery(this).unbind("submit");
	jQuery(this).submit(function(e) {
	    //e.preventDefault();
	    if ($(this).is(":visible")) {
		tipo = "despues";
		if ($(this).hasClass("vantes")) {
		    tipo = "antes";
		}
		else
		if ($(this).hasClass("vmarca")) {
		    tipo = "marca";
		}
		alerta = $(this).hasClass("valerta") ? true : false;
		alerta_string = "";
		limpiarFormulario(this);
		no_validados = new Array();
		var buscados = buscaElementos(this);
		if (buscados == false) {
		    if (alerta) {
			alert(alerta_string);
		    }
		    /*$("html,body").animate({
						scrollTop: $(this).offset().top
					}, 1000);*/
		    return false;
		}
		else {
		    return true;
		}
	    } else {
		return 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("input, select, textarea").removeClass("inputError");
	jQuery("div ."+clase_error,form.elements).fadeOut("slow");
    };

    function avisarError(elemento, pos_tipo) {
	var descripcion = (elemento.title && title) ? elemento.title : "Error";
	var id = elemento.id ? elemento.id : elemento.name;
	jQuery(elemento).addClass("inputError");
	var id_insercion = id+"-error";
	if (jQuery("#"+id_insercion).attr("class")==clase_error) {
	} else if (!alerta && tipo!="marca") {
	    var insercion = "<div class=\""+clase_error+"\" id=\""+id_insercion+"\"><span>"+descripcion+"</span></div>";
	    tipo=="despues" ? jQuery(elemento).after(insercion) : jQuery(elemento).before(insercion);
	}
	if (alerta) {
	    alerta_string = alerta_string ? alerta_string+"\n"+descripcion : descripcion;
	} else {
	    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 vfecha(valor) {
	return (vrequerido(valor)==false) ? true: evaluar(valor, "^[0-9\,]{2}/[0-9\,]{2}/[0-9\,]{4}$");
    }

    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();
});
