

FilterCombo = function(_FatherCombosId,_ChildComboId,_SplitChar,_ContainsDummy,_DummyValue)
{
	var i =0;	
	//Creates array of father controls
	this.FathersComboList = new Array(_FatherCombosId.split('-').length)
	//stores fathers controls in array
	while (i < _FatherCombosId.split('-').length )
	{
		this.FathersComboList[i] = document.getElementById(_FatherCombosId.split('-')[i]);
		i++;
	}
	
	this.ChildCombo =  document.getElementById(_ChildComboId);
	this.SplitChar = _SplitChar;
	this.ContainsDummy = _ContainsDummy;
	this.DummyValue = _DummyValue;
	
	if ( this.FathersComboList.length == 0 || this.ChildCombo == null || this.SplitChar == "" )
	{
		alert ('Error on Object FilterCombo ')
		return false;
	}
	
	if ( this.SplitChar == "|" )
	{
		this.SplitElementChar = "@";
	}
	else
	{
		this.SplitElementChar = "|";
	}
	this.Elements = new Array(this.ChildCombo.options.length);
	
	//Fill options to Elements
	i = 0;
	while (i < this.ChildCombo.options.length)
	{
		this.Elements[i] =  this.ChildCombo.options[i].value + this.SplitElementChar + this.ChildCombo.options[i].text;
		i++;
	}
	
}

FilterCombo.prototype = 
{
	_getFatherSelectedItem : function()
	{
		var selectedItem = ""
		var tempvalue = ""
		
		if (this.FathersComboList.length != 0)
		{
			var i,x
			var options
			
			for (i=0;i<this.FathersComboList.length;i++)
			{
				options = this.FathersComboList[i].getElementsByTagName("option");
				for( x = 0; x < options.length; ++x )
				{
					if( options[x].selected )
					{
						if ( Trim(options[x].value) == "")
							tempvalue = "x";
						else
							tempvalue = Trim(options[x].value);
						
						if (Trim(selectedItem) == "")
							selectedItem = tempvalue;
						else
							selectedItem = selectedItem+"-"+tempvalue;
						break;
					}
				}
			}
		}
		return selectedItem;
	},
	
	_clearChildElements : function()
	{
		if ( this.ChildCombo != null )
		{
			// Remove all selected options
			while ( this.ChildCombo.options.length != 0 )
			{
				this.ChildCombo.remove(0);
			}
		}
	},
	
	_getChildSelectedItem : function()
	{
		if (this.ChildCombo != null)
		{
			var options = this.ChildCombo.getElementsByTagName("option");
			for( var x = 0; x < options.length; ++x )
			{
				if( options[x].selected )
				{
					return options[x].value;
					break;
				}
			}
		}
	},
	
	_setChildSelectedItem : function(_value)
	{
		if (this.ChildCombo != null)
		{
			var options = this.ChildCombo.getElementsByTagName("option");
			for( var x = 0; x < options.length; ++x )
			{
				if( options[x].value == _value )
				{
					options[x].selected = true;
				}
				else
				{
					options[x].selected = false;
				}
			}
		}
	},

	onChange : function()
	{
		if ( this.FathersComboList.length == 0 || this.ChildCombo == null || this.SplitChar == "" )
		{
			alert ('Error on Object FilterCombo ')
			return false;
		}
	
		var childDefaultValue = this._getChildSelectedItem();
		var pattern = new RegExp('^'+this._getFatherSelectedItem(),'i'); 
		var i = 0;
		var value,text;
		
		this._clearChildElements();
		
		if ( this.ContainsDummy )
		{
			value = this.Elements[0].split(this.SplitElementChar)[0];
			text = this.Elements[0].split(this.SplitElementChar)[1];
			this.ChildCombo.options[this.ChildCombo.options.length] = new Option(text,value);
			i++;
		}
		
		if ( this._getFatherSelectedItem() != this.DummyValue )
		{
			while (i < this.Elements.length)
			{
				if( pattern.test( this.Elements[i].split(this.SplitElementChar)[0].split(this.SplitChar)[0] ) )
				{
					value = this.Elements[i].split(this.SplitElementChar)[0].split(this.SplitChar)[1];
					text = this.Elements[i].split(this.SplitElementChar)[1];
					this.ChildCombo.options[this.ChildCombo.options.length] = new Option(text,value);
				}
				i++;
			}
			this._setChildSelectedItem(childDefaultValue.split(this.SplitChar)[1]);
		}
	}
}

