<?

LogIt("DEBUG","reports","Including reputils.php3...");
include "./reputils.php3";
LogIt("DEBUG","reports","back from including reputils.php3...");
	
if (! IsSet($repname)) {
	LogIt("DEBUG","reports","No report found, prompting showing reports.");
	?>
	<tr bgcolor=#000000>
		<td align=center colspan=3>
		<table bgcolor=#000000 cellspacing=2>
			<tr bgcolor=#bbbbbb valign=top>
			<td align=center colspan=4>
				<h3>Available reports 
			</td>
			</tr>
			<tr bgcolor=#bbbbbb valign=top>
				<th>Report</th>
				<th>Dataset</th>
				<th>Title</th>
				<th>Version</th>
			</tr>
			<?
				$found=0;
				$zzz = opendir("./reports/");
				while ($foo=readdir($zzz)) {
 	      	if (ereg('^.*\.rep',$foo)) {
						$found=1;
						$repinfo = ParseReport("./reports/$foo");
						if ($repinfo[dataset] != $reptable) {
							continue;
						}
						echo "<tr bgcolor=#808080>\n";
						echo "<td>";
						echo "<a href=\"nbrowse.php3?f_func=reports&reptable=$reptable&repname=$foo\">";
						echo "<img src=\"$sys_graphics/circle.gif\" border=0>$foo</a>";
						echo "</td>\n";
						echo "<td>$repinfo[dataset]</td>\n";
						echo "<td>$repinfo[title]</td>";
						echo "<td>$repinfo[version]</td>";
						echo "</tr>\n";
					}
				}
				if (! $found) {
					echo "<tr bgcolor=#808080><td colspan=4>No reports available.</td></tr>";
				}
			?>
		<tr bgcolor=bbbbbb valign=top>
			<td align=center colspan=4>
				<form method=post action="nbrowse.php3">
				<input type=submit value="Cancel">
			</td>
		</tr>
		</form>
		</table>
		</td>
	</tr>
	<?
} else {
	LogIt("DEBUG","reports","Running report $repname.");
	#
	# Okay, $repname holds the name of the report file.  We need to throw up
	# any variables required, plus their prompts.
	#
	?>
	<form method="post" action="runreports.php3" target="_blank">
	<input type=hidden name=repname value=<? echo $repname ?>>
	<tr bgcolor=#000000>
		<td align=center>
		<table bgcolor=#000000 cellspacing=2>
			<tr bgcolor=#bbbbbb valign=top>
			<?
				$repinfo = ParseReport("./reports/$repname");
				echo "<td align=center colspan=2>\n";
				echo "<h3>Running report '$repinfo[title]'\n";
				echo "</td>\n";
				echo "</tr>\n";
				if ($repinfo[vcounter]) {
					for ($i=1;$i<=$repinfo[vcounter];$i++) {
						echo "<tr bgcolor=#888888 valign=center>\n";
						$p = $repinfo[prompt][$i];
						echo "<td>$p</td>";
						if ($repinfo[vdtype][$i] == 'date') {
							echo "<td>";
							PromptDate("VAR$i",$repinfo[def][$i]);
							echo "</td>";
						} else {
							echo "<td><input type=text name=VAR$i value=\"".$repinfo[def][$i]."\" size=20></td>";
						}
						echo "</tr>\n";
					}
				} else {
					echo "<tr bgcolor=#888888 align=center>";
					echo "<td colspan=2 align=center>";
					echo "This report has no configurable options.";
					echo "</td>";
					echo "</tr>";
				}
			?>
 			<tr>
			<? # FIXME: needs maxsize based on database query ?>
			<td colspan=2>
			<table width=100% cellspacing=0 cellpadding=0><tr>
			<td align=left valign=top width=33%><input type="reset" value="Reset"></td>
			<td align=center valign=top width=33%><input type="submit" value="Run this report"></form></td>
			<td align=right valign=top width=33%><form method="post" action="nbrowse.php3?f_func=reports&reptable=<? echo $reptable ?>"><input type=submit value="Cancel"></form></td>
			</tr></table></td>
			</tr>
		</table>
		</td>
	</tr>
<? } ?>
