<?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;
}

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>
");

// Display system defined static data
// In the following line 
// target = '_parent' ensures that the new window displayes in the whole browser and not just one frame
echo ("<div id='header'>");
echo ("<h4>Descriptive attributes");
if($debug)echo ("<script>  document.write(document.URL); </script>");
echo ("</h4></div>");
		
// Choose static section
$source_id = $_GET[source_id];
if($source_id != NULL && $source_id != "add"){
	$StaticDataPresent = true;
	$result = mysql_query("SELECT count(static_id) FROM Static WHERE source_id=".$source_id.";",$con);
	while($row = mysql_fetch_array($result)){
		if($row[0] == 0)$StaticDataPresent = false;
	}
	
	
	echo ("<form method='post'>");
	if($StaticDataPresent){
		echo ("<div id='content'>");
		echo ("<table width='100%'>");
		echo ("<tr><td>
						<button type='submit' {$StdRend} > <b> Tag </b> </button> 
					</td>
					<td>
						<button type='submit' {$StdRend} > <b> Value </b> </button> 
					</td>
				</tr>");
		
		$result = mysql_query("SELECT static_id,attribute,value FROM Static WHERE source_id=$source_id
								ORDER BY FIELD(attribute,'name') DESC",$con); // so that name is always at top
		while($row = mysql_fetch_array($result)){
			if($_GET[delete] == $row[0]){
				mysql_query("DELETE FROM Static WHERE static_id=".$row[0],$con);
			}else{
				if($row[1] == "name")$name_id = $row[0];
				if($row[1] == "name")continue;
				echo ("<tr>");
				//if($_POST[selectedItem2] == $row[0] || $source_id == $row[0] ){
				if($_POST[selectedItem2] == $row[0]){
					$static_id = $_POST[selectedItem2];
					$Render = $SelRend;
				}else{
					$Render = $NSelRend;
				}
				echo (	"<td>".
					"<button type='submit' {$Render} value= '" . $row[0] . "' name = 'selectedItem2'
					formaction='Static.php?source_id=$source_id&static_id=$static_id' > " .$row[1]. "</button>
					</td>");
				echo (	"<td>".
					"<button type='submit' {$Render} value= '" . $row[0] . "' name = 'selectedItem2'
					formaction='Static.php?source_id=$source_id&static_id=$static_id' > " .$row[2]. "</button>
					</td>");

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

	echo ("<div id='footer'>");
	echo ("<button class='btnAdd' type='submit' name='AddRecord' formaction='Static.php?source_id=$source_id'>+</button>");
	if($static_id != NULL && $static_id != $name_id){
		echo ("<button class='btnEdit' type='submit' name='EditRecord' formaction='Static.php?source_id=$source_id&PK=$_POST[selectedItem2]' style='float: right;'>Edit</button>");						
		echo ("<button class='btnDelete' type='submit' name='DeleteRecord' formaction='Static.php?source_id=$source_id&delete=$static_id'>-</button>");	
	}

	echo ("</div>");
	echo ("</form>");
} // end if block
echo ("<a id='#AddRecord' href= 'AddEditStatic.php?PK=add&FK={$source_id}' target='WorkingFrame'></a> "); 
echo ("<a id='#EditRecord' href='AddEditStatic.php?PK=$_GET[PK]&FK={$source_id}' target='WorkingFrame'></a> "); 

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>");
?>













