<?php

require '../Common/openDB.php';
$StdRend = $StandardRendering;
$SelRend = $SelectedRendering;
$NSelRend = $NotSelectedRendering;
$con = mysql_connect($website,$userid,$pw);
if (!$con)die('Could not connect: ' . mysql_error());
mysql_query("SET foreign_key_checks = 0",$con);

$curDB = "../CurrentlyOpenedDBName.tmp";
if(file_exists ($curDB)){
	$f = fopen($curDB, "r");
	$dbase   = trim(fgets($f)); // linked database name
	fclose($f);
	mysql_select_db($dbase, $con);
}else{
	//No database currently open
	return;
}

$selectionMade = false;

echo ("
<html>
<head>
	<link rel='stylesheet' type='text/css' href='../CSS/Font.css'>
	<link rel='stylesheet' type='text/css' href='../CSS/HeaderContentFooter.css'>
	<link rel='stylesheet' type='text/css' href='../CSS/Buttons.css'>

	<!-- ALternate lines have different colours -->
	<style type='text/css'>
      tr:nth-child(2n) {
             background-color: #f2f2f2;
        }
	</style>


</head>
<body>
	<div id='header'>
		<h4>Entities");
if($debug)echo ("<script>  document.write(document.URL); </script>");
echo ("</h4></div>");

// Choose source section
echo ("<form method='post'>");
echo ("<div id='content'>");
echo ("<table width='100%'>");
$source_id = $_POST[selectedItem];
if($source_id == NULL){
	$source_id = $_GET[source_id];
	$_POST[selectedItem] = $source_id;
}

$result = mysql_query("SELECT source_id,name FROM Source",$con);
while($row = mysql_fetch_array($result)){
	if($_GET[delete] == $row[0]){
		$resA = mysql_query("SELECT name FROM Source WHERE source_id = {$row[0]}",$con);
		while($rowA = mysql_fetch_array($resA))$nameS = $rowA[0];
		delTree("../../Inputs/{$dbase}/{$nameS}_{$row[0]}");
		mysql_query("DELETE FROM Source WHERE source_id=".$row[0],$con);
		mysql_query("DELETE FROM Static WHERE source_id=".$row[0],$con);
		mysql_query("DELETE FROM Dynamic WHERE source_id=".$row[0],$con);
		echo("<script> window.parent.location.reload();</script>");
	}else{
		echo ("<tr>");
		if($_POST[selectedItem] == $row[0]){
			$source_id = $_POST[selectedItem];
			$selectionMade = true;
			$Render = $SelRend;
		}else{
			$Render = $NSelRend;
		}
		echo (	"<td>".
					"<button type='submit' {$Render} value= '" . $row[0] . "' name = 'selectedItem'
					 formaction='Source.php?source_id=$_POST[selectedItem]' > " .$row[1]. "</button>
			</td>");

		echo ("</tr>");
	}
}
echo ("</table>");
echo ("</div>");

echo ("<div id='footer'>");
echo ("<button class='btnAdd' type='submit' name='AddRecord' formaction='Source.php?source_id=$_POST[selectedItem]'>+</button>");

if($source_id != NULL){
	echo ("<button class='btnEdit' type='submit' name='EditRecord' formaction='Source.php?source_id=$_POST[selectedItem]' style='float: right;'>Edit</button>");
	echo ("<button class='btnDelete' type='submit' name='DeleteRecord' formaction='Source.php?delete=$_POST[selectedItem]'>-</button>");
}

echo ("</div>");
echo ("</form>");

// This link reloads other frames if clicked,
// it cannot be clicked because there is no text between <a></a>
// but the js just below this will click it it $selectionMade
echo ("<a id='#my-link0' href='Dynamic.php?source_id=".$source_id."' target='DynamicFrame'></a> ");
echo ("<a id='#my-link1' href='../Common/BlankPage.html' target='WorkingFrame'></a> ");
echo ("<a id='#my-link2' href='Static.php?source_id=".$source_id."' target='StaticFrame'></a> ");
echo ("<a id='#AddRecord'  href='AddEditSource.php?source_id=add' target='WorkingFrame'></a> ");
echo ("<a id='#EditRecord' href='AddEditSource.php?source_id=$_POST[selectedItem]' target='WorkingFrame'></a> ");

if($selectionMade){
	if($_GET[loadothers] != "no"){
		echo("<script>
			document.getElementById('#my-link0').click();
			document.getElementById('#my-link1').click();
			document.getElementById('#my-link2').click();
		</script>");
	}
}
if(isset($_POST[AddRecord]))echo("<script>document.getElementById('#AddRecord').click();</script>");
if(isset($_POST[EditRecord]))echo("<script>document.getElementById('#EditRecord').click();</script>");

echo ("</body></html>");
?>
