//************************
//*** AIRPORT SELECTED ***
//************************

var efrom 		= "from";
var efrom_code 	= "from_code";
var efrom_within= "from_within";
var eto			= "to";
var eto_code	= "to_code";
var eto_within	= "to_within";
var elang		= "lang";

function airportCheckWithIn() {
//do: check if an airport is enabled or not

	fr_str = document.getElementById(efrom_code).value;
	to_str = document.getElementById(eto_code).value;
	if (fr_str == "") { 
		document.getElementById(efrom_within).disabled = true; 
		document.getElementById(efrom_within).selectedIndex = 0;
	} else { 
		document.getElementById(efrom_within).disabled = false; 
	}
	if (to_str == "") { 
		document.getElementById(eto_within).disabled = true; 
		document.getElementById(eto_within).selectedIndex = 0;
	} else { 
		document.getElementById(eto_within).disabled = false; 
	}
}

//********************
//*** ROUTE SEARCH ***
//********************

function route() {
//do: make a route

	fr_str 	 = document.getElementById(efrom_code).value;
	frin_str = document.getElementById(efrom_within).value;
	to_str 	 = document.getElementById(eto_code).value;
	toin_str = document.getElementById(eto_within).value;
	
	fmEngine = document.getElementById("fmASEngine");
	fmEngine.SetVariable("_root.outsideFrom", fr_str); 
	fmEngine.SetVariable("_root.outsideFromIn", frin_str); 
	fmEngine.SetVariable("_root.outsideTo", to_str); 
	fmEngine.SetVariable("_root.outsideToIn", toin_str); 
	fmEngine.SetVariable("_root.outsideAction", "route"); 
	fmEngine.TCallLabel("_root.outside_mc", "outsideAction");
}

function routeReset() {
//do: make a route

	document.getElementById(efrom).value = "";
	document.getElementById(efrom_code).value = "";
	document.getElementById(efrom_within).value = "";
	document.getElementById(eto).value = "";
	document.getElementById(eto_code).value = "";
	document.getElementById(eto_within).value = "";

	hybridAirline();
}

//*********************
//*** MAP FUNCTIONS ***
//*********************

function mapAirportSelect(id_str) { 
//do: when an user select an airport from the map

	if (document.getElementById(efrom_code).value == "") {
		airportSearch(id_str, document.getElementById(efrom), document.getElementById(efrom_code));
	} else {
		airportSearch(id_str, document.getElementById(eto), document.getElementById(eto_code));
	}
}

function mapCitySelect(id_str, label_str) { 
//do: when an user select a city from the map

	if (document.getElementById(efrom_code).value == "") {
		document.getElementById(efrom_code).value = id_str;
		document.getElementById(efrom).value = label_str;
	} else {
		document.getElementById(eto_code).value = id_str;
		document.getElementById(eto).value = label_str;
	}

	airportCheckWithIn();
	route();
}

// ************************
// *** SEARCH FUNCTIONS ***
// ************************

var _airport = null;
var _airport_code = null;
var _airport_chars = 1;
var _airport_list;
var _airport_list_background;
var _airport_list_count;

var _select_current;
var _select_prev;

// ************
// *** KEYS ***
// ************

var key_esc = 27;
var key_tab = 9;
var key_alt = 18;
var key_up = 38;
var key_down = 40;
var key_left = 37;
var key_right = 39;
var key_enter = 13;
var key_shift = 16;
var key_last = 0;
var key_who;
var _stop;

function keyDown(e) { 
	var pK = document.all? window.event.keyCode:e.which;
	var pK2 = String.fromCharCode(pK).toLowerCase(); key_last=pK; if(key_who!=null){key_who(pK, pK2);}
}
function keysInit(callback) { 
	document.onkeydown = keyDown; key_who=callback;
	if (document.layers) {document.captureEvents(Event.KEYPRESS);} 
}
function noEnter(){ 
	return key_last!=key_enter;
}

function airport_focus(id, id_code) {
	airport_clear(id);
	airport_clear_code(id_code);
	airportCheckWithIn();
	airport_init(id, id_code, 180);
}

function airport_clear(id) {
	if (id.value.length > 0) { 
		id.select();
	} else {
		if(id.className == "textfield")
			id.className = 'textfield'; 
		else
			id.className = 'airport';  
		id.value = '';
	}
}

function airport_clear_code(id) {
	if (id.value.length > 0) { 
	} else {
		id.className = 'textfield'; 
		id.value = '';
	}
}

function airport_cancel() {
    keysInit(null);
    if (_airportcode!=null && _airportcode.value!=null && _airportcode.value.length < 1) { aiport_setValue(0); }
    airport_close();
    _airport = null;
    _stop = false;
}


function airport_close() {
    if (_airport_list!=null) {
        _airport_list.style.display = 'none';
        _airport_list.innerHTML = "";
    }
    if (_airport_list_background!=null) {
        _airport_list_background.style.display = 'none';
    }
    _stop = true;
}

function airport_init(air, aircode) {

	_airportcode = _airport = null;
    airport_cancel();

    var body = document.getElementsByTagName("body")[0];

    _airport_list = document.getElementById('airportbox');
    if (!_airport_list) {
      _airport_list = document.createElement("div");
      _airport_list.id = "airportbox";
      _airport_list.style.zIndex = 51;
      _airport_list.style.position = 'absolute';
      _airport_list.style.display = 'none';
      _airport_list.style.width = 'auto';
      body.appendChild(_airport_list);
    }
    _airport_list.className = "aiportboxResult";

	_airport_list_background = document.getElementById('airportboxBG');
    if (!_airport_list_background) {
		_airport_list_background = document.createElement("iframe");
        _airport_list_background.id = "airportboxBG";
        _airport_list_background.style.zIndex = 50;
        _airport_list_background.setAttribute('scrolling', 'no');
        _airport_list_background.setAttribute('frameborder', '0');
        _airport_list_background.style.position = "absolute";
        _airport_list_background.style.display = "none";
       body.appendChild(_airport_list_background);
    }

    keysInit(keypressed);
    _airport = air;
    _airportcode = aircode; 
    
    var x = airport_x(air);
    var y = airport_y(air) + air.offsetHeight + 1;

    _airport_list.style.top=y + 'px';
    _airport_list.style.left=x + 'px';
	
    _select_current = -1;
    _select_prev = -1;
    _stop = false;
}

function airport_search(id) {
	
	if (_airport.value.length< _airport_chars){return;}
	
	if (window.XMLHttpRequest) 	{
		xhr=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		xhr=new ActiveXObject("Microsoft.XMLHTTP")
	}
		
	if (xhr!=null) {
		xhr.onreadystatechange = airport_searchResult;
		lang_str = document.getElementById(elang).value;
		xhr.open("GET", serverpath + "/maps11/maps/ptp/info/airports.php?lang=" +  lang_str + "&label=" + _airport.value, true);
		xhr.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.")
	}
}

function airportSearch(code_str, air, air_code) {
	
	_airport = air;
	_airport_code = air_code;
	
	if (window.XMLHttpRequest) 	{
		xhr=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		xhr=new ActiveXObject("Microsoft.XMLHTTP")
	}
		
	if (xhr!=null) {
		xhr.onreadystatechange = airportSearchFromToResult;
		lang_str = document.getElementById(elang).value;
		xhr.open("GET", serverpath + "/maps11/maps/ptp/info/airport_code.php?lang=" +  lang_str + "&label=" + code_str, true);
		xhr.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.")
	}
}

function airportSearchFromToResult() {
	if (xhr.readyState == 4) {
		if (xhr.status==200) { 
			label_str = xhr.responseText;
			var info = label_str.split("|");
			_airport.value = info[1];
			_airport_code.value = info[0];
			airportCheckWithIn();
			route();
        }
    }
}

function airport_searchResult() {
	if (xhr.readyState == 4) {
		if (xhr.status==200) { 
		
            _airport_list.innerHTML = xhr.responseText;
			
            var list = airport_REQUESTList();
            _airport_list_count = list.childNodes.length;

            if (_airport_list_count > 0) {
                for (var i=0; i < list.childNodes.length; i++) {
                    li = list.childNodes[i];
                    li.onmousedown = _sbMouseDown;
                    li.onmouseover = _sbMouseOver;
                    li.onmouseout = _sbMouseOut;
                }
                _airport_list.style.width = 'auto';
                _showChoices();
            }
            else {
                _hideChoices();
            }

            _select_current = 0;
            _select_prev = 0;
            selChoice(0);
        }
    }
}

function getSbIndex(elem) {
    var list = airport_REQUESTList();
    var retIndex = 0;
    for (var i=0; i < list.childNodes.length; i++) { 
        if (elem == list.childNodes[i]) {
            retIndex = i;
            break;
        }
    }

    return retIndex;
}

function _sbMouseOut(event) {
    var obj = event ? event.target : this;
    _select_current = -1;
    selChoice(-1);
}

function _sbMouseDown(event) {
    var obj = event ? event.target : this;
    _select_current = getSbIndex(obj);

    aiport_setValue(_select_current);
    airport_close();
}

function _sbMouseOver(event) {
    var obj = event ? event.target : this;
    _select_current = getSbIndex(obj);
    selChoice(_select_current);
}

function _showChoices() {
    _airport_list.style.display = 'inline';
    _airport_list_background.style.width = _airport_list.offsetWidth + "px";
    _airport_list_background.style.height = _airport_list.offsetHeight + "px";
    _airport_list_background.style.top = _airport_list.style.top;
    _airport_list_background.style.left = _airport_list.style.left;
}

function _hideChoices() {
    _airport_list.style.display = 'none';
    _airport_list_background.style.display = 'none';
}

function airport_REQUESTList() {
    var ul;
    for (var i=0; i < _airport_list.childNodes.length; i++) {
       var node = _airport_list.childNodes[i];
       if (node.nodeName == 'UL') {
           ul = node;
           break;
       }
    }
    return ul;
}

function aiport_setValue(newi) {
    if (newi<0) {
		_airportcode.value="";
    } else if (_airport_list_count > 0) {
        var list = airport_REQUESTList();
        if (list == null || list.childNodes == null) return;
        var selected = list.childNodes[newi];

        _airportcode.value = selected.id;
        if (selected.innerText != undefined) {
          _airport.value = selected.innerText;
        } else {
          _airport.value = selected.textContent;
        }
		
		airportCheckWithIn();
		route();
    }
}

function selChoice(newi) {
    var list = airport_REQUESTList();

    if (list.childNodes.length > 0) {
        if (_select_prev >= 0) {
            var prev = list.childNodes[_select_prev];
            prev.className = prev.className.replace(/ *airportHi/, "");
        }

        if (newi >= 0) {
			if (list.childNodes.length > 0) {
                var cur = list.childNodes[newi];
                cur.className = cur.className + " airportHi";
            }
            _select_prev = newi;
        }
    }
}

function keypressed(keycode, keyvalue) {
	
    switch (keycode){
		case key_left:
		case key_up:
			_select_current = _select_current - 1;if (_select_current<0){_select_current=0;}selChoice(_select_current);
			break;
		case key_right:
		case key_down:
			_select_current++;if (_select_current>=_airport_list_count){_select_current=_airport_list_count-1;}selChoice(_select_current);
			break;
		case key_enter:
			if (_airport_list_count>0){
				aiport_setValue(_select_current);
				airport_close();
			}
			break;
		case key_esc:
			airport_close();
			break;
		case key_tab:
			if (_select_current>=0&&_select_current<_airport_list_count){aiport_setValue(_select_current);}
		case key_alt:
		case key_shift:
			break;
		default:
			aiport_setValue(-1);
			_idletimer = self.setTimeout('search_key()', 0);
	}
}

function search_key() {
	if (_airport != null && _airport.value.length > 0) {
		airport_search(_airport); 
	} else {
		airport_close();
	}
}

function sbNoEnter() {
	var v = (_stop==null || _stop!=true);
    _stop = false; return (v ? true: noEnter());
}

function airport_x(obj){
	var _x=0;
	if(obj.offsetParent){
		while(obj.offsetParent){
			_x+=obj.offsetLeft;
			obj=obj.offsetParent;
		}
	}else{
		if(obj.x){
			_x+=obj.x;
		}
	}
	return _x;
}

function airport_y(obj){
	var _y=0;
	if(obj.offsetParent){
		while(obj.offsetParent){
			_y+=obj.offsetTop;
			obj=obj.offsetParent;
		}
	}else{
		if(obj.y){
			_y+=obj.y;
		}
	}
	return _y;
}	