// Zufallszahl erzeugen und zurück liefern
function GetRandom( min, max ) {
        if( min > max ) {
                return( -1 );
        }
        if( min == max ) {
                return( min );
        }
 
        return( min + parseInt( Math.random() * ( max-min+1 ) ) );
} 

// Show/Hide elements
function show( elementId ) {
	var e = document.getElementById( elementId );
	if( e ) e.style.display = 'block';
}
function hide( elementId ) {
	var e = document.getElementById( elementId );
	if( e ) e.style.display = 'none';
}

// Funktion zum ermitteln des aktuellen HeadTeasers
function SetMausi() {
	var AnzahlHeadTeaser = 5;
	var AktuellerHeadTeaser = GetRandom(1,AnzahlHeadTeaser);
	var HeadTeaserPrefix = 'HeadTeaser';

	//for (var i=1; i <= AnzahlHeadTeaser; i++) {
	//   HeadTeaserName = HeadTeaserPrefix + i;
	//   if (i == AktuellerHeadTeaser) {
	//      show(HeadTeaserName);
	//      }
	//    else {
	//      hide(HeadTeaserName);
	//      }
	//   }
	
	HeadTeaserName = HeadTeaserPrefix + AktuellerHeadTeaser;
	show(HeadTeaserName);
	//$(document).ready(function() {
  	//   $('div#HeadTeaser2').slideToggle("slow");
  	//})
  	
  	return HeadTeaserName;
  	
}

function $( idName ) { return document.getElementById( idName ); }
function pulldownMenu( id ) {        
	var that = this;
	var tds = $(id).getElementsByTagName('td');
	for( var i in tds ) {
		tds[i].onmouseover = function() { that.display( this, true ); }
		tds[i].onmouseout = function() { that.display( this, false ); }
		tds[i].onclick = function() { that.click( this ); }
	}

	this.display = function( e, status ) {              
	        var cnParts = /^(\w+)/.exec(e.className);        
		var cn = cnParts[1]; 
		e.className = status ? cn + ' ' + cn + 'Hover' : cn;				
		var children = e.getElementsByTagName( 'div' );
		for( i in children ) {
			if( children[i].className == 'sub' ) {
				children[i].style.display = status ? 'block' : 'none';
			}
		}
	}
	
	this.click = function( e ) {
		if( e.getElementsByTagName ) {
			var a = e.getElementsByTagName('A')[0];
			document.location.href = a.href;
			return false;
		}
	}
}

function katalogPlusKey( e ) {
//	if (!e) {
//		e = window.event;
//	}
//	if( (window.Event && e.which==43) || (!window.Event && e.keyCode==43) ) {
//		if( 
//			!((e.target||e.srcElement).tagName.match(/input|textarea/i)) 
//			|| ((e.target||e.srcElement).id == 'searchQuery' && (e.target||e.srcElement).value.length == 0)
//		) {
//			window.location='/cgi-bin/Content.pl?OnlineKatalog-KatalogPlusThemenUebersicht';
//		}
//	}
}
document.onkeypress = katalogPlusKey;

function setCheckBoxes( form, status ) {
	var i;
	for( i=0;i<form.length;i++ ) {
		var el=form.elements[i];
		if (el.type!="checkbox") continue;
		if (el.name.substring(0,2)!="L_") continue;
		el.checked = status;
	}
}

function testVerpackungseinheit(form,name,packung,artikel) {
	artikel = typeof(artikel) == 'undefined' ? "" : "'"+artikel+"' ";
	x=form.elements[name].value;
	y=x % packung;
	if (y==0) {
		return true;
	}
	alert("Die Verpackungseinheit für den Artikel "+artikel+"beträgt "+packung+". Bitte ändern Sie die Bestellmenge entsprechend ab.");
	return false;
}

function testMindestbestellmenge(form,name,mindestmenge,artikel) {
	artikel = typeof(artikel) == 'undefined' ? "" : "'"+artikel+"' ";
	x=form.elements[name].value;
	if ( (x==0) || (x>=mindestmenge) ) return true;
	if (confirm(
"Sie haben gerade die übliche Mindestbestellmenge unterschritten. Helfen\n"+
"Sie uns bei einer kostengünstigen Auftragsabwicklung indem Sie die\n"+
"Mindestbestellmenge einhalten. Vielen Dank. Mit OK wird automatisch die\n"+
"Mindestbestellmenge von "+mindestmenge+" für den Artikel "+artikel+"übernommen, mit\n"+
"Abbrechen bleibt die von Ihnen gewünschte Stückzahl erhalten.")) {
		form.elements[name].value=mindestmenge;
	}
	return true;
}

var upsellfenster=null;
function testUpsell(form, preis, spar, menge_min, menge_max) {
	if (form.elements["UpsellGesehen"].value==1) {
		if (upsellfenster!=null) upsellfenster.close();
		return true;
	}
	var x=form.elements["Anzahl"].value.replace(/,/,".");
	var i;
	for (i=0;i<menge_min.length;i++) {
		if (menge_min[i]<=x && x<menge_max[i]) break;
	}
	if (i<menge_min.length && form.elements["UpsellGesehen"].value!=1) {		
		form.elements["UpsellGesehen"].value=1;
		var mehr=menge_max[i]-x;
		var neu=menge_max[i];
		var alter_preis=x*preis[i-1];
		var neuer_preis=neu*preis[i];
		var spar_proz=(1-(preis[i]/preis[i-1]))*100;
		if (spar_proz > 1) {
                   upsellfenster=open("/cgi-bin/OnlineKatalog/Upsell-Info.pl?ALTER_PREIS="+alter_preis+"&NEUER_PREIS="+neuer_preis+"&SPAR_PROZ="+spar_proz+"&MENGE_MEHR="+mehr+"&MENGE_NEU="+neu,"artikelaenderung","scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,dependent=yes,width=350,height=200");
                   return false;
                   }
	}
}

function testKombination(form) {
	var erg;
	var alles=new Array();
	var gesamt=0;
	for (var i=0;i<form.elements.length;i++) {
		var el=form.elements[i];
		if (el.name.substring(0,1)!="M") continue;
		if (el.type=="select-one") {
			var ar=el.options[el.selectedIndex].value.split("_");
		}
		else if (el.type=="hidden") {
			var ar=el.value.split("_");
		}
		else {
			continue;
		}
		for (var j in ar) {
			if (ar[j]>9999) {
				alert("Sie haben noch nicht alle Eigenschaften des Artikels festgelegt.\nBitte vervollständigen Sie Ihre Angaben.");
				return false;
			}
			if (alles[ar[j]]==null) alles[ar[j]]=1; else alles[ar[j]]++;
		}
		gesamt++;
	}
	var anzahl=0;
	for (x in alles) {
		if (alles[x]==gesamt) anzahl++;
	}
	erg=(gesamt==0 || anzahl>0);
	if (!erg) alert(unescape("Diese Kombination ist nicht erlaubt. Bitte w%E4hlen Sie in den Eingabefeldern nur Merkmale ohne '*' aus."));
	return erg;
}

function initTest(form) {
	for (var i=0;i<form.elements.length;i++) {
		var el=form.elements[i];
		if (el.type=="select-one") {
			testFeld(el);
			break;
		}
	}
}

function testFeld(element) {
	var akt=element.options[element.selectedIndex].value.split("_"); // Array
	if (element.options[0].selected && element.options[0].value.indexOf("_")<0 && element.options[0].value>9999) {
		for (var i=0;i<element.form.elements.length;i++) {
			var el=element.form.elements[i];
			if (el.type!="select-one") continue;
			if (el.name.substring(0,1)!="M") continue;
			for (j=0;j<el.length;j++) {
				var op=el.options[j]
				if (op.text.charAt(0)=='*') op.text=op.text.substr(1);
			}
		}
		return;
	}
	for (var m=0;m<element.length;m++) {
		var op=element.options[m];
		if (op.text.charAt(0)=='*') op.text=op.text.substr(1);
	}
	for (var i=0;i<element.form.elements.length;i++) {
		var el=element.form.elements[i];
		if (el==element) continue;
		if (el.type!="select-one") continue;
		if (el.name.substring(0,1)!="M") continue;
		var element_option_autoselect=null;
		var element_count=0;
		for (j=0;j<el.length;j++) {
			var ok=0;
			var op=el.options[j]
			var cur=op.value.split("_");
			for (k=0;k<cur.length&&!ok;k++) {
				if (cur[k]>9999) ok=1;
				for (l=0;l<akt.length&&!ok;l++) {
					if (cur[k]==akt[l]) ok=2;
				}
			}
			var str;
			if (op.text.charAt(0)=='*') op.text=op.text.substr(1);
			if (ok==0) {
				op.text="*"+op.text; 
			} else if (ok==2) { // War nicht "Bitte waehlen"
				element_count++;
				element_option_autoselect=op;
			}
		}
		if (element_count==1 && el.options[0].selected==true) {
			element_option_autoselect.selected=true;
		}
	}
}

function warnungVerlassen() {
	if( typeof(geandert) == 'undefined' || geaendert==0 ) return true;
	return !confirm('Sie haben Werte geändert, die Änderungen aber nicht bestätigt! Drücken Sie "Okay" zum Weiterbearbeiten und bestätigen Sie anschliessend Ihre Änderungen mit dem "Artikel in Warenkorb"-Button. Mit "Abbrechen/Cancel" können Sie die Änderungen verwerfen.');
}

function warnungVerlassen2() {
	if( typeof(geandert) == 'undefined' || geaendert==0 ) return true;
	return !confirm('Sie haben Werte geändert, die Änderungen aber nicht bestätigt! Drücken Sie "Okay" zum Weiterbearbeiten und bestätigen Sie anschliessend Ihre Änderungen mit dem "Änderungen übernehmen"-Button. Mit "Abbrechen/Cancel" können Sie die Änderungen verwerfen.');
}


function popup( url, width, height ) {
	var wnd = window.open( url, "popup2", "scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,width="+width+",height="+height );
	wnd.focus();
	return (wnd == null);
}
function popupNR( url, width, height ) {
	var wnd = window.open( url, "popup2", "scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,width="+width+",height="+height );
	wnd.focus();
	return (wnd == null);
}

function contentWindow( name ) {
	var wnd = window.open("/cgi-bin/Content.pl?" + name + '&popup', "contentPopup", "scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,width=800,height=600");
	wnd.focus();
	return (wnd == null);
}

function pictureWindow( kopfnr, bild ) {
	var wnd = window.open( '/cgi-bin/OnlineKatalog/GrossbildGalerie.pl?kopfnr='+kopfnr + '&bild='+ bild +'&popup=1','BildFenster','width=700,height=600,scrollbars=yes,directories=no,menubar=no,resizable=yes,taskbar=no,toolbar=no');
	wnd.focus();
	return (wnd == null);
}

function openBlaetterkatalog( seite ) {
	seite = typeof(seite) != 'undefined' ? '&seite='+seite : '';
	var wnd = window.open('/cgi-bin/Content.pl?Blaetterkatalog&popup'+seite, 'blaetterkatalog','width=1020,height=740,menubar=no,status=no,locationbar=no,scrollbars=no,resizable=yes');
	wnd.focus();
	return (wnd == null);
}



function openInMain( url, keepOpen, popupToFocus ) {
	if( opener && opener.opener ) {
		
		if( opener.opener.location.href.match( new RegExp(encodeRE(url)+"$","i")) ) {
			opener.opener.location.reload();
		} else {
			opener.opener.location.href = url;
		}
		if( opener.name != 'blaetterkatalog' && (typeof(keepOpen) == undefined || !keepOpen) ) {
			opener.close();
		} 
		if( typeof(popupToFocus) != 'undefined' ) refocusPopup( opener.opener, url, popupToFocus, 0 );
	} else if( opener ) {
		if( opener.location.href.match( new RegExp(encodeRE(url)+"$","i")) ) {
			opener.location.reload();
		} else {
			opener.location.href = url;
		}
		if( typeof(popupToFocus) != 'undefined' ) refocusPopup( opener, url, popupToFocus, 0 );
	}
	if( typeof(keepOpen) == 'undefined' || !keepOpen ) {
		window.close();
	}
	return false;
}

function refocusPopup( mainWnd, mainUrl, wnd, count ) {
	if( count < 20 && wnd && mainWnd && !mainWnd.location.href.match( new RegExp(encodeRE(mainUrl)+"$","i")) ) {
		count++;
		setTimeout( function() { refocusPopup( mainWnd, mainUrl, wnd, count ) }, 250 );
	} else {
		setTimeout( function() { wnd.focus() }, 500 );
	}
}

function reloadMain() {
	if( opener && opener.opener ) {
		opener.opener.location.reload(); 
	} else if( opener ) {
		opener.location.reload(); 
	}
}

function scroller( instanceName, containerId, itemPrefix, buttonPrefix, numItems, total ) {
	this.containerId = containerId;
	this.itemPrefix = itemPrefix;
	this.buttonPrefix = buttonPrefix;
	this.numItems = numItems;
	this.currentItem = 1;
	this.scrollSpeed = 10;
	this.instanceName = instanceName;
	this.total = typeof(total) != 'undefined' ? total: 0;
	
	this.scrollerInterval = 0;
	this.autoScrollInterval = 0;
	
	this.prev = function() {
		this.setTarget( (this.currentItem > 1 ? this.currentItem - 1 : 1) );
	}	
	this.next = function() {
		this.setTarget( (this.currentItem < this.numItems ? this.currentItem + 1 : this.numItems) );
	}
	this.first = function() {
		this.setTarget( 1 );
	}
	this.end = function() {
		this.setTarget( this.numItems );
	}	
	this.nextContinuous = function() {
		this.setTarget( (this.currentItem < this.numItems ? this.currentItem + 1 : 1) );
	}
	this.prevContinuous = function() {
		this.setTarget( (this.currentItem > 1 ? this.currentItem - 1 : this.numItems) );
	}
	
	
	this.setTarget = function( targetNumber ) {
		this.currentItem = targetNumber > 0 ? targetNumber : 1;
		this.setStatus();
		
		var targetId = this.itemPrefix + targetNumber;
		var a = $( targetId );
		if( !a ) return;
		clearInterval( this.scrollerInterval );
		this.scrollerInterval = setInterval( this.instanceName + '.scrollTo('+a.offsetLeft+')', 10 );
		return false;
	}
	
	this.scrollTo = function( x ) {
		var box = $( this.containerId );
		if( !box ) return;
		
		i = box.innerWidth || box.clientWidth;
		w = box.scrollWidth;
		c = box.scrollLeft;
		n = c;
		
		if( x > c ) {
			if( w - x > i ) {
				n += Math.ceil( (x - c) / this.scrollSpeed )
			}
			else {
				n += Math.ceil( (x - c - (w - x)) / this.scrollSpeed )
			}
		} else {
			n = c + ( x - c ) / this.scrollSpeed;
		}
		box.scrollLeft = n;
		
		if( box.scrollLeft == x || Math.abs( box.scrollLeft - c ) < 1 ) {
			clearInterval( this.scrollerInterval );
		}
	}
	this.setStatus = function() {
		var left = this.currentItem > 1 ? true : false;
		var right = this.currentItem < this.numItems ? true : false;
		if( $(this.buttonPrefix + 'Begin') ) $(this.buttonPrefix + 'Begin').className = left ? "imageScrollAktiv" : "imageScrollInaktiv";
		if( $(this.buttonPrefix + 'Prev') ) $(this.buttonPrefix + 'Prev').className = left ? "imageScrollAktiv" : "imageScrollInaktiv";
		if( $(this.buttonPrefix + 'Next') ) $(this.buttonPrefix + 'Next').className = right ? "imageScrollAktiv" : "imageScrollInaktiv";
		if( $(this.buttonPrefix + 'End') ) $(this.buttonPrefix + 'End').className = right ? "imageScrollAktiv" : "imageScrollInaktiv";
		
		if( $(this.buttonPrefix + 'Position') ) { 
			$(this.buttonPrefix + 'Position').innerHTML = this.currentItem + 
			(this.total > 1
				? '-' + (
					this.currentItem+this.total-this.numItems > this.total 
					? this.total
					: this.currentItem+this.total-this.numItems
				) 
				: ''
			) + 
			' / ' + 
			(this.total);
		}
	}

	this.setAuto = function( interval ) {
		clearInterval( this.autoScrollInterval );
		if( interval > 0 ) {
			this.autoScrollInterval = setInterval( this.instanceName + '.nextContinuous()', interval );
		}
	}
}

// Grossbildansicht
var imageLayerScrollInterval = 0;
function showImageLayer( index, src ) {
	$('grossBild').src = src;
	$('grossBild').onload = showImage;
	$('grossBildUnterschrift').innerHTML = $('bildUnterschrift_' + index).innerHTML;
	if( $('bildwk_' + index) ) {
		$('grossBildWK').innerHTML = $('bildwk_' + index).innerHTML;
	} else {
		$('grossBildWK').innerHTML = "";
	}
	$('mask').style.display = 'block';
	return false;
}

function showImage() {
	$('bildLayer').style.display = 'block';
	$('grossBildContainer').style.top = 
			(((document.documentElement.clientHeight) - $('grossBildContainer').scrollHeight )/2) + "px";

	scrollImageLayer();
	clearInterval( imageLayerScrollInterval );
	imageLayerScrollInterval = setInterval( scrollImageLayer, 50 );
}

function hideImageLayer() {
	clearInterval( imageLayerScrollInterval );
	$('bildLayer').style.display = 'none';
	$('mask').style.display = 'none';
	$('grossBild').src = "";
}

function scrollImageLayer() {
	$('bildLayer').style.top = document.documentElement.scrollTop + "px";
	$('bildLayer').style.height = document.documentElement.clientHeight + "px";
	$('mask').style.top = document.documentElement.scrollTop + "px";
	$('mask').style.height = document.documentElement.clientHeight + "px";
}



// Suggest Suche
var lastScroll = 0;
function FFSuggest() {
	
	var pRequest;
	var pLayer;
	var pDebug=true;
	var pInstanceName			= "";
	var pSearchURL				= "";
	var pQueryParamName			= "";
	var pFormname 				= "";
	var pLayerName				= "";
	var pQueryInput;
	var pSuggest				= new Array();
	var pLastQuery;
	var pCurrentSelection		= 0;
	var pCellSpacing			= 0;
	var pHighlightBgColor 		= "#e5e5e5";
	var pHighlightTextColor 	= "#0063C4";
	var pStandardBgColor 		= "#FCFCFC";
	var pStandardTextColor 		= "#00376D";
	var pSuggestQueryStyle 		= "font-weight: bold; padding: 2 15 2 10px;";
	var pSuggestTypeStyle 		= "padding: 2 5 2 15px;";
	var submitted				= false;
	
	this.init = function(searchURL, formname, queryParamName, divLayername, instanceName, debugMode) {
		pSearchURL		= searchURL;
		pFormname		= formname;
		pQueryParamName	= queryParamName;
		pLayerName		= divLayername;
		pInstanceName	= instanceName;
 		// pDebug			= debugMode;
		pDebug=true;
		if (pSearchURL == "") {		
			if (pDebug) alert("no searchurl defined");
			return null;
		} else if (pInstanceName == "") {
			if (pDebug) alert("no instancename defined");
			return null;
		} else if (pFormname == "") {
			if (pDebug) alert("no formname 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[pFormname][pQueryParamName];
		pQueryInput.onkeyup	= handleKeyPress;
		pQueryInput.onfocus	= showLayer;
		pQueryInput.onblur	= hideLayer;
		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 handleSubmit() {
		submitted = true;
		if (pSuggest[pCurrentSelection] != undefined) {
			if (pSuggest[pCurrentSelection].split('###')[2] == "Kategorie") {
				// document[pFormname]["filterkategorie"].value = "__" + pSuggest[pCurrentSelection].split('###')[0] + "__";
				//document[pFormname][pQueryParamName].value = "";
                                document[pFormname][pQueryParamName].value = "Kategorie " + pSuggest[pCurrentSelection].split('###')[0];
			}
			else {
				document[pFormname][pQueryParamName].value = pSuggest[pCurrentSelection].split('###')[0];
			}
		}
	}
	
	this.handleClick = function() {
		if (pSuggest[pCurrentSelection] != undefined) {
			if (pSuggest[pCurrentSelection].split('###')[2] == "Kategorie") {
				// document[pFormname]["filterkategorie"].value = "__" + pSuggest[pCurrentSelection].split('###')[0] + "__";
				// document[pFormname][pQueryParamName].value = "";
				document[pFormname][pQueryParamName].value = "Kategorie " + pSuggest[pCurrentSelection].split('###')[0];
				document[pFormname].submit();
			}
			else {
				document[pFormname][pQueryParamName].value = pSuggest[pCurrentSelection].split('###')[0];
				document[pFormname].submit();
			}
		}
	}
	
	this.handleMouseOver = function(pos) {
		var tblCell = getTableCell(pos);
		unmarkAll();
		if (tblCell != null) {
			highlightSuggest(tblCell);
			pCurrentSelection = pos;
		}
	}
	
	this.handleMouseOut = function(pos) {
		var tblCell = getTableCell(pos);
		if (tblCell != null) {
			unmarkSuggest(tblCell);
			pCurrentSelection = -1
		}
	}
	
	function handleKeyPress(evt) {
		evt = (evt) ? evt : ((event) ? event : null);
		var keyCode = evt.keyCode;
		if (keyCode == 38) {
			moveSelection("up")
		} else if (keyCode == 40) {
			moveSelection("down");
		} else {
			if (pQueryInput.value == "") {
				hideLayer();
				if (pLayer != null) pLayer.innerHTML = "";
				return null;
			}
			if (pLastQuery != pQueryInput.value) startAjax();
			pLastQuery = pQueryInput.value;
		}
	}
	
	function moveSelection(direction) {
		var pos = pCurrentSelection;
		if (direction == "up")	pos--;
		else 					pos += 1;
		
		if (pos < 0) {
			unmarkAll();
			pQueryInput.focus();
			pCurrentSelection	= -1;
		} else {
			var tblCell = getTableCell(pos);
			if (tblCell != null) {
				unmarkAll();
				highlightSuggest(tblCell);
				pCurrentSelection = pos;
			}
		}
		
		var query = pQueryInput.value;
		pQueryInput.value = "";
		pQueryInput.focus();
		pQueryInput.value = query; 
	}
	
	function startAjax() {
		var query = pQueryInput.value;
//		var query = query1.replace(/(\d\d\d\d)\d+/g, $[1]);
//		alert (query1 + "---" + query);
		var requestURL = pSearchURL + "?" + pQueryParamName + "=" + escape(query);
		
		try {
			if( window.XMLHttpRequest ) {
				pRequest = new XMLHttpRequest();
			} else if( window.ActiveXObject ) {
				pRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
			} else {
				if (pDebug) alert( "" );
			}
			
			pLayer = $(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() {
		if (pLayer != null) {
			pLayer.style.display = "none";
		}
	}
	
	this.hideLayerOutsideCall = function() {
		if (pLayer != null) {
			pLayer.style.display = "none";
		}
	}
	
	function showLayer() {
		if (pLayer != null && pSuggest != null && pSuggest.length >= 1) {
			var pos = findPos( $('suggestContainer') );
			pLayer.style.top = pos.top + 'px';
			pLayer.style.left = pos.left + 'px';
			pLayer.style.display	= "block";
		}
	}
	
	function callbackAjax() {
		if (submitted == false) {
			if (pRequest.readyState == 4) {
				if (pRequest.status != 200) {
					hideLayer();
					if (pDebug) alert( "Error (" + pRequest.status + "): " + pRequest.statusText );
				} else {
					handleResponse(pRequest.responseText);
				}
			}
		}
  }
	
	function handleResponse(text) {
		pCurrentSelection = -1;
		pSuggest = new Array();
		pSuggest = text.split("\n");
		var outputText = '<table cellpadding="' + pCellSpacing + '" cellspacing="0" class="' + pLayerName + '" width="500" border="0" onMouseDown="' + pInstanceName + '.handleClick();">';
		outputText += '<tr class="suggestHeader" ><td class="suggestHeader" nowrap="nowrap" colspan="2">Vorschläge zu Ihrer Suche...<td></tr>';
		
		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]);
			}
		}
		pSuggest = pNewSuggest;
		var query = pQueryInput.value;
		for (var i in pSuggest) {
			pSuggestParts = new Array();
			pSuggestParts = pSuggest[i].split("###");
			
			// 4-stellige Produktnummer am Zeilenanfang entfernen
			pSuggestParts[0] = pSuggestParts[0].replace( /^\d{4}\s/i, '' ); 
			if( pSuggestParts[0].length > 60 ) {
				pSuggestParts[0] = pSuggestParts[0].substr(0,60) + '...';
			}
			// Anzahl der Ergebnise statt "Suchbegriff" anzeigen
			if( /Suchbegriff/.test(pSuggestParts[2]) ) {
				pSuggestParts[2] = pSuggestParts[1];
			}

			outputText += '<tr class="suggestEntry" id="' + pLayerName + '_' + i + '" style="background-color: ' + pStandardBgColor + ';" onMouseOver="' + pInstanceName + '.handleMouseOver(' + i + ');" onMouseOut="' + pInstanceName + '.handleMouseOut(' + i + ');">'
								+ '<td style="padding-right: 5px;" nowrap="nowrap">' + pSuggestParts[0].replace(new RegExp("("+encodeRE(query)+")","ig"),'<span class="suggestContent">$1</span>') + '</td>'
								+'<td nowrap="nowrap" width="30%">' + pSuggestParts[2] + '</td>'
								//+'<td nowrap="nowrap" align="right" width="30%">' + pSuggestParts[1] + '</td>'
						+'</tr>';
		}
		//outputText += '<tr><td  style="height:25px; background-color: ' + pStandardBgColor + '; color: ' + pStandardTextColor + '; border-top:solid 1px #5C637D; font-size:9px;" colspan="3" align="right">powered by <a onMouseDown="document.location.href=\'http://www.fact-finder.de\'"  target="_blank" href="http://www.fact-finder.de"><img src="images/result/logo_fact-finder_suggest.gif" style="border-width:0px;"/></a> &nbsp; </td></tr></table>';
		outputText += '</table>';
		
		if (pSuggest.length >= 1) {
			showLayer();
			pLayer.innerHTML		= outputText;
		} else {
			hideLayer();
			pLayer.innerHTML		= "";
		}
	}
	
	function highlightSuggest(tblCell) {
		tblCell.style.backgroundColor	= pHighlightBgColor;
		tblCell.style.color				= pHighlightTextColor;
	}
	
	function unmarkSuggest(tblCell) {
		tblCell.style.backgroundColor	= pStandardBgColor;
		tblCell.style.color				= pStandardTextColor;
	}
	
	function unmarkAll() {
		var tblCell;
		for (var i in pSuggest) {
			tblCell = getTableCell(i);
			if (tblCell != null) {
				unmarkSuggest(tblCell);
			}
		}
	}
	
	function getTableCell(pos) {
		var tblCell;
		tblCell = $(pLayerName + "_" + pos);
		return tblCell;
	}
}

function encodeRE(s) { return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1') }


// ----------------------------------------------------------------------------
// get current position of a html-node relative to the body-element
function findPos( obj ) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return {
		'left':curleft, 
		'top':curtop
	};
}

function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
			if( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}


// DF1.1 :: domFunction 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
//******************************************************
function domFunction(f, a) {
	var n = 0;
	var t = setInterval(function() {
		var c = true;
		n++;
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
			c = false;
			if(typeof a == 'object'){
				for(var i in a) {
					if( (a[i] == 'id' && $(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1) ) { 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		
		if(n >= 60) {
			clearInterval(t);
		}
	}, 250);
};
