#!/usr/bin/perl
require "cgi-lib.pl";

$time = localtime();
@tt = split(/ /,$time);
@pp = split(/:/,$tt[3]);
 
###&ReadParse(*cgi);
&parse_form;

$results = &PrintVariables(%cgi);
sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $cgi{$name} = $value;
   }
}
# load in the form contents

($agentname = $cgi{'agentname'}) =~ s/\n/ /g;
($RHLastName = $cgi{'RHLastName'}) =~ s/\n/ /g;
($RHHomePhArea = $cgi{'RHHomePhArea'}) =~ s/\n/ /g;
($RHTitle = $cgi{'RHTitle'}) =~ s/\n/ /g;
($firstname = $cgi{'RHFirstName'}) =~ s/\n/ /g;
($RHHomePhPrefix = $cgi{'RHHomePhPrefix'}) =~ s/\n/ /g;
($RHHomePhPost = $cgi{'RHHomePhPost'}) =~ s/\n/ /g;
($RHDBmonth = $cgi{'RHDBmonth'}) =~ s/\n/ /g;
($RHDBday = $cgi{'RHDBday'}) =~ s/\n/ /g;
($RHDByear = $cgi{'RHDByear'}) =~ s/\n/ /g;
($RHAddress = $cgi{'RHAddress'}) =~ s/\n/ /g;
($RHApt = $cgi{'RHApt'}) =~ s/\n/ /g;
($RHCity = $cgi{'RHCity'}) =~ s/\n/ /g;
($RHPCode = $cgi{'RHPCode'}) =~ s/\n/ /g;
($RHSIN = $cgi{'RHSIN'}) =~ s/\n/ /g;
($JOTitle = $cgi{'JOTitle'}) =~ s/\n/ /g;
($JOFirstName = $cgi{'JOFirstName'}) =~ s/\n/ /g;
($JOLastName = $cgi{'JOLastName'}) =~ s/\n/ /g;
($JOHomePhArea = $cgi{'JOHomePhArea'}) =~ s/\n/ /g;
($JOHomePhPrefix = $cgi{'JOHomePhPrefix'}) =~ s/\n/ /g;
($JOHomePhPost = $cgi{'JOHomePhPost'}) =~ s/\n/ /g;
($JODBmonth = $cgi{'JODBmonth'}) =~ s/\n/ /g;
($JODBday = $cgi{'JODBday'}) =~ s/\n/ /g;
($JODByear = $cgi{'JODByear'}) =~ s/\n/ /g;
($JOSIN = $cgi{'JOSIN'}) =~ s/[\r\n]/ /g;
($JOAddress = $cgi{'JOAddress'}) =~ s/\n/ /g;
($dicamount = $cgi{'dicamount'}) =~ s/\n/ /g;
($codamount = $cgi{'codamount'}) =~ s/\n/ /g;
($certificate = $cgi{'certificate'}) =~ s/\n/ /g;
($interestrate = $cgi{'interestrate'}) =~ s/\n/ /g;
($JOAddress = $cgi{'JOAddress'}) =~ s/\n/ /g;
($JOCity = $cgi{'JOCity'}) =~ s/\n/ /g;
($JOPCode = $cgi{'JOPCode'}) =~ s/\n/ /g;
($agree = $cgi{'yes'}) =~ s/\n/ g/;



# do any error checking here
if(($cgi{'individual'}=~/agent/) && ( length($agentname) < 1)){
        &myerror("You didn't enter Agent Information.\n");


} elsif(length($firstname) < 1){
        &myerror("Your First Name is missing.\n");
} elsif(length($RHLastName) < 1){
        &myerror("You did not enter your Last Name.\n");
} elsif(length($RHHomePhArea) < 1){
        &myerror("You did not enter your  Home Phone Area Code.\n");
} elsif(length($RHHomePhPrefix) < 1){
        &myerror("You did not enter your Home Phone's first 3 digits.\n");
} elsif(length($RHHomePhPost) < 1){
        &myerror("You did not enter your  Home Phone's last 4 digits.\n");
} elsif(length($RHDBmonth) < 1){
        &myerror("You did not enter your Birthday Month.\n");
} elsif(length($RHDBday) < 1){
        &myerror("You did not enter your Birthday Day.\n");
} elsif(length($RHDByear) < 1){
        &myerror("You did not enter your Birthday Year.\n");
} elsif(length($RHSIN) < 1){
        &myerror("You did not enter the Registered Holder's Social Insurance Number.\n");
} elsif(length($RHAddress) < 1){
        &myerror("You did not enter the Registered Holder's Address.\n");
} elsif(length($RHApt) < 1){
        &myerror("You did not enter the Registered Holder's Appartment Number - if not applicable enter N/A.\n");
} elsif(length($RHCity) < 1){
        &myerror("You did not enter the Registered Holder's City.\n");
} elsif(length($RHPCode) < 1){
        &myerror("You did not enter the Registered Holder's Postal Code.\n");


} elsif(($cgi{'type'}=~/DIC/) && ( length($dicamount) < 1)){
        &myerror("You didn't fill the DIC amount.\n");

} elsif(($cgi{'type'}=~/COD/) && ( length($codamount) < 1)){
        &myerror("You didn't fill the COD amount.\n");


} elsif(($cgi{'invtype'}=~/renewalinv/) && ( length($certificate) < 1)){
        &myerror("You didn't fill the Renewal Information - Old Cert Number.\n");

} elsif(length($interestrate) < 1){
        &myerror("Please enter an interest rate. <a href=http://solar.ezenet.com/cgi-bin/devel/gic/rateview target=_blank>Click here for the latest rates</a>  \n");

} elsif($cgi{'agree'}!~/yes/){
        &myerror("You must agree to the Terms And Conditions before you submit this form.<br>Please read them carefully and resubmit the form.\n");


}
sub myerror {
   
     $errstr = shift @_;
        printf("%s", &PrintHeader);
	
        
print <<EOF;
<center>
<h2><font color=#ff0000> The following information is required to process your application</h2></font>
<hr>
<table border=2 bgcolor=#fffddf cellpadding=5 cellborder=0><tr><td align=center>
<font face=arial color=blue>
<b>$errstr</b>
</font>
</td></tr></table>
<p>
Please close this window to return to the application form and complete all of the required information.  Thank you.
</center>
EOF

        exit;
}


if($cgi{'representative'}=~/rep1/){

    
open(MAIL, "| /usr/lib/sendmail kasra\@ezenet.com");
printf(MAIL "Subject: $RHLastName - $tt[1]$tt[2]$tt[4] --- $pp[0]$cgi{'type'}$pp[1]$pp[2] REP1\n");
printf(MAIL "From: $cgi{'type'}\@ONLINE.\n\n");

printf(MAIL "================================================\n\n");
close(MAIL);


} elsif($cgi{'representative'}=~/rep2/){

open(MAIL, "| /usr/lib/sendmail kasra\@ezenet.com");
printf(MAIL "Subject:\n");
printf(MAIL "From: $firstname\@INTERNET.\n\n");
printf(MAIL "================================================\n\n");
close(MAIL);
}


elsif($cgi{'representative'}=~/rep3/){

open(MAIL, "| /usr/lib/sendmail kasra\@ezenet.com");
printf(MAIL "Subject:REP3\n");
printf(MAIL "From: $firstname\@INTERNET.\n\n");
close(MAIL);

} elsif($cgi{'representative'}=~/dont/){
open(MAIL, "| /usr/lib/sendmail kasra\@ezenet.com");
printf(MAIL "Subject:Dont\n");
printf(MAIL "From: $firstname\@INTERNET.\n\n");
printf(MAIL "================================================\n\n");
close(MAIL);
}

if($cgi{'branch'}=~/stcatharines/){
     
  

printf("%s", &PrintHeader);
printf("<BODY bgcolor=\"#ffffff\"> \n");



#printf("<center><img src=\"http://www.tlcplus.com/byline.gif\"></center> \n");
printf("<center><H2> \n");

printf("<h2>Thank you $RHTitle. $firstname $RHLastName.</h2> \n");
printf("<h4>Your application has been submitted to Home Savings and Loan Corp., St.Catharines.</h4>\n");
printf("<BR>\n");

if(($cgi{'type'}=~/DIC/) && ( $cgi{'dicpayment'}=~/pad/)){
        
print "<h4>############################################################</h4>";
print "<h4>You have chosen the method of interest payment as <font color=#ff0000>PAD</font><br></h4>";
print "<h4>A <font color=#ff0000>Void Check</font> is needed to process your application<br></h2>";
print "<h4>############################################################</h4>";

}


elsif(($cgi{'type'}=~/DIC/) && ( $cgi{'dicpayment'}=~/check/)){
# print "PCheck hahaah<br>";
}

print "$mas<br>";
print "Your Reference Number is <font size=+1 color=#ff0000>$pp[0]$cgi{'type'}$pp[1]$pp[2]</font><br>";
print "<h4>Should you need to correspond with us please refer to this number</h4>";
print "<h3>Please send your payment along with your reference number to:<br></h3>";
print"<h4>";
print "Home Savings and Loan<br>"; 
print "15 Church Street, Suite 100<br>";
print "P.O. Box 1554<br>";
print "St. Catharines, Ontario<br>";
print "L2R 7J9<br><br>";
print "For mail-in applications, these rates remain active up to two business days from the Submission date<br>"; 
printf("<h4>Submission Date: $tt[1]-$tt[2]-$tt[4]</h4>\n");
print "<h4>To check the status of your application please contact:<br></h4>";
print "Telephone: 905-688-3131<br>";
print "Fax: 905-688-0534<br>";
print "</font>";
}





elsif($cgi{'branch'}=~/hamilton/){
       

printf("%s", &PrintHeader);
printf("<BODY bgcolor=\"#ffffff\"> \n");



#printf("<center><img src=\"http://www.tlcplus.com/byline.gif\"></center> \n");
printf("<center><H2> \n");

printf("<h2>Thank you $RHTitle. $firstname $RHLastName.</h2> \n");
printf("<h4>Your application has been submitted to Home Savings and Loan Corp., Hamilton.</h4>\n");
printf("<BR>\n");

if(($cgi{'type'}=~/DIC/) && ( $cgi{'dicpayment'}=~/pad/)){
        
print "<h4>############################################################</h4>";
print "<h4>You have chosen the method of interest payment as <font color=#ff0000>PAD</font><br></h4>";
print "<h4>A <font color=#ff0000>Void Check</font> is needed to process your application<br></h2>";
print "<h4>############################################################</h4>";

}


elsif(($cgi{'type'}=~/DIC/) && ( $cgi{'dicpayment'}=~/check/)){
# print "PCheck hahaah<br>";
}

print "$mas<br>";
print "Your Reference Number is <font size=+1 color=#ff0000>$pp[0]$cgi{'type'}$pp[1]$pp[2]</font><br>";
print "<h4>Should you need to correspond with us please refer to this number</h4>";
print "<h3>Please send your payment along with your reference number to:<br></h3>";
print"<h4>";







print "Home Savings and Loan<br>"; 
print "CIBC Phase II<br>";
print "21 King Street West, Suite 800<br>";
print "Hamilton, Ontario<br>";
print "L8P 4W7<br><br>";
print "For mail-in applications, these rates remain active up to two business days from the Submission date<br>"; 
printf("<h4>Submission Date: $tt[1]-$tt[2]-$tt[4]</h4>\n");
print "<h4>To check the status of your application please contact:<br></h4>";
print "Toll Free: 1-800-944-3419<br>";
print "Telephone: 905-522-0250<br>";
print "Fax: 905-522-1888 <br>";
print "</font>";
}



if($cgi{'branch'}=~/toronto/){
       

printf("%s", &PrintHeader);
printf("<BODY bgcolor=\"#ffffff\"> \n");



#printf("<center><img src=\"http://www.tlcplus.com/byline.gif\"></center> \n");
printf("<center><H2> \n");

printf("<h2>Thank you $RHTitle. $firstname $RHLastName.</h2> \n");
printf("<h4>Your application has been submitted to Home Savings and Loan Corp., Toronto.</h4>\n");
printf("<BR>\n");

if(($cgi{'type'}=~/DIC/) && ( $cgi{'dicpayment'}=~/pad/)){
        
print "<h4>############################################################</h4>";
print "<h4>You have chosen the method of interest payment as <font color=#ff0000>PAD</font><br></h4>";
print "<h4>A <font color=#ff0000>Void Check</font> is needed to process your application<br></h2>";
print "<h4>############################################################</h4>";

}


elsif(($cgi{'type'}=~/DIC/) && ( $cgi{'dicpayment'}=~/check/)){
# print "PCheck hahaah<br>";
}

print "$mas<br>";
print "Your Reference Number is <font size=+1 color=#ff0000>$pp[0]$cgi{'type'}$pp[1]$pp[2]</font><br>";
print "<h4>Should you need to correspond with us please refer to this number</h4>";
print "<h3>Please send your payment along with your reference number to:<br></h3>";
print"<h4>";
print "Home Savings and Loan<br>"; 
print "145 King Street West, Suite 1910<br>";
print "Toronto, Ontario<br>";
print "M5H 1J8<br>";

print "For mail-in applications, these rates remain active up to two business days from the Submission date<br>"; 
printf("<h4>Submission Date: $tt[1]-$tt[2]-$tt[4]</h4>\n");
print "<h4>To check the status of your application please contact:<br></h4>";
print "Toll Free: 1-800-990-7881<br>";
print "Telephone: 905-360-4663<br>";
print "Fax: 905-363-7611<br>";


print "</font>";
}





