function SGOE_DualList_Init() {
	for( var i = 0; i < SGOE_DualLists.length; i++ ) {
		var parentName = SGOE_DualLists[i];
		
		var leftBox = DynamicListBox_FindControl(parentName + "LeftBox");
		var rightBox = DynamicListBox_FindControl(parentName + "RightBox");
		
		if ( leftBox == null || rightBox == null ) {
			return;
		}
		
		var moveRight = DynamicListBox_FindControl(parentName + "MoveRight");
		var moveAllRight = DynamicListBox_FindControl(parentName + "MoveAllRight");
		var moveLeft = DynamicListBox_FindControl(parentName + "MoveLeft");
		var moveAllLeft = DynamicListBox_FindControl(parentName + "MoveAllLeft");
		var moveUp = DynamicListBox_FindControl(parentName + "MoveUp");
		var moveDown = DynamicListBox_FindControl(parentName + "MoveDown");
		
		var dualListParent = new Object();
		
		dualListParent.LeftBox = leftBox;
		dualListParent.RightBox = rightBox;
		dualListParent.MoveRight = moveRight;
		dualListParent.MoveAllRight = moveAllRight;
		dualListParent.MoveLeft = moveLeft;
		dualListParent.MoveAllLeft = moveAllLeft;
		dualListParent.MoveUp = moveUp;
		dualListParent.MoveDown = moveDown;
		dualListParent.SetEnabled = SGOE_DualList_SetEnabled;
		
		leftBox.Parent = dualListParent;
		leftBox.MoveSelection = SGOE_DualList_MoveRight;
		leftBox.ondblclick = leftBox.MoveSelection;

		rightBox.Parent = dualListParent;
		rightBox.MoveSelection = SGOE_DualList_MoveLeft;
		rightBox.ondblclick = rightBox.MoveSelection;
		if ( moveUp != null && moveDown != null ) {
			rightBox.SetUpDownEnabled = SGOE_DualList_SetUpDownEnabled;
			rightBox.onchange = rightBox.SetUpDownEnabled;
		} else {
			rightBox.SetUpDownEnabled = function() {};
		}

		moveRight.Parent = dualListParent;
		moveRight.DoCommand = SGOE_DualList_MoveRight;
		moveRight.onclick = moveRight.DoCommand;

		if ( moveAllRight != null ) {
			moveAllRight.Parent = dualListParent;
			moveAllRight.DoCommand = SGOE_DualList_MoveAllRight;
			moveAllRight.onclick = moveAllRight.DoCommand;
		}

		moveLeft.Parent = dualListParent;
		moveLeft.DoCommand = SGOE_DualList_MoveLeft;
		moveLeft.onclick = moveLeft.DoCommand;

		if ( moveAllLeft != null ) {
			moveAllLeft.Parent = dualListParent;
			moveAllLeft.DoCommand = SGOE_DualList_MoveAllLeft;
			moveAllLeft.onclick = moveAllLeft.DoCommand;
		}

		if ( moveUp != null ) {
			moveUp.Parent = dualListParent;
			moveUp.DoCommand = SGOE_DualList_MoveUp;
			moveUp.onclick = moveUp.DoCommand;
		}

		if ( moveDown != null ) {
			moveDown.Parent = dualListParent;
			moveDown.DoCommand = SGOE_DualList_MoveDown;
			moveDown.onclick = moveDown.DoCommand;
		}

		if ( !moveRight.disabled ) {
			dualListParent.SetEnabled();
			if ( moveUp != null ) {
				rightBox.SetUpDownEnabled();
			}
		}
	}
}
function SGOE_DualList_SetEnabled() {
	var leftItemsEmpty = ( this.LeftBox.options.length == 0 );
	var rightItemsEmpty = ( this.RightBox.options.length == 0 );
	this.MoveRight.disabled = leftItemsEmpty;
	if ( this.MoveAllRight != null ) {
		this.MoveAllRight.disabled = leftItemsEmpty;
	}
	this.MoveLeft.disabled = rightItemsEmpty;
	if ( this.MoveAllLeft != null ) {
		this.MoveAllLeft.disabled = rightItemsEmpty;
	}
	this.RightBox.SetUpDownEnabled();
}
function SGOE_DualList_SetUpDownEnabled() {
	var selectedIndex = this.options.selectedIndex;
	this.Parent.MoveUp.disabled = ( selectedIndex <= 0 );
	this.Parent.MoveDown.disabled = ( selectedIndex == -1 || selectedIndex == this.options.length - 1 );
}
function SGOE_DualList_MoveRight() {

	SGOE_DualList_MoveSelectedItems(this.Parent.LeftBox,this.Parent.RightBox);
	this.Parent.SetEnabled();
	return false;
}
function SGOE_DualList_MoveAllRight() {
	SGOE_DualList_MoveAllItems(this.Parent.LeftBox,this.Parent.RightBox);
	this.Parent.SetEnabled();
	return false;
}
function SGOE_DualList_MoveLeft() {
	SGOE_DualList_MoveSelectedItems(this.Parent.RightBox,this.Parent.LeftBox);
	this.Parent.SetEnabled();
	return false;
}
function SGOE_DualList_MoveAllLeft() {
	SGOE_DualList_MoveAllItems(this.Parent.RightBox,this.Parent.LeftBox);
	this.Parent.SetEnabled();
	return false;
}
function SGOE_DualList_MoveUp() {
	this.Parent.RightBox.MoveUp();
	this.Parent.SetEnabled();
	return false;
}
function SGOE_DualList_MoveDown() {
	this.Parent.RightBox.MoveDown();
	this.Parent.SetEnabled();
	return false;
}

function SGOE_DualList_MoveSelectedItems(source,target) {

	// hardcode: se impide que se seleccionen hasta 5 provincias en el cliente
	if (source.id == "cmbProvincias_dualList_LeftBox") {
			var items = 0;
			for (i=0;i<source.options.length;i++) {
				if (source.options[i].selected) items++;
			}
			if (items  + target.options.length > 5) {
				alert("Por favor, seleccione hasta 5 provincias");
				return false;
			}
		}
		if (source.id == "cmbSectoresPreferentes_dualList_LeftBox") {
			var items = 0;
			for (i=0;i<source.options.length;i++) {
				if (source.options[i].selected) items++;
			}
			if (items  + target.options.length > 10) {
				alert("Por favor, seleccione hasta 10 sectores");
				return false;
			}
		}
		
		if (source.id == "cmbProvinciasPreferentes_dualList_LeftBox") {
			var items = 0;
			for (i=0;i<source.options.length;i++) {
				if (source.options[i].selected) items++;
			}
			if (items  + target.options.length > 10) {
				alert("Por favor, seleccione hasta 10 provincias");
				return false;
			}
		}
	// fin hardcode

	if ( source.options.length == 0 ) {
		return;
	}
	var originalIndex = source.options.selectedIndex;
	while ( source.options.selectedIndex >= 0 ) {
		SGOE_DualList_MoveItem( source.options.selectedIndex, source, target );
	}
	if ( originalIndex < source.options.length ) {
		source.options.selectedIndex = originalIndex;
	} else {
		source.options.selectedIndex = source.options.length - 1;
	}
	target.options.selectedIndex = target.options.length - 1;
}
function SGOE_DualList_MoveAllItems(source,target) {
	while ( source.options.length > 0 ) {
		SGOE_DualList_MoveItem( 0, source, target );
	}
}
function SGOE_DualList_MoveItem(itemIndex,source,target) {
	var itemValue = source.options[itemIndex].value;
	var itemText = source.options[itemIndex].text;
	source.Remove( itemIndex );
	target.Add(itemValue, itemText);
}
