<?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_Trans.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);
}


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_BelongsToGroup,0) As Module_BelongsToGroup From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


$MainCompanyName=explode('_',$_SESSION['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$_SESSION['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$_SESSION['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
//echo $queryperiod;			  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;

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

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




print <<<HERE
<head>
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">

<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>
<link type="text/css" href="js/grid/scripts/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="js/grid/scripts/jquery-ui.min.js"></script>
<title>Search Notifications</title>
<script type="text/javascript" language="javascript">

function checknotification(vnotification,vname){

	if(document.getElementById(vname).checked)
			var xx = '1';
			else
			var xx = '0';
$('form').attr('target','');			
document.notification.action='Form_Notifications_Actions.php?xx='+xx;
document.notification.actionmode.value='update';
document.notification.notification_id.value=vnotification;
document.notification.submit();
}

function SearchKeyword(){
	document.notification.action='Form_Notifications.php';
	document.notification.submit();
}


function check(){

if (document.notification.chk.checked)
document.notification.not_checked.value=1;
else
document.notification.not_checked.value=0;
}

function openForm(trans_id,notification_type) {
	
	
	var xmlhttp  = getXMLHttpRequest();	
	
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = xmlhttp.responseText.split("[BRK]");
				
				if(notification_type == 'PVR' && data[1] == ''){
					document.notification.mainaction.value='edit';
					document.notification.actionmode.value='';
					$('form').attr('target','_blank');
					document.notification.action='Form_PaymentRqst.php?txtTransId='+trans_id;
					document.notification.submit();
				} else {
					alert(data[1]);
				}
			}
		}
		var str = '';
		if(notification_type == 'PVR'){
			str = "Form_PaymentRqst_Actions.php?mainmode=notification&STransId="+trans_id;
		} 
		
		if(str != ''){
		xmlhttp.open("GET",str);
		xmlhttp.send();
		}
		
	
}

function openForm2(desc_code,tablename) {
	if(tablename == 'STAFF' || tablename == 'VEHICLE'){
		window.open("Form_Notifications_Actions.php?actionmode=&mainmode=openForm&tablename="+tablename+"&desc_code="+desc_code,'_blank');
		
	}
}
HERE;

//echo"setInterval(function(){window.location.reload(true)},".$config['Refresh'].");";

print <<<HERE
</script>
</head>
<body >
<form name='notification' method='post' action="">
<input type='hidden' name='PMode' id='PMode' value='$PMode' />
<input type='hidden' name='SelectedID' id='SelectedID' value='$SelectedID'>
<input type='hidden' name='actionmode' id='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' id='mainaction' value='$mainaction'>
<input type='hidden' name='notification_id' id='notification_id' value='$notification_id'>


HERE;





$PRef = GetSQLValueString($keyword, 'text');
if($PRef=='')
	$PRef = 'Null';
	
$querysearch="select notification_id as notificationid, ifnull(notif_type.Notification_Description,notification_type) as notification_type, notification_action, notification.notification_description as notification_description,
			  notification_reference, notification_createduser, notification_level, Date_Format(notification_date,'%d/%m/%Y') notification_date, notification_done,
			  notification_checked, trans_id , ifnull(multiple_ids,0) as multiple_ids, upper(notification_table) as notification_table, createdby_user , approvedbyuser 
			  from notification 
				left join notification_type as notif_type on notif_type.Notification_Name =  notification.notification_type
				where 1=1 
				and (ifNull($PRef,ifnull(notification_checked,0) = 0) or if('$not_checked'='1',ifnull(notification_checked,0) = 1,ifnull(notification_checked,0) = 0)) ".
			"   And ( ($searchby = 1 and notification_type = ifNull($PRef, notification_type)) Or  ".
			"        ($searchby = 2 and Date_Format(notification_date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(notification_date,'%Y-%m-%d')))  ".
			"         ) and notification_createduser = '".$_SESSION['useraccount']."'";
			
			
			
if ($orderby == 1)
	$querysearch = $querysearch." Order By Date_Format(notification_date,'%Y-%m-%d') Desc, notification_reference Desc ";
else if ($orderby == 2)
	$querysearch= $querysearch." Order By notification_reference Desc ";	

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



// echo $querysearch;exit;

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

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>List of Notifications</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";
	  
echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";	  
echo"	</td></tr></table>";
echo"</td></tr>";	

echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>	
			<table border='0' cellspacing='1' style='table-layout:fixed' align='center'>
			  <col width=100>
			  <col width=160>
			  <col width=100>
			  <col width=150>
			  <col width=80>
			  <col width=100>
			  <col width=110>
			  <col width=115>";

echo"		 <tr  height='34px'>
				<td align='left'><span class='style4'>Search By</span></td>
				<td align='left' colspan='5'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' style='width:150px;' maxlength='100'  onkeydown='return kd(event)'/></span>
					<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
		
		
		
					if ($searchby==1)		$s21 = 'selected';
					else if ($searchby==2)	$s22 = 'selected';
					else if ($searchby==3)	$s23 = 'selected';
					else if ($searchby==4)	$s24 = 'selected';
					else if ($searchby==5)	$s25 = 'selected';
					else if ($searchby==6)	$s26 = 'selected';
					else if ($searchby==7)	$s27 = 'selected';
						
						if($not_checked==1)
							$checked = ' checked ';
						
					echo"<option ".$s21." value='1'>Type </option>
						 <option ".$s22." value='2'>Date (yyyy-mm-dd)</option>
						 <option ".$s23." value='3'>Action</option>
						 <option ".$s24." value='4'>Description</option>
						 <option ".$s25." value='5'>Created User</option>
						 </select>";
echo"				<button type='button' id='btnFind' name='btnFind' onclick='SearchKeyword(); 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>
					&nbsp;&nbsp;&nbsp;&nbsp;<span class='style4'>Show Checked</span><input type='checkbox' name='chk' id='chk' value='$chk' $checked onclick='check()'>
					<input type='hidden' name='not_checked' id='not_checked' value='$not_checked'>
				</td>";
			
echo"			<td align='right' valign='center' ><span class='style4'>Order By</span></td>
				<td align='left'><span class='style4'><select name='orderby' id='orderby' value='$orderby' onchange='RefreshPage();'>";
		
				if ($orderby==1)
					$s11 = 'selected';
				else if ($orderby==2)
					$s12 = 'selected';
					
				echo"<option ".$s11." value='1'>Date</option>
					 <option ".$s12." value='2'>Reference</option>
					 </select></span></td>";
echo"		 </tr>";
			  
if($message!=''){
	echo"	  <tr >
				<td height='15' align='center' colspan='8' class='searchmessage'>".$message."</td>
			  </tr>";
}
		
echo"		</table>
		</td>
		
	</tr>";		
	

echo"<tr>
		<td  colspan=10>
		<table border='0'  align='center' valign='top' width=100% >
   <tr ><td valign='top' align='left' colspan='10'>
		<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."')>";

			$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."')>";
			}
			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:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td  style='width:31px;'>&nbsp;</td>";
echo"</tr>";	


echo"<tr><td  style='width:31px;'>&nbsp;</td>
		<td >
			<div style='overflow:scroll;height:400px;width:1100px;'>
			<table border='0' style='table-layout:fixed' cellpadding='0' cellspacing='1' width='1080px' class='grid_data' >
			  <col width=120>
			  <col width=200>
			  <col width=60>
			  <col width=60>
			  <col width=66>
			  <col width=60>
			  <col width=60>
			  <col width=75>
			  <col width=30>
			  <col width=30>";
	  
echo" 		<tr bgcolor='#f2f2f2'>
				<td  height='25' align='center'><span class='style4'>Title</span></td>
				<td  height='25' align='center'><span class='style4'>Desc</span></td>
				<td  height='25' align='center'><span class='style4'>Reference</span></td>
				<td  height='25' align='center'><span class='style4'>Created By</span></td>
				<td  height='25' align='center'><span class='style4'>Approved By</span></td>				
				<td  height='25' align='center'><span class='style4'>User</span></td>
				<td  height='25' align='center'><span class='style4'>Level</span></td>
				<td  height='25' align='center'><span class='style4'>Date</span></td>
				<td  height='25' align='center'><span class='style4'>Done</span></td>
				<td  height='25' align='center'><span class='style4'>Checked</span></td>
			</tr>";
		


$i = 0;

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.'>';
	 
	if($notification_type == 'PVR') $notification_name='Payment Request';
	else $notification_name=$notification_type;
	echo'	<td align="left"><span class="style5">'.$notification_name.'</span></td> ';
	if($trans_id != '')
	echo '  <td align="center" onclick="openForm('.$trans_id.',\''.$notification_type.'\');"><span class="style5" onmouseover="style.cursor=\'hand\'">'.$notification_description.'</span></td>';
	else {
		if($multiple_ids == 1){
			$desc_codes = explode(':', $notification_description);
			$desc_codes1 = explode('on', $desc_codes[1]);
			$desc_codes2 = explode('-', $desc_codes1[0]);
			$desc_codes2 = array_reverse($desc_codes2);
			if($desc_codes[0] != '') $notification_description = $desc_codes[0].": ";
			foreach($desc_codes2 as $desc_code){
				$desc_code = str_replace(' ','',$desc_code); 
				if($desc_code != '') $notification_description .= "<span onmouseover=\"style.cursor='hand'\" onclick=\"openForm2('$desc_code', '$notification_table' );\" > $desc_code - </span>";
			}
			if($desc_codes1[1] != '') $notification_description .= " on ".$desc_codes1[1];
			echo '  <td align="center" ><span class="style5" onmouseover="style.cursor=\'hand\'">'.$notification_description.'</span></td>';
		} else 
		echo '  <td align="center" ><span class="style5" onmouseover="style.cursor=\'hand\'">'.$notification_description.'</span></td>';
	}
	echo '	<td align="left"><span class="style5">'.$notification_reference.'</span></td>
			<td align="left"><span class="style5">'.$createdby_user.'</span></td>
		    <td align="left"><span class="style5">'.$approvedbyuser.'</span></td>
			<td align="left"><span class="style5">'.$notification_createduser.'</span></td>
			<td align="left"><span class="style5">'.$notification_level.'</span></td>
			<td align="left"><span class="style5">'.$notification_date.'</span></td>
			<td align="left"><span class="style5">'.$notification_done.'</span></td>';
			
			
	if($notification_checked == 1)
		$chk = ' checked ';
		else
		$chk = '  ';
		
	echo"<td align='left'><span class='style5'><input type='checkbox' name='chk$ct' id='chk$ct' onclick=\"checknotification($notificationid,'chk$ct')\" $chk></span></td>";
	echo'</tr>';

	
	
	$i+=1;
}
							
echo"</table></div></td><td style='width:31px;'>&nbsp;</td></tr>";

	

echo"	<tr >
		<td  style='height:34px; width:31px;'>&nbsp;</td>
		<td  style='height:34px; width:908px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&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."')>";

		//$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."')>";
		}
		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:380px;'>&nbsp;</td></tr>";
		echo"</table></td><td  style='height:34px; width:31px;'>&nbsp;</td>";


echo"</tr></table>";
 echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
print <<<HERE

</form>
</body>

HERE;

?>