// JavaScript Document
var Validar = {
	
	vacio:function(str)
	{
		if (str=='')
			return true;
		else
			return false;
	},
	
	password: function(strPwd)
	{
		//(obligatorio) 1 minúscula
		//(obligatorio) 1 mayúscula
		//(obligatorio) 1 número
		//(obligatorio) no más de 3 caracteres iguales seguidos
		//(opcional) caracteres especiales aceptados: !#$&()*,./:;@\[]^_`{|}~
	
		var retorno=false;
		var re_str = /^(?!.*(.)\1{3})((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]))^([a-zA-Z0-9¡!#$&()*.\/:;@\\\[\]^_`{|}~]{8,})*$/
		var re = new RegExp(re_str);

		try{ if (strPwd.match(re)) retorno=true; } 
		catch(err) {retorno=false; }

		return ( retorno );
	},

	email: function(strEmail) { //Comprueba el formato de una dirección de email
		if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
			return true;
		else
			return false;
	},
	
	letras: function(strLetras){ //Comprueba que un campo solo tenga letras
		if (strLetras.search(/^[a-zA-Z-áéíóúÁÉÍÓÚñÑçÇ]+[a-zA-Z-áéíóúÁÉÍÓÚñÑçÇ_,\. ]+$/) != -1)
			return true;
		else
			return false;
	},
	
	numero: function(strNumero){ //Comprueba que el campo tiene un valor numerico
		if (strNumero.search(/^[0-9]+$/) != -1)
			return true;
		else
			return false;
	},
	
	longitud: function(strCadena,maximo,minimo){//Comprueba la longitud de un campo para ver si está entre max y el minimo
		if (strCadena.length>=minimo && strCadena.length<=maximo)
			return true;
		else
			return false;
	},
	
	telefono: function(strNumero){
		if (esNumero(strNumero)==true && parseInt(strNumero.charAt(0))==9 && noCorto(strNumero,9,9)==true)
			return true;
		else 
			return false;
	},
	
	movil: function(strNumero){
		if (esNumero(strNumero)==true && parseInt(strNumero.charAt(0))==6 && noCorto(strNumero,9,9)==true)
			return true;
		else 
			return false;
	},
	
	nif: function(abc){
		abc = abc.toUpperCase();
		var dni
		var dniValid
		var nifLetter
		var dnino
		var nifNo
		var nieLetter
		
		if (abc.length >= 4) { //Cambiar a 4 cuando todo este OK
			
			dni = abc;
			dniValid = true;
			nifLetter=dni.substring(dni.length-1,dni.length);
			
			if ((abc.charAt(0) == "X") || (abc.charAt(0) == "Y") || (abc.charAt(0) == "Z")) {
				dnino = dni.substring(0,dni.length-1).substring(1);
				nieLetter = dni.substring(0,1);
				if (nieLetter == "Y"){
					dnino = "1"+dnino;
				}
				if (nieLetter == "Z"){
					dnino = "2"+dnino;
				}
			}else if (abc.charAt(0) == "P") {
				return true;
			}else{
				dnino = dni.substring(0,dni.length-1);
			}
			
			//alert (dni);
			//alert (dnino);
			//alert (nifLetter);
			//alert (nieLetter);
			
			if (!isNaN(Number(nifLetter))){
				//alert("false1")
				return false;
			}else {
				var cadena="TRWAGMYFPDXBNJZSQVHLCKET"
				var posicion = Number(dnino) % 23;
				var letra = cadena.substring(posicion,posicion+1)
				if (letra!=nifLetter.toUpperCase()){
					//alert("false2")
					return false;
				}else{
					//alert("true")
					return true;
				}
			}
		} else {
			//alert("false3")
			return false
		}
	
	}
}

