<?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);
	//echo "Connected to " . $dbase . "<br/>";
}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'>
	
	<!-- ALternate lines have different colours -->
	<style type='text/css'>
      tr:nth-child(2n) {
             background-color: #FFEBCD;
        }
	</style>

</head>
<body>
");
$output_id = $_GET[output_id];
$frequency_id = $_GET[frequency_id];
$data_freq = "no";
if($_GET[data_freq] == "yes")$data_freq = "yes";
if($frequency_id != NULL && $frequency_id != "add")$_POST[selectedItem2] = $frequency_id;
$res = mysql_query("SELECT COUNT(frequency_id) FROM Frequency",$con);
while($row = mysql_fetch_array($res))$NumOutputs = $row[0];

echo ("<form method='post'>");
if($NumOutputs > 0){
	echo ("<div id='header'>");
	echo ("<h4>Available frequencies");
	if($debug)echo ("<script>  document.write(document.URL); </script>");
	echo ("</h4></div>");
	echo ("<div id='content'>");
	echo ("<table width='100%'>");

	$res = mysql_query("SELECT frequency_id,name FROM Frequency",$con);
	while($row = mysql_fetch_array($res)){
		
		// Do not display frequencies which start with dynamic_id because these are set when setting up dynamic data fetching
		if(substr($row[1],0,10) != "dynamic_id"){
			if($_GET[delete] != NULL){
				mysql_query("DELETE FROM Frequency WHERE frequency_id=".$_GET[delete],$con);
				echo ("<script>	window.opener.document.location.href ='Operation.php?output_id={$output_id}';</script>");
				echo ("<script> window.close() </script>");
				header("Location: Frequency.php?output_id={$output_id}");
	
			}else{
				echo ("<tr>");
				if($_POST[selectedItem2] == $row[0]){
					$frequency_id = $_POST[selectedItem2];
					$Render = $SelRend;
				}else{
					$Render = $NSelRend;
				}
				echo (	"<td>".
					"<button type='submit' {$Render} value= '" . $row[0] . "' name = 'selectedItem2'
					formaction='Frequency.php?output_id={$output_id}&frequency_id={$row[0]}&data_freq={$data_freq}' > " .$row[1]. "</button>
					</td>");
				echo ("</tr>");
			}
		}
	}
	echo("</table>");
}
echo "</div>";
echo ("<div id='footer'>");
echo ("<input type='submit' name='AddRecord' value='Add'/>");
if($frequency_id != NULL && $frequency_id != "add"){
	echo ("<input type='submit' name='UseRecord' value='Select' style='float: right;' formaction='Frequency.php?output_id={$output_id}&frequency_id={$_POST[selectedItem2]}&data_freq={$data_freq}'/>");
	echo ("<input type='submit' name='CancelRecord' value='Cancel' style='float: right;' formaction='Frequency.php?output_id={$output_id}&frequency_id={$_POST[selectedItem2]}&data_freq={$data_freq}'/>");
	echo ("<input type='submit' name='EditRecord' value='Edit' style='float: right;'
		formaction='Frequency.php?output_id={$output_id}&frequency_id={$_POST[selectedItem2]}&data_freq={$data_freq}'/>");
	echo "<button type='submit' value= '" . $row[0] . "' name = 'delete'
		formaction='Frequency.php?delete={$_POST[selectedItem2]}&output_id={$output_id}&data_freq={$data_freq}' >Delete</button>";
}
echo ("</div>");
echo ("</form>");

// Redirect execution based on JS
echo ("<a id='#AddRecord' href='#' onClick=\" window.open('AddEditFrequency.php?output_id={$output_id}&frequency_id=add&data_freq={$data_freq}','MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=yes,width=600,height=700') \"></a> "); 
echo ("<a id='#EditRecord' href='#' onClick=\" window.open('AddEditFrequency.php?output_id={$output_id}&frequency_id={$frequency_id}&data_freq={$data_freq}','MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=yes,width=600,height=700') \"></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[UseRecord])){
	if($data_freq == "yes"){
		mysql_query("UPDATE Output SET es_data_freq_id = {$frequency_id} WHERE output_id={$output_id}",$con);		
	}else{
		mysql_query("UPDATE Output SET frequency_id = {$frequency_id} WHERE output_id={$output_id}",$con);
	}
	echo ("<a id='GoBack' href='Operation.php?output_id={$output_id}' ' target='OperationFrame'></a> "); 
	echo("<script>document.getElementById('GoBack').click();</script>");
	echo ("<script> window.close() </script>");
}
if(isset($_POST[CancelRecord]))echo("<script>window.close()</script>");


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













