<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 6/3/2009
// Rezised: 6/29/2009	ELL
// Fixed bug of assignments corrupting templates that do not have all 4: top, bottom, left and right sections.
// Revised: 2/8/2012	ELL
// Added table prefix handling.  Added references to website files folder configurations.
	
	include ("check_session.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Breeze Website Builder</title>

<link rel="stylesheet" href="manager.css" type="text/css">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">

<style>
#help_button	{
	top:30px;
	right:20px;
}
</style>

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

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 OpenHelpWindow()
{
	window.open("http://breezewebsitebuilder.com/docs/cms/help.html#manage_special", "Help", "scrollbars=1,resizable=1,status=0,toolbar=0,menubar=0,location=0,height=700,width=800");
}

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

function DeletePage()
{
	var area_name = document.getElementById("area_name").value;
	document.getElementById("delete_confirm").style.display = "none";
	var url = "delete_special.php?area_name=" + area_name;
	window.location = url;
}

function HideDelete()
{
	document.getElementById("delete_confirm").style.display = "none";
}
</script>

</head>

<body>
<p align="center" class="title">Special Area Assignment</p>
<div id="help_button" onClick="OpenHelpWindow();">
&nbsp;
</div>
<table width="80%" border="1" align="center"><tr><td>
<span class="sub_title">Add Special Area</span>
<table width="100%" border="0" align="center">
<tr>
<td align="center">
<form name="frmAddRight" method="post" action="add_special.php">
<input type="submit" name="submit" value="Add Another Right Area" />
<input type="hidden" name="area_type" value="right" />
</form>
</td>
<td align="center">
<form name="frmAddLeft" method="post" action="add_special.php">
<input type="submit" name="submit" value="Add Another Left Area" />
<input type="hidden" name="area_type" value="left" />
</form>
</td>
<td align="center">
<form name="frmTop" method="post" action="add_special.php">
<input type="submit" name="submit" value="Add Another Top Area" />
<input type="hidden" name="area_type" value="top" />
</form>
</td>
<td align="center">
<form name="frmBottom" method="post" action="add_special.php">
<input type="submit" name="submit" value="Add Another Bottom Area" />
<input type="hidden" name="area_type" value="bottom" />
</form>
</td>
</tr>
</table>
</td></tr></table>
<br />

<table width="80%" border="1" align="center"><tr><td>
<table width="100%" border="0" align="center">
<tr bgcolor="#CCCCCC"><td colspan="6"><span class="sub_title">Assign Special Area to Page</span></td></tr>
  <?php
  		$bgcolor = "#EEEEEE";
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data";
		$result = mysql_query($query, $bwb_link);
		$i = 1;
		while($row = mysql_fetch_array($result))
		{
			$page_name = $row["page_name"];
			echo "<form name='frmAssign$i' method='post' action='assign_special.php'>\n";
			echo "<tr bgcolor='$bgcolor'>\n";
			echo "<td>$page_name</td>";
			
			/////////////////////////////////////////
			//Test to see if file already exists.
			$document_root = $_SERVER['DOCUMENT_ROOT'];
			if (BWB_BASE_DIR == '')
				$file_name = $document_root . "/" . BWB_WEBPAGE_DIR . "/" . $page_name . ".php";
			else
				$file_name = $document_root . "/" . BWB_BASE_DIR . "/" . BWB_WEBPAGE_DIR . "/" . $page_name . ".php";
			
			if (!file_exists($file_name))
			{
				echo "Error: $file_name does not exists.  Click <a href=\"manage_special.php\" title=\"here\">here</a> to return to the area assignment page.";
				exit;
			}
	
			// Read file.
			$fh = fopen($file_name, 'r') or die("Can't read $file_name: $php_errormsg");
			$contents = fread($fh, filesize($file_name));
			fclose($fh) or die($php_errormsg);
		
			$search_str_right = "page_name='right";
			$search_str_left = "page_name='left";
			$search_str_top = "page_name='top";
			$search_str_bottom = "page_name='bottom";
		
			$start_pos = strpos($contents, $search_str_right, 1);
			if ($start_pos !== false)
			{
				$end_pos = strpos($contents, "\"", $start_pos) - 1;
				$start_pos = $start_pos + strlen("page_name='");
				$right_str = substr($contents, $start_pos, $end_pos - $start_pos);
			}
			else
				$right_str = "-na-";
			
			$start_pos = strpos($contents, $search_str_left, 1);
			if ($start_pos !== false)
			{
				$end_pos = strpos($contents, "\"", $start_pos) - 1;
				$start_pos = $start_pos + strlen("page_name='");
				$left_str = substr($contents, $start_pos, $end_pos - $start_pos);
			}
			else
				$left_str = "-na-";
			
			$start_pos = strpos($contents, $search_str_top, 1);
			if ($start_pos !== false)
			{
				$end_pos = strpos($contents, "\"", $start_pos) - 1;
				$start_pos = $start_pos + strlen("page_name='");
				$top_str = substr($contents, $start_pos, $end_pos - $start_pos);
			}
			else
				$top_str = "-na-";
			
			$start_pos = strpos($contents, $search_str_bottom, 1);
			if ($start_pos !== false)
			{
				$end_pos = strpos($contents, "\"", $start_pos) - 1;
				$start_pos = $start_pos + strlen("page_name='");
				$bottom_str = substr($contents, $start_pos, $end_pos - $start_pos);
			}
			else
				$bottom_str = "-na-";
			////////////////////////////////////
			
			echo "<td>\n";
			if ($right_str != "-na-")
			{
				echo "<select name=\"right_area_name\">\n";
				$query2 = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special";
				$result2 = mysql_query($query2, $bwb_link);
				while($row2 = mysql_fetch_array($result2))
				{
					$area_name = $row2["page_name"];
					if ($area_name == $right_str)
						echo "<option value='$area_name' selected='selected'>$area_name</option>\n";
					elseif(substr($area_name,0,3) == "rig")
						echo "<option value='$area_name'>$area_name</option>\n";
				}
				echo "</select>\n";
			}
			else
				echo "-na-";
			echo "</td>\n";
			
			echo "<td>\n";
			if ($left_str != "-na-")
			{
				echo "<select name=\"left_area_name\">\n";
				$query2 = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special";
				$result2 = mysql_query($query2, $bwb_link);
				while($row2 = mysql_fetch_array($result2))
				{
					$area_name = $row2["page_name"];
					if ($area_name == $left_str)
						echo "<option value='$area_name' selected='selected'>$area_name</option>\n";
					elseif(substr($area_name,0,3) == "lef")
						echo "<option value='$area_name'>$area_name</option>\n";
				}
				echo "</select>\n";
			}
			else
				echo "-na-";
			echo "</td>\n";
			
			echo "<td>\n";
			if ($top_str != "-na-")
			{
				echo "<select name=\"top_area_name\">\n";
				$query2 = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special";
				$result2 = mysql_query($query2, $bwb_link);
				while($row2 = mysql_fetch_array($result2))
				{
					$area_name = $row2["page_name"];
					if ($area_name == $top_str)
						echo "<option value='$area_name' selected='selected'>$area_name</option>\n";
					elseif(substr($area_name,0,3) == "top")
						echo "<option value='$area_name'>$area_name</option>\n";
				}
				echo "</select>\n";
			}
			else
				echo "-na-";
			echo "</td>\n";
			
			echo "<td>\n";
			if ($bottom_str != "-na-")
			{
				echo "<select name=\"bottom_area_name\">\n";
				$query2 = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special";
				$result2 = mysql_query($query2, $bwb_link);
				while($row2 = mysql_fetch_array($result2))
				{
					$area_name = $row2["page_name"];
					if ($area_name == $bottom_str)
						echo "<option value='$area_name' selected='selected'>$area_name</option>\n";
					elseif(substr($area_name,0,3) == "bot")
						echo "<option value='$area_name'>$area_name</option>\n";
				}
						}
			else
				echo "-na-";
			echo "</select>\n";
			echo "</td>\n";
			
			echo "<td>\n";
			echo "<input type='submit' name='submit' value='Assign' />\n";
			echo "<input type='hidden' name='page_name' value='$page_name' />\n";
			echo "</td>\n";
			echo "</tr>\n";
			echo "</form>";
			$i++;
			if ($bgcolor == "#EEEEEE")
				$bgcolor = "#DDDDDD";
			else
				$bgcolor = "#EEEEEE";
		}
   ?>
</table>
</td></tr></table>
<br />
	<table width="80%" border="1" align="center"><tr><td>
    <span class="sub_title">Delete Special Area</span>
	<table width="100%" align="center" border="0"><tr>
	  <td align="center">Select the border area you want to delete: </td>
	  <td>
		<select name="area_name" id="area_name">
	<?php
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special";
		$result = mysql_query($query, $bwb_link);
		while($row = mysql_fetch_array($result))
		{
			$page_name = $row["page_name"];
			if (($page_name != "top") && ($page_name != "bottom") && ($page_name != "left") && ($page_name != "right"))
				echo "<option value='$page_name'>$page_name</option>";
		}
	?>
		</select>
	  </td>
	  <td align="center">
	  <input type="button" name="Submit" value="Delete" onclick="DeleteConfirm();">
	  </td>
	</tr></table>
    </td></tr></table>

<br />
<table width="80%" border="0" cellspacing="0" cellpadding="0">
  <tr><td align="right">
  <input type="button" name="Back" value="Back to Main Menu" onClick="window.location.href='main.php?mode=border';">
  </td></tr>
</table>

<div id="delete_confirm">
<table border="0" cellspacing="0" cellpadding="5" align="center">
  <tr>
    <td colspan="2">Are your sure you want to delete the <b><div id="delete_text">&nbsp;</div></b> page?</td>
  </tr>
  <tr>
	<td align="center"><input type="button" name="delete" id="delete" value="Yes" onClick="DeletePage();" /></td>
	<td align="center"><input type="button" name="hide_delete" value="No" onClick="HideDelete();" />
    </td>
  </tr>
</table>
</div>

</body>
</html>