// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++  Move one list contents to another list                       ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function switchListOpts(oListFrom, oListTo, bSelectedOnly , inVal ) {
	var nIndexUp , nLastIdx , bMoveIt , tmpOps , flag ;

//	with ( oListFrom ) {
		for (nIndexUp = 0; nIndexUp < oListFrom.length; nIndexUp++) {
			tmpOps = oListFrom.options[nIndexUp];
			//if( inVal != null ) {
			if( inVal ) {
				bMoveIt = ( inVal == tmpOps.value );
			}
			else {
				bMoveIt = ((bSelectedOnly) ?  tmpOps.selected : true);
			}

			if (bMoveIt) {
				nLastIdx = nIndexUp;
//				with (options[nIndexUp]) {
					flag    = true;
					for( nidx=0; nidx < oListTo.length; nidx++ ) {
						if( oListTo.options[nidx].value == tmpOps.value ) {
							flag = false;
							break;
						}
					}
					if( flag ) {
						oListTo.options[oListTo.length] = new Option( tmpOps.text, tmpOps.value );
					}
//				} 
				oListFrom.options[nIndexUp] = null;
				nIndexUp--;
			} 
		}
		//if( inVal == null ) {
		if( inVal ) {
			if (oListFrom.options[nLastIdx] != null) {
				oListFrom.options[nLastIdx].selected = true;
			}
			else if( length > 0 && oListFrom.options[length-1] != null) {
				oListFrom.options[length-1].selected = true;
			}
		}
//	}
}

function moveListOpts(oList, nCmd) {
	var nIndexUp, nCountUp, nInc , nTotalLen , oBaseNade;

	switch(nCmd) {
		case 1:
		case 2:
//			with (oList) {
				nInc	 = ((1 == nCmd) ? -1 : 1);
				// Note : Begin from index 1
				for (nCountUp = 1; nCountUp < oList.options.length; nCountUp++) {
					nIndexUp = ((1 == nCmd) ? nCountUp : (oList.options.length - nCountUp -1));
					if(oList.options[nIndexUp].selected) {
						if( !oList.options[nIndexUp + nInc].selected) {
							//options[nIndexUp + nInc].swapNode(options[nIndexUp]);
							oBaseNade = ((nInc > 0) ? oList.options[nIndexUp] : oList.options[nIndexUp].nextSibling);
							insertBefore(removeChild(oList.options[nIndexUp + nInc]), oBaseNade);
						}
					} 
				}
//			} 
			break;
		case 3:
//			with (oList) {
				// Note : Begin from index 0
				nTotalLen = oList.children.length;
				nCountUp = 0;
				nIndexUp = nTotalLen;
				while (nCountUp < nTotalLen) {
					nCountUp++;
					nIndexUp--;
					if(oList.children[nIndexUp].selected) {
						insertBefore(removeChild(oList.children[nIndexUp]), oList.children[0]);
						nIndexUp++;
					}
				}
//			} 
			break;
		 case 4:
//			with (oList) {
				// Note : Begin from index 0
				nTotalLen = oList.children.length;
				nCountUp = 0;
				nIndexUp = -1;
				while (nCountUp < nTotalLen) {
					nCountUp++;
					nIndexUp++;
					if(oList.children[nIndexUp].selected) {
						appendChild(removeChild(oList.children[nIndexUp]));
						nIndexUp--;
					}
				}
//			} 
			break;
		default:
			break;
	 }
}

function makeCSV( listTo,inText,splt ) {
	var csvList ;

	if( inText == null ) { return; }

	csvList = "";
	if( listTo.length > 0 ) {
		csvList = listTo.options[0].value;

		// Note : Begin from index 1
		for( nidx=1; nidx < listTo.length; nidx++ ) {
			csvList += splt + listTo.options[nidx].value ;
		}
	}
	inText.value = csvList;
}


function initOpts(oListFrom, oListTo, inVals, splt ) {
	var vals , i ;

	if( inVals != null ) {
		vals = inVals.split( splt );
		for ( i=0; i < vals.length; i++) {
			switchListOpts(oListFrom, oListTo, true , vals[i] );
		}
	}
}
