//************************
//*** SEARCH FUNCTIONS ***
//*********************

var efrom = "search";
var serverpath = urlROOT + "include/db/db_search.asp";

function websearch() { window.location = urlROOT + "search/?l=" + document.getElementById("search").value; }

// ************************
// *** SEARCH FUNCTIONS ***
// ************************

var _airport = null;
var _airportcode = null
var _airport_chars = 2;
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 search_focus(id) {
	search_clear(id);
	search_init(id, 180);
}

function search_clear(id) {
	if (id.value.length > 0) { 
		if(id.className == "textfield")
			id.className = 'textfield'; 
		else
			id.className = 'searchbox';  
		id.value = '';
	}
}

function search_cancel() {
    keysInit(null);
    if (_airportcode!=null && _airportcode.value!=null && _airportcode.length < 1) { search_setValue(0); }
    search_close();
    _airport = null;
    _stop = false;
}


function search_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 search_init(air) {

	_airport = null;
    search_cancel();

    var body = document.getElementsByTagName("body")[0];

    _airport_list = document.getElementById('boxsr');
    if (!_airport_list) {
      _airport_list = document.createElement("div");
      _airport_list.id = "boxsr";
      _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 = "boxsr";

	_airport_list_background = document.getElementById('srboxBG');
    if (!_airport_list_background) {
		_airport_list_background = document.createElement("iframe");
        _airport_list_background.id = "srboxBG";
        _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;
    
    var x = searchbox_x(air);
    var y = searchbox_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 searchLabel(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 = searchLabelResult;
		lang_str = "";
		xhr.open("GET", serverpath + "?label=" + _airport.value, true);
		xhr.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.")
	}
}

function searchLabelResult() {
	if (xhr.readyState == 4) {
		if (xhr.status==200) { 
		
            _airport_list.innerHTML = xhr.responseText;
			
            var list = search_REQUESTList();
			
			if (list != undefined) {
				_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 = search_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);

    search_setValue(_select_current);
    search_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 search_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 search_setValue(newi) {
    if (newi<0) {
		_airportcode = "";
    } else if (_airport_list_count > 0) {
        var list = search_REQUESTList();
        if (list == null || list.childNodes == null) return;
        var selected = list.childNodes[newi];

        _airportcode = selected.id;
        if (selected.innerText != undefined) {
          _airport.value = selected.innerText;
        } else {
          _airport.value = selected.textContent;
        }
    }
}

function selChoice(newi) {
    var list = search_REQUESTList();

	if (list != undefined) {
		if (list.childNodes.length > 0) {
			if (_select_prev >= 0) {
				var prev = list.childNodes[_select_prev];
				prev.className = prev.className.replace(/ *resHi/, "");
			}
	
			if (newi >= 0) {
				if (list.childNodes.length > 0) {
					var cur = list.childNodes[newi];
					cur.className = cur.className + " resHi";
				}
				_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){
				search_setValue(_select_current);
				search_close();
			}
			websearch();
			break;
		case key_esc:
			search_close();
			break;
		case key_tab:
			if (_select_current>=0&&_select_current<_airport_list_count){search_setValue(_select_current);}
		case key_alt:
		case key_shift:
			break;
		default:
			search_setValue(-1);
			_idletimer = self.setTimeout('search_key()', 0);
	}
}

function search_key() {
	if (_airport != null && _airport.value.length > 0) {
		searchLabel(_airport); 
	} else {
		search_close();
	}
}

function sbNoEnter() {
	var v = (_stop==null || _stop!=true);
    _stop = false; return (v ? true: noEnter());
}

function searchbox_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 searchbox_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;
}
