#!/usr/bin/perl -w
#
# script for newsletter

use strict;
use CGI_Lite;
use Mail::Sendmail;

######################## CONFIGURATION ########################
my $smtp_server = 'localhost';
my $email_subject = "ShopJesusFish Newsletter Signup";
my $email_address = "amanda\@consumerbuilding.com";
my $results_file = "/home/abesmer/shopjesusfish_data/newsletter.out";

# The hidden fields printform and printresults are puposely left out 
# of this list because this data does not need to be collected.
my @questions = qw(	firstname
			lastname
			email
			zipcode
			participate_online_survey
);

my @required = qw(	firstname
			email
);
###############################################################

my @errors;
 
# Parse form data
my $cgi = new CGI_Lite;
my %formdata = $cgi->parse_form_data();

&print_header;

# Results Page
if ($formdata{'page'} == 2) {
	&check_required(\%formdata,\@required);

	# Create message to mail
	my $message = &create_message(\@questions,\%formdata);

	# Create the object to mail (it's a hash)
	my %mail = ( 	To      => $email_address,
			From    => 'newsletter@shopjesusfish.com',
      			Subject => $email_subject,
       			Message => $message
         		);

	# Send the message (or print a nasty error to the screen)
	sendmail(%mail) or push @errors, $Mail::Sendmail::error;

	# Write to file
	my $error = &print_to_file(\%formdata,\@questions,$results_file,$email_address);
print qq|
<div class="padded">
<p>
<b><h2>Sign up for an Exclusive<br>ShopJesusFish E-mail Newsletter!</h2>
</p>

<p>
Thank you for signing up for our newsletter!
</p>
</div>
|;
}

# Form Page
else {
print qq|
<div class="padded">
<p>
<b><h2>Sign up for an Exclusive<br>ShopJesusFish E-mail Newsletter!</h2></b>
</p>

<p>
Wanna get the dig about new stuff at ShopJesusFish? How about special
promotions, discount codes and other goodies exclusively for folks who get
our newsletter? Well then, you need to sign up to receive the
ShopJesusFish Super Fun Happy E-mail Newsletter!<br>
<br>
About once a month, we gather up our team of creative FishHeads and write
an entertaining e-mail for you detailing new and exciting products,
interesting tidbits of ShopJesusFish-related info, and special promotions.
You'll wonder how you ever lived without it!<br>
<br>
ShopJesusFish is dedicated to privacy, we will never ever release your
email address to third parties other then our featured partner, Thirsty?,
<a href="http://www.areuthirsty.com" target="blank_">www.areuthirsty.com</a> who we basically 
made a deal to share info, so we hope you understand.
</p>
<p>
<hr>
<b>Ready to sign up?</b><br>
Sign Up Here<br>
All fields marked with a red asterisk (<font color="#DD0000"><b>*</b></font>) are required.<br>
<br>
<form action="newsletter.pl" method="GET" name="thisform">
<input type="hidden" name="page" value="2">
<table cellspacing="0" cellpadding="0" border="0" width="60%">
	<tr>
		<td class="default" width="50%">
		First Name:
		</td>
		<td class="default" width="50%">
		<input type="text" size="25" maxlength="75" name="firstname"><font color="#DD0000"><b>*</b></font>
		</td>	
	</tr>
	<tr>
		<td class="default" width="50%">
		Last Name:
		</td>
		<td class="default" width="50%">
		<input type="text" size="25" maxlength="75" name="lastname">
		</td>	
	</tr>
	<tr>
		<td class="default" width="50%">
		Email:
		</td>
		<td class="default" width="50%">
		<input type="text" size="25" maxlength="75" name="email"><font color="#DD0000"><b>*</b></font>
		</td>	
	</tr>
	<tr>
		<td class="default" width="50%">
		Zip Code:
		</td>
		<td class="default" width="50%">
		<input type="text" size="5" maxlength="10" name="zipcode">
		</td>	
	</tr>
	<tr>
		<td class="default" width="50%">
		Would you be willing to help us improve ShopJesusFish.com by participating in an online survey?
		</td>
		<td class="default" width="50%">
		<input type="radio" name="participate_online_survey" value="yes">Yes<img src="spacer.gif" width="10" height="1"><input type="radio" name="participate_online_survey" value="no">No<br>

		</td>	
	</tr>
	<tr>
		<td class="default" colspan="2">
		<br><input type="submit" value="Submit">
		</td>
	</tr>
</table>
</form>	
</div>
|;			
}

&print_footer;

sub print_header {
     print "Content-type: text/html\n\n";
     open(HEAD, "header.inc") || print "Error opening template";
     while(<HEAD>) { print; }
     close HEAD;
}

sub print_footer {
     open(FOOT, "footer.inc") || print "Error opening template";
     while(<FOOT>) { print; }
     close FOOT;
}


sub check_required {
	my $data_ref = shift;
	my $req_ref = shift;

	my %data = %$data_ref;
	my @req = @$req_ref;
	
	my @empty;

	foreach my $item (@req) {
	#print "$item *$data{$item}*<br>";
		if ($data{$item} !~/\S/) {
			push @empty, $item;
		}
	}

	if ($#empty > -1) {
		print "<br>";
		print "<p><b>One or more required fields was left blank</b></p>";
		print "<p>Please use your browser's back button to return to the form and kindly provide the following information:</p>";
		print "<ul>";
				
		foreach my $field (@empty) {
			$field =~s/_/ /g;
			print "<li><span class=\"listfont\">$field</span>";		
		}

		print "</ul>";
		&print_footer;
		exit;
	}
}

sub create_message {
	my $questions_ref = shift;
	my $answers_ref = shift;

	my @questions = @$questions_ref;
	my %answers = %$answers_ref;

	my $message = "";

	foreach (@questions) {
		$message .= $_;
		$message .= ": ";
		$message .= $answers{$_};
		$message .= "\n";
	}
	
	return $message;
}


sub print_to_file {
	my ($formdata_ref,$questions_ref,$file,$address) = @_;
	my %answers   = %$formdata_ref;
	my @questions = @$questions_ref;
	my $answer;

	open (OUT,">>$file") || return "Error writing results to file.";

	my $i;
	foreach $i (0 .. $#questions) {
		if (!defined($answers{$questions[$i]})) { 
			$answer = "*";
		}
		elsif ($answers{$questions[$i]} =~/^\s*$/) {
			$answer = "*";		
		}
		else {
			$answer = $answers{$questions[$i]};
		}
		# Remove excess newlines and spaces before printing, otherwise
		# the output looks messy!
		$answer =~ s/\n|\s+/ /g;
		print OUT "$answer:";
# For testing
# print $questions[$i]."=".$answer.":";
	}
	my $date = localtime;
	$date =~s/:/-/g;
	print OUT "$date\n";
	close OUT;

	return 0;
}
