#!/usr/bin/perl -wT

# This is add-thread-form.pl, which creates a form for add-thread.pl

use strict;
use diagnostics;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;

use lib qw(/home/reuven/www/cgi-bin);
use ATFConstants;

# Remove buffering
$| = 1;

# ------------------------------------------------------------
# Create an instance of CGI
my $query = new CGI;

# Send a MIME header
print $query->header("text/html");

# Get the thread id, if it was set
my $thread_id = $query->param("keywords") || 0;

# ------------------------------------------------------------
# Connect to the database
my $dbh = 
    DBI->connect("DBI:mysql:$database:$server:$port",$username,$password);
die "DBI error from connect:", $DBI::errstr unless $dbh;

# ------------------------------------------------------------
# Create the form
print $query->start_html(-title => "Add a new discussion thread",
			 -bgcolor => "#FFFFFF");

print "<H1>Add a new discussion thread</H1>\n";

print "<P>This form allows you to add a new discussion thread.  Enter ";
print "a title, along with an introductory paragraph describing the ";
print "thread's purpose.  Then click on \"add thread\", which will create ";
print "the thread.</P>\n\n";

print "<Form method=\"POST\" action=\"/cgi-bin/add-thread.pl\">\n";
print "<Table>\n";

# Here is the form

# ------------------------------------------------------------
# Thread title
print "<TR>\n";
print "<TD>Enter a short title</TD>\n";
print "<TD><input type=\"text\" size=\"50\" name=\"subject\"></TD>\n";
print "</TR>\n\n";

# ------------------------------------------------------------
# Let the person enter their name
my $name = $query->cookie(-name => "name") || "";
print "<TR>\n";
print "<TD>Your name</TD>\n";
print "<TD><input type=\"text\" size=\"50\" ";
print "value=\"$name\" name=\"name\"></TD>\n";
print "</TR>\n\n";

# ------------------------------------------------------------
# Enter the e-mail address
my $email = $query->cookie(-name => "email") || "";
print "<TR>\n";
print "<TD>Your e-mail address</TD>\n";
print "<TD><input type=\"text\" size=\"50\" ";
print "value=\"$email\" name=\"email\"></TD>\n";
print "</TR>\n\n";

# ------------------------------------------------------------
# Allow them to enter the message
print "<TR>\n";
print "<TD>What is the thread's purpose?  Enter introductory remarks:</TD>\n";
print "<TD><textarea cols=\"60\" rows=\"20\" name=\"text\">";
print "HTML tags will be removed.\n\n";
print "Use blank lines to separate paragraphs.\n";
print "</textarea></TD>\n";
print "</TR>\n\n";

# Finish the table
print "</Table>\n";

# Produce a submit button
print '<input type="submit" value="Create thread">';

# Finish the HTML
print "</Form>\n";

# ------------------------------------------------------------
# Menu bar
print "<P>\n";

# Send the user to the thread list
print "<a href=\"/cgi-bin/list-threads.pl\">[View all threads]</a>\n";

# Send the user to the search
print "<a href=\"/atf/search-form.shtml\">[Search]</a>\n";

# Allow the user to create a new thread
print "<a href=\"/atf/add-thread-form.shtml\">[Add a new thread]</a>\n";

# Give a plug for the ATF home page
print "<a href=\"/atf/\">[ATF home]</a>\n";

print "</P>\n";

print $query->end_html;


# ------------------------------------------------------------
# Menu bar
print "<P>\n";

# Send the user to the thread list
print "<a href=\"/cgi-bin/list-threads.pl\">[View all threads]</a>\n";

# Send the user to the search
print "<a href=\"/atf/search-form.shtml\">[Search]</a>\n";

# Allow the user to create a new thread
print "<a href=\"/cgi-bin/add-thread-form.pl\">[Add a new thread]</a>\n";

# Give a plug for the ATF home page
print "<a href=\"/atf/\">[ATF home]</a>\n";

print "</P>\n";

print $query->end_html;

# Disconnect, even though it isn't really necessary
$dbh->disconnect;

