/*
// File written by Jochen "Khuri" Höhmann <khuri@khuris.com>
// Copyright 2010
//
// File        : ajax_autocomplete.js
// Begin       : 2009.10.29 15:21:41
// Last Update : 2010.02.12 12:21:48
*/

// <![CDATA[
var request = null;
var field = null;
var fieldbox = null;
var code;
document.onkeydown = storekey;
// silly htmlentities transformation for now, since we use no UTF-8...
var entities = new Array('auml','uuml','ouml','szlig','Auml','Uuml','Ouml');
var chars = new Array('ä','ü','ö','ß','Ä','Ü','Ö');
function replacechars(thestring) {
	for(var i=0;i<entities.length;i++) {
		sstring = new RegExp();
		sstring.compile('&'+entities[i]+';','g');
		thestring = thestring.replace(sstring,chars[i]);
	}
	return thestring;
}

if(window.XMLHttpRequest) {
	request = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			// no ajax support
		}
	}
}

function completefield(uname) {
	if(uname != "" && uname != 0) {
		fieldbox.value = uname;
		//document.getElementById(field+'_id').value = uid;
		fieldbox.blur();
		// select submit button of form
		for(var i=0;i<fieldbox.parentNode.length;i++) {
			if(fieldbox.parentNode.elements[i].type == "submit") {
				fieldbox.parentNode.elements[i].focus();
				break;
			}
		}
		fieldbox.parentNode.submit();
	}
	// remove autocompletebox if exists
	for(var i=0;i<fieldbox.parentNode.childNodes.length;i++) {
		if(fieldbox.parentNode.childNodes[i].id == "autocompletebox") {
			fieldbox.parentNode.removeChild(fieldbox.parentNode.childNodes[i]);
		}
	}
}

function datahandler() {
	if(request.readyState == 4) {
		// remove autocompletebox if exists
		for(var i=0;i<fieldbox.parentNode.childNodes.length;i++) {
			if(fieldbox.parentNode.childNodes[i].id == "autocompletebox") {
				fieldbox.parentNode.removeChild(fieldbox.parentNode.childNodes[i]);
			}
		}
		// get data
		var json = eval("("+request.responseText+")");
		
		var displayul = true;
		var ul = document.createElement("ul");
		var ulclass = document.createAttribute("class");
		ulclass.nodeValue = "autocompletebox";
		var ulid = document.createAttribute("id");
		ulid.nodeValue = "autocompletebox";
		var ulclick = document.createAttribute("onclick");
		ulclick.nodeValue = "completefield(0)";
		ul.setAttributeNode(ulclick);
		ul.setAttributeNode(ulclass);
		ul.setAttributeNode(ulid);
		
		if(json.result.length > 0) {
			for(var i=0;i<json.result.length;i++) {
				var li = document.createElement("li");
				var liclick = document.createAttribute("onclick");
				userdata = replacechars(json.result[i].tag);
				liclick.nodeValue = "completefield('"+userdata+"')";
				li.setAttributeNode(liclick);
				if(i % 2 > 0) {
					var liclass = document.createAttribute("class");
					liclass.nodeValue = "l2";
					li.setAttributeNode(liclass);
				}
				var litext = document.createTextNode(userdata);
				li.appendChild(litext);
				ul.appendChild(li);
			}
		}
		else {
			// we got no results, display notification
			var li = document.createElement("li");
			var liclick = document.createAttribute("onclick");
			liclick.nodeValue = "completefield(0)";
			li.setAttributeNode(liclick);
			var liclass = document.createAttribute("class");
			liclass.nodeValue = "lit";
			li.setAttributeNode(liclass);
			var litext = document.createTextNode("Nicht gefunden");
			li.appendChild(litext);
			ul.appendChild(li);
		}
		if(displayul) {
			fieldbox.parentNode.insertBefore(ul,fieldbox.nextSibling);
		}
	}
}

function ajax_autocomplete(what,who,where,g) {
	if(what.length > 0) {
		// if key pressed is not up or down
		if(code != 38 && code != 40) {
			if(request.readyState < 4) {
				request.abort();
			}
			if(what.length > 0) {
				field = where;
				fieldbox = document.getElementById(where);
				request.open("GET","modules/"+who+".php?q="+what+"&g="+g,true);
				request.onreadystatechange = datahandler;
				request.send(null);
			}
		}
		else {
			// nothing yet
		}
	}
}

function storekey(e) {
	// capture what key is pressed
	var e;
	if(!e) {
		e=window.event;
	}
	code = ((e.charCode) && (e.keyCode==0)) ? e.charCode : e.keyCode;
}
// ]]>
