// support checkbox shortcuts on lists

	function check_list_items(value) {
		for (i=0; i<document.list.elements.length; i++) {
			if (document.list.elements[i].name == "items[]") {
				document.list.elements[i].checked = value;
			}
		}
	}

	function perform_list_action(action) {
		if (action) {
			document.list.action = action;
		}
		document.list.submit();
	}


// round any floating decimal to a two-digit number and avoid type errors
	
	function twoDigits(n) { return two_digits(n); }

	function two_digits(n) {
		if (n === "") {
			return "";
		} else {
			if (isNaN(n)) {
				return "0.00";
			} else {
				return String(Math.round((parseFloat(n) * 1000) + 1) / 1000).slice(0, -1);
			}
		}
	}


// open a popup window, specifying source, width, and height, and optionally a name; if no name, choose a random one

	function popup(source, width, height, window_name) {
		if (! window_name) { 
			now = new Date();
			window_name = now.getTime();
		} else {
			window_name = window_name.replace(/ /g, "_");
		}
		popup_window = window.open(source, window_name, "width=" + String(width) + ",height=" + String(height) + ",location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes");
		popup_window.focus();
	}


// generate a random string of the specified length

	function random_string(strlen) {
		// chars contains no vowels to avoid accidentally generating a bad word
		chars = "123456789bcdfghjklmnpqrstvwxyz";
		str = "";
		for (var i=0; i<strlen; i++) {
			random = Math.floor(Math.random() * chars.length);
			str += chars.substr(random, 1);
		}
		return str;
	}


// the simplest possible rollover function

	function swap(name, state) {
		eval('document.images.' + name + '.src = ' + name + '_' + String(state) + '.src');
	}
	

// write a block of code that preloads a list of graphics
// - this version makes on and off states and is most often used for button rollovers

// names - a comma-delimited list of button names (e.g. "home,about,contact")
// path - the path to the graphics; defaults to "../graphics" if not set (e.g. "../graphics/menus/home")
// extension - the file extension of the graphics files; defaults to "gif" if not set (e.g. "jpg")

	function preload_buttons(names, path, extension) {
		names = names.split(",");
		path = (path) ? path : "../graphics" ;
		extension = (extension) ? extension : "gif" ;
		for (n=0; n < names.length; n++) {
			this_name = names[n];
			this_path = path + "/" + this_name;
			eval(this_name + "_0 = new Image()");
			eval(this_name + "_0.src = '" + this_path + "_0." + extension + "'");
			eval(this_name + "_1 = new Image()");
			eval(this_name + "_1.src = '" + this_path + "_1." + extension + "'");
		}
	}
	

// figure out whether a variable has been set or not without generating an undefined error if it hasn't

	function isset(variable) {
		eval("result = (typeof(" + variable + ") != 'undefined')");
		return result;
	}
	

// returns -1 if not present

	function get_position(string, array) {
		for (n=0; n < array.length; n++) {
			if (array[n] == string) {
				return n;
				break;
			}
		}
		return -1;
	}


// sets a menu to a given text or value

	function set_menu(form_name, field_name, text_or_value, key) {
		eval("these_options = document." + form_name + ".elements['" + field_name + "'].options");
		for (n=0; n < these_options.length; n++) {
			eval("this_text_or_value = these_options[n]." + text_or_value);
			if (this_text_or_value == key) {
				eval("document." + form_name + "." + field_name + ".selectedIndex = " + n);
				break;
			}
		}
		return "";
	}


// sets a radio button to a given value (we can't set the text of radio buttons)

	function set_radio(form_name, field_name, value) {
		eval("these_elements = document." + form_name + ".elements['" + field_name + "']");
		for (n=0; n < these_elements.length; n++) {
			eval("this_value = these_elements[n].value");
			if (this_value == value) {
				these_elements[n].checked = true;
				break;
			}
		}
		return "";
	}


// sets a checkbox group to a given value (we can't set the text of checkboxes)

	function set_checkbox(form_name, field_name, value, checked) {
		eval("these_elements = document." + form_name + ".elements['" + field_name + "']");
		if (these_elements.length > 0) {
			for (n=0; n < these_elements.length; n++) {
				eval("this_value = these_elements[n].value");
				if (this_value == value) {
					these_elements[n].checked = checked;
					break;
				}
			}
		} else {
			eval("this_value = these_elements.value");
			if (this_value == value) {
				these_elements.checked = checked;
			}
		}
		return "";
	}


// set date menus to a new SQL-standard date
// leave date blank to select today's date
// set to -1 to clear the menu

	function select_date(form_and_menu, new_date) {
		if (new_date != "-1") {
			if (new_date == "") {
				date = new Date();
				
				day = date.getDate();
				month = date.getMonth() + 1;
				year = date.getFullYear();
				
			} else {
				date = new_date;
				
				year = date.substring(0, date.indexOf("-"));
				month = date.substring(date.indexOf("-") + 1, date.lastIndexOf("-"));
				day = date.substring(date.lastIndexOf("-") + 1);
				
			}
					
			eval("document." + form_and_menu + "_month.selectedIndex = month");
			eval("document." + form_and_menu + "_day.selectedIndex = day");
			eval("document." + form_and_menu + "_year.selectedIndex = year - document." + form_and_menu + "_year.options[1].value + 1");
		} else {
			eval("document." + form_and_menu + "_month.selectedIndex = 0");
			eval("document." + form_and_menu + "_day.selectedIndex = 0");
			eval("document." + form_and_menu + "_year.selectedIndex = 0");
		}
	}


// set a cookie
	
	function set_cookie(name, value) {
		// specify a default key-name prefix for this project
		name_prefix = "";
		
		// the following are cookie parameters are optional ... change values to null if you don't need them
		hours = 43800;
		path = "/";						
		domain = ".mgtsuite.com";
		
		key = name_prefix + name;		
		(hours) ? expiration_date = new Date((new Date()).getTime() + hours*3600000).toGMTString() : expiration_date = false;
		cookie_string = key + '=' + escape(value) + ((expiration_date)?(';expires=' +expiration_date):'') + ((path)?(';path='+path):'') + ((domain)?(';domain='+domain):'');
		document.cookie = cookie_string;
	}
	

// get values from a cookie

	function get_cookie(name) {
		var output_value;
		if (document.cookie != "") {
			// specify a default key-name prefix for this project
			name_prefix = "";
			cookie_array = document.cookie.split("; ");
			for (i = 0; i < cookie_array.length; i++) {
				cookie_pair = cookie_array[i].split("=");
				key = cookie_pair[0];
				value = cookie_pair[1];
						
				if (key == (name_prefix + name)) {
					output_value = unescape(value);
					break;
				} else {
					output_value = "";
				}
			}
		} else {
			output_value = "";
		}
		return output_value;
	}
	
	
// sort a table by a single column

	function sort(column) {
		document.sort.sort.value = column;
		document.sort.submit();
	}
	

