function buscar()
{
	if (document.fBuscadorFacil.id_localizacion.value == '')	
	{
		alert('No hay localización donde buscar');
		return false
	}
	else return true;
}

function mos(div)
{
	obj = document.getElementById(div);
	obj.style.display = 'block'
}

function oc(div)
{
	obj = document.getElementById(div);
	obj.style.display = 'none';
	lay = -1;
}

function mosoc(div)
{
	obj = document.getElementById(div);
	if (obj != null)
	{
		if (obj.style.display == 'none')
			obj.style.display = 'block'
		else
			obj.style.display = 'none'
	}	
	oc('predictivoFacil');
}

function predic(l)
{
	num_enlaces = 0
	
	var head = document.getElementsByTagName('head')[0];
	script = document.getElementById('extraScript');
	if (script != null)
		head.removeChild(script);
    script = document.createElement('script');
    script.id = 'extraScript';
    script.type = 'text/javascript';
    script.src = 'http://www.expocasa.es/elconfidencial/070517/predictivo.cfm?l=' + l 
    head.appendChild(script)
}

function presionarTecla(letras, e)
{	
	teclas = {"38":1,"40":2,"13":3, "27":4, "8": 5, "46":6}
	if(window.event)
		key = e.keyCode;
	else if(e.which) 
		key = e.which;
		
	if ((letras != '') && !(key in teclas))
	{
		predic(letras);
	}
	else
	{
		if (key == 8)
			predic(letras);
		else if (key == 40)
			IrAbajo();	
		else if (key == 38)
			IrArriba();
		else  if (key == 13)
			return false;
		else if (key == 27)
				ocultaPredictivo();
		else if (letras == '')
				ocultaPredictivo();
	}
}

function IrAbajo()
{
	if (posicion < (num_enlaces - 1))	
	{
		posicion++;
		PintarEnlace();
	}
}

// IrArriba
function IrArriba()
{
	if (posicion != 0)	
	{
		posicion--;
		PintarEnlace();
	}
}

function PintarEnlace()
{
	for (i = 0; i < num_enlaces; i++)
	{
		if (i == posicion)
		{
			document.getElementById('enlace_' + i).className = "seleccionado";			
			document.fBuscadorFacil.nombrepoblacion.value = obj.data.nombre[i];
			document.fBuscadorFacil.id_localizacion.value = obj.data.id[i];
		}
		else document.getElementById('enlace_' + i).className = '';
	}	
}

function seleccionarEnlace(paso)
{
	posicion = paso;
	PintarEnlace();
	posicion = -1;
	ocultaPredictivo();
}

function llegada()
{
	s = resultado;
	a = s.split('$');
	if (a[0] != 'error')
	{
		obj = eval('(' + a[0] + ')');
		s = '';
		num_enlaces = obj.recordcount;
		if (num_enlaces > 0)
		{
			for (j = 0; j < obj.recordcount; j++)
			{
				s += '<a href="javascript:void" onclick="seleccionarEnlace(' + j + ')" onmousemove="posicion = ' + j + '" id="enlace_' + j + '">' + obj.data.nombre[j].toString() + '</a><br>'
			}
			o = document.getElementById('predictivoFacil');
			o.innerHTML = s;
			o.style.display = 'block';
		}
		else ocultaPredictivo()
	}
	else error();
}

function ocultaPredictivo()
{
	if (posicion == -1)	
	{
		o = document.getElementById('predictivoFacil');
		o.innerHTML = '';
		o.style.display = 'none';
	}
}

function error()
{
	alert('error');
	ocultaPredictivo();
}

var obj = null;
var posicion = -1;
var num_enlace = 0;
var lay= -1;