
	/*
		stringTool uServers: Checador de strings varias en JS
	*/
	
	// $URBANO_VERSION = "2.490";
	
	function stringTool() {

	}
	
	stringTool.prototype = new uObject();
	
	
	
	stringTool.prototype.Redondea = function(numero, digitos) {
		// #uDOC:TITLE Redondea
		// #uDOC:DESCR Redondea un numero a ciertos decimales
		
		if(digitos == undefined) { digitos = 2; }
		
		if (digitos > 0) {
			
			// Adicion cerdo: Si no vienen ceros. Agrega
			if(numero.toString().lastIndexOf(".") == -1) {
				numero = numero.toString() + ".";
				for(var i=0; i<digitos; i++) { numero = numero + "0"; }
				return numero;
			}
			
			if ((numero.toString().length - numero.toString().lastIndexOf(".")) > (digitos + 1)) {
				// Para redondear, sube el numero a la ^Digitos potencia, redondea y luego divdide
				
				var potencia = Math.pow(10, digitos);
				resultado = Math.round(numero * potencia) / potencia;
				
				// Verifica que tenga los digitos que pedimos:
				var adigitos = (resultado.toString().length - resultado.toString().lastIndexOf(".")) - 1;
				
				if(adigitos < digitos) {
					for(i=adigitos; i<digitos; i++) { resultado = resultado + "0"; }
				}
				
				return resultado;
				
				
			} else {
				
				adigitos = (numero.toString().length - numero.toString().lastIndexOf(".")) - 1;						
				for(i=adigitos; i<digitos; i++) { 
					numero = numero + "0";
				}
				
				return numero;
			}
		} else return Math.round(numero);
		
	}
	
	stringTool.prototype.esNombreCompleto = function(estringu) {
		//#uDOC:TITLE esNombreCompleto
		//#uDOC:DESCR Indica si una string es un nombre completo o no
		
		if(estringu.match(/\S+\s+\S+/)) { return true; }
		else { return false; }
	}
		
	stringTool.prototype.esTelefono = function(estringu) {
		//#uDOC:TITLE esTelefono
		//#uDOC:DESCR Indica si una string parece como un numero de telefono
		
		if(estringu.match(/^[-0-9\(\)\.\s+]+$/)) { return true; }
		else { return false; }
	}
		
	stringTool.prototype.esMailValido = function(estringu) {
		//#uDOC:TITLE esMailValido
		//#uDOC:DESCR Indica si una string es una direccion de mail valida
		
		if(estringu == undefined) { return false; }
		if(! estringu.match) { return false; }
		if(estringu.match(/\s+/)) { return this.setError("Mail invalido, la direccion no puede contener espacios"); }
		if(estringu.match(/^\S+@[-a-z0-9\.]+.[a-z]{2,8}$/i)) { return true; }
		else { return false; }
	}
	
	stringTool.prototype.esUserIDValido = function(estringu) {
		//#uDOC:TITLE esUserIDValido
		//#uDOC:DESCR Checa si la string es un userid valido o no
		
		if(estringu == undefined) { return this.setError("imposible checar userid, no recibi string"); }
		if(estringu.match == undefined) { return this.setError("Imposible checar userid, no recibi string valida"); }
		if(estringu.length != 6) { return this.setError("UserID invalido. Todos los userids son de 6 caracteres"); }
		if(estringu.match(/^[a-z][a-z0-9]{5}$/)) {
			return true;
		} else {
			return false;
		}
		
	}
	
	stringTool.prototype.esLoginValido = function(estringu) {
		//#uDOC:TITLE esLoginValido
		//#uDOC:DESCR Indica si la string conforma a un login uServers valido
		if(estringu == undefined) { return this.setError("Login invalido: No recibi una string definida"); }
		if(estringu.match == undefined) { return this.setError("Login invalido, no recibi una string"); }
		if(estringu.match(/\s/)) { return this.setError("La cadena que forma el login no puede contener espacios"); }
		if(estringu.match(/^[-a-z0-9_\.]{2,125}$/)) {
			return true;
		} else {
			return this.setError("Login invalido, los caracteres validos son el alfabetao(a-z), los digitos (0-9), los guiones y el punto.");
		}
	}
	
	stringTool.prototype.esIPValida = function(string) {
		if(string == undefined) { return this.setError("Imposible comprobar IP, no recibi un valor"); }
		if(string == "") { return this.setError("Imposible comprobar IP, no recibi un valor"); }
		
		if(! string.match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/)) { return this.setError("El valor no es una direccion IP valida"); }
		
		// FIXME, checa que sean validos los octetos
		return true;
	}
	
	stringTool.prototype.checkPassword = function(passwdstring) {
		// #uDOC:TITLE checkPassword
		// #uDOC:DESCR Regresa true si es un password valido para servicios de usvx
		
		if(passwdstring.match(/^\d+$/)) { return this.setError("La clave de acceso no puede estar formada sólo de dígitos"); }
		if(passwdstring.length < 6) { return this.setError("La longitud de la contraseña tiene que ser mayor a 6 caracteres"); }
		if(! passwdstring.match(/\d/)) { return this.setError("La contraseña tiene que contener un dígito cuando menos"); }
	
		return true;
		
	}
	
	stringTool.prototype.esUUIDValido = function(stringu) {
		// #uDOC:TITLE esUUIDValido
		// #uDOC:DESCR Indica si la string es un UUID valido o no:
		
		if((stringu == undefined) || (stringu == "")) { return this.setError("String no es un UUID valido, recibi null string"); }
		if(stringu.length != 36) { return this.setError("String no es un UUID valido, la longitud no concuerda"); }
		
		if(stringu.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)) {
			return true;
		} else {
			return this.setError("String no es un UUID bien formado");
		}

	}
	
	stringTool.prototype.checkMailAddress = function(mailstring) {
		// #uDOC:TITLE checkMailAddress
		// #uDOC:DESCR Verifica suna string es una direccion de mail correcta
		return this.esMailValido(mailstring);
	}
	
	stringTool.prototype.checkUserIdent = function(identificador) {
		// #uDOC:TITLE checkUserIdent
		// #uDOC:DESCR Checa si una string es un identificador de3 user valido
		
	}
	
	stringTool.prototype.checkDomainName = function(name) {
		// #uDOC:TITLE checkDomainName
		// #uDOC:DESCR Checasi una string es un dominio valido
		
		if(name.match(/^[-\.a-z0-9]+\.[a-z]{2,8}$/i)) { return true; }
		else { return false; }
	}
	
	stringTool.prototype.ratePassword = function(pw) {
		//#uDOC:TITLE ratePassword
		//#uDOC:DESCR Califica una contrasena segun su fuerza
		
		if((pw == undefined) || (pw == "")) {
			return this.setError("Imposible juzgar password, no recibi string");
		}
		
		if((pw.length < 5) || (pw.match(/^\d+$/))) {
			return 0;
		} else {
			
			// Ya un punto
			var pts = 1;
			
			// Si es de siete o mas, ta mejor:
			if(pw.length >= 7) { pts++; }
			if(pw.length >= 10) { pts++; }
			if(pw.length >= 12) { pts++; }
			
			// Si tiene numeros, uno mas:
			if(pw.match(/\d+/)) { pts++; }
			
			// Si tiene no-alfanumericos, mejor
			if(! pw.match(/^[a-z0-9]+$/i)) { pts = pts + 2; }
			
			// Si tiene mayusculas, bueno uno mor:
			if(pw.match(/[A-Z]+/)) { pts++; }
			
			return pts;
		}
		
	}
	
	stringTool.prototype.stripAcentos = function(estringu) {
		// #uDOC:TITLE stripAcentos
		// #uDOC:DESCR Quita los acentos de una string y reemplazalos por vocales sin
		
		if(estringu == undefined) { return this.setError("Imposible reemplazar acentos, no recibi string"); }
		if(! estringu.replace) { return this.setError("Imposible reemplazar acentos, no recibi objecto de string"); }
		
		estringu = estringu.replace("á", "a");
		estringu = estringu.replace("é", "e");
		estringu = estringu.replace("í", "i");
		estringu = estringu.replace("ó", "ó");
		estringu = estringu.replace("ú", "u");
		estringu = estringu.replace("Á", "A");
		estringu = estringu.replace("É", "E");
		estringu = estringu.replace("Í", "I");
		estringu = estringu.replace("Ó", "O");
		estringu = estringu.replace("Ú", "U");
		estringu = estringu.replace("ñ", "n");
		estringu = estringu.replace("Ñ", "N");
		estringu = estringu.replace("ü", "u");
		estringu = estringu.replace("Ü", "U");
		
		return estringu;
		
	}
	
	
	
	

