<?php

	// if this is not an administrator, redirect to the user's own record
	if ($role < 3) {
		redirect($wrapper_page."?page=users_edit");
	}
	
	// get any feedback from the submit script
	$feedback = get_feedback();

	// create an onload string
	$onload = "load_list();";
	if ($feedback) { $onload .= "show_feedback(\"".trim(addslashes($feedback))."\");"; }
	
?>

<html>

	<head>
		<title><?php print EDITOR_NAME; ?></title>
		<?php eval(include_namespace(HEAD_FILE)); ?>
		<script language="JavaScript">
		
			function edit(ID) {
				location = "<?=$wrapper_page?>?page=users_edit&ID=" + ID;
			}

			function add_user() {
				location = "<?=$wrapper_page?>?page=users_edit";
			}

			function delete_user(ID, label) {
				if (confirm("Are you sure you wish to delete the user '" + label + "'?")) {
					location = "<?=$editor_index?>?page=users_submit&ID=" + ID + "&delete=1";
				}
			}
			
			function apply_filters() {
				load_list();
			}
			
			function reset_filters() {
				for (i=0; i<filters.elements["filter_role[]"].length; i++) {
					document.filters.elements["filter_role[]"][i].checked = 0;
				}
				document.filters.filter_active.checked = 0;
			}
			
			function serialize_filters() {
				data = new Object;
				filter_role = new Array();
				for (i=0; i<document.filters.elements["filter_role[]"].length; i++) {
					if (document.filters.elements["filter_role[]"][i].checked) {
						filter_role[filter_role.length] = document.filters.elements["filter_role[]"][i].value;
					}
				}
				filter_role = filter_role.join(",");
				data["filter_role"] = filter_role;
				data["filter_active"] = (document.filters.filter_active.checked) ? 1 : 0 ;
				data["sort"] = document.filters.sort.value;
				data["start"] = document.filters.start.value;
				data["filters_submitted"] = document.filters.filters_submitted.value;
				data = ajax_serialize(data, 0);
				return data;
			}
			
			function sort(column) {
				document.filters.sort.value = column;
				load_list();
			}

			function click_pagination_link(start) {
				document.filters.start.value = start;
				load_list();
			}

			function load_list() {
				clear_feedback();
				ajax_URL = "<?=$editor_index?>?page=users_manage_list&" + serialize_filters();
				success_code = "document.getElementById('list').innerHTML = ajax_response;";
				failure_code = "alert(\"There was an error loading the list.\");";
				ajax_request(ajax_URL, success_code, failure_code);
			}
			
			function show_feedback(message) {
				document.getElementById("feedback").innerHTML = message;
			}
			
			function clear_feedback() {
				document.getElementById('feedback').innerHTML = "";
			}
			
		</script>
	</head>
	
	<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
	
		<?php eval(include_namespace(HEADER_FILE)); ?>

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

		<div class="filters">
		<?php

			// get saved filter values from the database
			prepare_filters("user", $user, "users_manage", "role,active");

			// display the filter options
			print "<form name=\"filters\" onsubmit=\"return false;\">\n\n";
			
			print "<p class=\"body stack_left\">View users of role...<br />\n";
			print make_checkboxes("role", "filter_role", "ID", "name", 0, 0, "&nbsp;", $filter_role);
			print "</p>\n\n";

			$filter_active_checked = ($filter_active) ? "checked" : "" ;
			print "<p class=\"body stack_left\"><br />\n";
			print "<input type=\"checkbox\" name=\"filter_active\" value=\"1\" $filter_active_checked />\n";
			print "View active users only</p>\n\n";
			
			print "<div class=\"reset\"></div>\n";
			
			print "<input type=\"hidden\" name=\"sort\" value=\"\">\n";
			print "<input type=\"hidden\" name=\"start\" value=\"0\">\n";
			print "<input type=\"hidden\" name=\"filters_submitted\" value=\"1\">\n";
			print "<input type=\"submit\" value=\"View\" class=\"form\" onclick=\"apply_filters()\">\n";
			print "&nbsp;<input type=\"button\" value=\"Reset\" class=\"form\" onclick=\"reset_filters()\">\n\n";
			
			print "</form>\n\n";

		?>
		</div>
		
		<div id="list" class="body">
		</div>
		
		<script language="JavaScript">
			<?php print $onload; ?>
		</script>
		
		<?php eval(include_namespace(FOOTER_FILE)); ?>

	</body>
	
</html>