#!/usr/bin/perl -T
use warnings;
use strict;
use diagnostics;
use CGI qw(:standard);
use DBI;
use Email::Valid;
BEGIN {
	$|=1;
	use CGI::Carp('fatalsToBrowser');
}
delete @ENV { 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

my @user; #Here @user deals with: first name, last name, username, password, CLSCC Email, and Student Number
my @errors;
my $dbh;

sub db_connect {
	use constant username => 'AmeriVista';
	use constant password => 'secret';
	my $database = 'AmeriVista';
	my $server = 'localhost';

	my $dsn = "DBI:mysql:database=$database;host=$server;port=3306" || die "Couldn't Connect to the Database: $!";
	my $dbh = DBI->connect($dsn, username, password, {RaiseError => 1}) || die "couldn't authenticate to the Database: $!";
}

db_connect ();
print header;
print start_html (-title=>"AmeriVista Event Logging",
			-author=>'vendion@vendion.net');
print "<h1>Registration Form</h1>\n";
print "<hr>\n";

if (param) {
	form_verify (@user);
} else {
	print start_form;
	print_form ();
	print end_form, "\n";
}

sub form_verify (@user) {
	$user[0] = param('FirstName');
	if ( $user[0] =~ /^([-\w.]+)$/ )  {
		$user[0] = $1;
	} else {
		push @errors, "<p>First Names should only contain letters</p>\n";
	}
	$user[1] = param('LastName');
	if ( $user[1] =~ /^([-\@\w.]+)$/ ) {
		$user[1] = $1;
	} else {
		push @errors, "<p>Last Name Should Only Contain Letters</p>\n";
	}
	$user[2] = param('Username');
	if ( $user[2] =~ /^([-\@\w.]+)$/ ) {
		$user[2] = $1;
	} else {
		push @errors, "<p>Usernames Should Only Contain Letters and Numbers</p>\n";
	}
	$user[3] = param('Password1');
	if ( $user[3] =~ /^([-\@\w.]+)$/ ) {
		if ( length ( $user[3] ) eq '6' ) {
			$user[3] = $1;
		} else {
			push @errors, "<p>The password is to short, it should be atleast 6 charaters long</p>\n";
		}
	} else {
		push @errors, "<p>The Password Should only Contain alphanumeric characters!\n";;
	}
	$user[4] = param('Password2');
	if ( $user[4] =~ /^([-\@\w.]+)$/ ) {
		if ( length ( $user[4] eq '6' ) ) {
				$user[4] = $1;
			} else {
				push @errors, "<p>The Password is to Short, it should be at least 6 charaters long</p>\n";
			}
	} else {
		push @errors, "<p>The Password Should only Contain alphanumeric characters!\n";
	}
	if ( $user[3] != $user[4] ) {
		push @errors, "<p>Error: Passwords do not match!</p>\n";
	}
	$user[5] = param('Email');
	if ( $user[5] =~ /^([-\@\w.]+)$/ ) {
		$user[5] = $1;
		eval {
			my $GoodMail = Email::Valid->address( -address => "$user[5]", -mxcheck => 1);
			return;
		}
		#push @errors, "<p>Error: Double check your email address</p>" if $@;
		$user[5] = $GoodMail;
	} else {
		push @errors, "<p>Incorrect Email given</p>\n";
	}
	$user[6] = param('studentid');
	if ( $user[6] =~ /^([-\@\w.]+)$/ ) {
		$user[6] = $1;
	} else {
		die "Incorrect studentid given";
	}
	if ( @errors ) {
		print_form (@errors);
	} else {
		return @user;
	}
}

sub print_form {
	my @errors = @_;
	print "<div align='center'>\n";
	print "<table width='25%' border=1 summary='Register'>\n";
	print "<td align='left' valign='middle'>\n";
	if ( @errors ) {
		while ( @errors ) {
			print $_, "\n";
			print "<br>\n";
		}
	}
	print "<p>Registration</p>\n";
	print "<p>First Name: ", textfield(-name=>'FirstName',
					-maxlength=>120), "\n";
	print "<br>\n";
	print "Last Name: ", textfield(-name=>'LastName',
				-maxlength=>120), "\n";
	print "<br>\n";
	print "Username: ", textfield(-name=>'Username',
					-maxlenght=>120), "\n";
	print "<br>\n";
	print "Password: ", password_field(-name=>'Password1',
					-maxlength=>120), "\n";
	print "<br>\n";
	print "Repeat Password: ", password_field(-name=>'Password2',
						-maxlength=>120), "\n";
	print "<br>\n";
	print "CLSCC Email: ", textfield(-name=>'Email',
					-maxlenght=>120), "\n";
	print "<br>\n";
	print "Student ID #: ", textfield(-name=>'studentid',
					-maxlength=>120), "</p>";
	print submit(-name=>'Submit_Form',
			-value=>'Submit');
	print reset, "\n";
	print "</td>\n";
	print "</table>\n";
	print "</div>\n";
}
