#!/usr/bin/perl -w
###########################################
# SCRIPT NAME : contact.cgi
# DATE: Sep 2004
# VERSION: 1.0
# AUTHOR: Sarun Chinskul
# EMAIL: schinskul@crystalmckenzieinc.com
###########################################
use lib "/home/httpd/vhosts/cminyc.com/httpdocs/admin";
chdir "/home/httpd/vhosts/cminyc.com/httpdocs/";

use CGI;
use DBI;
use HTML::Template;
use cmiModule;
use strict;

########################
# Read Config.conf file
########################
my @info = &cmiModule::ReadConfig("config.conf");
########################
# Logfile config setup
########################
my %Log_Config;
   # Setting EasyLog is ON or OFF in config.conf file
   $Log_Config{SWITCH} = $info[0];
   $Log_Config{FILENAME} = $info[1];
   $Log_Config{MESSAGE} = "-------STARTED LOG [".$0."]-------";
   &cmiModule::EasyLog(\%Log_Config);
########################
# Database config setup
########################
my %DB;
   $DB{DATABASE} = $info[2];
   $DB{ACCOUNT}  = $info[3];
   $DB{PASSWORD} = $info[4];
my $dbh;
unless( $dbh = &cmiModule::ConnectToMySQL(\%DB) ) {
  $Log_Config{"MESSAGE"} = "Bad DBH from ConnectToMySQL.";
  &cmiModule::EasyLog(\%Log_Config);
  print "Content-type:text/html\n\n";
  print "<b>Bad DBH from ConnectToMySQL.</b>";
  exit;
}
###### End config ######

my $cgi = new CGI;

##########################################################
#                     Contact text
##########################################################
my $text =  qq{&nbsp;<b>Contact Us</b><br><br>};
   $text .= qq{&nbsp;Crystal McKenzie, Inc.<br>};
   $text .= qq{&nbsp;220 East 23rd Street,&nbsp;&nbsp;Suite 305<br>};
   $text .= qq{&nbsp;New York, NY 10010<br>};
   $text .= qq{&nbsp;Tel 212-598-4567<br>};
   $text .= qq{&nbsp;Fax 212-598-4566<br>};
   $text .= qq{&nbsp;contact\@cminyc.com<br><br>};

   $text .= qq{&nbsp;New Jersey Offices:<br>};
   $text .= qq{&nbsp;43 Leigh Avenue<br>};
   $text .= qq{&nbsp;Princeton, NJ 08542<br>};
   $text .= qq{&nbsp;Tel 201-349-8445<br>};
   $text .= qq{&nbsp;Fax 609-799-4568<br><br>};

   $text .= qq{&nbsp;Brian Jones<br>};
   $text .= qq{&nbsp;COO / Creative Director<br>};
   $text .= qq{&nbsp;bjones\@cminyc.com<br>};

##########################################################

my %CONTINFO;
   $CONTINFO{contact_template} = HTML::Template->new(filename => 'tmpl/contact.tmpl');
   $CONTINFO{email_filter} = q{/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i};
   $CONTINFO{contactustext} = $text;
   $CONTINFO{name} = $cgi->param('name');
   $CONTINFO{from} = $cgi->param('from');
   $CONTINFO{subject} = $cgi->param('subject');
   $CONTINFO{emailbody} = $cgi->param('emailbody');


&contact(\%CONTINFO);

1;

sub contact {
    my ($params_hashref) = @_;
                                                                                                                             
    my %params_hash      = %{ $params_hashref };
    my $contact_template = $params_hash{contact_template};
    my $email_filter     = $params_hash{email_filter};
    my $contactustext    = $params_hash{contactustext};
    my $name             = $params_hash{name};
    my $from             = $params_hash{from};
    my $subject          = $params_hash{subject};
    my $emailbody        = $params_hash{emailbody};
 
    $contact_template->param(email_filter  => $email_filter);
    $contact_template->param(contactustext => $contactustext);

 my ($to,$message);
 if ( $name ) {

  # Send email
  $to = "contact\@cminyc.com";  
  $message  = "\nSender's name : $name\n";
  $message  .= "\n$emailbody\n";
  &cmiModule::SendEmail($from,$to,$subject,$message);
  
  # Insert data
  my $sql  = "INSERT INTO QuickContact";
     $sql .= " (ContactID,ContactName,ContactEmail,ContactSubject,ContactContent)";
     $sql .= " Values ('','$name','$from','$subject','$emailbody')";
  $dbh->do($sql);
 }

print "Content-type:text/html\n\n";
print $contact_template->output;

}