<?php

header("Content-type: text/html; charset=windows-1256");

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


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");


$PMode    	= $_POST['PMode']; if ($PMode == ""){$PMode = $_GET["PMode"];}
$keyword    = $_POST['keyword']; if ($keyword == ""){$keyword = $_GET["keyword"];}
$searchby   = $_POST['searchby']; if ($searchby == ""){$searchby = $_GET["searchby"];}
$perpage    = $_POST['perpage']; if ($perpage == ""){$perpage = $_GET["perpage"];}
$startrange = $_POST['startrange']; if ($startrange == ""){$startrange = $_GET["startrange"];}



if (empty($searchby)) $searchby = 1;

if ($startrange=='') $startrange = 0;
if ($perpage=='')	 $perpage = 20;

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Search Staff</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.housesearch.keyword.value='';
  document.housesearch.action='Form_HouseSearch.php';
  document.housesearch.submit();

}

function SetNextPage(startrange,perpage,category){
	
	document.housesearch.action='Form_HouseSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage+'&category='+category;
	document.housesearch.submit();
}

function SetPrvPage(startrange,perpage,category){
	
	document.housesearch.action='Form_HouseSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage+'&category='+category;
	document.housesearch.submit();
}



function DeleteRecord(housecode){

	if (housecode != '') {
		if (confirm ( "Are you sure you want to Delete the staff with code = "+housecode+" ?") )
		{
			document.housesearch.action="house_action.php?actionmode=deletestaff_house&housecode="+housecode;
			document.housesearch.submit();
		}
	}
	
}

function UpdateRecord(housecode){
		
	opener.document.formhouse.actionmode.value='edit';
	opener.document.formhouse.house_code.value=housecode;
	opener.document.formhouse.submit();
	self.close();
}



function SelectSearchBy()
{	
 
  //document.search.keyword.value='';
  document.housesearch.action='Form_HouseSearch.php?startrange=0';
  document.housesearch.submit();

}

function SubmitForm()
{	

  //document.search.keyword.value='';
  document.housesearch.action='Form_HouseSearch.php?startrange=0';
  document.housesearch.submit();

}

function kd(e)
{
	x=document.housesearch;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		return false;
    }
    return true;
}


function RefreshPage()
{
   document.housesearch.action='Form_HouseSearch.php?startrange=0';
  document.housesearch.submit();

}


function SetFirstPage(startrange,perpage,category){
	document.housesearch.action='Form_staffSearch.php?startrange=0&perpage='+perpage+'&category='+category;
	document.housesearch.submit();
}

function SetLastPage(perpage,totalrows,category){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.housesearch.action='Form_HouseSearch.php?startrange='+startrange+'&perpage='+perpage+'&category='+category;
	document.housesearch.submit();
}




</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='housesearch' method='post' action=''>
HERE;

$PRef = GetSQLValueString($keyword, 'text');
echo $pref;

			
$querysearch="select house_code, House_name from staff_house ";

							 
$querysearch= $querysearch.											 
			 " Where ($searchby = 2 and house_name Like Concat(ifNull($PRef, house_name),'%')) Or ".
			 "        ($searchby = 1 and  house_code Like Concat(ifNull($PRef,  house_code),'%')) ";
		
			 
//echo $querysearch;			 

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);

		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		


echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:569px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of House
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
echo"<table border='0' cellspacing='1'  style='table-layout:fixed'>
	  <col width=20> <col width=20><col width=160>
			  <col width=200>
			  <col width=200>";
	  

echo" <tr class='Search-header-bg'>	  
	    <td align='left' colspan='3'><span class='style4'>Search By</span></td>
		<td align='left' colspan='2'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30' onkeydown='return kd(event)'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
		
			<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
		
			if ($searchby==1)
				$s21 = 'selected';
			else if ($searchby==2)
				$s22 = 'selected';
							
			echo"<option ".$s21." value='1'>house code</option>
				 <option ".$s22." value='2'>house name</option>";
			echo" </select>";	
				 
echo"	    <button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='clearform();return false;' title='Cancel'><span><span>Cancel</span></span></button>
			</td>
		<td align='right' valign='center' ></td>
		<td align='left' valign='center' ></td>";
			 
		
echo" </tr>
</table>
		</td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>"; 
echo" </tr>";

echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:600px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><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></span></td>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."','".$category."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."','".$category."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."','".$category."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."','".$category."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";			

	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:600px;'>
			<table border='0'  cellpadding='0' cellspacing='1' style='width:600px;'>
			  <col width=20> <col width=20><col width=130><col width=160>
			  ";
	  
	if($message!=''){
		echo"<tr class='Search-header-bg'>
				<td height='15' align='center' colspan='16' class='searchmessage'>".$message."</td>
			</tr>";
	}
	
	echo"<tr class='Search-Label-bg' >
			<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
			<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
			<td  height='25' align='center'><span class='style4'>House Code </span></td>
			<td  height='25' align='center'><span class='style4'>House Name </span></td>";
		
	
//echo" <td  height='25' align='center'><span class='style4'>Last Name</span></td>";

echo " </tr>";

while($rows=mysql_fetch_array($resultsearch)){
		
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			
	
	
	echo'<tr '.$Color.'>'; 
	
	echo'<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord(\''.$house_code.'\');" title="'.$EditHint.'"  style="width: 20px; height: 20px; "></span></td>';
		
	echo'<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord(\''.$house_code.'\');" title="'.$DelHint.'" style="width: 20px; height: 20px; "></span></td>';
	echo'	<td align="left"><span class="style5">'.$house_code.'</span></td>
			<td align="center"><span class="style5">'.$House_name.'</span></td>';
	
	
		
	echo '</tr>';
	
}
							

			
			
echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";		
		
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:569px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:280px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."','".$category."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."','".$category."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='left'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."','".$category."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."','".$category."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

/*echo"<div id='PrintOptions' style='position:absolute; display:none; z-index:9999999; width:200px; height:150px; border:2px solid #E6E6E6; background-color:#F7F7F7;'>
		<div style='position:relative; float:left; clear:both; margin:10px 0 0 10px;'>
			<input type='radio' name='lang' value='EN' checked>English<br>
			<input type='radio' name='lang' value='FR'>French<br>
			<input type='radio' name='lang' value='AR'>Arabic
		</div>	
		<div style='position:relative; float:left; clear:both; margin:50px 0 0 30px;'>
			<button type='button' id='btnPreview' name='btnPreview' onclick='PrintPreview1(); return false;' title='Preview'><span><span>Preview</span></span></button>
			<button type='button' id='btnExit' name='btnExit' onclick='unloadPopupBox();return false;' title='Close'><span><span>Close</span></span></button></td>
		</div>
	</div>";*/
echo "	<div id='backgroundPopup' style='position:absolute; display:none; z-index:1; width:100%; height:100%; top:0; left:0;'></div>";
          

		  

print <<<HERE
</form>
</body>

HERE;


?>