// If you change the value of max selections, make sure you make the
// categories list one more than this.
var max_selections = 10;


// This function determines the number of elements in the right hand list
function get_r_num()
{
	var r_num = 0;
	var right = document.keywordForm.categories;

	for (var i = 0; i < right.options.length; i++) {
		if (right.options[i].value != "") {
			r_num++;
		}
	}

	return r_num;
}

// Copy the items chosen on the left to the list on the right
function sel_it(mode)
{
	var r_num, addit, limit_reached = false;
	var left  = document.keywordForm.critwrds;
	var right = document.keywordForm.categories;

	// Go through the left list and find what has been selected
	for (var i = 0; i < left.options.length - 1; i++) {
		if (left.options[i].selected && left.options[i].value != "") {
			// Deselect the left hand value. We have to deselect all the left hand values on return in case the user
			// has chosen heaps, cos if they have it can clog up the system and cause payload to be lost!
			left.options[i].selected = false;

			if (limit_reached == false) {
				// Check if this value is already in the right hand list
				add_it = true;
				for (var j = 0; j < right.options.length; j++) {
					if (right.options[j].value == left.options[i].value) {
						alert('You have already selected the entry "' + left.options[i].text + '"');
						add_it = false;
					}
				}

				// Check that we are not adding more than 'max_selections'
				r_num = get_r_num();
				if (r_num >= max_selections) {
					limit_reached = true;
					if (mode == 'normal') {
						alert('Sorry, the maxiumum number of selections allowed is ' + max_selections + '!');
						// return true;
					}
				}

				if (add_it == true && limit_reached == false) {
					// Add it to the right hand list
					while (r_num >= right.options.length) {
						var x = right.options.length
						right.options[x] = new Option();
					}
					right.options[r_num].value	= left.options[i].value;
					right.options[r_num].text	= left.options[i].text;
				}
			}
		}
	}
}


// Remove the selected options from the right hand side
function unsel_it()
{
	var right = document.keywordForm.categories;

	for (var i = 0; i < right.options.length; i++) {
		if (right.options[i].selected == true) {
			right.options[i] = null;
		}
	}
}


// This function is called on submit. It selects all the values in the
// right hand box so that they are passed to the next page.
function select_lists()
{
	var left  = document.keywordForm.critwrds;
	var right = document.keywordForm.categories;

	// Find out how many elements there are in the right hand box
	var r_num = get_r_num();

	// If none are selected, then don't let the user out
	if (r_num == 0) {
		alert('Please select at least one keyword before executing the search');
		return false;
	}

	// Go through the right list and set them all to selected
	for (var i = 0; i < r_num; i++) {
		if (right.options[i].value != "") {
			// Make it selected
			right.options[i].selected = true;

			// Make it selected in the left hand list as well, so we know what was selected if we return
			for (j = 0; j < left.options.length; j++) {
				if (left.options[j].value == right.options[i].value) {
					left.options[j].selected = true;
				}
			}
		}
	}
	return true;
}


// End script hiding -->
