//Funktion: 	setlocation()
//Bescheibung:  Mit dieser Funktion wird ueberprueft, welche Institute in der SelectBox ausgewaehlt sind
//				und dementsprechend werden in der SelectBoxen "Standort" die
//				entsprechenden Eintraege gesetzt.
//Rückgabewert: -
//Parameter: 	-
//Author: 		Antje Parnitzke
//Datum: 		17.06.2005

function setlocation()
{		
		// var source = document.forms[0].id31_subjects;
		var source = findElem("subjects");
		var j = 0;
		var list =  new Array;
		
		//speichert alle ausgewaehlten Institute in einem Array
		for(var i = 0; i < source.length; i++ )
        {
			if (source.options[i].selected == true)
			{
				list[j] = source.options[i].value;
				j++;				
			}			
		}

		unselectlocation();
		
		//Wenn ein oder mehrere Institute ausgewaehlt sind, dann wird fuer jedes Institut der Standort
		//automatisch gesetzt.
		
		if (j > 0)
		{
			for(var i = 0; i < list.length; i++ )
        	{							
        		selectlocation(list[i]);
			}
		}
	
}

function selectlocation(institut) 
{
	// var source = document.forms[0].id31_subjects;
	var source = findElem("subjects");
	// var target = document.forms[0].id32_location; 	
	var target = findElem("location");
	
	//In diesem Array befindet sich genau ein Element mit dem ausgewaehlten Institut und den dazugehoerigen
	//Standort(en).
	var element = getlocation(institut);  
	
	//Ein Institut kann an mehreren Standorten sein, z.B. standorte = ("ba, kp")
	var standorte = element[0]["DLRStandort"];
	
	if (element[0]["DLRInstitut"] == institut)
	{		
		for(var i = 0; i < DLRStandort.length; i++ )
		{	
			//alle Standorte in SelectBox auswaehlen
			for(var j = 0; j < standorte.length; j++ )
			{	
				if(standorte[j] == DLRStandort[i]["Value"])
				{	
					target.options[i].selected = true;				
				}
			}
		}
	}
}

function unselectlocation()
{
	// var source = document.forms[0].id31_subjects;
	var source = findElem("subjects");
	// var target = document.forms[0].id32_location; 	
	var target = findElem("location");
	var list   = DLRStandort;
	
	//unselect Element in SelectBox
	for( i = 0; i < list.length; i++ )
	{
		target.options[i].selected = false;
	}
}

function getlocation(institut)
{
	var matrix = location2institute(); 
	var institute;
	var element = new Array();
	var standorte = new Array();
		
	for(var i = 0; i < matrix.length; i++ )
	{	
		institute = matrix[i]["DLRInstitut"];
		
		for(var j = 0; j < institute.length; j++ )
		{	
			if (institute[j] == institut)
			{	//alle Standorte fuer dieses Institut in einem Array speichern
				standorte.push(matrix[i]["DLRStandort"]);
			}
		}
	}
	element[0] = new Array();				
	element[0]["DLRStandort"] = standorte;
	element[0]["DLRInstitut"] = institut; 
	
	return element;
}
