// 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 (isset(action)) {
			document.list.action = action;
		}
		document.list.submit();
	}


// return a value up to the delimiter, or the original value if the delimiter isn't there
// - this is used most often when parsing an argstring to set button states

	function crop(value, delimiter) {
		result = (value.indexOf(delimiter) != -1) ? value.substring(0, value.indexOf(delimiter)) : value ;
		return result;
	}
	

// 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();
	}


// open an event edit window with a consistent size

	function popup_event(ID, date, date_end) {
		if (ID == 0 && date) {
			// pre-set the current list if we're viewing a list
			list = (document.URL.indexOf("list.php") != -1) ? document.filters.filter_list.options[document.filters.filter_list.selectedIndex].value : -1 ;
			list_argument = "&date=" + date;
			if (date_end){
				list_argument = list_argument + "&date_end_set=" + date_end;
			}
		} else {
			list_argument = "";
		}
		
		width = 445;
		height = 495;
		if (navigator.platform.indexOf("Win") != -1) { height = height + 75; } // open a taller window on Windows
		if ((navigator.platform.indexOf("Win") != -1)&&(navigator.appName.indexOf("Netscape") != -1)) { height = height + 25; } // open an even taller window in Firefox on Windows
		if ((navigator.platform.indexOf("Mac") != -1)&&(navigator.appName.indexOf("Netscape") != -1)) { height = height + 100; } // open an even taller window in Firefox on Macintosh
		popup("../pages/event_edit.php?ID=" + ID + list_argument, width, height);
	}


// open a Betaboard note window

	function popup_note(ID, readwrite) {
		readwrite = parseFloat(readwrite); // convert to an integer, since readwrite = "0" evaluates to true
		betaboard_domain = location.protocol + "//" + location.host.replace(/chronolog/, "betaboard");
		note_page = (readwrite) ? "/board/note_readwrite.php" : "/board/note_readonly.php" ;
		note_URL = betaboard_domain + note_page + "?ID=" + ID;
		popup(note_URL, 450, 500);
	}
	
	
// open an Itemize item window

	function popup_item(ID) {
		itemize_domain = location.protocol + "//" + location.host.replace(/chronolog/, "itemize");
		width = 335;
		height = ((navigator.platform.indexOf("Win") != -1)||(navigator.appVersion.indexOf("Safari") != -1)) ? 440 : 400 ; // Windows and Safari space out the lines more, so open a taller window
		item_URL = itemize_domain + "/pages/item_edit.php?ID=" + ID;
		popup(item_URL, width, height);
	}


// navigate to a view page based on a selection from the date shortcut menus

	function go_date_shortcut(view) {
		year = document.date_shortcut.date_shortcut_year.options[document.date_shortcut.date_shortcut_year.selectedIndex].value;
		if (view == "year") {
			month = date.substr(5, 2);
		} else {
			month = document.date_shortcut.date_shortcut_month.options[document.date_shortcut.date_shortcut_month.selectedIndex].value;
		}
		if ((view == "year")||(view == "month")) {
			// show the same day as the current month when navigating to a new month
			day = date.substr(8, 2);
		} else {
			day = document.date_shortcut.date_shortcut_day.options[document.date_shortcut.date_shortcut_day.selectedIndex].value;
		}
		new_date = year + "-" + month + "-" + day;
		location = "view_" + view + ".php?date=" + new_date;
	}


// 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 the index of an array element, something that ought to be built into JavaScript but isn't!
// capitalize the first letter of a string, and make the others lowercase

	function ucfirst(str) {
		str = str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
		return str;
	}
	

// capitalize the first letter of every word in a string, and make the others lowercase
// this also collapses multiple spaces into single spaces, for better or for worse

	function ucwords(str) {
		result = new Array();
		str = str.split(/\s/);
		count = str.length;
		for (n=0; n < count; n++) {
			result[n] = ucfirst(str[n]);
		}
		result = result.join(" ");
		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 + "." + 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 "";
	}


// 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 time menus to a new SQL-standard time
// leave time blank to select the current time

	function select_time(form_and_menu, new_time, increment) {
		if (new_time != "-1") {
			if (new_time == "") {
				time = new Date();
				
				hours = time.getHours();
				minutes = time.getMinutes();
				seconds = time.getSeconds();

			} else {
				time = new_time;
				
				hours = time.substring(0, time.indexOf(":"));
				minutes = time.substring(time.indexOf(":") + 1, time.lastIndexOf(":"));
				seconds = time.substring(time.lastIndexOf(":") + 1);
				
			}
			
			if (hours >= 12) {
				ampm_index = 2;
				if (hours > 12) {
					hours = hours - 12;
				}
			} else {
				ampm_index = 1;
				if (hours == 0) {
					hours = 12;
				}
			}
			
			if (increment) {
				// this assumes that the increment is for minutes only and not for seconds
				minutes = Math.floor(minutes / increment);
			}
			
			eval("document." + form_and_menu + "_hours.selectedIndex = hours");
			eval("document." + form_and_menu + "_minutes.selectedIndex = minutes + 1");
			eval("document." + form_and_menu + "_seconds.selectedIndex = seconds + 1");
			eval("document." + form_and_menu + "_ampm.selectedIndex = " + ampm_index);
		}
	}


// get the individual values selected for a set of date menus and join them into an SQL date string
// if any of the individual menus are unset, we'll return an empty value

	function get_date(form_name, field_name) {
		eval("year = document." + form_name + "." + field_name + "_year.options[document." + form_name + "." + field_name + "_year.selectedIndex].value;");
		eval("month = document." + form_name + "." + field_name + "_month.options[document." + form_name + "." + field_name + "_month.selectedIndex].value;");
		eval("day = document." + form_name + "." + field_name + "_day.options[document." + form_name + "." + field_name + "_day.selectedIndex].value;");

		if ((year != "-1")&&(month != "-1")&&(day != "-1")) {
			date = year + "-" + month + "-" + day;
		} else {
			date = ""; // our classes let us use "" for convenience and convert it to 0000-00-00 when saving to the database
		}
		
		return date;
	}


// get the individual values selected for a set of time menus and join them into an SQL time string
// if any of the individual menus are unset, we'll return an empty value

	function get_time(form_name, field_name) {
		eval("hours = document." + form_name + "." + field_name + "_hours.options[document." + form_name + "." + field_name + "_hours.selectedIndex].value;");
		eval("minutes = document." + form_name + "." + field_name + "_minutes.options[document." + form_name + "." + field_name + "_minutes.selectedIndex].value;");
		eval("seconds = (document." + form_name + "." + field_name + "_seconds.options) ? document." + form_name + "." + field_name + "_seconds.options[document." + form_name + "." + field_name + "_seconds.selectedIndex].value : '00' ;");
		eval("ampm = document." + form_name + "." + field_name + "_ampm.options[document." + form_name + "." + field_name + "_ampm.selectedIndex].value;");
	
		if ((hours != "-1")&&(minutes != "-1")&&(seconds != "-1")&&(ampm != "-1")) {
			hours = parseInt(hours, 10); // thanks http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604
			if ((ampm == "am")&&(hours == 12)) { hours = 0; }
			if ((ampm == "pm")&&(hours < 12)) { hours = parseInt(hours) + 12; }
			if (hours < 10) { hours = "0" + hours; }
			time = hours + ":" + minutes + ":" + seconds;
		} else {
			date = ""; // our classes let us use "" for convenience and convert it to 00:00:00 when saving to the database
		}
		
		return time;
	}
	
	
// 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();
	}

	
// sort a list without losing the filter values

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


// sort a report without losing the filter values

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

