function ListboxItem(id,name,parentListboxItem)
{
	this.id=id;
	this.name=name;
	this.childrenArray = new Array();
	this.parentListboxItem = parentListboxItem;
}//ListboxItem

function ListboxInfo(listboxItemArray,bAddAllWhenNoneSelected,bAddAllOption,parentListbox,childListbox)
{
	this.listboxItemArray = listboxItemArray;
	this.bAddAllWhenNoneSelected = bAddAllWhenNoneSelected;
	this.bAddAllOption = bAddAllOption;
	this.parentListbox = parentListbox;
	this.childListbox = childListbox;
}//ListboxInfo

function UpdateListbox(childListbox)
{
	var nIndex;
	var parentObj;
	var childObj;
	var nListIndex=0;
	var parentArray=childListbox.listboxInfo.parentListbox.listboxInfo.listboxItemArray;
	var parentListbox=childListbox.listboxInfo.parentListbox;
	var bAddAllOption=childListbox.listboxInfo.bAddAllOption;
	
	
	if( parentListbox.selectedIndex>=0 )
	{		
		parentObj = GetListboxItem(parentArray,parentListbox.options[parentListbox.selectedIndex].value);

		if( bAddAllOption )
		{
			childListbox.options[nListIndex++] = new Option("Alla","",false,false);
			childListbox.options[nListIndex++] = new Option("-------------------------","",false,false);
		}
		
		if( parentObj!=null )
		{			
			for( nIndex=0; nIndex<parentObj.childrenArray.length;nIndex++ )
			{
				childObj = parentObj.childrenArray[nIndex];
				childListbox.options[nListIndex++] = new Option(childObj.name,childObj.id,false,false);
			}
			
			//Remove options
			for( ; nListIndex<childListbox.options.length; )
			{
				childListbox.options[nListIndex]=null;
			}
			
			if( childListbox.onPopulate!=null )
				childListbox.onPopulate(childListbox,parentObj.childrenArray.length);
		}
		else
		{
			var nCount = 0;
			if( childListbox.listboxInfo.bAddAllWhenNoneSelected && (parentListbox==null || parentListbox.selectedIndex<0 || parentListbox.options[parentListbox.selectedIndex].value=="") )
			{
				tempArray = childListbox.listboxInfo.listboxItemArray;
				for( nIndex=0; nIndex<tempArray.length;nIndex++ )
				{
					childObj = tempArray[nIndex];
					childListbox.options[nListIndex++] = new Option(childObj.name,childObj.id,false,false);
				}
				nCount = tempArray.length;
			}
			//Remove options
			for( ; nListIndex<childListbox.options.length; )
			{
				childListbox.options[nListIndex]=null;
			}
			
			if( childListbox.onPopulate!=null )
				childListbox.onPopulate(childListbox,nCount);
		}
		
		if( childListbox.listboxInfo.childListbox!=null )
			UpdateListbox(childListbox.listboxInfo.childListbox);
	}
}//UpdateListbox

function GetListboxItem(parentArray,id)
{
	var nIndex;
	for( nIndex=0; nIndex<parentArray.length; nIndex++)
	{
		if( parentArray[nIndex].id==id )
			return parentArray[nIndex];
	}
	
	return null;
}//GetListboxItem

function ListboxItemCompareFunc(listboxitemA,listboxitemB)
{
	var nCompare;
	
	if( listboxitemA.name>listboxitemB.name )
	{
		if( listboxitemA.sSortorder=="ASCENDING" )
			nCompare=1;
		else if( listboxitemA.sSortorder=="DESCENDING" )
			nCompare=-1;
		else
			nCompare=0;
	}
	else if( listboxitemA.name<listboxitemB.name )
	{
		if( listboxitemA.sSortorder=="ASCENDING" )
			nCompare=-1;
		else if( listboxitemA.sSortorder=="DESCENDING" )
			nCompare=1;
		else
			nCompare=0;
	}
	else
		nCompare=0;
	
	return nCompare;
}//ListboxItemCompareFunc


function InitArray(childString,childArray,parentArray, sSortorder)
{
	var tempArray;
	var temp2Array;
	var nIndex;
	var parentObj;
	var newListboxItem;


	tempArray = childString.split(";");
	for( nIndex=0; nIndex<tempArray.length; nIndex++)
	{
		temp2Array=tempArray[nIndex].split(":");

		newListboxItem = new ListboxItem(temp2Array[0],temp2Array[1],null);
		newListboxItem.sSortorder=sSortorder;
		
		if( childArray!=null )
			childArray[childArray.length] = newListboxItem;
		
		if( parentArray!=null )
		{
			parentObj = GetListboxItem(parentArray,temp2Array[2]);
			if( parentObj!=null )
			{
				newListboxItem.parentListboxItem=parentObj;
				parentObj.childrenArray[parentObj.childrenArray.length] = newListboxItem;
				
				//FIXME!! SORTERING
				if( sSortorder!=null || sSortorder=="NONE" )
					parentObj.childrenArray.sort(ListboxItemCompareFunc);
			}
		}
	}
	
	//FIXME!! SORTERING
	if( sSortorder!=null || sSortorder=="NONE" )
		childArray.sort(ListboxItemCompareFunc);
}//InitArray


function UpdateParentListbox(childListbox,childID)
{
	var nIndex;
	var itemObj;
	var optionObj;
	var tempArray;

	itemObj = GetListboxItem(childListbox.listboxInfo.listboxItemArray,childID);
	if( itemObj!=null && itemObj.parentListboxItem!=null )
	{
		if( itemObj.parentListboxItem.parentListboxItem!=null )
			tempArray = itemObj.parentListboxItem.parentListboxItem.childrenArray;
		else
			tempArray = childListbox.listboxInfo.parentListbox.listboxInfo.listboxItemArray;

		PopulateListbox(tempArray,childListbox.listboxInfo.parentListbox,itemObj.parentListboxItem.id,childListbox.listboxInfo.parentListbox.listboxInfo.bAddAllOption);
		UpdateParentListbox(childListbox.listboxInfo.parentListbox,itemObj.parentListboxItem.id);
	}else
	{	
		
		//Check if the id exists in parent
		if( childListbox.listboxInfo.parentListbox != null )
		{	
			id = childID;		
			tempArray = childListbox.listboxInfo.parentListbox.listboxInfo.listboxItemArray;		
			itemObj = GetListboxItem(tempArray,childID);
			
			if( itemObj != null )
			{
				PopulateListbox(tempArray,childListbox.listboxInfo.parentListbox,id,childListbox.listboxInfo.parentListbox.listboxInfo.bAddAllOption);		
				UpdateListbox(childListbox);
			}
			UpdateParentListbox(childListbox.listboxInfo.parentListbox,id);
		}	
	}
}//UpdateParentListbox

function UpdateListboxEx(childListbox,childID)
{
	var nIndex;
	var itemObj;
	var optionObj;
	var tempArray;	
	itemObj = GetListboxItem(childListbox.listboxInfo.listboxItemArray,childID);
	if( itemObj!=null )
	{
		if( itemObj.parentListboxItem!=null )
			tempArray = itemObj.parentListboxItem.childrenArray;
		else
			tempArray = childListbox.listboxInfo.listboxItemArray;

		PopulateListbox(tempArray,childListbox,childID,childListbox.listboxInfo.bAddAllOption);
		if( childListbox.listboxInfo.parentListbox!=null )
			UpdateListboxEx(childListbox.listboxInfo.parentListbox,itemObj.parentListboxItem.id);
	}
}//UpdateListboxEx

function PopulateListbox(tempArray,childListbox,childID,bAddAllOption)
{
	var childObj;
	var nListIndex=0;
	if( bAddAllOption )
	{
		childListbox.options[nListIndex++] = new Option("Alla","",false,false);
		childListbox.options[nListIndex++] = new Option("-------------------------","",false,false);
	}

	for( nIndex=0; nIndex<tempArray.length;nIndex++ )
	{
		childObj = tempArray[nIndex];
		if( childID!=null && childObj.id==childID )
			bSelected=true;
		else
			bSelected=false;
		childListbox.options[nListIndex++] = new Option(childObj.name,childObj.id,bSelected,bSelected);
		if( bSelected )
			childListbox.selectedIndex = nListIndex-1;
	}
	
	//Remove options
	for( ; nListIndex<childListbox.options.length; )
	{
		childListbox.options[nListIndex]=null;
	}
	
	if( childListbox.onPopulate!=null )
		childListbox.onPopulate(childListbox,tempArray.length);
		
}//PopulateListbox

function InitListbox(childArray,childListbox,childID,parentArray,parentListbox,bAddAllWhenNoneSelected,bAddAllOption)
{
	var childObj;
	var tempArray;
	var nIndex;
	var bSelected;

	childListbox.listboxInfo = new ListboxInfo(childArray,bAddAllWhenNoneSelected,bAddAllOption,parentListbox,null);
	if( parentListbox!=null )
		parentListbox.listboxInfo.childListbox = childListbox;
	
	if( childID!=null )
	{		
		UpdateParentListbox(childListbox,childID);

		childObj = GetListboxItem(childArray,childID)
		if( childObj!=null && childObj.parentListboxItem!=null )
			tempArray = childObj.parentListboxItem.childrenArray;
		else
			tempArray = childArray;
	
		PopulateListbox(tempArray,childListbox,childID,bAddAllOption);
	}
	else
	{
	
		if( bAddAllWhenNoneSelected && (parentListbox==null || parentListbox.selectedIndex<0 || parentListbox.options[parentListbox.selectedIndex].Value=="") )
		{				
			PopulateListbox(childArray,childListbox,null,bAddAllOption);
			
		}
		else if( childListbox.listboxInfo.parentListbox!=null )
		{				
			UpdateListbox(childListbox);
		}
		/*
		else 
		{
			PopulateListbox(childArray,childListbox,null,bAddAllOption);		
		}
		*/
		
	}
}//InitListbox


if(!document.getElementById && document.all)
{
	document.getElementById = function(id)
	{
		return document.all[id];
	}
}
else if(!document.getElementById && !document.all)
{
	document.getElementById = function(id)
	{
		// Very old browser. We do this so that
		// object.style keeps working (but
		// without doing anything)
		return { style: {} };
	}
}


