var position=0;
var test_position=0;

function alphaNumericCheck(theChar) {

	if ((theChar < 48) || (theChar > 122) || 
	   ((theChar > 57) && (theChar < 65)) || 
	   ((theChar > 90) && (theChar < 97))   ) {
		return false;
	} else {
		return true;
	}
}


function getdata(location,search_string)
{
	xmlhttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	a=Math.random();
	query_string=location+'?'+'search_string='+search_string+'&a='+a;
	xmlhttp.open('GET',query_string,true);
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			receiveData(xmlhttp.responseText);
		}
	}
	xmlhttp.send(null);
}

function receiveData(code)
{
	handle=document.getElementById('search_dropdown');
	handle.innerHTML=unescape(code);
}

function Tastendruck (Ereignis)
{
	if (!Ereignis)
		Ereignis = window.event;
	code = Ereignis.keyCode;
	if(code == 38)
	{
		test_position = position -1;
	}
	else if(code == 40)
	{
		test_position = position +1;
	}
	else if(code == 13)
	{
		if(	handle = document.getElementById('dropdown_'+position))
		{
			document.location.href=handle.href;
		}		
	}
	if(code >= 96 && code <=105)
	{
		code -= 48;
	}
	if(test_position != position)
	{
		if(	handle = document.getElementById('result_'+test_position))
		{
			handle_old = document.getElementById('result_'+position);
			position = test_position;
			if(handle_old)
			{
				//handle_old.style.background='#34A881';
				handle_old.style.background='#f2f2f2';
			}
			//handle.style.background='#47cfa2';
			handle.style.background='#dddddd';
		}
	}
	else
	{
		if(alphaNumericCheck(code))
		{
			search_value = document.getElementById('ajax_search').value+String.fromCharCode(code);
		}
		else
		{
			if(code == 8)
			{
				search_value = document.getElementById('ajax_search').value;
				search_value = search_value.substr(0,search_value.length-1);
			}
			else
			{
				search_value = document.getElementById('ajax_search').value;
			}
		}
		getdata('search_dropdown.php',search_value);
	}
}

function check_submit()
{
	if(position)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function hide_dropdown(Ereignis2)
{
	var src_element;
	if (!Ereignis2)
	{
		src_element = window.event.srcElement;
	}
	else
	{
		src_element = Ereignis2.target;
		1==1;
	}
	//tvs debug
	//alert("hallo");
	//alert(typeof(src_element.parentNode.parentNode));
	if(src_element.tagName == "HTML" || src_element.id.substr(0,8) == "dropdown" || (typeof(src_element.parentNode.id) != "undefined" && src_element.parentNode.id.substr(0,8) == "dropdown") || (typeof(src_element.parentNode.parentNode.id) != "undefined"  && src_element.parentNode.parentNode.id.substr(0,8) == "dropdown"))
	{
		return;
	}
	
	
	
	handle = document.getElementById('search_dropdown');
	handle.innerHTML = '';
	position=0;
}
