var searchTimeout = false;
var countEntityUpper = -1;
var countEntityLower = 0;
var stringLength = 0;
var obj;

function startAutocomplete(e){
	//Taste ueberpruefen
	if(window.event){
		etype = window.event.type;
		kCode = window.event.keyCode;
	}else{
		etype = e.type;
		kCode = e.which;
	}

	if(kCode == 38 || kCode == 40){
		//Nach unten Taste wird gedrueckt
		if(kCode == 40){
			if(countEntityUpper > 0 && document.getElementById(countEntityUpper)){
				phonebookElementOut(document.getElementById(countEntityUpper-1));
				countEntityLower = countEntityUpper -1;
			}
			if(document.getElementById(countEntityUpper)){
				obj = document.getElementById(countEntityUpper);
				phonebookElementOver(obj);
				document.getElementById('search_tel').value = obj.title;
				CompleteInput(document.getElementById('search_tel'));
				countEntityUpper ++;
				if(countEntityUpper > 15){
					scrollAutocomplete('divPhonebookList', 16);
				}
			}
		}
		//Nach oben Taste wird gedrueckt
		if(kCode == 38){
			if(countEntityLower >= 0 && document.getElementById(countEntityLower)){
				phonebookElementOut(document.getElementById(countEntityLower+1));
				countEntityUpper = countEntityLower +1;
			}
			if(document.getElementById(countEntityLower)){
				obj = document.getElementById(countEntityLower);
				phonebookElementOver(obj);
				document.getElementById('search_tel').value = obj.title;
				CompleteInput(document.getElementById('search_tel'));
				countEntityLower --;
				scrollAutocomplete('divPhonebookList', -16);
			}
		}
		return false;
	}

	countEntityUpper = 0;
	countEntityLower = 0;
	startTimeout("showNameList()");
}

function startPhonebookFilter(){
	startTimeout("showNameListInBook()");
}

function startTimeout(startFunction){
	if(searchTimeout !== false){
		window.clearTimeout(searchTimeout);
		searchTimeout = false;
	}
	searchTimeout = window.setTimeout(startFunction,50);
}

/*
 * Scrolling fuer Autocompletefeld
 *
*/
function scrollAutocomplete(id, size){
	var objDiv = document.getElementById(id);
	if ( typeof(objDiv) == 'object'){
		if(objDiv.scrollTop == 0 && size > 0){
			objDiv.scrollTop = 5+ size;
		}else if(size < 0) {
			objDiv.scrollTop = objDiv.scrollTop - (size*-1);
		}else{
			objDiv.scrollTop = objDiv.scrollTop + size;
		}	
	}
}


function showNameList(){
	if(searchTimeout !== false){
		window.clearTimeout(searchTimeout);
		searchTimeout = false;
	}
	stringLength = document.getElementById('search_tel').value.length;
	//alert(stringLength);
	xajax_getPhonebookNamesAutocomplete(xajax.getFormValues('formSearch'));
}

function showNameListInBook(){
	if(searchTimeout !== false){
		window.clearTimeout(searchTimeout);
		searchTimeout = false;
	}
	stringLength = document.getElementById('search_tel').value.length;
	//xajax_getPhonebookFilterList(document.getElementById('inputNameFilter').value);
	//alert(xajax.getFormValues('formSearch'));
	xajax_getPhonebookFilterList(xajax.getFormValues('formDetailSearch'));
}

/*********************************************************************
 * Auswahl des Namens aus der Vorschlagliste
 *********************************************************************/
function phonebookElementOver(obj){
	obj.style.backgroundColor 	= 'highlight';
	obj.style.color 			= 'highlighttext';
	if(document.getElementById(obj.id+"_mail")){
		document.getElementById(obj.id+"_mail").style.color = 'highlighttext';
	}
	if(document.getElementById(obj.id+"_phone")){
		document.getElementById(obj.id+"_phone").style.color = 'highlighttext';
	}
}

function phonebookElementOut(obj){
	obj.style.backgroundColor 	= 'white';
	obj.style.color 			= 'black';
	if(document.getElementById(obj.id+"_mail")){
		document.getElementById(obj.id+"_mail").style.color = '#0066CC';
	}
	if(document.getElementById(obj.id+"_phone")){
		document.getElementById(obj.id+"_phone").style.color = '#0066CC';
	}
}

function writeIntoInputField(obj){
	document.getElementById('search_tel').value = obj.title;
	document.getElementById('search_tel').focus();
	document.getElementById('divPhonebookList').style.display = 'none';
	document.formSearch.submit();
}


//*////////////FUNCTION///////////////////////////////////
//*Name: CompleteInput
//*Desc: Vervollstaendigt die gemachte Eingabe mit dem ersten Vorschlag
//*Parameter:
//*Object obj		Desc: Aktuelles Feld
//*Array suggestion 	Desc: Daten des ersten Vorschlag [0]="Data",[1]="ID"
//*//////////////////////////////////////////////////////
function CompleteInput(obj){
	curr = obj.value;
	if(obj.createTextRange){
		var range = obj.createTextRange();
		range.moveStart('character',stringLength);
		range.select();
	} else if (obj.setSelectionRange){
		obj.setSelectionRange(stringLength,obj.value.length);
	}
}

function load_employee(acName) {
	parent.location.href = URL_BASE+'virtual/dHBsPWFscCxjYWxsPTksY250PTk.html?name=<?=$_REQUEST["name"];?>&acName='+acName;
}
