//
// 2-panel system, for user to specify data columns they want in their CSV download.
//
function allselect() {
	document.getElementById('addbutton').disabled = false;
	document.getElementById('removebutton').disabled = true;
	document.getElementById('moveupbutton').disabled = true;
	document.getElementById('movedownbutton').disabled = true;
}
function myselect() {
	document.getElementById('addbutton').disabled = true;
	document.getElementById('removebutton').disabled = false;
	document.getElementById('moveupbutton').disabled = false;
	document.getElementById('movedownbutton').disabled = false;
}
function focusallchoice() {
//alert("focusallchoice:"+document.getElementById('allchoice').selectedIndex);
	if (document.getElementById('allchoice').selectedIndex>=0)
		document.getElementById('addbutton').disabled = false;
	else
		document.getElementById('addbutton').disabled = true;
	document.getElementById('removebutton').disabled = true;
	document.getElementById('moveupbutton').disabled = true;
	document.getElementById('movedownbutton').disabled = true;
}
function focusmychoice() {
//alert("focusmychoice:"+document.getElementById('mychoice').selectedIndex);
	document.getElementById('addbutton').disabled = true;
	if (document.getElementById('mychoice').selectedIndex>=0) {
		document.getElementById('removebutton').disabled = false;
		document.getElementById('moveupbutton').disabled = false;
		document.getElementById('movedownbutton').disabled = false;
	} else {
		document.getElementById('removebutton').disabled = true;
		document.getElementById('moveupbutton').disabled = true;
		document.getElementById('movedownbutton').disabled = true;
	}
}
// Move all selected items in mychoice up 1 unit.
// If selection has gaps, the lower items are pulled up to be with the upper items.
// The going-up position is determined by: uppermost item, going up 1 level.
// If uppermost selected item is at the top, we do nothing.
function moveup() {
	// remember position of uppermost item. if first item is selected, can't move any higher than that.
	// also copy all items that are selected, for insertion later.
	var pos = -1;	// index position of top selected item
	var myitems = new Array();
	var numselected = 0;
	for (var i=0; i<document.getElementById('mychoice').length; i++) {
		if (document.getElementById('mychoice').options[i].selected) {
			if (i == 0)  return;		// can't move higher
			if (pos < 0)  pos = i;
			var name = document.getElementById('mychoice').options[i].text;
			var val = document.getElementById('mychoice').options[i].value;
			var opt = new Option(name,val);
			myitems.push(opt);
			numselected++;
		}
	}
	// if nothing was selected at all, we're done here.
	if (pos < 0)  return;
	// calculate item insertion position (up 1 spot)
	pos--;
	if (pos < 0)  pos = 0;	// should never happen
//alert("pos="+pos);
	// remove selected items from mychoice.
	removeitem();
	// Now pull all following items from the list, since we can only append to end of list.
	for (var i=pos; i<document.getElementById('mychoice').length; i++) {
		var name = document.getElementById('mychoice').options[i].text;
		var val = document.getElementById('mychoice').options[i].value;
		var opt = new Option(name,val);
		myitems.push(opt);
	}
	var totl = document.getElementById('mychoice').length;
	for (var i=pos; i<totl; i++) {
		document.getElementById('mychoice').remove(pos);
	}
//alert("numselected="+numselected);
	// add all this back to the list, it's in the proper order.
	for (var i=0; i<myitems.length; i++) {
		document.getElementById('mychoice').add(myitems[i],undefined);
		if (numselected-- > 0)
			document.getElementById('mychoice').options[pos].selected = true;
		pos++;
	}
	focusmychoice();
}
// Move all selected items in mychoice down 1 unit.
// If selection has gaps, the upper items are pushed down to be with the lower items.
// The going-down position is determined by: lowermost item, going down 1 level.
// If lowermost selected item is at the bottom, we do nothing.
function movedown() {
	// remember position of lowermost item. if last item is selected, can't move any lower than that.
	// also copy all items that are selected, for insertion later.

	var pos = -1;	// index position of top selected item
	var myitems = new Array();
	var numselected = 0;
	for (var i=0; i<document.getElementById('mychoice').length; i++) {
		if (document.getElementById('mychoice').options[i].selected) {
			pos = i;
			var name = document.getElementById('mychoice').options[i].text;
			var val = document.getElementById('mychoice').options[i].value;
			var opt = new Option(name,val);
			myitems.push(opt);
			numselected++;
		}
	}
	// if nothing was selected at all, we're done here.
	if (pos < 0)  return;
	// if last selected element is last element, we have nothing to do.
	if (pos == document.getElementById('mychoice').length-1)  return;
	// calculate item insertion position (up 1 spot)
	pos++;
	if (pos > document.getElementById('mychoice').length-1)  pos = document.getElementById('mychoice').length-1;	// should never happen
//alert("pos="+pos);
	// Now pull all following items from the list, since we can only append to end of list.
	for (var i=pos+1; i<document.getElementById('mychoice').length; i++) {
		var name = document.getElementById('mychoice').options[i].text;
		var val = document.getElementById('mychoice').options[i].value;
		var opt = new Option(name,val);
		myitems.push(opt);
	}
	// pull the items beyond the selected ones, which are to stay at the end of the list.
	var totl = document.getElementById('mychoice').length;
	for (var i=pos+1; i<totl; i++) {
		document.getElementById('mychoice').remove(pos+1);
	}
	// remove selected items from mychoice.
	removeitem();
//alert("numselected="+numselected+", pos="+pos);
	// add all this back to the list, it's in the proper order.
	for (var i=0; i<myitems.length; i++) {
		document.getElementById('mychoice').add(myitems[i],undefined);
		if (numselected-- > 0)
			document.getElementById('mychoice').options[document.getElementById('mychoice').length-1].selected = true;
	}
	focusmychoice();
}
// Look thru all selected items in allchoice, any we don't have in mychoice yet
// are copied over (appended to end of list).
function additem() {
	for (var i=0; i < document.getElementById('allchoice').options.length; i++) {
		if (document.getElementById('allchoice').options[i].selected) {
			var name = document.getElementById('allchoice').options[i].text;
			var val = document.getElementById('allchoice').options[i].value;
			if (!selectHasValue(document.getElementById('mychoice'),val)) {
				var opt = new Option(name, val);
				var before = undefined;		// ie6 cannot use this field
				document.getElementById("mychoice").add(opt,before);
			}
		}
	}
}
// Remove any items from mychoice that are selected.
function removeitem() {
	// have to do 'em in reverse order, because indexes are renumbered after each remove!
	for (var i=document.getElementById('mychoice').options.length-1; i>=0; i--) {
		if (document.getElementById('mychoice').options[i].selected)
			document.getElementById('mychoice').remove(i);
	}
	// readjust buttons, probably nothing's selected now
	focusmychoice();
}
// check if a select box actually contains an option with a specific value or not
function selectHasValue(sel,val) {
	for (var k=0; k < sel.options.length; k++)
		if (sel.options[k].value == val)  return true;
	return false;
}
// Returns true if this is an IE browser, false if not.
// To help deal with IE's double-click bug (double-clicks only call ondblclick, not onclick, in IE).
function ie() {
	return (navigator.appName == "Microsoft Internet Explorer");
}
// Select everything that's in the csv columns box, so it all gets submitted with the form
function panel_presubmit() {
	for (var i=0; i < document.getElementById('mychoice').options.length; i++)
		document.getElementById('mychoice').options[i].selected = true;
	return true;
}
