<%@ LANGUAGE = PerlScript %>
<%
$requestMethod = $Request->ServerVariables('REQUEST_METHOD')->Item;
$product_code        = &f_getClientVariable('p_code', '');
$product_title		 = &f_getClientVariable('p_title', '');
$message             = &f_getClientVariable('message', 'Not Provided');
$visitor_name        = &f_getClientVariable('visitor_name', 'Not Provided');
$telephone_no        = &f_getClientVariable('telephone_no','Not Provided');
$email               = &f_getClientVariable('email','Not Provided');
$address	         = &f_getClientVariable('address','Not Provided');
$action				 = &f_getClientVariable('action','');

if ( $action eq '' ){
$title        = "Kimtee Jewellers - Product Order Form" ;
$form = &f_readFile($Server->MapPath("product-order-form.txt"));
$form =~ s/<!--REPLACE.MESSAGE-->//;
$form =~ s/<!--REPLACE.P_CODE-->//;
$form =~ s/<!--REPLACE.P_TITLE-->//;
$form =~ s/<!--REPLACE.MESSAGE-->//;
$form =~ s/<!--REPLACE.NAME-->//;
$form =~ s/<!--REPLACE.TELEPHONE-->//;
$form =~ s/<!--REPLACE.EMAIL-->//;
$form =~ s/<!--REPLACE.ADDRESS-->//;
$content = &f_readFile($Server->MapPath("template.html"));
$content =~ s/<!--REPLACE.BODY-->/$form/;
$content =~ s/<!--REPLACE.TITLE-->/$title/;
$Response->write($content);
return;
}

$found  = 0;
@dbProductCode = &f_readFileIntoArray($Server->MapPath("product-code.txt"));
$found = grep(/^$product_code$/i,@dbProductCode);

if ($found == 0 ){
$error	= "Product Code is incorrect. Please check.";
$form = &f_readFile($Server->MapPath("product-order-form.txt"));
$form =~ s/<!--REPLACE.MESSAGE-->/$error/;
$form =~ s/<!--REPLACE.P_CODE-->/$product_code/;
$form =~ s/<!--REPLACE.P_TITLE-->/$product_title/;
$form =~ s/<!--REPLACE.MESSAGE-->/$message/;
$form =~ s/<!--REPLACE.NAME-->/$visitor_name/;
$form =~ s/<!--REPLACE.TELEPHONE-->/$telephone_no/;
$form =~ s/<!--REPLACE.EMAIL-->/$email/;
$form =~ s/<!--REPLACE.ADDRESS-->/$address/;
$form =~ s/Not Provided//g;
$content = &f_readFile($Server->MapPath("template.html"));
$content =~ s/<!--REPLACE.BODY-->/$form/;
$content =~ s/<!--REPLACE.TITLE-->/$title/;
$Response->write($content);
return;
}

&f_send();

$thanks = <<TX1;
<h1>Product Order Form</h1>
<h3>Your order has been sent successfully.We will come back to you as soon as possible.</h3>
<h3>Thank you.</h3>
TX1

$title        = "Kimtee Jewellers - Product Order Form" ;
$content = &f_readFile($Server->MapPath("template.html"));
$content =~ s/<!--REPLACE.BODY-->/$thanks/;
$content =~ s/<!--REPLACE.TITLE-->/$title/;
$Response->write($content);

sub f_send {
my $body = <<BODY11;
The following is the order details received from your website visitor: http://kimteejewellers.com/

<br>Product Code:$product_code<br> 

Product Title:$product_title<br>

Visitor Name: $visitor_name <br>

Visitor Phone : $telephone_no<br>

Email:  $email<br>

Address : $address<br>

Message : $message <br>

### end of message ###
BODY11

&f_sendEmail( "Website Visitor", "mayor\@HamaraShehar.com", "info\@kimteejewellers.com", "Order from your website visitor", $body );
#$Response->write($body);
}

sub f_readFile {
  local( $fileName ) = shift;
  local( $tempContent );
  open( FL2, "<$fileName" );
  @tempContent  = <FL2>;
  close( FL2 );
  return ( join( '', @tempContent ) );
}

sub f_getClientVariable	 {
   my $var1     = shift;
   my $default1 = shift;
   my $temp1    = '';

   if ( $var1 eq null ) {
	  return 'null';
   }
   if ( defined($Request->Form($var1)->Item) ) {
      $temp1 = $Request->Form($var1)->Item;
   }
   if ( $temp1 ne '' ) {
      return $temp1;
   }
   elsif ( defined($default1)) {
   	  return $default1;
   }
   return 'null';
}	##f_getClientVariable

sub f_readFileIntoArray {
     my $fileName  = shift;
     my @lines;
     open( FL1, "<$fileName" );
     @lines = <FL1>;
     close( FL1 );
     return ( @lines );
    }

sub f_sendEmail {
   my $fromWhom  = shift;
   my $fromEmail = shift;
   my $toEmail   = shift;
   my $subject   = shift;
   my $body      = shift;

   my $Mailer = $Server->CreateObject("SMTPsvg.Mailer");
   $fromWhom  = 'From HamaraShehar' if ($fromWhom eq '');
   $fromEmail = 'mayor@hamarashehar.com' if ($fromEmail eq '');

$objMail = $Server->CreateObject("CDONTS.Newmail");
$objMail->{'To'} = $toEmail;
$objMail->{'Subject'} = $subject;
$objMail->{'From'} = $fromEmail;
$objMail->{'Body'} = $body;
$rc = $objMail->send();
undef $objMail;
return 1;
}
%>