var warned_change = false;
var warned_delete = false;
var response = false;

function show_hide(val) {
	var block = window.document.getElementById("category_values");
	if (val == "single_select" || val == "multi_select") {
		block.style.display = "block";
	} else {
		block.style.display = "none";
	}
}

function add_values() {
	var valueSelect = window.document.frm.current_values;
	var valueText 	= window.document.frm.new_value;
	
	if (valueText.value == '') { 
		alert('Please type the value to add.');
		return false;
	}
	for (i = 0; i < valueSelect.options.length; i++) {
		if (valueText.value.toLowerCase() == 
			valueSelect.options[i].text.toLowerCase()) {
			alert('A value with the same name already exists, please choose another value.');
			return false;
		}
	}
	// create new option
	valueSelect.options[valueSelect.options.length] = 
		new Option(valueText.value, '0', false, false);
	// reset text box
	valueText.value = '';
}

function change_values() {
	var valueSelect = window.document.frm.current_values;
	var valueText 	= window.document.frm.new_value;

	if (valueSelect.options.selectedIndex < 0) {
		alert('Please select a value in the list to be changed.');
		return false;
	}
	
	if (valueText.value == '') { 
		alert('Please provide the new value in the text box.');
		return false;
	}
	response = true;
	if (!warned_change &&													// not yet warned
		valueSelect.options[valueSelect.options.selectedIndex].value > 0) { // this item exists in DB
		response = confirm(message_change);
		warned_change = true;
	}
	if (response) {
		// ok, we should have both new and old value available
		valueSelect.options[valueSelect.options.selectedIndex].text = valueText.value;
		valueText.value = '';
	}

}

function remove_values() {
	var valueSelect = window.document.frm.current_values;
	var valueText 	= window.document.frm.new_value;

	if (valueSelect.options.selectedIndex < 0) {
		alert('Please select a value in the list to be removed.');
		return false;
	}
	
	response = true;
	
	if (!warned_delete && 													// not yet warned
		valueSelect.options[valueSelect.options.selectedIndex].value > 0) { // this item exists in DB
		response = confirm(message_remove);
		warned_delete = true;
	}
	if (response) {
		valueSelect.options[valueSelect.options.selectedIndex] = null;
	}
}

function fill_box() {
	var valueSelect = window.document.frm.current_values;
	var valueText 	= window.document.frm.new_value;
	
	if (valueSelect.selectedIndex >= 0) {
		valueText.value = valueSelect.options[valueSelect.selectedIndex].text;
	}		
}

function frm_submit() {
	
	if (!frm_submit_err()) return false;
	
	var valueSelect 	= window.document.frm.current_values;
	if (valueSelect == null) { 
		return true;
	}
	var valueIdArray 	= window.document.frm.value_id;
	var valueNameArray 	= window.document.frm.value_name;
	var ids 	= "";
	var names 	= "";
	var sep	= '\n';
	for (var i = 0; i < valueSelect.options.length; i++) {
		if (i > 0) { ids += sep; names += sep; }
		ids		+= valueSelect.options[i].value;
		names 	+= valueSelect.options[i].text;
	}
	valueIdArray.value 		= ids;
	valueNameArray.value 	= names;
	return true;
}

function sortChangeValues(action) {
	var valueSelect = window.document.frm.current_values;
	var valueText 	= window.document.frm.new_value;
	if (valueSelect.options.selectedIndex < 0) {
		alert('Please select a value in the list to move ' + action + '.');
		return false;
	}
	var curIndex = valueSelect.options.selectedIndex;
	var shift;
	if 		(action == 'up') 	{ shift = -1; }
	else if (action == 'down') 	{ shift = +1; }
	if (valueSelect.options.selectedIndex + shift > valueSelect.options.length - 1 ||
		valueSelect.options.selectedIndex + shift < 0) {
		// do nothing
		return false;
	}
	// swap
	swapOptions(
		valueSelect.options[valueSelect.options.selectedIndex],
		valueSelect.options[valueSelect.options.selectedIndex + shift]
		);
	// reset selected item
	valueSelect.options.selectedIndex += shift;
	return false;
}

function swapOptions (opt1, opt2) {
	var temp_value = opt1.value;
	var temp_text  = opt1.text;
	opt1.value = opt2.value;
	opt1.text  = opt2.text;
	opt2.value = temp_value;
	opt2.text  = temp_text;
}

