// ----------------------------------------------------------------------
// Filename: form_lib.js
// Created By: Steve Gallo
// Date: 3/6/2002
//
// General Javascript routines that are useful to have.
//
// redirect()
// Redirect the user to the specified page.
//
// checkAllCheckboxes()
// Check all checkboxes on a form.
//
// uncheckAllCheckboxes()
// Uncheck all checkboxes on a form.
//
// prepareData()
// Prepare the form data for submission.
//
// moveSelectElements()
// Move elements from one select box to another.
//
// ----------------------------------------------------------------------

// ----------------------------------------------------------------------
// Redirect the user to the specified page

function redirect(page) {
	document.location = page;
}  // redirect()

// ----------------------------------------------------------------------
// Check all checkboxes on a form

function smg_form_checkAllCheckboxes(f) {
	for (var i=0; i < f.length; ++i) {
		if (f.elements[i].type == 'checkbox') {
			f.elements[i].checked = true;
		}
	}
}  // smg_form_checkAllCheckboxes()


// ----------------------------------------------------------------------
// Check all checkboxes on a form where the checkbox name begins with
// the specified prefix

function smg_form_checkAllCheckboxesMatchingPrefix(f, prefix) {
	for (var i=0; i < f.length; ++i) {
		if (f.elements[i].type == 'checkbox') {
			if (f.elements[i].name.indexOf(prefix) != 0) { continue; }
			f.elements[i].checked = true;
		}
	}
}  // smg_form_checkAllCheckboxes()

// ----------------------------------------------------------------------
// Uncheck all checkboxes on a form

function smg_form_uncheckAllCheckboxes(f) {
	for (var i=0; i < f.length; ++i) {
		if (f.elements[i].type == 'checkbox') {
			f.elements[i].checked = false;
		}
	}
}  // smg_form_uncheckAllCheckboxes()

// ----------------------------------------------------------------------
// Uncheck all checkboxes on a form where the checkbox name begins with
// the specified prefix

function smg_form_uncheckAllCheckboxesMatchingPrefix(f, prefix) {
	for (var i=0; i < f.length; ++i) {
		if (f.elements[i].type == 'checkbox') {
			if (f.elements[i].name.indexOf(prefix) != 0) { continue; }
			f.elements[i].checked = false;
		}
	}
}  // smg_form_uncheckAllCheckboxes()

// -----------------------------------------------------------------
// Select all options (items) in a multiple select box

function selectAllOptions(box) {
	if (box == null || box.options == null) return;
	
	box.selectedIndex = -1;
	
	for (var i=0; i < box.options.length; i++) {
		box.options[i].selected = true;
	}
}  // selectAllOptions()


// -----------------------------------------------------------------
// Unselect all options (items) in a multiple select box

function unselectAllOptions(box) {
	if (box == null || box.options == null) return;
	
	box.selectedIndex = -1;
	for (var i=0; i < box.options.length; i++) {
		box.options[i].selected = false;
	}
}  // unselectAllOptions()


// ----------------------------------------------------------------------
// Prepare the form data for submission.  This entails packing
// the list of messages into a hidden form element whose value
// has been set to null.

function prepareData(form) {
  var selectList = form.current_items;
  var length = selectList.length;
  var hiddenInput = form.item_list;

  // Go through the select list and place the values into an
  // array.

  tmp_array = new Array(length);
  for (i=0; i < length; i++) {
    tmp_array[i] = selectList.options[i].value;
  }

  // Set the value of the hidden input element

  if (tmp_array.length != 0) {
    hiddenInput.value = tmp_array.toString();
    return true;
  }

}  // prepareData()

// ----------------------------------------------------------------------
// Compare the text of two select options.  Used by
// moveSelectElements() during sorting.
// ----------------------------------------------------------------------

function compareOptions(opt1, opt2)
{
  if (opt1.text < opt2.text) return -1;
  if (opt1.text == opt2.text) return 0;
  return 1;
}

// ----------------------------------------------------------------------
// Move elements from one select box to another.  This is meant to be
// used with two lists, side by side, where the use selects elements
// from one list and moves them to another.  Note that
// prepareSelectList() should be called on submission because the
// options in the target select list may not actually be selected.
// The destination list will be re-sorted after the new elements are
// added.
// ----------------------------------------------------------------------

function moveSelectElements(source, destination)
{
  var sourceOptions = source.options;
  var destOptions = destination.options;
  var newDestOptions = new Array();

  if ( 0 == sourceOptions.length ||
       -1 == sourceOptions.selectedIndex )
  {
    return;
  }

  // Examine each source option.  We need to go through the list
  // backwards because we are shortening the list every time we
  // switch elements.

  for (i = sourceOptions.length - 1; i >= 0; i--) {
    
    // For each selected source element create a new option and add it
    // to the destination list.
	
    if ( sourceOptions[i].selected == true ) {
	                                        
      // The element was found, create a new one and store it into the
      // new destination values array.  
	
      newOption = new Option (sourceOptions[i].text, sourceOptions[i].value);
      newDestOptions[newDestOptions.length] = newOption;
	
      // Remove the element from the source list
	                        
      sourceOptions[i] = null;
	      
    }  // if ( sourceOptions[i].selected == true )

  }  // for (i >= 0) 

  // Sort the destination list
        
  // Add the existing items from the destination list and sort
  // the entire list.
        
  for (i=0; i < destOptions.length; i++) {
    newOption = new Option (destOptions[i].text, destOptions[i].value);
    newDestOptions[newDestOptions.length] = newOption;
  }
  newDestOptions.sort(compareOptions);
        
  // Clear the destination list and re-populate it with the sorted list
        
  destOptions.length = 0;
  for (i=0; i < newDestOptions.length; i++) {
    destOptions[destOptions.length] = newDestOptions[i];
  }
  
  // Reset the selections

  sourceOptions.selectedIndex = -1;
  destOptions.selectedIndex = -1;

  return true;

}  // moveSelectElements()

// ----------------------------------------------------------------------
// Prepare a select list for submission by marking all of its elements
// as selected or the browser will not think that any have been
// selected by a user.  This is useful in conjunction with
// moveSelectElements()
// ----------------------------------------------------------------------

function prepareSelectList(element)
{
  for (i = 0; i < element.length; i++ )
  {
    element[i].selected = true;
  }
}  // prepareSelectList()

