/*
* selectList v1.2
*
* This is a small library of functions that are used to copy items between
* two <select> lists and change the order of the items displayed in those
* lists.
*
* addSrcToDestList(srcList,destList,unique)
* deleteFromDestList(destList,srcList)
*
* moveUpList(list)
* moveDownList(list)
*
* selectList(list_id)
*
* Note: if using parameter screens, where the options in the left <select>
* list are moved to the right <select> list, be sure to select all the items
* in the <select> lists before submitting the form, otherwise the server
* program won't see them. The selectList(list_id) function is for that purpose.
*
* Copyright (c) 2001 - 2005 Mackley F. Pexton. All rights reserved.
*/
/*
* Add the selected items in a <select> to a destination <select> list.
*
* If the unique flag is true then the selected items are added to the
* destination <select> list only if they do not already exist there.
*
* Thanks to Pankaj Mittal (pankajm@writeme.com) for initial version.
*/
function addSrcToDestList(srcList,destList,unique) {
var len = destList.length;
for(var i = 0; i < srcList.length; i++) {
if ((srcList.options[i] != null) && (srcList.options[i].selected)) {
var found = false;
if (unique) {
//Check if this value already exist in destList.
for(var count = 0; count < len; count++) {
if (destList.options[count] != null) {
if (srcList.options[i].text == destList.options[count].text) {
found = true;
break;
}
}
}
}
if (!found) {
destList.options[len] = new Option(srcList.options[i].text);
destList.options[len].selected = true;
len++;
}
}
}
}
/*
* Delete selected items in a <select> list.
*
* The items deleted from the <select> list can be highlighted in another
* <select> list by specifying it as the second srcList prameter.
*
*/
function deleteFromDestList(destList,srcList) {
var opt = new Object(); // track options that are deleted
var len = destList.options.length;
for(var i = (len-1); i >= 0; i--) {
if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
if (srcList) opt[destList.options[i].value] = true;
destList.options[i] = null;
}
}
if (srcList && srcList.options) {
// Select items in source list that were deleted.
for (i = 0; i < srcList.options.length; i++) {
if (opt[srcList.options[i].value]) {
srcList.options[i].selected = true;
}
else {
srcList.options[i].selected = false;
}
}
}
}
/*
* Move selected items in a <select> list up or down.
*
* All selected items in the list are moved up or down. If the selected
* items are contiguous, they all move as a block.
*/
function moveUpList(list) {
// Move all selected items in list up.
// Note: Option objects are not moved within the list. Attributes
// of the existing Option objects are changed. The display is
// smoother and current position in list is not lost.
if (! list || ! list.options || list.options.length == 0) return;
var saved = new Object;
var i,j,k;
for (i = list.options.length - 1; i >= 0; i--) {
if (list.options[i].selected) {
// Find next unselected item.
for (j = i-1; j >= 0; j--) {
if (!list.options[j].selected) break;
}
if (j >= 0) {
// Save current selection.
cpAttr(list.options[j],saved);
// Add the item above selection.
for (k = j; k < i; k++) {
cpAttr(list.options[k+1],list.options[k]);
}
cpAttr(saved,list.options[i]);
i = j;
}
}
}
}
function moveDownList(list) {
// Move all selected items in list down.
// Note: Option objects are not moved within the list. Attributes
// of the existing Option objects are changed.
if (! list || ! list.options || list.options.length == 0) return;
var saved = new Object;
var i,j,k;
for (i = 0; i < list.options.length; i++) {
if (list.options[i].selected) {
// Find next unselected item.
for (j = i+1; j < list.options.length; j++) {
if (! list.options[j].selected) break;
}
if (j < list.options.length) {
// Save current selection.
cpAttr(list.options[j],saved);
// Add the item above selection.
for (k = j; k > i; k--) {
cpAttr(list.options[k-1],list.options[k]);
}
cpAttr(saved,list.options[i]);
i = j;
}
}
}
}
function cpAttr(src,dest) {
dest.text = src.text;
dest.value = src.value;
dest.selected = src.selected;
dest.defaultSelected = src.defaultSelected;
}
/*
* selectList - Mark all items in a <select> list as "selected".
*
* Values in a <select> list are not sent to a server program unless
* they are selected. The selectList() function selects all the items
* in the specified <select> list.
*/
function selectList(sourceList) {
if (typeof sourceList == 'string') sourceList = document.getElementById(sourceList);
for(var i = 0; i < sourceList.options.length; i++) {
if (sourceList.options[i] != null) {
sourceList.options[i].selected = true;
}
}
return true;
}