
/*
	This function can be added to the onChange of any select box
	It makes the select box extensible - if the user chooses  the
	last option (no matter what it is) then the user is prompted
	to enter the new value. This is then added to
	the drop-down and selected

	To use it do
	<SELECT onChange="check_extend_select(this)" />
	or
	$optionbox->set_extra('onChange="check_extend_select(this)"');
*/

function check_extend_select (select,prompt,value,check_trigger) {
	if (prompt==undefined) prompt='Please enter the desired value';
	if (value==undefined) value='';
	if (
		(check_trigger==undefined && select.selectedIndex==select.options.length-1) ||
		(check_trigger!=undefined && select.options[select.selectedIndex].value==check_trigger)
	) {
		new_value = window.prompt(prompt,value,prompt);
		// check to see if they actually provided a value
		if (new_value == null || new_value=='') {
			select.selectedIndex = 0;
		} else {
			if (check_trigger==undefined) {
				// add the new option in next-to-last on the list
				select.options[select.options.length] = new Option( select.options[select.options.length-1].text, select.options[select.options.length-1].value);
				select.options[select.options.length-2] = new Option(new_value,new_value);
				select.selectedIndex = select.options.length-2;
			} else {
				// add the new option in at the very end
				select.options[select.options.length] = new Option(new_value,new_value);
				select.selectedIndex = select.options.length-1;
			}
		}
	}
}


