// make some navigation functions so that we can change page URLs without editing our Flash files

	function go_yourgift() {
		if (location.href.indexOf("giftcard_display") != -1) {
			location = "../assembled/yourgift_intro.html";
		} else {
			alert("We've disabled this button on this page, but it will work in the final gift card.");
		}
	}

	function go_giftcard() {
		if (location.href.indexOf("giftcard_display") != -1) {
			location = "../assembled/giftcard_intro.html";
		} else {
			alert("We've disabled this button on this page, but it will work in the final gift card.");
		}
	}


// remove the blank value indicator when clicking a field

	function remove_blank_value(blank_value, form_name, field_name) {
		eval("this_field = " + form_name + "." + field_name);
		if (this_field.value == blank_value) {
			this_field.value = "";
		}
	}


// redirect if we're not using a secure connection (in case the user forgot to specify https as the protocol)
// this can also be used to force a particular subdomain, in this case secure

	function require_SSL() {
		if (document.URL.indexOf("https://secure.") == -1) { 
			document.write("<p class=subheading>Redirecting to a secure port...</p>");
			location = "https://secure." + document.URL.substr(document.URL.indexOf("typea.net"), document.URL.length);
		}
	}


// redirect if we no longer need a secure connection

	function avoid_SSL() {
		if (document.URL.indexOf("https://") != -1) { 
			document.write("<p class=subheading>Redirecting to a standard port...</p>")
			// use the same subdomain we're on right now; just change the protocol
			location = "http://www." + document.URL.substr(document.URL.indexOf("typea.net"), document.URL.length);
		}
	}


// load a different stylesheet for Mac or Windows
// in your content file, just use a hard link to the platform that the files will most often be edited on
// - you can set it to styles_mac.css during development, then change to styles_win.css at delivery
// - since this runs on every page load, we've commented it out here; comment it back in if you need it

	//platform = (navigator.platform.indexOf("Win") == -1) ? "mac" : "win" ;
	//document.write("<link rel=stylesheet href=../content/styles_" + platform + ".css type=text/css>");


// construct Flash object and embed tags
// FlashVars should be a string of name/value pairs separated by ampersands

	function get_flash_tags(filename, width, height, bgcolor, flashvars) {
		name = filename;
		if (name.indexOf("/")) { name = name.substring(name.lastIndexOf("/") + 1); }
		if (name.indexOf(".")) { name = name.substring(0, name.indexOf(".")); }
		
		output = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
		output += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"';
		output += ' WIDTH="' + String(width) + '" HEIGHT="' + String(height) + '" id="' + name + '" name="' + name + '" ALIGN="">';
		output += '<PARAM NAME=movie VALUE="' + filename + '"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=' + bgcolor + '> <EMBED src="' + filename + '" quality=high bgcolor=' + bgcolor + ' WIDTH="' + String(width) + '" HEIGHT="' + String(height) + '" NAME="' + name + '" ALIGN=""';
		output += ' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"';
		output += ' FlashVars="' + flashvars + '"></EMBED>';
		output += '<PARAM NAME="FlashVars" VALUE="' + flashvars + '">';
		output += '</OBJECT>';
		
		// output the parameters, for testing, until we have an actual flash file
		/*
		output = "file: " + filename + "<br>";
		output += "width: " + width + "<br>";
		output += "height: " + height + "<br>";
		output += "bgcolor: " + bgcolor + "<br>";
		output += "text: " + flashvars.replace("statements=", "");
		*/
		return output;
	}


// set a cookies
	
	function set_cookie(name, value) {
		// specify a default key-name prefix for this project
		name_prefix = "projectname_";
		
		// the following are cookie parameters are optional ... change values to null if you don't need them
		hours = 43800;
		path = "/";						
		domain = null;
		
		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 = "projectname_";
			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 = value;
					break;
				} else {
					output_value = "";
				}
			}
		} else {
			output_value = "";
		}
		return output_value;
	}
	
	
// grab all name/value pairs from the URL and create JavaScript variables from them

	function get_vars() {
	
		if (location.href.indexOf('?') != -1) {
		
			query_string = location.href.substring(location.href.indexOf('?') + 1, location.href.length);
			query_pairs = query_string.split("&");
			
			for (n=0; n < query_pairs.length; n++) {
				pair = query_pairs[n].split("=");
				key = pair[0];
				value = pair[1];
				eval(key + " = '" + value + "'");
			}
			
			if (query_string.indexOf("args=") != -1) {
				args_pieces = args.split(",");
				for (n=0; n < args_pieces.length; n++) {
					j = n + 1;
					eval("arg" + j + " = '" + args_pieces[n] + "'");
				}
			}
		
		} else {
			query_string = ""; query_pairs = ""; template = ""; args = "";
		}
			
	}
	

// gets the name value of a dynamic page, even if we're rewriting URLs

	function get_page_name() {
		if (location.href.indexOf("page=") != -1) {
			page_name = location.href.substring(location.href.indexOf('page=') + 5, location.href.length);
		} else {
			page_name = location.href.substring(location.href.lastIndexOf('/') + 1, location.href.indexOf('.htm'));
		}
		return page_name;
	}
	
	
// 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()
	}


// write a random element from the specified array

	function write_random(array_name) {
		which = (Math.round(Math.random() * (arrayName.length - 1)));
		document.write(array_name[which]);
	}


// the simplest possible rollover function

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

// rollovers with sticky highlights
// - used in navigation frames where the buttons persist but other pages are changing

	var previous = null;
	var current = null;

	function sticky_swap(name, state, hold) {
		if (hold == 1) {
			// set previously lit button to normal
			previous = current;
			if (previous != null) {
				eval('document.images.' + previous + '.src = ' + previous + '_0.src');
			}
		}
		if ((name != null) && (name != current)) {
			// set new button state
			eval('document.images.' + name + '.src = ' + name + '_' + String(state) + '.src');
			if (hold == 1) { current = name; }
		}
	}
	

// rollovers with separate but linked button and label graphics
// - highlights both the button and label if you mouse over either one 
// - requires graphics to be named like "button_0.gif" and "button_label_0.gif"

	function label_swap(name, state) {
		eval('document.images.' + name + '.src = ' + name + '_' + String(state) + '.src');
		
		// change the label if one exists
		if (eval('document.images.' + name + '_label')) {
			eval('document.images.' + name + '_label.src = ' + name + '_label_' + String(state) + '.src');
		}
		
		// change the button if this is a label
		if (name.indexOf("_label") != -1) {
			name = name.substr(0, (name.length - 6));
			if (eval('document.images.' + name)) {
				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 + "'");
		}
	}
	

// write a block of code that preloads a list of graphics
// - this version just makes an on state and is most often used for tips associated with 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_tips(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 + " = new Image()");
			eval(this_name + ".src = '" + this_path + "." + extension + "'");
		}
	}
	

// write a block of code that preloads a list of graphics
// - this version uses the same source file for each instance and is most often used for markers associated with button rollovers

// names - a comma-delimited list of instance names (e.g. "home_marker,about_marker,contact_marker")
// marker_name - the base name of the marker graphic
// 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_markers(names, marker_name, 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 + "/" + marker_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 + "'");
		}
	}
	

// leave this here to catch calls to the original preload script

	function make_preloads(names, path) {
		preload_buttons(names, path);
	}
	

// 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 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.getYear();
				
				// work around a Safari bug
				if ((year < 1900)&&(navigator.appVersion.indexOf("Safari") != -1)) { year = year + 1900; }

			} 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) {
		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;
			}

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


// set a base text size for each platform; can be used in conjunction with static font tags for face and color
// remember to close this tag with a static </font> later in your document	

	function text_size(mac, win) {
		if (navigator.appVersion.indexOf("Mac") != -1) {
			document.write("<font face=geneva,arial size=" + mac + ">");
		} else {
			document.write("<font face=geneva,arial size=" + win + ">");
		}
	}


// jumble up some text for safer transfer in places where cookies or PHP encryption can't go

	function pseudo_crypt(input, direction, key) { 
		output = "";
		input = (input.split(" ")).join("+"); // remove spaces so it's URL safe
		palette = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+&=:/"; // any other characters will be untouched
		key = "everygoodboydeservesfudge"; // 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);
			}
		}
		output = (output.split("+")).join(" ");
		return output;
	}
	
	
// pass arguments into a frameset document to automatically set any of the frame locations
// place  onload=setFrames()  into the frameset tag, then include  framename=location.html  in your URL

	function set_frames() {

		if (location.href.indexOf('?') != -1) {

			query_string = location.href.substring(location.href.indexOf('?') + 1, location.href.length);
			query_pairs = query_string.split("&");
			
			for (n=0; n < query_pairs.length; n++) {
				
				pair = query_pairs[n].split("=");
				key = pair[0];
				value = pair[1];
				
				for (j=0; j < frames.length; j++) {
					if (key == frames[j].name) {
						eval(key + ".location = '" + value + "'");
						break;
					}
				}
				
			}

		}

	}


// this is handy for preventing the Return key from submitting forms (set form action to "JavaScript:nothing()")
	
	function nothing() {}
		
