#!/usr/bin/perl -T
use warnings;
use strict;
use diagnostics; #Comment out after testing
use CGI qw(:standard);
use Email::Valid;
#use Email::LocalDelivery;
BEGIN {
	$|=1;
	use CGI::Carp('fatalsToBrowser');
}

#Define variables
my $name; #Used to store the commenters name
my $email; #Stores the unchecked email address
my $comment; #stores the message
my $addr; #Stores the correct email address
$ENV{PATH} = '/usr/bin';

print header;
print start_html (-title=>"Contact Me",
			-style=>{'src'=>'/se.css'});
print '<div id="topsection">', "\n";
print "<h1>Contact Me</h1>\n";
print "<hr>\n";
print '</div>', "\n";

print '<div id="leftcolumn">', "\n";
print '<img src="/banner.jpg" alt="Banner" height="200" width="168" border="2">', "\n";
print "<hr>\n";
print "<h5>Some Navigation:</h5>\n";
print <<ENDMENU;
<ul>
			<li><a href="/index.html" title="Home Page">Home Page</a></li>
			<li><a href="/personal.html" title="Personal page">Personal Page</a></li>
			<li><a href="/projects.html" title="My Projects">My Projects</a></li>
			<li><a href="/resume.html" title="My Resume">My Resume</a></li>
			<li><a href="/cgi-bin/guestbook" title="Guestbook">Guestbook</a></li>
		</ul>
		<h5>Links:</h5>
		<!--- Links to other sites go here -->
		<ul>
			<LI><a href="http://www.thinkgeek.com" title="Think Geek">Think Geek</a></LI>
			<li><a href="http://www.perl.org" title="Perl">Perl</a></li>
			<li><a href="http://www.gaiaonline.com" title="Gaia Online">Gaia Online</a></li>
			<li><a href="http://www.whona.com">Whona</a></li>
			<li><a href="http://www.chugalug.org">CHUGALUG</a></li>
		</ul>
		<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
			<input type="hidden" name="cmd" value="_s-xclick">
			<input type="hidden" name="hosted_button_id" value="1552757">
			<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="">
			<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
		</form>
ENDMENU
print '</div>';

print '<div id="contentcolumn">'; 
if (param) {
	$name = param('Name');
	if ($name =~ /^([-\@\w.]+)$/) {
		$name = $1;
	};
	$email = param('Email');
	if ($email =~ /^([-\@\w.]+)$/) {
		$email = $1;
	};
	eval {
		$addr = Email::Valid->address( -address => "$email",
		-mxcheck => 1);
	};
	#Stop if a incorrect email address found
	unless ($addr) {
		print "<p><strong>Invalid Address ($email)</strong></p>\n";
		print '<p>Please <a href="/cgi-bin/contact" title="Go Back">go back</a> and try again.</p>';
		print '</div>', "\n";
		print '<div id="footer">', "\n";
		print <<ENDFOOT;
			<p><a href="http://validator.w3.org/check?uri=referer"><img
        		src="http://www.w3.org/Icons/valid-html401-blue"
        		alt="Valid HTML 4.01 Transitional" height="31" width="88"></a>
        		<a href="http://www.anybrowser.org/campaign/"><img src="/ab-white.gif" width="88" height="31" alt="Viewable With Any Browser" ></a>
        		<a href="http://www.apache.org"><img src="/apache_pb22_ani.gif" alt="Hosted with Apache web server"></a>
        		<a href="http://www.opensuse.org"><img src="/geeko.gif" alt="Hosted on openSUSE"></a>
        		<br>
        		2008 &copy; vendion</p>
ENDFOOT
		print end_html;
		exit 1;
	}
	#If a correct email address is given then continue with the work	
	$comment = param('Message');
	if ($comment =~ /^([-\@\w.]+)$/) {
		$comment = $1;
	};
	open(MAIL, "|/usr/bin/mail -s \"User, $name, has left a message\" vendion") || die "Can't send mail: $!"; #Find a way to keep this with -T switch active
	print MAIL "Name: $name\n";
	print MAIL "Email: $addr\n";
	print MAIL "Message: $comment\n";
	close (MAIL);
	print "<p>Your message has been sent.</p>\n";
} else {
	#start new form if no data is received
	print start_form;
	print "<p><strong>Name: </strong>", textfield(-name=>'Name',
				-maxlength=>120), "\n";
	print "<br>\n";
	print "<strong>Email: </strong>", textfield(-name=>'Email',
			-maxlength=>120), "\n";
	print "(This will only be used to reply to you, it will not be given out to anyone)\n";
	print "<br>\n";
	print "<strong>Message: </strong>\n";
	print "<br>\n";
	print textarea(-name=>'Message',
		-rows=>20,
		-columns=>80), "</p>\n";
	print "<br>\n";
	print '<div align="right">', submit(-name=>'Contact_Me',
		-value=>'Contact Me');
	print reset, '</div>', "\n";
	print end_form, "\n";
	print '</div>', "\n";
}

print '<div id="footer">', "\n";
print <<ENDFOOT;
<p><a href="http://validator.w3.org/check?uri=referer"><img
	src="http://www.w3.org/Icons/valid-html401-blue"
	alt="Valid HTML 4.01 Transitional" height="31" width="88"></a>
	<a href="http://www.anybrowser.org/campaign/"><img src="/ab-white.gif" width="88" height="31" alt="Viewable With Any Browser" ></a>
	<a href="http://www.apache.org"><img src="/apache_pb22_ani.gif" alt="Hosted with Apache web server"></a>
	<a href="http://www.opensuse.org"><img src="/geeko.gif" alt="Hosted on openSUSE"></a>
	<br>
	2008 &copy; vendion</p>
ENDFOOT
print '</div', "\n";
print end_html, "\n";
