//	******************************************************************
//	MultiPopup Script
//	controls multiple dependent select elements.
//	tb
//	call function onload: setPopups(";rubrik;unterrubrik");
//	Required:
//		hidden form field "_mfPopup"
//		form definitions, where the source data is located
//		variables "popupHeader,popupData"
//		select elements on the HTML page e.g. "mPopup1, mPopup2, mPopupX"
//	******************************************************************

// global variables
delPrefix='['
delSuffix=']'
valueDel='='
rowDelm=unescape("%01")
colDelm=unescape("%02")

rowDelmIn='%01'
colDelmIn='%02'

aPopupValues=new Array()
var popupForm,theForm
var popupDepth, currentLine, aPopupHeader, putData
var popupOk
popupHeader=''
popupData=''
popupOk=true

// onload function (initialize popups)
function setPopups(popupHeader){
	if(theForm){
		popupOk=checkFormField(popupForm.elements["_mPopup1"],"select")
		if(popupOk){
			popupData = popupControlForm.elements["_popupDataBox"].value;
			aPopupHeader=(';'+popupHeader).split(';')
			putData=theForm.elements["_mfPopup"].value
			getDepth(1)
			initPopups()
			readData(popupData,1)
		}
	}else{
		alert("multipopup.js: theForm nicht definiert")
	}
}


// empty the popups and fill the master with dynamic entries
function initPopups(){
	if(popupOk=checkFormField(popupForm.elements["_mPopup1"],"select")){
		for(n=1;n<=popupDepth;n++){
			pField=popupForm.elements["_mPopup"+n]
			if(checkFormField(pField,"select")){
				clearPopup(pField)
				pField.options[0]=new Option('----')
			}
		}
		aMyData=getLineData(popupData,1)
		pWriteField=popupForm.elements["_mPopup1"]
		if(checkFormField(pWriteField,"select")){
			for(i=1;i<aMyData.length;i++){pWriteField.options[i]=new Option(aMyData[i])}
		}
	}
}


//	populate popups from hidden fields
function readData(str,n){
	myDel=delPrefix+n+delSuffix
	nextDel=delPrefix+(n+1)+delSuffix
	aPutData=getPutData(putData)
	aTempData=str.split(myDel)
	searchString=str
	pWriteField=popupForm.elements["_mPopup"+n]
	if(checkFormField(pWriteField,"select")){
		for(i=1;i<aTempData.length;i++){
			pos=aTempData[i].indexOf(nextDel)
			if(pos>0){myItem=getItemValue(aTempData[i].substring(0,pos))}
			else{myItem=getItemValue(aTempData[i])}
			//aItem=aPutData[n].split(colDelmIn)
			if(aPutData[n]&&(aPutData[n].toLowerCase()==myItem.toLowerCase())){
				pWriteField.selectedIndex=i
				searchString=aTempData[i]
			}
		}
		changePopup(n+1)
		if(n<aPutData.length-1 && searchString!=str && searchString!='none'){readData(searchString,n+1)}
	}
}


// wenn ein Popup gewechselt wird, sucht diese Funktion nach dessen Kindern und fuellt das folgende Popup
function changePopup(n){
	// das zu wechselnde und alle nachfolgenden popups loeschen
	for(y=n;y<=popupDepth;y++){
		pField=popupForm.elements["_mPopup"+y]
		if(checkFormField(pField,"select")){
			clearPopup(pField)
			pField.options[0]=new Option('----')
		}
	}
	
	// Datenstamm des gewaehlten Eintrags ermitteln
	findLine(popupData,1,n-1)
	myData=currentLine
	 
	// das Popop mit den ermittelten Daten fuellen
	aMyData=getLineData(myData,n)
	pWriteField=popupForm.elements["_mPopup"+n]
	if(checkFormField(pWriteField,"select")){
		for(m=1;m<aMyData.length;m++){
			pWriteField.options[m]=new Option(aMyData[m])
		}
	}
}


// save selected values (all pulldowns) to the respective hidden fields
function saveData(which){
	theData=''
	writeEmpty=false
	for(x=1;x<=popupDepth;x++){
		fieldName=aPopupHeader[x]
		i=popupForm.elements["_mPopup"+x].selectedIndex
		if(x<aPopupValues.length){
			fieldValue=aPopupValues[x][i]
			theData+=fieldName+colDelm+fieldValue
	
			// write value to the hidden field with the same name
			if(theForm.elements[fieldName.toLowerCase()]){
				theForm.elements[fieldName.toLowerCase()].value=fieldValue
			}
		}
		else{writeEmpty=true}
		
		if(writeEmpty){ theData+=fieldName+colDelm }
		if(x<popupDepth) theData+=rowDelm;
	}
	theForm.elements["_mfPopup"].value=theData
}


// findet den Datenstrang in der gewuenschten Ebene
// anhand der bereits eingestellten Popups
function findLine(str,n,end){
	myDel=delPrefix+n+delSuffix
	nextDel=delPrefix+(n+1)+delSuffix
	aTempData=str.split(myDel)
	searchString='none'
	searchValue=mfSelIndex(popupForm.elements["_mPopup"+n])
	for(a=1;a<aTempData.length;a++){
		//if(getLineTitle(aTempData[a],n+1).indexOf(searchValue)>=0){ // wert in dieser zeile gefunden
		if(getLineTitle(aTempData[a],n+1)==searchValue){ // wert in dieser zeile gefunden
			pos=aTempData[a].indexOf(nextDel)
			searchString=aTempData[a].substring(pos,aTempData[a].length)
		}
	}
		
	if(searchString!='none' && n<end){  // muss noch tiefer gesucht werden
		findLine(searchString,n+1,end)   // eine ebene tiefer suchen
	}
	else{
		currentLine=searchString=="none"?"":searchString;    // write line or empty string to a global variable
	}
}


// Wandelt einen Datenstrang in ein Array um
function getLineData(str,n){
	myDel=delPrefix+n+delSuffix
	nextDel=delPrefix+(n+1)+delSuffix
	
	aData=str.split(myDel)
	aLineData=new Array()
	aLineValues=new Array()
		
	for(i=0;i<aData.length;i++){
		pos=aData[i].indexOf(nextDel)
		
		if(pos>0){ myItem=aData[i].substring(0,pos) }
		else{ myItem=aData[i] }
		
		aLineValues[i]=getItemValue(myItem)
		aLineData[i]=getItemLabel(myItem)
	}
	
	aPopupValues[n]=aLineValues
	return aLineData
}


// liest aus einem Datenstrang den ersten Eintrag (Titel)
function getLineTitle(str,n){
	myDel=delPrefix+n+delSuffix
	pos=str.indexOf(myDel)
	
	if(pos>0){
		myTitle=getItemLabel(str.substring(0,pos))
	}
	else{ myTitle='!error' }
	return myTitle
}


// gibt die Value eines Eintrags zurueck (alles vor '=')
function getItemValue(str){
	if(str.indexOf(valueDel)>0){
		aItem=str.split(valueDel)
		return aItem[0]
	}
	else{ return str }
}

// gibt das Label eines Eintrags zurueck (alles nach '=')
function getItemLabel(str){
	if(str.indexOf(valueDel)>0){
		aItem=str.split(valueDel)
		return aItem[1]
	}
	else{return str}
}


// ermittelt die Tiefe (Ebenen) der Popup-Daten
function getDepth(n){
	if(popupData.indexOf(delPrefix+n+delSuffix)>=0){
		popupDepth=n
		getDepth(n+1)
	}
}


// liest die Daten vom Server (aus Hidden Field), sortiert diese
// und gibt ein Array mit den Values zurueck
function getPutData(){
	aTempData=putData.split(rowDelmIn)
	aOut=new Array()
	for(n=1;n<aPopupHeader.length;n++){
		for(i=0;i<aTempData.length;i++){
			aItem=aTempData[i].split(colDelmIn)
			if(aItem[0].toLowerCase()==aPopupHeader[n].toLowerCase()){
				aOut[n]=aItem[1]
			}
		}
	}
	return aOut
}


// loescht die eintraege eines Popups
function clearPopup(pField){
	for(i=pField.options.length;i>=0;i--){ pField.options[i]=null; }
}