<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 11/13/2008	ELL
// Added data type handling for text boxes as well as drop downs.
// Revised: 5/28/2009	ELL
// Added sorting of templates.	
// Revised:  6/17/2009	ELL
// Added third type 2 for checkboxes.  This will first be used for option to force page names to lower case.
// Revised: 8/21/2009	ELL
// Fixed bug with images showing up on line after they are supposed to show up.
// Revised: 2/8/2012	ELL
// Added table prefix handling.
// Revised: 12/13/2013	ELL
// Added refresh of main menu to update buttons and set to last selections.
//
	include ("check_session.php");
	
	$options_saved = $_REQUEST['options_saved'];
?>
<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">
#save_options_btn	{	width:110px;	}
</style>

<script language="javascript" type="text/javascript" src="js/browser_detect.js"></script>
<script language="javascript" type="text/javascript" src="js/mouse_position.js"></script>
<script language="javascript" type="text/javascript" src="js/popup_msg.js"></script>
<script language="javascript" type="text/javascript" src="js/options.js"></script>
</head>

<body>
<br>
<form name="frmCreatePage" id="frmCreatePage" method="post" action="save_options.php">
<div align="center">

	<table width="80%" border="0"><tr><td>
	<div class="button_heading_sub_level_1"><div class="button_heading_text_sub_level_1">Change Site Options</div><div class="button_heading_help" onClick="OpenHelpWindow();">&nbsp;</div></div>
    </td></tr></table>
    
	<table width="80%" class="layout_box_table">
	<tr bgcolor='#CCCCCC'><td class="sub_title">Option</td><td class="sub_title">Value</td><td>&nbsp;</td></tr>
<?php
	
	//First, create image style list for images in dropdown.
//	echo "<style type=text/css>\n";
//	echo "<!--\n";
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "options` ORDER BY `option_name`, `sort_order`";
	$result = $bwb_dbh->query($query);
	$num_results = $result->rowCount();
	for ($i = 0; $i < $num_results; $i++)
	{	
		//Get record from options table with all possible options.
		$row = $result->fetch(PDO::FETCH_ASSOC);
//		$option_image = $row["option_image"];
		$option_image[$i] = $row["option_image"];
//		echo ".img$i { width: 93px; height: 100px; background: url('images/$option_image'); }\n";
	}
//	echo "-->\n";
//	echo "</style>\n";
	
	//Open options table and get names, strings and all possible values for each option.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "options` ORDER BY `option_name`, `sort_order`";
	$result = $bwb_dbh->query($query);
	$num_results = $result->rowCount();
	$last_option_name = "not_set";
	for ($i = 0; $i < $num_results; $i++)
	{	
		//Get record from options table with all possible options.
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$option_name = $row["option_name"];
		$option_string = $row["option_string"];
		$option_value = $row["option_value"];
		$data_type = $row["data_type"];
		
		//If this is the 1st time to see this option, write table row and put option string in first cell and dropdown list in 2nd.
		if ($option_name != $last_option_name)
		{
			//If this is the last possible option value for this option, the close drop-down list and finish row.
			if ($last_option_name != "not_set")
			{
				echo "   </select>\n";
				if ($option_image[$k])
				{
					echo "<td><img src='images/$option_image[$k]'></td></td></tr>\n";
					$option_image[$k] = "";
				}
				else
					echo "<td>&nbsp;</td></td></tr>\n";
			}
			if ($bgclr == "#DDDDDD")
				$bgclr = "#EEEEEE";
			else
				$bgclr = "#DDDDDD";
			//After finishing last row start new row.
			echo "<tr bgcolor='$bgclr'><td>\n";
			echo "   $option_string:\n";
			echo "</td><td>\n";
			if ($data_type == 0)
				echo "   <select name='$option_name' onChange=\"return dropdown(this)\">\n";
		}
		
		//Find selected option value from options selected table.
		$query2 = "SELECT * FROM `" . BWB_TABLE_PREFIX . "options_selected` WHERE `option_name` = '$option_name'";
		$result2 = $bwb_dbh->query($query2);
		$row2 = $result2->fetch(PDO::FETCH_ASSOC);
		$option_value_selected = $row2["option_value"];
		
		if ($data_type == 1)
			echo "   <input name='$option_name' type='text' value='$option_value_selected' />";
		elseif ($data_type == 2)
		{
			if ($option_value_selected == 1)
				echo "   <input name='$option_name' type='checkbox' value='1' checked='checked' />";
			else
				echo "   <input name='$option_name' type='checkbox' value='1' />";
		}
		elseif ($data_type == 0)
		{
			//If coming from select change instead of initial opening of this page, then look for last selected option instead.
			if ($_GET['option_name'] == $option_name)
			{
				$option_value_onchange = $_GET['option_value'];
	//			$option_name_onchange = $_GET['option_name'];
				//Add option value. If option value is the same as option value selected, the set as selected.
				if ($option_value == $option_value_onchange)
				{
					echo "      <option value='$option_value' selected>$option_value</option>\n";
					$k = $i;
				}
				else
					echo "      <option value='$option_value'>$option_value</option>\n";
				}
			// Else compare selected option to option from possible options in table.
			else
			{
				//Add option value. If option value is the same as option value selected, the set as selected.
				if ($option_value == $option_value_selected)
				{
		//			echo "      <option class='img$i' value='$option_value' selected>$option_value</option>\n";
					echo "      <option value='$option_value' selected>$option_value</option>\n";
					$k = $i;
				}
				else
		//			echo "      <option class='img$i' value='$option_value'>$option_value</option>\n";
					echo "      <option value='$option_value'>$option_value</option>\n";
			}
		}	//data type
		
		$last_option_name = $option_name;
    }
	
	if ($option_image[$k])
		echo "</td><td><img src='images/$option_image[$k]'></td></tr>\n";
	else
		echo "</td><td>&nbsp;</td></tr>\n";
?>
	<tr><td colspan="3">&nbsp;</td></tr>
	<tr><td align="center" colspan="3">
        <div class='button_console_save' id="save_options_btn" onClick="document.getElementById('frmCreatePage').submit();"><span class='button_console_text_w_icon'>Submit</span></div>
  	</td>
	</tr>
  </table>
</div>
</form>

<?php include("lib/popup_msg.html"); ?>

<script language="JavaScript"  type="text/javascript">
	HidePopupMsg();
<?php	
	if ($options_saved == 1)	{	
		$_SESSION['button_group'] = "other_functions";
		$_SESSION['button'] = "btn_users_site_options";
?>
	popup_msg.top_offset = 0;
	popup_msg.left_offset = 320;
	popup_msg.popup_msg_type = 1;
	popup_msg.popup_timeout = 3000;
	popup_msg.status_msg = "The options have been saved.";
	popup_msg.ShowPopupMsg();
	
	function RefreshMainMenu()
	{
		window.parent.location = "main.php";
	}
	
	var timer = setTimeout(function() { RefreshMainMenu(); }, 3000);
<?php	}	?>
</script>

</body>
</html>
