#!/usr/bin/perl -w
###########################################
# SCRIPT NAME : estimate.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;

my $submit_status = $cgi->param('submit_status');

my %ESTIMATEINFO;
   $ESTIMATEINFO{estimate_template} = HTML::Template->new(filename => 'tmpl/estimate.tmpl');
   $ESTIMATEINFO{email_filter} = q{/\S+\@\S+\.\S+/};
   $ESTIMATEINFO{name} = $cgi->param('name');
   $ESTIMATEINFO{from} = $cgi->param('from');
   $ESTIMATEINFO{subject} = qq{Request Estimate.};
     my  $service  = $cgi->param('service'); 
     my  $target   = $cgi->param('target');
     my  $budget   = $cgi->param('budget');
     my  $otherdes = $cgi->param('otherdes');
   $ESTIMATEINFO{emailbody} = qq{Service name : $service\n}; 
   $ESTIMATEINFO{emailbody} .= qq{Target completion : $target\n};
   $ESTIMATEINFO{emailbody} .= qq{Budget range : $budget\n\n};
   $ESTIMATEINFO{emailbody} .= qq{===========================\n};
   $ESTIMATEINFO{emailbody} .= qq{      Other description    \n};
   $ESTIMATEINFO{emailbody} .= qq{===========================\n};
   $ESTIMATEINFO{emailbody} .= qq{$otherdes\n};
    

&estimateform(\%ESTIMATEINFO) if ( $submit_status eq "" );
&estimate(\%ESTIMATEINFO)     if ( $submit_status eq "insert" ); 
&showmessage(\%ESTIMATEINFO)  if ( $submit_status eq "result");


1;


sub estimateform {
    my ($params_hashref) = @_;
                                                                                                                             
    my %params_hash       = %{ $params_hashref };
    my $estimate_template = $params_hash{estimate_template};
    my $email_filter      = $params_hash{email_filter};
   
    $estimate_template->param(email_filter  => $email_filter);
    $estimate_template->param(estimateform  => 1);

    my $servicename = &ShowServiceName();
    $estimate_template->param(servicename => $servicename); 
    
    my $targetlist = &ShowTarget();
    $estimate_template->param(targetlist => $targetlist); 
       
    my $budgetlist = &ShowBudget();
    $estimate_template->param(budgetlist => $budgetlist); 

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


sub ShowServiceName {

  my $sql = "SELECT cmiServiceName";
     $sql .= " From cmiServices";
     $sql .= " Order by cmiServiceID";

  my $rec = $dbh->prepare($sql);
     $rec->execute;

  my $out = qq{ <select name="service" class=emenu style="width:175px;"><option value="" selected>--- select service ---};
      while (my @fld = $rec->fetchrow) {
       $fld[0] =~ s/\"//ig;    
       $fld[0] =~ s/\'//ig;    
    
       $out .= qq{<option value="$fld[0]" >$fld[0]};
      }$out .= qq{</select>};

 return $out;
}


sub ShowTarget {

 my $out = qq{ <select name=target class=emenu style="width:175px;"><option value="" selected>--- select target ---};
    $out .= qq{<option value="3 - 4 weeks">3 - 4 weeks};
    $out .= qq{<option value="5 - 8 weeks">5 - 8 weeks};
    $out .= qq{<option value="More than 2 months">More than 2 months};
    $out .= qq{<option value="1st Quarter this year">1st Quarter this year};
    $out .= qq{<option value="2nd Quarter this year">2nd Quarter this year};
    $out .= qq{<option value="Next year">Next year};
    $out .= qq{</select>};

 return $out;
}


sub ShowBudget {

 my $out = qq{ <select name=budget class=emenu style="width:175px;"><option value="" selected>--- select budget ---};
    $out .= qq{<option value="Less than \$5000">Less than \$5,000};
    $out .= qq{<option value="\$5,000 - \$10,000">\$5,000 - \$10,000};
    $out .= qq{<option value="\$10,000 - \$50,000">\$10,000 - \$50,000};
    $out .= qq{<option value="\$50,000 - \$100,000">\$50,000 - \$100,000};
    $out .= qq{<option value="\$100,000 - \$500,000">\$100,000 - \$500,000};
    $out .= qq{<option value="To be determined">To be determined};
    $out .= qq{</select>};

 return $out; 
}



sub estimate {
    my ($params_hashref) = @_;
                                                                                                                             
    my %params_hash      = %{ $params_hashref };
    my $name             = $params_hash{name};
    my $from             = $params_hash{from};
    my $subject          = $params_hash{subject};
    my $emailbody        = $params_hash{emailbody};
 
 my ($to,$message);
 if ( $name ) {

  # Send email
  $to = "info\@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 "location:estimate.cgi?submit_status=result\n\n";
 }

}



sub showmessage {
    my ($params_hashref) = @_;
    
    my %params_hash       = %{ $params_hashref };
    my $estimate_template = $params_hash{estimate_template};
    my $email_filter      = $params_hash{email_filter};

  $estimate_template->param(email_filter  => $email_filter);
  $estimate_template->param(showmessage => 1);

  my $result = qq{<br>&nbsp;&nbsp;&nbsp;Thank you for submitting your request.<br>};
     $result .= qq{We will get in touch with you regarding your project.<br>};
     $result .= qq{If you are new to our system, please click here to register and become}; 
     $result .= qq{a member of the CMI community to receive information and tools to improve your};
     $result .= qq{&nbsp;business.<br><br>};
     $result .= qq{&nbsp;&nbsp;&nbsp;If you are already a member, please go to the <br><a href="http://www.cminyc.com/login.cgi?"><b>Member Area</b></a>,&nbsp;login and submit your project from the<br> CMI FTP web-based application.};

  $estimate_template->param(result => $result); 

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

