#!/usr/bin/perl -w

use CGI qw(:standard);

my $dotw = param("dotw");
my $day = param("day");
my $month = param("month");
my $location = param("location");
my $info = param("info");
my $contact = param("contact");

sub print_form() {
  print "<TABLE><TR><TD align=\"left\">";
  print start_form;
  print p("Datum (<I>date</I>): ", 
	  popup_menu("dotw",['maandag', 'dinsdag', 'woensdag', 'donderdag',
			     'vrijdag', 'zaterdag', 'zondag']),
	  popup_menu("day", [1..31]),
	  popup_menu("month", ['januari', 'februari', 'maart', 'april',
			       'mei', 'juni', 'juli', 'augustus', 'september',
			       'oktober', 'november', 'december']));
  print p("Locatie (<I>location</I>):<BR>", textfield("location","",60));
  print p("Concertgegevens / Deelnemers (<I>concert information / participants</I>)<BR>",
	  textarea("info","",10,60));
  print p("Adres / E-mail / Tel.nr. (<I>address / e-mail / tel.nr.</I>)<BR>",
	  textfield("contact","",60));
  print p(submit("Submit"));
  print end_form;
  print "<BR><BR>";
  print "e-mail: <A href=\"mailto:tputten\@koncon.nl\">&lt;tputten\@koncon.nl&gt;</A><BR>";
  print "</TD></TR></TABLE>";
}

print header;
open INFH, "/home/groups/q/qu/quest/htdocs/form/zangkalender_form.html" or die;
while (<INFH>) {
  if (/INSERT_FORM/) {  
    if ($dotw) {
      if($day and $month and $location and $info and $contact) {
	
	$mail = $dotw . " " . $day . " " . $month . "\n\n" . 
	  $location . "\n\n" . $info . "\n\n" . $contact;
	$mail_html =  $dotw . " " . $day . " " . $month . "<BR><BR>" . 
	  $location . "<BR><BR>" . $info . "<BR><BR>" . $contact;
	print "<TABLE><TR><TD>";
	print p("Bericht verzonden (<I>message sent</I>):<BR><BR> $mail_html");
	open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or
	  die "Can't fork for sendmail: $!\n";
	print SENDMAIL "From: zangkalender formulier <tputten\@koncon.nl>\n";
	print SENDMAIL "To: Thea van der Putten <tputten\@koncon.nl>\n";
	print SENDMAIL "Subject: informatie voor de zangkalender\n\n";
	print SENDMAIL $mail;
	close(SENDMAIL) or 
	  warn "sendmail didn't close nicely\n";
	print "</TD></TR></TABLE>";
	
      } else {
	
	print "<TABLE><TR><TD><FONT color=\"#FF0000\"><B>Graag alle velden invullen ";
	print "(<I>please fill in everything</I>):</B></FONT><BR><BR></TD></TR></TABLE>";
	print_form();
	
      }
    } else {
      print_form();
    }
  } else {
    print;
  }
}
close INFH or die;
