
var tmpLength = 0;
var timer;
var articles = new Array();
var wasBg = "";
var waitTimer;

			function processInput(ident) {
				//window.setTimeout('document.getElementById("browserAct").style.display = "none";', 1000);
				
				if (ident.value.length > 2 && ident.value.length > tmpLength && ident.value.toLowerCase() != "suchwort") {
					tmpLength = ident.value.length;
					clearTimeout(timer);
					if (document.getElementById("discoveryDiv").style.display != "none") 
						startWait();
					
					timer = window.setTimeout('getResults();', 250);
					cleanResults(ident.value);
				}
				if (ident.value.length < 2) {
				 document.getElementById("discovery").innerHTML = "";
				 document.getElementById("discoveryDiv").style.display = "none";
				}
				else {
					tmpLength = ident.value.length;
				}
			}
			
			function startWait() {
				getObj("searchWait").style.display = "block";
			}
			
			function stopWait() {
				getObj("searchWait").style.display = "none";
			}
			
			
			function cleanResults(t) {
				var obj = document.getElementById("discovery");
				var i = 0;
			//	alert(obj.childNodes.length);
				while(obj.childNodes[i]) {
					if (obj.childNodes[i].childNodes[0] != null) {
							var clean = stripTags(obj.childNodes[i].childNodes[0].innerHTML);
							if (clean.toLowerCase().indexOf(t.toLowerCase()) < 0) {
								obj.removeChild(obj.childNodes[i]);
							}
							else {
								obj.childNodes[i].childNodes[0].innerHTML = replaceIgnoreCase(stripTags(getObj("search").value), "", clean);
							}
					}
					i++;
				}
			}
			
			function stripTags(t) {
				return t.replace(/<.*?>/gi, '');
			}
			
			function handleArrowKeys(evt) {
				    evt = (evt) ? evt : ((window.event) ? event : null);
				    if (evt) {
				        switch (evt.keyCode) {
										case 27:
											hideSearch();
											break;
				            case 38:
											moveMark(0);
				                break;    
				            case 40:
											moveMark(1);
			                	break;    
			/*				case 13:
											submitSearch();
				*/							
				         }
				    }
			}
			
			function hideSearch() {
				getObj("discoveryDiv").style.display = "none";
			}
					
			function moveMark(dir) {
				var c = document.getElementById("discovery");
				markedRow = parseInt(document.getElementById("mark").innerHTML);
				
				if (markedRow >= 0 && c.childNodes[markedRow]) 
					c.childNodes[markedRow].childNodes[0].style.backgroundColor = c.childNodes[markedRow].childNodes[0].getAttribute("class");

				if (dir)
					markedRow++
				else
					markedRow--;

				if (markedRow < 0) {
					document.getElementById("AdvSearchArticleNumber").value = "";
					document.getElementById("mark").innerHTML = markedRow;
					return;
				}
				if (markedRow > (c.childNodes.length -1)) markedRow = (c.childNodes.length-1);
				wasBg = c.childNodes[markedRow].childNodes[0].style.backgroundColor;
				c.childNodes[markedRow].childNodes[0].style.backgroundColor = "#BC0202";
				document.getElementById("mark").innerHTML = markedRow;
				
				document.getElementById("AdvSearchArticleNumber").value = c.childNodes[markedRow].childNodes[0].getAttribute("name");
			}
			
			function resetMarkedRow() {
			//	alert(document.getElementById("mark").innerHTML);
				if (parseInt(document.getElementById("mark").innerHTML) != -1) {
						var obj = document.getElementById("discovery");
						var i = 0;
						while(obj.childNodes[i]) {
							obj.childNodes[i].childNodes[0].style.backgroundColor=obj.childNodes[i].childNodes[0].getAttribute("class");
							i++;
						}
				}
				document.getElementById("mark").innerHTML = -1;
				document.getElementById("AdvSearchArticleNumber").value = "";
			}
			
			function submitSearch() {
				if (getObj("AdvSearchArticleNumber").value == "" && getObj("standardSearch")) {
					getObj("standardSearch").submit();
					finishAdvSearch();
				}
			}
			
			function finishAdvSearch() {
				getObj("discoveryDiv").style.display = "none";
				getObj("search").value = "Suchwort";
			}
			
			function getResults() {
				//	alert(document.getElementById("search").value);
					makeSearchAdvRequest("../modules/searchDiscovery/xgetSearchDiscovery.php", "q=" + document.getElementById("search").value);
					document.getElementById("mark").innerHTML = -1;

			}
			
			function replaceIgnoreCase(needle, rep, source) {
					var begin = source.toLowerCase().indexOf(needle.toLowerCase());
					if (begin < 0) 
						return source;
						
					var replaceString = source.substr(begin, needle.length);
					
					return source.replace(replaceString, "<b>" + replaceString + "</b>") ;
			}					
			
			function processResponse(responseText) {
				if (responseText == "") {
					return;
				}
				
				if (responseText == "0") {
					hideSearch();
					return;
				}
				
				articles = new Array();
				stopWait();
				var advSearchDiscoveryData = responseText.split("|");
				if (advSearchDiscoveryData.length < 1) {
					document.getElementById("discovery").innerHTML = "";
				 	document.getElementById("discoveryDiv").style.display = "none";
					return;
				}
				var c = document.getElementById("discovery");
				document.getElementById("discoveryDiv").style.display="inline";
				clearTable(c);
				var i = 0;
				while(advSearchDiscoveryData[i]) {
					var row = c.insertRow(c.childNodes.length);
					row.setAttribute("nowrap", "nowrap");
					var color = (i%2 == 0) ? "#2B2A53" : "#050512";
					var tmp = advSearchDiscoveryData[i].split("[x]");
			
					var td   = document.createElement("td");
			
					td.innerHTML = replaceIgnoreCase(getObj("search").value, "<b>" + getObj("search").value + "</b>", tmp[0]);
				
					td.style.backgroundColor = color; 
					td.setAttribute("class", color);
					td.style.cursor = "pointer";

					td.setAttribute("name", tmp[1]);
					td.setAttribute("id", "autoSearchResult" + tmp[1]);
					
					td.onmouseover = function() {
						//	wasBg = getObj(this.getAttribute("id")).style.backgroundColor;
							getObj(this.getAttribute("id")).style.backgroundColor = '#BC0202';
					}; 
					td.onmouseout = function() { 
						getObj(this.getAttribute("id")).style.backgroundColor = this.getAttribute("class");
					}; 
					//
					td.onclick= function() {
						loadProduct(getObj(this.getAttribute("id")).getAttribute("name"), SessionId , getObj(this.getAttribute("id")).innerHTML);
						finishAdvSearch();
						
						
					};
					
					row.appendChild(td);
					i++;
				}
			}
			
			function getTextNode(obj) {
				var gtnI = 0;
				while(obj.childNodes[gtnI]) {
					alert(obj.childNodes[gtnI] + " -> " + obj.childNodes[gtnI].nodeType);
					getTextNode(obj.childNodes[gtnI]);
					gtnI++;
				}
			}
			
			function clearTable(id) {
				while(id.childNodes.length > 0) {
					id.removeChild(id.childNodes[0]);
				}
			}
			
			function searchLoad() {
				if (http_request.readyState == 4) {
						if (http_request.status == 200) {
							var xmldoc = http_searchObject.responseXML;
							processResponse(http_searchObject.responseText);
						}
					}
			}
			
			function makeSearchAdvRequest(dUrl,pData) {
					$.ajax({
					  url: dUrl,
					  cache: false,
					  data: pData,
					  type: "POST",
					  success: function(html) {
					   	processResponse(html);
					  }
					});
				}
		
			
			/*
				http_searchObject = false;
				if (window.XMLHttpRequest) { 
					http_searchObject = new XMLHttpRequest();
					if (http_searchObject.overrideMimeType) {
						http_searchObject.overrideMimeType('text/xml');
					}
				} else if (window.ActiveXObject) {
					try {
						http_searchObject = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
						try {
						http_searchObject = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e) {}
					}
				}

				if (!http_searchObject) {
					alert('Giving up :( Cannot create an XMLHTTP instance');
					return false;
				}
				
				http_searchObject.onreadystatechange = searchLoad;
				http_searchObject.open('POST', url, true);
				http_searchObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				http_searchObject.setRequestHeader('Content-length', data.length);
				http_searchObject.send(data);
				
			}*/
			