// function controls moving select options
// from one field to another, and sorting
function move(fbox, tbox) {
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}
	var fLength = 0;
	var tLength = arrTbox.length;
	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		} else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
   		}
	}

	//arrFbox.sort();
	//arrTbox.sort();
	
	fbox.length = 0;
	tbox.length = 0;
	var c;
	for(c = 0; c < arrFbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}
	for(c = 0; c < arrTbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
   	}
}	
	




// ------------------------------------------------------------------------
// 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;
				// 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};
			// 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;

			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.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 rel_target_select() {
	// 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 of this select field, and select them
				for ( x=0; x < sel.length; x++ ) sel[x].selected = true;

			}
		}
	
	
	}
}