#!/usr/bin/perl -w
use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $locCode=param("locCode");
my $username=param("user");
my $userpassword=param("pass");
my $location =param("location");
my $frmType = param("frmType");
my $visitCode=param('visitCode');
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";

my $strQuery="select ShID,fName,nationality,N.natName,gender,age,country from shopper S, nationality N where S.nationality=N.natCode and username='$username' and userpassword='$userpassword' and forCF='T'"; 
$sthN=$dbh->prepare($strQuery);
$sthN->execute or die "Couldn't execute statement: " . $dbh->errstr;
#$intCount=$sthN->rows;
$intCount=0;
my @Sha;
while (@Sh=$sthN->fetchrow_array())
   	{
		$intCount=1;
		$Sha[0]="$Sh[0]";
		$Sha[1]="$Sh[1]";
		$Sha[2]="$Sh[2]";
		$Sha[3]="$Sh[3]";
		$Sha[4]="$Sh[4]";
		$Sha[5]="$Sh[5]";
		$Sha[6]="$Sh[6]";
	}

$sthVisit=$dbh->prepare("select hasVisited, hasMailed from cf_visitDetail where visitCode='$visitCode'");
$sthVisit->execute or die "Couldn't execute statement: " . $dbh->errstr;

while ($hasvisited=$sthVisit->fetchrow_hashref())
   	{
		$visited="$hasvisited->{'hasVisited'}";
		$mailed="$hasvisited->{'hasMailed'}";
	}


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

<html>
<head>
<script type="text/javascript">
function docheck()
{
	
		
	if ("$locCode" != "" && $intCount==1 && "$frmType"=="" && "$visited"=="F" && "$mailed"=="T") 
	{
	document.getElementById('shID').value="$Sha[0]";
	document.getElementById('fName').value="$Sha[1]";
	document.getElementById('Nationality').value="$Sha[2]";
	document.getElementById('natName').value="$Sha[3]";
	document.getElementById('Gender').value="$Sha[4]";
	document.getElementById('Age').value="$Sha[5]";
	document.getElementById('locCode').value="$locCode";
	document.getElementById('visitCode').value="$visitCode";
	document.frmMain.method="post";

	document.frmMain.action="http://www.arabdemog.com/cf/cf.pl";
	

	document.frmMain.submit();
	return true;

	}
	else if("$locCode" != "" && $intCount==1 && "$frmType"==""  && "$visited"=="T" && "$mailed"=="T")
	{
		window.location = "http://www.arabdemog.com/reg/reg.pl?inv=5&locCode=$locCode&visitCode=$visitCode";
		return true;
	}
	else if("$locCode" != "" && $intCount==1 && "$frmType"=="" && "$visited"=="F" && "$mailed"=="F")
	{
		window.location = "http://www.arabdemog.com/reg/reg.pl?inv=6&locCode=$locCode&visitCode=$visitCode";
		return true;
	}
	else if("$intCount"==0)
	{
		if("$locCode"!="")
		{
			window.location = "http://www.arabdemog.com/reg/reg.pl?inv=1&locCode=$locCode&visitCode=$visitCode";
			return true;
		}
		else if("$frmType"!="")
		{
			window.location = "http://www.arabdemog.com/reg/reg.pl?inv=1&frmType=$frmType";
			return true;
		}
		else if("$locCode"=="" && "$frmType"=="")
		{
			window.location = "http://www.arabdemog.com/reg/reg.pl?inv=1";
			return true;
		}
		
	}
	else if("$locCode" == "" && "$intCount"==1 && "$frmType"==3)
	{

		document.getElementById('shID').value="$Sha[0]";
		document.getElementById('fName').value="$Sha[1]";
		document.getElementById('Nationality').value="$Sha[2]";
		document.getElementById('natName').value="$Sha[3]";
		document.getElementById('Gender').value="$Sha[4]";
		document.getElementById('Age').value="$Sha[5]";
		document.getElementById('locCode').value="$locCode";
		document.getElementById('conCode').value="$Sha[6]";
		document.frmMain.method="post";
		document.frmMain.action="http://www.arabdemog.com/cf/visits/location.pl";
		document.frmMain.submit();
			
			return true;
	}
	else if("$locCode" != "" && "$intCount"==1 && "$frmType"!="")
	{
			window.location = "http://www.arabdemog.com/reg/reg.pl?inv=2";
			return true;
	}
	else
	{
		window.location = "http://www.arabdemog.com/reg/reg.pl?inv=3";
		return true;
	}
	
}
</script>
</head>

<body onload="docheck();">

<form  name="frmMain" method="post" >
	<input type="hidden" id='shID' name='shID'>
	<input type="hidden" id='Nationality' name='Nationality'>
	<input type="hidden" id='Gender' name='Gender'>
	<input type="hidden" id='Age' name='Age'>
	<input type="hidden" id='fName' name='fName'>
	<input type="hidden" id='natName' name='natName'>
	<input type="hidden" id='locCode' name='locCode'>
	<input type="hidden" id='conCode' name='conCode'>
	<input type="hidden" id='visitCode' name='visitCode'>
</form>
</body>
</html>
blk1
