/***********************************************************************************************************
	
	Function: scSwapOptions()
	
	Params:
		src 	(mixed) id OR reference to a select tag set as "multiple"
		target 	(mixed) id OR reference to a select tag set as "multiple"
	
	Purpose:
		Move selected options from "src" select tag to "target" select tag
		
***********************************************************************************************************/
function scSwapOptions(src,target) {
	// if ID strings are passed, grab elements
	if (typeof(src) == 'string') 	src 	= document.getElementById(src);
	if (typeof(target) == 'string') target 	= document.getElementById(target);
	
	// while we have something selected, loop through each
	// selected option in src select tag
	while (src.selectedIndex != -1) {
		// add a new option to end of target select option, mirroring the selected option from src select tag
		target.options[target.length] = new Option(
			src.options[src.selectedIndex].text,
			src.options[src.selectedIndex].value
		);
		
		// if we are on a Navigation_Block/Theme relationship,
		// nav blocks need to go to TOP of target list (under "Primary Navigation"),
		// instead of at the bottom...this does that
		if (target.id == 'Navigation_Block_rn' || target.id == 'Links_rn') {
			// select new option
			target.options[target.length-1].selected = true;	
			// move to top of list
			moveOpt(target.id,'top');
			// deselect
			target.options[2].selected = false;
		}
		
		// wipe out/remove the selected option from the src select tag
		src.options[src.selectedIndex] = null;
	}
}
	




// ------------------------------------------------------------------------
// Start of code to scroll options up and down
// ------------------------------------------------------------------------
//
// Start of timer code....to scroll options up or down, onmousedown
//
// variable for timer
var timerID = null;
var t;
var selId, moveHow;

function startScrollOpt(a,b) {
    // place initial code after this line
    t = 0;
    selId = a; moveHow = b
    // and before this line
    // this is the time before the looping event will trigger
    timerID = window.setTimeout('scrollOpt()',250);
}

function scrollOpt() {
    // place your main code after this line
    t++;
	moveOpt(selId,moveHow);
    // and before this line
    // this is the time between each function call to the main function
    timerID = window.setTimeout('scrollOpt()',200);
}

function stopScrollOpt() {
    // place temination code after this line
    // and before this line
    window.clearTimeout(timerID);
}
// --------------------------------------------------------------------
// End of timer code....to scroll options up or down, onmousedown
// --------------------------------------------------------------------

function moveOpt(selId,moveHow) {
	// purpose of function is to move 1 or more options
	// up or down within a select input
	//
	// selId 	the id of the select input
	// moveHow	the direction....'up','down','top','bottom'

	var optsArr = Array();
	var indexes = Array();
	var selectedOpts = Array();
	var msg = "";
	var move;
	
	// grab ref to select input
	selId = document.getElementById(selId);

	// grab a copy of the options
	selOpts = new cloneObject(selId.options);
	//selOpts = cloneObject(selId.options);

	
	// need to handle this differently for IE, because
	// the method that is being used for NN is slow on IE
	// and vice versa
	if (navigator.appName == "Microsoft Internet Explorer") {
		// loop through options, and create an array representing them
		for(i=0; i<selOpts.length; i++) {
			selOpt = selOpts[i]; // current option
			// optsArr is used later, to put the options back together
			//optsArr[i] = {text: selOpt.text, value: selOpt.value, selected: selOpt.selected};
			// indexes is actually passed to moveArrayPos()...it containts the indexes of the options
			//indexes[i] = i;
			
			if (selOpt.selected) {
				// figure out how many to move....
				move = 		moveHow == "up" 	? i-1 :
							moveHow == "down" 	? i+1	 :
							moveHow == "top"	? 0 :
							moveHow == "bottom" ? selOpts.length -1:
							null;
				
				// if proposed new index is less than 0, just set it to 0
				if (move < 0) move = 0;

				// Navigation_Blocks to Themes relationships are special....
				// there are dummy options that need to be accounted for.
				// At the top is one used as a label that reads "Primary Navigation"
				// this code below ensures that nothing can move above that position
				// 
				// this accounts for any code that needs to do something similar in the future,
				// by just checking to see if the top option is disabled, and not allowing
				// anything to go above it if it is by altering "move" before it is passed to moveArrayPos()
				//
				// loop is here so we can skip up to 2 disabled options used as fillers
				var a = 0;
				while(a++ < 2) {
					// if moving to the top position, and option already there is disabled, move to spot below it
					if (selId.options[0].disabled && move == 0) {
						// if 2nd option from top is disabled (because it is used as a spacer),
						if (selId.options[1] && selId.options[1].disabled) {
							// move right below that 2nd option
							move = 2;
						} else { 
							// else move below first option
							move = 1;
						}
					// if moving to a spot where a disabled option is
					} else if (selId.options[move] && selId.options[move].disabled) {

						if (moveHow == 'up') {
							move = move - 1;
						} else if (moveHow == 'down') {
							move = move + 1;
						}
					}
				}
				
				// remove currently select option from select input
				selId.remove(i);
				// add it back
				selId.add(selOpt,move);
			}
		}		
	
	} else {
		//dumpElement(selOpts);
		
		// loop through options, and create an array representing them
		for(i=0; i<selOpts.length; i++) {
			selOpt = selOpts[i]; // current option
			// optsArr is used later, to put the options back together
			optsArr[i] = {text: selOpt.text, value: selOpt.value, selected: selOpt.selected, className: selOpt.className, disabled: selOpt.disabled};
			// indexes is actually passed to moveArrayPos()...it containts the indexes of the options
			indexes[i] = i;
			if (selOpt.selected) selectedOpts[selectedOpts.length] = i;
		}
		
		// move indexes...once for each selected option
		for(i=0;i < selectedOpts.length;i++){
			
			// figure out how many to move....
			move = 		moveHow == "up" 	? -1 :
						moveHow == "down" 	? 1	 :
						moveHow == "top"	? -selectedOpts[i] :
						moveHow == "bottom" ? selOpts.length :
						null;
						
			// Navigation_Blocks to Themes relationships are special....
			// there are dummy options that need to be accounted for.
			// At the top is one used as a label that reads "Primary Navigation"
			// this code below ensures that nothing can move above that position
			// 
			// this accounts for any code that needs to do something similar in the future,
			// by just checking to see if the top option is disabled, and not allowing
			// anything to go above it if it is by altering "move" before it is passed to moveArrayPos()
			a = 0;
			while (a++ < 2) {
				// if the item we are moving above/below is disabled AND it's not the top option
				if (
						selId.options[selectedOpts[i] + move] 
					&& 	selId.options[selectedOpts[i] + move].disabled 
					&& 	((selectedOpts[i] + move) != 0) 
				) {
					move = moveHow == 'up' ? move -1 : move + 1;
				}			
			}
			
			// if top option is diabled and we are moving above it, bump it down 1
			if (selId.options[0].disabled && (selectedOpts[i] + move) <= 0) move++
			// if 2nd from top option is diabled and we are moving above it, bump it down 1
			if (selId.options[1].disabled && (selectedOpts[i] + move) == 1) move++

			indexes = moveArrayPos(indexes,selectedOpts[i],move);
		}

		// loop through options, and put them back!
		for(i=0;i < indexes.length;i++) {
			index = indexes[i];
			opt = optsArr[index];
			selId.options[i] = new Option(opt.text, opt.value, false, opt.selected);
			
			if (opt.className) 	selId.options[i].className = opt.className;
			if (opt.disabled) 	selId.options[i].disabled = opt.disabled;
			
			if (opt.selected) lastSelectedIndex = i;
		}
	}
}




// purpose of function is to select all the options 
// in the target select field on record forms
// this is done just before submission, so the selected items can be written to correpsonding
// relationship table
function relTargetSelect() {
	// grab all select input objects
	
	selects = document.getElementsByTagName("select");
	if (selects) {
		
		// loop through each select
		for(i=0;selects[i];i++) {
			sel = selects[i];
			if (sel.className == "rel_target") {
				// loop through all options
				for ( x=0; x < sel.length; x++ ) {
 					// some might be disabled, for nav block/theme relationships...enable them
 					sel[x].disabled = false;
 					// select them
 					sel[x].selected = true;
				}
			}
		}
	
	
	}
}
