<?php
/*
	##################
	## MDF SWITCHES ##
	##################

1. If HighLevInfo.name = MDF 	has HighLevInfo.value = yes makes dynamic entities from data file (BAX/Eltek etc.)
															has HighLevInfo.value = no	does not make said dynamic entities
2. HighLevInfo.name = MDF_status 	has HighLevInfo.value = fet Display start/stop button as stop (i.e. DF is under way)
																	has HighLevInfo.value = not Display button as start

3. Dynamic.fetch_status = red/green create or not create crontab task when start is pressed
												= NA				value given to each MDF DID, when gives this value red is displayed and is not
																		not possible to change it

4. Dynamic.MDF_status	= NULL		This is normal DID
											= MDF		  This is an MDF DID and crontab entry will not be made for it when start is pressed
																(the associated MDF should fetch data for it)


*/
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);
	//echo "Connected to " . $dbase . "<br/>";
}else{
	//No database currently open
	return;
}

$selectionMade = false;
?>

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

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

</head>
<body>

<?php

echo ("<form method='post'>");
echo ("<div id='header'>");
//echo ("<h4>Available Multi Data Fetchers");
if($debug)echo ("<script>  document.write(document.URL); </script>");
echo ("</h4></div>");
echo ("<div id='content'>");
echo ("<table width='100%'>");
$res = mysql_query("SELECT fetcher_id,name FROM Fetcher",$con);
while($row = mysql_fetch_array($res)){
	// Only display fetchers which start with MDF
	if(substr($row[1],0,3) == "MDF"){
		$MDF_name = substr($row[1],3);
		if($_GET[delete] != NULL){
			mysql_query("DELETE FROM Fetcher WHERE fetcher_id=".$_GET[delete],$con);
			header("Location: MDF.php");
		}else{
			echo ("<tr>");
			if($_POST[selectedItem2] == $row[0]){
				$fetcher_id = $_POST[selectedItem2];
				$Render = $SelRend;
			}else{
				$Render = $NSelRend;
			}
			echo (	"<td>".
				"<button type='submit' {$Render} value= '" . $row[0] . "' name = 'selectedItem2'
				formaction='MDF.php?fetcher_id={$row[0]}' > {$MDF_name}</button>
				</td>");
			echo ("</tr>");
		}
	}
}
echo("</table>");

echo "</div>";
echo ("<div id='footer'>");
echo ("<input type='submit' name='AddRecord' value='Add'/>");
if($fetcher_id != NULL && $fetcher_id != "add"){
	echo ("<input type='submit' name='CancelRecord' value='Cancel' style='float: right;' formaction='MDF.php?fetcher_id={$_POST[selectedItem2]}'/>");
	echo ("<input type='submit' name='EditRecord' value='Edit' style='float: right;'     formaction='MDF.php?fetcher_id={$_POST[selectedItem2]}'/>");
	echo "<button type='submit' value= '" . $row[0] . "' name = 'delete'                 formaction='MDF.php?delete={$_POST[selectedItem2]}' >Delete</button>";
}
echo ("</div>");
echo ("</form>");

// Redirect execution based on JS
echo ("<a id='#AddRecord'  href='AddEditMDF.php?fetcher_id=add'></a> ");
echo ("<a id='#EditRecord' href='AddEditMDF.php?fetcher_id={$_GET[fetcher_id]}'></a> ");


if(isset($_POST[AddRecord]))echo("<script>document.getElementById('#AddRecord').click();</script>");
if(isset($_POST[EditRecord]))echo("<script>document.getElementById('#EditRecord').click();</script>");
if(isset($_POST[CancelRecord]))header("Location: ../Common/BlankPage.html");


?>
</body></html>
