#!/usr/bin/perl

use CGI qw(:standard);
use strict;
use DBI;
#use Mail::Sendmail;

my $adminemail = "test\@reack.com";
my ($valid, @errormsg);
my $username = CGI::param('username');
my $userpassword = CGI::param('userpassword');
my $userstreet = CGI::param('userstreet');
my $usercity = CGI::param('usercity');
my $userzip = CGI::param('userzip');
my $userstate = CGI::param('userstate');
my $userphone = CGI::param('userphone');
my $useremail = CGI::param('useremail');
my $usercctype = CGI::param('usercctype');
my $userccexpmo = CGI::param('userccexpmo');
my $userccexpyr = CGI::param('userccexpyr');
my $userccnum = CGI::param('userccnum');

my $C_name = cookie(-name => "username",
		    -value => "$username",
                    -path => "/",
                    -expires => "+6M");


if ($username eq "" or $userpassword eq "" or $userstreet eq "" or $usercity eq "" or $userzip eq "" or $userstate eq "" or $userphone eq "" or $useremail eq "" or $usercctype eq "" or $userccnum eq "" or $userccexpmo eq "" or $userccexpyr eq "") {
    push(@errormsg, "complete all items on the form.");
    create_error_page();
    exit;
}
else {
    ($userccnum, $userphone, $useremail) = clean_up_input();
}

($valid, @errormsg) = validate_input();
if ($valid eq "N") {
    create_error_page();
    exit;
}
else {
    print header(-cookie => $C_name);
    print "<html><body bgcolor=white text=black><center><img src='img/tcc.jpg'><br>Your order is confirmed. <a href='redirect.cgi'>Click here to continue</a></body></html>";
}

#*****user-defined functions*****

sub create_error_page {
    my $size;
    $size = @errormsg;
    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<HEAD><TITLE>Caffeine Consumption Cooperative</TITLE></HEAD>\n";
    print "<BODY>\n";
    print "<H2>Please return to the form and correct the following errors:</H2><BR>\n";
    for(my $x = 0; $x < $size; $x = $x + 1) {
	print "<H2>-- $errormsg[$x]</H2><BR>\n";
    }
    print "</BODY></HTML>\n";
} #end create_error_page


sub create_display_page {
    my (@records, $dcode, $fname);

    print "<HTML>\n";
    print "<HEAD><TITLE>The Caffeine Company<TITLE></HEAD>\n";
    print "<BODY>\n";
    print "<H1>The Caffeine Company.</H1>\n";
    print "<H2>You order has been received and will be processed shortly.</H2>\n";
    print "</BODY></HTML>\n";
} #end create_display_page



sub clean_up_input {
    my ($c, $p, $e);

    ($c, $p, $e) = ($userccnum, $userphone, $useremail); 

#remove leading and trailing spaces from credit card number
    $c =~ s/^ +//;
    $c =~ s/ +$//;
#remove leading and trailing spaces from phone
    $p =~ s/^ +//;
    $p =~ s/ +$//;
#remove leading and trailing spaces from email
    $e =~ s/^ +//;
    $e =~ s/ +$//;
#remove periods, commas, hyphens, etc.
    $c =~ tr/-.,//d;
    $p =~ tr/-.,//d;
#remove all spaces
    $c =~ tr/ //d;
    $p =~ tr/ //d;

    return $c, $p, $e;

} #end clean_up_input


sub validate_input {
    $valid = "Y";
    
#Not sure how to get the system date, so I just plugged in values to use for card expiration
    my $year = 2002;
    my $month = 12;


#I'm checking for the correct number of digits in the card number.
    if ($userccnum !~ m/[0-9]{16,16}/) {
	$valid = "N";
	push(@errormsg, "please check your credit card number and reenter.");
    }

#I'm checking to be sure they entered the correct starting number for a VISA.
    if ($userccnum !~ m/^4/ and $usercctype eq "Visa") {
	$valid = "N";
	push(@errormsg, "please check your credit card number and reenter.");
    }

#I'm checking to be sure they entered the correct starting number for a MasterCard.
    if ($userccnum !~ m/^5/ and $usercctype eq "MasterCard") {
	$valid = "N";
	push(@errormsg, "please check your credit card number and reenter.");
    }

#I'm checking for the expiration year
    if ($userccexpyr < $year) {
	$valid = "N";
	push(@errormsg, "please use a different credit card, this $usercctype is expired.");
    }

#I'm checking for the expiration month
    if ($userccexpyr == $year and $userccexpmo < $month) {
	$valid = "N";
	push(@errormsg, "please use a different credit card, this $usercctype is expired.");
    }

#I'm checking for the correct number of digits in the phone number
    if ($userphone !~ m/[0-9]{10,10}/) {
	$valid = "N";
	push (@errormsg, "please reenter your phone number including area code.");
    }

#Checking for a valid format of their email address
    if ($useremail !~ m/[\w\-]+\@[\w\-]+\.[\w\-]+/) {
	$valid = "N";
	push(@errormsg, "enter a valid e-mail address.");
    }
    return $valid, @errormsg;
} #end validate_input

my $error;

if ($valid == "N") {
    foreach $error (@errormsg) {
	print "$error<BR>";
    }
    exit;
}

my $msg = "
		You just received an order at caffeine shop.com\n
		This is the content of that order:\n
		Name: $username\n
                Street: $userstreet\n
                Zip: $userzip\n
                State: $userstate\n
                Phone: $userphone\n
                Email: $useremail\n
                CC Type: $usercctype\n
                CC Num: $userccnum\n
                CC Exp: $userccexpmo/$userccexpyr\n
		";

my %mail;
$mail{To} = $adminemail;
$mail{From} = "caffeine\@sotre.com";
$mail{Subject} = "Caffeine Order";
$mail{Smtp} = "localhost";
$mail{Message} = $msg;
sendmail(%mail);

#print "<html><body><center><img src='img/tcc.jpg'><br>Your order is confirmed yeehaw.</center></body></html>";