var selectedItem = null;
var itemsAvailable = null;
var lastCommand = null;
var id = null;
var timer = null;

var http_loadcommands;
if (window.XMLHttpRequest) {
	http_loadcommands = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	http_loadcommands = new ActiveXObject("Microsoft.XMLHTTP");
}

function loadcommands(localid) {
	if (arguments.length == 0) localid = '';
	id = localid;
	var list = document.getElementById("foundcommands");
	if(lastCommand == document.getElementById('command'+id).value) return true;
	lastCommand = document.getElementById('command'+id).value;
	if(list != null) {
		selectedItem = null;
		itemsAvailable = null;
		list.parentNode.removeChild(list);
		delete list;
		itemsAvailable = null;
	}
	if (http_loadcommands != null && document.getElementById('command'+id).value.length > 2) {
		http_loadcommands.open("GET","/crewlist/js/loadcommands.php?name="+encodeURI(document.getElementById('command'+id).value),"false");
		http_loadcommands.onreadystatechange = openbox;
		http_loadcommands.send(null);
		timer = window.setTimeout("abort()", 2000);
	}
	return true;
}

function closeBox() {
	if(null != document.getElementById("foundcommands"))
	document.getElementById("foundcommands").parentNode.removeChild(document.getElementById("foundcommands"));
}

function openbox() {
	if(http_loadcommands.readyState != 4) return true;
	window.clearTimeout(timer);
	var list = document.getElementById("foundcommands");
	if(list==null) {
		var commandinput = document.getElementById("command"+id);
		list = document.createElement("ul");
		list.id = "foundcommands";
		var closebutton = document.createElement("div");
		closebutton.innerHTML = '<a href="#" onclick="javascript:closeBox()" class="verybright">x</a>\n';
		closebutton.id = 'foundcommandsclose';
		list.appendChild(closebutton);
		commandinput.parentNode.appendChild(list);
	}
	list.style.visibility = "hidden";
	if (http_loadcommands.responseText.length > 4) {
		var entries = eval("(" + http_loadcommands.responseText + ")");
		itemsAvailable = entries.length;
		for( var i = 0; i < entries.length; i++ ) {
			var entry = entries[i];
			var li = document.createElement("li");
			var a = document.createElement("a");
			a.href = "javascript:selectEntry(\""+entry.name+"\","+entry.value+","+entry.type+")";
			a.setAttribute("onclick","selectEntry(\""+entry.name+"\","+entry.value+","+entry.type+")");
			a.style.textDecoration = "none";
			a = highlight_text(entry.name,document.getElementById('command'+id).value.replace(/-/g,' '),a);
			if (a!=null) {
				li.appendChild(a);
				li.className = "deselected";
				list.appendChild(li);
			}
		}
		list.style.visibility = "visible";
	}
	return true;
}

function highlight_text(text,highlight,obj) {
	if(text.toLowerCase().indexOf(highlight.toLowerCase()) != -1) {
		var txt1 = document.createTextNode(text.substr(0,text.toLowerCase().indexOf(highlight.toLowerCase())));
		var b = document.createElement("span");
		var txt2 = document.createTextNode(text.substr(text.toLowerCase().indexOf(highlight.toLowerCase())+highlight.length));
		b.innerHTML = text.substr(text.toLowerCase().indexOf(highlight.toLowerCase()),highlight.length);
		b.style.fontWeight = "700";
		obj.appendChild(txt1);
		obj.appendChild(b);
		obj.appendChild(txt2);
	} else {
		var txt = document.createTextNode(text);
		obj.appendChild(txt);
	}
	return obj;
}

function selectEntry(value,commandid,type) {
	document.getElementById("command"+id).value = value;
	if(null!=document.getElementById("commandid"+id)) document.getElementById("commandid"+id).value = commandid;
	if(type == 3 && null!=document.getElementById("division")) {
		document.getElementById("division").value = 'squadron';
		document.getElementById("division").readOnly = true;
		document.getElementById("division").disabled = true;
	} else {
		if (null!=document.getElementById("division")) {
			if('squadron' == document.getElementById('division')) {
				document.getElementById("division").value = '';
			}
			document.getElementById("division").readOnly = false;
			document.getElementById("division").disabled = false;
		}
	}
	var list = document.getElementById("foundcommands");
	if(list) {
		list.parentNode.removeChild(list);
		delete list;
	}
	selectedItem = null;
	itemsAvailable = null;
}

function highlightEntry() {
	if(null==document.getElementById("foundcommands")) return true;
	var lis = document.getElementById("foundcommands").childNodes;
	var linr = 0;
	var i;
	var lichilds = null;
	for(i = 0; i < lis.length; i++) {
		if(lis[i].nodeName.toLowerCase() == "li") {
			if(linr == selectedItem) {
				lis[i].className = "selected";
			} else {
				lis[i].className = "deselected";
			}
			linr++;
		}
	}
	return true;
}

function keypressed(evnt) {
	var keycode = null;
	if(!evnt) evnt = window.event;
	if (evnt.which) {
		keycode = evnt.which;
	} else if (evnt.keyCode) {
		keycode = evnt.keyCode;
	}

	if(null == document.getElementById("foundcommands")) return true;

	switch (keycode) {
		case 38:	//key up
			if(null!=selectedItem) {
				if(--selectedItem < 0) selectedItem = null;
			} else {
				selectedItem = itemsAvailable - 1;
			}
			break;
		case 40:	//key down
			if(null!=selectedItem) {
				if(++selectedItem >= itemsAvailable) selectedItem = itemsAvailable-1;
			} else {
				selectedItem = 0;
			}
			break;
		case 13:
		case 32:
			var value = null;
			var lis = document.getElementById("foundcommands").childNodes;
			var linr = 0;
			var lichilds = null;
			var i;
			var j;
			for(i = 0; i < lis.length; i++) {
				if(lis[i].nodeName.toLowerCase() == "li") {
					if(linr == selectedItem) {
						lichilds = lis[i].childNodes;
						for(j = 0; j < lichilds.length; j++) {
							if(lichilds[j].nodeName.toLowerCase() == "a") {
								var str = lichilds[j].getAttribute("onclick");
								eval(str);
								document.getElementById("command"+id).blur();
								selectedItem = null;
								itemsAvailable = null;
								return true;
							}
						}
					} else {
						linr++;
					}
				}
			}
			break;
		case 27: //ESC
			closeBox();
			break;
		default:
			break;
	}
	highlightEntry();
	return true;
}

function abort() {
	http_loadcommands.abort();
	timer = null;
}

document.onkeydown = keypressed;
