<!--
/*
Validador.js
versión 1.2
19/04/2002
Copyright 2002 Yagé, evolución digital
desarrollado por Ariel Esquivel, Director de desarrollo
*/

function Validador(formulario) {
	this.formulario = formulario;
	this.expresiones = function() {
		for (var i=0; i<arguments.length; i+=3) {
			this.formulario[arguments[i]].setAttribute("expresion", arguments[i+1]);
			this.formulario[arguments[i]].setAttribute("nombre", arguments[i+2]);
		}
	}
	this.emailValido = function(email) {
		if (window.RegExp) {
			var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
			var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
			var reg1 = new RegExp(reg1str);
			var reg2 = new RegExp(reg2str);
			if (!reg1.test(email) && reg2.test(email)) {
				return true
			} else {
				return false
			};
		} else if(email.indexOf("@") >= 0) {
			return true
		} else {
			return false
		}
	}
	this.fechaValida = function(fecha) {
		var s = fecha.split("/");
		if (s.length != 3) return false;
		var dia = parseInt(s[0]);
		var mes = parseInt(s[1]);
		var anio = parseInt(s[2]);
		var d = new Date(anio, mes-1, dia)
		if (anio != d.getFullYear() || mes-1 != d.getMonth() || dia != d.getDate()) {
			return false;
		} else {
			return true;
		}
	}
	this.HoraValida = function(hora){
		var s = hora.split(":");
		if (s.length != 2 || isNaN(s[0]) || isNaN(s[1])) return false;
		var horas = parseInt(s[0]);
		var minutos = parseInt(s[1]);
		if (horas<0 || horas >23) return false;
		if (minutos<0 || minutos >59) return false;
		return true;
	}
	this.cumpleRegExp = function(patron, valor) {
		var regularExpresion = new RegExp(patron);
		var resultado = regularExpresion.exec(valor);
		if (resultado == null) return false;
		return (resultado.length == 1);
	}
	this.cedulaValida = function(cedula) {

		//Valida que la cédula sea de la forma ddddddddd-d
		if (! this.cumpleRegExp(/\d{9}-\d{1}/, cedula)) return false;

		//Valida que el # formado por los dos primeros dígitos esté entre 1 y 21
		var dosPrimerosDigitos = parseInt(cedula.substr(0, 2));
		if (dosPrimerosDigitos < 1 || dosPrimerosDigitos > 21) return false;

		//Valida que el valor acumulado entre los primeros 9 números coincida con el último
		var acumulado = 0, digito, aux;
		for (var i=1; i<=9; i++) {
			digito = parseInt(cedula.charAt(i-1));
			if (i % 2 == 0) { //si está en una posición par
				acumulado += digito;
			} else { //si está en una posición impar
				aux = 2 * digito;
				if (aux > 9) aux -= 9;
				acumulado += aux;
			}
		}
		acumulado = 10 - (acumulado % 10);
		if (acumulado == 10 ) acumulado = 0;
		var ultimoDigito = parseInt(cedula.charAt(10));
		if (ultimoDigito != acumulado) return false;
		
		//La cédula es válida
		return true;
	}
	this.validar = function() {
		var elemento, expresion, nombre, otroNombre, atomos, subAtomos, i;
		for (var e=0; e<this.formulario.length; e++) {
			elemento = this.formulario.elements.item(e);
			expresion = elemento.getAttribute("expresion");
			nombre = elemento.getAttribute("nombre");
			if (expresion != null) {
				atomos = expresion.split("|");
				for (i=0; i<atomos.length; i++) {
					switch(atomos[i]) {
						case "v":
							if (!elemento.value) {
								alert("Debe ingresar algún valor para el campo " + nombre + ".");
								elemento.focus();
								return false
							}
							break;
						case "l":
							if (elemento.options.length == 0) {
								alert("Debe seleccionar al menos un elemento de la lista  " + nombre + ".");
								elemento.focus();
								return false
							}
							break;
						case "e":
							if (! this.emailValido(elemento.value)) {
								alert("El valor del campo " + nombre + " no es válido.");
								elemento.focus();
								elemento.select();
								return false
							}
							break;
						case "f":
							if (! this.fechaValida(elemento.value)) {
								alert("El valor del campo " + nombre + " no es válido.");
								elemento.focus();
								elemento.select();
								return false;
							}
							break;
						case "h":
							if (! this.HoraValida(elemento.value)) {
								alert("El valor del campo " + nombre + " no es válido.");
								elemento.focus();
								elemento.select();
								return false;
							}
							break;
						case "c":
							if (! this.cedulaValida(elemento.value)) {
								alert("El valor del campo " + nombre + " no es válido.");
								elemento.focus();
								elemento.select();
								return false;
							}
							break;
						case "n": //Número natural
							var esNatural = true;
							if (isNaN(elemento.value)) {
								esNatural = false;
							} else if (parseInt(elemento.value) < 0) {
								esNatural = false;
							} else if (elemento.value.indexOf(".") >= 0) {
								esNatural = false;
							}
							if (! esNatural) {
								alert("El valor del campo " + nombre + " no es válido.");
								elemento.focus();
								elemento.select();
								return false
							}
							break;
						default:
							subAtomos = atomos[i].split("\:");
							switch(subAtomos[0]) {
								case "i":
									otroElemento = this.formulario[subAtomos[1]];
									otroNombre = otroElemento.getAttribute("nombre");
									if (elemento.value != otroElemento.value) {
										alert("Los campos " + otroNombre + " y " + nombre + " deben coincidir.")
										elemento.focus();
										elemento.select();
										return false;
									}
									break;
								case "re":
									if (! this.cumpleRegExp(subAtomos[1], elemento.value)) {
										alert("El valor del campo " + nombre + " no es válido.");
										elemento.focus();
										elemento.select();
										return false;
									}
									break;
							}
					}
				}
			}
		}
		return true;
	}
}

//-->
