<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.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);
}

$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ".
		  " ifNull(FA_PaymentMod,0) As FA_PaymentMod, ifNull(FA_ReceiptMod,0) As FA_ReceiptMod, ".
		  " ifNull(FA_CreditNoteMod,0) As FA_CreditNoteMod, ifNull(FA_DebitNoteMod,0) As FA_DebitNoteMod, ".
		  " ifNull(FA_ContraMod,0) As FA_ContraMod, ifNull(ShowTransaction,0) As ShowTransaction, ifNull(print,0) As FA_JournalPreview ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
			 " ifNull(Module_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent, ".
			 " 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);
}


$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);
}

$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);
}	
			  
//echo $queryperiod;	

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);		  

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$_SESSION['dbLabel'].".ledgerstk  ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " order by ledger_number";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];


//Get Default Project
$querydflproject="select project_code as defaultproject from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)	
	$PCostCentCode = $defaultcostcent;	

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

if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
$PMode      = $_POST["PMode"]; if ($PMode == ""){$PMode = $_GET["PMode"];} 
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="tabs.css">
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Search $Title</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  var vTransType = document.search.TransType.value;	
  document.search.PRef.value='';
  document.search.PProjectCode.value='';
  document.search.PCostCentCode.value='';
  document.search.SelectedID.value = '';
  document.search.startrange.value = '';
  document.search.action='Form_JournalSearchUnPosted.php';
  document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	//x.PRef.focus();
	ChangeLocation();
}
function PrintPreview(JourId)
{
	str = 'Form_JournalPreview.php?journal_id='+JourId;
	window.open(str);

}
function SetNextPage(startrange,perpage){ 
    var vTransType = document.search.TransType.value;	
	document.search.startrange.value = startrange;
	document.search.action='Form_JournalSearchUnPosted.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
    var vTransType = document.search.TransType.value;
	document.search.startrange.value = startrange;
	document.search.action='Form_JournalSearchUnPosted.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


function PostTransactions() {
	
	var vSelectedRows = document.search.SelectedRows.value;
	
	if (vSelectedRows != '') {
		if (confirm ( "Are you sure you want to Post selected transaction(s) ?") )
		{
			document.search.action="Form_Journal_Actions.php?mainmode=posttrx";
			document.search.submit();
		}	
	} else
		alert ('Please select a record.');

}

function SelectRecord() {
	var MasterCheckName, vSelectedRows;
	var x, y, z;
	
	vSelectedRows = '';
	for (var i=1; i <= document.search.i.value; i++)
	{
		MasterCheckName='M'+i;
		
		x = document.getElementById(MasterCheckName);
		if (x.checked)
			vSelectedRows = vSelectedRows + ',' + x.value;
		
	}
	
	vSelectedRows = vSelectedRows.substr(1);
	document.search.SelectedRows.value = vSelectedRows;
}

/*function UpdateRecord(TransId){
	var vTransType = document.search.TransType.value;
	
	document.search.action="Form_Journal_Actions.php?TransType="+vTransType+"&mainmode=searchunposted&STransId="+TransId;
	document.search.submit();
	
	document.search.PMode.value=1;
	
	opener.document.trans.txtTransId.value=TransId;
	opener.document.trans.mainaction.value='edit';
	opener.document.trans.actionmode.value='';
	
	//opener.document.trans.action='Form_Trans.php';
	//opener.document.trans.submit();
	
	
	//self.close();
	

}*/

function UpdateRecord(JourId, InTempJour){
	window.open("Form_Journal_Actions.php?mainmode=search2&SJourId="+JourId+"&InTempJour="+InTempJour, '_blank');
	//document.search.submit();
}
function ShowHideDetails(TransId, Option) {
	if (Option == 'S') {
		document.search.SelectedID.value = TransId;
	}
	else if (Option == 'H') {
		document.search.SelectedID.value = '';
	}	
	//document.search.action='Form_JournalSearchUnPosted.php?SelectedID='+TransId;
	document.search.submit();
}


function ChangeLocation()
{
	var vTransType = document.search.TransType.value;
	
    if (document.search.PMode.value == 1) {
		opener.document.trans.message.value = document.search.message.value;
		opener.document.trans.action='Form_Trans.php?TransType='+vTransType;
		opener.document.trans.submit();
		self.close();
	}	
}



function showlistProject(str){

  window.open('List_SorProject.php?frm=6&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(str){

  window.open('List_SorCostcenter.php?frm=6&item='+str,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function SelectOrderBy()
{
  document.search.startrange.value = '';
  document.search.SelectedID.value = '';
  //document.search.PRef.value='';
  //document.search.action='Form_JournalSearchUnPosted.php';
  document.search.submit();

}

function SelectSearchBy()
{	
  document.search.startrange.value = '';
  document.search.SelectedID.value = '';
  //document.search.PRef.value='';
  //document.search.action='Form_JournalSearchUnPosted.php';
  document.search.submit();

}

function SelectResultPerPage()
{	
  document.search.startrange.value = '';
  document.search.SelectedID.value = '';
  //document.search.PRef.value='';
  //document.search.action='Form_JournalSearchUnPosted.php';
  document.search.submit();

}

function SubmitForm()
{	
  document.search.startrange.value = '';
  document.search.SelectedID.value = '';
  //document.search.PRef.value='';
  //document.search.action='Form_JournalSearchUnPosted.php';
  document.search.submit();

}

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


</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;
}
.style11 {color: #999999; font-weight: bold; }
.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();'>
<form name='search' method='post' action="">
<input type='hidden' name='TransType' value='$TransType' >
<input type='hidden' name='SelectedRows' value='$SelectedRows'>
<input type='hidden' name='SelectedID' id='SelectedID' value='$SelectedID'>
<input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/>
HERE;


echo "<input type='hidden' name='message' value='".htmlspecialchars($message, ENT_QUOTES)."'>";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
if ($HideMenu != 1) 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 Confirmed Journal</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";
	  

echo"<tr>
		<td  height='15'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='15'>&nbsp;</td>
				<td valign='middle' height='15' width='720px'>";			
echo"   </td><td align='left' width='250px'>";





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



echo"<tr><td id='content-body'>";	


echo"<br><table border='0' cellspacing='1'  style='table-layout:fixed' align=center>
	  <col width=20> <col width=20>
	  <col width=20> <col width=20>
	  <col width=85>
	  <col width=98>
	  <col width=75>
	  <col width=75>
	  <col width=110>
	  <col width=110>
	  <col width=110>
	  <col width=110>
	  <col width=110>
		     
	  ";
	  
echo "<td colspan='13'><table border='0' style='table-layout:fixed;' align='left'>
		<col width=85>
		<col width=160>
		<col width=100>
		<col width=160>
		<col width=180>
		<col width=110>";
echo "<tr>";
	  
if ($Module_Project == 1) {
	echo" <td align='left' ><span class='style11'>Project</span></td>
	      <td align='left' ><span class='style11'>
		    <input type='text' name='PProjectCode' id='PProjectCode' value='$PProjectCode' size='15' maxlength='15' /></span>
		    <label><span class='style11'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";
} else {
	echo" <td align='left' colspan='2'><input type='hidden' name='PProjectCode' id='PProjectCode' value='$PProjectCode' /></td>";

}			

if ($Module_CostCenter == 1) {		  
	echo" <td align='right' ><span class='style11'>Costcenter</span></td>		
		  <td align='left' ><span class='style11'>
		    <input type='text' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' size='15' maxlength='15' /></span>
		    <label><span class='style11'><img src='img/btnopen.jpg' title=' Select Costcenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistCostcenter(1); ' /></span></label></td>";
} else {
	echo" <td align='left' colspan='2'><input type='hidden' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' /></td>";

}	  


echo" <td align='right' ><span class='style11'>Order By</span></td>		
	  <td align='left' ><span class='style11'><select name='orderby' id='orderby' value='$orderby' onchange='SelectOrderBy();'>";
		
		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>";
echo" <tr >	  
	    <td align='left' ><span class='style11'>Search By</span></td>
		<td align='left' colspan='3'><span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' maxlength='30'/></span>
			<input type='hidden' name='startrange' value='$startrange'>
			<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';	
				
			echo"<option ".$s21." value='1'>Date (yyyy-mm-dd)</option>
				 <option ".$s22." value='2'>Reference</option>
				 <option ".$s23." value='3'>Remark</option>
				 <option ".$s24." value='4'>Amount</option>
				 <option ".$s25." value='5'>Folio</option>
				 </select>";	
				 
echo"       <input type='submit' name='btnSubmit' value='Find' onclick='SubmitForm();' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
		<td align='right' valign='center'><span class='style11'>Result per page</span></td><td><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange='SelectResultPerPage();'>";
              


        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>';	
echo" </tr>";

if($alljv=='a') $chk1=' checked '; else $chk1='';
if($alljv=='c') $chk2=' checked '; else $chk2='';
if($alljv=='n') $chk3=' checked '; else $chk3='';
if($alljv=='') $chk1=' checked '; 

echo"<tr><td colspan=1><span class='style11'>All JVs&nbsp;</span></td><td><input type='radio' onclick='SubmitForm();' name='alljv' value='a'  $chk1 onMouseOver=\"style.cursor='hand'\" ></tD>";
echo"<td colspan=1><span class='style11'>Confirmed &nbsp;</span></td><td><input type='radio'  onclick='SubmitForm();' name='alljv' value='c'    $chk2 onMouseOver=\"style.cursor='hand'\" ></tD>";
echo"<td colspan=1><span class='style11'>Not Confirmed &nbsp;</span></td><td><input type='radio'  onclick='SubmitForm();' name='alljv' value='n'    $chk3 onMouseOver=\"style.cursor='hand'\" ></tD></tr>";


	  echo"</table></td></tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='13'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style25'>Date </span></td>
		<td  height='25' align='center'><span class='style25'>Ref Government </span></td>
		<td  height='25' align='center'><span class='style25'>Folio</span></td>
		<td  height='25' align='center'><span class='style25'>Type</span></td>
		<td  height='25' align='center'><span class='style25'>Amnt1_db</span></td>
		<td  height='25' align='center'><span class='style25'>Amnt1_cd</span></td>
		<td  height='25' align='center'><span class='style25'>Amnt2_db</span></td>
		<td  height='25' align='center'><span class='style25'>Amnt2_cd</span></td>
		<td  height='25' align='center'><span class='style25'>Remark</span></td>
		<td  height='25' align='center'><span class='style25'>Confirmed</span></td>
	  </tr>";
		

	

	
if (empty($PRef))   
   $PRef = 'Null';
else
   $PRef = "'".$PRef."'"; 

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
/*			 
$querysearch=" Select Distinct Trans.Trans_ID, Trans.Trans_Reference, Date_Format(Trans.Trans_Date,'%d-%m-%Y') As Trans_Date, ".
             " Trans.Trans_Type, Trans.Trans_Remark, Trans.Trans_Representative, Trans.Trans_CashReference, Trans.CashType_code, ".
			 " Trans.Trans_ManualNo, Project.Project_Name As ProjectName, Costcent.Costcent_name As CostcentName, ".
			 " Round(ifNull(Trans.Trans_AmountDb1,0), ".$DecimalBase1.") as Trans_AmountDb1, Round(ifNull(Trans.Trans_AmountDb2,0), ".$DecimalBase2.") as Trans_AmountDb2, ".
			 " Round(ifNull(Trans.Trans_Amountcd1,0), ".$DecimalBase1.") as Trans_Amountcd1, Round(ifNull(Trans.Trans_Amountcd2,0), ".$DecimalBase2.") as Trans_Amountcd2, ".
			 " ifNull(Temp_Trans.Trans_Id, -1) As TempTransId, Trans.Trans_ManualNo as TransManualNo ".
			 " From Trans ".
			 " Left Join Project on Trans.Project_Code = Project.Project_Code ".
			 " Left Join Costcent on Trans.Costcent_code = Costcent.Costcent_code ".
			 " Left Join Temp_Trans on Temp_Trans.Trans_ID = Trans.Trans_ID ";*/
			 
$querysearch=" SELECT Journal.Journal_id, date_format(Journal.Journal_Date,'%d/%m/%Y') As Journal_Date, Journal.Journal_refGovernment Journal_refGovernment, Journal.Journal_Folio, Journal.Journal_Type,".
             " Journal.Amnt1_db, Journal.Amnt1_cd, Journal.Amnt2_db, Journal.Amnt2_cd, ".
             " Journal.Journal_Remark, Journal.Journal_Confirmed, Journal.Journal_confirmedUser  ,ifNull(Temp_Journal.Journal_Id, -1) As TempJournalId
			 From journal   Left Join Temp_Journal on Temp_Journal.Journal_ID = Journal.Journal_ID ";
			 
			 
if ($projectexist >	0)
  $querysearch= $querysearch." Inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and journal.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and journal.project_code = Q1.project_code )) ";
							 
if ($costcentexist > 0)
  $querysearch= $querysearch." Inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and journal.costcent_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and journal.costcent_code = Q2.costcent_code )) ";

$querysearch= $querysearch.											 
			 " Where (($searchby = 2 and ifNull(Journal.Journal_refGovernment,-1) Like Concat(ifNull($PRef, ifNull(Journal.Journal_refGovernment,-1)),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(Journal.Journal_Date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(Journal.Journal_Date,'%Y-%m-%d'))) Or  ".
			 "        ($searchby = 3 and Journal.Journal_Remark Like Concat('%', ifNull($PRef, Journal.Journal_Remark),'%')) or ".
			 "        ($searchby = 4 and Round(ifNull(Journal.Amnt1_db,0), ".$DecimalBase1.") Like Concat( ifNull($PRef, Round(ifNull(Journal.Amnt1_db,0), ".$DecimalBase1.")),'%')) or ".
			 "        ($searchby = 5 and Journal.Journal_folio Like Concat(ifNull($PRef, Journal.Journal_folio),'%')) ) ".
			 "	 And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(Journal.Journal_Date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',Journal.Journal_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
             "                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(Journal.Journal_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ";

			 
 if ($alljv=='c') 
     $querysearch= $querysearch." And (Journal.journal_confirmed = True) ";
   else if ($alljv=='n') 
      $querysearch= $querysearch." And ( (Journal.journal_confirmed = False) or (Journal.journal_confirmed is NULL) )";

//echo $querysearch;  

if ($Module_BelongsToGroup == 1)			 
	$querysearch= $querysearch." And ifNull(Journal.Journal_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
	
if ($ShowTransaction != 1)
	$querysearch= $querysearch." And Upper(Journal.User_Account) = Upper('".$_SESSION['useraccount']."') ";
				 
if ($PProjectCode != '')
	$querysearch= $querysearch." And Journal.project_code like concat('".$PProjectCode."','%') ";
if ($PCostCentCode != '')
	$querysearch= $querysearch." And Journal.costcent_code like concat('".$PCostCentCode."','%') ";			 



if ($orderby == 1)
	$querysearch= $querysearch." Order By Journal.Journal_Date Desc, Journal.Journal_refgovernment Desc ";
else if ($orderby == 2)
	$querysearch= $querysearch." Order By Journal.Journal_refgovernment Desc ";	

			// $querysearch= $querysearch." order by Journal_confirmedUser ";	
	
//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());		

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

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
		


	$SelHint  = 'Select Record';
	$EditHint = 'Edit Record';
	$SelMode  = '';
	if ($TempJournalId != -1) {$SelHint=$EditHint = 'Exists in Pending'; $SelMode = ' disabled ';}
	
    if ($FA_Mod == 0)	$EditMode = ' disabled '; 
	else if ($FA_Mod == 1 && $TempJournalId != -1  ) $EditMode = ' disabled ';
	else if ($FA_Mod == 1 && $TempJournalId == -1  ) $EditMode = '';
		   
	echo'<tr '.$Color.'>';
	
	$SelMaster  = 'M'.$i;
	$$SelMaster = $Journal_id;
	 	   
	if($Journal_Confirmed==1)	   
		$SelMode = ' disabled ';
	
	echo"	<td align='center'><span class='style15'><input type='checkbox' name='$SelMaster' id='$SelMaster' value='".$$SelMaster."' onclick='SelectRecord();' title='".$SelHint."' $SelMode /></span></td>";
	
	if($Journal_Confirmed!=1 && $TempJournalId == -1 )	 
	echo"<td align='center'><span class='style15'><img src='img/button_edit.png' onMouseOver=\"style.cursor=\'hand\'\" onclick=\"UpdateRecord('$Journal_id', '$TempJournalId');\" title='$EditHint' $EditMode  ></span></td>";
	else echo "<td></td>";
	if ($FA_JournalPreview == 1)	{
	    echo'<td align="center"><span class="style5"><img src="img/printpreview.png" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview('.$Journal_id.');"  title="'.$PrevHint.'" style="width:20px; height:20px; " '.$PrevMode.'></span></td>';
	} else {
		echo'<td align="center" ><span class="style5"></span></td>';
	}
	if ($SelectedID != "" && $Journal_id == $SelectedID)		
		echo'<td align="center"><span class="style15"><img src="img/hide.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$Journal_id.', \''.'H'.'\');" title="Hide Details" height="15px" width="15px"></span></td>';
	else	
		echo'<td align="center"><span class="style15"><img src="img/show.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$Journal_id.', \''.'S'.'\');" title="Show Details" height="15px" width="15px"></span></td>';		
	
	
	echo'   <td align="left"><span class="style15">'.$Journal_Date.'</span></td>
			<td align="center"><span class="style15">'.$Journal_refGovernment.'</span></td>
			<td align="left"><span class="style15">'.$Journal_Folio.'</span></td>
			<td align="left"><span class="style15">'.$Journal_Type.'</span></td>
			<td align="right"><span class="style15">'.number_format($Amnt1_db,$DecimalBase1).'</span></td>
			<td align="right"><span class="style15">'.number_format($Amnt1_cd,$DecimalBase1).'</span></td>
			<td align="right"><span class="style15">'.number_format($Amnt2_db,$DecimalBase2).'</span></td>
			<td align="right"><span class="style15">'.number_format($Amnt2_cd,$DecimalBase2).'</span></td>
			<td align="right"><span class="style15">'.$Journal_Remark.'</span></td>
			<td align="right"><span class="style15">'.$Journal_Confirmed.'</span></td>
		</tr>';
		
	if ($SelectedID != "" && $Journal_id == $SelectedID) {
	
		$sub_query = " Select JournalDt.JournalDt_id, JournalDt.Ledger_Number, Ledger.Ledger_Name, JournalDt_Desc, JournalDt.Currency_Code, ".
					 " if (JournalDt_dbcd='D', Round(Journaldt_Amount, ".$DecimalBase."),'') As DebitAmount, ".
					 " if (JournalDt_dbcd='C', Round(Journaldt_Amount, ".$DecimalBase."),'') As CreditAmount, ".
					 " Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As JournalDt_ValueDate, JournalDt_dbcd, Ledger.Ledger_Activity,JournalDt_dbcd ,Journaldt_Amount1,Journaldt_Amount2".
					 " From JournalDt Left Join Ledger on JournalDt.Ledger_Number = Ledger.Ledger_Number ".
					 " Where JournalDt.Journal_ID = '".$SelectedID."' ".
					 " Order By JournalDt.JournalDt_ID Desc ";
		//echo $sub_query;			 
		$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
		$sub_totalrows = mysql_num_rows($sub_result);
		
		if ($sub_totalrows > 0) {
			echo "<tr ".$Color.">";
			echo "<td colspan='13' valign='top' align='left'><div style='border:1px solid #33CCFF; z-index:9999; background-color:#CCFFFF; padding-left:5px; padding-bottom:5px;'>";
			echo "<table width='900px' border='0' cellpading='0' cellspacing='1'>";
			echo "<tr>";
			echo "	<td width='90'><span class='style15'>Ledger No.</span></td>";
			echo "	<td width='300'><span class='style15'>Ledger Desc.</span></td>";
			echo "	<td width='300'><span class='style15'>Description</span></td>";
			echo "	<td width='80' align='center'><span class='style15'>D/C</span></td>";
			echo "	<td width='80' align='center'><span class='style15'>Currency</span></td>";
			echo "	<td width='120' align='center'><span class='style15'>Amount</span></td>";
			echo "	<td width='120' align='center'><span class='style15'>$base1</span></td>";
			echo "	<td width='120' align='center'><span class='style15'>$base2</span></td>";
			echo "	<td width='100' align='center'><span class='style15'>Value Date</span></td>";
			echo "</tr>";
			while($sub_rows=mysql_fetch_array($sub_result)){
				echo "<tr>";
				echo "	<td align='left' valign='top'>".$sub_rows[1]."</td>";
				echo "	<td align='left' valign='top'>".$sub_rows[2]."</td>";
				echo "	<td align='left' valign='top'>".$sub_rows[3]."</td>";
				echo "	<td align='center' valign='top'>".$sub_rows[10]."</td>";
				echo "	<td align='center' valign='top'>".$sub_rows[4]."</td>";
				if($sub_rows[10]=='D')
				echo "	<td align='right' valign='top'>".number_format($sub_rows[5], $DecimalBase)."</td>";
				else
				echo "	<td align='right' valign='top'>".number_format($sub_rows[6], $DecimalBase)."</td>";
				
				echo "	<td align='center' valign='top'>".number_format($sub_rows[11], $DecimalBase)."</td>";
				echo "	<td align='center' valign='top'>".number_format($sub_rows[12], $DecimalBase)."</td>";
				echo "	<td align='left' valign='top'>".$sub_rows[7]."</td>";
				echo "</tr>";
				
				$sub_query = " Select Costcent_code, Project_Code, Cstjour_prcnt, Cstjour_amount, Cstjour_amount1, Cstjour_amount2 ".
					 " From CstJour  ".
					 " Where CstJour.Journaldt_ID = '".$sub_rows[0]."' ".
					 " Order By CstJour.Cst_Increment Desc ";
				echo $sub_query;			 
				$sub_resultCstJour=mysql_query($sub_query,$connection) or die(mysql_error());	
				$sub_totalrowsCstJour = mysql_num_rows($sub_resultCstJour);	
					if ($sub_totalrowsCstJour > 0) {
							echo"<tr><td colspan=11><table align=center>";
							echo "<tr>";
							echo "	<td width='90'><span class='style15'><u><i>Project</i></span></td>";	
							echo "	<td width='90'><span class='style15'><u><i>Costcenter</i></span></td>";	
							echo "	<td width='90'><span class='style15'><u><i>Percentage</i></span></td>";	
							echo "	<td width='90'><span class='style15'><u><i>Amount</i></span></td>";	
							echo "	<td width='90'><span class='style15'><u><i>$base1</i></span></td>";	
							echo "	<td width='90'><span class='style15'><u><i>$base2</i></span></td>";	
							echo "</tr>";
							while($sub_rowsCst=mysql_fetch_array($sub_resultCstJour)){
								
							echo "<tr>";
							echo "	<td width='90'><i>".$sub_rowsCst[1]."</i></td>";	
							echo "	<td width='90'><i>".$sub_rowsCst[0]."</i></td>";	
							echo "	<td width='90'><i>".$sub_rowsCst[2]."</i></td>";	
							echo "	<td width='90'><i>".$sub_rowsCst[3]."</i></td>";	
							echo "	<td width='90'><i>".$sub_rowsCst[4]."</i></td>";	
							echo "	<td width='90'><i>".$sub_rowsCst[5]."</i></td>";	
							echo "</tr>";
								
							}
						echo"</table></td></tr>";	
					}
			echo"<tr><td colspan=11><hr></td></tr>";
			}
			
			echo "</table></div></td>";
			echo "</tr>";
			
			//////////////  cstjour table
		
			
		}
	}	
}
							
echo "<input type='hidden' name='i' value='$i'>";
echo"   <tr bgcolor='#DDDDDD'><td colspan='11' align='center'><input type='button' name='btnPost' value='Post Transactions' onclick='PostTransactions();' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td></tr>";
		
echo"   <tr><td colspan='11'>&nbsp;</td></tr>
		<tr><td colspan='11'><table width='100%' border=0><tr><td width='385'>&nbsp;</td><td width='150' 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='150' 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='385'>&nbsp;</td></tr>";
		echo"</table>";

echo"</td></tr></table>";
	
echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";
//include("footer.php");

          

		echo"</table>";  

print <<<HERE

</form>
</body>

HERE;

?>