// JavaScript Document
addEvent(window,'load',inicializarEventos,false);

function inicializarEventos()
{
  var ob = document.getElementById('BAR');
  addEvent(ob,'keyup',presionTecla,false);
}

var conexion1;
function presionTecla(e)
{
  bar=document.getElementById('BAR').value;
  if (bar.length > 1){
  conexion1=crearXMLHttpRequest();
  conexion1.onreadystatechange = procesarEventos;
  conexion1.open('GET','autocompletar.asp?casilla=bares&bar=' + bar, true);
  conexion1.send(null);
  }else{
	  if(document.getElementById('menu'))document.getElementById('menu').style.display='none';
	  
  }
}

function procesarEventos()
{
  var resultados = document.getElementById("resultados");
  if(conexion1.readyState == 4)
  {
    if (conexion1.status==200)
    {
      var xml = conexion1.responseXML;
      var pals=xml.getElementsByTagName('opcion');
	  var loc=xml.getElementsByTagName('localidad');
      cadena='<div id="menu"><ul>';      
      for(f=0;f<pals.length;f++)
      {
        cadena = cadena + '<li><a id_bar="'+pals[f].getAttribute("id_bar")+'" localidad="'+pals[f].getAttribute("localidad")+'" provincia="'+pals[f].getAttribute("provincia")+'" bar="'+pals[f].firstChild.nodeValue+'">' + pals[f].firstChild.nodeValue + '  ( '+pals[f].getAttribute("localidad")+' )</a></li>';  
      } 
      cadena = cadena + '</ul></div>';
      resultados.innerHTML = cadena; 
      var obj=document.getElementById("menu");
      var lista=obj.getElementsByTagName('a');
      for(f=0;f<lista.length;f++)
      {
        addEvent(lista[f],'click',seleccionPalabra,false);
      }
    }
    else
      alert(conexion1.statusText);
  } 
  else 
  {
    resultados.innerHTML = '<img src="images/cargandomini.gif">';
  }
}

function seleccionPalabra(e)
{
  var enlace;
  if (window.event)
    enlace=window.event.srcElement;
  else
    enlace=e.target;
	
	document.getElementById('BAR').value = enlace.getAttribute("bar");
	document.getElementById('LOCALIDAD').value = enlace.getAttribute("localidad");
	document.getElementById('PROVINCIA').value = enlace.getAttribute("provincia");
	document.getElementById('ID_BAR').value = enlace.getAttribute("id_bar");
  document.getElementById('menu').style.display='none';
}

//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on' + nomevento,funcion);
    return true;
  }
  else  
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

