
var dbglog = "";
var dbcnt = 0;
function dbg(txt)
{
	if(dbcnt > 20)
	{
		dbcnt = 0;
		dbglog = "";
	}
	dbglog += txt + "<br>";
	dbcnt++;
//	if (document.getElementById("debug"))
//		document.getElementById("debug").innerHTML = dbglog;
}


function AjaxDropDown()
{
			
			var vDisplayTime=800;
			
			var vNoResults="(Keine Treffer)";

      this.Init = function(instanceName, anchorControlId, dropdownId, url)
      {
            this.instanceName = instanceName;
            this.anchorControlId = anchorControlId;
            this.dropdownId = dropdownId;
            this.url = url;
            this.isIE = false;
            this.hideDelay = 100;
            this.timer = null;
            this.lastQuery = "";

            if (window.XMLHttpRequest) 
            {
							this.httpReq = new XMLHttpRequest();
            } 
            else
						{ 
							this.httpReq = new ActiveXObject("Microsoft.XMLHTTP");
						}
                  
            var dropdown = document.getElementById(this.dropdownId);
            dropdown.style.display = "none";
            dropdown.style.position = "absolute";
            dropdown.style.top = "auto";
            dropdown.style.left = "auto";
						
						//alert(BrowserDetect.browser);

            if (navigator.product == "Gecko") 
            {
                  var thisObj = this;
 									if(BrowserDetect.browser!="Safari")
									{
										document.getElementById(this.anchorControlId).addEventListener("keypress", function (event) { thisObj.keyPress(event) },false);
									}
									// document.getElementById(this.anchorControlId).addEventListener("blur", function () { thisObj.Hide() },false);
            } 
            else 
            {
                  var thisObj = this;
                  document.getElementById(this.anchorControlId).attachEvent('onkeydown', function (event) { thisObj.keyPress(event) });
									//document.getElementById(this.anchorControlId).attachEvent('onblur', function () { thisObj.Hide() });
                  this.isIE = true;
            }
            document.getElementById(this.anchorControlId).setAttribute("autocomplete","off");      
      }
      
      this.Hide = function() 
      {
			  dbg("hide");	
        var thisObj = this;
        window.setTimeout(function() { thisObj.hideDropDown() }, this.hideDelay);
      }
      
      this.SetUrl = function(url)
	  {
	  		this.url = url;
	  }
	  
      this.hideDropDown = function() 
      {
		  dbg("hideDropDown");	
            if (document.all)
                  this.closeShim(document.getElementById(this.dropdownId));
            document.getElementById(this.dropdownId).style.display = "none";
            var activeLine = document.getElementById("DropDownActive");
            if (activeLine) 
                  activeLine.removeAttribute("id");
						document.searchform.CanBeSubmitted.value="true";
      }
      
      this.keyPress = function(event)
      {
			var nKeyCode = document.layers ? event.which : event.keyCode;
//		  alert("keyPress, keycode : " + nKeyCode);
			//alert("keyPress, keycode : " + String(event.keyCode));
			
            if (this.DisableSuggest && this.DisableSuggest()) return;
            if (event.keyCode == 40 || event.keyCode == 63233) // DOWN
            {
                  highlight = document.getElementById("DropDownActive");
                  if (!highlight)
                        highlight = document.getElementById(this.dropdownId + "_inner").firstChild.firstChild;
                  else 
                  {
                        highlight.removeAttribute("id");
                        if (highlight.nextSibling)
                              highlight = highlight.nextSibling;
                  }
                  if (highlight) 
                  {
                        highlight.setAttribute("id","DropDownActive");
                        var res = document.getElementById(this.dropdownId);
                        if (highlight.offsetTop + highlight.offsetHeight > res.scrollTop + res.offsetHeight)
                              res.scrollTop = highlight.offsetTop - res.offsetHeight + highlight.offsetHeight;
                  } 
                  if (!this.isIE) 
                        event.preventDefault();
                  if (highlight)
										if(highlight.firstChild.data!=vNoResults)
										{
	                  	 this.SelectionChanged(highlight.firstChild.data); // + ".");
										}
								
									document.searchform.CanBeSubmitted.value="false";
									
            } 
            else if (event.keyCode == 38 || event.keyCode == 63232) // UP
            {
                  highlight = document.getElementById("DropDownActive");
                  if (!highlight)
                        highlight = document.getElementById(this.dropdownId).firstChild.firstChild.lastChild;
                  else 
                  {
                        highlight.removeAttribute("id");
                        if (highlight.previousSibling)
                              highlight = highlight.previousSibling;
                  }
                  if (highlight) 
                  {
                        highlight.setAttribute("id","DropDownActive");
                        var res = document.getElementById(this.dropdownId);
                        if (highlight.offsetTop < res.scrollTop)
                              res.scrollTop = highlight.offsetTop;
                  }
                  if (!this.isIE)
                        event.preventDefault();
                  if (highlight)
										if(highlight.firstChild.data!=vNoResults)
										{
	                  	 this.SelectionChanged(highlight.firstChild.data ); // + ".");
										}
												
									document.searchform.CanBeSubmitted.value="false";
												
            }
						else if(event.keyCode == 9) //Tab
						{

						}
            else if (event.keyCode == 27 || event.keyCode == 13) //ESC, Enter
            {
                  highlight = document.getElementById("DropDownActive");
                  if (highlight)
                        highlight.removeAttribute("id");
                  if (document.all)
                        this.closeShim(document.getElementById(this.dropdownId));
                  document.getElementById(this.dropdownId).style.display = "none";
									
									if(document.searchform.CanBeSubmitted.value=="false")
									{
											// DO NOT SUBMIT THE FORM...
											if(navigator.appName=="Microsoft Internet Explorer")
											{
												window.event.returnValue = 0;
											}
											else
											{
												event.preventDefault();
												event.stopPropagation();
											}
									}
									else if(navigator.appName=="Netscape")
									{
										//alert('1');
										//document.searchform.CanBeSubmitted.value="true";
										//window.event.returnValue = 0;
										check_form();
										//document.searchform.Func.value='Search';
										//document.searchform.SearchId.value='';
										//document.searchform.CurrentPage.value='0';
							      //document.searchform.submit();
										//alert('2');
										
									}

									document.searchform.CanBeSubmitted.value="true";
									
            }
            else
						{
                  this.Start(event);
						}
						
						var vCrit = document.getElementById(this.anchorControlId + "_img");
            this.timer = window.setTimeout(function() { vCrit.style.visibility = "hidden"; }, vDisplayTime);
						
      }
			
     
      this.Start = function(evt) 
      {
		  dbg("Start");	
            if (this.timer) 
                  window.clearTimeout(this.timer);
                  
            var keyCode = document.layers ? evt.which : document.all ? evt.keyCode : evt.keyCode;
      
            if (keyCode != 13)
            {
                  var thisObj = this;
                  this.timer = window.setTimeout(function() { thisObj.Search() }, 200);
            }
      }
      
      this.Search = function() 
      {
			  //alert("Search");
			
//				document.searchform.CanBeSubmitted.value="false";
			
            var query = document.getElementById(this.anchorControlId).value;
            if (this.lastQuery != query) 
            {     
                  if (this.httpReq && this.httpReq.readyState < 4) 
                        this.httpReq.abort();
                  if (query == "") 
                  {
                        this.lastQuery = "";
                        this.hideDropDown();
                        return false;
                  }
									
									
									var vCrit = document.getElementById(this.anchorControlId + "_img");
									vCrit.style.visibility = "visible";
   	              this.timer = window.setTimeout(function() { vCrit.style.visibility = "hidden"; }, vDisplayTime);
								
									
									
			            if (window.XMLHttpRequest) 
			            {
										this.httpReq = new XMLHttpRequest();
			            } 
			            else
									{ 
				            this.httpReq = new ActiveXObject("Microsoft.XMLHTTP");
									}
									
                  var thisObj = this;
                  this.httpReq.onreadystatechange= function () { thisObj.readyStateChange() };
//                  this.httpReq.open("GET", this.url + encodeURIComponent(query));
                  this.httpReq.open("GET", this.url + escape(query) + "&end=1");
                  this.httpReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
                  this.lastQuery = query;
                  this.httpReq.send(null);
            }
      }     
      
      this.readyStateChange = function()
      {
	  		/*
			 *	pay attention that custom attribute is not supported by Firefox and Netscape
			 */
			if (this.httpReq.readyState == 4 && this.httpReq.responseText != "") 
            {     
                var inner = document.getElementById(this.dropdownId + "_inner");
 				var items = this.GetItems(this.httpReq.responseText);
				var html = "<div id='DropDownResults' class='DropDownResults'>";	
				for(var i = 0; i < items.items.length; i++)
					html += "<div  style='white-space: nowrap; " + items.styles[i] + "' onmouseover='" + this.instanceName + ".dropdownHover(this)' onclick='" + this.instanceName + ".dropdownClicked(this)' DDdata='" + items.data[i] + "'>" + items.items[i] + "</div>";
				html += "</div>";
				inner.innerHTML = html;

				var ddown = document.getElementById(this.dropdownId);
				ddown.scrollTop = 0;
				if (items.items.length > 0)
				{
//					if (items.items.length > 20)
//					{
						ddown.style.height = "300px";
						ddown.style.overflow = "auto";
//					}
//					else
//					{
//						ddown.style.height = "";
//						ddown.style.overflow = "auto";
//					}
					ddown.style.display = "";
					if (document.all)
						this.openShim(ddown);
				}
				else
				{
					ddown.style.display = "none";
					if (document.all)
						this.closeShim(ddown);
				}
            }
      }
      
      this.dropdownHover = function(el) 
      {
			
			document.searchform.CanBeSubmitted.value="false";
			
		  dbg("dropdownHover");	
                  /*
									var highlight;
									highlight = document.getElementById("DropDownActive");
                  if (highlight)
                        highlight.removeAttribute("id");
                  el.setAttribute("id","DropDownActive");
									if(el.firstChild.data!=vNoResults)
									{
										this.SelectionChanged(el.firstChild.data ); //+ ".");
									}
									*/
									
      }
      
      this.dropdownClicked = function(el) 
      {
		  dbg("dropdownClicked");	
                  highlight = document.getElementById("DropDownActive");
                  if (highlight)
                        highlight.removeAttribute("id");
                  el.setAttribute("id","DropDownActive");
									if(el.firstChild.data!=vNoResults)
									{
                  	return this.Clicked(el.firstChild.data ); //+ ".");
									}
      }
      
      //Creates a new shim for the dropdown
      this.createShim = function(dropdown)
      {
            if (dropdown==null) return null;

            var shim = document.createElement("<iframe scrolling='no' frameborder='0'"+
                                                "style='position:absolute; top:0px;"+
                                                "left:0px; display:none'></iframe>"); 
            shim.name = this.getShimId(dropdown);
            shim.id = this.getShimId(dropdown);
            //Unremark this line if you need your menus to be transparent for some reason
            //shim.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";

            if (dropdown.offsetParent==null || dropdown.offsetParent.id=="")
            {
                  window.document.body.appendChild(shim);
            }
            else 
            {
                  dropdown.offsetParent.appendChild(shim);
            }

            return shim;
      }     
      //Creates an id for the shim based on the dropdown id
      this.getShimId = function(dropdown)
      {
            if (dropdown.id==null) return "__shim";
            return "__shim"+dropdown.id;
      }

      //Returns the shim for a specific dropdown
      this.getShim = function(dropdown)
      {
            return document.getElementById(this.getShimId(dropdown));
      }     
      
      //Opens a shim, if no shim exists for the menu, one is created
      this.openShim = function(dropdown)
      {           
          if (dropdown==null) 
            return;
          var shim = this.getShim(dropdown);
          if (shim==null) shim = this.createShim(dropdown,this.getShimId(dropdown));
          
          //Change menu zIndex so shim can work with it
          dropdown.style.zIndex = 1000;
          
          var width = dropdown.offsetWidth;
          var height = dropdown.offsetHeight;
    
          shim.style.width = width;
          shim.style.height = height;
          shim.style.left = dropdown.offsetLeft + "px";
          shim.style.top = dropdown.offsetTop + "px";
          shim.style.zIndex = dropdown.style.zIndex - 1;
          shim.style.position = "absolute";
          shim.style.display = "block";
      }
      
      //Closes the shim associated with the dropdown
      this.closeShim = function(dropdown)
      {
          if (dropdown==null) return;
          var shim = this.getShim(dropdown);
          if (shim!=null) shim.style.display = "none";
      }     
}


function SubmitIfEnter(event,quoi)
{

	if (event.keyCode==13)
	{
		check_form();
	}
	
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
