/*  Script Language="JavaScript 1.1"

	FlexiList v2.3 - Written by Fini A. Alring <fini.alring@bytefarmers.com> - Copyright 1998 - 2005

	Purpose:
		To manipulate <select [multiselect="multiselect"> fields in a nice manner.

	Features:
		Copy / Move items from one list to another.
		Select / De-Select / Invert all items in a list.
		Move selected items up / down in a list.
		Delete an item from a list.

	History:
		v0.1 - 17.Dec.1998 - First version! Although not complete yet! (Look at To Do list (Deprecated..! ;-) he he)
		v2.0 - 14.Apr.1999 - Using a whole other approach than v0.1, it's cleaner now!  ;-) only pure object collaboration ;)
		v2.1 - 03.Jan.2000 - My first codework in the new Millenium! Happy New Year to ya'l!
		                            Added support for multiSelect via moveMultiSelectedListItem().
									Note: You can use the old moveSelectedListItem() instead if you don't need multiSelect support.
									Added selectAllInList() & invertSelectionInList() - use them for manipulating selections in a multiSelect list.
									Added moveItemInList()
		v2.2 - 05.Sep.2003 - Added Copy and Add functionality...
		v2.3 - 02.Mar.2005 - Minor cleanup, email changed, prepare for public launch ;) enjoy your journey..


		Known Bugs:
		MS IE 5.00.2314.1003 : Causes Browser to crash. - Solution: don't use that browser, especially that particular version.
		
		License:
			Use it as you please as long as you keep this file intact and unmodified.
			
		Updates:
			Get latest version somewhere near http://www.bytefarmers.com/log/
*/

function addListItem(toList, inpText, inpValue) {
// Add an item in a list (<Select>).

	if( toList ) {

		toList.length++;
		listPos = toList.length - 1;
		toList.options[listPos].text = inpText;
		toList.options[listPos].value = inpValue;
	}
}


function deleteListItem(listObj,item) {
// Deletes an Item in a list (<Select>). - 1.1
	listLength = listObj.length;

	if( listLength > 0 ) { // Only delete if there is at least 1 item in the list.

		for(var i = item; i < listLength-1; i++) {

			listObj.options[i].selected = listObj.options[i+1].selected
			listObj.options[i].text = listObj.options[i+1].text;
			listObj.options[i].value = listObj.options[i+1].value;
		}

		listObj.length--;
	}

	return true;
}


function deleteMultiListItem(fromList) {
// Moves the .text and .value attribute of an item in a list, to another list (<Select>).

	 if( (fromList.options.length > 0) && (fromList.selectedIndex > -1) ) { // Only move if there is at least 1 item in fromList.

		fL = fromList.options.length; // make a static var, to contain the old size of the list, even if we delete items.

		for(i = fL-1; i >= 0; i--) { // ...then we delete the items from fromList...

			if(fromList.options[i].selected) { // is this item selected?

				fromList.options[i].selected = false; // let's deselect the item.
				deleteListItem(fromList, i); // item has been moved, now we delete the old item in the fromList.
			}
		}
	}
}


function moveSelectedListItem(fromList, toList, selIndex) {
// Moves the .text and .value attribute of an item in a list, to another list (<Select>).

	if((!fromList) || (!toList) || (!selIndex)) {
	
		return false;
	}

	if(selIndex != null) {

		fromList.selectedIndex = selIndex;
	}

	if( (fromList.length > 0) && (fromList.selectedIndex > -1) ) { // Only move if there is at least 1 item in fromList.

		 toList.length++;
		listPos = toList.length - 1;

		toList.options[listPos].text = fromList.options[fromList.selectedIndex].text;
		fromList.options[fromList.selectedIndex].text ='';

		toList.options[listPos].value = fromList.options[fromList.selectedIndex].value;
		fromList.options[fromList.selectedIndex].value ='';

		deleteListItem(fromList,fromList.selectedIndex);
	}
}


function moveMultiSelectedListItem(fromList, toList) {
// Moves the .text and .value attribute of an item in a list, to another list (<Select>).

	 if( (fromList.options.length > 0) && (fromList.selectedIndex > -1) ) { // Only move if there is at least 1 item in fromList.

		fL = fromList.options.length; // make a static var, to contain the old size of the list, even if we delete items.

		for(var i = 0; i < fL; i++) { // first we copy the items to toList...

			if(fromList.options[i].selected) {

				toList.options.length++; // increase list by one item.
				listPos =  toList.options.length - 1;

				toList.options[listPos].text = fromList.options[i].text; // Copy the item caption
				toList.options[listPos].value = fromList.options[i].value; // Copy the item value
			}
		}

		for(i = fL-1; i >= 0; i--) { // ...then we delete the items from fromList...

			if(fromList.options[i].selected) { // is this item selected?

				fromList.options[i].selected = false; // let's deselect the item.
				deleteListItem(fromList, i); // item has been moved, now we delete the old item in the fromList.
			}
		}
	}
}


function copyMultiSelectedListItem(fromList, toList) {
// Copies the .text and .value attribute of an item in a list, to another list (<Select>), but only if it doesn't exist at toList.

	 if( (fromList.options.length > 0) && (fromList.selectedIndex > -1) ) { // Only move if there is at least 1 item in fromList.

		fL = fromList.options.length; // make a static var, to contain the old size of the list, even if we delete items.

		for(var i = 0; i < fL; i++) { // first we copy the items to toList...

			if( (fromList.options[i].selected) && (findValueInListItem(toList, fromList.options[i].value) == -1) ) {

				toList.options.length++; // increase list by one item.
				listPos =  toList.options.length - 1;

				toList.options[listPos].text = fromList.options[i].text; // Copy the item caption
				toList.options[listPos].value = fromList.options[i].value; // Copy the item value
			}
		}
	}
}


function moveItemInList(listObj, dir) {
// Move the selected items in a <SELECT [MULTIPLE]> either up or down, set dir to 0 for down and 1 for up.

	var tempText, tempValue, tempSel;

	if( listObj.length > 0 ) { // Only if there is at least 1 item in listObj.

		if(dir == 0) { // move down

			for(i = listObj.length-2; i >= 0; i--) {

				if(listObj.options[i].selected) {

						tempText = listObj.options[i].text; // Copy the item caption
					tempValue = listObj.options[i].value; // Copy the item value

					listObj.options[i].text = listObj.options[i+1].text; // Copy the item caption
					listObj.options[i].value = listObj.options[i+1].value; // Copy the item value

					tempSel = listObj.options[i+1].selected; // is dest selected already?

					listObj.options[i+1].text = tempText; // Copy the item caption
					listObj.options[i+1].value = tempValue; // Copy the item value

					listObj.options[i+1].selected = true;
					listObj.options[i].selected = tempSel;

				}
			}
		}


		if(dir == 1) { // move up

			for(i = 1; i <= listObj.length-1; i++) {

				if(listObj.options[i].selected) {

					tempText = listObj.options[i].text; // Copy the item caption
					tempValue = listObj.options[i].value; // Copy the item value

					listObj.options[i].text = listObj.options[i-1].text; // Copy the item caption
					listObj.options[i].value = listObj.options[i-1].value; // Copy the item value

					tempSel = listObj.options[i-1].selected; // is dest selected already?

					listObj.options[i-1].text = tempText; // Copy the item caption
					listObj.options[i-1].value = tempValue; // Copy the item value

					listObj.options[i-1].selected = true;
					listObj.options[i].selected = tempSel;

				}
			}
		}
	}
}


function selectAllInList(listObj, deSelect) {
// Selects all items in a <SELECT MULTIPLE>, if you set deSelect to true, it will de-select all items instead.

	if( listObj.length > 0 ) { // Only if there is at least 1 item in listObj.

		for(i = 0; i < listObj.length; i++) {

			listObj.options[i].selected = !deSelect;
		}
	}
}


function invertSelectionInList(listObj) {
// Invert-Selects all items in a <SELECT MULTIPLE>.

	if( listObj.length > 0 ) { // Only if there is at least 1 item in listObj.

		for(i = 0; i < listObj.length; i++) {

			listObj.options[i].selected = !listObj.options[i].selected;
		}
	}
}


function findValueInListItem(listObj, searchValue) {
// v1.1 - changed to return -1 instead of false.

	if(listObj.length > 0) {

		for(var i = 0; i < listObj.length; i++) {

			if(listObj.options[i].value == searchValue)	{
				return i
			}
		}

	}

	return -1;
}


function transferListValuesToCSV(listObj) {
// Take all item-values of a list (<Select>), and join them in a CSV string (Comma Delimitted String).

	var CSV = '';

	if( listObj.length > 0 ) { // Only move if there is at least 1 item in listObj.

		for(i = 0; i < listObj.length; i++) {

			CSV += listObj.options[i].value + ',';
		}
	}

	return CSV;
}

