#!/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;
#my $dbh=DBI->connect('dbi:mysql:parc_demog:localhost:3306','root','root') or die "Unable to connect: $DBI::errstr\n";

print <<blk;
content-type: text/html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<script type="text/javascript">
window.history.forward(1);

document.attachEvent("onkeydown", my_onkeydown_handler);                                                                                                                                     

function my_onkeydown_handler()
{                                                          
	var type = document.activeElement.getAttribute("type");
	switch (event.keyCode)                                     
	{
		case 8 : //Backspace                
        if ( type == "textarea" || type == "text" || type =="password")
        {
			break;
        } 
        case 116 : // 'F5'                                               
        event.returnValue = false;                                             
        event.keyCode = 0;                                                                  
        break;
        case 122:
        event.returnValue = false;
        event.keyCode = 0;
        break;
        default:
        if(event.ctrlKey == false && event.altKey == false) 
        break;
        else
        if (event.keyCode != 50 && event.keyCode != 67 && event.keyCode != 86 && event.keyCode != 88)
        event.returnValue = false;
	}
}           

function hidebutt()
{
	document.getElementById("pri").style.display="none";
	document.getElementById("clo").style.display="none";
	window.print();
	document.getElementById("pri").style.display="";
	document.getElementById("clo").style.display="";
	
}

</script>

<style type="text/css">
td
{
border:1px solid;
Margin:7px;
}
.mtb
{
border:1px solid green;
width:'100%';
border-collapse:collapse;
border-style: inset;
border-spacing:0;
border-width: 1px;
border:black 1px solid;
margin:20px;
}
.ath
{
	background-color:green;
	color:white;
}
.uth
{
	background-color:red;
	color:white;
}
</style>

</head>
<body oncontextmenu="return false;" onload='flgblok()'>
blk
$cnt=param("cnt");
$shID=param("shID");

$FLG=0;
$FLG1=0;
$unAlloc="";
$emailFrom="parcdxb\@emirates.net.ae";

$sthE=$dbh->prepare("SELECT email from shopper WHERE ShID=$shID");
$sthE->execute;
while ($em=$sthE->fetchrow_hashref())
{
	$email="$em->{'email'}";
}
for ($i=1;$i<=$cnt ;$i++) 
{
	 $n1 = "pa1".$i;
	 $n2 = "pb2".$i;
	 $n3 = "pc3".$i;
	 $n4 = "pd4".$i;
	 $n5 = "pe5".$i;
	 $n6 = "pf6".$i;
	 $n7 = "pg7".$i;

	my $locCode =param("$n1");
	my $city =param("$n2");
	my $location =param("$n3"); #uncommented for mail
	#my $type =param("$n4");
	my $planDate =param("$n5");

	my @arsdate=split('-',$planDate);
	$planDate=$arsdate[2]."-".$arsdate[1]."-".$arsdate[0];
	
	my $planTime =param("$n6");
	my $planCode =param("$n7");
	my$YY = substr $arsdate[2],-2;
	my $visitCode=$locCode.$arsdate[0].$arsdate[1].$YY.$shID;

	#### Outer Loop Begins here
	if($planDate ne '--')
	{
		
		$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";

		$sthPC=$dbh->prepare("SELECT planCode  from cf_visitPlan WHERE planCode=$planCode and hasMailed='F'");
		$sthPC->execute;
		if($sthPC->rows==1)
		{
			my $strsql="INSERT INTO cf_visitDetail(planCode,visitCode,locCode,visitDate,visitTime,shID,hasMailed,hasVisited)VALUES ($planCode,'$visitCode','$locCode','$planDate','$planTime',$shID,'T','F')";
			my $rv=$dbh->do($strsql);

			if($rv==1)
			{
				my $strSQL="update cf_visitPlan set hasMailed='T' where planCode='$planCode'";
				my $rv=$dbh->do($strSQL);
				
				my $lnk = "http://www.arabdemog.com/reg/reg.pl?locCode=$locCode&planDate=$planDate&visitCode=$visitCode";
				$comments="$comments  $lnk";

				$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 $!"); 
				$k=$k+1;
				$Alloc=$Alloc."<TR><TD align='center'>$k</TD><TD>$city</TD><TD>$location</TD><TD>$planDate</TD><TD>$planTime</TD></TR>";
				$FLG1=1;
				$msg="Visit Plan submitted successfully!!!";
			}
			
		}
		else
		{
			$m=$m+1;
			$FLG=1;
			$unAlloc=$unAlloc."<TR><TD align='center'>$m</TD><TD>$city</TD><TD>$location</TD><TD>$planDate</TD><TD>$planTime</TD></TR>";
		}
		
	}
	### Outer Loop Ends here

}
$dbh->disconnect;
if($FLG1==1)
{
 	$Alloc="<P style='margin:20px;'><font size='4' color='green'><b><u>Allocated Visit(s)</u></b></font></P><table class='mtb'><TR><TH class='ath' width='3%'>S.No.</TH><TH class='ath' width='17%'>City</TH><TH class='ath'  width='40%'>Location</TH><TH class='ath'  width='20%'>Selected Date</TH><TH class='ath'  width='20%'>Time</TH></TR>" .$Alloc."</TABLE>";
}
else
{
	$Alloc="&nbsp;";
}
if($FLG==1)
{
 	$unAlloc="<P style='margin:20px;'><font size='4' color='Red'><b><u>Unallocated Visit(s)</u></b></font></P><table class='mtb'><TR><TH class='uth' width='3%'>S.No.</TH><TH class='uth' width='17%'>City</TH><TH class='uth'  width='40%'>Location</TH><TH class='uth'  width='20%'>Selected Date</TH><TH class='uth'  width='20%'>Time</TH></TR>" .$unAlloc."</TABLE>";
}
else
{
	$unAlloc="&nbsp;";
}
print<<blk1;
<div>
<tableborder="1"cellpadding="0"cellspacing="0"width="100%"bordercolor="#000080"height="359">
<tr>
<tdwidth="100%"height="357">
<p align="center"><fontface="Verdana"color="#000080"size="5"><b>$msg</b></font></P>
</td>
</tr>
</table>
<div id='allo' style='width=80%;border-width: .01em;border-style: solid;border-color: #00000;'>
$Alloc
</div>
<br>
<div id='uallo' style='width=80%;border-width: .01em;border-style: solid;border-color: #00000;'>
$unAlloc
</div>
<br>
<div style='width=80%;border-width:0;text-align:center'>
<input type="button" id='pri' value="Print" onClick="hidebutt();">&nbsp;
<input type="button" id='clo' value="Close" onClick="window.close();">
<div>
<script type="text/javascript">
function flgblok()
{
	var f1=$FLG1;
	var f2=$FLG;
	if(f1==0)
	{
		document.getElementById("allo").style.display="none";
	}
	if(f2==0)
	{
		document.getElementById("uallo").style.display="none";
	}
}
</script>
</body>
</html>
blk1
