var outp;
var Search_Destination;
var oldins;
var posi = -1;
var words = 10;
var input;
var key;

function Autocomplete(Destination)
{
	window.document.getElementById('City').value = '';
	window.document.getElementById('Country').value = '';
	window.document.getElementById('Id_City').value = '';
	window.document.getElementById('Id_Country').value = '';
	
	Search_Destination = document.getElementById(Destination);
	
	init(Destination);
	//Ajax_Send_Data(Search_Destination.name+'='+Search_Destination.value,'Ajax/Ajx_Autocomplete.php');
}

function Destination_Selected(Place, City, Country, Id_City, Id_Country)
{
	window.document.getElementById('Destination').value = Place;
	window.document.getElementById('Autocomplete').style.display = 'none';
	window.document.getElementById('City').value = City;
	window.document.getElementById('Country').value = Country;
	window.document.getElementById('Id_City').value = Id_City;
	window.document.getElementById('Id_Country').value = Id_Country;
}

function init(Destination){
	outp = document.getElementById("Autocomplete");
	//window.setInterval("lookAt()", 100);
	//setVisible("hidden");
	document.getElementById(Destination).onkeydown = keygetter; //needed for Opera...
	document.getElementById(Destination).onkeyup = keyHandler;
}

function setColor (_posi, _color, _forg){
	outp.childNodes[_posi].style.background = _color;
	outp.childNodes[_posi].style.color = _forg;
}

function keygetter(event){
	if (!event && window.event) event = window.event;
	if (event) key = event.keyCode;
	else key = event.which;
}
	

function keyHandler(event){
	var textfield = document.getElementsByName("Destination")[0];
	if (key == 40){ //Key down
		//alert (words);
		if (outp.childNodes.length > 0 && posi < outp.childNodes.length-1){
			if (posi >=0) 
				setColor(posi, "#FFFFFF", "#0088AA");
			else 
				input = textfield.value;
			setColor(++posi, "#D5E5FF", "#0088AA");
			textfield.value = outp.childNodes[posi].firstChild.nodeValue;
		}
	}
	else if (key == 38){ //Key up
		if (outp.childNodes.length > 0 && posi >= 0){
			if (posi >=1){
				setColor(posi, "#fff", "#0088AA");
				setColor(--posi, "#D5E5FF", "#0088AA");
				textfield.value = outp.childNodes[posi].firstChild.nodeValue;
			}
			else{
				setColor(posi, "#fff", "#0088AA");
				textfield.value = input;
				textfield.focus();
				posi--;
			}
		}
	}
	else if (key == 27){ // Esc
		textfield.value = input;
		document.getElementById('Autocomplete').style.display = 'none';
		posi = -1;
		oldins = input;
	}
	else if (key == 8){ // Backspace
		posi = -1;
		oldins=-1;
		Ajax_Send_Data(Search_Destination.name+'='+Search_Destination.value,'Ajax/Ajx_Autocomplete.php');
	}
	else if (key == 13){ // Return/Enter
		aux = outp.childNodes[posi];
		Destination_Selected(aux.firstChild.nodeValue,aux.childNodes[1].value,aux.childNodes[2].value,aux.childNodes[3].value,aux.childNodes[4].value);
		posi = -1;
		oldins=-1;
		
	}
	else
		Ajax_Send_Data(Search_Destination.name+'='+Search_Destination.value,'Ajax/Ajx_Autocomplete.php');
}

var mouseHandler=function(){
	for (var i=0; i < words.length; ++i)
		setColor (i, "white", "black");

	this.style.background = "blue";
	this.style.color= "white";
}

var mouseHandlerOut=function(){
	this.style.background = "white";
	this.style.color= "black";
}

var mouseClick=function(){
	document.getElementsByName("text")[0].value = this.firstChild.nodeValue;
	setVisible("hidden");
	posi = -1;
	oldins = this.firstChild.nodeValue;
}
