#!/usr/bin/perl

#------------------- standard CGI skeleton ----------------

$MAIN::rcs = ' $Id ';

use strict;
use CGI qw(all);
use CGI "shortcuts";
use Mail::Send;

# begin CGI

my ($q) = new CGI;

print $q->header(-type => 'text/html',
                 -status => '200 OK');
print $q->start_html(-title => 'Add a Contact' ,
     -head=> <<END_OF_HEADER,
<link href="http://www.paganlink.org/paganlink.css" rel="stylesheet" type="text/css" title="Paganlink Style Sheet">

<link href="http://www.paganlink.org/help.html" rel="help">
<link href="http://www.paganlink.org/culprits.html" rel="author">
END_OF_HEADER

-lang=>'en-GB');

$q->param('live') ? do_form($q) : print_form($q);

print $q->end_html;

exit 0;

#------------------- support routines ----------------------

sub do_form {
   my ($q) = shift @_;
   my ($recp) = "feorag\@paganlink.org";
   my ($from) = "feorag";
   my ($subj) = "CONTACTS: New Addition\n";
   my ($body) = "Originally from: " .
                $q->param("contact_name") .
                "\n" .
                $q->param("contact_email") .
                "\n\n" .
		"Country: " .
		$q->param("country") .
		"\n" .
		"Headline: " .
		$q->param("region_wanted") .
		"\n" .
                $q->param("content");
   my ($m) = new Mail::Send ;
   $m->subject($subj);
   $m->to($recp);
   my ($fh) = $m->open;
   print $fh $body;
   $fh->close ;

   print "<H2>Mail sent</H2>\n",
         "Your mail has been sent. Thank you.<P>";
   return;
}

sub print_form {

# Need to ask the Italian Lunatic what these next two lines are. I suspect
# I don't need the , "<P>\n" of the second one

   my ($q) = shift @_;
   print $q->startform, "<P>\n";
   
# open the file containing the html mail form. This makes life easier for me
# because I don't have to re-do it in Perl everytime I redesign the site!

open(FORM_BODY,"contact_header.html") or die "Can't open file! $!";

# reads each line in in turn, printing it. If line exists is true. When line
# runs out, is false, so exits while.
while(<FORM_BODY>) {
    print $_;
}
close FORM_BODY;
   print $q->endform;
   # print $q->hr();
   return;
}


