<?php

	$onload = "";
	
	// we want this to appear before anything else
	$onload = "build_page_browser(\"".addslashes($onload)."\");";
	
	// for contemplate, preload all the templates, content files, fields and forms into JavaScript arrays
	if (CONTENT_TYPE == "contemplate") {
	
		$templates = get_contemplate_templates();
		$templates_javascript = "templates = new Array('".implode("', '", $templates)."');\n";
		
		$content_files = get_content_files();
		$content_files_javascript = "content_files = new Array('".implode("', '", $content_files)."');\n";
		
		get_contemplate_database();
		$database_tables = get_contemplate_database_tables();
		$database_tables_javascript = "database_tables = new Array('".implode("', '", $database_tables)."');\n";
		
		$file_fields_javascript = "";
		foreach ($content_files as $file) {
			$file_javascript_name = javascript_name($file);
			if (strpos($file, "/") === false) { $file = "content/".$file; }
			$file_text = get_contemplate_file($file);
			$file_fields = get_contemplate_field_names($file_text, $file);
			if (count($file_fields)) {
				$file_fields_javascript .= $file_javascript_name."_fields = new Array('".implode("', '", $file_fields)."'); // $file\n\t\t\t";
			}
			
			$field_forms_javascript = "";
			foreach ($file_fields as $field) {
				$field_text = get_contemplate_field($file_text, $field, $file);
				$field_forms = get_contemplate_form_names($field_text);
				if (count($field_forms)) {
					$field_javascript_name = javascript_name($field);
					$field_forms_javascript .= $file_javascript_name."_".$field_javascript_name."_forms = new Array('".implode("', '", $field_forms)."'); // $field\n\t\t\t";
				}
			}
		}
		
		$table_columns_javascript = "";
		foreach ($database_tables as $table) {
			$table_javascript_name = javascript_name($table);
			$table_columns = array();
			$query = "SHOW COLUMNS FROM $table";
			$result = db_do_query($query);
			$count = db_num_rows($result);
			for ($i=0; $i<$count; $i++) {
				$row = db_get_row($result, $i);
				$table_columns[] = $row["Field"];
			}
			if (count($table_columns)) {
				$table_columns_javascript .= $table_javascript_name."_columns = new Array('".implode("', '", $table_columns)."'); // $table\n\t\t\t";
			}
			// rather than getting all the keys for all the potential key columns (which amounts to every value in every column of the table, since we're not pre-defining the key columns), we'll populate that menu via AJAX as needed
		}
		

	} else {
		$templates_javascript = "";
		$content_files_javascript = "";
		$database_tables_javascript = "";
		$file_fields_javascript = "";
		$field_forms_javascript = "";
	}

	function javascript_name($name) {
		$name = preg_replace("/\W/", "_", $name);
		$name = preg_replace("/^_+/", "_", $name);
		return $name;
	}
	
?>

<html>

	<head>
		<title><?php print EDITOR_NAME; ?></title>
		<?php eval(include_namespace(HEAD_FILE)); ?>
		<script language="JavaScript">
		
			content_type = "<?php print CONTENT_TYPE; ?>";
			cookie_path = "<?php print ini_get('session.cookie_path'); ?>";
			editor_index = "<?php print $editor_index; ?>";
			contemplate_directory = "<?php print str_replace(SITE_DIRECTORY.'/', '', CONTEMPLATE_DIRECTORY); ?>";
			changed = 0;

			function build_page_browser(next) {
				PHP_command = escape("build_page_browser()");
				ajax_URL = editor_index + "?page=ajax_handler&command=" + PHP_command;
				success_code = "show_page_browser(ajax_response);";
				if (next) { success_code += next; }
				failure_code = "alert(\"There was an error starting the page browser.\");";
				ajax_request(ajax_URL, success_code, failure_code);
			}
			
			function show_page_browser(response) {
				document.getElementById('page_browser').innerHTML = response;
				show_current_page();
				get_page_names();
			}
			
			function change_edit_mode(mode) {
				clear_feedback();
				document.getElementById("page_editor_controls_upper").innerHTML = document.getElementById("page_editor_controls_" + mode).innerHTML;
				if ((mode == "edit")||(mode == "delete")) {
					document.getElementById("page_editor_controls_" + mode + "_name").innerHTML = page;
				} else if ((mode == "copy")||(mode == "rename")) {
					document.getElementById("page_editor_controls_" + mode + "_name_original").innerHTML = page;
					document.getElementById("page_editor_controls_" + mode + "_name").value = (mode == "copy") ? page.replace(/([^\.]+)(\.?.*)/, "$1"+"_copy$2") : page ;
				}
				if ((mode == "edit")&&(content_type == "contemplate")) {
					document.getElementById("page_editor_controls_lower").innerHTML = document.getElementById("page_editor_controls_save").innerHTML;
				} else {
					document.getElementById("page_editor_controls_lower").innerHTML = "";
				}
			}
		
			function clear_editor() {
				document.getElementById("page_editor_controls_upper").innerHTML = "";
				document.getElementById("page_editor_controls_middle").innerHTML = "";
				document.getElementById("page_editor_controls_lower").innerHTML = "";
			}
		
			function click_page(page) {
				show_progress_state(0); // this is in case the progress state gets stuck after a previous operation
				if (warn_unsaved("load")) {
					// if it's okay to replace the current content, load the new content
					show_progress_state(1);
					if (content_type == "contemplate") {
						PHP_command = escape("get_contemplate_page_definition('" + page + "')");
						ajax_URL = editor_index + "?page=ajax_handler&command=" + PHP_command;
						success_code = "load_page('" + page + "', ajax_response);";
						failure_code = "alert(\"There was an error loading the '" + page + "' page.\");";
						ajax_request(ajax_URL, success_code, failure_code);
					} else if (content_type == "dreamweaver") {
						load_page(page, "");
					}
					remember_current_page(page);
				}
			}
			
			function load_page(page, page_definition) {
				clear_feedback();
				if (content_type == "contemplate") {
					window.args = ajax_unserialize(page_definition, 1);
					form_elements = "";
					load_commands = "";
					for (arg in args) {
						form_elements += "<div class=\"page_editor_edit_item\">\n";
						form_elements += "<p class=\"page_editor_edit_item_name\">" + arg + "</p>";
						if (arg == "template") {
							// template argument
							form_elements += "<div class=\"reset\"></div>\n";
							form_elements += "<select id=\"arg_template_file\" onchange=\"show_unsaved_page();\">\n";
							form_elements += "	<option value=\"\">File...</option>\n";
							form_elements += "</select>\n";
							load_commands += "set_template('" + args[arg] + "');\n";
						} else if (args[arg].indexOf(",") != -1) {
							// content argument
							form_elements += "<a href=\"JavaScript:inspect_content('" + arg + "');\" class=\"link_inspect\">Inspect</a>\n";
							form_elements += "<div class=\"reset\"></div>\n";
							form_elements += "<select id=\"arg_" + arg + "_type\" onchange=\"load_next('" + arg + "', 1, ''); show_unsaved_page();\">\n";
							form_elements += "	<option value=\"\">Type...</option>\n";
							form_elements += "	<option value=\"file\">File</option>\n";
							form_elements += "	<option value=\"field\">Field</option>\n";
							form_elements += "	<option value=\"database\">Database</option>\n";
							form_elements += "	<option value=\"form\">Form</option>\n";
							form_elements += "	<option value=\"none\">None</option>\n";
							form_elements += "</select><br />\n";
							form_elements += "<select id=\"arg_" + arg + "_1\" onchange=\"load_next('" + arg + "', 2, ''); show_unsaved_page();\">\n";
							form_elements += "	<option value=\"\">(not needed)</option>\n";
							form_elements += "</select><br />\n";
							form_elements += "<select id=\"arg_" + arg + "_2\" onchange=\"load_next('" + arg + "', 3, ''); show_unsaved_page();\">\n";
							form_elements += "	<option value=\"\">(not needed)</option>\n";
							form_elements += "</select><br />\n";
							form_elements += "<select id=\"arg_" + arg + "_3\" onchange=\"load_next('" + arg + "', 4, ''); show_unsaved_page();\">\n";
							form_elements += "	<option value=\"\">(not needed)</option>\n";
							form_elements += "</select><br />\n";
							form_elements += "<select id=\"arg_" + arg + "_4\" onchange=\"show_unsaved_page();\">\n";
							form_elements += "	<option value=\"\">(not needed)</option>\n";
							form_elements += "</select><br />\n";
							load_commands += "set_menus('" + arg + "', '" + args[arg] + "');\n";
						} else {
							// value argument
							form_elements += "<div class=\"reset\"></div>\n";
							form_elements += "<input type=\"text\" size=\"24\" id=\"arg_" + arg + "_value\" onchange=\"show_unsaved_page();\"><br>";
							load_commands += "set_value('" + arg + "', '" + args[arg] + "');\n";
						}
						form_elements += "</div>\n\n";
					}
					form_elements += "<div class=\"reset_left\"></div>\n";
					document.getElementById("page_editor_controls_middle").innerHTML = form_elements;
					eval(load_commands);
				} else if (content_type == "dreamweaver") {
					page_preview = "<iframe width=\"100%\" height=\"400\" frameborder=\"0\" src=\"../" + page + "\" onload=\"show_progress_state(0);\"></iframe>\n"; // the onload event here is non-standard but supported by all browsers
					document.getElementById("page_editor_controls_middle").innerHTML = page_preview;
				}
				window.page = page; // this makes the variable available to all the functions
				change_edit_mode("edit", page);
				changed = 0;
				if (content_type == "contemplate") {
					show_progress_state(0);
				}
			}
			
			function save_page() {
				if (!window.page) {
					alert("There was an error identifying the page you are editing. Please try selecting the page again.");
				} else {
					show_progress_state(1);
					page_definition = "";
					for (arg in window.args) {
						if (arg == "template") {
							// template argument
							args[arg] = read_template(arg);
						} else if (document.getElementById("arg_" + arg + "_type")) {
							// content argument
							args[arg] = read_menus(arg);
						} else {
							// value argument
							args[arg] = read_value(arg);
						}
					}
					page_definition = ajax_serialize(args);
					//alert(page_definition);
					PHP_command = escape("manage_page('" + page + "', 'save', '', '" + page_definition + "')");
					ajax_URL = editor_index + "?page=ajax_handler&command=" + PHP_command;
					success_code = "save_page_finish(ajax_response);";
					failure_code = "alert(\"There was an error saving the page in the page definitions file.\");";
					ajax_request(ajax_URL, success_code, failure_code);
				}
			}
			
			function save_page_finish(response) {
				show_feedback(response);
				show_saved_page();
				show_progress_state(0);
			}
			
			function copy_page() {
				if (window.page) {
					new_name = document.getElementById("page_editor_controls_copy_name").value;
					if (!check_name(new_name)) {
						document.getElementById("page_editor_controls_copy_name").focus();
						return;
					}
					PHP_command = escape("manage_page('" + page + "', 'copy', '" + new_name + "')");
					ajax_URL = editor_index + "?page=ajax_handler&command=" + PHP_command;
					success_code = "copy_page_finish(ajax_response, '" + new_name + "');";
					failure_code = "alert(\"There was an error copying the page in the page definitions file.\");";
					ajax_request(ajax_URL, success_code, failure_code);
				}
			}
			
			function copy_page_finish(response, new_page) {
				show_feedback(response);
				if (response.indexOf("feedback_positive") != -1) {
					window.page = new_page;
					remember_current_page(new_page);
					build_page_browser();
					change_edit_mode("edit");
				}
			}
			
			function rename_page() {
				if (window.page) {
					new_name = document.getElementById("page_editor_controls_rename_name").value;
					if (!check_name(new_name)) {
						document.getElementById("page_editor_controls_rename_name").focus();
						return;
					}
					PHP_command = escape("manage_page('" + page + "', 'rename', '" + new_name + "')");
					ajax_URL = editor_index + "?page=ajax_handler&command=" + PHP_command;
					success_code = "rename_page_finish(ajax_response, '" + new_name + "');";
					failure_code = "alert(\"There was an error renaming the page in the page definitions file.\");";
					ajax_request(ajax_URL, success_code, failure_code);
				}
			}
			
			function rename_page_finish(response, new_page) {
				show_feedback(response);
				if (response.indexOf("feedback_positive") != -1) {
					window.page = new_page;
					remember_current_page(new_page);
					build_page_browser();
					change_edit_mode("edit");
				}
			}
			
			function delete_page() {
				if (window.page) {
					PHP_command = escape("manage_page('" + page + "', 'delete')");
					ajax_URL = editor_index + "?page=ajax_handler&command=" + PHP_command;
					success_code = "delete_page_finish(ajax_response);";
					failure_code = "alert(\"There was an error deleting the page from the page definitions file.\");";
					ajax_request(ajax_URL, success_code, failure_code);
				}
			}
			
			function delete_page_finish(response) {
				show_feedback(response);
				if (response.indexOf("feedback_positive") != -1) {
					window.page = "";
					remember_current_page("");
					build_page_browser();
					clear_editor();
				}
			}
			
			function show_saved_page() {
				document.getElementById("page_editor_controls_edit_status").innerHTML = "";
				changed = 0;
			}

			function show_unsaved_page() {
				// to do: remove this if we change the content, then change it back
				//current_content = tinyMCE.activeEditor.getContent();
				//if ((current_content != saved_content)&&(!show_unsaved)) {
					document.getElementById("page_editor_controls_edit_status").innerHTML = "(unsaved)";
					changed = 1;
				//}
			}

			function show_feedback(message) {
				document.getElementById("feedback").innerHTML = message;
			}
			
			function clear_feedback() {
				document.getElementById('feedback').innerHTML = "";
			}
			
			function warn_unsaved(action) {
				if (!changed) {
					return true;
				} else {
					if (action == "load") {
						message = "Your changes are not saved. Are you sure you want to edit a different page?";
						return confirm(message);
					} else if (action == "leave") {
						message = "Your changes are not saved. Are you sure you want to leave?";
						return true; // returning false doesn't stop the unload event, so there's no point in using this
					}
				}
			}
			
			function remember_current_page(page) {
				set_cookie("page_browser", "current_page", page);
			}
			
			function show_current_page() {
				page = get_cookie("page_browser", "current_page");
				if (page) {
					click_page(page);
				}
			}
			
			function get_page_names() {
				// scan through the page browser HTML and make a list of page names contained there
				window.page_names = new Array();
				links = document.getElementById("page_browser").getElementsByTagName("a");
				for (i=0; i<links.length; i++) {
					if (links[i].className == "page_browser_page") {
						page_name = links[i].id.replace(/page_/, "");
						window.page_names.push(page_name);
					}
				}
			}
			
			function check_name(new_name) {
				new_name_compare = new_name.replace(/[\w-\.\/]/g, ""); // remove all allowed characters
				if (new_name_compare.length) {
					alert("Page names can't contain the following characters: " + new_name_compare + ". Please enter a different name.");
					return false;
				}
				new_name_stripped = new_name.replace(/\W/g, "_"); // match the stripped versions used for the link IDs
				if (get_position(new_name_stripped, window.page_names) != -1) {
					alert("A page named \"" + new_name + "\" already exists. Please enter a different name.");
					return false;
				}
				return true;
			}

			function inspect_content(tag_name) {
				arg = read_menus(tag_name);
				if (arg) {
					popup("../" + contemplate_directory + "/assembler.php?args=" + arg, 650, 500);
				} else {
					alert("Please set these menus before inspecting the content.");
				}	
				
			}
			
			function set_value(tag_name, value) {
				document.getElementById("arg_" + tag_name + "_value").value = value;
			}

			function read_value(tag_name) {
				value = document.getElementById("arg_" + tag_name + "_value").value;
				return value;
			}
			
			function set_template(file) {
				template_menu = document.getElementById("arg_template_file");
				load_menu(template_menu, "File...", templates);
				template_menu.selectedIndex = get_position(file, templates) + 1;
			}

			function read_template() {
				template_menu = document.getElementById("arg_template_file");
				file = template_menu.options[template_menu.selectedIndex].value;
				return file;
			}

			function set_menus(tag_name, args) {
				type_menu = document.getElementById("arg_" + tag_name + "_type");
				arg_1_menu = document.getElementById("arg_" + tag_name + "_1");
				arg_2_menu = document.getElementById("arg_" + tag_name + "_2");
				arg_3_menu = document.getElementById("arg_" + tag_name + "_3");
				arg_4_menu = document.getElementById("arg_" + tag_name + "_4");
				
				args = args.split(",");
				type = args[0];
				arg_1 = args[1];
				arg_2 = (args.length > 2) ? args[2] : "" ;
				arg_3 = (args.length > 3) ? args[3] : "" ;
				arg_4 = (args.length > 4) ? args[4] : "" ;
				
				//if (arg_1.indexOf("../") == -1) { arg_1 = "../content/" + arg_1; } // add the default folder name if needed
				
				type_menu.selectedIndex = get_position(type, tag_types) + 1;
				load_next(tag_name, 1, arg_1);
				load_next(tag_name, 2, arg_2);
				load_next(tag_name, 3, arg_3);
				load_next(tag_name, 4, arg_4);
			}
			
			function load_next(tag_name, index, selected) {
			
				type_menu = document.getElementById("arg_" + tag_name + "_type");
				arg_1_menu = document.getElementById("arg_" + tag_name + "_1");
				arg_2_menu = document.getElementById("arg_" + tag_name + "_2");
				arg_3_menu = document.getElementById("arg_" + tag_name + "_3");
				arg_4_menu = document.getElementById("arg_" + tag_name + "_4");
				
				type = type_menu.options[type_menu.selectedIndex].value;

				switch (index) {
				
					case 1:
					
						if ((type == "file")||(type == "field")||(type == "form")) {
							if (isset("content_files")) {
								load_menu(arg_1_menu, 'File...', content_files, selected);
							} else {
								clear_menu(arg_1_menu, '(none available)');
							}

						} else if (type == "database") {
							if (isset("database_tables")) {
								load_menu(arg_1_menu, 'Table...', database_tables, selected);
							} else {
								clear_menu(arg_1_menu, '(none available)');
							}

						} else {
							arg_1_menu.selectedIndex = 0;

						}
						
						if (type == "file") {
							clear_menu(arg_2_menu, '(not needed)');
							clear_menu(arg_3_menu, '(not needed)');
							clear_menu(arg_4_menu, '(not needed)');
						} else if (type == "field") {
							clear_menu(arg_2_menu, 'Field...');
							clear_menu(arg_3_menu, '(not needed)');
							clear_menu(arg_4_menu, '(not needed)');
						} else if (type == "database") {
							clear_menu(arg_2_menu, 'Column...');
							clear_menu(arg_3_menu, 'Key column...');
							clear_menu(arg_4_menu, 'Key...');
						} else if (type == "form") {
							clear_menu(arg_2_menu, 'Field...');
							clear_menu(arg_3_menu, 'Form...');
							clear_menu(arg_4_menu, '(not needed)');
						} else if (type == "none") {
							clear_menu(arg_1_menu, '(not needed)');
							clear_menu(arg_2_menu, '(not needed)');
							clear_menu(arg_3_menu, '(not needed)');
							clear_menu(arg_4_menu, '(not needed)');
						}

						break;
						
					case 2:
					
						if ((type == "field")||(type == "database")||(type == "form")) {
							items = new Array();
							arg_1 = arg_1_menu.options[arg_1_menu.selectedIndex].value;

							if (arg_1) {
								items_javascript_name = javascript_name(arg_1);
								items_type = (type == "database") ? "columns" : "fields" ;
								items_array = items_javascript_name + "_" + items_type;
								if (isset(items_array)) {
									items = eval(items_array);
								}
							}

							if (items.length) {
								label = (type == "database") ? "Column..." : "Field..." ;
								load_menu(arg_2_menu, label, items, selected);
								if (type == "database") {
									// the key columns menu has the same values as the columns menu, so we can just load it now
									load_menu(arg_3_menu, "Key column...", items, '');
								}
							} else {
								clear_menu(arg_2_menu, '(none available)');
								if (type == "database") {
									// the key columns menu has the same values as the columns menu, so we can clear it now
									clear_menu(arg_3_menu, '(none available)');
								}
							}
						}
						
						break;
						
					case 3:
					
						// we don't have to worry about loading the key column menu for database types, because that goes loaded along with the columns menu
						// we just have to set it if requested
						if (type == "form") {
							items = new Array();
							arg_1 = arg_1_menu.options[arg_1_menu.selectedIndex].value;
							arg_2 = arg_2_menu.options[arg_2_menu.selectedIndex].value;

							if ((arg_1)&&(arg_2)) {
								items_javascript_name = javascript_name(arg_1 + "_ " + arg_2);
								items_type = "forms";
								items_array = items_javascript_name + "_" + items_type;
								if (isset(items_array)) {
									items = eval(items_array);
								}
							}

							if (items.length) {
								label = "Form...";
								load_menu(arg_3_menu, label, items, selected);
							} else {
								clear_menu(arg_3_menu, '(none available)');
							}
						} else if (type == "database") {
							if (selected) {
								set_menu("page_edit", "arg_" + tag_name + "_3", "value", selected);
							}
						}
						
						break;
						
					case 4:
					
						if (type == "database") {
							arg_1 = arg_1_menu.options[arg_1_menu.selectedIndex].value;
							arg_2 = arg_2_menu.options[arg_2_menu.selectedIndex].value;
							arg_3 = arg_2_menu.options[arg_3_menu.selectedIndex].value;

							if ((arg_1)&&(arg_2)&&(arg_3)) {
								PHP_command = escape("get_contemplate_database_keys('" + arg_1 + "', '" + arg_3 + "')");
								ajax_URL = editor_index + "?page=ajax_handler&command=" + PHP_command;
								success_code = "load_menu(arg_4_menu, 'Key...', ajax_response.split(','), '" + selected + "');";
								failure_code = "alert(\"There was an error loading the contents of the '" + arg_1 + "' table.\");";
								ajax_request(ajax_URL, success_code, failure_code);
							}
						}
						
						break;
						
				}
				
			}
			
			function read_menus(tag_name) {
				// read the current values of the menu(s) for a tag and return them in the argument format
				type_menu = document.getElementById("arg_" + tag_name + "_type");
				arg_1_menu = document.getElementById("arg_" + tag_name + "_1");
				arg_2_menu = document.getElementById("arg_" + tag_name + "_2");
				arg_3_menu = document.getElementById("arg_" + tag_name + "_3");
				arg_4_menu = document.getElementById("arg_" + tag_name + "_4");

				type = type_menu.options[type_menu.selectedIndex].value;
				arg = type;

				if ((type == "file")||(type == "field")||(type == "database")||(type == "form")) {
					arg_1 = arg_1_menu.options[arg_1_menu.selectedIndex].value;
					arg += "," + arg_1;
				}

				if ((type == "field")||(type == "database")||(type == "form")) {
					arg_2 = arg_2_menu.options[arg_2_menu.selectedIndex].value;
					arg += "," + arg_2;
				}

				if ((type == "database")||(type == "form")) {
					arg_3 = arg_3_menu.options[arg_3_menu.selectedIndex].value;
					arg += "," + arg_3;
				}

				if (type == "database") {
					arg_4 = arg_4_menu.options[arg_4_menu.selectedIndex].value;
					arg += "," + arg_4;
				}

				return arg;
			}

			function load_menu(menu_name, menu_title, values, selected) {
				clear_menu(menu_name, menu_title);
				menu_name.className = menu_name.className.replace(/\s*hide/g, "");
				menu_name.options[0] = new Option(menu_title, "");
				if (values.length) {
					for (i=0; i < values.length; i++) {
						this_value = values[i];
						this_value_display = this_value;
						menu_name.options[i+1] = new Option(this_value_display, this_value);
						if (this_value == selected) { menu_name.options[i+1].selected = true; }
					}
				} else {
					menu_name.options[1] = new Option('(none available)', "");
				}
			}
			
			function clear_menu(menu_name, menu_title) {
				if (menu_title == "(not needed)") {
					menu_name.className = menu_name.className.replace(/\s*hide/g, "") + " hide";
				} else {
					menu_name.className = menu_name.className.replace(/\s*hide/g, "");
				}
				menu_name.options[0] = new Option(menu_title, '0');
				for (i=menu_name.options.length; i > 1; i--) {
					menu_name.options[i-1] = null;
				}
			}
			
			/* delete me
			function load_key_columns(data, menu) {
				data = ajax_unserialize(data, 1);
				keys = data["keys"].split(",");
				if (keys.length) {
					label = "Key...";
					load_menu(menu, label, items, selected);
				} else {
					clear_menu(arg_4_menu, '(none available)');
				}
			}
			*/

			function javascript_name(name) {
				name = name.replace(/\W/, "_");
				name = name.replace(/\./, "_");
				name = name.replace(/^_+/, "");
				return name;
			}
			
			function show_progress_state(state) {
				if (state) {
					width = document.getElementById("page_editor_controls_middle").clientWidth;
					height = document.getElementById("page_editor_controls_middle").clientHeight;
					document.getElementById("page_editor_progress").style.width = width + "px";
					document.getElementById("page_editor_progress").style.height = height + "px";
					document.getElementById("page_editor_progress_inner").style.width = width + "px";
					document.getElementById("page_editor_progress_inner").style.height = height + "px";
					document.getElementById("page_editor_progress").className = "";
				} else {
					document.getElementById("page_editor_progress").className = "hide";
				}
			}
			
			// set up arrays to populate the form elements
			tag_types = new Array("file", "field", "database", "form", "none");
			// <?="\n\t\t\t".$templates_javascript?>
			// <?="\n\t\t\t".$content_files_javascript?>
			// <?="\n\t\t\t".$database_tables_javascript?>
			// <?="\n\t\t\t".$file_fields_javascript?>
			// <?="\n\t\t\t".$field_forms_javascript?>
			// <?="\n\t\t\t".$table_columns_javascript?>
			
		</script>
	</head>
	
	<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
	
		<?php eval(include_namespace(HEADER_FILE)); ?>

		<p class="title">Manage pages</p>
		
		<div id="feedback"></div>

		<div id="page_browser" class="body"></div>
		
		<div id="page_editor" class="body"><div id="page_editor_inner">
			<form name="page_edit" onsubmit="return false;">
				<div class="page_editor_controls_upper"><div class="page_editor_controls_inner">
					<div id="page_editor_controls_upper"></div>
					<div class="reset"></div>
				</div></div>
				<div class="page_editor_controls_middle <?=CONTENT_TYPE?>">
					<div id="page_editor_progress" class="hide"><div id="page_editor_progress_inner"></div></div>
					<div id="page_editor_controls_middle"></div>
					<div class="reset"></div>
				</div>
				<div class="page_editor_controls_lower"><div class="page_editor_controls_inner">
					<div id="page_editor_controls_lower"></div>
					<div class="reset"></div>
				</div></div>
			</form>
		</div></div>
		
		<div id="page_editor_controls_sets" style="display: none;">
			<!-- edit controls -->
			<div id="page_editor_controls_edit">
				<div class="page_editor_controls_left">
					Editing <span id="page_editor_controls_edit_name"></span>
					<span id="page_editor_controls_edit_status"></span>
				</div>
				<div class="page_editor_controls_right">
					<input type="button" value="Copy" onclick="change_edit_mode('copy');" class="form" />
					<input type="button" value="Rename" onclick="change_edit_mode('rename');" class="form" />
					<input type="button" value="Delete" onclick="change_edit_mode('delete');" class="form" />
				</div>
			</div>
			<!-- save controls -->
			<div id="page_editor_controls_save">
				<div class="page_editor_controls_left">
				</div>
				<div class="page_editor_controls_right">
					<input type="button" value="Save" onclick="save_page();" class="form" />
				</div>
			</div>
			<!-- copy controls -->
			<div id="page_editor_controls_copy">
				<div class="page_editor_controls_left">
					Copy <span id="page_editor_controls_copy_name_original"></span> to <input type="text" name="name" size="24" id="page_editor_controls_copy_name" class="form" />
				</div>
				<div class="page_editor_controls_right">
					<input type="button" value="Cancel" onclick="change_edit_mode('edit');" class="form" />
					<input type="button" value="OK" onclick="copy_page();" class="form" />
				</div>
			</div>
			<!-- rename controls -->
			<div id="page_editor_controls_rename">
				<div class="page_editor_controls_left">
					Rename <span id="page_editor_controls_rename_name_original"></span> to <input type="text" name="name" size="24" id="page_editor_controls_rename_name" class="form" />
				</div>
				<div class="page_editor_controls_right">
					<input type="button" value="Cancel" onclick="change_edit_mode('edit');" class="form" />
					<input type="button" value="OK" onclick="rename_page();" class="form" />
				</div>
			</div>
			<!-- delete controls -->
			<div id="page_editor_controls_delete">
				<div class="page_editor_controls_left">
					Deleting <span id="page_editor_controls_delete_name"></span>
				</div>
				<div class="page_editor_controls_right">
					<input type="button" value="Cancel" onclick="change_edit_mode('edit');" class="form" />
					<input type="button" value="OK" onclick="delete_page();" class="form" />
				</div>
			</div>
		</div>
		
		<script language="JavaScript">
			<?php print $onload; ?>
			document.onunload = "warn_unsaved('leave')";
		</script>
		
		<?php eval(include_namespace(FOOTER_FILE)); ?>

	</body>
	
</html>