<?php

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

include("config.inc.php");
include("Functions_UserAccess.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");

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

//Getting Global Values
$perpage = $_POST["perpage"]; if ($perpage == ""){$perpage = $_GET["perpage"];}
$keyword = $_POST["keyword"]; if ($keyword == ""){$keyword = $_GET["keyword"];}
$startrange = $_POST["startrange"]; if ($startrange == ""){$startrange = $_GET["startrange"];}
$message = $_POST["message"]; if ($message == ""){$message = $_GET["message"];}
 
 




	

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<title>Search </title>

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

function EditRecord(code,name){

opener.salesmen_commission.salesmen_code.value =code;

opener.salesmen_commission.salesmen_name.value=name;
opener.salesmen_commission.submit();
	self.close();
	
}

function clearform(){
	document.search.perpage.value='';
	document.search.keyword.value='';
	document.search.action='Form_SearchSalesmen.php';
	document.search.submit();
}


function changeorder(orderby){
var direction;
direction = document.search.direction.value;

	if(document.search.orderby.value==orderby)	{

		if(direction=='asc')
			direction = 'desc';
		else		
			direction = 'asc';
	}
	else
		direction = 'desc';

	document.search.orderby.value = orderby;
	document.search.direction.value = direction;
	
	document.search.action='Form_SearchSalesmen.php?orderbytitle='+orderby+'&direction='+direction;
	document.search.submit();

}


function LoadForm() {
	x=document.search;
	x.keyword.focus();
}

function SetNextPage(startrange,perpage){
	document.search.action='Form_SearchSalesmen.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	document.search.action='Form_SearchSalesmen.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function set_limit(){
	perpage=document.search.perpage.value;
	document.search.action='Form_SearchSalesmen.php?perpage='+perpage;
	document.search.submit();
}

function DeleteRecord(Id) {
	
	if (confirm ( "Are you sure you want to Delete this Record ?")){
		document.search.action="Form_proforma_Action.php?actionmode=delete&proforma_id="+Id;
		document.search.submit();
	}
}					

function SearchKeyword(){
	document.search.submit();
}

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

function SetLastPage(perpage,totalrows){
	var Result=totalrows % perpage;
	startrange=totalrows-Result;
	document.search.action='Form_SearchSalesmen.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

</script>		
</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>
<input type='hidden' name='direction' value='$direction'> 
<input type='hidden' name='orderby' value='$orderby'>
<input type='hidden' id = 'rownumber' name='rownumber' value='$rownumber'>
HERE;

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='750'>";
		     
echo"<tr><td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	 <td id='search-top2' style='height:34px;width:688px' align='left' valign='bottom'>
		  <table border='0'>
		<tr><td><span class='style3'>&nbsp; List of Salesmen</span></td></tr>
		<tr height='2px'><td></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' >
		<table border='0' cellspacing='1' style='table-layout:fixed' width='688px'>
		<col width=200>
		<col width=300>";


if ($r1=='a')
	$a = ' checked ';
else if ($r1=='g')
	$g = ' checked ';
else if ($r1=='p')
	$p = ' checked ';
else if ($r1=='c')
	$c = ' checked ';
else $a = ' checked ';
	
echo"<tr class='Search-header-bg' height=34px>
	    <td align='center'><span class='style4'>Search</td>
		<td align='left'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30'/>
		 &nbsp;<button id='btnFind' name='btnFind' onclick='SearchKeyword(); return false;' title='Find'><span><span>Find</span></span></button>
		<button id='btnCancel' name='btnCancel' onclick='clearform(); return false;' title='Clear'><span><span>Clear</span></span></button></td></tr>";
	

	if($message!=''){
		echo"<tr class='Search-header-bg'>
		<td height='15' align='center' colspan='2' class='message'>".$message."</td>
		</tr>";
	}
	echo"</table></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";
	
	$querysearch="select  ledger_number,ledger_name
				
					from salesmen where 1 = 1 ";
					
			if($keyword != ""){
		$querysearch=$querysearch." and (ledger_number like"."('%".$keyword."%') or ledger_name like"."('%".$keyword."%') ) ";
}
	
	
	if($orderby!='' and $direction!='')
		$querysearch=$querysearch." order by ".$orderby." ".$direction." ";

	
//echo $querysearch;

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	

echo"<tr >
		<td  id='search-content1' style='width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px; width:688px;'><table width='100%' border=0><tr>";
	echo"<td colspan=3 align='right' style='width:270px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange='set_limit();'>";
              
		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."')>";

		$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."','".$option."')>";
		}
		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."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}
		} else
			echo "&nbsp;";
	
			
		echo"</td><td style='width:270px;'>&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-y:scroll;height:340px;width:688px;'><table border='0' style='table-layout:fixed' cellpadding='0' cellspacing='1' width='670'>

	<col width =110>
	<col width=455>

	
	";	
	 echo"<tr class='Search-Label-bg'>

		<td  height='25' align='center'><span class='style4'>Code</span></td>
		<td  height='25' align='center'><span class='style4'>Description </span></td>
		
			
		
	
		
	
		
		</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  onMouseOver=\'style.cursor="hand"\'   '.$Color.'>';

	echo"
		<td ><span class='style5'><a onclick=\"EditRecord('$rows[0]','$rows[1]')\">$rows[0]&nbsp;</a></span></td>
		<td ><span class='style5'><a onclick=\"EditRecord('$rows[0]','$rows[1]')\">$rows[1]&nbsp;</a></span></td>
		</tr>";

}
		
echo"</div></table></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";
		
echo"   <tr ><td id='search-content1'>&nbsp;</td><td id='search-content2'>&nbsp;</td><td id='search-content3'>&nbsp;</td></tr>";
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:688px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:270px;'>&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."','".$option."')>";

		//$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."','".$option."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='left' style='width:74px;'>";
		
		
		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."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}
		} else
			echo "&nbsp;";
	
			
		echo"</td><td style='width:270px;'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

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

?>											
	