var CATEGORIES_FIELD_NAME = "leftList"
var SELECTED_FIELD_NAME = "rightList"
var HIDDEN_FIELD_NAME = "subscribed_to"

/*
 * function get_categories_string()
 *
 * returns a comma separated list of the values from the selected categories
 * select box
 */
function get_categories_string()
  {
  field = document.getElementById(SELECTED_FIELD_NAME);
  
  var csv_string = '';
  for (var i = 0; i < field.options.length; i++)
    {
    if (i == 0)
      csv_string += field.options[i].value;
    else
      csv_string += ',' + field.options[i].value;
    }
  
  return csv_string;
  }

/*
 * function swap(fromFieldName, toFieldName)
 *
 * swaps the selected value from one select box to the other,
 * and then updates the hidden field with the new list of
 * selected categories
 */
function swap(fromFieldName, toFieldName)
  {
  fromField = document.getElementById(fromFieldName);
  toField = document.getElementById(toFieldName);
  var toOptions = new Array();
  var selectedOption;

  index = fromField.selectedIndex

  if (index < 0)
    return;

  selectedOption = fromField.options[index];

  fromField.options[index] = null
  toField.options[toField.options.length] = selectedOption

  sort_select(toField);

  hiddenField = document.getElementById(HIDDEN_FIELD_NAME);
  hiddenField.value = get_categories_string();
  }

/*
 * function has_options(obj)
 *
 * determines if a select box has options or not
 */
function has_options(obj)
  {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

/*
 * function sort_select(obj)
 *
 * sorts the contents of a select box based on the option text
 */
function sort_select(obj)
	{
	var o = new Array();

  if (!has_options(obj)) { return; }

  for (var i=0; i<obj.options.length; i++)
		{
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}

  if (o.length == 0)
		{
    return;
    }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++)
    {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

/*
 * function populate_subscribed_to()
 *
 * on a page load, populates the subscribed to select box to reflect
 * the initial contents of the hidden field
 */
function populate_subscribed_to()
  {
  hiddenField = document.getElementById(HIDDEN_FIELD_NAME);
  categoriesField = document.getElementById(CATEGORIES_FIELD_NAME);
  selectedField = document.getElementById(SELECTED_FIELD_NAME);

  ids = hiddenField.value.split(',');

  // add each selected field
  for (var i = 0; i < ids.length; i++)
    {
    // first, find the entry from the left with the matching value
    for (var j = 0; j < categoriesField.options.length; j++)
      {
      if (categoriesField.options[j].value == ids[i])
        {
        // add this to the list of selected categories
        selectedField.appendChild(categoriesField.options[j]);
        // this should work, and works above, but for some reason fails in IE
        //selectedField.options[selectedField.options.length] = categoriesField.options[j];
        }
      }
    }

  sort_select(categoriesField);
  }

/*
 * function set_focus()
 *
 * on a page with forms, sets the focus to the first text input field found
 */
function set_focus()
  {
  if (document.forms.length > 0)
    {
    var field = document.forms[0];
    for (i = 0; i < field.length; i++)
      {
      if ((field.elements[i].type == "text") ||
          (field.elements[i].type == "textarea") ||
	        (field.elements[i].type.toString().charAt(0) == "s"))
        {
        document.forms[0].elements[i].focus();
        break;
        }
      }
    }
  }

/*
 * function on_load()
 *
 * main on_load handler for all pages
 */
function on_load()
  {
  set_focus();
  // if we are on one of the digest category selection pages, populate the selected list
  hiddenField = document.getElementById(HIDDEN_FIELD_NAME);
  if (hiddenField == null)
    {
    return;
    }
  else
    {
    if (hiddenField.value != "")
      {
      populate_subscribed_to();
      }
    }
  }