
var IDNumber = 0;
var idNames = new Array("hazard","countriesUsedIn","neutralisation","disarming" );

function NewAddButton()
{
	document.write( "<INPUT type='button'  class='button' value='Add' onclick='AddItemToListbox( GetIDNumberFromName( this.name ) )' name='AddButton" + IDNumber + "'>" );
}

function NewRemoveButton()
{
	document.write( "<INPUT type='button'  class='button' value='Remove' onclick='RemoveItemFromListbox( GetIDNumberFromName( this.name ) )' name='RemoveButton" + IDNumber + "'>" );
}

function NewUpButton()
{
	if ( IDNumber > 1 )
	{
		document.write( "<IMG src='portal_images/i_move_up.jpg' onclick='MoveItemUp( GetIDNumberFromName( this.name ) )' name='UpButton" + IDNumber + "'>" );
	}
}

function NewDownButton()
{
	if ( IDNumber > 1 )
	{
		document.write( "<IMG src='portal_images/i_move_down.jpg' onclick='MoveItemDown( GetIDNumberFromName( this.name ) )' name='UpButton" + IDNumber + "'>" );
	}
}

function IncreaseIDNumber()
{
	IDNumber++;
}

function AddItemToListbox( id )
{
	var ListBox = document.getElementById( idNames[id] + "_currentItems" );
	var ComboBox = document.getElementById( idNames[id] + "_allAvailableItems" );
	var text = ComboBox.options[ ComboBox.selectedIndex ].text;
	var value = ComboBox.options[ ComboBox.selectedIndex ].value;
	
	if ( id < 2 )
	{
		InsertInSortedSelect( text, value, ListBox, true );
		var oldSelectedIndex = ComboBox.selectedIndex;
		ComboBox.remove( ComboBox.selectedIndex );
		ComboBox.selectedIndex = oldSelectedIndex - 1;
	}
	else
	{
		ListBox.selectedIndex = -1;
		ListBox.options[ ListBox.length ] = new Option( text, value, false, true );
	}	
	UpdateHiddenResult( id );
	document.getElementById( "changesMade" ).value = "true";
}

function RemoveItemFromListbox( id )
{
	var ListBox = document.getElementById( idNames[id] + "_currentItems" );
	if ( ListBox.selectedIndex >= 0 )
	{
		if ( id < 2 )
		{	// insert into allAvailableItems
			var ComboBox = document.getElementById( idNames[id] + "_allAvailableItems" );
			InsertInSortedSelect( ListBox.options[ListBox.selectedIndex].text, ListBox.options[ListBox.selectedIndex].value, ComboBox, false );
		}
		ListBox.remove( ListBox.selectedIndex );
	
/*			for ( var i=ListBox.selectedIndex; i<ListBox.length; i++ )
			{
				if ( ListBox.options[i].selected )
				{
					var text = ListBox.options[ i ].text;
					var value = ListBox.options[ i ].value;
					InsertInSortedSelect( text, value, ComboBox, false );
				}
			} 
		}
		for ( var i = ListBox.length-1; i>=ListBox.selectedIndex; i-- )
		{
			if ( ListBox.options[i].selected )
			{
				ListBox.remove( i );
			}
		}*/
		UpdateHiddenResult( id );
	  	document.getElementById( "changesMade" ).value = "true";
	}
}

function MoveItemUp( id )
{
	var ListBox = document.getElementById( idNames[id] + "_currentItems" );
	var selIndex = ListBox.selectedIndex;
	if ( selIndex > 0 )
	{
		var tempOption1 = ListBox.options[selIndex];
		var tempOption2 = ListBox.options[selIndex-1];
		ListBox.options[selIndex-1] = new Option( tempOption1.text, tempOption1.value, false, true );
		ListBox.options[selIndex] = new Option( tempOption2.text, tempOption2.value, false, false );
		document.getElementById( "changesMade" ).value = "true";
	}
	UpdateHiddenResult( id );
}

function MoveItemDown( id )
{
	var ListBox = document.getElementById( idNames[id] + "_currentItems" );
	var selIndex = ListBox.selectedIndex;
	if ( selIndex < ListBox.length-1 && selIndex >= 0 )
	{
		var tempOption1 = ListBox.options[selIndex];
		var tempOption2 = ListBox.options[selIndex+1];
		ListBox.options[selIndex+1] = new Option( tempOption1.text, tempOption1.value, false, true );
		ListBox.options[selIndex] = new Option( tempOption2.text, tempOption2.value, false, false );
		document.getElementById( "changesMade" ).value = "true";
	}
	UpdateHiddenResult( id );
}

function UpdateHiddenResult( id )
{
	var HiddenResult = document.getElementById( idNames[id] + "_hiddenResult" );
	var ListBox = document.getElementById( idNames[id] + "_currentItems" );
	
	HiddenResult.value = "";
	for ( var i=0; i<ListBox.length; i++ )
	{
		HiddenResult.value += ListBox.options[i].value;
		HiddenResult.value += ";";
	}
}

function GetIDNumberFromName( name )
{
	return name.charAt( name.length - 1 );
}


