#!/opt/local/bin/perl

use HTML::Entities ();
use CGI;
use lib '/Library/WebServer/perl/lib';
use EduFileTwo;
use EduFileTwo::Util;

my $query = new CGI;

#my $ok_chars = '@.a-zA-Z0-9 ,-';
my $ok_chars = '%+\-0-9<=>A-Z_a-z{|}@.,';
foreach my $param_name ( $query->param ) {
    $_ = HTML::Entities::decode( $query->param($param_name) );
    $_ =~ s/[^$ok_chars]//go;
    $query->param($param_name,$_);
}

print $query->header;


#Declare some local variables to save typing.

EduFileTwo::Util::printMessage("Testing print message");
my $course_id = $query->param('course_id');
my $firstname = $query->param('first_name');
my $lastname = $query->param('last_name');
my $student_id = $query->param('student_id');
my $password = $query->param('password');
#EduFileTwo::Util::printMessage("Before password = $password");
my $password_confirm = $query->param('password_confirm');
#EduFileTwo::Util::printMessage("Before password_confirm = $password_confirm");
my $phone = $query->param('phone');
my $email = $query->param('email');

$student_id =~ s/-//g;
$student_id =~ s/ //g;
$password =~ tr/"%+\-0-9<=>A-Z_a-z{|}//cd;
$password_confirm =~ tr/"%+\-0-9<=>A-Z_a-z{|}//cd;
#EduFileTwo::Util::printMessage("After password = $password");
#EduFileTwo::Util::printMessage("After password_confirm = $password_confirm");

#Error Checking Statements
if ($firstname eq ""){

	EduFileTwo::Util::printHead("");
	&printMessage("Invalid Field", "The first name field was left blank. Please click back on your browser to try again.\n");
	EduFileTwo::Util::printFoot();
}
if ($lastname eq ""){

	EduFileTwo::Util::printHead("");
	&printMessage("Invalid Field", "The last name field was left blank. Please click back on your browser to try again.\n");
	EduFileTwo::Util::printFoot();
}
if ($student_id eq ""){

	EduFileTwo::Util::printHead("");
	&printMessage("Invalid Field", "The student_id field was left blank. Please click back on your browser to try again.\n");
	EduFileTwo::Util::printFoot();
}
if ($password eq ""){

	EduFileTwo::Util::printHead("");
	&printMessage("Invalid Field", "The password field was left blank. Please click back on your browser to try again.\n");
	EduFileTwo::Util::printFoot();
}
if ($password_confirm eq ""){

	EduFileTwo::Util::printHead("");
	&printMessage("Invalid Field", "The password_confirm field was left blank. Please click back on your browser to try again.\n");
	EduFileTwo::Util::printFoot();
}
if ($phone eq ""){

	EduFileTwo::Util::printHead("");
	&printMessage("Invalid Field", "The phone field was left blank. Please click back on your browser to try again.\n");
	EduFileTwo::Util::printFoot();
}
if ($email !~ /.+\@.+\..+/ || $email eq ""){

	EduFileTwo::Util::printHead("");
	&printMessage("Invalid Email", "The Email Address you provided was invalid. Please click back on your browser to try again.\n");
	EduFileTwo::Util::printFoot();
}
if($password ne $password_confirm)
{
	EduFileTwo::Util::printHead("");
	&printMessage("Passwords Don't Match", "The passwords you have entered don't match. Please click back on your browser and try again.\n");
	EduFileTwo::Util::printFoot();

}
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
my $date = "$year-$mon-$mday";

my $student = new EduFileTwo::Student();
$student->setLoginID($student_id);
$student->setPassword($password);
$student->setFirstName($firstname);
$student->setLastName($lastname);
$student->setPhoneNumber($phone);
$student->setEmailAddress($email);
$student->setCourseID($course_id);

if($student->exists())
{
	# Associate this student with the course ID and notify the prof.
	$student->addToCourse($course_id);
	EduFileTwo::Util::notifyInstructor($firstname, $lastname, $course_id);
	EduFileTwo::Util::printHead("");
	&printMessage("User Added To Course", "The user $student_id has been added to course $course_id. You are currently registered in another course with this system. Your student ID has been added to the course you have requested. However, you will not be able to use the system until your account has been enabled by your instructor.\n");
	EduFileTwo::Util::printFoot(); 
}
if(!$student->save())
{
	EduFileTwo::Util::printHead("");
	&printMessage("User Creation Failed", "The user $student_id could not be created.\n");
	EduFileTwo::Util::printFoot();

}

EduFileTwo::Util::notifyInstructor($firstname, $lastname, $course_id);

EduFileTwo::Util::printHead("");
&printMessage("User Created", "The user $firstname $lastname (ID: $student_id) has been successfully created. \n" .
				"A notification message has been sent to your professor to enable this account.\n");
EduFileTwo::Util::printFoot();


#Subroutines
sub printMessage{
	print "<h1>$_[0]</H1>";
	print "<p>$_[1]</p>";
}
