function add(variableCode) {

	var textarea = document.form['V_' + variableCode];
	var text = textarea.value;
	if (text != '' && text.charAt(text.length - 1) != '\n')
		text += '\n';
	var codes = text.split('\n');
	for (var n = 0; n < codes.length; n++) {
		var index = codes[n].indexOf(' ');
		if (index > -1)
		codes[n] = codes[n].substring(0, index);
	}

	var len = variableCode.length;
	if (variableCode.lastIndexOf('_AND') == len - 4) {
		variableCode = variableCode.substring(0, len - 4);
	}
	var finderOptions = document.form['codes-' + variableCode].options;
	var selections = '';
	var selected = false;
	for (var n = 0; n < finderOptions.length; n++) {
		if (finderOptions[n].selected) {
			selected = true;
			var option = finderOptions[n];
			var present = false;
			for (var m = 0; !present && m < codes.length; m++)
				if (codes[m] == option.value)
					present = true;
			if (!present)
				selections += option.value + ' ' + getLabel(option.text) + '\n';
//				selections += parseOptionText(option.value, option.text, false) + '\n';
//			option.selected = false;
		}
	}

	if (!selected) {
		alert("Please select item(s) to move.");
		return;
	}

	text += selections;
	textarea.value = text;
}

/*
	Old UI - Add codes from code list to selections list.
	For some reason if function was named add() it wasn't found - "unknown function"
	Reason - the button was named add and that conflicted with the function named add...
*/
function add1() {

	var code_options = document.form.codes.options;
	var sel_options = document.form.selections.options;
	var count = 0;
	for (var n = 0; n < code_options.length; n++) {
		if (code_options[n].selected) {
			count++;
			var option = code_options[n];
			if (!selected(option)) {
				var str = parseOptionText(option.value, option.text, true);
				var newoption = new Option(str, option.value);
				sel_options[sel_options.length] = newoption;
			}
			option.selected = false;
		}
	}

	if (count == 0) {
		alert("Please select item(s) to add.");
	}
	else {
		sortOptions(sel_options);
		//setSelectionsSize();
	}
}

function clear(str) {
	var index;
	for (index = 0; index < str.length; index++) {
		var ch = str.charAt(index);
		if (ch != '+' && ch != '-' && ch != '>' && ch != ' '  && ch != '\t' && ch != '\u00a0')
			break;
	}
	return str.substring(index);
}


function clearTextarea(variableCode) {
	var textarea = document.form['V_' + variableCode];
	textarea.value = '';
}

/*
	variable is the div holding the finder. Scroll to it and then back a bit.
*/
function finderFocus(variable) {

	var elem = document.getElementById(variable);
	if (elem != null) {
		elem.scrollIntoView();
		window.scrollBy(0, -80);
	}
}

function finderHighlights(select, tname) {

	var ta = document.getElementById(tname);
	if (ta == null)
		return;
	ta.value = '';
	var options = select.options;
	for (var n = 0; n < options.length; n++) {
		var option = options[n];
		if (option.selected) {
			ta.value += option.value + ' ' + getLabel(option.text) + '\n';
		}
	}
}

function getLabel(text) {
	var start = text.indexOf('(');
	var end = text.lastIndexOf(')');
	return text.substring(start, end + 1);
}

/*
	Adjust width of finder body and scroll to selected items when loading a page with a finder
*/
function loadFinder(variableCode, mode) {

	// display regular or advanced finder depending on mode
	if (mode == 'freg')
		toggleFinder(variableCode, 'none', 'freg', 'fadv');
	else if (mode == 'fadv')
		toggleFinder(variableCode, 'none', 'fadv', 'freg');

	// adjust width
	var codesSelect = document.getElementById("codes-" + variableCode);
	if (codesSelect != null)
		finderHighlights(codesSelect, variableCode + '-fhi');

	var codesDiv = document.getElementById("codes-div-" + variableCode);
	if (codesDiv == null || codesDiv.offsetWidth == 0)
		return;
	if (codesDiv.offsetWidth > codesSelect.offsetWidth)
		codesDiv.style.width = codesSelect.offsetWidth - 10;

	if (codesDiv.offsetWidth < 300) {
		var body = document.getElementById("finder-body-" + variableCode);
		body.style.width = 340;
	}

	// check for properties needed to scroll the codes window
	if (!codesDiv.scrollHeight || !codesDiv.offsetHeight)
		return;

	// get the index of the last selected item
	var options = codesSelect.options;
	var totOptions = options.length;
	var maxOption = 0;
	for (var n = 0; n < options.length; n++)
		if (options[n].selected)
			maxOption = n;
	// this lines things up better
	maxOption += 2;

	// total height in pixels of all the codes
	var totPx = codesDiv.scrollHeight;
	// size of the visible window used to scroll
	var winPx = codesDiv.offsetHeight;
	// don't know why the 18, obtained by observation
	var maxOffset = totPx - winPx + 18;
	var oneOptionPx = totPx / totOptions;
	// pixel count of last selected option = its index * # of pixels for one option
	var targetPx = maxOption * (totPx / totOptions);
	// pixels for top of visible window is target pixels minus half its height
	var offsetPx = targetPx - (winPx / 2);
	if (offsetPx < 0)
		offsetPx = 0;
	if (offsetPx > maxOffset)
		offsetPx = maxOffset;

	codesDiv.scrollTop = offsetPx;

}

function loadFinders(variableCodes, modes) {

	var codes = variableCodes.split(',');
	var modes = modes.split(',');
	for (var n = 0; n < codes.length; n++)
		loadFinder(codes[n], modes[n]);
}

function otherToggle(variable, relatedVariable, onSuffix, offSuffix) {

	var vcode1 = variable.substring(variable.indexOf('.') + 1);
	document.form['O_' + vcode1 + '_Fother'].value = variable + '-' + onSuffix;
	replaceClass(variable + '-other', offSuffix, onSuffix);
	toggleDisplay(variable + '-' + onSuffix, variable + '-' + offSuffix);

	if (relatedVariable != 'none') {
		var vcode2 = relatedVariable.substring(variable.indexOf('.') + 1);
		document.form['O_' + vcode2 + '_Fother'].value = relatedVariable + '-' + onSuffix;
		replaceClass(relatedVariable + '-other', offSuffix, onSuffix);
		toggleDisplay(relatedVariable + '-' + onSuffix, relatedVariable + '-' + offSuffix);
	}
}

/*
	Call text.match with a regular expression that matches all non-whitespace
	characters. Also call with /g so an array of matches is returned, each member
	of the array being a word. Then concatenate the words with a space between them.
	Use the code passed in rather than the code in the text because the text uses
	the display code which can include extra characters (* and dagger) and can also
	have the leading > for search results.

	We want the regular expression in the call to match() to find all non-whitespace
	characters. ^\s by itself works in Mozilla (\s is all whitespace characters), but
	IE doesn't include &nbsp; in the whitespace character list, so that has to be added
	to the regular expression. Decimal value for &nbsp; is 160, hex a0, so \u00a0 is
	included in the regular expression.
*/
function parseOptionText(code, text, truncate) {

	// words is the array of words
	// first element may be + or -, next is display code, then label
	var words = text.match(/[^\s\u00a0]+/g);
	var str = code;
	var start = 1;
	if (words[0] == '-' || words[0] == '+') {
		start = 2;
	}

	for (var n = start; n < words.length; n++)
		str += " " + words[n];
	
	if (truncate && str.length > 24) {
		str = str.substring(0, 20);
		str += "...";
	}
	return str;
}

/*
function parseOptionText(text) {

	var words = text.match(/[^\s\u00a0]+/g);
	var start = 1;
	var str = words[0];
	if (str == '-' || str == '+') {
		start = 2;
		str = words[1];
	}

//	if (str.charAt(0) == '\u2022')
	if (str.charAt(0) == '>')
		str = str.substring(1);
	if (str.charAt(0) == '*')
		str = str.substring(1);
	
	for (var n = start; n < words.length; n++)
		str += " " + words[n];
	
	if (str.length > 24) {
		str = str.substring(0, 20);
		str += "...";
	}
	return str;
}
*/

/*
	Old UI: Remove codes from selections list.
	For some reason if function was named remove() it wasn't found - "unknown function"
*/
function remove1() {
	var options = document.form.selections.options;
	var count = 0;
	for (var n = 0; n < options.length; n++) {
		if (options[n].selected) {
			count++;
			options[n--] = null;
		}
	}

	if (count == 0)
		alert("Please select item(s) to remove.");
	//else
		//setSelectionsSize();
}

function removeAll() {
	var options = document.form.selections.options;
	options.length = 0;
	//setSelectionsSize();
}

function selected(option) {
	var options = document.form.selections.options;
	for (var n = 0; n < options.length; n++)
		if (options[n].value == option.value)
			return true;
	return false;
}
/*
function setSelectionsSize() {

	var elem1 = document.getElementById("codes");
	var size = elem1.size;
	size = size < 6 ? 6 : size;
	size = size > 12 ? 12 : size;

	var elem = document.getElementById("selections");
	elem.size = size;
}
*/

function sortOptions(options) {
	var arr = new Array(options.length);
	for (var n = 0; n < options.length; n++) {
		var option = new Option(options[n].text, options[n].value);
		arr[n] = option;
	}
	arr.sort(sortOptionsCompare);

	for (var n = 0; n < arr.length; n++) {
		options[n].text = arr[n].text;
		options[n].value = arr[n].value;
	}
}

function sortOptionsCompare(a, b) {

	var a = Number(a.value);
	var b = Number(b.value);

	if (a < b)
		return -1;
	if (a > b)
		return 1;
	return 0;
}

function submitCheck() {
/*
	if (target_flag == "popup")
		target_flag = "self";
	else
		document.form.target = "_self";
*/
	if (document.finder_type == 'standalone')
		return;

	var options = document.form.selections.options;
	for (var n = 0; n < options.length; n++)
		options[n].selected = true;
}

function toggleFinder(variable, relatedVariable, onSuffix, offSuffix) {

	toggleFinder2(variable, onSuffix, offSuffix);
	if (relatedVariable != 'none')
		toggleFinder2(relatedVariable, onSuffix, offSuffix);
}

function toggleFinder2(variable, onSuffix, offSuffix) {

	var vcode1 = variable.substring(variable.indexOf('.') + 1);
	document.form['O_' + vcode1 + '_fmode'].value = onSuffix;
	toggleDisplay(variable + '-' + onSuffix + '-intro', variable + '-' + offSuffix + '-intro');

	// stuff to remove *All* from advanced list
	// only works in Firefox and Sigrid thinks *All* should stay anyway	
	//var select = document.getElementById("codes-" + variable);
	//var allOption = select.options[0];
		//allOption.className = 'displayoff';
		//finderHighlights(select, variable + '-fhi');
		//allOption.className = '';
		//finderHighlights(select, variable + '-fhi');

	if (onSuffix == 'fadv') {
		replaceClass(variable + '-fadv', 'displayoff', 'displayon');
		replaceClass('move-' + variable, 'displayoff', 'displayon');
		replaceClass('finder-' + variable, 'finder-back1', 'finder-back2');
		replaceClass('finder-tabs-' + variable, 'finder-back1', 'finder-back2');
		replaceClass('codes-' + variable, 'codes-color1', 'codes-color2');
		replaceClass('finder-buttons-' + variable, 'finder-buttons-color1', 'finder-buttons-color2');
		replaceClass('browse-msg-' + variable, 'normal', 'text-lite');
		replaceClass('search-msg-' + variable, 'normal', 'text-lite');
		replaceClass('fhi-' + variable, 'displayon', 'displayoff');
	}
	else {
		replaceClass(variable + '-fadv', 'displayon', 'displayoff');
		clearTextarea(variable);
		replaceClass('move-' + variable, 'displayon', 'displayoff');
		replaceClass('finder-' + variable, 'finder-back2', 'finder-back1');
		replaceClass('finder-tabs-' + variable, 'finder-back2', 'finder-back1');
		replaceClass('codes-' + variable, 'codes-color2', 'codes-color1');
		replaceClass('finder-buttons-' + variable, 'finder-buttons-color2', 'finder-buttons-color1');
		replaceClass('browse-msg-' + variable, 'text-lite', 'normal');
		replaceClass('search-msg-' + variable, 'text-lite', 'normal');
		replaceClass('fhi-' + variable, 'displayoff', 'displayon');
	}
}
