var result = "";
var combineWith = "";
var preSelected = "";
var queryString = decodeURI(document.location.href);
var debug = false;
if( queryString.indexOf("debug=EKM") != -1 ) debug = true;

var aNav = new Array();

function drawMenu(){
	var start = new Date();
	// there is no menu to solve, quit!
	try{
		var col3_content = xGetElementById("col3_content");
		if( !col3_content ){
			var tc = col3_content.innerHTML;
			if( tc.search(/<div class="news-catmenu">/) == -1 ) {
				alert("No Menu found!");
				return;
			}
		}

		if( ! aSelectBoxTitles ){
			alert("No Menu Titles found!");
			return;
		}
	} catch(e){ alert("Could not create the menu!"); return; };
	
	// get a selection if there is any
	var selectedOptions = new Array();
	tmp = queryString.split("&");
	for( i=0; i<tmp.length; i++ ){
		for( j=0; j<aSelectBoxTitles.length; j++ ){
			if( tmp[i].split("=")[0] == "Sel" + j )
				selectedOptions[j] = tmp[i].split("=")[1]
		}
	}

	result += "<div class='floatbox'>\n";
	if( catmenu_maintitle ) result += catmenu_maintitle;
	for( b=0; b<aSelectBoxTitles.length; b++ ){
		// build the box
		result += "<h1>" + aSelectBoxTitles[b] + ":</h1>\n";
		result += "<select id='Combo_" + b + "' ";
		if( debug ){
			result += " onchange='drawProducts(this);'";
			if( document.all ){
				result += " onfocusin='setSelection(this);'>\n";
			} else {
				result += " onfocus='setSelection(this);'>\n";
			}
		} else {
//			result += " onchange='ChangeSelection(this);'";
			result += " onchange='drawProducts(this);'";
			if( document.all ){
				result += " onfocusin='setSelection(this);'>\n";
			} else {
				result += " onfocus='setSelection(this);'>\n";
			}
		}
		
		if( !selectedOptions[b] || selectedOptions[b] == "" || selectedOptions[b] == "undefined") 
			result += " <option>*</option>\n";
		else 
			result += " <option>" + selectedOptions[b] + " </option>\n";
			
		result += "</select>\n";
	}
	result += "</div>";

	if( xGetElementById("newsDropDown")){
		xGetElementById("newsDropDown").innerHTML = result;
	} else {
		alert("No element called 'newsDropDown' found to put the Navigation there!");
	}
	
	// now get the menu structures

	tc = col3_content.innerHTML.replace(/\r|\n/g, "");
	
	// get the main stock
	var catmenu = tc.split("news-catmenu-");
window.__debug = catmenu;
window.__debug1 = tc;

	aMenuOption = new Array();
	
	for( i=1; i<catmenu.length; i++ ){
	
		// get the title
		var mTitle = /href=.*?>(.*?)</;
		mTitle.exec( catmenu[i] );
		mTitle = RegExp.$1;

		// get the link
		var mLink = /href="(.*?)"/;
		mLink.exec( catmenu[i] );
		mLink = decodeURI(RegExp.$1);

		var mCategory = mLink.split("tx_ttnews[cat]=")[1].split("&")[0];
		if( catmenu[i].indexOf("src=") == -1 ) {
			aMenuOption = new Array();
			aMenuOption[0] = mTitle;
		} else {
			while( String(catmenu[i].match(/(join|line|joinbottom|blank)\.gif/g)).split(",").length < aMenuOption.length ) {
				aMenuOption.pop();
			}
			aMenuOption[aMenuOption.length] = mTitle;
		}

		aNav[aNav.length] = new Array( mTitle, mLink, mCategory, aMenuOption.join("||").split("||"));
		//if( ! prompt("aMenuOption",mCategory + ": "+ aMenuOption)) return;
	}
//	if( debug ) alert( "Draw Menu Time measured: " + ((new Date()) - start));
	if( queryString.indexOf("&sword_list[0]=") > -1 ) window.setTimeout("drawProducts();", 10);
}

function drawProducts(l){
	debug=true;
//	if( debug ) 
//		alert("Reached");
	var start = new Date();
	var aSword_list = new Array();
	var categories = "";
	
	if( l == null ) {
		var l = xGetElementById("Combo_0");
		var s = 0;
		while( queryString.indexOf("&sword_list[" + s + "]=") > -1 ){
			tmpSword = queryString.split("&sword_list[" + s++ + "]=")[1];
			if( tmpSword.indexOf("&") > -1 ) tmpSword = tmpSword.split("&")[0];
			aSword_list[aSword_list.length] = tmpSword;
		}

	} else {
		categories = l.options[l.selectedIndex].value.split(',');
	}
		
	var targetLink = l.options[l.selectedIndex].getAttribute("Link");

	if( targetLink ) {
		if( debug ) 
			alert("split targetlink");
		
		v = targetLink.split('href="')[1].split('"')[0];
		for( i=0; i<aSelectBoxTitles.length; i++ ){
			var combo = xGetElementById("Combo_" + i );
			if( combo ){
				t = combo.options[combo.selectedIndex].text;
				if( t == "*") continue;
				v += "&Sel" + i + "=" + t;
			}
		}
		document.location.href = v.replace(/\&amp;/g, "&");
		if( debug) alert( "Warning, no targetlink" );
		return;
	}
		
	try{ 
		if( CTR )
			if( aSword_list.length > 0 ){
				var re_sWord = new RegExp( "(" + aSword_list.join("|") + ")", "gi" );
				var ra_sWord = new RegExp( "(<a.*?>.*?)(" + aSword_list.join("|") + ")(.*?<\/a>)", "gi" );
				var ra_sWordReplace = '$1<span class="csc-sword">$2</span>$3';
				var re_sWordReplace = '<span class="csc-sword">$1</span>';
			}
				
			for( c=0; c < CTR.length; c++ ){
				var listedProducts = 0;
				result = '<table class="Products" cellpadding="0" cellspacing="0" border="0" width="100%">';
				result += '<tr><td width="*">';
				result += '<h1>' + aResultTitles[c] + '</h1>\n';
				result += '<div id="news-list-counter"><span id="news-list-counter-' + c + '">0</span> ' + aSuchergebnis[0];
				if( aSword_list.length > 0 ) result += ' ' + aSuchergebnis[1] + ' "' + aSword_list.join('" ' + aSuchergebnis[2] + ' "') + '"';
				result += '</div>\n';
				result += '!tdSpecials!';
				result += '</td><td align="right" valign="top">';
				result += '<img src="' + aProductImage[c] + '" height="80">';
				result += '</td></tr></table>';
				result += '<table class="Products" cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
				if(aTableHeaders.length > 0){
					var th = aTableHeaders[c];
					for( var j=0; j<th.length; j++ ){
						result += '<th class="' + aColAlignment[c][j] + '" >' + th[j] + '</th>';
					}
					// one more for ROHS
					result += '<th></th>';
				} else {
					result += '<th>Data Error: No aTableHeaders defined for Product Group ' + c + '<th>';
				}
				result += '</tr>';
				if( aSword_list.length > 0 ) result = result.replace(re_sWord, re_sWordReplace );

				
				// output a single row
				for( r=0; r < CTR[c].length; r++ ){
				
					if( ! CTR[c][r][3] || CTR[c][r][3] == "" ) continue;
					var possibleProduct = false;
					
					if( categories == "" && aSword_list.length == 0 ){
						possibleProduct = true;
					} else if ( categories == "" && aSword_list.length > 0 ) {
						re_sWord.test("IE Bug - don't remove this line!");
						possibleProduct = re_sWord.test(CTR[c][r][1] + "|" + CTR[c][r][3]);
					} else {
						for( o=0; o < categories.length; o++){
							if( categories[o] == CTR[c][r][4] ){
								possibleProduct = true;
								break;
							}
						}
					}
					
					if( possibleProduct === true ){

						var td = CTR[c][r][3].replace(/<(\/)?p>/ig, '').replace(/\n|\r/i, '').split('|');

						// find new or ROHS
						var NEW = "";
						var ROHS = false;
						var tdlen = td.length -1;

						var tdSpecials = td[td.length -1].split("@")[1];
						if( td[td.length -1].indexOf("@") != -1 ) td[td.length -1] = td[td.length -1].split("@")[0];

						if( tdSpecials && tdSpecials != '' ){
							
							tdSpecials = tdSpecials.split(';');
							for( tds=0; tds < tdSpecials.length; tds++ ){
								if( tdSpecials[tds] == 'new' ) {
									NEW = " newProduct";
								}
								if( tdSpecials[tds] == 'RoHS' ) {
									ROHS = true;
								}
							}
						}

						// now create the row
						result += '<tr class="news-list-item">';
						result += '<td class="NewsTitle ' + aColAlignment[c][0] + NEW + '">';
						var title = ""; 

						if( CTR[c][r][2] == "" ){
							title = aSword_list.length > 0 ? CTR[c][r][1].replace(re_sWord, re_sWordReplace ) : CTR[c][r][1];
						} else {
							title = CTR[c][r][2].replace(/\.pdf\</ig, '<').replace(/\<\/dl\>/ig, '');
							if( aSword_list.length > 0 ) title = title.replace(ra_sWord, ra_sWordReplace );
						}
						result += title;
						result += '</td>';
	
						for( j=0; j<td.length; j++ ){
							title = (aSword_list.length > 0 ? td[j].replace(re_sWord, re_sWordReplace ) : td[j]);
							result += '<td class="' + aColAlignment[c][j+1] + NEW + '">' + title + '</td>';
						}
						
						if( ROHS === true ) {
							result += '<td class="' + NEW + '"><img width=11 heigh=11 src="/fileadmin/img/PageContent/RoHS.gif" class="RoHS"></td>';
						} else {
							result += '<td></td>';
						}
						result += '</tr>';

						listedProducts++;
					}
					
				}
				
				var tmpResult = "";
				if( result.indexOf('RoHS.gif') != -1 ) tmpResult += '<div class="LegendRoHS"><img width=13 heigh=13 src="/fileadmin/img/PageContent/RoHS.gif" class="RoHS"> RoHS<div>\n';
				if( result.indexOf(' newProduct') != -1 ) tmpResult += '<div class="LegendNEW"><span class="newProduct">' + newProduct + '</span></div>\n';
				result = result.replace(/\!tdSpecials\!/g, tmpResult );
				
				result += '</table><br/><br/>&nbsp;';


				var container=null;
				container = xGetElementById("news-list-container-" + c );
				if( container ){
					container.innerHTML = result;
		
					if( listedProducts == 0 ){
						container.innerHTML = NothingFound + "<br/><br/>&nbsp;"; 
						container.style.display = "None";
					} else {
						container.style.display = "Block";
					}
				} else
					alert( 'Error: Result Pane for Product Group ' + c + ' not found!');
		
				
		
		
				container = xGetElementById("news-list-counter-" + c );
				if( container ) {
					container.innerHTML = listedProducts;
				}
				
				// turn off all other elements
				if( listedProducts > 0 ){
					xGetElementsByClassName("csc-frame csc-frame-frame2","","",HideIt);
				}
			}
	} catch(e) { 
		var bla=e;
//		alert ("Error in DrawProducts: " + e );
	}
		
//	if( debug) 
//		alert( "draw Products Time measured: " + ((new Date()) - start));
}

function setSelection(l){
	var start = new Date();
	var combo = 0;

	// get a selection if there is any
	var selectedOptions = new Array();
	for( j=0; j<aSelectBoxTitles.length; j++ ){
		combo = xGetElementById("Combo_" + j );
		if( combo ) 
			selectedOptions[j] = combo.options[combo.selectedIndex].text
	}

	combo = l.id.split("_")[1];

	tmpOptions = new Object();
	tmpResets = "";
	mLink = "";
	mLinks = new Array();
	tmpCategories = new Array();
	for( n=0; n<aNav.length; n++ ){
		// get the possibilities
		possibleRecord = true;
//		for( o=0; o<selectedOptions.length; o++ ){
		for( o=0; o<combo; o++ ){
			if( combo == o ) continue;
			if( selectedOptions[o] == null || selectedOptions[o] == "*" || selectedOptions[o] == "" ) continue;
			if( selectedOptions[o] == aNav[n][3][o]) continue;
			possibleRecord = false;
			break;
		}

		if( possibleRecord === true ){
			tmpCategories[aNav[n][2]] = 1;
			var oVal = aNav[n][3][combo];
			if( oVal == null ) continue;
			
			if( tmpOptions[oVal] == null ){
				tmpOptions[oVal] = aNav[n][2];
			} else {
				tmpOptions[oVal] += "," + aNav[n][2];
			}
			if( mLinks[oVal] == null ){
				mLinks[oVal] = aNav[n][1];
			} else {
				mLinks[oVal] += "," + aNav[n][1];
			}
		}

		// get the reset values for the default option *
//		for( b=0; b<aSelectBoxTitles.length; b++ ){
		for( b=0; b<=combo; b++ ){
			possibleRecord = true;
			for( o=0; o<selectedOptions.length; o++ ){
				if( combo == o ) continue;
				if( selectedOptions[o] == null || selectedOptions[o] == "*" || selectedOptions[o] == "" ) continue;
				if( selectedOptions[o] == aNav[n][3][o]) continue;
				possibleRecord = false;
				break;
			}

			if( possibleRecord === true ){
				if( tmpResets == "" ){
					tmpResets = aNav[n][2];
				} else {
					tmpResets += "," + aNav[n][2];
				}
			}
		}
	}

	l.options.length = 0;
	l.options[l.options.length] = new Option( "*", tmpResets, (selectedOptions[combo] == "*" ? true : false), (selectedOptions[combo] == "*" ? true : false));	
	
	// sort the entries
	var sortOptions = new Array();
	for (var kOpt in tmpOptions ){
		sortOptions[sortOptions.length] = new Array( kOpt, tmpOptions[kOpt]);
	}
	
//	if( debug ) {
		sortOptions = MySort( sortOptions, combo );
//	} else {
//		sortOptions.sort();
//	}
	
	for (i=0; i<sortOptions.length; i++ ){
		l.options[l.options.length] = new Option( sortOptions[i][0], sortOptions[i][1], (sortOptions[i][0] == selectedOptions[combo] ? true : false), (sortOptions[i][0] == selectedOptions[combo] ? true : false));
	}

	try {
		if( aProducts )
			if( combo == aSelectBoxTitles.length -1 ){
//				if( debug )
					aProducts = MySort( aProducts, combo );
//				else 
//					aProducts.sort();
				for( p=0; p<aProducts.length; p++ ){
					if(( tmpCategories[aProducts[p][2]] && tmpCategories[aProducts[p][2]] == 1) || tmpCategories.length == 0) { 
						l.options[l.options.length] = new Option( aProducts[p][0], "", false, false);
						l.options[l.options.length -1].setAttribute("Link",aProducts[p][1]);
					}
				}
			}
	} catch(e) {
//		alert("Try selection returned with: " + e);
	}

	for( i=++combo; i < aSelectBoxTitles.length; i++ ){
		sCombo = xGetElementById("Combo_" + i);
		if( sCombo ){
			sCombo.options.length = 0;
			sCombo.options[0] = new Option( "*" );
		}
	}


//	if( debug) alert( "Set Selection Time measured: " + ((new Date()) - start));
	//alert("SelectionSet");
}

function MySort(sortOptions, combo){
/*	if( debug ) {
		dbgMsg = "SortOptions:\n";
		for( i=0; i<sortOptions.length; i++ ){
			dbgMsg += sortOptions[i][0] + "\n";
		}
		alert( dbgMsg );
	}
*/	
	var sortType = "String";
	var sortOrder = "ASC";
	
	try{ 
//		if( debug ) alert( "Sortierung = " + aSelectBoxSort[combo]);
		sortType = aSelectBoxSort[combo].split(",")[0];
		sortOrder = aSelectBoxSort[combo].split(",")[1];
	} catch( e ) { if( debug ) alert( e );};
	
	if( sortType == "Number" ){
		sortOptions.sort(OptionSort);
	} else {
		sortOptions.sort();
	}
	
	if( sortOrder == "DESC" )
		sortOptions.reverse();
	
/*	if( debug ) {
		dbgMsg = "New SortOptions: " + sortOrder + "\n";
		for( i=0; i<sortOptions.length; i++ ){
			dbgMsg += sortOptions[i][0] + "\n";
		}
		alert( dbgMsg );
	}
*/	return sortOptions;
}

function OptionSort( a, b ){
	var ia = Number(a[0].match(/\d*/)[0]);
	var ib = Number(b[0].match(/\d*/)[0]);
	return ia - ib;
}

function ChangeSelection(l){
	//alert("Reached");
	var start = new Date();
	var categories = l.options[l.selectedIndex].value.split(',');
	var targetLink = l.options[l.selectedIndex].getAttribute("Link");
	var container=null;

	if( targetLink ) {
		v = targetLink.split('href="')[1].split('"')[0];
		for( i=0; i<aSelectBoxTitles.length; i++ ){
			var combo = xGetElementById("Combo_" + i );
			if( combo ){
				t = combo.options[combo.selectedIndex].text;
				if( t == "*") continue;
				v += "&Sel" + i + "=" + t;
			}
		}
		document.location.href = v.replace(/\&amp;/g, "&");
		return;
	}
		
	try{ 
		if( CTR )

			for( c=0; c < CTR.length; c++ ){
				var listedProducts = 0;
				result = '<table class="Products" cellpadding="0" cellspacing="0" border="0" width="100%">';
				result += '<tr><td width="*">';
				result += '<h1>' + aResultTitles[c] + '</h1>\n';
				result += '<div id="news-list-counter"><span id="news-list-counter-' + c + '">0</span> Treffer</div>\n';
				result += '!tdSpecials!';
				result += '</td><td align="right" valign="top">';
				result += '<img src="' + aProductImage[c] + '" height="80">';
				result += '</td></tr></table>';
				result += '<table class="Products" cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
				if(aTableHeaders.length > 0){
					var th = aTableHeaders[c];
					for( j=0; j<th.length; j++ ){
						result += '<th class="' + aColAlignment[c][j] + '" >' + th[j] + '</th>';
					}
					// one more for ROHS
					result += '<th></th>';
				} else {
					result += '<th>Data Error: No aTableHeaders defined for Product Group ' + c + '<th>';
				}
				result += '</tr>';
				if( debug ) {
					container = xGetElementById("news-list-container-" + c );
					if( container ){
						container.innerHTML = result + "</table>";
					}
				}
				
				// output a single row
				for( r=0; r < CTR[c].length && debug == false; r++ ){
				
					if( ! CTR[c][r][3] || CTR[c][r][3] == "" ) continue;
					var possibleProduct = false;
					
					if( categories == "" ){
						possibleProduct = true;
					} else {
						for( o=0; o < categories.length; o++){
							if( categories[o] == CTR[c][r][4] ){
								possibleProduct = true;
								break;
							}
						}
					}
					
					if( possibleProduct === true ){
						var td = CTR[c][r][3].replace(/<(\/)?p>/ig, '').replace(/\n|\r/i, '').split('|');

						// find new or ROHS
						var NEW = "";
						var ROHS = false;

						var tdSpecials = td[td.length -1].split("@")[1];
						if( td[td.length -1].indexOf("@") != -1 ) td[td.length -1] = td[td.length -1].split("@")[0];

						if( tdSpecials != '' ){
							
							tdSpecials = tdSpecials.split(';');
							for( tds=0; tds < tdSpecials.length; tds++ ){
								if( tdSpecials[tds] == 'new' ) {
									NEW = " newProduct";
								}
								if( tdSpecials[tds] == 'RoHS' ) {
									ROHS = true;
								}
							}
						}

						// now create the row
						result += '<tr class="news-list-item">';
						result += '<td class="NewsTitle ' + aColAlignment[c][0] + NEW + '" >';
//						result += '<td class="newProduct"><span class="newProduct">';
						result += (CTR[c][r][2] == "" ? CTR[c][r][1] : CTR[c][r][2].replace(/\.pdf\</ig, '<').replace(/\<\/dl\>/ig, '') );
						result += '</td>';
		
						for( j=0; j<td.length; j++ ){
							result += '<td class="' + aColAlignment[c][j+1] + NEW + '">' + td[j] + '</td>';
						}
						
						if( ROHS === true ) {
							result += '<td class="' + NEW + '"><img width=11 heigh=11 src="/fileadmin/img/PageContent/RoHS.gif" class="RoHS"></td>';
						} else {
							result += '<td></td>';
						}
						result += '</tr>';
						listedProducts++;
					}
					
				}
				
				var tmpResult = "";
				if( result.indexOf(' newProduct') != -1 ) tmpResult += '<div class="LegendRoHS"><img width=13 heigh=13 src="/fileadmin/img/PageContent/RoHS.gif" class="RoHS"> RoHS<div>\n';
				if( result.indexOf('RoHS.gif') != -1 ) tmpResult += '<div class="LegendNEW"><span class="newProduct">' + newProduct + '</span></div>\n';
				result = result.replace(/\!tdSpecials\!/g, tmpResult );
				
				result += '</table><br/><br/>&nbsp;';
				container = xGetElementById("news-list-container-" + c );
				if( container ){
					container.innerHTML = result;
		
					if( listedProducts == 0 || debug == true){
						container.innerHTML = NothingFound + "<br/><br/>&nbsp;"; 
						container.style.display = "None";
					} else {
						container.style.display = "Block";
					}
				} else
					alert( 'Error: Result Pane for Product Group ' + c + ' not found!');
		
				
		
		
				container = xGetElementById("news-list-counter-" + c );
				if( container ) {
					container.innerHTML = listedProducts;
				}
				
				// turn off all other elements
				if( listedProducts > 0 && debug == false){
					xGetElementsByClassName("csc-frame csc-frame-frame2","","",HideIt);
				}
			}
	} catch(e) { 
		//alert ("Error in ChangeSelection: " + e );
	}
		
	if( debug) 
		alert( "Change Selection Time measured: " + ((new Date()) - start));
}

function HideIt( e ) {
	e.style.display = "none";
}
window.onload = drawMenu;

