<?php
/////////////////////////////////////////////
// Manage Page Access Management /////
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 6/28/2012	ELL
// 
// 
//
	include ("check_session.php");
	
	$last_user_table = $_SESSION['last_user_table'];
	$last_user = $_SESSION['last_user'];
	$last_access_level = $_SESSION['last_access_level'];
?>
<html>
<head>
<title>Breeze Website Builder</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="manager.css" type="text/css">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">

<style type="text/css">
#delete_confirm_user	{	display:none;	}
#delete_confirm_access_level	{	display:none;	}
</style>

<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/trim.js"></script>


<script language="javascript" type="text/javascript">

function OpenHelpWindow()
{
	window.open("http://breezewebsitebuilder.com/docs/cms/help.html#manage_page_access", "Help", "scrollbars=1,resizable=1,status=0,toolbar=0,menubar=0,location=0,height=700,width=800");
}


function DeleteUserConfirm()
{
	var ntop;
	ntop = tempY;
	if (ntop < 500)
		ntop = ntop + 20;
	else
		ntop = ntop - 20;
	var strtop = ntop + "px";
	
	document.getElementById("delete_confirm_user").style.display = "block";
	document.getElementById("delete_confirm_user").style.position = "absolute";
	document.getElementById("delete_confirm_user").style.top = strtop;
	document.getElementById("delete_confirm_user").style.left = "300px";
	document.getElementById("delete_confirm_user").style.width = "450px";
	document.getElementById("delete_confirm_user").style.backgroundColor = "#FFF";
	document.getElementById("delete_confirm_user").style.borderStyle = "solid";
	document.getElementById("delete_confirm_user").style.borderWidth = "2px";
	document.getElementById("delete_confirm_user").style.borderColor = "#000";
	document.getElementById("delete_text_user").innerHTML = document.getElementById("selectUserNameDelete").value;
}

function DeleteUser()
{
	var user_name = document.getElementById("selectUserNameDelete").value;
	var table = document.getElementById("table_delete_user").value;
	document.getElementById("delete_confirm_user").style.display = "none";
	var url = "delete_user.php?user_name=" + user_name + "&table=" + table;
	window.location = url;
}

function HideUserDelete()
{
	document.getElementById("delete_confirm_user").style.display = "none";
}

function GeneratePassword(source)
{
	var password = "";
	var password_length = rand(6,10);
	var i;
	var assci_segment;
	
	for (i = 0; i < password_length; i++)
	{
		assci_segment = rand(1,3);
		if (assci_segment == 1)
			password = password + String.fromCharCode(rand(48,57));	// numeric
		else if (assci_segment == 2)
			password = password + String.fromCharCode(rand(65,90));	// alpha upper case
		else
			password = password + String.fromCharCode(rand(97,122));	// alpha lower case
	}
	password = password.replace('I', 'i');
	password = password.replace('O', 'o');
	password = password.replace('l', 'L');
	
	if (source == "new")
		var frm_obj = document.getElementById ('password_new_user');
	else
		var frm_obj = document.getElementById ('password_existing_user');
		
	frm_obj.value = password;
		
	return password;
}

// Equivalent to PHP rand(min, max);
function rand ( n, m )
{
  return ( n + Math.floor ( Math.random ( ) * (m - n + 1)) );
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

var pages = new Array();
var tables = new Array();
var access_levels = new Array();
var login_pages = new Array();
var bgclr = "#eeeeee";
var update_page_table_mode = 0;
<?php echo "var last_user_table = '$last_user_table';\n"; ?>
<?php echo "var last_user = '$last_user';\n"; ?>
<?php echo "var last_access_level = '$last_access_level';\n"; ?>
var access_level_add = "-not set-";
var access_level_remove = "-not set-";

<?php
	$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data ORDER BY page_name";
	$result = mysql_query($query, $bwb_link);
	$num_results = mysql_num_rows($result);
	for ($j = 0; $j < $num_results; $j++)
	{
		$row = mysql_fetch_array($result);
		$page_name = $row["page_name"];
		echo "pages[$j] = '$page_name';\n";
	}
	echo "var page_cnt = $j;\n\n";
	
	$query = "SHOW TABLES";
	$result = mysql_query($query, $bwb_link);
	$tmp_table = "";
	$j = 0;
	$tables = array();
	while ($row = mysql_fetch_array($result))
	{
		$tmp_table = $row[0];
		if ((substr($tmp_table, 0, strlen(BWB_TABLE_PREFIX)) != BWB_TABLE_PREFIX) || (BWB_TABLE_PREFIX == ""))
		{
			echo "tables[$j] = '" . $tmp_table . "';\n";
			$tables[$j] = $tmp_table;
			$j++;
		}
	}
	echo "var table_cnt = $j;\n\n";
	$table_cnt = $j;
	
	
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "access_levels";
	$result = mysql_query($query, $bwb_link);
	$num_results = mysql_num_rows($result);
	for ($j = 0; $j < $num_results; $j++)
	{
		$row = mysql_fetch_array($result);
		$access_level = $row["access_level"];
		echo "access_levels[$j] = '$access_level';\n";
	}
	echo "var access_level_cnt = $j;\n\n";
	
	
	$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data WHERE page_name LIKE '%login%'";
	$result = mysql_query($query, $bwb_link);
	$num_results = mysql_num_rows($result);
	for ($j = 0; $j < $num_results; $j++)
	{
		$row = mysql_fetch_array($result);
		$login_page = $row["page_name"];
		echo "login_pages[$j] = '$login_page';\n";
	}
	echo "var login_page_cnt = $j;\n\n";
?>

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////
function DeleteAccessLevelConfirm()
{
	var ntop;
	ntop = tempY;
	if (ntop < 500)
		ntop = ntop + 20;
	else
		ntop = ntop - 20;
	var strtop = ntop + "px";
	
	document.getElementById("delete_confirm_access_level").style.display = "block";
	document.getElementById("delete_confirm_access_level").style.position = "absolute";
	document.getElementById("delete_confirm_access_level").style.top = strtop;
	document.getElementById("delete_confirm_access_level").style.left = "200px";
	document.getElementById("delete_confirm_access_level").style.width = "450px";
	document.getElementById("delete_confirm_access_level").style.backgroundColor = "#FFF";
	document.getElementById("delete_confirm_access_level").style.borderStyle = "solid";
	document.getElementById("delete_confirm_access_level").style.borderWidth = "2px";
	document.getElementById("delete_confirm_access_level").style.borderColor = "#000";
	document.getElementById("delete_text_access_level").innerHTML = document.getElementById("access_level_remove").value;
}

function HideAccessLevelDelete()
{
	document.getElementById("delete_confirm_access_level").style.display = "none";
}

//////////////////////////////////////////////////////////////////////////////////////
function UpdatePageRestriction(page_name, i)
{		
	var params = "";
	var login_required_ctl = "login_required" + i;
	var user_table_ctl = "user_table" + i;
	var access_level_ctl = "access_level" + i;
	var login_page_ctl = "login_page" + i;

	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		document.getElementById("StatusMsg").innerHTML = "Starting update...";
		document.getElementById("StatusMsg2").innerHTML = "Starting update...";
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","update_page_restriction.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "Update");
		
		params = "page_name=" + page_name;
		params = params + "&login_required=" + document.getElementById(login_required_ctl).checked;
		if (document.getElementById(user_table_ctl).value != "")
			params = params + "&user_table=" + document.getElementById(user_table_ctl).value;
		if (document.getElementById(access_level_ctl).value != "")
			params = params + "&access_level=" + document.getElementById(access_level_ctl).value;
		if (document.getElementById(login_page_ctl).value != "")
			params = params + "&login_page=" + document.getElementById(login_page_ctl).value;
//		alert(params);
		xmlhttp.send(params);
	}
}


//////////////////////////////////////////////////////////////////////////////////////
function UpdateMessage(xmlhttp, StatusMsg_control_name, Source)
{
	var StatusMsg_control_name2 = StatusMsg_control_name + "2";
	var query_complete_no_record = "Query complete.  No records found.";
	var query_complete = "<b>Query complete<\/b><br/>\n";
	var query_read_to_send = "Query ready to send...";
	var query_sent = "Query sent";
	var query_starting = "Locating page restrictions...";
	
	if (Source == "Update")
	{
		query_complete_no_record = "No record found. Could not update page restrictions.";
		query_complete = "<b>Page restrictions updated<\/b><br/>\n";
		query_starting = "Updating page restrictions...";
	}
	else if (Source == "UpdateUserStatus")
	{
		query_complete_no_record = "No record found. Could not update user status.";
		query_complete = "<b>User status updated<\/b><br/>\n";
		query_starting = "Updating user status...";
	}
	else if (Source == "AddFields")
	{
		query_complete_no_record = "Could not modify table.";
		query_complete = "<b>Fields added to table<\/b><br/>\n";
		query_starting = "Adding fields to table...";
	}
	else if (Source == "AssignWelcomeMessage")
	{
		query_complete_no_record = "Could not modify table.";
		query_complete = "<b>Welcome message page added to table<\/b><br/>\n";
		query_starting = "Adding welcome message page to table...";
	}
	else if (Source == "AddRemoveAccessLevel")
	{
		query_complete_no_record = "Could not add or delete access level.";
		query_complete = "<b>Access level records modified<\/b><br/>\n";
		query_starting = "Changing access levels...";
	}
	else if (Source == "PopulateAccessLevels")
		query_starting = "Locating access levels...";
	else if (Source == "PopulateUsers")
		query_starting = "Locating users...";

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4)
		{
			if (xmlhttp.responseText.length > 0)
			{
				var rows_str = xmlhttp.responseText;
				//alert(rows_str);
				if (rows_str == "-no data-")
				{
					document.getElementById(StatusMsg_control_name).innerHTML = query_complete_no_record;
					document.getElementById(StatusMsg_control_name2).innerHTML = query_complete_no_record;
				}
				else
				{
					document.getElementById(StatusMsg_control_name).innerHTML = query_complete;
					document.getElementById(StatusMsg_control_name2).innerHTML = query_complete;
				}
				if (Source == "Populate")
					PopulatePageTable(rows_str);
				else if (Source == "PopulateAccessLevels")
					PopulateAccessLevels(rows_str);
				else if (Source == "AddFields")
					alert("Make sure the form you are using for users has fields with the names 'email', 'UserID' and 'Password'. If the table you selected was created using the account-creation form plugin, then these fields already exist in this table.");
				else if (Source == "AddRemoveAccessLevel")
					RetrieveAccessLevels(1);
				else if (Source == "PopulateUsers")
					PopulateUserLists(rows_str);

			}
			else
			{
				document.getElementById(StatusMsg_control_name).innerHTML = query_complete_no_record;
				document.getElementById(StatusMsg_control_name2).innerHTML = query_complete_no_record;
			}

			var ClearMessageString = "ClearMessage('" + StatusMsg_control_name + "');";
			mTimer = setTimeout(ClearMessageString, 2000);
		}
		else if (xmlhttp.readyState==1)
		{
			document.getElementById(StatusMsg_control_name).innerHTML = query_read_to_send;
			document.getElementById(StatusMsg_control_name2).innerHTML = query_read_to_send;
		}
		else if (xmlhttp.readyState==2)
		{
			document.getElementById(StatusMsg_control_name).innerHTML = query_sent;
			document.getElementById(StatusMsg_control_name2).innerHTML = query_sent;
		}
		else if (xmlhttp.readyState==3)
		{
			document.getElementById(StatusMsg_control_name).innerHTML = query_starting;
			document.getElementById(StatusMsg_control_name2).innerHTML = query_starting;
		}
	}
	return (xmlhttp);
}

//////////////////////////////////////////////////////////////////////////////////////
function ClearMessage(StatusMsg_control_name)
{
	var StatusMsg_control_name2 = StatusMsg_control_name + "2";
	document.getElementById(StatusMsg_control_name).innerHTML = "&nbsp;";
	document.getElementById(StatusMsg_control_name2).innerHTML = "&nbsp;";
	clearInterval(mTimer);
}


////////////////////////////////////////////////////////////////////////////////////
function RetrievePages()
{		
	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		document.getElementById("StatusMsg").innerHTML = "Retrieving page restrictions...";
		document.getElementById("StatusMsg2").innerHTML = "Retrieving page restrictions...";
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","populate_page_restrictions.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "Populate");
		xmlhttp.send();
	}
}

//////////////////////////////////////////////////////////////////////////////////////
function PopulatePageTable(rows_str)
{
	var rows = new Array();

	var output_str = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>\n";
	output_str = output_str + "<tr><td colspan='5' class='sub_title'>Restrict Page Access</td></tr>\n";
	output_str = output_str + "<tr bgcolor='#CCCCCC' class='header_row'>\n";
	output_str = output_str + "<td class='header_row'><b>Page<\/b><\/td>\n";
	output_str = output_str + "<td class='header_row'><b>Require Login<\/b><\/td>\n";
	output_str = output_str + "<td class='header_row'><b>User Table<\/b><\/td>\n";
	output_str = output_str + "<td class='header_row'><b>Access Level<\/b><\/td>\n";
	output_str = output_str + "<td class='header_row'><b>Login Page<\/b><\/td>\n";
	output_str = output_str + "<td>&nbsp;<\/td>\n";
	output_str = output_str + "<\/tr>\n";

	rows = rows_str.split("@@@@@");
	if (rows[0] == "-no data-")	// If no pages yet (first time).
		var i = 0;
	else	// Create rows for page restrictions.
	{
		for (var i in rows)
		{
			var JSON_row = JSON.parse(rows[i]);
			
			if (bgclr == "#dddddd")
				bgclr = "#eeeeee";
			else
				bgclr = "#dddddd";
				
			output_str = output_str + "<tr id=\"row" + i + "\" bgcolor=\"" + bgclr + "\">\n";
	
			output_str = output_str + "<td valign='top'><input type='hidden' name='ID" + i + "' id='ID" + i + "' value='" + JSON_row.ID + "'>\n";
			output_str = output_str + "<input type='hidden' name='page_name" + i + "' id='page_name" + i + "' value='" + JSON_row.page_name + "'>" + JSON_row.page_name + "<\/td>\n";
				
			output_str = output_str + "<td>\n";
			output_str = output_str + " <input type='checkbox' name='login_required" + i + "' id='login_required" + i + "'";
			if (JSON_row.login_required == 1)
				output_str = output_str + " checked='checked'";
			output_str = output_str + " />\n";
			output_str = output_str + "<\/td>\n";

			output_str = output_str + "<td valign='top'>\n";
			output_str = output_str + "<select name='user_table" + i + "' id='user_table" + i + "'>\n";
			output_str = output_str + "<option value=''>-not selected-<\/option>\n";
			for (j = 0; j < table_cnt; j++)
			{
				if (tables[j] == JSON_row.user_table)
					output_str = output_str + "<option value='" + tables[j] + "' selected='selected'>" + tables[j] + "<\/option>\n";
				else
					output_str = output_str + "<option value='" + tables[j] + "'>" + tables[j] + "<\/option>\n";
			}
			output_str = output_str + "<\/select>\n";
			output_str = output_str + "<\/td>\n";

			output_str = output_str + "<td valign='top'>\n";
			output_str = output_str + "<select name='access_level" + i + "' id='access_level" + i + "'>\n";
			output_str = output_str + "<option value=''>-not selected-<\/option>\n";
			for (j = 0; j < access_level_cnt; j++)
			{
				if (access_levels[j] == JSON_row.AccessLevel)
					output_str = output_str + "<option value='" + access_levels[j] + "' selected='selected'>" + access_levels[j] + "<\/option>\n";
				else
					output_str = output_str + "<option value='" + access_levels[j] + "'>" + access_levels[j] + "<\/option>\n";
			}
			output_str = output_str + "<\/select>\n";
			output_str = output_str + "<\/td>\n";
			
			output_str = output_str + "<td valign='top'>\n";
			output_str = output_str + "<select name='login_page" + i + "' id='login_page" + i + "'>\n";
			for (j = 0; j < login_page_cnt; j++)
			{
				if (login_pages[j] == JSON_row.login_page)
					output_str = output_str + "<option value='" + login_pages[j] + "' selected='selected'>" + login_pages[j] + "<\/option>\n";
				else
					output_str = output_str + "<option value='" + login_pages[j] + "'>" + login_pages[j] + "<\/option>\n";
			}
			output_str = output_str + "<\/select>\n";
			output_str = output_str + "<\/td>\n";
			
			output_str = output_str + "<td valign='top'>\n<img src='images/save.png' border=0 alt='Update' name='Update' onClick=\"UpdatePageRestriction('" + JSON_row.page_name + "', " + i + ");\" />\n<\/td>\n";
			
			output_str = output_str + "<\/tr>\n";

		}
	}
	output_str = output_str + "<\/table>\n";
	
	document.getElementById("PageRestrictionsTable").innerHTML = output_str;
	document.getElementById("nCount").value = i;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
function RetrieveAccessLevels(update_page_table)
{
	update_page_table_mode = update_page_table;
	
	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		document.getElementById("StatusMsg").innerHTML = "Retrieving page restrictions...";
		document.getElementById("StatusMsg2").innerHTML = "Retrieving page restrictions...";
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","populate_access_levels.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "PopulateAccessLevels");
		xmlhttp.send();
	}
}

//////////////////////////////////////////////////////////////////////////////////////
function PopulateAccessLevels(rows_str)
{
	var rows = new Array();
	
	var output_str = "<select name='access_level_remove' id='access_level_remove'>\n";
	var output_str2 = "<select name='selectAccessLevel' id='selectAccessLevel'>\n";
	output_str = output_str + "<option>-not selected-</option>\n";
	output_str2 = output_str2 + "<option>-not selected-</option>\n";

	rows = rows_str.split("@@@@@");
	if (rows[0] == "-no data-")	// If no pages yet (first time).
		var i = 0;
	else	// Create options for access level list.
	{
		for (var i in rows)
		{
			var JSON_row = JSON.parse(rows[i]);
			output_str = output_str + "<option value='" + JSON_row.access_level + "'>" + JSON_row.access_level + "</option>\n";
			output_str2 = output_str2 + "<option value='" + JSON_row.access_level + "'>" + JSON_row.access_level + "</option>\n";
		}
	}
	output_str = output_str + "<\/select>\n";
	output_str2 = output_str2 + "<\/select>\n";
	
	document.getElementById("AccessLevelRemoveDiv").innerHTML = output_str;
	document.getElementById("AccessLevelAddUserDiv").innerHTML = output_str2;

	if (update_page_table_mode == 1)
	{
		if (access_level_add != "-not set-")
		{
			access_levels[access_level_cnt] = access_level_add;
			access_level_cnt++;
			access_level_add = "-not set-";
		}
		else if (access_level_remove != "-not set-")
		{
			access_levels.splice(access_levels.indexOf(access_level_remove), 1);
			access_level_cnt--;
			access_level_remove = "-not set-";
		}
		RetrievePages();
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
function RetrieveUserLists(user_table)
{	
	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		document.getElementById("StatusMsg").innerHTML = "Retrieving users...";
		document.getElementById("StatusMsg2").innerHTML = "Retrieving users...";
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","populate_users.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "PopulateUsers");
		params = "user_table=" + user_table;
		xmlhttp.send(params);
	}
	
	document.getElementById("user_table_for_users_table").value = user_table;
	document.getElementById("user_table_for_fields").value = user_table;
	document.getElementById("table_add_user").value = user_table;
	document.getElementById("table").value = user_table;
	document.getElementById("table_delete_user").value = user_table;
}

//////////////////////////////////////////////////////////////////////////////////////
function PopulateUserLists(rows_str)
{
	var rows = new Array();

	var output_str = "<select name='selectUserName' id='selectUserName'>\n";
	var output_str2 = "<select name='selectUserNameDelete' id='selectUserNameDelete'>\n";
	var output_str3 =  "<table width='100%' border='0' cellspacing='0' cellpadding='0'>\n";
	output_str = output_str + "<option>-not selected-</option>\n";
	output_str2 = output_str2 + "<option>-not selected-</option>\n";
	output_str3 = output_str3 + "<tr bgcolor='#CCCCCC' class='header_row'>\n";
	output_str3 = output_str3 + "<td class='header_row'><b>User<\/b><\/td>\n";
	output_str3 = output_str3 + "<td class='header_row'><b>Active<\/b><\/td>\n";
	output_str3 = output_str3 + "<td>&nbsp;<\/td>\n";
	output_str3 = output_str3 + "</tr>\n";

	rows = rows_str.split("@@@@@");
	if (rows[0] == "-no data-")	// If no pages yet (first time).
		var i = 0;
	else	// Create options for access level list.
	{
		for (var i in rows)
		{
			var JSON_row = JSON.parse(rows[i]);
			if (JSON_row.UserID)
			{
				if (JSON_row.UserID == last_user)
				{
					output_str = output_str + "<option value='" + JSON_row.UserID + "' selected='selected'>" + JSON_row.UserID + "</option>\n";
					output_str2 = output_str2 + "<option value='" + JSON_row.UserID + "' selected='selected'>" + JSON_row.UserID + "</option>\n";
				}
				else
				{
					output_str = output_str + "<option value='" + JSON_row.UserID + "'>" + JSON_row.UserID + "</option>\n";
					output_str2 = output_str2 + "<option value='" + JSON_row.UserID + "'>" + JSON_row.UserID + "</option>\n";
				}
				
				if (bgclr == "#dddddd")
					bgclr = "#eeeeee";
				else
					bgclr = "#dddddd";
			
				output_str3 = output_str3 + "<tr bgcolor='" + bgclr + "'>\n<td>" + JSON_row.UserID + "<input type='hidden' value='" + JSON_row.UserID + "' id='user_id" + i + "' name='user_id" + i + "' /></td>\n<td>\n";
				
				output_str3 = output_str3 + " <input type='checkbox' name='active" + i + "' id='active" + i + "'";
				if (JSON_row.Active == 1)
					output_str3 = output_str3 + " checked='checked'";
				output_str3 = output_str3 + " />\n</td>\n";
				
				output_str3 = output_str3 + "<td valign='top'>\n<img src='images/save.png' border=0 alt='Update' name='Update' onClick=\"UpdateUserStatus('" + JSON_row.UserID + "', " + i + ");\" />\n<\/td>\n";

			}
			if (JSON_row.welcome_message_page)
				document.getElementById("welcome_message_page").value = JSON_row.welcome_message_page;
			else
				document.getElementById("welcome_message_page").selectedIndex = 0;
		}
	}
	output_str = output_str + "<\/select>\n";
	output_str2 = output_str2 + "<\/select>\n";
	output_str3 = output_str3 + "</tr>\n</table>\n";
	
	document.getElementById("UsersChangePassword").innerHTML = output_str;
	document.getElementById("UsersDelete").innerHTML = output_str2;
	document.getElementById("UsersTable").innerHTML = output_str3;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
function UpdateUserStatus(UserID, i)
{		
	var params = "";
	var user_id_ctl = "user_id" + i;
	var active_ctl = "active" + i;

	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		document.getElementById("StatusMsg").innerHTML = "Starting update...";
		document.getElementById("StatusMsg2").innerHTML = "Starting update...";
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","update_user_status.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "UpdateUserStatus");
		
		params = "UserID=" + document.getElementById(user_id_ctl).value;
		params = params + "&Active=" + document.getElementById(active_ctl).checked;
		params = params + "&user_table=" + document.getElementById("user_table_for_users_table").value;
		//alert(params);
		xmlhttp.send(params);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
function AddFieldsToTable()
{		
	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		document.getElementById("StatusMsg").innerHTML = "Adding login fields to table...";
		document.getElementById("StatusMsg2").innerHTML = "Adding login fields to table...";
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","add_login_fields.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "AddFields");
		
		params = "user_table=" + document.getElementById("user_table_for_fields").value;
		xmlhttp.send(params);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
function AssignWelcomeMessagePageToTable()
{		
	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		document.getElementById("StatusMsg").innerHTML = "Assigning welcome message page to table...";
		document.getElementById("StatusMsg2").innerHTML = "Assigning welcome message page to table...";
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","assign_welcome_message_to_table.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "AssignWelcomeMessage");
		
		params = "user_table=" + document.getElementById("user_table_for_fields").value;
		params = params + "&welcome_message_page=" + document.getElementById("welcome_message_page").value;
		xmlhttp.send(params);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
function AddRemoveAccessLevel(action)
{		
	if (document.getElementById("StatusMsg").innerHTML == "&nbsp;")
	{
		if (action == "add")
		{
			document.getElementById("StatusMsg").innerHTML = "Adding access level...";
			document.getElementById("StatusMsg2").innerHTML = "Adding access level...";
		}
		else
		{
			HideAccessLevelDelete();
			document.getElementById("StatusMsg").innerHTML = "Removing access level...";
			document.getElementById("StatusMsg2").innerHTML = "Removing access level...";
		}
	}
	var xmlhttp;
	xmlhttp = getXmlHttpRequestObject();

	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
	{
		xmlhttp.open("POST","add_remove_access_level.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp = UpdateMessage(xmlhttp, "StatusMsg", "AddRemoveAccessLevel");
		
		params = "action=" + action;
		if (action == "add")
		{
			params = params + "&access_level=" + document.getElementById("access_level_add").value;
			access_level_add = document.getElementById("access_level_add").value;
		}
		else
		{
			params = params + "&access_level=" + document.getElementById("access_level_remove").value;
			access_level_remove = document.getElementById("access_level_remove").value;
		}
		xmlhttp.send(params);
	}
}

</script>

</head>

<body>
<br>
<div align="center">
<table width="80%" border="0"><tr>
<td class="title">Manage Restricted Pages and Website Users</td>
<td align="center"><div id="StatusMsg">&nbsp;</div></td>
<td align="right"><input type="button" name="Back" value="Back to Main Menu" onClick="window.location.href='main.php?mode=admin';"></td>
</tr></table>

<table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
  <div id="PageRestrictionsTable">
	&nbsp;
  </div>
  <input type="hidden" name="nCount" id="nCount" />
  </td></tr></table>
  <br />
  
<table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="sub_title">Approve Access to Users</td>
     <td align="right" class='header_row'><b>Table:</b>&nbsp;</td>
    <td>
    <select name="user_table_for_users_table" id="user_table_for_users_table" onChange="RetrieveUserLists(this.value);">
    	<option>-not selected-</option>
<?php
	for ($i = 0; $i < $table_cnt; $i++)
	{
		if ($tables[$i] == $last_user_table)
			echo "<option value='" . $tables[$i] . "' selected='selected'>" . $tables[$i] . "</option>";
		else
			echo "<option value='" . $tables[$i] . "'>" . $tables[$i] . "</option>";
	}
?>
    </select>
    </td>
  </tr>
  <tr>
  	<td colspan="3"><div id="UsersTable">&nbsp;</div></td>
  </tr>
</table>
</td></tr></table>
  <br />
  
<table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="6" class="sub_title">User Table Setup</td>
  </tr>
  <tr>
    <td align="right">Table:&nbsp;</td>
    <td>
    <select name="user_table_for_fields" id="user_table_for_fields" onChange="RetrieveUserLists(this.value);">
      <option>-not selected-</option>
      <?php
	for ($i = 0; $i < $table_cnt; $i++)
	{
		if ($tables[$i] == $last_user_table)
			echo "<option value='" . $tables[$i] . "' selected='selected'>" . $tables[$i] . "</option>";
		else
			echo "<option value='" . $tables[$i] . "'>" . $tables[$i] . "</option>";
	}
?>
    </select>
    </td>
    <td>
    <input type="button" name="add_fields_to_table" id="add_fields_to_table" value="Add User Login Fields to Table" onClick="AddFieldsToTable();">
    </td>
    <td align="right"> Message Page: </td>
     <td>
    <select name="welcome_message_page" id="welcome_message_page">
      <option>-not selected-</option>
<?php
	$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data WHERE page_name LIKE '%message%' ORDER BY page_name";
	$result = mysql_query($query, $bwb_link);
	while($row = mysql_fetch_array($result))
	{
		$page_name = $row["page_name"];
		echo "<option value='" . $page_name . "'>" . $page_name . "</option>";
	}
?>
    </select>
    </td>
    <td>
    <input type="button" name="assign_message_page_to_user_table" id="assign_message_page_to_user_table" value="Assign Welcome Message Page to User Table" onClick="AssignWelcomeMessagePageToTable();">
    </td>
  </tr>
  </table>
  </td></tr></table>
  <br />
  
  <table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="4" class="sub_title">Access Levels</td>
  </tr>
  <tr>
    <td align="right">
    <input type="text" name="access_level_add" id="access_level_add" />&nbsp;
    </td>
    <td><input type="button" name="add_access_level" id="add_access_level" value="Add" onClick="AddRemoveAccessLevel('add');" /></td>
    <td align="right">
    <div id="AccessLevelRemoveDiv">
    <select name="access_level_remove" id="access_level_remove">
    <option>-not selected-</option>
	<option value="restricted">Restricted</option>
    <option value="user">User</option>
    <option value="power_user">Power User</option>
    <option value="admin">Admin</option>
    </select>
    </div>
    </td>
    <td><input type="button" name="remove_access_level" id="remove_access_level" value="Remove" onClick="DeleteAccessLevelConfirm();" /></td>
  </tr>
  </table>
  </td></tr></table>
  <br />
  <hr style="width:80%;" />
  <p class="small_message">Users are typically added through the form(s) created for the website.  The forms below are included to allow addition of users through the Breeze Website Builder backend by site administrators and power users.</p>
  
<table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
<form name="formAddUser" method="post" action="add_user.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="10" class="sub_title">Add User</td>
  </tr>
  <tr>
    <td align="right">User Name: </td>
    <td><input type="text" name="user_name"></td>
    <td align="right">Password: </td>
    <td> <input type="text" name="password" id="password_new_user"></td>
    <td align="right">Access Level: </td>
    <td>
    <div id="AccessLevelAddUserDiv">
    <select name="selectAccessLevel" id="selectAccessLevel">
	<option value="restricted">Restricted</option>
    <option value="user">User</option>
    <option value="power_user" selected>Power User</option>
    <option value="admin">Admin</option>
    </select>
    </div>
    </td>
    <td align="right">Email: </td>
    <td><input type="text" name="email"></td>
    <td align="right">Table: </td>
    <td>
    <select name="table" id="table_add_user" onChange="RetrieveUserLists(this.value);">
    <option value="-not selected-">-not selected-</option>
<?php
	for ($i = 0; $i < $table_cnt; $i++)
	{
		if ($tables[$i] == $last_user_table)
			echo "<option value='" . $tables[$i] . "' selected='selected'>" . $tables[$i] . "</option>";
		else
			echo "<option value='" . $tables[$i] . "'>" . $tables[$i] . "</option>";
	}
?>
    </select>
    </td>
  </tr>
  <tr>
  <td colspan="3">&nbsp;</td>
	<td>
	<input type="button" name="generate_password" value="Generate Password" onClick="GeneratePassword('new');">
	</td>
    <td colspan="6">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="10" align="center"><input type="submit" name="Submit" value="Add"></td>
  </tr>
  </table>
  </form>
  </td></tr></table>
  <br>
  <table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>
  <form name="formChangePassword" method="post" action="change_password.php">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="6" class="sub_title">Change Password</td>
  </tr>
  <tr>
    <td align="right">Table: </td>
    <td>
    <select name="table" id="table" onChange="RetrieveUserLists(this.value);">
    <option value="-not selected-">-not selected-</option>
<?php
	for ($i = 0; $i < $table_cnt; $i++)
	{
		if ($tables[$i] == $last_user_table)
			echo "<option value='" . $tables[$i] . "' selected='selected'>" . $tables[$i] . "</option>";
		else
			echo "<option value='" . $tables[$i] . "'>" . $tables[$i] . "</option>";
	}
?>
    </select>
    </td>
    <td align="right">User Name: </td>
    <td>
    <div id="UsersChangePassword">
    <select name="selectUserName" id="selectUserName">
    </select>
    </div>
    </td>
    <td align="right">New Password: </td>
    <td>
    	<input type="text" name="password" id="password_existing_user">
		<input type="button" name="generate_password2" value="Generate Password"  onClick="GeneratePassword('existing');" />
	</td>
  </tr>
  <tr><td colspan="6">&nbsp;</td></tr>
  <tr>
    <td colspan="6" align="center"><input type="submit" name="Submit" value="Change Password"></td>
  </tr>
  </table>
  </form>
  </td></tr></table>
  <br>
  <table width="80%" border="1" cellspacing="0" cellpadding="5"><tr><td>

  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="6" class="sub_title">Delete User</td>
  </tr>
  <tr>
  	<td align="right">Table: </td>
    <td>
    <select name="table" id="table_delete_user" onChange="RetrieveUserLists(this.value);">
    <option value="-not selected-">-not selected-</option>
<?php
	for ($i = 0; $i < $table_cnt; $i++)
	{
		if ($tables[$i] == $last_user_table)
			echo "<option value='" . $tables[$i] . "' selected='selected'>" . $tables[$i] . "</option>";
		else
			echo "<option value='" . $tables[$i] . "'>" . $tables[$i] . "</option>";
	}
?>
    </select>
    </td>
    <td align="right">User Name: </td>
    <td>
    <div id="UsersDelete">
    <select name="selectUserNameDelete" id="selectUserNameDelete">
    </select>
    </div>
    </td>
	<td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr><td colspan="6">&nbsp;</td></tr>
  <tr>
    <td colspan="6" align="center"><input type="button" name="delete" value="Delete" onClick="DeleteUserConfirm();"></td>
  </tr>
</table>

  </td></tr></table>
  <div id="StatusMsg2">&nbsp;</div>
  <table width="80%" border="0" cellspacing="0" cellpadding="0">
  <tr>
  <td align="center">
  <input type="button" name="Back" value="Back to Main Menu" onClick="window.location.href='main.php?mode=admin';">
  </td>
  <td valign="top" align="right" width="10%">
      <div id="help_button_sm" onClick="OpenHelpWindow();">
        &nbsp;
      </div>
  </td>
  </tr>
</table>
</div>

<div id="delete_confirm_user">
<table border="0" cellspacing="0" cellpadding="5" align="center">
  <tr>
    <td colspan="2">Are your sure you want to delete the user <b><div id="delete_text_user" style="display:inline; font-weight:bold;">&nbsp;</div></b>?</td>
  </tr>
  <tr>
	<td align="center"><input type="button" name="delete" id="delete" value="Yes" onClick="DeleteUser();" /></td>
	<td align="center"><input type="button" name="hide_delete" value="No" onClick="HideUserDelete();" />
    </td>
  </tr>
</table>
</div>

<div id="delete_confirm_access_level">
<table border="0" cellspacing="0" cellpadding="5" align="center">
  <tr>
    <td colspan="2">Are your sure you want to delete the access level <b><div id="delete_text_access_level" style="display:inline; font-weight:bold;">&nbsp;</div></b>?</td>
  </tr>
  <tr>
	<td align="center"><input type="button" name="delete" id="delete" value="Yes" onClick="AddRemoveAccessLevel('remove');" /></td>
	<td align="center"><input type="button" name="hide_delete" value="No" onClick="HideAccessLevelDelete();" />
    </td>
  </tr>
</table>
</div>
<script language='javascript' type='text/javascript'>
   RetrievePages();
   RetrieveAccessLevels(0);
   if (last_user_table != "")
   	RetrieveUserLists(last_user_table);
</script>
</body>
</html>
