/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>     						  |
// | Modified by: braus.ch												  |
// +----------------------------------------------------------------------+
*/

var searchboxReq=false;
var t=null;
var isIE=false;
var isFireFox=false;
var isSafari=false;
var defaultSubmitLocation="";
var searchid="";
var searchbox="";
var RESULTLAYER="resultlayer";
var searchform="";
var SEARCH_SITE="";
var old_searchbox="";
var isChosen=false;
var toOne=true;
var arrayLabels=null;
var offsetInfoLabel=15;
searchboxReq=null;


function select(id,obj){
	var searchtable = document.getElementById(searchid+"T");
	if(isSafari && searchtable.rows.length){
		if(searchtable.rows[0].getAttribute("id") == "safariDummy")	
			searchtable.deleteRow(0);
	}
	var npos = searchtable.rows.length;
	if (npos == 1) {
		if (document.forms[searchform].elements[searchid].value == id)
			npos = -1;
		else if (toOne) {
			npos = 0;
			searchtable.deleteRow(npos);
		}
	} else if (npos > 1) {
		for (var i=0;npos>-1&&i<document.forms[searchform].elements[searchid].length;i++) {
			if (document.forms[searchform].elements[searchid][i].value == id) {
				npos = -1;
			}
		}
	}
	if (npos == -1) {
		alert('Diese Verknüpfung existiert bereits!');
	} else {
		var x = searchtable.insertRow(npos);
		x.id = searchid+id;
		eval(searchid+'W(x,obj.childNodes[0].nodeValue,id)');
	}
	searchboxHideDelayed();
}

function dynloadDelete(searchid,pkid) {
	var pos = -1;
	var table = document.getElementById(searchid+'T')
	var findid = searchid+pkid;
	for (var i=0;pos<0&&i<table.rows.length;i++) {
		if (table.rows[i].id == findid) {
			pos = i;;
		}
	}
	if (pos > -1)
		table.deleteRow(pos);
}

function prepareToSend(searchboxReq, searchValue, dontCheckDoubleSpace){
	if(0 < trimAll(searchValue).length){
		if(!(searchValue.substring(searchValue.length-1, searchValue.length) == ' ') || dontCheckDoubleSpace){
				while (searchValue.substring(searchValue.length-1, searchValue.length) == ' '){
					searchValue=searchValue.substring(0,searchValue.length-1);
				}
				searchboxReq.onreadystatechange= searchboxProcessReqChange;
				searchboxReq.open("GET", SEARCH_SITE + searchValue+"&r="+Math.random(), true);
				searchboxReq.send(null);
		} else {
			searchboxHideDelayed();
		}
	}
}

function submit_dummy(){/*Do not delete this function*/}

function searchboxProcessReqChange() {
	if (searchboxReq.readyState == 4) {
		if (searchboxReq.status == 200) {
			createDiv(searchboxReq.responseText);
			var res=document.getElementById(RESULTLAYER);
			document.getElementById(RESULTLAYER).style.left=
				findPosX(document.getElementById(searchbox))+'px';
			document.getElementById(RESULTLAYER).style.top=
				setPosY()+'px';
			if(isIE)
				removeSelect();
		}
	}
}
function searchboxInit(){
	if (navigator.userAgent.indexOf("Safari") > 0) {
		document.getElementById(searchbox).addEventListener("keydown",searchboxKeyPress,false);
		document.getElementById(searchbox).addEventListener("keyup",setInfoBox,false);
		isSafari = true;
	} else if (navigator.product == "Gecko") {
		document.getElementById(searchbox).addEventListener("keypress",searchboxKeyPress,false);
		document.getElementById(searchbox).addEventListener("keyup",setInfoBox,false);
		document.getElementById(searchbox).addEventListener("blur",searchboxHideDelayed,false);
		isFireFox=true;
	} else {
		if(''!=old_searchbox){
			document.getElementById(old_searchbox).detachEvent('onkeydown',searchboxKeyPress);
			document.getElementById(searchbox).detachEvent("onkeyup",setInfoBox);
			}
		document.getElementById(searchbox).attachEvent('onkeydown',searchboxKeyPress);
		document.getElementById(searchbox).attachEvent("onkeyup",setInfoBox);
		isIE=true;
	}
	document.getElementById(searchbox).setAttribute("autocomplete","off");
}

function searchboxHideDelayed(){
	window.setTimeout("searchboxHide()",250);
}
function searchboxHide(){
	var highlight=document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
		isChosen=false;
	}
	if(defaultSubmitLocation)	
		document.forms[searchform].action=defaultSubmitLocation;
	if(isIE)
		displaySelect();
	if(null!=document.getElementById(RESULTLAYER))
		document.getElementById(RESULTLAYER).parentNode.removeChild(document.getElementById(RESULTLAYER));
}

function searchboxKeyPress(event){
	if((event.keyCode == 32) || (event.which == 32)){
			if (searchboxReq && searchboxReq.readyState < 4)
				searchboxReq.abort();
			if (window.XMLHttpRequest) {
				searchboxReq = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				searchboxReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
		prepareToSend(searchboxReq, document.getElementById(searchbox).value);
	}
	else if((event.keyCode == 13) || (event.which == 13)){
		if("javascript:submit_dummy();"!=document.forms[searchform].action)
			defaultSubmitLocation=document.forms[searchform].action;
		document.forms[searchform].action="javascript:submit_dummy();";
		if (isChosen == true){
			highlight.onclick();
			searchboxHideDelayed();
			}
		else{
			if (window.XMLHttpRequest) {
				searchboxReq = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				searchboxReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			prepareToSend(searchboxReq, document.getElementById(searchbox).value, true);
			}
	}
	//Key down
	else if (event.keyCode == 40) {
		highlight=document.getElementById("LSHighlight");
		if (!highlight) {
			highlight=document.getElementById(RESULTLAYER).getElementsByTagName("ul")[0].getElementsByTagName("li")[0];
		} else {
			highlight.removeAttribute("id");
			highlight=highlight.nextSibling;
		}
		if (highlight)
			highlight.setAttribute("id","LSHighlight");
		if (!isIE)
			event.preventDefault();
		isChosen=true;
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight=document.getElementById("LSHighlight");
		if (!highlight) {
			last_element=document.getElementById(RESULTLAYER).getElementsByTagName("ul")[0].getElementsByTagName("li").length-1;
			highlight=document.getElementById(RESULTLAYER).getElementsByTagName("ul")[0].getElementsByTagName("li")[last_element];
		} 
		else {
			highlight.removeAttribute("id");
			highlight=highlight.previousSibling;
		}
		if (highlight)
			highlight.setAttribute("id","LSHighlight");
		if (!isIE)
			event.preventDefault();
		isChosen=true;
	} 
	//ESC
	else{
		setInfoBox();
		searchboxHide();
	} 
}

function setInfoBox(){
	var selection = 0;
	if(typeof document.getElementById(searchbox).selectionStart != 'undefined')
		selection = document.getElementById(searchbox).selectionStart;
	var searchValue = document.getElementById(searchbox).value;	
	if(isIE){
		selection = document.selection.TextRange;
		var range = document.selection.createRange();
		range.moveStart('character',-1024);
		var pos_text = range.text;
	}else if (isFireFox)
		var pos_text = searchValue.substring(0,selection);
	else
		pos_text = searchValue;
		
	var msg_string = "";
	for(var i=pos_text.split(' ').length;i<arrayLabels.length+1;i++){
		if(i==pos_text.split(' ').length){
			msg_string += "<b>"+arrayLabels[i-1]+"</b>&nbsp;";
		}else{
			msg_string += arrayLabels[i-1]+"&nbsp;"
		}
	}
	var infoDiv = document.getElementById(searchid+"I");
	if(msg_string=='')
		msg_string="&nbsp;";
	infoDiv.innerHTML = msg_string;
}

function searchboxStart(searchid_,form_,toOne_,arrLabel_,url_) {
	old_searchbox= searchbox;
	searchid=searchid_;
	searchbox=searchid_+"S";
	toOne=toOne_;
	arrayLabels = arrLabel_;
	searchform=form_;
	SEARCH_SITE=url_;
	searchboxInit();
	setInfoBox();
	if (trimAll(document.getElementById(searchbox).value)=='') {
		searchboxHide();
		return false;
	}
}

function trimAll(sString){
	while (sString.substring(0,1) == ' '){
		sString=sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString=sString.substring(0,sString.length-1);
	}
	return sString;
}

function removeSelect(){
	var layerT=findPosY(document.getElementById(RESULTLAYER),true);
	var layerH=document.getElementById(RESULTLAYER).offsetHeight;
	var layerL=findPosX(document.getElementById(RESULTLAYER),true);
	var layerW=document.getElementById(RESULTLAYER).offsetWidth;
	for(var i=0;i<document.getElementsByTagName("select").length;i++){
		var selectT=findPosY(document.getElementsByTagName("select")[i]);
		var selectL=findPosX(document.getElementsByTagName("select")[i]);
		var selectH=document.getElementsByTagName("select")[i].offsetHeight+5;
		var selectW=document.getElementsByTagName("select")[i].offsetWidth+5;
		if((selectT < (layerT+layerH) && (selectT+selectH > layerT))
		&& (selectL < (layerL+layerW) && (selectL+selectW > layerL)))
			document.getElementsByTagName("select")[i].style.visibility="hidden";
		else
			document.getElementsByTagName("select")[i].style.visibility="visible";
	}
}

function displaySelect(){
	for(var i=0;i<document.getElementsByTagName("select").length;i++){
		document.getElementsByTagName("select")[i].style.visibility="visible";
	}
}

function setPosY(){
	if(!isIE)
		var boxH=20;
	else
		var boxH=document.getElementById(searchbox).offsetHeight;		
	var boxT=findPosY(document.getElementById(searchbox));
	var layerH=document.getElementById(RESULTLAYER).offsetHeight;
	var setPosY=0;
	if(isIE){
		var screenH=document.body.offsetHeight;
		var currPos=document.body.scrollTop;
	}else {
		var screenH=window.innerHeight;
		var currPos=window.pageYOffset;
	}
	if((screenH+currPos-boxH-boxT) > layerH)
		setPosY=boxT+boxH + offsetInfoLabel;
	else if((boxT-currPos) > layerH)
		setPosY=screenH - (screenH-boxT) - layerH;
	else
		setPosY=boxT+boxH + offsetInfoLabel;
	return setPosY;
}

function createDiv(txt){
	old_resultlayer=document.getElementById(RESULTLAYER);
	if(!old_resultlayer){
		var tmp =document.createElement("div");
		tmp.innerHTML=txt;
		tmp.setAttribute("id",RESULTLAYER);
		document.getElementById(searchbox).parentNode.appendChild(tmp);
	} else {
		document.getElementById(RESULTLAYER).parentNode.removeChild(document.getElementById(RESULTLAYER));
		createDiv(txt);
	}
}

function findPosX(obj, doNotIgnorediv) {
	var curleft=0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			if('DIV'!=obj.nodeName||doNotIgnorediv)
				curleft+=obj.offsetLeft
			obj=obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft+=obj.x;
	return curleft;
}

function findPosY(obj, doNotIgnorediv){
	var curtop=0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			if('DIV'!=obj.nodeName||doNotIgnorediv)
				curtop += obj.offsetTop;
			obj=obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
