<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}
	
include("config.inc.php");
include("function.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");


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

$querymodule=" Select ifnull(toBeApprovedByHOD,0) as toBeApprovedByHOD ".
			 " From paymentsetup";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

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

print <<<HERE
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<title>Search Applications</title>
<script type="text/javascript" language="javascript">

function SelectRecord(value, value2){

var FormOption = document.search.frm.value; 
var index = document.search.index.value; 

	if(FormOption == 1){
	opener.document.paymentrqst.txtApprovedBy.value=value;	
	}
	else if(FormOption == 2){
	opener.document.salesorder.txtAssignedUser.value=value;	
	}
	else if(FormOption == 3){
		opener.document.getElementById('hodusercode'+index).value=value;	
		opener.document.getElementById('hodusername'+index).value=value2;	
	}
	else if(FormOption == 4){
		opener.document.getElementById('usercode'+index).value=value;	
		opener.document.getElementById('username'+index).value=value2;	
	}
	else if(FormOption == 5){
	opener.document.paymentrqst.txtConfirmedBy.value=value;	
	}
	else if(FormOption == 6){
	opener.document.getElementById('txtRequestBy').value=value;	
	}
	else {	
	opener.users.value=value;
	}
	//opener.employeeid.value=value2;
	self.close();
}


function clearform()
{
  vFrm  = document.search.frm.value;
  var index = document.search.index.value; 
  var funds = document.search.funds.value; 
  document.search.perpage.value='';
  document.search.keyword.value='';
  document.search.action='list_users.php?frm='+vFrm+'&index='+index+'&funds='+funds;
  document.search.submit();
}

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

function SetNextPage(startrange,perpage){
	vFrm  = document.search.frm.value;
	var index = document.search.index.value; 
	var funds = document.search.funds.value; 
	document.search.action='list_users.php?keyword=&frm='+vFrm+'&index='+index+'&funds='+funds+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
    vFrm  = document.search.frm.value;
	var index = document.search.index.value; 
	var funds = document.search.funds.value; 
	document.search.action='list_users.php?keyword=&frm='+vFrm+'&index='+index+'&funds='+funds+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function Searchkeyword(){
  vFrm  = document.search.frm.value;
  var index = document.search.index.value;  
  var funds = document.search.funds.value;   
  document.search.action='list_users.php?frm='+vFrm+'&index='+index+'&funds='+funds+'&startrange=0';
  document.search.submit();
}

function SetFirstPage(startrange,perpage){
	vFrm  = document.search.frm.value;
	var index = document.search.index.value;
	var funds = document.search.funds.value;	
	document.search.action='list_users.php?frm='+vFrm+'&index='+index+'&funds='+funds+'&startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	vFrm  = document.search.frm.value;
	var index = document.search.index.value; 
	var funds = document.search.funds.value;
	var Result=totalrows % perpage;
	startrange=totalrows-Result;
	document.search.action='list_users.php?frm='+vFrm+'&index='+index+'&funds='+funds+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function set_limit(){
	vFrm  = document.search.frm.value;
	var index = document.search.index.value;
	var funds = document.search.funds.value;
	perpage=document.search.perpage.value;
	document.search.action='list_users.php?frm='+vFrm+'&index='+index+'&funds='+funds+'&perpage='+perpage;
	document.search.submit();
}

</script>
</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>
<input type='hidden' name='frm' value='$frm'>
<input type='hidden' name='index' value='$index'>
<input type='hidden' name='funds' value='$funds'>

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 Users</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>";
		
echo"<tr class='Search-header-bg' height=34px>
	    <td align='center'><span class='style4'>Search by Name</span></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='Search'><span><span>Search</span></span></button>
		<button id='btnCancel' name='btnCancel' onclick='clearform(); return false;' title='Cancel'><span><span>Cancel</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>";
	
//Application List
	$querysearch .="select user_account,user_name from ".$_SESSION['dbLabel'].".login ";
	if ($frm == 1 && $toBeApprovedByHOD == 1)
		$querysearch .= " inner join ".$_SESSION['dbLabel'].".HOD_Users on HOD_Users.hod_code = login.user_account and HOD_Users.user_code = '".$_SESSION['useraccount']."' ";
	if ($frm == 5 && $toBeApprovedByHOD == 1)
		$querysearch .= " inner join Source_Fundsdt on Source_Fundsdt.user_code = login.user_account and sourceFund_Code = '$funds' ";
	$querysearch .= " where 1=1 ";
	
	if ($frm == 1 && $toBeApprovedByHOD == 0)
	$querysearch .=	" and ifnull(Branch_Manager,0) = 1 and  ifnull(Branch_Manager,0) ";
	
	$querysearch .=	" and Upper(login_schema) =  Upper('".$_SESSION['dbName']."') ";
	
	if($keyword != ""){
		$querysearch=$querysearch." and (user_account like"."('%".$keyword."%') or user_name like"."('%".$keyword."%')  ) ";
	}
	//echo $querysearch;
	
	$result=mysql_query($querysearch,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($result);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
	
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
$result=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 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=150>	
	<col width=450>	";	
	
	echo"<tr class='Search-Label-bg'>
	<td  height='25' align='center'><span class='style4'>User Account</span></td>
		<td  height='25' align='center'><span class='style4'>User Name</span></td>

	</tr>";
		

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

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			
	
	$code=rawurlencode($rows[1]);
	$name = $rows[1]." ".$rows[3].",".$rows[2];
	
	echo"<tr $Color>";
  
	  echo"<td align='left'><a href='#' onclick=\"SelectRecord('$rows[0]','$rows[1]');\"><span class='style5'>$rows[0]</span></a></td>";
    echo"<td align='left'><a href='#' onclick=\"SelectRecord('$rows[0]','$rows[1]');\"><span class='style5'>$rows[1]</span></a></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;

?>						
					