#!/usr/bin/perl
require "wiz_resources";
header("List Templates","","edit");

$tempdir="$serverroot/templates";

if ($user ne "") {
  $tablecount=-1;
  print <<EOF;
<form method="post" action="$default_cgi/edit">
<input type="hidden" name="action" value="settemplate">

<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td colspan="4" bgcolor="#999999"><img src="$image_dir/pad.gif" width="600" height="1" alt="-"></td>
</tr>

EOF

  opendir LIST, "$tempdir"
          or die("Unable to open directory $tempdir for listing");
  while ($file = readdir(LIST)) {
    if ((-d "$tempdir/$file") && ($file ne ".") && ($file ne "..") && (!($file =~ /~/)) && (!($file =~ /^_/)) ) {
      show_template($file);
    }
  }
  closedir LIST;
  if (($tablecount % 4) != 3) {
    print "</tr>\n";
    print "<tr><td colspan=\"4\" bgcolor=\"#999999\"><img src=\"$image_dir/pad.gif\" width=\"600\" height=\"1\" alt=\"-\"></td></tr>\n\n";
  }
  print "<tr><td colspan=\"4\" align=\"center\"><input type=\"submit\" value=\"continue\"></td></tr>\n\n";
  print "<tr><td colspan=\"4\" bgcolor=\"#999999\"><img src=\"$image_dir/pad.gif\" width=\"600\" height=\"1\" alt=\"-\"></td></tr>\n\n";
  print "</table></form>\n";

} else {
  headline("Error: Access denied","red","","You should not use this page unless you have logged into the system.  Please <a href=\"$default_cgi/login\">log in</a> and try again.");
}
footer();

sub show_template {
  $showtemp=$_[0];
  $tablecount++;
  
  $printname=ucfirst($showtemp);
  if (-e "$tempdir/$showtemp/defaults.txt") {
    open READ, "<$tempdir/$showtemp/defaults.txt";
    while ($line = <READ>) {
      if ($line =~ m~^name[\t| ]+(.*?)$~is) {
        $printname=$1;
      }
    }
    close READ;
  }
  

  if (($tablecount % 4) == 0) {
    print "<tr valign=\"top\" align=\"center\" cols=\"4\">\n";
  }
  print "<td width=\"25%\">";
  print "<b>$printname</b><br>\n";
  if (-e "$tempdir/$showtemp/thumb.gif") {
    print "<img src=\"http://$servername/$templatepos/$showtemp/thumb.gif\" alt=\"Thumbnail\"><br>\n";
  } else {
    print "<img src=\"http://$servername/$image_dir/noimage.gif\" alt=\"No thumbnail available\" width=\"150\" height=\"100\"><br>\n";
  }
  print "<input type=\"radio\" name=\"template\" value=\"$showtemp\"";
  print " checked" if ($showtemp eq $template);
  print ">\n";
  print "</td>";
  if (($tablecount % 4) == 3) {
    print "</tr>\n\n";
    print "<tr><td colspan=\"4\" bgcolor=\"#999999\"><img src=\"http://$servername/$image_dir/pad.gif\" width=\"600\" height=\"1\" alt=\"-\"></td></tr>\n\n";
  }
}
