var oSelectHandler =
{
	arSelects: [],

	add: function(oSelect)
	{
		this.arSelects[oSelect.sId] = oSelect;
	},

	onChange: function(event, oSelect)
	{
		var oSelect = this.arSelects[oSelect.id];

		if (oSelect != null)
			oSelect.onChange(event);
	}
};

function Group(sId, sName)
{
	this.sId = sId;
	this.sName = sName;
	this.arItems = [];
	this.arIdMapping = [];
}

Group.prototype.add = function(oItem)
{
	this.arItems[this.arItems.length] = oItem;
	this.arIdMapping[oItem.sId] = oItem;
}

function Item(sId, sName)
{
	this.sId = sId;
	this.sName = sName;
}

function StandardSelect(sId, oDynamicSelect)
{
	this.sId = sId;
	this.oSelect = document.getElementById(this.sId);
	this.oConnectedTo = oDynamicSelect || null;
	this.arItems = [];
}

StandardSelect.prototype.add = function(oStanardItem)
{
	this.arItems[this.arItems.length] = oStanardItem;
}

StandardSelect.prototype.onChange = function(event)
{
	if (this.oConnectedTo)
	{
		this.oConnectedTo.autoFill(event, this.oSelect.options[this.oSelect.selectedIndex].value);
	}

	if (this.fItemSelected)
	{
		var oItem = this.arItems[this.oSelect.selectedIndex -1];
		this.fItemSelected(event, this, oItem);
	}
}

StandardSelect.prototype.selectItem = function(sId)
{
	for (var i = 1; i < this.oSelect.options.length; i++)
	{
		if (this.oSelect.options[i].value == sId)
		{
			this.oSelect.selectedIndex = i;
			this.onChange(null);
			return;
		}
	}
}

function DynamicSelect(sId, oDynamicSelect)
{
	this.sId = sId;
	this.oSelect = document.getElementById(this.sId);
	this.oConnectedTo = oDynamicSelect || null;
	this.fItemSelected = null;
	this.arGroups = [];
	this.oDisplayingGroup = null;
	this.iGroupsAdded = 0;
}

DynamicSelect.prototype.add = function(oGroup)
{
	this.arGroups[oGroup.sId] = oGroup;
	this.iGroupsAdded++;
}

DynamicSelect.prototype.onChange = function(event)
{
	if (this.oConnectedTo)
	{
		this.oConnectedTo.autoFill(event, this.oSelect.options[this.oSelect.selectedIndex].value);
	}

	if (this.fItemSelected)
	{
		var oItem = this.oDisplayingGroup.arIdMapping[this.oSelect.options[this.oSelect.selectedIndex].value];
		this.fItemSelected(event, this, oItem);
	}
}

DynamicSelect.prototype.selectItem = function(sId)
{
	if (this.oDisplayingGroup)
	{
		for (var i = 1; i < this.oSelect.options.length; i++)
		{
			if (this.oSelect.options[i].value == sId)
			{
				this.oSelect.selectedIndex = i;
				this.onChange(null);
				return;
			}
		}
	}
}

DynamicSelect.prototype.autoFill = function(event, sValue)
{
	if (this.iGroupsAdded > 0)
	{
		//this.oSelect.selectedIndex = 1;

		for (var i = this.oSelect.options.length - 1; i > 0; i--)
		{
			this.oSelect.remove(i);
		}

		var oGroup = this.arGroups[sValue];

		if (!oGroup)
		{
			this.oSelect.disabled = true;
			return;
		}

		this.oDisplayingGroup = oGroup;

		for (var i = 0; i < oGroup.arItems.length; i ++)
		{
			var oItem = oGroup.arItems[i];
			var oOption = document.createElement("OPTION");
			oOption.value = oItem.sId;
			oOption.text = oItem.sName;
			this.oSelect.options.add(oOption);
		}

		this.oSelect.disabled = false;
	}
}
