var x=0;
var xPos=0;
var nextPos=0;
var prevPos=0;
var catLen=0;
var catInfo=new Array();
var catPage="";
var width=0;
var height=0;
var catName="";
var catNamePos="";
var mSel=0;
var mSubSel=0;
var mPageSel=0;
var mLevel=0;
var level1Name="";
var level2Name="";
var level3Name="";
var fromHist=false;

//populating the first Listbox
function popMain(){
  var oList=document.forms.cats.catMain;
  for (var i = 0; i < valMain.length; i++){
    optName=valMain[i].split(";;",3);
		oList.options[oList.options.length] = new Option(optName[0],valMain[i]);
  }
}
function showCat(cat,catLevel){  
  mLevel=catLevel;
	catNamePos=cat;
	catName=cat.substring(0,cat.indexOf("["));
	catElement=cat.substring(cat.indexOf("[")+1,cat.indexOf("]"))-0;
	//alert(cat.substring(cat.indexOf("[")+1,cat.indexOf("]")));
	catInfo=eval(catNamePos).split(",",3);
	catPage=catInfo[0];
	width=catInfo[1];
	height=catInfo[2];
	 
	catLen=eval(catName).length-1;
	nextPos=catLen;
	xPos=catElement;
      		
	prevPos=xPos-1;
	nextPos=xPos-0+1;
	if(nextPos > catLen)
			document.getElementById('nextCat').style.visibility="hidden";
			
	if(prevPos < 0)
			document.getElementById('prevCat').style.visibility="hidden";

	if(catLen == 0)
			document.getElementById('katSeiten').style.visibility="hidden";
	else
			document.getElementById('katSeiten').style.visibility="visible";
	
	//alert(xPos + "prev " + prevPos + "next " + nextPos);
	fillHist(cat,catElement,catLevel,mSel,mSubSel,mPageSel,level1Name,level2Name,level3Name);
	chIframe(catPage,width,height);
}
function nextPrev(wr){
		if(wr=="n"){
			nextPos=nextPos-0+1;
			prevPos=xPos-0;
			xPos=xPos-0+1;
			//alert(catName);
			catInfo=eval(catName)[xPos].split(",",3)
  	  catPage=catInfo[0];
  	  width=catInfo[1];
  	  height=catInfo[2];
			
			document.getElementById('prevCat').style.visibility="visible";
			//alert(nextPos + " " + catLen);
			if(nextPos > catLen){
				document.getElementById('nextCat').style.visibility="hidden";
			}
		}
		else{
			nextPos=xPos;
			prevPos=prevPos-1;
			xPos=xPos-1;
			
			catInfo=eval(catName)[xPos].split(",",3)
  	  catPage=catInfo[0];
  	  width=catInfo[1];
  	  height=catInfo[2];
			
			document.getElementById('nextCat').style.visibility="visible";
			//alert(nextPos + " " + prevPos);
			if(prevPos < 0){
				document.getElementById('prevCat').style.visibility="hidden";
			}
		}
		chIframe(catPage,width,height);
}
function chIframe(targetPage,h,w){
	// ändert den Inhalt des IFrames
	//alert (targetPage);
			if(nextPos > catLen)
				document.getElementById('nextCat').style.visibility="hidden";
			else
				document.getElementById('nextCat').style.visibility="visible";
				
			if(prevPos < 0)
				document.getElementById('prevCat').style.visibility="hidden";
			else
				document.getElementById('prevCat').style.visibility="visible";
				
		 	document.getElementById('Kom').src="";
		  document.getElementById('Kom').src=targetPage;
    	document.getElementById('Kom').height=h;
    	document.getElementById('Kom').width=w;
			
			document.getElementById('navi').style.visibility="hidden";
			document.getElementById('navi').style.zIndex=-1;
			document.getElementById('conHead').style.visibility="visible";
			document.getElementById('conHead').style.zIndex=99;
			document.getElementById('content').style.visibility="visible";
			document.getElementById('content').style.zIndex=99;
}

function showNavi()
	{			
	// Zeigt die 1-3 Navi-Boxen an	
		  document.getElementById('navi').style.visibility="visible";
			document.getElementById('navi').style.zIndex=99;
			//alert(catLen + " " + mLevel + "  " + mSel);
			if(catLen>0)
				{
				if(mLevel==1)
					document.forms.cats.catMain.options[xPos].selected=true;
				else if(mLevel==2)
					document.forms.cats.catSub.options[xPos].selected=true;
				else if(mLevel==3)
					document.forms.cats.catSubSub.options[xPos].selected=true;
				}
			//document.getElementById('conHead').style.visibility="hidden";
			//document.getElementById('conHead').style.zIndex=-1;
			document.getElementById('content').style.visibility="hidden";
			document.getElementById('content').style.zIndex=-1;
			
		document.getElementById('conHead').style.visibility="hidden";
		//document.getElementById('conHead').style.background="#FF0000";
		document.getElementById('catHistInt').style.visibility="hidden";
		document.getElementById('katSeiten').style.visibility="hidden";
		document.getElementById('prevCat').style.visibility="hidden";
		document.getElementById('nextCat').style.visibility="hidden";
		
	}
function selectSub(){
	mSel=document.forms.cats.catMain.selectedIndex;
	//document.forms.cats.catMain.options[mSel].style.color="#FF0000";
	level1Name=document.forms.cats.catMain.options[mSel].text;
	optSelected=document.forms.cats.catMain.options[mSel].value.split(";;",3);
	hasChilds=optSelected[1];
	nextStep=optSelected[2];	
	//alert(eval(nextStep).length);
	var oList=document.forms.cats.catSub;
	if(eval(hasChilds)){		
		document.forms.cats.catSub.style.visibility="visible";
		document.forms.cats.catSubSub.style.visibility="hidden";
		for (var i = oList.options.length; i >= 0; i--){
  			oList.options[i] = null;
 		}
		for (var i = 0; i < eval(nextStep).length; i++){
				optName=eval(nextStep)[i].split(";;",3);
  			oList.options[oList.options.length] = new Option(optName[0],eval(nextStep)[i]);
  	}
	}
	else{
		document.forms.cats.catSub.style.visibility="hidden";
		document.forms.cats.catSubSub.style.visibility="hidden";
		showCat(nextStep,1);
	}
}
function selectSubSub(){
	mSel=document.forms.cats.catMain.selectedIndex;
	mSubSel=document.forms.cats.catSub.selectedIndex;
	level2Name=document.forms.cats.catSub.options[mSubSel].text;
	optSelected=document.forms.cats.catSub.options[mSubSel].value.split(";;",3);
	hasChilds=optSelected[1];
	nextStep=optSelected[2];	
	var oList=document.forms.cats.catSubSub;
	if(eval(hasChilds)){
		document.forms.cats.catSubSub.style.visibility="visible";
		for (var i = oList.options.length; i >= 0; i--){
  			oList.options[i] = null;
 		}
		for (var i = 0; i < eval(nextStep).length; i++){
  			optName=eval(nextStep)[i].split(";;",3);
				oList.options[oList.options.length] = new Option(optName[0],eval(nextStep)[i]);
  	}
  }
  else{
  	document.forms.cats.catSubSub.style.visibility="hidden";
		showCat(nextStep,2);
	}	
}
function selectPage(){
	mSubSel=document.forms.cats.catSub.selectedIndex;
	mPageSel=document.forms.cats.catSubSub.selectedIndex;
	level3Name=document.forms.cats.catSubSub.options[mPageSel].text;
	optSelected=document.forms.cats.catSubSub.options[mPageSel].value.split(";;",3);
	hasChilds=optSelected[1];
	nextStep=optSelected[2];	
	showCat(nextStep,3);
}
function showHist()
	{
	// Untere History anzeigen oder verstecken
	if (document.forms.cats.catHist.style.visibility=="visible")
		{
		document.forms.cats.catHist.style.visibility="hidden";
		}
	else
		{
		document.forms.cats.catHist.style.visibility="visible";
		}
	}

function showHistInt()
	{
	// Obere History anzeigen oder verstecken	
	if (document.forms.cats.catHistInt.style.visibility=="visible")
		{
		document.forms.cats.catHistInt.style.visibility="hidden";
		}
	else
		{
		document.forms.cats.catHistInt.style.visibility="visible";
		}
	}
	
function fillHist(cat,catElement,catLevel,mS,mSubS,mPageS,level1N,level2N,level3N){
	// History mit neuem Element füllen...nämlich dem derzeitigen
	mLevel=catLevel;
	catNamePos=cat;
	catName=cat.substring(0,cat.indexOf("["));
	catElement=cat.substring(cat.indexOf("[")+1,cat.indexOf("]"))-0;
	mSel=mS;
	mSubSel=mSubS;
	mPageSel=mPageS;
	level1Name=level1N;
	level2Name=level2N;
	level3Name=level3N;
	//alert(mLevel+" "+catName+" "+mSel+" "+mSubSel+" "+mPageSel+" "+level1Name+" "+level2Name+" "+level3Name);
	var HistOptName="";
	var HistOptValue=mLevel + "," + catName + "," + mSel + "," + mSubSel + "," + mPageSel;
	
	// Angezeigten Namen zusammenstellen, etwa "Hauptkategorie / Unterkategorie / etc." bei mLevel=3
	
	if(mLevel==1)
		HistOptName=level1Name;
	else if(mLevel==2)
		HistOptName=level1Name + "/" + level2Name;
	else if(mLevel==3)
		HistOptName=level1Name + "/" + level2Name + "/" + level3Name;
		
	var oList=document.forms.cats.catHist;
	var oListInt=document.forms.cats.catHistInt;
	
	
	if(oList.options.length >10)
		{
		// ab dem 10. Eintrag keine weiteren Einträge aufnehmen?	
		oList.options[10]=null;
		oListInt.options[10]=null;
		}
	
	for (var i = 0; i < oList.options.length; i++)
		{
		// Wenn der aufzunehmende Eintrag schon in der Liste ist...dann den schon vorhandenen leeren!	
    if(oList.options[i].text == HistOptName)
    	{
			oList.options[i]=null;
			oListInt.options[i]=null;
			}
  	}
 
	//shift the array one step back to insert the current option
	
	for (var i = oList.options.length; i >0 ; i--)
		{
    oList.options[i]=new Option(oList.options[i-1].text,oList.options[i-1].value);
		oListInt.options[i]=new Option(oListInt.options[i-1].text,oListInt.options[i-1].value);
  	}
  	  	
	oList.options[0] = new Option(HistOptName,HistOptValue);
	oListInt.options[0] = new Option(HistOptName,HistOptValue);
	
	//GW 080410: So wird der erste Eintrag [0] selektiert
	oList.options[0].selected=true;
	oListInt.options[0].selected=true;
	
	//create the cookie
	var ExpDays=365;
	var CookValue="";
	var CookText="";
	var cookContent="";
	oList=document.forms.cats.catHist;
	
	for(var i = 0; i < oList.options.length; i++)
		{
		CookText = CookText + oList.options[i].text + "$$";
		CookValue = CookValue + oList.options[i].value + "$$";
		}
		
	//alert (CookText+"\n\n"+CookValue);
	// Text ist das was angezeigt wird in der SelectBox, Value der Wert.
	cookContent=CookText + "+++" + CookValue + CookEndName;
	createCookie(CookName,cookContent,ExpDays);
}

function selectHist(intExt)
	{
	// Lädt bei Änderung der Select-Box eine neue	Katalogseite
	if(intExt=="ext")
		{
  	// var histIndex=document.forms.cats.catHist.selectedIndex-1;
  	
  	// "ext" ist die untere History
  	var histIndex=document.forms.cats.catHist.selectedIndex;
  	var histValue=document.forms.cats.catHist.options[histIndex].value;
		}
	else
		{
		// z.B. intExt == "int"
		//var histIndex=document.forms.cats.catHistInt.selectedIndex-1;
		
		// "int" ist die obere History
		var histIndex=document.forms.cats.catHistInt.selectedIndex;
  	var histValue=document.forms.cats.catHistInt.options[histIndex].value;
		}
	
		// alert(histIndex+" - "+histValue);
		// histValue ist z.B. 1,CatF,5,0,0 für LK+ / LPK+
		histInfo=histValue.split(",",5);
		mLevel=histInfo[0]-0;
		catName=histInfo[1]; //default: 1
		mSel=histInfo[2]-0;
		mSubSel=histInfo[3]-0;
		mPageSel=histInfo[4]-0;
		if(mLevel >= 1){
			document.forms.cats.catMain.options[mSel].selected=true;
			selectSub();
		}
		if(mLevel >= 2){
			document.forms.cats.catSub.options[mSubSel].selected=true;
			selectSubSub();
		}
		if(mLevel >= 3){
			document.forms.cats.catSubSub.options[mPageSel].selected=true;
			selectPage();
		}
	}
	
function getCookie( name,cLength,endName ) {
  var start = document.cookie.indexOf( name );
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( endName );
  return unescape( document.cookie.substring( start+cLength+1, end ) );
}

function createCookie(CName,CValue,EDays) {
	var start = document.cookie.indexOf( CookName );
	var end = document.cookie.indexOf( CookEndName );
	end=end+CookEndName.length;
	var otherCookieData=document.cookie.substring( 0, start )+document.cookie.substring( end );
	
	CValue=CValue+otherCookieData;
	
	var date = new Date();
	date.setTime(date.getTime()+(EDays*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	
	document.cookie = CName+"="+CValue+expires+"; path=/";
}
function popHist(){
	var cc=getCookie(CookName,CookNameLength,CookEndName);
	
	// Cookie wird gesplittet am +++ (vorher §§§, aber wegen falscher Codierung geändert)
	// ca[0] ist der Textanteil, ca[1] ist der Wert
	// Beide werden intern am $$ gesplittet
	//alert("Cookie "+CookName+" =\n"+cc);
	if(cc != null){
		var ca = cc.split('+++');
		//alert(ca[0]);
  	var CookText=ca[0].split('$$');
  	var CookValue=ca[1].split('$$'); //default:1 // war aber [0]
  	//alert (CookText+"\n\n"+CookValue);
  	var oList=document.forms.cats.catHist;
		var oListIn=document.forms.cats.catHistInt;
  	 for(var i=0;i < (CookValue.length-1) ;i++){   //length-1
  	//for(var i=0;i < CookValue.length ;i++){   //length-1	
  		// war (CookValue.length) ...  aber dann 1 zu lang
  		oList.options[i] = new Option(CookText[i],CookValue[i]);
			oListIn.options[i] = new Option(CookText[i],CookValue[i]);
  	}
	}
}