// set variables that all functions can access

	component = ((document.location.href.indexOf("/betaboard.mgtsuite.com/") != -1)||(document.location.href.indexOf("/betaboard-dev.mgtsuite.com/") != -1)) ? "board" : "browser" ;
	

// open the note window

	function note(ID, readwrite) {
		readwrite = parseFloat(readwrite); // convert to an integer, since readwrite = "0" evaluates to true
		if (ID > 0) {
		
			// open an existing note
		
			note_path = "../board/";
			note_page = (readwrite) ? "note_readwrite.php" : "note_readonly.php" ;
			note_URL = note_path + note_page + "?ID=" + ID + "&component=" + component;
			window.open(note_URL, "note_" + ID, "width=450,height=500,location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes");

		} else {
		
			if (component == "board") {
			
				// if from the board, find out what project they want a new note for ...
				
				note_URL = "../board/note_blind.php";
				window.open(note_URL, "", "width=450,height=500,location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes");
			
			} else {
			
				// ... otherwise, open a new note for the project they're browsing now
				
				betaboard_header.document.new_note_info.page.value = betaboard_main.location.href;

				if (betaboard_main.frames[0]) {  // include frame names if there are any
					frame_names = "";
					frame_URLs = "";
					for (var i = 0; i < betaboard_main.frames.length; i++) {
						frame_names += betaboard_main.frames[i].name + ";";
						frame_URLs += betaboard_main.frames[i].location.href + ";";
					}
					frame_names = frame_names.substr(0, (frame_names.length - 1)); // remove the last delimiter
					frame_URLs = frame_URLs.substr(0, (frame_URLs.length - 1)); // remove the last delimiter
					betaboard_header.document.new_note_info.frame_names.value = frame_names;
					betaboard_header.document.new_note_info.frame_URLs.value = frame_URLs;
				}

				window.open("http://betaboard.mgtsuite.com/shared/blank.html", "note", "width=450,height=500,location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes");
				betaboard_header.document.new_note_info.submit();
			
			}
			
		}
	}
	
	
// refresh the main frame (this is only used by the browser component)
	
	function refresh() {
		betaboard_main.location = betaboard_main.location;
	}
	
	
// browse a project if selected in the bar menu
	
	function browse(root) {
		if (root.substr(root.length - 1) != "/") { root += "/"; }
		browser_URL = root + "betaboard/index.html";
		location = browser_URL;
	}
	
	
// open the board component

	function board(project) {
		board_URL = "http://betaboard.mgtsuite.com/board/board.php";
		if (project) { board_URL += "?project=" + project };
		location = board_URL;
	}
	
	
// open the scratch pad in a popup window

	function scratch_pad() {
		scratch_pad_URL = "http://betaboard.mgtsuite.com/board/scratch_pad.php";
		window.open(scratch_pad_URL, "scratch_pad", "width=250,height=180,location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes");
	}
	
	
// open a window to help find previously logged notes on a page

	function check_previous() {
		betaboard_header.document.check_previous_info.page.value = betaboard_main.location.href;
		window.open("http://betaboard.mgtsuite.com/shared/blank.html", "previous", "width=500,height=250,location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes");
		betaboard_header.document.check_previous_info.submit();
	}
	
	
// log out by returning to the login page

	function logout() {
		if (component == "browser") {
			browser_directory = document.URL.substr(0, document.URL.lastIndexOf("betaboard/") + 10);
			location = "http://betaboard.mgtsuite.com/shared/logout.php?redirect=" + browser_directory;
		} else {
			location = "/shared/logout.php";
		}
	}
	
	
// before submitting a note, do a little verification
// NOTE: we need to rework the hard-coded status numbers here in case someone adds more statuses to their account

	function prep_submission() {
		if (! validate("note", validate_fields)) {
			// you must complete the key fields listed in each note file
			return false;
		} else if (((document.note.status)&&(document.note.assigned))&&((document.note.status.options[document.note.status.selectedIndex].value != status_new)&&(document.note.status.options[document.note.status.selectedIndex].value != status_completed))&&(document.note.assigned.options[0].selected)) {
			// you must assign the note unless it's New or Completed
			alert("Please assign this note to someone before submitting it.");
			return false;
		} else {
			if (((document.note.status)&&(document.note.status.options[document.note.status.selectedIndex].value == status_new))&&((document.note.assigned)&&(document.note.assigned.options[0].selected == 0))) {
				// notes assigned but left at New automatically change to In Progress
				set_menu("note", "status", "value", status_inprogress)
			}
			//if (document.note.status.options[document.note.status.selectedIndex].value == status_completed) {
				// notes set to Completed automatically change to no assigned value
				// we're doing this in the submit script now, in case we want to send an email notification when we complete the note
				//document.note.assigned.selectedIndex = 0;
			//}
			return true;
		}
	}
	
	
// save the scratch pad data by loading the submit form into a small popup window
	
	function scratch_pad_save_popup() {
		window.open("../shared/blank.html", "scratch_pad_save", "width=220,height=80,location=no,menubar=no,directories=no,toolbar=no,scrollbars=auto,resizable=yes,status=no");
	}


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


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


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


// simple 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!
// 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 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;
	}
	
	
// jumble up some text for safer transfer in places where cookies or PHP encryption can't go
	
	function pseudo_crypt(input, direction, key) { 
		output = "";
		//if (direction) { input = (input.split(" ")).join("+"); } // before encrypting, remove spaces so it's URL safe
		palette = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789%=& ";
		key = "betaboardrocksmyworld"; // can only contain members of palette
		key_position = 0;
		for (n=0; n < input.length; n++) {
			input_char = palette.indexOf(input.charAt(n));
			if (input_char != -1) {
				key_char = key.charAt(key_position);
				offset = palette.indexOf(key_char);
				if (direction == 1) {
					offset = ((input_char + offset) > (palette.length - 1)) ? offset - palette.length : offset ;
					output += palette.charAt(input_char + offset);
				} else {
					offset = ((input_char - offset) < 0) ? offset - palette.length : offset ;
					output += palette.charAt(input_char - offset);
				}
				key_position++;
				key_position = (key_position > key.length) ? 0 : key_position ;
			} else {
				output += input.charAt(n);
			}
		}
		//if (! direction) { output = (output.split("+")).join(" "); } // after decrypting, restore spaces
		return output;
	}
			

// sort a table by a single column

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

