/*
Author: Paul Berry - PBerry@CirrusSoftware.com   
Date: July 2002
Licence: Its Freeware!... Just leave my name against the code and send me a mail if you like.
Purpose: Provides simple dropdown functionality that could be used on multiple cascading dropdown lists.
Compatability: This process will work on both IE and Netscape (tested on Ns 4.5 and IE 5.5)
Modified, Added functionality: Sami.Nygard@eu.panasonic.com

-----Define the following variables/ objects on your page----
as the page loads - first thing to do is to load the dropdown array
var bOk = LoadArrays()

function LoadArrays()
{
this can be thought of as an object array, each element can be identified as a property of the array position
a very powerful structure for 'client side' data caching.
Array[i]= new sElement('[parentId]','[thisId]','[thisDescription]','[0|1]')

return true
}

----Populate the first dripDown-select manually with first level parent elements in sElement-object----

----Define further variables----
var iArrayMax = 0 //length of sElement array
var sRstText = "";
var sRstId = "";
var iLastSelLev = 0;//last select level in dripDown menu
var sSelectedId = 0;
var sFormName = "";

----Define last select----
var sLastSelectName = "sType";
var sLastSelectItem = new Array;
sLastSelectItem[0] = new ssLastSelectItem(sRstText,sRstId);
sLastSelectItem[i] = new ssLastSelectItem('[thisDescription]','[thisId]');

----Define the function for last select's onChange event as needed on your page----
*/

function ssLastSelectItem(sDescription,sId){
	
	//Elements that will be loaded into the last select array structure
	this.Id = sId;
	this.Description = sDescription;
}

function bSetLastSelect(){
	var i;
	document[sFormName][sLastSelectName].options.length = 0;
	document[sFormName][sLastSelectName].options[0] = new Option (sRstText,sRstId);
	for (i=0; i<sLastSelectItem.length; i++){ 
		document[sFormName][sLastSelectName].options[i] = new Option (sLastSelectItem[i].Description,sLastSelectItem[i].Id);
	}
}

function sElement(sParentId,sValue,sDescription,sType)
{
	// elements that will be loaded into the main array structure and persisted
	this.ParentId = sParentId
	this.Id = sValue
	this.Description = sDescription
	this.Type = sType
}

function bCascadeDrop(oDDsource,oDDdest,iSel)
{
	//function to enable cascading dropdowns
	//called as the parent dropdown changes.
	var iX
	var sText
	var iY= 1
	var sOptionId
	var sOptionDesc
	var iStartPos
 	var iSels = document[sFormName].length;
 	var iCnt = 1
 	
 	//reset all sub cascading selects
 	for (iCnt=iCnt+iSel; iCnt<iSels; iCnt++){
    if(document[sFormName].elements[iCnt].tagName=="SELECT"){
 		  var oSelEle = document[sFormName].elements[iCnt];
 		  oSelEle.options.length = 0;
		  oSelEle.options[0] = new Option (sRstText,sRstId);
    }
 	}
 	
	//find the value of the item currently selected		
	sText = oDDsource.options[oDDsource.selectedIndex].value 

	if (sText != 'x')
	{
		//clear down the destination list box
		oDDdest.options.length = 0                
		oDDdest.options[0] = new Option (sRstText,sRstId);		

		//loop through the elements that are in the array
		// if they match the parent if then they should be displayed.
		for (iX=0; iX<=iArrayMax; iX++) 
		{	
			//if current element is last level in array or current select is last in dripDown menu
			//set the last select on page
			if((sText == Array[iX].Id && Array[iX].Type == 1) || iSel == iLastSelLev){
				sSelectedId = sText;
				bSetLastSelect();
			}

			if(sText == Array[iX].ParentId)
			{
			//grab the values out of the element 
			//(Netscape is not happy doing too many things in a function call!)
			sOptionId = Array[iX].Id
			sOptionDesc= Array[iX].Description
			
			//write the element into the dripdown box.
			oDDdest.options[iY] = new Option (sOptionDesc,sOptionId)	
			iY = iY +1		
			}
			
			if ((iX + 'query') == Array[iX].ParentId && iSel == iLastSelLev-1) {
			  sOptionId = Array[iX].Id;
			  sOptionDesc= Array[iX].Description;
			  oDDdest.options[iY] = new Option (sOptionDesc,sOptionId);
			  iY = iY +1;
			}
		}
	}
}
