#!/usr/bin/perl -w
use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
#my $dbh=DBI->connect('dbi:mysql:parc_demog:localhost:3306','root','root') or die "Unable to connect: $DBI::errstr\n";

my $dbh=DBI->connect('dbi:mysql:parc_demog:localhost','parcdemog','parc9904')or die "Couldn't connect to database: " . DBI->errstr;
##Start Uploading
use File::Basename; 
$CGI::POST_MAX = 1024 * 5000; 
my $safe_filename_characters = "a-zA-Z0-9_.-"; 
my $upload_dir = "/var/www/vhosts/arabdemog.com/httpdocs/cf/images"; 
my $query = new CGI; 
my $filename = $query->param("photo"); 
my $visitCode=param('visitCode');
##End Uploading

sub escapedStr{
$out="";
$str=$_[0];
$len=length($str);
for ($i=0;$i<$len;$i++)
{
	$checkChar=substr($str,$i,1);
	if ($checkChar eq "'" || $checkChar eq "\"")
	{
		$out=$out."\\'";
	}
	else
	{
		$out=$out.$checkChar;
	}
}
return $out;
}
print <<blk;
content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
blk

my $country = param("conCode");
my $city = param("cityCode");
my $storeType   = param("typeCode");
my $sDate=param("datum1");	
my @arsdate=split('-',$sDate);
$sDate=$arsdate[2]."-".$arsdate[1]."-".$arsdate[0];
my $tilt=param("tilt");	
my $location= param("locCode");
my $ath=param("ath");
my $atm=param("atm") - 1;
if (param("atap") == 1)
{
	$atampm='AM';
}
elsif (param("atap")== 2)
{
	$atampm='PM';
}
my $aTime=$ath.":".$atm." ".$atampm;
my $shID=param("shID");	
my $Nationality=param("nat");	
my $Gender=param("gender");	
my $Age=param("age");
my $dth=param("dth");	
my $dtm=param("dtm") - 1;
if (param("dtap") == 1)
{
	$dtampm='AM';
}
elsif (param("dtap")== 2)
{
	$dtampm='PM';
}
my $dTime = $dth.":".$dtm." ".$dtampm;
my $stName=param("stName");
my $stGender=param("stGender");	
my $receipt = escapedStr(param("receipt"));	
my $amount=param("amount");	
my $DOW=param("DOW");	
my $questNo = escapedStr(param("questNo")); 

my $q1=param("q1");
my $c1=escapedStr(param("c1"));	
my $q2=param("q2");	
my $c2=escapedStr(param("c2"));	
my $q3=param("q3");	
my $c3=escapedStr(param("c3"));	
   
my $q4=param("q4");	
my $c4=escapedStr(param("c4"));	
my $q5=param("q5");	
my $c5=escapedStr(param("c5"));	
my $q6=param("q6");	
my $c6=escapedStr(param("c6"));	
my $q7=param("q7");	
my $c7=escapedStr(param("c7"));	
my $q8=param("q8");	
my $c8=escapedStr(param("c8"));	
my $q9=param("q9");	
my $c9=escapedStr(param("c9"));	
my $q10=param("q10");	
my $c10=escapedStr(param("c10"));
my $observation=escapedStr(param("c11"));
   


if ($q1 eq "") { $q1=-1; }
if ($q2 eq "") { $q2=-1; }
if ($q3 eq "") { $q3=-1; }
if ($q4 eq "") { $q4=-1; }
if ($q5 eq "") { $q5=-1; }
if ($q6 eq "") { $q6=-1; }
if ($q7 eq "") { $q7=-1; }
if ($q8 eq "") { $q8=-1; }
if ($q9 eq "") { $q9=-1; }
if ($q10 eq "") { $q10=-1; }

##Start Uploading
if ( !$filename ) 
{ 
#print "There was a problem uploading the receipt (try a smaller file)."; 
#exit; 
}
else
{
	my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); 
	$filename = $questNo . $extension; $filename =~ tr/ /_/; $filename =~ s/[^$safe_filename_characters]//g; 
	if ( $filename =~ /^([$safe_filename_characters]+)$/ ) 
	{ 
		$filename = $1; 
	} 
	else 
	{ 
		die "Filename contains invalid characters"; 
	} 

	my $upload_filehandle = $query->upload("photo"); 
	open ( UPLOADFILE, ">$upload_dir/$filename" ) or die " Can't write to  $upload_dir/$filename $!"; 
	binmode UPLOADFILE; 
	while ( <$upload_filehandle> ) 
	{ 
		print UPLOADFILE; 
	} 
	close UPLOADFILE; 
}
##End Uploading


@gettheip=split(/\./,$ENV{'REMOTE_ADDR'});
my $eip="$gettheip[0].$gettheip[1].$gettheip[2].$gettheip[3]";

my $strsql="UPDATE cf_entry SET country=$country,city=$city,storeType=$storeType,sDate='$sDate',tilt='$tilt',location='$location',aTime='$aTime',shID=$shID,Nationality=$Nationality,Gender=$Gender,Age=$Age,dTime='$dTime',stName='$stName',stGender=$stGender,receipt='$receipt',amount=$amount,DOW=$DOW,q1=$q1,c1='$c1',q2=$q2,c2='$c2',q3=$q3,c3='$c3',q4=$q4,c4='$c4',q5=$q5,c5='$c5',q6=$q6,c6='$c6',q7=$q7,c7='$c7',q8=$q8,c8='$c8',q9=$q9,c9='$c9',q10=$q10,c10='$c10',observation='$observation',edate=now(),userip='$eip' where questNo='$questNo'";
		#print $strsql;
	#print $strsql;
	my $rv=$dbh->do($strsql);
	
	if ($rv==1) 
	{
		$msg="Thanks!!!   Updated Successfully";
		my $rv1=$dbh->do("update cf_visitDetail set hasVisited='T' where visitCode='$visitCode'");
		my $rv2=$dbh->do("update cf_visitPlan set hasVisited='T' where planCode in (select planCode from cf_visitDetail where visitCode='$visitCode')");
	}
	else
	{
		$msg="Unable to Update!! Please try later  or contact Admin";
	}


	$dbh->disconnect;
	

print<<blk1;
<div style="margin-left: auto;margin-right: auto;margin-top: auto;margin-bottom: auto;width: 500px;height: 70px;background-color: #AAC6FF;color: #333333;vertical-align:middle;">
<p style="border: 1px;margin-top: 100px;margin-bottom: auto;vertical-align:middle;" align="center"><fontface="Verdana"color="#FFFFFF"size="5"><b>$msg</b></font></p>
</div>
<script language="javascript">
alert("Succefully Updated");
document.location="http://www.arabdemog.com/cf/cf_Edit.pl?ed=true&questNo=$questno";
</script>
</body>

</html>
blk1
