var codeField = "newCode";
var companiesField = "companies";
var codeTable = "codes";
var finalField = "finalList";


function appendCompany()
{
	// Get the code
	//var code = document.forms[codeTable][codeField].value;
	var elSel = document.getElementById(companiesField);
	var i;
	for (i = elSel.length - 1; i>=0; i--) 
	{
    		if (elSel.options[i].selected) 
		{
      			//elSel.remove(i);
			appendCode(elSel.options[i].value);
    		}
  	}

}

function appendCode(code)
{
	// Get the code from the code field if not already present
	if(code=="")
	{
		code = document.forms[codeTable][codeField].value;
		document.forms[codeTable][codeField].value = ""; //Blank that field
	}
	
	//Check for dup
	if(isDup(code))
		return;

	//Create the new option
	var elOptNew = document.createElement('option');
	elOptNew.text = code;
	elOptNew.value = code;
	var elSel = document.getElementById('finalList');

	try 
	{
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) 
	{
		elSel.add(elOptNew); // IE only
	}
	
}

//This removes the currently selected code from the final list
function removeSelectedCode(code)
{
	var finalList = document.getElementById(finalField);
	var i;

	for (i = finalList.length - 1; i>=0; i--) 
	{
    		if (finalList.options[i].selected) 
		{
      		finalList.remove(i);
    		}
  	}
}

//This function checks whether a code is already in the list, returns true if it is
function isDup(code)
{
	var elSel = document.getElementById(finalField);
	var i;
	for (i = elSel.length - 1; i>=0; i--) 
	{
    		if (elSel.options[i].value == code) 
		{
      			return true;
    		}
  	}
	return false;
}
