/**
 * 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();
					tipo = $(this).hasClass("vantes") ? "antes" : "despues";
					alerta = $(this).hasClass("valerta") ? true : false;
					alerta_form = $(this).hasClass("vform") ? true : false;
					alerta_string = "";
					limpiarFormulario(this);
					no_validados = new Array();
					var buscados = buscaElementos(this);
					if (buscados==false) {
						if (alerta) {
							alert(alerta_string);
						} else if (alerta_form) {
							var insercion = "<div class=\""+clase_error+"\" id=\"msgFormValidacion\"><span></span>"
									+alerta_string+"</div>";
							jQuery(this).find("div:first").before(insercion);
							jQuery("#msgFormValidacion").hide().fadeIn("slow");
						}
						return false;
					} 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("div ."+clase_error, form.elements).remove();
		jQuery("div ."+clase_error).remove();
		jQuery(form.elements).removeClass(clase_error+"Input");
	};

	function avisarError(elemento, pos_tipo) {
		var descripcion = (elemento.title&&title) ? elemento.title : "Campo requerido";
		var id = elemento.id ? elemento.id : elemento.name;
		var id_insercion = id+"-error";
		if (jQuery("#"+id_insercion).attr("class")==clase_error) {
		} else if (!alerta&&!alerta_form) {
			var insercion = "<div class=\""+clase_error+"\" id=\""+id_insercion+"\"><span></span>"+descripcion+"</div>";
			tipo=="despues" ? jQuery(elemento).after(insercion) : jQuery(elemento).before(insercion);
		}
		if (alerta||alerta_form) {
			if (alerta) {
				alerta_string = alerta_string ? alerta_string+"\n"+descripcion : descripcion;
			} else {
				alerta_string = alerta_string ? alerta_string+"<br />"+descripcion : descripcion;
			}
		} else {
			jQuery("#"+id_insercion).hide().fadeIn("slow");
		}
		jQuery(elemento).addClass(clase_error+"Input");
	};

	function validar(elemento, tipo, pos_tipo) {
		var dev = true;
		var v_elemento = jQuery(elemento).val();
		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) {
		var dev = false;
		var tipo_input = jQuery(elemento).attr("type");
		if (tipo_input=="checkbox") {
			//console.log(jQuery(elemento).is(":checked"));
			if (!jQuery(elemento).is(":checked")) {
				avisarError(elemento, pos_tipo);
			} else {
				dev = true;
			}
		} else {
			if (eval(tipo+"(valor)")==false) {
				avisarError(elemento, pos_tipo);
			} else {
				dev = true;
			}
		}
		return dev;
	};

	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) {
		return (obj.constructor.toString().indexOf("Array")==-1) ? false : true;
	};
};

jQuery(function($) {
	$("form").validacion();
});