#!/usr/bin/perl
# Kasra Meshkin May 1998 For HSL

require "cgi-lib.pl";

$time = localtime();
@tt = split(/ /,$time);
if($tt[3] > 9) {
	@pp=split(/:/,$tt[3]);
}else {
	@pp=split(/:/,$tt[4]);
}

 
###&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;
($JOCity = $cgi{'JOCity'}) =~ s/\n/ /g;
($JOPCode = $cgi{'JOPCode'}) =~ s/\n/ /g;
($JOApt = $cgi{'JOApt'}) =~ s/\n/ /g;
($dicamount = $cgi{'dicamount'}) =~ s/[\,\n ]//g;
($codamount = $cgi{'codamount'}) =~ s/[\,\n ]//g;
($codterm = $cgi{'codterm'}) =~ s/\n/ /g;
($certificate = $cgi{'certificate'}) =~ s/\n/ /g;
($interestrate = $cgi{'interestrate'}) =~ s/\n/ /g;

($agree = $cgi{'yes'}) =~ s/\n/ g/;


@SIN = split(//,$RHSIN);


$B1 = $SIN[1] + $SIN[1];$B2 = $SIN[3] + $SIN[3];$B3 = $SIN[5] + $SIN[5];$B4 = $SIN[7] + $SIN[7];if($B1 < 9) { 	$X1 = $B1;} elsif(2>1) {	$X1 = $B1 - 9;}if($B2 < 9) { 	$X2 = $B2;} elsif(2>1) {	$X2 = $B2 - 9;}if($B3 < 9) { 	$X3 = $B3;} elsif(2>1) {	$X3 = $B3 - 9;}if($B4 < 9) { 	$X4 = $B4;} elsif(2>1) {	$X4 = $B4 - 9;}$SUM = $SIN[0]+$SIN[2]+$SIN[4]+$SIN[6]+$X1+$X2+$X3+$X4;

$KUM = $SUM + 0;
$SUM = 10 - $SUM;$SUM = $SUM %10;if($SUM !~ $SIN[8]) {
        &myerror("S.I.N NUMBER $RHSIN is Invalid.\n");


# if the SIN is more than 0 and less than 9 means any digit check and report error } elsif((length($RHSIN) < 9) && (length($RHSIN) > 0 )){
        &myerror("Social Insurance Number must be nine digits.\n");
}



# 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 Last Name.\n");
} elsif(length($RHHomePhArea) < 1){
        &myerror("You did not enter Home Phone Area Code.\n");
} elsif(length($RHHomePhPrefix) < 1){
        &myerror("You did not enter Home Phone's first 3 digits.\n");
} elsif(length($RHHomePhPost) < 1){
        &myerror("You did not enter Home Phone's last 4 digits.\n");
} elsif(length($RHDBmonth) < 1){
        &myerror("You did not enter Birth Month.\n");
} elsif(length($RHDBday) < 1){
        &myerror("You did not enter Birth Day.\n");
} elsif(length($RHDByear) < 1){
        &myerror("You did not enter Birth Year.\n");

# ============================================================
# check for correct birtday year. 1- can not be less than four digits 2- can no be in the future 3- can not be less than 1848
# =============================================================
} elsif(length($RHDByear) < 4){
        &myerror("Birthday Year must be four digits.\n");

} elsif ($RHDByear > 1998) {
        &myerror("Birth Year can not be in the future\n");

} elsif ($RHDByear < 1848) {
        &myerror("Birth Year can not be less than 1848 - NOT on Earth :-).\n");

# ============================================================
# check month - can not be less than 1 and more than 12
# ============================================================

} elsif($RHDBmonth < 1){
        &myerror("Month of birth can not be 0.\n");
} elsif($RHDBmonth > 12){
        &myerror("Month of birth is invalid.\n");

# ============================================================
# check day - can not be less than 1 and more that 31
# ============================================================

} elsif($RHDBday < 1){
        &myerror("Day of birth can not be 0.\n");
} elsif($RHDBday > 31){
        &myerror("Day of birth is invalid.\n");


} elsif(length($RHSIN) < 1){
        &myerror("You did not enter the Registered Holder's Social Insurance Number.\n");
		
} elsif(($cgi{'RHemail'} ne "") && ($cgi{'RHemail'} !~ /[a-zA-Z][a-zA-Z0-9._-]+\@[a-zA-Z][a-zA-Z0-9._-]+\.[a-zA-Z]+/)){
        &myerror("Please check Registered Holder's email.\n");


} elsif(length($RHAddress) < 1){
        &myerror("You did not enter the Registered Holder's Address.\n");

} elsif(length($RHCity) < 1){
        &myerror("You did not enter the Registered Holder's City.\n");

# Postal code is not mandetory 
#} elsif(length($RHPCode) < 1){
#        &myerror("You did not enter the Registered Holder's Postal Code.\n");

} elsif(($cgi{'JOemail'} ne "") && ($cgi{'JOemail'} !~ /[a-zA-Z][a-zA-Z0-9._-]+\@[a-zA-Z][a-zA-Z0-9._-]+\.[a-zA-Z]+/)){
        &myerror("Please check JOINT OWNER's email.\n");


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

} elsif(($cgi{'type'}=~/DIC/) && ($cgi{'dicfrequency'}=~/annually/) && ($dicamount < 1000)){
        &myerror("The DIC amount can not be less than 1000 when payment frequency is Annually\n");


} elsif(($cgi{'type'}=~/DIC/) && ($cgi{'dicfrequency'}=~/semi-annually/) && ($dicamount < 5000)){
        &myerror("The DIC amount can not be less than 5000 when payment frequency is Semi-Annually\n");


} elsif(($cgi{'type'}=~/DIC/) && ($cgi{'dicfrequency'}=~/monthly/) && ($dicamount < 10000)){
        &myerror("The DIC amount can not be less than 10000 when payment frequency is Monthly\n");


} elsif(($cgi{'type'}=~/DIC/) && ($dicamount > 60000)){
        &myerror("The DIC amount can not be more than 60000\n");




#===========================================================
# COD CHECK
#=========================================================



} elsif(($cgi{'type'}=~/COD/) && ( length($codamount) < 1)){
        &myerror("You didn't fill the COD amount.\n");
} elsif(($cgi{'type'}=~/COD/) && ($codamount < 5000 )){ 
        &myerror("The COD amount can not be less than 5000 dollars\n");


} elsif(($cgi{'type'}=~/COD/) && ($codterm < 1)){
         &myerror("COD term is missing\n");

} elsif(($cgi{'type'}=~/COD/) && ($codterm < 30)){
        &myerror("Minimum COD term is 30 days\n");
} elsif(($cgi{'type'}=~/COD/) && ($codamount > 60000)){
        &myerror("The COD amount can not be more than 60000\n");
} elsif(($cgi{'type'}=~/COD/) && ($codterm > 364)){
      &myerror("Maximum COD term is 364 days\n");














# old cert is not mandetory
#} 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 "testing $RHDBysplit";  

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;
}

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





# =====Toronto======================================================================================


       
#===================================================
# send mail to Toronto
#===================================================
open(MAIL, "| /usr/lib/sendmail marions\@hslcorp.com,kasra\@ezenet.com,jamesa\@hslcorp.com");
#open(MAIL, "| /usr/lib/sendmail edward\@ezenet.com");

if($cgi{'individual'}=~/agent/){

printf(MAIL "Subject: $RHLastName - $tt[1]$tt[2]$tt[4] --- $pp[0]$cgi{'type'}$pp[1]$pp[2] TORONTO AGENT\n");

}elsif($cgi{'individual'}!~/agent/){
printf(MAIL "Subject: $RHLastName - $tt[1]$tt[2]$tt[4] --- $pp[0]$cgi{'type'}$pp[1]$pp[2] TORONTO\n");
}
printf(MAIL "From: $cgi{'type'}\-ONLINE\@EZENET\n\n");

printf(MAIL "=============TORONTO DIC APPLICATION============\n");
printf(MAIL "Submission Date: $tt[1]-$tt[2]-$tt[4]\n");
printf(MAIL "$RHLastName - $tt[1]$tt[2]$tt[4] --- $pp[0]$cgi{'type'}$pp[1]$pp[2] TORONTO\n");
printf(MAIL "================================================\n\n");

if($cgi{'individual'}=~/agent/){
printf(MAIL "Filled by : AGENT\n AGENT NAME: $agentname\n NUMBER: $cgi{'agentnumber'}\n");
printf(MAIL "Deposit Type : $cgi{'agentdeposittype'}\n\n");
}elsif($cgi{'individual'}!~/agent/){
        printf(MAIL "Filled by : INDIVIDUAL\n\n");
}

printf(MAIL "================ REGISTERED HOLDER =============\n");
printf(MAIL "APPLICANT'S NAME: $cgi{'RHTitle'}. $firstname $RHLastName\n");
printf(MAIL "DATE OF BIRTH: $RHDBmonth-$RHDBday-$RHDByear (mm/dd/yyyy)\n");
printf(MAIL "S.I.N: $RHSIN\n");
printf(MAIL "Email: $cgi{'RHemail'}\n");

printf(MAIL "ADDRESS: $RHAddress, #$RHApt, $RHCity, $cgi{'RHProv'}, $RHPCode\n");
printf(MAIL "HOME PHONE: $cgi{'RHHomePhArea'}-$cgi{'RHHomePhPrefix'}-$cgi{'RHHomePhPost'}\n\n");


if((length($JOFirstName) < 1) && (length($JOLastName) < 1)) {
printf(MAIL "============ JOINT OWNER INFORMATION ===========\n");
printf(MAIL "NOT APPLICABLE\n");
printf(MAIL "================================================\n");


}elsif((length($JOFirstName) > 0) || (length($JOLastName) > 0)) {


printf(MAIL "============ JOINT OWNER INFORMATION ===========\n");
printf(MAIL "CO-APPLICANT'S NAME: $cgi{'JOTitle'}. $JOFirstName $JOLastName\n");
printf(MAIL "DATE OF BIRTH: $JODBmonth-$JODBday-$JODByear (mm/dd/yyyy)\n");
printf(MAIL "S.I.N: $JOSIN\n");
printf(MAIL "Email: $cgi{'JOemail'}\n");

}
printf(MAIL "ADDRESS: $JOAddress, #$JOApt, $JOCity, $cgi{'JOProv'}, $JOPCode\n");
printf(MAIL "HOME PHONE: $cgi{'JOHomePhArea'}-$cgi{'JOHomePhPrefix'}-$cgi{'JOHomePhPost'}\n");
printf(MAIL "================================================\n\n");


printf(MAIL "TYPE: $cgi{'type'}\n");

if($cgi{'type'}=~/DIC/){
       printf(MAIL "AMOUNT: $dicamount \n");
    printf(MAIL "TERM: $cgi{'dicterm'} \n");
    printf(MAIL "PAYMENT FREQUENCY: $cgi{'dicfrequency'} \n");
printf(MAIL "INTEREST PAYMENT: $cgi{'dicpayment'} \n");


} elsif($cgi{'type'}=~/COD/) {
    printf(MAIL "AMOUNT: $codamount \n");
 printf(MAIL "TERM: $cgi{'codterm'} \n");


}


if($cgi{'invtype'}=~/renewalinv/){

printf(MAIL "INVESTMENT TYPE: RENEWAL - OLD CERT #: $certificate\n");
}elsif ($cgi{'invtype'}!~/renewalinv/){
printf(MAIL "INVESTMENT TYPE: NEW INVESTMENT\n");

}

printf(MAIL "INTEREST RATE: $interestrate\n");

#printf(MAIL "$RHLastName\n");
#printf(MAIL "$agentname\n");


close(MAIL);

#===================================================
# Now display info on the screen
#===================================================
printf("%s", &PrintHeader);
printf("<HTML>\n<TITLE>Thank you for your submission!</TITLE>\n");
printf("<BODY bgcolor=\"#ffffff\"> \n");




printf("<center><H2> \n");

if($cgi{'individual'}=~/agent/) {  

printf("<h2>Thank you $agentname<BR>$cgi{'agentnumber'}.</h2> \n");

}
elsif($cgi{'individual'}!~/agent/) {  

printf("<h2>Thank you $RHTitle. $firstname $RHLastName.</h2> \n");

}

printf("<h4>Your application has been submitted to Home Savings & Loan Corp., Toronto.</h4>\n");


if(($cgi{'invtype'}=~/renewalinv/)){
       printf("<font size=+1 color=#ff0000>Renewal</font><h4>The effective Interest rate will be the rate in effect on the day\n");
       printf("of maturity</h4>\n");
}


if(($cgi{'individual'}=~/agent/) && ( $cgi{'agentdeposittype'}=~/directdeposit/)){

printf("<h4>Please send deposit slip and include the appropriate reference number[s]</h4>\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>\n";
print "<h4>A <font color=#ff0000>Void Cheque</font> is needed to process your application<br></h2>\n";
print "<h4>############################################################</h4>";

}


elsif(($cgi{'type'}=~/DIC/) && ( $cgi{'dicpayment'}=~/cheque/)){
}

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

print "For mail-in applications, these rates remain active up to five 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>\n";
print "Toll Free: 1-800-990-7881<br>\n";
print "Telephone: 416-360-4663<br>\n";
print "Fax: 416-363-7611<br>\n";
print "Your Representative is <font size=+1 color=#ff0000> Marion Samler</font>\n";


print "</font>";
print "</BODY>\n</HTML>\n";

exit;



