var validFoocus = false;

function cityServerRequest(url, returnFunctionName){  
    var xmlHttpReqCity = false;
    var self = this;
    
    if (window.XMLHttpRequest) { // Mozilla/Safari
        self.xmlHttpReqCity = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { // IE
        self.xmlHttpReqCity = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    self.xmlHttpReqCity.open('GET', url, true);
    self.xmlHttpReqCity.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReqCity.onreadystatechange = function() {
        if (self.xmlHttpReqCity.readyState == 4) {
            var response = self.xmlHttpReqCity.responseText;
            eval(returnFunctionName)(response);
        }
    }
    self.xmlHttpReqCity.send('');
}        

function citySel(event, objSelect) {
	var objCitySearch = document.getElementById("city");
	var objHidCity = document.getElementById("hidCitySearch");
	
	if (event.keyCode == 13) {
		objCitySearch.value = objSelect.options[objSelect.selectedIndex].text;
		objHidCity.value = objSelect.value;
		
		var objDivSearch = document.getElementById("divSearch");
		objDivSearch.style.display = "none";
		objCitySearch.focus();
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function searchCity(objEvent, objText) {
	if ((objEvent.keyCode > 96 && objEvent.keyCode < 123) ||
		(objEvent.keyCode > 64 && objEvent.keyCode < 89)) {

		var objDivSearch = document.getElementById("divSearch");
		var pos = findPos(objText);
		objDivSearch.style.top = (parseInt(pos[1]) + parseInt(objText.clientHeight)) + "px";
		objDivSearch.style.left = pos[0] + "px";
		objDivSearch.style.width = objText.clientWidth + "px";
		objDivSearch.style.display = "block";

		var params = "name=" + objText.value + "&country=" + actualCountry + "&state=" + actualState;
		
		cityServerRequest("data/citiesByName2.php?" + params, "searchCityDataArrival");
		
	} else if (objEvent.keyCode == 40) {
		validFoocus = true;
		document.getElementById("selCity").focus();
	}
}

function getFocus(obj){
	validFoocus = true;
	obj.focus();
	obj.selectedIndex = 0;
}

function onCityClick(objEvent, objSelect) {
	var objCitySearch = document.getElementById("city");
	var objHidCity = document.getElementById("hidCitySearch");
	
	objCitySearch.value = objSelect.options[objSelect.selectedIndex].text;
	objHidCity.value = objSelect.value;
	
	var objDivSearch = document.getElementById("divSearch");
	objDivSearch.style.display = "none";
	objCitySearch.focus();
}

function searchCityDataArrival(responce) {
	var objCity = document.getElementById("selCity");
	removeAllOptions(objCity);
	
    var arrFilas = responce.split("<#>");
	var cantFilas = arrFilas.length;
	
	for (var i = 0; i < cantFilas; i++) {
		var strRow = arrFilas[i];
		
		var arrCols = strRow.split("<@>");

		var option = document.createElement("OPTION");
		option.value = arrCols[0];
		option.text = arrCols[1];
		option.setAttribute("state", arrCols[2]);
		
		objCity.options.add(option);
	}
}

function leaveFocus(e) {
	if (!validFoocus) {
		document.getElementById('divSearch').style.display = 'none';
	} else {
		validFoocus = false;
	}
}
