<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
include("config.inc.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$query1="select grid_color1, grid_color2, DecimalBase, DecimalBase1, DecimalBase2, base1, base2 from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}



set_time_limit(0);


   
$frm	   			= $_GET['frm']; 				if ($frm == "")				 {	$frm 				= $_POST["frm"];				}
$LocType			= $_GET['LocType']; 			if ($LocType == "") 		 {	$LocType 			= $_POST['LocType'];				}
$perpage   			= $_GET['perpage']; 			if ($perpage == "")			 {	$perpage 			= $_POST["perpage"];			}
$startrange 		= $_GET['startrange']; 			if ($startrange == "")		 {	$startrange 		= $_POST["startrange"];			}

$PItem_Description  = $_POST["PItem_Description"]; 	if ($PItem_Description == ""){	$PItem_Description 	= $_GET["PItem_Description"];	}
$PItem_Description  = $_POST["PItem_Description"]; 	if ($PItem_Description == ""){	$PItem_Description 	= $_GET["PItem_Description"];	}

if ($frm == '2')
   $type = "Type";
else {
if ($LocType == '1')
	$type = "State";
else if ($LocType == '2')
   $type = "LGA";
else if ($LocType == '3')
   $type = "Region";
}

if($_GET['lcode']!=""){
	if ($frm == '2')
		$query1 = "select type_code as code, type_description as descp from support_infoType where type_code = '".$_GET['lcode']."' ";
	else
	$query1="select Location_code as code, Location_description as descp
			from LocationSetup where Location_code = '".$_GET['lcode']."' and Location_type = $LocType";
	
	$result1=mysql_query($query1,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result1)){
    	extract($rows);
	}

    $_GET['post']="1";
	
}



?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Select a <?php echo $type; ?></title>
<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<script type="text/javascript" language="javascript">

function clearform()
{
	document.form1.PLoc_Code.value='';
	document.form1.PLoc_Description.value='';
	vFrm       = document.form1.frm.value;
	vLocType   = document.form1.LocType.value;
	document.form1.action='List_LocationByType.php?frm='+vFrm+'&LocType='+vLocType;
	document.form1.submit();
}
function SetNextPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vLocType   = document.form1.LocType.value;
	
	document.form1.action='List_LocationByType.php?lcode=&frm='+vFrm+'&LocType='+vLocType+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vLocType   = document.form1.LocType.value;
	
	document.form1.action='List_LocationByType.php?lcode=&frm='+vFrm+'&LocType='+vLocType+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}
function RefreshPage(){
	vFrm       = document.form1.frm.value;
	vLocType   = document.form1.LocType.value;
	vPerPage   = document.form1.perpage.value;
	
	document.form1.action='List_LocationByType.php?lcode=&frm='+vFrm+'&LocType='+vLocType+'&startrange=0&perpage='+vPerPage;
	document.form1.submit();
}


</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
	font-size: 11px;
}
.style3 {color: #999999}
.style4{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
-->
</style></head>

<body>

<?php

echo "
<form id='form1' name='form1' method='post' action=''>
  <table width='760' border='0' cellspacing='1' align='center' style='table-layout:fixed'>
	<col width=25>
	<col width=200>
	<col width=550>";
  

echo "
    <tr>
      <th colspan='3' bgcolor='#DDDDDD'><span class='style1'> Search </span></th>
    </tr>
    <tr>
      <td bgcolor='#DDDDDD' colspan='2'> <span style='font-size: 11px; color: #999999; font-weight: bold;'>Search for $type  <br> Code / Desc.</span></td>
      <td bgcolor='#DDDDDD' >
		   
	        <input type='text' name='PLoc_Code' value='$PLoc_Code' placeholder='Code' autofocus style='width: 120px;' />
            <input type='hidden' name='code1' id='code1'  value='$code'>
			<input type='hidden' name='code2' id='code2'  value='$descp'>
			<input type='hidden' name='LocType' id='LocType'  value='$LocType'>
			<input type='hidden' name='frm' id='frm'  value='$frm'>
	        <input type='text' name='PLoc_Description' placeholder='Description'  value='$PLoc_Description' style='width: 150px;'  />
			<input type='submit' name='Submit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='cancelbtn' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
			
				
			echo"<span style='font-size: 11px; color: #999999; font-weight: bold;'>Result per page </span><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();' >";
              


        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';

			
				
        echo"<option ".$s20." value='20'>20</option>
			 <option ".$s30." value='30'>30</option>
			 <option ".$s40." value='40'>40</option>
			 <option ".$s50." value='50'>50</option>
			 </select></td>

    </tr>";
?>	
<?php if($_GET['post']=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var LocType = document.form1.LocType.value;

if (FormOption == 1) {
	if (LocType == 1)
		opener.document.getElementById('pState').value=document.form1.code1.value;
	if (LocType == 2)
		opener.document.getElementById('pLga').value=document.form1.code1.value;
	if (LocType == 3)
		opener.document.getElementById('pRegion').value=document.form1.code1.value;
	
} else if (FormOption == 2) {
	opener.document.getElementById('pInfoType').value=document.form1.code1.value;
}
self.close();


</script>
<?php } ?>

<?php

echo" <tr bgcolor='#E5E5E5'>
        <td ><span class='style2'>&nbsp</span></td>
		<td ><span class='style2'>$type Code </span></td>
        <td ><span class='style2'>$type Description </span></td>";

echo" </tr>";


if ($frm == 2){
	$query = "select type_code as code, type_description as descp from support_infoType where 1=1 ";
	if ($PLoc_Code != '') 		 $query .= " and type_code like '$PLoc_Code%' ";
	if ($PLoc_Description != '') $query .= " and type_description like '%$PLoc_Description%' ";
	$query = $query." order by type_code  ";
} else {

	$query = "select Location_code as code, Location_description as descp  from LocationSetup where Location_type = $LocType ";
	if ($PLoc_Code != '') 		 $query .= " and Location_code like '$PLoc_Code%' ";
	if ($PLoc_Description != '') $query .= " and Location_description like '%$PLoc_Description%' ";
	$query = $query." order by Location_code  ";
}
	//echo $query;//exit;	

if($_GET['post']!="1") {
	
	$resultsearch=mysql_query($query,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($resultsearch);
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 20;
		
	$query = $query." limit ".$startrange.",".$perpage;
	//echo $query;exit;
	$result1=mysql_query($query,$connection) or die(mysql_error());
	
	$ct = 0;
	while($rows=mysql_fetch_array($result1)){
		extract($rows);

		$ct+=1;

				
		if($ct%2==0)
		{
			$Color="style='color:#0000FF;background-color:".$grid_color1."'";
		} else {
			$Color="style='color:#0000FF;background-color:".$grid_color2."'";	
		}
		
			echo "<tr  $Color > 
				<td align='center'></td>
				<td align='left' ><span class='style4'><a href='List_LocationByType.php?frm=$frm&LocType=$LocType&lcode=$code'>$code</a></span></td>
				<td align='left' ><span class='style4'>$descp </span></td>";
			echo "</tr>";


	}
	
	echo"<tr><td colspan='6'>&nbsp;</td></tr>
		 <tr><td colspan='6'><table width='100%' border=0><tr><td width='270'>&nbsp;</td><td width='110' align='center'>";
		
	$diff= $startrange - $perpage;
		
	if (($diff) >= 0 ){
		$prvstartrange = $startrange - $perpage;
		echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
	}
	else
		echo "&nbsp;";
		
	echo"</td><td width='110' align='center'>";
		
		
	if ($startrange <= $totalrows){
		$startrange = $startrange + $perpage;
		if  ($startrange <= $totalrows)
			echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
	} else
		echo "&nbsp;";
			
	echo"</td><td width='270'>&nbsp;</td></tr>";
	echo"</table>";

	echo"</td></tr>";
}



?>
  </table>
</form>
</body>
</html>
