#!/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;

my $conCode= url_param('conCode');
my $natCode=url_param('natCode');
my $areaName=url_param('areaName');
my $selValue=url_param('selValue');
my $gender= url_param('gender');

my $hasVisited= url_param('hasVisited');


if ($conCode eq '')
{
 $conCode= "All";
}
if ($natCode eq '')
{
 $natCode= "All";
}
if ($areaName eq '')
{
 $areaName= "All";
}
if ($selValue eq '')
{
 $selValue= "All";
}
if ($gender eq '')
{
 $gender= "All";
}
#if ($hasVisited eq '')
#{
# $hasVisited= "All";
#}

$strCon="select distinct conCode,conName from cf_location";
$sthCon=$dbh->prepare($strCon);
$sthCon->execute;

###
$strNat="select distinct natCode,natName from nationality";
$sthN=$dbh->prepare($strNat);
$sthN->execute;
###


$strArea="select distinct area from shopper";
$sthArea=$dbh->prepare($strArea);
$sthArea->execute;

#$strS="select distinct ShID,fName,lName from shopper";
#$sthS=$dbh->prepare($strS);
#$sthS->execute;
###

######Checking for Country
$strCond="";
if ($conCode ne "All")
{
	$strCond="s.country='$conCode'";
}

#########Checking for Nationality
if ($natCode ne "All")
{
	if ($strCond ne "") 
	{
		$strCond="$strCond and s.nationality='$natCode'";
	}
	else
	{
		$strCond="s.nationality='$natCode'";
	}
}

######Checking for City
if ($areaName ne "All")
{
	if ($strCond ne "") 
	{
		$strCond="$strCond and s.area='$areaName'";
	}
	else
	{
		$strCond="s.area='$areaName'";
	}
}


#########Checking for Gender
if ($gender ne "All")
{
	if ($strCond ne "") 
	{
		$strCond="$strCond and gender='$gender'";
	}
	else
	{
		$strCond="gender='$gender'";
	}
}
#########Checking for Visited
#if ($hasVisited ne "All")
#{
#	if ($strCond ne "") 
#	{
#		$strCond="$strCond and hasVisited='$hasVisited'";
#	}
#	else
#	{
#		$strCond="hasVisited='$hasVisited'";
#	}
#}

##
#if ($typeCode ne "All")
#{
#	if ($strCond ne "") 
#	{
#		$strCond="$strCond and typeCode='$typeCode'";
#	}
#	else
#	{
#		$strCond="typeCode='$typeCode'";
#	}
#}
##
#########Checking for Shopper
#if ($shID ne "All")
#{
#	if ($strCond ne "") 
#	{
#		$strCond="$strCond and v.shID='$shID'";
#	}
#	else
#	{
#		$strCond="v.shID='$shID'";
#	}
#}
#########
if ($strCond eq "")
{
	$strVisit="SELECT s.ShID,s.fName,s.lName,s.gender,n.natName,s.mobile,s.email,s.area from shopper s,nationality n where s.nationality=n.natCode";
}
else
{
	$strVisit="SELECT s.ShID,s.fName,s.lName,s.gender,n.natName,s.mobile,s.email,s.area from shopper s,nationality n where s.nationality=n.natCode and $strCond";
}
$sthV=$dbh->prepare($strVisit);
$sthV->execute;
$cnt=$sthV->rows;

print <<blk1;
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 xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>PAN ARAB RESEARCH CENETER - Mystery Shopping</title>
<link href="shopper.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function plcCityName()
{
	frm=document.frmMain;
	frm.conCode.value="$conCode";
	frm.areaName.value="$selValue";
	frm.natCode.value="$natCode";
    frm.gender.value="$gender";

    //frm.hasVisited.value="$hasVisited";
    
	document.getElementById("msg").style.display="none";
	document.getElementById("tbl").style.display="";
	if("$cnt"==0)
	{
		document.getElementById("msg").style.display="";
		document.getElementById("tbl").style.display="none";
	}
}

function selfpost()
{

	var selObj = document.getElementById('areaName');
	var selIndex = selObj.selectedIndex;
	var areaName = selObj.options[selIndex].text;
	var selValue = selObj.options[selIndex].value;

	var natCode = document.getElementById('natCode').value;
	var conCode = document.getElementById('conCode').value;
	var gender = document.getElementById('gender').value;

	//var typeCode = document.getElementById('typeCode').value;
	
	//var path="http://localhost:8081/cf/visits/adminvisits/shopper.pl?conCode=" + conCode + "&natCode="+ natCode + "&areaName="+ areaName + "&selValue=" + selValue + "&gender="+gender;
	var path="http://www.arabdemog.com/cf/visits/shopper.pl?conCode=" + conCode + "&natCode="+ natCode + "&areaName="+ areaName + "&selValue=" + selValue + "&gender="+gender;
	lnk=encodeURI(path);
	document.frmMain.method="post";
    document.frmMain.action=lnk;
	document.frmMain.submit();
    return true;
}
</script>
</head>

<body style="background-color: #666666;color: inherit;" onload="plcCityName();">
<table class="TBL">
<tr>
<td style="width:3%;background-color:#666666">&nbsp;</td>


<td style="width:94%;background-color: #FFFFFF">
<div style='height:70px;padding-top: 15px;padding-bottom: 5px;line-height: 3px;'><h1 style='padding-left: 30px;color: #607291;padding-right: 30px;font-size:1.5em;'>PAN ARAB RESEARCH CENTER</h1>
<p style='padding-left: 30px;color: #607291;padding-right: 30px;font-size:1.2em;'>Shopper Details</p>
</div>
<div id="navbar">&nbsp; </div>
<div id="content" >
<form method="post" name="frmMain">

<div id="srch" style="border:0px solid black;vertical-align:middle;padding-right:15px;"><font size='2'color='#000000'><p align="right"><b>Country:</b>


<select name="conCode" id="conCode"  style="width:90px;border:1px solid #666666;">
                    <option value="All">All</option>
blk1
 while (@cont=$sthCon->fetchrow_array())
    {
        
        print "<option value=$cont[0]>$cont[1]</option>";
    }
print <<blk2;
</select>
&nbsp;<b>Nationality:</b>
<select name="natCode" id="natCode"  style="width:90px;border:1px solid #666666;">
                    <option value="All">All</option>

blk2
while (@nat=$sthN->fetchrow_array())
    {
        
        print "<option value=$nat[0]>$nat[1]</option>";
    }
print <<blk3;
</select>
&nbsp;<b>Area:</b>
<select name="areaName" id="areaName"  style="width:90px;border:1px solid #666666;">
                    <option value="All">All</option>
blk3
while (@ar=$sthArea->fetchrow_array())
    {
        
        print "<option value=$ar[0]>$ar[0]</option>";
    }
print <<blk4;
</select>
&nbsp;<b>Gender:</b>
<select name="gender" id="gender"  style="width:90px;border:1px solid #666666;">
                    <option value="All">All</option>
					<option value="1">Male</option>
					<option value="2">Female</option>
</select>
<a href="#" style="text-decoration: none;" onclick="selfpost();"><img src="go.png" style='vertical-align:middle;' alt="GO" height="25" border="0" width="25"/> </a>
</p>
</font>
</div>
<div id="msg" style="display:none"><font size='3'color='#000000'><p align="center">No Records to Display</p></font></div>

<font size='2'color='#000000'>
<table width="100%" align='center' class="Mtable" id="tbl">
<tr height='25' bgcolor='#97a8bb'>
<td class="Mtd" width="6%" style="font-size:0.86em;padding-left: 2px;"><b>ID</b></td>
<td class="Mtd" width="25%" style="font-size:0.86em;padding-left: 2px;"><b>Name</b></td>
<td class="Mtd"width="7%" style="font-size:0.86em;padding-left: 2px;"><b>Gender</b></td>
<td class="Mtd" width="12%" style="font-size:0.86em;padding-left: 2px;"><b>Nationality</b></td>
<td class="Mtd" width="22%" style="font-size:0.86em;padding-left: 2px;"><b>e-mail</b></td>
<td class="Mtd" width="13%" style="font-size:0.86em;padding-left: 2px;"><b>Phone No.</b></td>
<td class="Mtd" width="15%"  style="font-size:0.86em;padding-left: 2px;"><b>Area</b></td>

</tr>
<!-- while loop to populate rows -->

blk4
my $i= 0;
while ($visit=$sthV->fetchrow_hashref())
   	{
		$i=$i+1;
	my $ShID ="$visit->{'ShID'}";
	my $name ="$visit->{'fName'} $visit->{'lName'}";
	my $gender ="$visit->{'gender'}";
	if($gender==1)
		{
		$gender='Male';
		}
	else
		{
		$gender='Female';
		}
	my $natName ="$visit->{'natName'}";
	my $mobile ="$visit->{'mobile'}";
	my $email ="$visit->{'email'}";
	my $area ="$visit->{'area'}";

	$mod=$i%2;
	
	if($mod == 0)
	{
		$col='#D7DADE';
	}
	else
	{
		$col='#FFFFFF';
	}
print <<blk5;


<tr height='25' valign='center' bgcolor='$col'>
<font color='#000000' size='0.8em' face="Verdana, Arial, Helvetica, sans-serif"><b>
<td class="Mtd">
$ShID
</td>
<td class="Mtd">
$name
</td>
<td class="Mtd">
$gender
</td>
<td class="Mtd">
$natName
</td>
<td class="Mtd">
$email
</td>
<td class="Mtd">
$mobile
</td>
<td class="Mtd">
$area
</td>
</b></font>
</tr>
blk5
	
	}
print <<blk7;

<!-- End while loop -->
<!--
<tr>
<input type="hidden" value="$i" name="cnt" id="cnt">
<td colspan="14" align="center">&nbsp;</td>
</tr>
<tr>
<td colspan="14" align="center"><input type="button" Value="Re-send" name="resend" onclick="doresend();" /></td>
</tr> -->
</table>
</font>






</form>
</div>

<div id="footer">
<p class="footerpmiddle"><br /></p>
</div>	

</td>



<td style="width:3%;background-color:#666666"></td>
</tr>
</table>

</body>
</html>
blk7
