/*
Script para validación genérica de formularios. v 1.0 11-DIC-2006

DEPENDENCIAS
Necesita la librería kinetica.js v 1.0.0 27-SEP-2006 o superior

INSTRUCCIONES
Para hacerla funcionar, el formulario a validar debe tener la id 'formulario',
debe tener un botón submit, y los campos deben tener tres atributos extra:
	- tipodato -> numero, texto, email, rut, fecha, etc...
	- obligatorio -> Si es false, el campo sólo se validará si hay texto en él.
	- msgerror -> el mensaje de error que se emitirá al usuario en caso de que el campo falle la validación.
	
La validación se hace en el evento 'onsubmit' del formulario y se registra automágicamente.
Lo único que se necesita es incluir este archivo y agregar los atributos a los campos que necesiten
ser validados

*/
var errores = Array();
$(document).ready(function() {
	
	/*VALIDADOR DE TEXTO GENERICO*/
	// * solo valida existencia de texto!
	$("textarea[@tipodato='texto']").each(function() {
		this.validation = function() {
			var o = $(this);
			if(o.attr("obligatorio") == 'true') {
				if($.trim(o.val()).length == 0) {
					errores.push($(this).attr("msgerror"));
				}
			}
		};
	});


	
	$("input[@tipodato='texto']").each(function() {
		this.validation = function() {
			var o = $(this);
			if(o.attr("obligatorio") == 'true') {
				if($.trim(o.val()).length == 0) {
					errores.push($(this).attr("msgerror"));
				}
			}
		};
	});

	/*VALIDADOR DE EMAIL*/
	// * valida por expresión regular
	$("input[@tipodato='email']").each(function() {
		this.validation = function() {
			var o = $(this);
			var regex = /^(\w|[-])+(\.(\w|[-])+)*@((\[([0-1]?\d?\d|2[0-4]\d|25[0-5])\.([0-1]?\d?\d|2[0-4]\d|25[0-5])\.([0-1]?\d?\d|2[0-4]\d|25[0-5])\.([0-1]?\d?\d|2[0-4]\d|25[0-5])\])|((([a-zA-Z0-9])+(([-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([-])+([a-zA-Z0-9])+)*))$/;
			if(o.attr("obligatorio") == 'true') {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				}
			} else if($.trim(o.val()).length > 0)  {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				}	
			}
		};
	});

	/*VALIDADOR DE RUT*/
	// * valida por funcion especial + expresion regular (15216626-k)
	$("input[@tipodato='rut']").each(function() {
		this.validation = function() {
			var o = $(this);
			var dvcalc = function (T){var M=0,S=1;for(;T;T=Math.floor(T/10)) S=(S+T%10*(9-M++%6))%11;return S?S-1:'k';};
			var regex = /^[1-9]{1}[0-9]{6,7}[-]{1}[0-9kK]{1}$/
			var rut = o.val().split("-")[0];
			var dv = o.val().split("-")[1];
			
			if(o.attr("obligatorio") == 'true') {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				} else {
					if(dvcalc(rut) != dv.toLowerCase()) errores.push($(this).attr("msgerror"));
				}
			} else if($.trim(o.val()).length > 0)  {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				} else {
					if(dvcalc(rut) != dv.toLowerCase()) errores.push($(this).attr("msgerror"));
				}	
			}
		};
	});
	
	/*VALIDADOR DE NUMEROS*/
	//valida por expresion regular.
	$("input[@tipodato='numero']").each(function() {
		this.validation = function() {
			var o = $(this);
			var regex = /^[0-9.,]+$/;
			if(o.attr("obligatorio") == 'true') {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				}
			} else if($.trim(o.val()).length > 0)  {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				}	
			}
		};
	});
	
	/*VALIDADOR DE FECHAS*/
	//utiliza expresion regular, considera años biciestos en formato dd/mm/yyyy
	$("input[@tipodato='fecha']").each(function() {
		this.validation = function() {
			var o = $(this);
			var regex = /^(((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(02)([-./])(\d{4}))|((29)(\.|-|\/)(02)([-./])([02468][048]00))|((29)([-./])(02)([-./])([13579][26]00))|((29)([-./])(02)([-./])([0-9][0-9][0][48]))|((29)([-./])(02)([-./])([0-9][0-9][2468][048]))|((29)([-./])(02)([-./])([0-9][0-9][13579][26])))$/;
			if(o.attr("obligatorio") == 'true') {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				}
			} else if($.trim(o.val()).length > 0)  {
				if(!regex.test(o.val())) {
					errores.push($(this).attr("msgerror"));
				}	
			}
		};
	});
	
	/*VALIDADOR DE SELECTBOXES*/
	//Opciones inválidas en el select deben tener valor -1
	$("select").each(function() {
		this.validation = function() {
			var o = $(this);
			if(o.attr("obligatorio") == 'true') {
				if(o.val() == -1) {
					errores.push($(this).attr("msgerror"));
				}
			} 
		};
	});
	
	$("#formulario").submit(function(){
		$("input, select, textarea",this).each(function(){
			if(this.validation) { this.validation(); }
		});
		if(errores.length > 0) {
			var msg = "Debe corregir los siguientes errores:\n";
			$.each(errores, function(i) {
				msg=msg+"-"+this+"\n";
			});
			alert(msg);
			errores = Array();
			return false;
		}
		else {
			return true;
		}
	});
});

