#!/usr/bin/perl -w
use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $dbh=DBI->connect('dbi:mysql:parc_demog:localhost','parcdemog','parc9904')or die "Couldn't connect to database: " . DBI->errstr;
#######
use CGI;
my $query = new CGI;
#print $query->header( );
########
print <<blk;
content-type: text/html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
blk
$cnt=param("cnt");
$shID=param("shID");
for ($i=1;$i<=$cnt ;$i++) 
{
	 $n1 = "pa".$i;  #location Code
	 #$n2 = "pb".$i;  #City Name
	 $n3 = "pc".$i;  #Location Name
	 #$n4 = "pd".$i;  #Store Type
	 $n5 = "pe".$i;  #Plan Date
	 $n6 = "pf".$i;  #Plan Time
	 #$n7 = "pg".$i;  #Shopper Name
	 #$n8 = "ph".$i;  #Gender
	 #$n9 = "pi".$i;  #Age
	 #$n10 = "pj".$i;  #Nationality
	 #$n11 = "pk".$i;  #Mailed
	 #$n12 = "pl".$i;  #Visited
	 $n13 = "pm".$i;  #send
	 #$n14 = "pn".$i;  #Plan Code
	 $n15 = "po".$i;  #visit Code
	 $n16 = "pp".$i;  #Email
	my $locCode =param("$n1");
	#my $city =param("$n2");
	my $location =param("$n3");
	#my $type =param("$n4");
	my $planDate =param("$n5");

	my @arsdate=split('-',$planDate);
	$planDate=$arsdate[2]."-".$arsdate[1]."-".$arsdate[0];
	
	my $planTime =param("$n6");
	#my $shopperName =param("$n7");
	#my $gender =param("$n8");
	#my $age =param("$n9");
	#my $nationality =param("$n10");
	#my $hasMailed =param("$n11");
	#my $hasVisited =param("$n12");
	my $send =param("$n13");
	#my $planCode =param("$n14");
	my $visitCode =param("$n15");
	my $email =param("$n16");
	#$email='sujith@arabresearch.com';
    #$emailFrom="vasudha\@arabresearch.com";
	$emailFrom="parcdxb\@emirates.net.ae";
	#$email=~s/@/\@;
	#$comments="\nKindly note the following points. \n\n(1) Please use the INTERNET EXPLORER to fill the Questionnaire.\n\t YOU MAY HAVE TO RE-FILL, in case, if you have filled the previous questionnaires using OTHER THAN Internet Explorer.\n\n(2) We are RE-SENDING THE SAME LINKS, in case YOU WERE NOT ABLE TO FILL THE QUESTIONNAIRE FOR SOME LOCATIONS.\n\n\tKindly use the new links below, IF YOU HAVE FACED ANY PROBLEM WHILE SAVING THE QUESTIONNAIRE.\n\n\tPlease IGNORE THE MAIL, if you HAVE SUCCESSFULLY SAVED THE QUESTIONNAIRE FOR THE SAME LINK BEFORE.\n\n";
	$comments="Visit has been approved for $location on $planDate $planTime. \n\nPlease use the INTERNET EXPLORER to fill the Questionnaire.\n\nPlease use the following link. \n\n\n";
	if($send ne "T")
	{
		
		if($send==1)
		{
			
			my $lnk = "http://www.arabdemog.com/reg/reg.pl?locCode=$locCode&planDate=$planDate&visitCode=$visitCode";
			$comments="$comments  $lnk";
			my $strSQL="update cf_visitDetail set hasMailed='T' where visitCode='$visitCode'";
			my $rv=$dbh->do($strSQL);
			$dbh->disconnect;
			#########
			# Email the form results
			$subject="Approved Visits of Carrefour!"; 
			$sendmailpath="/usr/sbin/sendmail"; 
			open (sil, "| $sendmailpath -t"); 
			print sil "Subject: $subject\n"; 
			print sil "From: $emailFrom\n"; 
			print sil "To: $email\n";
			print sil "$comments\n\n"; 
			print sil "Thanks"; 
			close (sil)|| print("unable to send $!"); 
			#########
			$msg="Mail has been Delivered";

		}
	}



}
print<<blk1;
<tableborder="1"cellpadding="0"cellspacing="0"width="100%"bordercolor="#000080"height="359">
<tr>
<tdwidth="100%"height="357">
<palign="center"><fontface="Verdana"color="#000080"size="5"><b>$msg</b></font></td>
</tr>
</table>
</body>
</html>
blk1
