var preSelectedValue = 0;
var optArray = new Array();

function DataChainObj(id, name, childs) 
{
	this.id = id;
	this.name = name;
	if (typeof(childs) != 'undefined') this.childs = childs;
}


function _InitializeChainedData(targetID)
{	
	var cboTargetObj = getObjectByID(targetID);
	var nextTargetObj;	
	var objArray = eval(cboTargetObj.getAttribute("ChainedDataSource"));		
	
	var ChainedObj;// = new DataChainObj();			
	var oOption;
	var loadData = false;
	//if(!arguments[1])
	//	loadData = arguments[1];
	//alert(loadData);	
	//if(loadData)
	//return;
	if(cboTargetObj.options.length == 1)
	{
		_ClearSelectContent(cboTargetObj, 'all');
		loadData = true;
	}
	//alert(loadData);
	//loadData = false;	
	
	for(var i=0; i<objArray.length; i++)
	{
		ChainedObj = objArray[i]; 
		//alert(ChainedObj.id);
		if(loadData)
		{
			oOption = document.createElement('OPTION');
			cboTargetObj.options.add(oOption);
			
			oOption.value = ChainedObj.id;
			
			oOption.text = ChainedObj.name;
		}
		else
			oOption=FindItem(cboTargetObj, ChainedObj.id);
		
		if (ChainedObj.childs != null)
		{		
			optArray[targetID + ChainedObj.id] = ChainedObj.childs;
			oOption.setAttribute("ChainedDataSource", targetID + ChainedObj.id);
		}
//		alert('init-opt ds' + oOption.getAttribute("ChainedDataSource"));
	}
	
	
	if(cboTargetObj.getAttribute('SelectedID'))
	{
		//alert('sl');
		selectCombo(cboTargetObj, cboTargetObj.getAttribute('SelectedID'));
		cboTargetObj.removeAttribute('SelectedID');
		_OnDataChange(cboTargetObj);
	}	
}

function FindItem(targetObj, value)
{
	for(var i=0; i<targetObj.options.length; i++)
	{
		var oOpt = targetObj.options[i];
		if(oOpt.value == value)
			return oOpt;
	}
	return null;
}
function Fill(targetObj, chainedObjArray)
{
	var ChainedObj = new DataChainObj();
	var oOption = document.createElement('OPTION');
		
	targetObj.options.length = 1;
	for(var i=0; i<chainedObjArray.length; i++)
	{
		
		ChainedObj = chainedObjArray[i];
		oOption = document.createElement('OPTION');
		targetObj.options.add(oOption);
		oOption.value = ChainedObj.id;
		oOption.text = ChainedObj.name;		
		if (ChainedObj.childs != null)
		{
			optArray[targetObj + ChainedObj.id] = ChainedObj.childs;
			oOption.setAttribute("ChainedDataSource", targetObj + ChainedObj.id);
		}
	}
}
function _FillChainedData(targetObjID)//, parentObjID)
{
	var objArray = getObjectByID("chainedObjArray");
	var cboTargetObj = getObjectByID(targetObjID);	
	var oOption = document.createElement('OPTION');
	
	var parentObj, childObj, i;
	
	//how to get parentObj if  grandPa > Son > grandSon	
	var tempArray = objArray;
	if (currentIDArray.length > 0)
	{
		parentObj = tempArray[currentIDArray[i]];
	}
	else
	{
		for (i=1; i<currentIDArray.length; i++)
		{
			parentObj = tempArray[currentIDArray[i]];		
			if (parentObj.childs != null)
			{
				tempArray = parentObj.childs;
			}
			else
				return;
		}
	}
	if (parentObj.childs != null)
	{
		for (i=0; i<parentObj.childs.length; i++)
		{
			childObj = parentObj.childs[i];
			
			oOption = document.createElement('OPTION');
			cboTargetObj.options.add(oOption);
			oOption.value = childObj.id;
			oOption.text = childObj.name;
			oOption.objID = i;
		}
	}
}
function _OnDataChange(obj)
{	
	_curObj = obj;//event.srcElement;
	
	var chainedObjArray;
	if(_curObj.getAttribute("NextChain") != 'undefined')
	{		
		
		_nextChainObj = getObjectByID(_curObj.getAttribute("NextChain"));
		
		//alert("next chain:" + _nextChainObj.name);	
		
		if(_curObj.selectedIndex == 0)
		{	//alert("_curObj.selectedIndex == 0");
			
			//if(_nextChainObj.selectedIndex != 0)
			//{	
				_ClearSelectContent(_nextChainObj, 'all');
			//}
			_nextChainObj = getObjectByID(_nextChainObj.getAttribute("NextChain"));
			
			if(_nextChainObj != null)
			{
				
				_ClearSelectContent(_nextChainObj, 'all');
			}	
			return;
		}
		//alert(_nextChainObj.id);
		if(_nextChainObj != null)
		{				
			if(_curObj.options[_curObj.selectedIndex].getAttribute("ChainedDataSource") != null )		
				chainedObjArray = optArray[_curObj.options[_curObj.selectedIndex].getAttribute("ChainedDataSource").toString()];			
			
			if (chainedObjArray != null)
			{
				Fill(_nextChainObj, chainedObjArray);
			}
			else
			{
				_nextChainObj.options.length = 1;
			}
				
			if(_nextChainObj.getAttribute('SelectedID'))
			{	
				selectCombo(_nextChainObj, _nextChainObj.getAttribute('SelectedID'));
				_nextChainObj.removeAttribute('SelectedID');				
			}
			
			//For next child element
			_curObj = _nextChainObj;
			//alert('attr' + _nextChainObj.getAttribute("NextChain"));
			_nextChainObj = getObjectByID(_nextChainObj.getAttribute("NextChain"));				
			//alert("next chainr2:" + _curObj.options[_curObj.selectedIndex].value);			
			if(_nextChainObj != null)
			{
				if(_curObj.options[_curObj.selectedIndex].getAttribute("ChainedDataSource") && optArray[_curObj.options[_curObj.selectedIndex].getAttribute("ChainedDataSource").toString()] != null)
					chainedObjArray = optArray[_curObj.options[_curObj.selectedIndex].getAttribute("ChainedDataSource").toString()];
				else if(_curObj.options[_curObj.selectedIndex].getAttribute("ChainedDataSource") != null)
					chainedObjArray = eval(_curObj.options[_curObj.selectedIndex].getAttribute("ChainedDataSource"));
				
				//alert('nxobj' + _nextChainObj);
				if (chainedObjArray != null)
				{
					Fill(_nextChainObj, chainedObjArray);
				}
				else
				{
					_nextChainObj.options.length = 1;
				}
					
				if(_nextChainObj.getAttribute('SelectedID'))
				{	
					selectCombo(_nextChainObj, _nextChainObj.getAttribute('SelectedID'));
					_nextChainObj.removeAttribute('SelectedID');				
				}			
			}
		}
		else
		{
			
		}
	}
	
}
function _EnableDataChain(obj, state, direction)
{
	if(obj == null)
		return;
	obj.disabled = !state;
	if(direction == 'both' || direction == 'next')
	{
		if(obj.getAttribute('NextChain'))
			_EnableDataChain(getObjectByID(obj.getAttribute('NextChain')),state , 'next');
		
	}
	if(direction == 'both' || direction == 'prev')
	{
		if(obj.getAttribute('PrevChain'))
			_EnableDataChain(getObjectByID(obj.getAttribute('PrevChain')),state, 'prev');
	}
	
}
function _OnDataLoad(/*data*/)
{
	//alert('data load:' + data);
	//if(data.return_value)
	//{
	//	_ChangeContent(data.return_value);
	//}
	//else
	//{
		//_ClearSelectContent(_nextChainObj, 'all');
	//	_EnableDataChain(_curObj,true,'both');
		//_curObj.disabled = false;
		//_nextChainObj.disabled = false;
		
	//}
}
function _OnError(data)
{
	
	if (data.message) {
		alert("Context:" + data.context + "\nError: " + data.message);
	}
	//_curObj.disabled = false;
	//_nextChainObj.disabled = false;
	_EnableDataChain(_curObj,true,'both');
	_curObj.selectedIndex = preSelectedValue;
	
}
function _ChangeContent(dataSource)
{
	
	var rows = dataSource.split('||');//Split into Rows
	_ClearSelectContent(_nextChainObj, 'all');
	for(var i = 0; i < rows.length; i++)
	{
		var currow = rows[i];
		var cols = currow.split('#'); //Split into Columns
		//alert("cols:" + cols.toString());
		opt = document.createElement('OPTION');
		opt.value = cols[0];
		opt.text = cols[1];
		_nextChainObj.add(opt);
	}
	if(_nextChainObj.getAttribute('SelectedID'))
	{
		SelectCombo(_nextChainObj, _nextChainObj.getAttribute('SelectedID'));
		_nextChainObj.removeAttribute('SelectedID');
	}
	//_curObj.disabled = false;
	//_nextChainObj.disabled = false;
	_EnableDataChain(_curObj,true,'both');
	preSelectedValue = _curObj.selectedIndex;
}
function _ClearSelectContent(cboObj, type)
{
	if(cboObj == null)
		return;
	cboObj.length = 0;
	//alert('ce' + document.createElement);
	var opt = document.createElement('OPTION');
	opt.value = '';
	opt.text = 'All';
	cboObj.options.add(opt);
	if(type == 'all' && cboObj.getAttribute('NextChain'))
	{
		_ClearSelectContent(getObjectByID(cboObj.getAttribute('NextChain')), type);
	}
}


