var IE4 = ((navigator.userAgent.indexOf("MSIE")!=-1) && navigator.appVersion.substring(0,1) >= 4);	

ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false

function clikker(a) {
	if(IE4) {
		if (a.style.display =='') {
			a.style.display = 'none';
		}
		else {
			a.style.display='';
		}
	}
	else {
		if (a.style.visibility == "visible")
		{
			a.style.visibility = "hidden";
			a.style.display = "none";
		}
		else
		{
			a.style.visibility = "visible";
			a.style.display = "block";
		}
	}
}

function agregarFavoritos(direccion,titulo){
   if ((navigator.appName=="Microsoft Internet Explorer") && 
         (parseInt(navigator.appVersion)>=4)) {
      window.external.AddFavorite(direccion,titulo);
   } else { 
         alert("Presione Crtl+D para agregar este sitio a sus favoritos"); 
   }
} 

function esNumero(campo)
{
	esValido=false;
	arrayCaracteresPermitidos = new Array('0','1','2','3','4','5','6','7','8','9');
	for (f = 0; f < campo.length; f++)
	{     
		caracterRecibido=campo.charAt(f);
		esValido=false;
		for(x=0; x<arrayCaracteresPermitidos.length;x++)
		{
			//y ahora comparo
			if(caracterRecibido==arrayCaracteresPermitidos[x])
			{
				esValido = true;
				break;
			}
		}
		if(!esValido)// Si el caracter recibido coincide con alguno del array será true.
		{
			return false;
			break;
		}
	}
	if(!esValido)// Si el caracter recibido coincide con alguno del array será true.
	{
		return false;
	}
	else return true;
}

function ValidarCorreo(email)
{ 
	var foundError = false; 
	var contador = 0; 
	var posicion = 1;
	var encontrada = false;
	var posicionpunto = 0;
	var posicionarroba = 0;
	var uno = "";
	var ultimo = 0;  
	var longitud = 0;
	var arrobaux = 0;
	var uno;
	uno = " ";
         
	// Bloque que quita los blancos por la derecha y por la izquierda
	sinblancos = Trim(email);   
   
	// Bloque para comprobar que no hay blancos entre medias de la cadena
	for (x=0 ; x < sinblancos.length ; x++) 
	{
		uno = sinblancos.substring(x, x +1); 
		if (uno.charAt() == " ")
		{ 
			foundError = true;
		} 
	}
  
	// Bloque para controlar que el primer caracter no sea una arroba
	if (foundError == false && sinblancos.charAt(0) == "@" ) 
	{ 
		foundError = true;              
	}

	// Bloque para controlar que el primer caracter no sea un punto
	if (foundError == false && sinblancos.charAt(0) == "." ) 
	{ 
		foundError = true;              
	}
	
	//Bloque para controlar que el ultimo caracter no sea un punto
	if (foundError == false) 
	{     
		longitud = sinblancos.length;
		if (sinblancos.charAt(longitud - 1) == ".")
		{
			foundError = true;      
		} 
	} 
	
	//Bloque para controlar que el ultimo caracter no sea una arroba
	if (foundError == false) 
	{     
		longitud = sinblancos.length;
		if (sinblancos.charAt(longitud - 1) == "@")
		{
			foundError = true;      
		} 
	} 

	// Bloque para comprobar que no hay mas de una arroba
	if (foundError == false) 
	{
		for (x=0 ; x < sinblancos.length ; x++) 
		{
			uno = sinblancos.substring(x, x +1); 
			if (uno == "@")
			{ 
				contador++;
			} 
		}
    
		if (contador < 1 || contador > 1 )      
		{
			foundError = true;
		}
	} 
	// Bloque para comprobar que al menos hay un punto
	if (foundError == false) 
	{ 
		var contador=0;
		for (x=0 ; x < sinblancos.length ; x++) 
		{
			uno = sinblancos.substring(x, x +1); 
			if (uno.charAt() == ".")
			{ 
				contador = contador + 1;
			} 
   	}
		if (contador == 0)      
		{      
			foundError = true;
		}
	} 

	// Bloque para comprobar el sitio donde esta la arroba 
	if (foundError == false) 
	{
		for (x=0 ; x < sinblancos.length ; x++) 
		{
			uno = sinblancos.substring(x, x +1); 
			if (uno.charAt() == "@")
			{
				encontrada = true; 
				posicionarroba = posicion;
				break; 
			}
   
			if (encontrada == false)
			{  
				posicion = posicion + 1; 
			} 
		} 
	} 
	var HayPuntoDespArroba='N';    

	// Bloque para controlar si hay un punto tras la arroba y caracteres entre punto y punto
	if (foundError == false) 
	{
		posicion = 1;
		uno = "";
		for (x=0 ; x < sinblancos.length ; x++) 
		{
			uno = sinblancos.substring(x, x +1); 
			if (uno.charAt() == ".")
			{
				posicionpunto = posicion; 
				if ((posicionpunto - 1) == posicionarroba)
				{        
					foundError = true; 
				}   
				if (posicion - 1 == ultimo)
				{
					foundError = true; 
				}
			} 
			posicion = posicion +1 ;
			ultimo = posicionpunto;
		}
		var buscaPunto=sinblancos.indexOf('.',posicionarroba);
		if(buscaPunto!='-1')
		{
			HayPuntoDespArroba='S';
		}
		if ( HayPuntoDespArroba=='N')
		{
			foundError = true; 
		}
	}
	if (foundError)
	{	
		return false;
	}
	else
		return true; 
}

function Trim(str)
{
	return LTrim(RTrim(str));
}

function LTrim(str)
{
	var whitespace = new String("\t\n\r");
	var s = new String(str);
	
	if(whitespace.indexOf(s.charAt(0))!= -1)
	{
		var j = 0,i=s.length;
		while(j<i&&whitespace.indexOf(s.charAt(j))!= -1) j++
			s = s.substring(j,i);
	}
	return s;
}

function RTrim(str)
{
	var whitespace = new String("\t\n\r");
	var s = new String(str);
	
	if(whitespace.indexOf(s.charAt(s.length-1))!= -1)
	{	
		var i = s.length-1;
		while(i>0&&whitespace.indexOf(s.charAt(i))!=-1) i--
			 s = s.substring(0,i+1);
	}
	return s;
}

function validarNIF(nif) {
	letra=nif.substring(nif.length, nif.length-1);
	dni=nif.substring(0, nif.length-1);
	var valida=true;
	var tabla=new Array('T','R','W','A','G','M','Y','F','P','D','X','B','N','J','Z','S','Q','V','H','L','C','K','E');
	var esnum = esNumero(dni);
	if(esnum==false)
	{
		valida=false;
		//alert("El NIF / NIE es incorrecto");
		return valida;
	}
	if(!validarNum(dni))
	{
		valida=false;
		//alert("El NIF / NIE es incorrecto");
		return valida;
	}
	if(valida) 
	{
		var num=dni%23;
		if(letra.toUpperCase()!=tabla[num]) 
		{
			valida=false;
			//alert("El NIF / NIE es incorrecto");
			return valida;
		}
	}
	return valida;
}

function validarNum(cosa)
{
	for(f=0;f<cosa.length;f++) 
	{
		if(cosa.charCodeAt[f]<48||cosa.charCodeAt[f]>57)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}


function muestra_capa(id)
{
	if (ie4) {
		capa = document.all(id);
		obj = capa.style;
		obj.display = "inline";
	}
	else{
		obj = document.getElementById(id);
		obj.style.visibility = "visible";
		obj.style.display = "inline";
	}
}

function oculta_capa(id)
{
	if (ie4) {
		capa = document.all(id);
		obj = capa.style;
		obj.display = "none";
	}
	else{
		obj = document.getElementById(id);
		obj.style.visibility = "hidden";
		obj.style.display = "none";
	}
}

function selecciona_opcion(id)
{
	//teniendo varias capas, solo una visible, pasamos de una a otra haciendo asi una navegacion entre capas sin recargar nada
	capas=document.getElementsByTagName("div");	
	for (i=0; i<capas.length;i++)
	{
		capa=capas[i].id;
		if(capa.indexOf("datos_")==0)
		{
			if (capa==id)
			{
				muestra_capa(capa);
			}
			else
			{
				oculta_capa(capa);
			}
		}
	}
} 


function campoRelleno(campo)
{
	dato=eval(campo+".value");
	if (dato=="")
		return false;
	else return true;
}

function enviarClave(pagina)
{
	document.frmClaves.nif.value=document.frmValidacion.nif.value;
	document.frmClaves.submit();
}

function validaContacto()
{
	error="";
	for(i=0;i<document.frmContacto.length;i++)
	{
		elemento=document.frmContacto.elements[i];
		if (error != "") continue;

		switch (elemento.name)
		{
			case "Nombre":
				if(elemento.value=="")
				{
					error="Debe rellenar el campo Nombre";
					foco=elemento;
				}
			break;
			case "Email":
				if(elemento.value=="")
				{
					error="Debe rellenar el campo E-Mail";
					foco=elemento;
				}
				else if(!ValidarCorreo(elemento.value))
				{
					error="El E-Mail indicado no es correcto";
					foco=elemento;
				}
			break;
			case "Telefono":
				if(elemento.value=="")
				{
					error="Debe rellenar el campo Teléfono";
					foco=elemento;
				}
				else if(!esNumero(elemento.value) || elemento.value.length!="9")
				{
					error="El Teléfono indicado no es correcto";
					foco=elemento;
				}
			break;
			case "Asunto":
				if(elemento.value=="")
				{
					error="Debe rellenar el campo Asunto";
					foco=elemento;
				}
			break;
			case "Consulta":
				if(elemento.value=="" || elemento.value=="Escriba aquí su consulta")
				{
					error="Debe rellenar el campo Consulta";
					foco=elemento;
				}
			break;
		}
	}
	
	if(error!="")
	{
		alert(error);
		foco.focus();
		return false;
	}
	else
		return true;
}

function rayarTbl(id)
{
  var normal = false;
  var normalClass = arguments[1] ? arguments[1] : "rayadoclaro";
  var otraClass = arguments[2] ? arguments[2] : "rayadonormal";

	var tables = document.getElementsByClassName(id);

	for (var t = 0; t < tables.length; t++) {
		var tbodies = tables[t].getElementsByTagName("tbody");
		for (var h = 0; h < tbodies.length; h++) {
			var trs = tbodies[h].getElementsByTagName("tr");
			for (var i = 0; i < trs.length; i++) {
				if (! hasClass(trs[i]) ) {
					trs[i].className =  normal ? normalClass : otraClass;
			 }
			// cambiar entre otra y normal, o vice-versa
				normal =  ! normal;
			}
		}
	}
}

document.getElementsByClassName = function (dato)
{
  var         my_array = document.getElementsByTagName("*");
  var         resultados = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + dato + " ") != -1)
      resultados[j++] = my_array[i];
  }
  return resultados;
}

function hasClass(obj) {
     var result = false;
     if (obj.className) {
         result = obj.className;
     }
     return result;
  }   
