// ----------------------------------------------------------------------------------------------------------------------------------------
// listbox functions
// ----------------------------------------------------------------------------------------------------------------------------------------

/*
function sortListObj(obj) {
	var lb=obj;
	arrTexts = new Array();

	for(i=0; i<lb.length; i++) { 
		arrTexts[i] = lb.options[i].text;
	}

	arrTexts.sort();

	for(i=0; i<lb.length; i++)  {
  		lb.options[i].text = arrTexts[i];
  		lb.options[i].value = arrTexts[i];
	}

	lb.selectedIndex=0;
}
*/

// ----------------------------------------------------------------------------------------------------------------------------------------

function sortListObj(obj) { 
	var lb = obj; 
	arrTexts = new Array(); 
	arrValues = new Array(); 
	arrOldTexts = new Array(); 

	for(i=0; i<lb.length; i++) { 
		arrTexts[i] = lb.options[i].text; 
		arrValues[i] = lb.options[i].value; 
		arrOldTexts[i] = lb.options[i].text; 
	} 

	arrTexts.sort(); 

	for(i=0; i<lb.length; i++) { 
		lb.options[i].text = arrTexts[i]; 
		for(j=0; j<lb.length; j++) { 
			if (arrTexts[i] == arrOldTexts[j]) { 
				lb.options[i].value = arrValues[j]; 
				j = lb.length; 
			} 
		} 
	} 
	
	lb.selectedIndex=0;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function sortList(element) {
	var obj=el(element);
	sortListObj(obj);
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function numOfOptionsInList(obj) {
	return obj.options.length;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function setOptionSelected(obj,value) {
	for(idx=0; idx<obj.options.length; idx++ ) {
		if ( obj.options[idx].value==value ) {
			obj.selectedIndex=idx;
			break;
		}
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function selectAll(CONTROL,value){
	for (var i = 0;i < CONTROL.length;i++) { CONTROL.options[i].selected = value; }
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function selectAllLBOptions(CONTROL) {
	selectAll(el(CONTROL),true);
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function removeAllOptions(obj) {
	var i;
	for (i=obj.length-1;i>=0;i--) { obj.remove(i); }
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function addOption(obj,text,value) {
	var objNew=document.createElement('option');
	objNew.text=text;
	objNew.value=value;

	try { obj.add(objNew,null); }
	catch(ex) { obj.add(objNew); }
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function compareOptionValues(a, b) {
	// Radix 10: for numeric values
	// Radix 36: for alphanumeric values

	var sA = parseInt( a.value, 36 );
	var sB = parseInt( b.value, 36 );

	return sA - sB;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function compareOptionText(a, b) {
	// Radix 10: for numeric values
	// Radix 36: for alphanumeric values

	var sA = parseInt( a.text, 36 );
	var sB = parseInt( b.text, 36 );

	return sA - sB;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function moveDualList( srcList, destList, moveAll ) {
	// Do nothing if nothing is selected
	if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )) { return; }

	newDestList = new Array( destList.options.length );

	var len = 0;

	for( len = 0; len < destList.options.length; len++ ) {
		if ( destList.options[ len ] != null ) {
		  newDestList[ len ] = new Option(
			destList.options[ len ].text,
			destList.options[ len ].value,
			destList.options[ len ].defaultSelected,
			destList.options[ len ].selected );
		}
	}

	for( var i = 0; i < srcList.options.length; i++ ) {
		if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) ) {
			// Statements to perform if option is selected
			// Incorporate into new list
			newDestList[ len ] = new Option(
				srcList.options[i].text,
				srcList.options[i].value,
				srcList.options[i].defaultSelected,
				srcList.options[i].selected );
			len++;
			}
	}

	// Sort out the new destination list
	newDestList.sort( compareOptionValues );   // BY VALUES
	// newDestList.sort( compareOptionText );   // BY TEXT

	// Populate the destination with the items from the new array
	for ( var j = 0; j < newDestList.length; j++ ) {
		if ( newDestList[ j ] != null ) { destList.options[ j ] = newDestList[ j ]; }
	}

	// Erase source list selected elements
	for( var i = srcList.options.length - 1; i >= 0; i-- ) {
		if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) ) {
		   // Erase Source
		   //srcList.options[i].value = "";
		   //srcList.options[i].text  = "";
		   srcList.options[i]       = null;
		}
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

/*
function addOption(theSel, theText, theValue) {
	var newOpt = new Option(theText, theValue);
	newOpt.selected=true;
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}
*/

// ----------------------------------------------------------------------------------------------------------------------------------------

function deleteOption(theSel, theIndex) {
	var selLength = theSel.length;
	if(selLength>0) { theSel.options[theIndex] = null; }
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function copyOptions(theSelFrom, theSelTo) {
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;

	var i;

	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--) {
		if(theSelFrom.options[i].selected) {
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			selectedCount++;
		}
	}

	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--) { addOption(theSelTo, selectedText[i], selectedValues[i]); }

	sortListObj(theSelTo);

	//if(NS4) history.go(0);
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function moveOptionPositionInList(zxcid,zxcud) {
	// zxcud: -1,1,top,bottom
	var zxcsel=el(zxcid);
	var zxcmove=zxcsel.selectedIndex;

	if (zxcmove<0) return;

	var zxcoptary=[];

	for (var zxc0=0;zxc0<zxcsel.options.length;zxc0++) zxcoptary.push(zxcsel.options[zxc0]);

	var zxcsave=(zxcud=='top')?0:(zxcud=='bottom')?zxcoptary.length-1:zxcmove+zxcud;

	if (zxcsel.options[zxcsave]) {
		for (var zxc1=0;zxc1<zxcoptary.length;zxc1++) { zxcsel.appendChild((zxc1==zxcmove)?zxcoptary[zxcsave]:(zxc1==zxcsave)?zxcoptary[zxcmove]:zxcoptary[zxc1]); }
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function zxcSize(zxcobj,zxcsz) {
	if (typeof(zxcobj)=='string') zxcobj=el(zxcobj);
	zxcobj.size=(zxcobj.size==zxcsz)?1:zxcsz;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function deleteOptions(theSelFrom) {
	var selLength = theSelFrom.length;
	for(i=selLength-1; i>=0; i--) {
		if(theSelFrom.options[i].selected) { deleteOption(theSelFrom, i); }
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function moveOptions(theSelFrom, theSelTo) {
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;

	var i;

	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--) {
		if(theSelFrom.options[i].selected) {
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}

	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--) {
		//alert('moveoptions\ntext='+selectedText[i]+'\nvalue='+selectedValues[i]);
		addOption(theSelTo, selectedText[i], selectedValues[i]); 
	}

	//sortListObj(theSelTo);

	//if(NS4) history.go(0);
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for (z=0; z<theForm.length;z++){
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') { theForm[z].checked = theElement.checked; }
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function checkUncheckSome(controller,theElements) {
	//Programmed by Shawn Olson
	//Copyright (c) 2006
	//Permission to use this function provided that it always includes this credit text
	//  http://www.shawnolson.net
	//Find more JavaScripts at http://www.shawnolson.net/topics/Javascript/

	//theElements is an array of objects designated as a comma separated list of their IDs
	//If an element in theElements is not a checkbox, then it is assumed
	//that the function is recursive for that object and will check/uncheck
	//all checkboxes contained in that element

	var formElements = theElements.split(',');
	var theController = el(controller);

	for (var z=0; z<formElements.length;z++) {
		theItem = el(formElements[z]);

		if (theItem) {
			if (theItem.type) {
				if (theItem.type == 'checkbox' && theItem.id != theController.id) { theItem.checked = theController.checked; }
				else {
					var nextArray = '';
					for(var x=0;x <theItem.childNodes.length;x++){
						if(theItem.childNodes[x]) {
							if (theItem.childNodes[x].id) {
								nextArray += theItem.childNodes[x].id+',';
							}
						}
					}

					checkUncheckSome(controller,nextArray);
				}

			}
		}
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------
