
/* Array con los textos del idioma para todas las funciones de validación */
var txt_idioma= new Array(); 

	txt_idioma[0] = "El campo ";
	txt_idioma[1] = " no puede estar vacio";
	txt_idioma[2] = " debe contener 6 carácteres mínimo";
	txt_idioma[3] = " no puede contener espacios";
	txt_idioma[4] = " contiene carácteres no válidos";
	txt_idioma[5] = " debe ser de tipo texto";
	txt_idioma[6] = " debe ser de tipo alfanumúrico";
	txt_idioma[7] = "El E-Mail introducido no es correcto";
	txt_idioma[8] = "El ";
	txt_idioma[9] = " introducido no es correcto";
	txt_idioma[10] = " debe contener 9 dígitos mínimo";
	txt_idioma[11] = " debe ser de tipo numérico";
	txt_idioma[12] = " no puede contener más de ";
	txt_idioma[13] = " caracteres";
	txt_idioma[14] = " debe seguir el siguiente patrón: 'dd-mm-aaaa'";
	txt_idioma[15] = " debe contener números";
	txt_idioma[16] = "La fecha introducida no es válida";
	txt_idioma[17] = "El nombre de archivo ";
	txt_idioma[18] = " no es válido";
	txt_idioma[19] = " debe ser de un número decimal válido";
	txt_idioma[20] = "La URL (dirección web) introducida no es válida";

/****************************************************************/
/*		Función genérica para la validación de formularios 		*/
/****************************************************************/
	
	function validarFormulario(f){
		if(validar(f))
			f.submit();
	}

	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(txt_idioma[0] + f.elements[n].title + txt_idioma[1]);
					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=="url"){				// URL web
					
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0)){
						
							if(!validaUrl(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;
						}
						
					}else if(tipoCampo=="ndc"){				// Números decimales
					
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0)){
						
							if(!validaNdc(f.elements[n],f.elements[n].title))
								return false;
						}
						
					}else if(tipoCampo=="img"){				// Imagen 
					
						if(obligatorio || (!obligatorio && f.elements[n].value.length>0))
						
							if(!validaImg(f.elements[n],f.elements[n].title))
							
								return false;
					}
				}
			}
		}
		return true;
	}
	
	function validaLogin(campo,nombreCampo){
		var CARACTERES = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_&.";
		
		if(campo.value.length<6 || campo.value.indexOf(" ")!=-1){
			if(campo.value.length<6)
				alert(txt_idioma[0]+nombreCampo+txt_idioma[2]);
			else
				alert(txt_idioma[0]+nombreCampo+txt_idioma[3]);
			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(txt_idioma[0]+nombreCampo+txt_idioma[4]);
					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(txt_idioma[0]+nombreCampo+txt_idioma[5]);
				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 ( (campo.value.charCodeAt(i) != 13) && (campo.value.charCodeAt(i) != 10) && (ALFANUMERICO.indexOf(temp) == -1)){
				alert(txt_idioma[0]+nombreCampo+txt_idioma[6]);
				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 ( (campo.value.charCodeAt(i) != 13) && (campo.value.charCodeAt(i) != 10) && (ALFANUMERICO_TXA.indexOf(temp) == -1)){
				alert(txt_idioma[0]+nombreCampo+txt_idioma[6]);
				campo.focus();
				campo.select();
				return false;
			}
		}
		return true;
	}
	
	function validaEml(campo,nombreCampo){
		var CARACTERES = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_&.";
		
		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(txt_idioma[7]);
			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(txt_idioma[0]+nombreCampo+txt_idioma[4]);
					campo.focus();
					campo.select();
					return false;
				}
			}
		}
		return true;
	}
	
	function validaUrl(campo,nombreCampo){
		
		//if (!/^(http:|https:|ftp:)\/\/\w+(\.\w+)*(\-\w+)?\.\w{2,3}(\:\d{2,6})?(\/{1,2}(\:|\-|\w|\.|\?|\/|\=|\&|\%|\@|\\|\,)*)?$/.test(campo.value)){
		// Sin http:
		if (!/\w+(\.\w+)*(\-\w+)?\.\w{2,3}(\:\d{2,6})?(\/{1,2}(\:|\-|\w|\.|\?|\/|\=|\&|\%|\@|\\|\,)*)?$/.test(campo.value)){

			alert(txt_idioma[20]);
			campo.focus();
			campo.select();
		
			return false;	
		}
		return true;	
	}
	
	function validaTlf(campo,nombreCampo){
		var DIGITOS = "1234567890";
		var ESPECIALES = "() /.+-";
		
		if(campo.value.length<9){
			alert(txt_idioma[8]+nombreCampo+txt_idioma[9]);
			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(txt_idioma[0]+nombreCampo+txt_idioma[4]);
				campo.focus();
				campo.select();
				return false;
			}
		}
		if(num<9){
			alert(txt_idioma[8]+nombreCampo+txt_idioma[10]);
			campo.focus();
			campo.select();
			return false;
		}
		return true;
	}
		
	function validaNdc(campo,nombreCampo){
		var NUMEROS = "1234567890";
		var SEPARADOR = ".";
		
		var valor = campo.value.split(SEPARADOR);
//		Decimal no valido
		if((valor.length!=2) || 
			((campo.value.substring(campo.value.length-1, campo.value.length)==SEPARADOR) || (campo.value.substring(0,1)==SEPARADOR))){
			alert(txt_idioma[0]+nombreCampo+txt_idioma[19]);
			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!=SEPARADOR) && (NUMEROS.indexOf(temp) == -1)){
				alert(txt_idioma[0]+nombreCampo+txt_idioma[19]);
				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(txt_idioma[0]+nombreCampo+txt_idioma[11]);
				campo.focus();
				campo.select();
				return false;
			}
		}
		return true;
	}
	
	function validaImg(campo,nombreCampo){
		var i = campo.value.length-4;
		var temp = campo.value.substring(i, i+1);
		if(temp != "."){
			alert(txt_idioma[17]+nombreCampo+txt_idioma[18]);
			campo.select();
			return false;
		}
		var ext = campo.value.substring(i+1, i+4);
		ext = ext.toLowerCase();
		// all = todos; gif - jpg - png
		if(campo.alt != "all"){
			if(ext != campo.alt){
				alert(txt_idioma[17]+nombreCampo+txt_idioma[18]);
				campo.select();
				return false;
			}
		} else {
			if((ext != "gif") && (ext != "jpg") && (ext != "png")){
				alert(txt_idioma[17]+nombreCampo+txt_idioma[18]);
				campo.select();
				return false;
			}
		}
		return true;
	}
	
	function limiteCorrecto(campo,nombreCampo){		// Control de caracteres de textarea
		if(campo.maxlength<campo.value.length){
			alert(txt_idioma[0]+nombreCampo+txt_idioma[12]+campo.maxlength+txt_idioma[13]);
			campo.focus();
			campo.select();
			return false;		
		}
		return true;
	}

	function validaFecha(campo,nombreCampo){
		var NUMEROS = "1234567890";

		var temp = campo.value.split("-");
		if(campo.value.length<8 || temp.length!=3){
			alert(txt_idioma[0]+nombreCampo+txt_idioma[14]);
			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(temp != "-"){
					if(NUMEROS.indexOf(temp) == -1){
						alert(txt_idioma[0]+nombreCampo+txt_idioma[15]);
						campo.focus();
						campo.select();
						return false;
					}
				}
			}
			var fchTemp = new objFecha(campo.value);

			if(!fchTemp.valida){
				alert(txt_idioma[16]);
				campo.focus();
				campo.select();
				return false;
			}
			return true;
		}
	}

	// hay que pasarle una cadena con el siguiente formato dd-mm-aaaa,
	// si la fecha pasada como parametro no es valida devuelve false
	function objFecha(keFecha){
		var fecha = keFecha.split("-");
		this.dia = fecha[0];
		this.mes = fecha[1];
		this.anyo = fecha[2];
		if (fecha.length == 4){
			var keHora = fecha[3];
			var hora = keHora.split(":");
			this.hora = hora[0];
			this.minutos = hora[1];
		}else{
			this.hora = null;
			this.minutos = null;
		}
		this.diasMes = dimeDias(this.mes, this.anyo);
		this.anyoBisiesto = esBisiesto(this.anyo);
		this.valida = esValida(this.diasMes, this.dia, this.mes, this.anyo);
	}
	
	function esValida(diasMes, dia, mes, anyo){

		if(dia==0 || mes==0 || anyo==0)
		{
			return false;
		}
		else
		{
			if ((dia < 1) || (dia > diasMes))
				return false;
			if ((mes < 1) || (mes > 12))
				return false;
			if (anyo.length != 4)
				return false;
		}
		return true;
	}
	
	function esBisiesto(anno){
		if((((anno%4) == 0) || ((anno%100) == 0)) && ((anno%400) != 0)){
			return true;
		}else{
			if (anno == 2000){
				return true;
			}else{
				return false;
			}
		}
	}
	
	function dimeDias(mes, anno){
		var dias = 0;
		if((mes == 1) || (mes == 3) || (mes == 5) || (mes == 7) || (mes == 8) || (mes == 10) || (mes == 12)){
			dias = 31;
		} else {
			dias = 30;
			if(mes == 2){
				if (esBisiesto(anno)){
					dias = 29;
				}else{
					dias = 28;
				}
			}
		}
		return dias;
	}
	
	function fechaToMysql(fecha){
		var fTemp = fecha.split("-");
		return fTemp[2]+"-"+fTemp[1]+"-"+fTemp[0];
	}