function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
}

function stateChanged()
{
    if (xmlhttp.readyState==4)
    {                           
        var a = xmlhttp.responseText;
        municipios = a.split("<br>");        
		municipios = limpiarRepetidos(municipios);		
		$("#destination").autocomplete(municipios, { max: 6 , mustMatch:true, matchContains:true }  );
    }
		
}

function pedirMunicipios()
{  
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      }    
    var url="http://www.buscadestinos.com/search_suggestAutocompletar.asp";         
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);   
}

//función que elimina los elementos repetidos de un array
function limpiarRepetidos( b ) {
    var a = [];
    for( i=0; i<b.length; i++ ) 
    {
        repetido=false;
        //comprobar si ya esta en a
        for( j=0; j<a.length; j++ ) 
        {                        
            if(b[i] == a[j])
            {
                repetido = true;
            }            
        }
        if(!repetido)
        {
            a.push(b[i]);
        }            
    }
    return a;
};


//************************************************************************************************************************************
   
//cargar autocompletar de municipios y poner foco en el campo nombre      
$(document).ready(function()
{   
    //obtener los municipios	
	pedirMunicipios();
	
});
