/**
* Classes included in this file:
*     SelectHelper
*     SelectToggle
* This file requires the use of the following files:
*     base.js
**/
SelectHelper = {}
SelectHelper.AddOption = function(p_selectId, p_value, p_text, p_selected)
{
	if(p_selectId == null || p_selectId.length == 0) { throw ArgumentNullException('p_selectId'); return; }
	SelectHelper.__AddOption(SelectHelper.__GetSetSelectBox(p_selectId), p_value, p_text, p_selected);
	return;
}
SelectHelper.TransferOptions = function(p_sourceSelectId, p_destinationSelectId, p_transferSelectedItemsOnly, p_removeItemsFromSource)
{
	if(p_sourceSelectId == null || p_sourceSelectId.length == 0) { throw ArgumentNullException('p_sourceSelectId'); return; }
	if(p_destinationSelectId == null || p_destinationSelectId.length == 0) { throw ArgumentNullException('p_destinationSelectId'); return; }
	var v_option;
	var v_source = SelectHelper.__GetSetSelectBox(p_sourceSelectId);
	var v_dest   = SelectHelper.__GetSetSelectBox(p_destinationSelectId);
	p_transferSelectedItemsOnly = p_transferSelectedItemsOnly ? true : false;
	p_removeItemsFromSource = p_removeItemsFromSource ? true : false;
	for(var i = 0; i < v_source.options.length; i++)
	{
		if(p_transferSelectedItemsOnly == false || v_source.options[i].selected == true)
		{
			SelectHelper.__AddOption(v_dest, v_source.options[i].value, v_source.options[i].text, v_source.options[i].selected);
			if(p_removeItemsFromSource == true) { v_source.remove(i); i--; }
		}
	}
	return;
}
SelectHelper.SelectAll = function(p_selectId)
{
	if(p_selectId == null || p_selectId.length == 0) { throw ArgumentNullException('p_selectId'); return; }
	var v_select = SelectHelper.__GetSetSelectBox(p_selectId);
	for(var i = 0; i < v_select.options.length; i++) { v_select.options[i].selected = true; }
	return;
}
SelectHelper.DeselectAll = function(p_selectId)
{
	if(p_selectId == null || p_selectId.length == 0) { throw ArgumentNullException('p_selectId'); return; }
	var v_select = SelectHelper.__GetSetSelectBox(p_selectId);
	for(var i = 0; i < v_select.options.length; i++) { v_select.options[i].selected = false; }
	return;
}
SelectHelper.RemoveOptions = function(p_selectId, p_onlyRemoveSelected)
{
	if(p_selectId == null || p_selectId.length == 0) { throw ArgumentNullException('p_selectId'); return; }
	p_onlyRemoveSelected = p_onlyRemoveSelected ? true : false;
	var v_select = SelectHelper.__GetSetSelectBox(p_selectId);
	for(var i = 0; i < v_select.options.length; i++) { if(p_onlyRemoveSelected == false || v_select.options[i].selected == true) { v_select.remove(i); i--; } }
	return;
}
SelectHelper.__GetSetSelectBox = function(p_selectId)
{
	var v_box = document.getElementById(p_selectId);
	if(v_box == null) { throw Exception('Select box is NULL!'); }
	return v_box;
}
SelectHelper.__AddOption = function(p_select, p_value, p_text, p_selected)
{
	p_selected = p_selected ? true : false;
	var v_option = document.createElement('option');
	v_option.value = p_value;
	v_option.text = p_text;
	v_option.selected = p_selected;
	try { p_select.add(v_option, null); } // For all browsers but IE (standard way).
	catch(e) { p_select.add(v_option); } // For IE.
	return;
}
/******************************************************************************/
function SelectToggle(p_leftIdString, p_rightIdString)
{
	if(p_leftIdString == null || p_leftIdString.length == 0) { throw ArgumentNullException('p_leftIdString'); return; }
	if(p_rightIdString == null || p_rightIdString.length == 0) { throw ArgumentNullException('p_rightIdString'); return; }
	this.LeftSelectBoxId  = String(p_leftIdString);
	this.RightSelectBoxId = String(p_rightIdString);
	return;
}
SelectToggle.prototype.GetType = function() { return 'SelectToggle'; }
SelectToggle.prototype.ToString = function() { return '[object ' + this.GetType() + ']'; }
SelectToggle.prototype.toString = function() { return this.ToString(); }
SelectToggle.prototype.TransferLeftToRight = function(p_removeFromLeft, p_transferAll)
{
	p_transferAll = p_transferAll ? false : true; // We need to reverse the value because SelectHelper.TransferOptions() expects a TRUE value when we are only transfering selected items.
	SelectHelper.TransferOptions(this.LeftSelectBoxId, this.RightSelectBoxId, p_transferAll, p_removeFromLeft);
	return;
}
SelectToggle.prototype.TransferRightToLeft = function(p_removeFromRight, p_transferAll)
{
	p_transferAll = p_transferAll ? false : true; // We need to reverse the value because SelectHelper.TransferOptions() expects a TRUE value when we are only transfering selected items.
	SelectHelper.TransferOptions(this.RightSelectBoxId, this.LeftSelectBoxId, p_transferAll, p_removeFromRight);
	return;
}
SelectToggle.prototype.RemoveSelectedLeft = function() { SelectHelper.RemoveOptions(this.LeftSelectBoxId, true); return; }
SelectToggle.prototype.RemoveSelectedRight = function() { SelectHelper.RemoveOptions(this.RightSelectBoxId, true); return; }
SelectToggle.prototype.RemoveSelectedBoth = function() { this.RemoveSelectedLeft(); this.RemoveSelectedRight(); return; }
SelectToggle.prototype.ClearLeft = function() { SelectHelper.RemoveOptions(this.LeftSelectBoxId, false); return; }
SelectToggle.prototype.ClearRight = function() { SelectHelper.RemoveOptions(this.RightSelectBoxId, false); return; }
SelectToggle.prototype.ClearBoth = function() { this.ClearLeft(); this.ClearRight(); return; }
SelectToggle.prototype.SelectAllLeft = function() { SelectHelper.SelectAll(this.LeftSelectBoxId); return; }
SelectToggle.prototype.SelectAllRight = function() { SelectHelper.SelectAll(this.RightSelectBoxId); return; }
SelectToggle.prototype.SelectAllBoth = function() { this.SelectAllRight(); this.SelectAllLeft(); return; }
