<?php

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


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

$querylog=" select ifNull(FA_PurchaseJVDel,0) As FA_PurchaseJVDel, ifNull(FA_PurchaseJVMod,0) As FA_PurchaseJVMod, ".
          " ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(ShowTransaction,0) As ShowTransaction, ifnull(FI_PorderJVConfirmed,0) as FI_PorderJVConfirmed ".
          " 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_PostToJournal,0) As Module_PostToJournal From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


//Access Mode On Insert/Edit/Delete
$DelMode  =1;
$EditMode =1;
$ConfMode =1;	
$PrevMode =1;

$PrevHint   = '';
$EditHint   = '';
$DelHint    = '';



$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]."') ";
			  
$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']."' ".
			  " and (ledger_number is not null and ledger_number != '') ".
			  " 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 (empty($option)) $option = 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 Purchases Order JV</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
  document.search.keyword.value='';
  document.search.PProjectCode.value='';
  document.search.PCostCentCode.value='';

  document.search.action='Form_PurchasesOrderJVSearch.php';
  document.search.submit();

}

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

function SetNextPage(startrange,perpage){ 

	document.search.action='Form_PurchasesOrderJVSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){

	document.search.action='Form_PurchasesOrderJVSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


function DeleteRecord(PId, PRef){

	if (PId != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+PRef+" ?") )
		{
			document.search.action="Form_PurchasesOrderJV_Actions.php?mainmode=delete&SId="+PId;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(PId){
		
	document.search.action="Form_PurchasesOrderJV_Actions.php?mainmode=search&SId="+PId;
	document.search.submit();
	
	document.search.PMode.value=1;
	
	opener.document.porderjv.txtPorderJVId.value=PId;
	opener.document.porderjv.mainaction.value='edit';
	opener.document.porderjv.actionmode.value='';
}

function ConfirmOrder(PorId, PorRef){

	var vMainMode = '';
	var vMsg = '';
	
	vMainMode = 'confirmorder'; 
	vMsg = 'Are you sure you want to Confirm order with reference = '+PorRef+' ?';
	
	if (PorId != '') {
		if (confirm (vMsg) )
		{
			document.search.action="Form_PurchasesOrderJV_Actions.php?mainmode="+vMainMode+"&SId="+PorId;
			document.search.submit();
		}
	}
	
}

function PrintPreview(PId)
{
	var str = '';
	
	str = 'Form_PurchasesOrderJVPreview.php?PorderJVId='+PId;
	window.open(str);
	
	//self.close();

}

function ChangeLocation()
{
    if (document.search.PMode.value == 1) {
		opener.document.porderjv.action='Form_PurchasesOrderJV.php';
		opener.document.porderjv.submit();
		self.close();
	}	
}



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

}

function SubmitForm()
{	

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

}

function ShowHideDetails(PurJVId, Option, startrange) {
	/*if (Option == 'S') {
		document.search.SelectedID.value = PurJVId;
		document.search.action='Form_PurchasesOrderJVSearch.php?startrange='+startrange+'&SelectedID='+PurJVId;
	}
	else if (Option == 'H') {
		document.search.SelectedID.value = '';
		document.search.action='Form_PurchasesOrderJVSearch.php?startrange='+startrange;
	}	
	
	document.search.submit();*/
	
	vSlideDivId = '#displaydetails'+PurJVId;
	$(vSlideDivId).slideToggle();
}

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 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;
}
function SelectOption()
{
  document.search.keyword.value='';
  
  document.search.action='Form_PurchasesOrderJVSearch.php?startrange=0';
  document.search.submit();

}
function RefreshPage()
{
  
  //document.search.keyword.value='';
  document.search.action='Form_PurchasesOrderJVSearch.php?startrange=0';
  document.search.submit();

}

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

function SetLastPage(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='Form_PurchasesOrderJVSearch.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}
function showAllDetails(){ 
	if (document.search.showDetails.checked) {
		$('.slideAll').slideDown();
	}else{
		$('.slideAll').slideUp();
	}
}
</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	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;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

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


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

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			
$querysearch=" Select Distinct PorderJV.PorderJV_ID, PorderJV.PorderJV_Reference, Date_Format(PorderJV.PorderJV_Date,'%d-%m-%Y') As PorderJV_Date, ".
             " PorderJV.Supplier_Code As SupplierCode, Suppliers.Ledger_name As SupplierName, PorderJV.Currency_code, ".
			 " Project.Project_Name As ProjectName, Costcent.Costcent_name As CostcentName, ".
			 " Round((ifNull(PorderJV.PorderJV_Total,0) + IfNull(PorderJV.PorderJV_VAT,0) - IfNull(PorderJV.PorderJV_DiscAmount,0)), ".$DecimalBase.") As PorderJV_NetTotal,  ".
			 " ifNull(Temp_PorderJV.PorderJV_ID, -1) As TempPorderJVId, Journal.Journal_folio, Journal.Journal_refgovernment, ifnull(PorderJV.PorderJV_confirmed,0) as PorderJV_confirmed ".
			 " From PorderJV Left Join Suppliers on PorderJV.Supplier_Code = Suppliers.Ledger_Number ".
			 " Left Join Project on PorderJV.Project_Code = Project.Project_Code ".
			 " Left Join Costcent on PorderJV.Costcenter_code = Costcent.Costcent_code ".
			 " Left Join Journal on PorderJV.Journal_ID = Journal.Journal_ID ".
			 " Left Join Temp_PorderJV on Temp_PorderJV.PorderJV_ID = PorderJV.PorderJV_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 PorderJV.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and PorderJV.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 PorderJV.Costcenter_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and PorderJV.Costcenter_code = Q2.costcent_code )) ";

if ($ledgerexist > 0)
  $querysearch= $querysearch." Inner join ( select distinct ledger_number from ".$_SESSION['dbLabel'].".ledgerstk ".
							 "			    where user_account='".$_SESSION['useraccount']."' ".
							 "              and (ledger_number is not null and ledger_number != '') ".
							 "			 )  Q3 on (($ledgerinclude = 1 and PorderJV.Supplier_Code like concat(Q3.ledger_number,'%') ) or ($ledgerinclude = 0 and PorderJV.Supplier_Code Not like concat(Q3.ledger_number,'%') ) or PorderJV.Supplier_Code is null) ";							 

						 
$querysearch= $querysearch.											 
			 " Where (($searchby = 2 and PorderJV.PorderJV_Reference Like Concat(ifNull($PRef, PorderJV.PorderJV_Reference),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(PorderJV.PorderJV_Date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(PorderJV.PorderJV_Date,'%Y-%m-%d'))) Or  ".
			 "        ($searchby = 3 and (PorderJV.Supplier_Code Like Concat(ifNull($PRef, PorderJV.Supplier_Code),'%') or Suppliers.Ledger_name Like Concat('%', ifNull($PRef, Suppliers.Ledger_name),'%')) ) Or  ".
			 "        ($searchby = 5 and Journal.Journal_folio Like Concat( ifNull($PRef, Journal.Journal_folio),'%')) Or ".
			 "        ($searchby = 6 and Journal.Journal_refgovernment Like Concat( ifNull($PRef, Journal.Journal_refgovernment),'%')) ) ".
			 " And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(PorderJV.PorderJV_Date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',PorderJV.PorderJV_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
             "                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(PorderJV.PorderJV_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ".
             " And (($option = 2 and ifNull(PorderJV.PorderJV_confirmed,0)=1) or ($option = 3 and ifNull(PorderJV.PorderJV_confirmed,0)=0) or ($option = 1 and 1=1) ) ".
			 " And ifNull(PorderJV.PorderJV_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
			 
if ($ShowTransaction != 1)
	$querysearch= $querysearch." And Upper(PorderJV.User_Account) = Upper('".$_SESSION['useraccount']."') ";
			 
if ($PProjectCode != '')
	$querysearch= $querysearch." And PorderJV.project_code like concat('".$PProjectCode."','%') ";
if ($PCostCentCode != '')
	$querysearch= $querysearch." And PorderJV.costcenter_code like concat('".$PCostCentCode."','%') ";				 
			 

if ($orderby == 1)
	$querysearch= $querysearch." Order By PorderJV.PorderJV_Date Desc, PorderJV.PorderJV_Reference Desc ";
else if ($orderby == 2)
	$querysearch= $querysearch." Order By PorderJV.PorderJV_Reference Desc ";	
	
			 
//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' width='1050px'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px;' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Purchases Order JV</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=20>
	  <col width=70>
	  <col width=70>
	  <col width=70>
	  <col width=55>
	  <col width=80>
	  <col width=200>
	  <col width=100>
	  <col width=100>
	  <col width=60>
	  <col width=100><col width=100>
	       
	
	  <tr class='Search-header-bg'>";
	  
if ($Module_Project == 1) {
	echo" <td align='left' colspan='4'><span class='style4'>Project</span></td>
	      <td align='left' colspan='3'><span class='style4'>
		    <input type='text' name='PProjectCode' id='PProjectCode' value='$PProjectCode' size='15' maxlength='15' /></span>
		    <label><span class='style4'><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='7'><input type='hidden' name='PProjectCode' id='PProjectCode' value='$PProjectCode' /></td>";

}			

if ($Module_CostCenter == 1) {		  
	echo" <td align='left' ><span class='style4'>Costcenter</span></td>		
		  <td align='left' colspan='2'><span class='style4'>
		    <input type='text' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' size='15' maxlength='15' /></span>
		    <label><span class='style4'><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='style4'>Order By</span></td>		
	  <td align='left' colspan='2' ><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"	<td align='right' valign='center' ><span class='style4'>Option&nbsp;</span></td>	
		<td align='right' valign='center'><span class='style4'><select name='option' id='option' value='$option' onchange='SelectOption();' style='width:85px;' >";
		
		if ($option==1)
			$s41 = 'selected';
		else if ($option==2)
			$s42 = 'selected';
		else if ($option==3)
			$s43 = 'selected';
		//else if ($option==4)
		//	$s44 = 'selected';
			
		echo"<option ".$s41." value='1'>All</option>
			 <option ".$s42." value='2'>Confirmed</option>
			 <option ".$s43." value='3'>Unconfirmed</option>
			
			 </select></span></td>";		  // <option ".$s44." value='4'>Cancelled</option>
echo" </tr>";
echo" <tr class='Search-header-bg'>	  	  
	    <td align='left' colspan='4'><span class='style4'>Search By</span></td>
		<td align='left' colspan='10'><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>
			<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==5)
				$s25 = 'selected';
			else if ($searchby==6)
				$s26 = 'selected';	
				
			echo"<option ".$s21." value='1'>Date (yyyy-mm-dd)</option>
				 <option ".$s22." value='2'>Reference</option>
				 <option ".$s23." value='3'>Supplier</option>";
				 
				 if ($Module_PostToJournal == 1) {
					echo" <option ".$s25." value='5'>Folio</option>
						  <option ".$s26." value='6'>Jv Ref.</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>
			&nbsp;&nbsp;<span class='style4'>Show Details</span><input type='checkbox' name='showDetails' id='showDetails' onclick='showAllDetails()' value='' ></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:1025px;'>
		<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 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:980px;'>
			<table border='0'  cellpadding='0' cellspacing='1' width='1000px' >  
			  <col width=20>
			  <col width=20>
			  <col width=20>
			  <col width=20>
			  <col width=20>
			  <col width=70>
			  <col width=85>
			  <col width=70>
			  <col width=55>
			  <col width=80>
			  <col width=200>
			  <col width=100>
			  <col width=100>
			  <col width=60>
			  <col width=100>";
	  if($message!=''){
		echo"<tr class='Search-header-bg'>
				<td height='15' align='center' colspan='14' 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'>&nbsp;</span></td>
		<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='left'><span class='style4'>Reference </span></td>
		<td  height='25' align='center'><span class='style4'>Date </span></td>";

if ($Module_PostToJournal == 1) {	
	echo" <td  height='25' align='center'><span class='style4'>Folio </span></td>
		  <td  height='25' align='center'><span class='style4'>JV Ref. </span></td>";
} else {
	echo" <td  height='25' align='center' colspan='2'>&nbsp;</td>";
}

echo"		
		<td  height='25' align='center'><span class='style4'>Supplier</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>Project</span></td>
		<td  height='25' align='center'><span class='style4'>CostCenter</span></td>
		<td  height='25' align='center'><span class='style4'>Currency</span></td>
		<td  height='25' align='center'><span class='style4'>Total</span></td>
		<td  height='25' align='center'><span class='style4'>Status</span></td>";
		
echo"</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."'";	
		
		

	$PrevHint   = 'Preview Invoice';
	$EditHint   = 'Edit Record';
	$DelHint    = 'Delete Record';
	
	if ($TempPorderJVId != -1) {$PrevHint=$EditHint=$DelHint = 'Exists in Pending';}
	
    if ($FA_PurchaseJVMod == 0)	$EditMode =0; 
	else if ($FA_PurchaseJVMod == 1 && $TempPorderJVId != -1) $EditMode =0;
	else if ($FA_PurchaseJVMod == 1 && $TempPorderJVId == -1) $EditMode =1;
	
	if ($FA_PurchaseJVDel == 0)	$DelMode =0; 
	else if ($FA_PurchaseJVDel == 1 && $TempPorderJVId != -1) $DelMode =0;
	else if ($FA_PurchaseJVDel == 1 && $TempPorderJVId == -1) $DelMode =1;
	
	
	if ($TempPorderJVId != -1) $PrevMode =0; else $PrevMode =1;		
	
	echo'<tr '.$Color.'>';
			
		if($EditMode==1 && $PorderJV_confirmed == 0)	
	        echo'<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord('.$PorderJV_ID.');" title="'.$EditHint.'" style="width:20px; height:20px; "></span></td>';
		else echo'<td align="center">&nbsp;</td>';		
		
		if($DelMode==1)
			echo'<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord('.$PorderJV_ID.', \''.$PorderJV_Reference.'\');" title="'.$DelHint.'" ></span></td>';
		else echo'<td align="center">&nbsp;</td>';	
		
		if($PrevMode==1)
			echo'<td align="center"><span class="style5"><img src="img/print.png" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview('.$PorderJV_ID.');"  title="'.$PrevHint.'" style="width: 15px; height: 15px; " ></span></td>';
		else echo'<td align="center">&nbsp;</td>';	
		if($FI_PorderJVConfirmed == 1 && $PorderJV_confirmed == 0) 
			echo'<td align="center"><span class="style5"><img src="img/confirmorder.png" onMouseOver="style.cursor=\'hand\'"  onclick="ConfirmOrder('.$PorderJV_ID.', \''.$PorderJV_Reference.'\');"  title="'.$PrevHint.'" style="width: 15px; height: 15px; " ></span></td>';
		else echo'<td align="center">&nbsp;</td>';	
		
	echo'<td align="center"><span class="style5"><img src="img/details.gif" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$PorderJV_ID.', \''.'S'.'\','.$startrange.');" title="Show Details" style="width:15px; height:15px; "></span></td>';
	
	echo'<td align="left"><span class="style5">'.$PorderJV_Reference.'</span></td>
		<td align="center"><span class="style5">'.$PorderJV_Date.'</span></td>';
			
	if ($Module_PostToJournal == 1) {		
		echo' <td align="center"><span class="style5">'.$Journal_folio.'</span></td>
		      <td align="center"><span class="style5">'.$Journal_refgovernment.'</span></td>';
	} else {
		echo" <td align='center' colspan='2'>&nbsp;</td>";
	}
		
	echo'	<td align="center"><span class="style5">'.$SupplierCode.'</span></td>
			<td align="left"><span class="style5">'.$SupplierName.'</span></td>
			<td align="left"><span class="style5">'.$ProjectName.'</span></td>
			<td align="left"><span class="style5">'.$CostcentName.'</span></td>
			<td align="center"><span class="style5">'.$Currency_code.'</span></td>
			<td align="right"><span class="style5">'.number_format($PorderJV_NetTotal, $DecimalBase).'</span></td>
			<td align="center"><span class="style5">'.($PorderJV_confirmed == 1?"Confirmed":"").'</span></td>';
	echo'</tr>';
	
	
	$sub_query = " Select PorderJVDt.PorderJVDt_Id, Ledger.Ledger_Number, Ledger.Ledger_Name, PorderJVDt.PorderJVDt_Description, PorderJVDt.PorderJVDt_Description2, ".
				 " PorderJVDt_Quantity, PorderJVDt.PorderJvDt_ManualNo, ".
				 " PorderJVDt.Currency_code, Round(PorderJVDt_Price, ".$DecimalBase."), Round(PorderJVDt_DiscPer, ".$DecimalBase."),  ".
				 " Round(((PorderJVDt_Quantity*PorderJVDt_Price)-((PorderJVDt_Quantity*PorderJVDt_Price*ifNull(PorderJVDt_DiscPer,0))/100)), ".$DecimalBase.") As Total ".
				 " From PorderJVDt ".
				 " Inner Join Ledger on PorderJVDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where PorderJVDt.PorderJV_Id = '".$PorderJV_ID."' ".
				 " Order By Ledger.Ledger_Number ";

	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error($connection));	
	$sub_totalrows = mysql_num_rows($sub_result);
		
	if ($sub_totalrows > 0) {
		$SlideDivId = 'displaydetails'.$PorderJV_ID;
			
		echo "<tr ".$Color.">";
		echo "<td colspan='15' valign='top' align='left'><div id='$SlideDivId' class='div-search-details slideAll' style='display:none; margin-left:80px;'>";
		echo "<table border='0' style='table-layout:fixed' cellpadding='0' cellspacing='1' width='900px'>";
		echo "<tr><td width='100px'><span class='style4'>Ledger No.</span></td>
				  <td width='130px'><span class='style4'>Ledger Name</span></td> 
				  <td width='160px'><span class='style4'>Description</span></td> 
				  <td width='80px' align='center'><span class='style4'>Qty</span></td>
				  <td width='50px' align='center'><span class='style4'>Curr.</span></td>
				  <td width='100px' align='center'><span class='style4'>Price</span></td>
				  <td width='80px' align='center'><span class='style4'>Disc(%)</span></td>
				  <td width='120px' align='center'><span class='style4'>Total</span></td>
				  <td width='80px' align='center'><span class='style4'>Manual No</span></td>
			  </tr>";
		while($sub_rows=mysql_fetch_array($sub_result)){
			echo "<tr><td align='left' valign='top' class='style9'>".$sub_rows[1]."</td>
					  <td align='left' valign='top' class='style9'>".$sub_rows[2]."</td>
					  <td align='left' valign='top' class='style9'>".$sub_rows[3]."</td>
					  <td align='center' valign='top' class='style9'>".$sub_rows[5]."</td>
					  <td align='center' valign='top' class='style9'>".$sub_rows[7]."</td>
					  <td align='right' valign='top' class='style9'>".number_format($sub_rows[8],$DecimalBase)."</td>
					  <td align='right' valign='top' class='style9'>".number_format($sub_rows[9],$DecimalBase)."</td>
					  <td align='right' valign='top' class='style9'>".number_format($sub_rows[10],$DecimalBase)."</td>
					  <td align='center' valign='top' class='style9'>".$sub_rows[6]."</td>";
  			echo "</tr>";
				
			if ($sub_rows[4] != "") {
				echo "<tr>
						<td align='left' valign='top' class='style9' colspan='2'>&nbsp;</td>
						<td align='left' valign='top' class='style9' colspan='7'>".$sub_rows[4]."</td>";
				echo "</tr>";	  
			}
			
		}
			
		echo "</table></div></td>";
		echo "</tr>";
	}
}
							
echo"</table></div></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:980px;' 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 id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

	 
echo"</tr>";

echo"</table>";
	


          

		  

print <<<HERE

</form>
</body>

HERE;

?>