/*		Creado por: Luis M. Montesinos - luis@yatdigo.com 		*/
/****************************************************************/
/*		Funciones de uso general para validar formularios		*/
/****************************************************************/
	
	function jsTrim(str){
	// Elimina los espacios de inicio y final de una cadena de caracteres
		for(var i=0; i<str.length;)	{
			if(str.charAt(i)==" ")
				str=str.substring(i+1, str.length);
			else
				break;
		}
	
		for(var i=str.length-1; i>=0; i=str.length-1){
			if(str.charAt(i)==" ")
				str=str.substring(0,i);
			else
				break;
		}
		return str;
	}
	
	function quitarEspacios(str){
	// Elimina los espacios interiores sobrantes de una cadena
		var aux = "";
		for(var i=0; i<str.length; i++){
			if(str.charAt(i)!=" " || (str.charAt(i)==" " && str.charAt(i-1)!=" "))
				aux+= str.charAt(i);
		}
		return aux;
	}
	
	function validar(f){
	// Validación genérica de formularios para campos text, password y textArea
	// Utiliza las propiedades del formulario: name, type, title, maxlength
		for(var n=0; n<f.elements.length; n++){
			if((f.elements[n].type == "text") || (f.elements[n].type == "password") || (f.elements[n].type == "textarea")){
				var tipoCampo = f.elements[n].name.substring(0,3);
				var obligatorio = (f.elements[n].name.substring(3,5) == "Ob");
				f.elements[n].value = jsTrim(f.elements[n].value);
				if(f.elements[n].value.length<1 && obligatorio){
					alert("El campo "+f.elements[n].title+" no puede estar vacio");
					f.elements[n].focus();
					return false;
				}else{
					if(tipoCampo=="log" || tipoCampo=="psw"){	// Login o Password
						if(!validaLogin(f.elements[n],f.elements[n].title))
							return false;
					}else if(tipoCampo=="txt"){				// Texto 
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0))
							if(!validaTxt(f.elements[n],f.elements[n].title))
								return false;
					}else if(tipoCampo=="txa"){				// TextArea
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0)){
							if(!validaTxa(f.elements[n],f.elements[n].title))
								return false;
							if(!limiteCorrecto(f.elements[n],f.elements[n].title))
								return false;
						}
					}else if(tipoCampo=="alf"){				// Alfanumérico
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0))
							if(!validaAlf(f.elements[n],f.elements[n].title))
								return false;
					}else if(tipoCampo=="fch"){				// Fecha
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0))
							if(!validaFecha(f.elements[n],f.elements[n].title))
								return false;
					}else if(tipoCampo=="eml"){				// eMail
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0)){
							if(!validaEml(f.elements[n],f.elements[n].title))
								return false;
						}
					}else if(tipoCampo=="tlf"){				// Teléfono o Fax
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0)){
							if(!validaTlf(f.elements[n],f.elements[n].title))
								return false;
						}
					}else if(tipoCampo=="num"){				// Números
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0)){
							if(!validaNum(f.elements[n],f.elements[n].title))
								return false;
						}
					}
				}
			}
		}
		return true;
	}
	
	function validaLogin(campo,nombreCampo){
		var CARACTERES = "1234567890abcdefghijklmnñopqrstuvwxyzáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ-_&";
		
		if(campo.value.length<6 || campo.value.indexOf(" ")!=-1){
			if(campo.value.length<6)
				alert("El campo "+nombreCampo+" debe contener 6 carácteres mínimo");
			else
				alert("El campo "+nombreCampo+" no puede contener espacios");
			campo.focus();
			campo.select();
			return false;
		}else{
			for(var i=0; i<campo.value.length; i++){
				var temp = campo.value.substring(i, i+1);
				if(CARACTERES.indexOf(temp) == -1){
					alert("El campo "+nombreCampo+" contiene carácteres no válidos");
					campo.focus();
					campo.select();
					return false;
				}
			}
		}
		return true;
	}
	
	function validaTxt(campo,nombreCampo){
		var TEXTO = " abcdefghijklmnñopqrstuvwxyzáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ-_&~,.";
		
		for(var i=0; i<campo.value.length; i++){
			var temp = campo.value.substring(i, i+1);
			if(TEXTO.indexOf(temp) == -1){
				alert("El campo "+nombreCampo+" debe ser de tipo texto");
				campo.focus();
				campo.select();
				return false;
			}
		}
		return true;
	}
	
	function validaAlf(campo,nombreCampo){
		var ALFANUMERICO = "1234567890 abcdefghijklmnñopqrstuvwxyzáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ-_&()[]@.,:;´`^¡!¿?=%$ºª+*{}çÇ<>/\"";
		
		for(var i=0; i<campo.value.length; i++){
			var temp = campo.value.substring(i, i+1);
			if(ALFANUMERICO.indexOf(temp) == -1){
				alert("El campo "+nombreCampo+" debe ser de tipo alfanumérico");
				campo.focus();
				campo.select();
				return false;
			}
		}
		return true;
	}
	
	function validaTxa(campo,nombreCampo){
		var ALFANUMERICO_TXA = "1234567890 abcdefghijklmnñopqrstuvwxyzáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ-_&()[]@.,:;´`^¡!¿?=%$ºª+*{}çÇ<>/\"";
		
		for(var i=0; i<campo.value.length; i++){
			var temp = campo.value.substring(i, i+1);
			if(ALFANUMERICO_TXA.indexOf(temp) == -1){
				alert("El campo "+nombreCampo+" debe ser de tipo alfanumérico");
				campo.focus();
				campo.select();
				return false;
			}
		}
		return true;
	}
	
	function validaEml(campo,nombreCampo){
		var CARACTERES = "1234567890abcdefghijklmnopqrstuvwxyzáéíóúABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚ-_&.";
		
		var posArroba = campo.value.indexOf("@");
		var posPunto = campo.value.lastIndexOf(".");
		if(posArroba==-1 || posPunto==-1 || posPunto<posArroba || (posPunto==(posArroba+1)) || campo.value.length<6 || (posPunto<(campo.value.length-4))){
			alert("El E-Mail introducido no es correcto");
			campo.focus();
			campo.select();
			return false;
		}
		for(var i=0; i<campo.value.length; i++){
			var temp = campo.value.substring(i, i+1);
			if(temp!="@" && temp!="."){
				if(CARACTERES.indexOf(temp) == -1){
					alert("El campo "+nombreCampo+" contiene carácteres no válidos");
					campo.focus();
					campo.select();
					return false;
				}
			}
		}
		return true;
	}
	
	function validaTlf(campo,nombreCampo){
		var DIGITOS = "1234567890";
		var ESPECIALES = "() /.+-";
		
		if(campo.value.length<9){
			alert("El "+nombreCampo+" introducido no es correcto");
			campo.focus();
			campo.select();
			return false;
		}
		var num = 0;
		for(var i=0; i<campo.value.length; i++){
			var temp = campo.value.substring(i, i+1);
			if(DIGITOS.indexOf(temp) != -1)
				num++;
			else if(ESPECIALES.indexOf(temp) == -1){
				alert("El campo "+nombreCampo+" contiene carácteres no válidos");
				campo.focus();
				campo.select();
				return false;
			}
		}
		if(num<9){
			alert("El "+nombreCampo+" debe contener 9 dígitos mínimo");
			campo.focus();
			campo.select();
			return false;
		}
		return true;
	}
	
	function validaNum(campo,nombreCampo){
		var NUMEROS = "1234567890";
		
		for(var i=0; i<campo.value.length; i++){
			var temp = campo.value.substring(i, i+1);
			if(NUMEROS.indexOf(temp) == -1){
				alert("El campo "+nombreCampo+" debe ser de tipo numérico");
				campo.focus();
				campo.select();
				return false;
			}
		}
		return true;
	}
	
	function limiteCorrecto(campo,nombreCampo){		// Control de caracteres de textarea
		if(campo.maxlength<campo.value.length){
			alert("El campo "+nombreCampo+" no puede contener más de "+campo.maxlength+" caracteres");
			campo.focus();
			campo.select();
			return false;		
		}
		return true;
	}

