//hv_content_no_tloc\..\ffsuggest_deatch.js, v.20080418

function FFSuggest() {
	
	var pRequest;
	var pLayer;
	var pDebug			= false;
	var pInstanceName		= "";
	var pSearchURL			= "";
	var pQueryParamName		= "";
	var pFormname 			= "";
	var pLayerName			= ""
	var pQueryInput;
	var pSuggest;
	var pLastQuery;
	var pCurrentSelection		= -1;
	var pCellSpacing		= 0;
	var pHighlightBgColor 		= "#ebebeb";
	var pHighlightTextColor 	= "#000000";	
	var pStandardBgColor 		= "#FCFCFC";
	var pStandardBgColor1 		= "#ebebeb";
	var pStandardTextColor 		= "#4b453f";
	var firstLoad 			= true;
	var firstKeyPress		= true;
	var partsX 			= 0;
	
	this.init = function(searchURL, queryParamName, divLayername, instanceName, debugMode) {
		
		pSearchURL		= searchURL;		
		pQueryParamName	= queryParamName;
		pLayerName		= divLayername;
		pInstanceName	= instanceName;
		pDebug			= debugMode;
		if (pSearchURL == "") {		
			//if (pDebug) alert("no searchurl defined");
			return null;
		} else if (pInstanceName == "") {
			//if (pDebug) alert("no instancename defined");
			return null;
		
		} else if (pQueryParamName == "") {
			//if (pDebug) alert("no queryparamname defined");
			return null;
		} else if (pLayerName == "") {
			//if (pDebug) alert("need a layer for output");
		}
		pQueryInput = document.getElementById(queryParamName);
		pQueryInput.onkeyup	= handleKeyPress;	
		//pQueryInput.onfocus	= handleFocus;
		//pQueryInput.onblur	= handleBlur;
		pQueryInput.onclick	= handleFirstClick;

		//document[pFormname].onsubmit = handleSubmit;
		
	}
	
	this.setHighlightColors = function(highlighBackgroundColor, highlighTextColor) {
		pHighlightBgColor	= highlighBackgroundColor;
		pHighlightTextColor	= highlighTextColor;
	}
	
	this.setStandardColors = function(standardBackgroundColor, standardTextColor) {
		pStandardBgColor	= standardBackgroundColor;
		pStandardTextColor	= standardTextColor;
	}
	
	this.setCellspacing = function(cellspacing) {
		pCellSpacing = cellspacing;
	}
	
	function handleBlur() {
		if(pQueryInput.value == ""){
			pQueryInput.value = "- Begriff oder Artikelnummer -";
			}
		hideLayer();	
	}

	function handleFocus() {
		if(pQueryInput.value == "- Begriff oder Artikelnummer -"){
			if(!firstLoad)
			pQueryInput.value = "";
			}
		showLayer();
		firstLoad = false;	
	}	
	function handleFirstClick() {
		handleFocus();	
	}

	function handleSubmit() {
		
		//document[pFormname].target = "_self";
		if (pSuggest[pCurrentSelection] != undefined) {
			if (pSuggest[pCurrentSelection].split('###')[1] == "Kategorie_Test") {
				document[pFormname]["filterkategorie"].value = "__" + pSuggest[pCurrentSelection].split('###')[0] + "__";
				document[pFormname][pQueryParamName].value = "";
			}
			else {
				document[pFormname][pQueryParamName].value = pSuggest[pCurrentSelection].split('###')[0];
			}
		}
	}
	
	this.handleClick = function() {
		//document[pFormname].target = "_self";
		if (pSuggest[pCurrentSelection] != undefined) {
			if (pSuggest[pCurrentSelection].split('###')[1] == "Kategorie_Test") {
				document[pFormname]["filterkategorie"].value = "__" + pSuggest[pCurrentSelection].split('###')[0] + "__";
				document[pFormname][pQueryParamName].value = "";
				document[pFormname].submit();
			}
			else {				
				//document[pFormname][pQueryParamName].value = pSuggest[pCurrentSelection].split('###')[0];
				//document[pFormname].submit();
				
				document.getElementById(queryParamName).value = pSuggest[pCurrentSelection].split('###')[0];
				search_check_inc();
			}
		}
	}
	
	this.handleMouseOver = function(y) {
		unmarkAll();
		for (var x = 1; x <= 5; x++) {
			var tblCell = getTableCell(x, y);
			if (tblCell != null) {
				highlightSuggest(tblCell);
				pCurrentSelection = y;
			}
		}
	}
	
	this.handleMouseOut = function(y) {
		for (var x = 1; x <= 5; x++) {
			var tblCell = getTableCell(x,y);
			if (tblCell != null) {
				unmarkSuggest(tblCell);
				pCurrentSelection = -1
			}
		}
	}
	
	function handleKeyPress(evt) {		
		
		evt = (evt) ? evt : ((event) ? event : null);
		var keyCode = evt.keyCode;
		if (keyCode == 38) {
			if(firstKeyPress){			
				pCurrentSelection = -1;	
				firstKeyPress = false;
			}			
			moveSelection("up")
		}
		else if (keyCode == 40) {
			if(firstKeyPress){			
				pCurrentSelection = -1;	
				firstKeyPress = false;
			}						
			moveSelection("down");
		} 
		else if(keyCode == 13){
			if(pSuggest[pCurrentSelection] != null){
				document.getElementById('001').value = pSuggest[pCurrentSelection].split('###')[0];
			}
			search_check_inc();
			
		} 
		else {
			if (pQueryInput.value == "") {
				hideLayer();
				if (pLayer != null) pLayer.innerHTML = "";
				return null;
			}
			if (pLastQuery != pQueryInput.value) startAjax();						
			
			pLastQuery = pQueryInput.value;
			pCurrentSelection = -1;
			firstKeyPress = true;
		}
		if((pSuggest != null) && (pSuggest[pCurrentSelection] != null)){
			document.getElementById('001').value = pSuggest[pCurrentSelection].split('###')[0];	
		}
	}
	
	function moveSelection(direction) {
		
		var y = pCurrentSelection;	
		if (direction == "up")	y--;
		else 
			if((partsX-y) == 1)	//Ende eines Suggest, Cursor bleibt an der letzten Position				
				pCurrentSelection = y;
			else	
				y += 1;
		
		if (y < 0) {
			
			unmarkAll();
			pQueryInput.focus();
			pCurrentSelection	= -1;
		
		
		} else {
			
			unmarkAll();			
			for (var x = 1; x <= 5; x++) {
				var tblCell = getTableCell(x,y);
				if (tblCell != null) {					
					highlightSuggest(tblCell);
					pCurrentSelection = y;
				}
				else{
					
				}
			}
		}
		
		var query = pQueryInput.value;
		pQueryInput.value = "";
		pQueryInput.focus();
		pQueryInput.value = query; 		
	}
	
	function startAjax() {
		//alert("startAjax");
		var query = replaceRegExp(pQueryInput.value);
		// replace the search name in url
		var requestURL = pSearchURL;
		requestURL = pSearchURL.replace('XREPLACEX',query)+pQueryInput.value;
		try {
			if( window.XMLHttpRequest ) {
				pRequest = new XMLHttpRequest();
			} else if( window.ActiveXObject ) {
				pRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
				
				//pRequest = new CreateObject("Microsoft.XMLHTTP"); 

				
			} else {
				// if (pDebug) alert( "" );
			}
			
			pLayer = document.getElementById(pLayerName);
			if (pLayer != null) {
				if (query != "") {
					pRequest.open( "GET", requestURL, true );
					pRequest.onreadystatechange = callbackAjax;
					pRequest.send( null );
				} else {
					hideLayer();
				}
			} else {
				//if (pDebug) alert( "no layer for output found" );
			}
		} catch( ex ) {
			hideLayer();
			if (ex == undefined) {
				//if (pDebug) alert( "Error: " + ex.getmessage );
			} else {
				//if (pDebug) alert( "Error: " + ex );
			}
		}
	}
	

	function hideLayer() {
		//alert("hideLayer");
		if (pLayer != null) {
			pLayer.style.display = "none";
		}
	}
	
	this.hideLayerOutsideCall = function() {
		if (pLayer != null) {
			pLayer.style.display = "none";
		}
	}
	
	function showLayer() {
		//alert("showLayer");
		if (pLayer != null && pSuggest.length >= 1) {
			pLayer.style.display	= "block";
		}
	}
	
	function callbackAjax() {
		//alert("callbackAjax");
		if (pRequest.readyState == 4) {
			if (pRequest.status != 200) {
				hideLayer();
				//if (pDebug) alert( "Error (" + pRequest.status + "): " + pRequest.statusText );
			} else {
				handleResponse("\r\n\r\n\r\n\r\n"+trim(pRequest.responseText));
				
			}
		}
    }
	
	function handleResponse(text) {
		//alert("handleResponse");
		pCurrentSelection = -1;
		pSuggest = new Array();
		pSuggest = text.split("\n");
		var outputText = '<table cellpadding="' + pCellSpacing + '" cellspacing="0" class="' + pLayerName + '" width="100%" border="0" onMouseDown="' + pInstanceName + '.handleClick();">';
		var pNewSuggest = new Array();
		for (var i in pSuggest) {
			var firstChar = pSuggest[i].charCodeAt(0);
			if (firstChar != 13 && firstChar != 10 && pSuggest[i].length >= 1) {
				pNewSuggest.push(pSuggest[i]);
			}
		}
		//alert("handleResponse pSuggest mit Werten");
		pSuggest = pNewSuggest;
		for (var i in pSuggest) {
			pSuggestParts = new Array();
			pSuggestParts = pSuggest[i].split("###");
		if(pSuggestParts[2]!= null){
			
			outputText += '<tr>';
			outputText += '	<td nowrap="nowrap" id="' + pLayerName + '_' + i + '_1" style="background-color: ' + pStandardBgColor + '; color: ' + pStandardTextColor + ';padding: 2 2 2 0px;" onMouseOver="' + pInstanceName + '.handleMouseOver(' + i + ');" onMouseOut="' + pInstanceName + '.handleMouseOut(' + i + ');">';			
			outputText += pSuggestParts[0];
			outputText += '	</td>';			
			outputText += '	<td nowrap="nowrap"   style="background-color: ' + pStandardBgColor1 + ';padding 2 2 2 0px;"  width="30%" align="right">'; 
			outputText += pSuggestParts[2];
			outputText += ' </td>';
			outputText += '</tr>';				
			
			partsX ++;
			}				
		}
		outputText += '</table>';
		//alert("OutputText:" +outputText);
		if (pSuggest.length >= 1) {
			showLayer();
			pLayer.innerHTML		= outputText;
		} else {
			hideLayer();
			pLayer.innerHTML		= "";
		}
		unmarkAll();
	}
	
	function highlightSuggest(tblCell) {
		tblCell.style.backgroundColor	= pHighlightBgColor;
		tblCell.style.color		= pHighlightTextColor;
		tblCell.style.cursor		= "pointer";
	}
	
	function unmarkSuggest(tblCell) {
		tblCell.style.backgroundColor	= pStandardBgColor;
		tblCell.style.color		= pStandardTextColor;		
	}
	

	
	function unmarkAll() {
		var tblCell;
		for (var y in pSuggest) {
			for (var x = 1; x <= 5; x++) {
				tblCell = getTableCell(x,y);
				if (tblCell != null) {
					unmarkSuggest(tblCell);
				}
			}
		}
	}
	
	function getTableCell(x, y) {
		var tblCell;		
		tblCell = document.getElementById(pLayerName + "_" + y + "_" + x);		
		return tblCell;
	
	}

	function trim (zeichenkette) {
	  // Erst fuehrende, dann Abschliessende Whitespaces entfernen
	  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
	}

	function replaceRegExp(query) {
		query = query.replace('?','ue');
		query = query.replace('?','ue');
		query = query.replace('?','ae');
		query = query.replace('?','ae');
		query = query.replace('?','oe');
		query = query.replace('?','oe');
		query = query.replace('?','ss');
		query = query.replace(' ','');
		query = query.replace('`','');
		query = query.replace('?','');
		query = query.replace('+','');
		query = query.replace('-','');
		query = query.replace('%','');
		return query;
	}

}