<?php
session_start();

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


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

$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];

   
print <<<HERE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Ledgers</title>
<script type="text/javascript" language="javascript">

function SelectRecord(value1,value2){

	vFrm  = document.search.frm.value;
	vItem = document.search.item.value;
	vIndex = document.search.index.value;
	if (vFrm=='1'){
		if (vItem == 1) {
			opener.document.adjustment.txtInvDebit.value = value1;
			opener.document.adjustment.txtInvDebitName.value = value2;
			opener.document.adjustment.txtInvDebit.focus();
			//opener.document.adjustment.submit();
		} else if (vItem == 2) {
			opener.document.adjustment.txtInvCredit.value = value1;
			opener.document.adjustment.txtInvCreditName.value = value2;
			opener.document.adjustment.txtInvCredit.focus();
			//opener.document.adjustment.submit();
		}  else if (vItem == 3) {
			opener.document.getElementById('InvDEBITaccount['+vIndex+']').value = value1;
			opener.document.adjustment.txtInvCredit.focus();
			//opener.document.adjustment.submit();
		}
	}
	else if (vFrm=='2'){
		if (vItem == 1) {
			opener.document.chargesdt.txtDebitAcc.value = value1;
			opener.document.chargesdt.txtDebitAccName.value = value2;
			opener.document.chargesdt.txtDebitAcc.focus();
			//opener.document.chargesdt.submit();
		} else if (vItem == 2) {
			opener.document.chargesdt.txtCreditAcc.value = value1;
			opener.document.chargesdt.txtCreditAccName.value = value2;
			opener.document.chargesdt.txtCreditAcc.focus();
			//opener.document.chargesdt.submit();
		}
	}if (vFrm=='3'){
		if (vItem == 1) {
			opener.document.adjustmentrequest.txtInvDebit.value = value1;
			opener.document.adjustmentrequest.txtInvDebitName.value = value2;
			opener.document.adjustmentrequest.txtInvDebit.focus();
			//opener.document.adjustmentrequest.submit();
		} else if (vItem == 2) {
			opener.document.adjustmentrequest.txtInvCredit.value = value1;
			opener.document.adjustmentrequest.txtInvCreditName.value = value2;
			opener.document.adjustmentrequest.txtInvCredit.focus();
			//opener.document.adjustmentrequest.submit();
		}  else if (vItem == 3) {
			opener.document.getElementById('InvDEBITaccount['+vIndex+']').value = value1;
			opener.document.adjustmentrequest.txtInvCredit.focus();
			//opener.document.adjustmentrequest.submit();
		}
	}
		

	self.close();
}

function ClearForm()
{
	document.search.PRef.value='';
	document.search.submit();
}


function SetNextPage(startrange,perpage){
	vFrm  = document.search.frm.value;
	vItem = document.search.item.value;
	vIndex = document.search.index.value;
	document.search.action='Form_Adjustment_ListLedgers.php?frm='+vFrm+'&item='+vItem+'&index='+vIndex+'&PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm  = document.search.frm.value; 
	vItem = document.search.item.value;
	vIndex = document.search.index.value;
	document.search.action='Form_Adjustment_ListLedgers.php?frm='+vFrm+'&item='+vItem+'&index='+vIndex+'&PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


</script>
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 13px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
}
.style3 {
	color: #999999; 
    font-weight: bold;

}
.style4 {
	color: #3333FF; 
    
}
.style5 {
	color: #999999;
	font-weight: bold;
	font-size: 12px;
}
</style>

</head>

<body>
<form id="search" name="search" method="post" action="">
<input type='hidden' name='item' id='item' value='$item' />
<input type='hidden' name='frm' id='frm' value='$frm' />
<input type='hidden' name='index' id='index' value='$index' />
<input type='hidden' name='message' value='$message' size='200'>
			
  <table width="600" border="0" cellspacing="1" align="center">
    <tr>
      <th colspan="2" bgcolor="#DDDDDD"><span class="style1"> List of Ledgers </span></th>
    </tr>
	
HERE;




echo"<tr>
	  <td bgcolor='#DDDDDD'><span class='style3'>Search by Code / Name</span></td>
	  <td bgcolor='#DDDDDD'><input type='text' name='PRef' value='$PRef'  size='30' />
		<input type='submit' name='Submit' value='Find' 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>
	</tr>";


	
echo"<tr bgcolor='#E5E5E5'>
      <td width='120'><span class='style2'>Code </span></td>
      <td width='450'><span class='style2'>Name </span></td>
    </tr>";

		
$sub_query = " select ledger.ledger_number, ledger_name from ledger ".
             " where Ledger_grpdet=0 ";
	
if($ledgerinclude== 1)
	$sub_query=$sub_query." And Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
						  "              where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
						  "              and ledger.ledger_number like concat(ledgerstk.ledger_number,'%') ".
						  "            ) ";

if($ledgerinclude== 0)
	$sub_query=$sub_query." And Not Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
						  "                  where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
						  "                  and ledger.ledger_number like concat(ledgerstk.ledger_number,'%') ".
						  "                ) ";
	
	
$sub_query=$sub_query." and (ledger.ledger_number like"."('%".$PRef."%') or ledger_name like"."('%".$PRef."%')) order by ledger_number";			
		
$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($sub_result);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$sub_query = $sub_query." limit ".$startrange.",".$perpage;

$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
while($sub_rows=mysql_fetch_array($sub_result)){
	
	extract($sub_rows);

	$ct+=1;

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

    $desc=ereg_replace("'","%27",$sub_rows[1]);

	echo"<tr $Color>";
	echo"  <td align='left'><a onclick=\"SelectRecord('$sub_rows[0]','$desc');\" href=''><span class='style4'>$sub_rows[0]</span></a></td>";
	echo"  <td align='left'><span class='style4'>$sub_rows[1]</span></td>";
    echo"</tr>";
	
}	

echo"   <tr><td colspan='2'>&nbsp;</td></tr>
		<tr><td colspan=2><table width='100%' border=0><tr><td width='150'>&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='150'>&nbsp;</td></tr>";
		echo"<table>";

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

print <<<HERE

</form>
</body>
</html>

HERE;
?>