var sortitems = true;

function additem(fbox,tbox) {
	move(fbox,tbox);
	removeitembyname(tbox,"None Selected")
	if (sortitems) SortD(tbox);
}

function additembyname(tbox, theName) {
	if (tbox.options.length == 0) {
			var no = new Option();
			no.value = theName;
			no.text = theName;
			tbox.options[0] = no;
			//tbox.length++;
	}
	//BumpUp(tbox); //? needed
}
		
function removeitembyname(fbox, theName) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].value == theName) {
			fbox.options[i].value = "";
			fbox.options[i].text = "";
			}
	}
	BumpUp(fbox);
}

function removeitem(fbox) {
	var i;
	for(i = 0; i < fbox.options.length; i++) 
	{
		if(fbox.options[i].selected && fbox.options[i].value != "") 
		{
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		}
	}
	BumpUp(fbox);
	additembyname (fbox,"None Selected")
}

function move(fbox,tbox) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			var no = new Option();
			no.value = fbox.options[i].value;
			no.text = fbox.options[i].text;
			
			// check that this entry doesn't already exist.  If it doesn't, then add it

			var len = tbox.length;
			var found = false;
			for(var count = 0; count < len; count++) {
				if (tbox.options[count] != null) {
					if (no.text == tbox.options[count].text) {
					found = true;
					break;
					}
				}
			}


			if (found != true) {
				tbox.options[tbox.options.length] = no;
				len++;
			}
			// tbox.options[tbox.options.length] = no;
			// fbox.options[i].value = "";
			// fbox.options[i].text = "";
			}
	}
	// BumpUp(fbox);
	if (sortitems) SortD(tbox);
}

function BumpUp(box) {
 	var i = 0; 
 	var boxLength = box.options.length;
	while (i < boxLength) {
		if (box.options[i].value == "")
		{
			box.options[i] = null;
			boxLength--;
			// by setting the individual Option object to null, it is removed from the array
			// and the other objects' indexes are automatically adjusted
		}
		else
			i++; // otherwise look at the next item
	}
} // end function

function SortD(box)  
{
	var temp_opts = new Array();
	var temp = new Object();
	var i;
	
	for(i=0; i<box.options.length; i++)  
	{
		temp_opts[i] = box.options[i];
	}
	for(var x=0; x<temp_opts.length-1; x++)  
	{
		for(var y=(x+1); y<temp_opts.length; y++)  
		{
			if(temp_opts[x].text > temp_opts[y].text)
			{
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
			}
		}
	}
	
	for(i=0; i<box.options.length; i++)  
	{
		box.options[i] = temp_opts[i];
		box.options[i].value = temp_opts[i].value;
		box.options[i].text = temp_opts[i].text;
	}
}

function selectAll(theSelect)
{
	if(theSelect.options.length > 0)
	{
		for(var i =0;i < theSelect.options.length; i++) 
		{
			theSelect.options[i].selected = true;
		}
	}
}

function numberOfSelectedEntries(theSelect) {
	n = 0
	count = 0
	while (n < theSelect.options.length) {
	if(theSelect.options[n].selected && theSelect.options[n].value != "None Selected") {
		count ++ }
	n ++ }
	return count
}

function ValidateSelectedLocations(oSrc, args)
{
	if(args.Value == "None Selected")
	{
		args.IsValid = false;
	}

}