<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., 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.
//
	include ("check_session.php");
?>
<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">

<script type="text/javascript">
function dropdown(sel){
	var opt_val=sel.options[sel.selectedIndex].value;
	var opt_name=sel.name;
    location.href="options.php?option_value=" + opt_val + "&option_name=" + opt_name;
}

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

<body>
<br>
<form name="frmCreatePage" method="post" action="save_options.php">
<div align="center">
	<table>
	<tr bgcolor='#CCCCCC'><td class="sub_title">Option</td><td class="sub_title">Value</td><td>&nbsp;</td></tr>
<?php
//	include "../connect/db_connection.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 = mysql_query($query, $bwb_link);
	$num_results = mysql_num_rows($result);
	for ($i = 0; $i < $num_results; $i++)
	{	
		//Get record from options table with all possible options.
		$row = mysql_fetch_array($result);
//		$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 = mysql_query($query, $bwb_link);
	$num_results = mysql_num_rows($result);
	$last_option_name = "not_set";
	for ($i = 0; $i < $num_results; $i++)
	{	
		//Get record from options table with all possible options.
		$row = mysql_fetch_array($result);
		$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 = mysql_query($query2, $bwb_link);
		$row2 = mysql_fetch_array($result2);
		$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">
  		<input type="submit" name="Submit" value="Submit" />
  	</td>
	<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">
      <div id="help_button" onClick="OpenHelpWindow();">
        &nbsp;
      </div>
  	</td>
	</tr>
  </table>
</div>
</form>

</body>
</html>
