/*
	SingleCombo object
	
	heeft maar 2 comboboxen
	er kan maar 1 item en daarvan dan meerdere subitems gekozen worden


*/

function SingleCombo(p_name,p_formname,p_style,p_all,p_allitem,p_allsubitem)
{

	p_style=p_style||"multiple";
	p_name=p_name||"";
	p_all=p_all||false;
	p_allitem=p_allitem||"";
	p_allsubitem=p_allsubitem||"";

	p_formname=p_formname||"";
	
	var m_form=F_GetElementbyID(p_formname);
	this.value=m_form[p_name].value;
	this.form=m_form;
	this.value2="";
	this.value2split=new Array();

	this.all=p_all;
	this.allitem=p_allitem;
	this.allsubitem=p_allsubitem;
	
	this.items=eval(p_name+"_items");

	this.subitems=eval(p_name+"_subitems");
	this.sourceitem=m_form[p_name+"_sourceitem"];
	this.sourcesubitem=m_form[p_name+"_sourcesubitem"];
	this.name=p_name;
	this.formname=p_formname;
	this.style=p_style;
	this.prompt_item="Item";
	this.prompt_subitem="Subitem";
	this.oldvalue=this.value;
	// methods
	this.fillSourceItem=SingleCombo_fillSourceItem;
	this.fillSourceSubitem=SingleCombo_fillSourceSubitem;
	this.Init=SingleCombo_Init;

}

// wordt aangeroepen door de submit functie van de form
function SingleCombo_valueOf()
{ 
	return this.GetValue();
}

function SingleCombo_Init()
{
	// input this.value: (1,1);(1,2);(2,3)
	// output array: (1,1,1,2,2,3);
	var items=this.value.split(";");
	var i;
	var l_result=new Array();

	if (items[0]!="")
	{

		for (i=0; i<items.length; i++)
		{
			// zoek de goede index van item in p_items
			var a;
			a=items[i].replace(/\(/g,"").replace(/\)/g,"").split(",");
			if (i==0)
				this.value1=parseInt(a[0]);
			this.value2split[this.value2split.length]=a[1];
		}
	}
	else
	{
		this.value1=-1;
		this.value2split=new Array();
	}

	this.fillSourceItem();
	this.fillSourceSubitem(true);
}


function SingleCombo_fillSourceItem() 
{
	var index=0;
	var beginindex=0;
	
	this.sourceitem.options.length=0;
	
	if (this.all)
	{
		this.sourceitem.options[0]=new Option(this.allitem,-1);
		if (this.value1==-1)
			index=0;
		beginindex=1;

	}
	
	for (var i=0; i<this.items.length; i++)
	{
		this.sourceitem.options[i+beginindex]=new Option(this.items[i][1],this.items[i][0]);
		if (this.items[i][0]==this.value1)
			index=i+beginindex;
	}
	
	this.sourceitem.selectedIndex=index;
}

// vul combobox met subitems van geselecteerde item
// p_first geeft aan dat subitems geselecteerd moeten worden 
function SingleCombo_fillSourceSubitem(p_first)
{
	this.sourcesubitem.options.length=0;

	if (this.all && this.sourceitem.selectedIndex==0)
	{
		this.sourcesubitem.options[0]=new Option(this.allsubitem,-1);		
	}
	else
	{
		for (var i=0; i<this.subitems[this.sourceitem.selectedIndex].length; i++)
		{
			this.sourcesubitem.options[i]=new Option(this.subitems[this.sourceitem.selectedIndex][i][1],this.subitems[this.sourceitem.selectedIndex][i][0]);
		}
	}
	
	if (p_first==true)
	{

		for (var i=0; i<this.sourcesubitem.options.length; i++)
			{
				for (var j=0; j<this.value2split.length; j++)
				{
	
					if (parseInt(this.sourcesubitem.options[i].value)==parseInt(this.value2split[j]))
					{
						
						this.sourcesubitem.options[i].selected=true;
					}
				}
			}
	}
			
}


