// ----------------------------------------------------------------------
// 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.
//
// smg_form_checkAllCheckboxesMatchingPrefix()
// Check all checkboxes where the name begins with the specified prefix.
//
// smg_form_uncheckAllCheckboxesMatchingPrefix()
// Clear all checkboxes where the name begins with the specified prefix.
//
// 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;
//  } else {
//    alert ("You must select at least one message to place on the page.");
//    hiddenInput.value = "";
//      return false;
  }

}  // prepareData()

// ----------------------------------------------------------------------
// Move elements from one select box to another

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

function moveSelectElements(source, destination) {
        var sourceOptions = source.options;
        var destOptions = destination.options;
        var newDestOptions = new Array();
        
        // 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()
