<?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");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) 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);
}	

$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];
if (empty($ledgerinclude)) $ledgerinclude = 0;


$query1="select grid_color1,grid_color2 from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}

if (!empty($TransDate))
   $TransDate = formatDateNoTime($TransDate);

if (empty($TransID))   
   $TransID = 'null';
   

if (empty($TransDate))   
   $TransDate = 'null';
else
   $TransDate = "'".$TransDate."'"; 
   
   
   
if($_GET['lcode']!=""){

$query1="select Ledger_number, Ledger_name, Client_PaymentTerms, Client_CreditLimit, Client_VAT, Client_TC from clients where ledger_number='".$_GET['lcode']."'";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
	extract($rows);
}

$post="1";
}

//User Access
$querylog=" select ifNull(Clients,0) As ClientsView, ifNull(ClientsApp,0) As ClientsApp ".
          " 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);
}


?>

<!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=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Clients</title>
<script type="text/javascript" language="javascript">

function clearform()
{
	document.form1.PLedger_number.value='';
	document.form1.PLedger_name.value='';
	//document.form1.action='List_SorClient.php';
	document.form1.submit();
}

function openclientform(){

 win = window.open('./Form_Clients.php?mainmode=insert&addclient=1', 'Form_Clients', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=750,top=20,left=100');		
	win.creator = self;
	win.focus();
}

function SetNextPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	vcurCode   = document.form1.curCode.value;
	
	document.form1.action='List_SorClient.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&curCode='+curCode+'&startrange='+startrange+'&perpage='+perpage+"&invoicing_id=<?php echo $_GET['invoicing_id']; ?>";
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	vcurCode   = document.form1.curCode.value;
	
	document.form1.action='List_SorClient.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&curCode='+curCode+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}
function RefreshPage(){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	vcurCode   = document.form1.curCode.value;
	vPerPage   = document.form1.perpage.value;
	
	document.form1.action='List_SorClient.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&curCode='+curCode+'&startrange=0&perpage='+vPerPage;
	document.form1.submit();
}

function SubmitForm() {
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	vcurCode   = document.form1.curCode.value;
	vPerPage   = document.form1.perpage.value;
	
	document.form1.action='List_SorClient.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&curCode='+curCode+'&startrange=0&perpage='+vPerPage+"&invoicing_id=<?php echo $_GET['invoicing_id']; ?>&Client_EndofMonthPay=<?php echo $Client_EndofMonthPay; ?>&Client_PaymentTerms=<?php echo $Client_PaymentTerms; ?>&Client_PaymentMode=<?php echo $Client_PaymentMode; ?>&Client_paymentTerms2=<?php echo $Client_paymentTerms2; ?>";
	document.form1.submit();
}

</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
}
.style3 {color: #3500FF; font-size: 13px;}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="" onsubmit="SubmitForm();">
  <table width="800" border="0" cellspacing="1" align="center">
    <tr>
      <th colspan="3" bgcolor="#DDDDDD"><span class="style1"> CLIENTS </span></th>
    </tr>
    <tr>
	<?php
	if($is_percentage == "") $is_percentage = 1;
	if($is_percentage == 1){
		$checked = "checked";
	}
	else{
		$checked = "";
	}
	?>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Client  <br> Number / Name</span></td>
      <td bgcolor="#DDDDDD">
	  <input type="hidden" id="is_percentage" name="is_percentage" value=<?php echo $is_percentage; ?> />
	  <input type="checkbox" id="chk_is_percentage" name="chk_is_percentage" <?php echo $checked; ?> onclick="ckhIsPercentage()" />%
	    <input type="text" name="PLedger_number" placeholder="Ledger Number" value="<?php echo $PLedger_number; ?>" style="width:100px;" />
        <input type="text" name="PLedger_name" placeholder="Ledger Name"  value="<?php echo $PLedger_name; ?>" style="width:200px;"/>
        <input name="code1" type="hidden" id="code1"  value="<?php echo $Ledger_number; ?>">
		<input name="desc1" type="hidden" id="desc1" value="<?php echo $Ledger_name; ?>" />
		
			<input name="warehouse1" type="hidden" id="warehouse1" value="<?php echo $warehouse1; ?>" />
			
			
		<input name="client_email" type="hidden" id="client_email" value="<?php echo $_GET['ClientEmail']; ?>" />
		<input name="client_technicalemail" type="hidden" id="client_technicalemail" value="<?php echo $_GET['TechnicalEmail']; ?>" />
		<input name="client_telephone" type="hidden" id="client_telephone" value="<?php echo $_GET['ClientTelephone']; ?>" />
	    <input name="paymentterms" type="hidden" id="paymentterms"  value="<?php echo $Client_PaymentTerms; ?>">
		<input name="creditlimit" type="hidden" id="creditlimit"  value="<?php echo $Client_CreditLimit; ?>">
		<input name="clientvat" type="hidden" id="clientvat"  value="<?php echo $Client_VAT; ?>">
		<input name="clienttc" type="hidden" id="clienttc"  value="<?php echo $Client_TC; ?>">
        <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="cancelbtn" value="Cancel" onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
<?php
if($Client_EndofMonthPay == 1){
		$client_status =  "End of Month";
	}
	else
		if($Client_PaymentTerms > 0){
			$client_status =  "Payment Term";
	}
	else
			if($Client_PaymentMode != "" && $Client_paymentTerms2 > 0){
				$client_status =  "Weekly";
			}
			else
				if($Client_CashMode==1){
				    $client_status =  "Cash Mode";
				}
?>
<input type="hidden" name="client_status" id="client_status" value="<?php echo $client_status; ?>" />

<?php

echo" <span style='font-size: 11px; color: #999999; font-weight: bold;'>Per page </span><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>";
?>
		
		</td>
      <td bgcolor="#DDDDDD">
	<?php 
	//Gilbert 19-11-2013
	
	if($ClientsApp==1 && $ClientsView==1){
		echo"<input type='button' name='addbtn' value='Add Client' onclick='openclientform();' style='background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
	}
	?>  
	  
	  <input name="item1" type="hidden" id="item1" value="<?php echo $_GET['item']; ?>" /><input name="frm" type="hidden" id="frm" value="<?php echo $_GET['frm']; ?>" />
	  <input name="TransDate" type="hidden" id="TransDate" value="<?php echo $_GET['TransDate']; ?>" /></td>
	  <input name="curCode" type="hidden" id="curCode" value="<?php echo $_GET['curCode']; ?>" /></td>
	  
    </tr>
	<script>
	function ckhIsPercentage(){
	if(document.getElementById('chk_is_percentage').checked){
		document.getElementById('is_percentage').value = 1;
	}
	else{
		document.getElementById('is_percentage').value = 0;
	}
}
	</script>
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">

var FormOption 	= document.form1.frm.value;
var FormItem 	= document.form1.item1.value;

if (FormOption == 1) {
	opener.document.salesorder.txtClientCode.value		= document.form1.code1.value;
	opener.document.salesorder.txtClientName.value		= document.form1.desc1.value;
	opener.document.salesorder.txtPaymentTerms.value	= document.form1.paymentterms.value;
	if(opener.document.salesorder.txtClientVat)
		opener.document.salesorder.txtClientVat.value		= document.form1.clientvat.value;
	if(opener.document.salesorder.txtSorVatChk)
		opener.document.salesorder.txtSorVatChk.value		= document.form1.clientvat.value;
	
	window.opener.SetMaturityDate();
	opener.document.salesorder.txtClientCode.focus();
	//opener.document.salesorder.txtClientCode.blur();
	opener.document.salesorder.submit();
} 


if (FormOption == 500) {
 
	opener.document.transferrequest.txtClientCode.value		= document.form1.code1.value;
	opener.document.transferrequest.txtClientName.value		= document.form1.desc1.value;
	
	
	try
	{
	  //  opener.document.transferrequest.fromwarehouse.value = 'u';	
	}
	
	catch
	{
		
	}
	
//	opener.document.transferrequest.txtPaymentTerms.value	= document.form1.paymentterms.value;
	 
	//opener.document.transferrequest.submit();
} 



else if (FormOption == 2) {
	opener.document.sales.txtClientCode.value	= document.form1.code1.value;
	opener.document.sales.txtClientName.value	= document.form1.desc1.value;
	opener.document.sales.txtPaymentTerms.value	= document.form1.paymentterms.value;
	opener.document.sales.txtClientVat.value	= document.form1.clientvat.value;
	opener.document.sales.txtSaVatChk.value		= document.form1.clientvat.value;
	opener.document.sales.txtSalesmanCode.value	= '';
	opener.document.sales.txtCurr.value			= document.form1.curCode.value;
	window.opener.SetMaturityDate();
	opener.document.sales.txtClientCode.focus();
	//opener.document.sales.txtClientCode.blur();
	opener.document.sales.submit();

} 
else if (FormOption == 3) {
	opener.document.sales.txtClientCode.value	= document.form1.code1.value;
	opener.document.sales.txtClientName.value	= document.form1.desc1.value;
	opener.document.sales.txtPaymentTerms.value	= document.form1.paymentterms.value;
	opener.document.sales.txtClientVat.value	= document.form1.clientvat.value;
	
	window.opener.SetMaturityDate();
	opener.document.sales.txtClientCode.focus();
	//opener.document.sales.txtClientCode.blur();
	opener.document.sales.submit();

} 
else if (FormOption == 4) {
	opener.document.retailpay.txtRetClientCode.value	= document.form1.code1.value;
	opener.document.retailpay.txtRetClientName.value	= document.form1.desc1.value;
	opener.document.retailpay.txtRetClientName.focus();
	opener.document.retailpay.submit();

}
else if (FormOption == 5) {
	opener.document.returnsales.txtClientCode.value		= document.form1.code1.value;
	opener.document.returnsales.txtClientName.value		= document.form1.desc1.value;
	opener.document.returnsales.txtPaymentTerms.value	= document.form1.paymentterms.value;
	opener.document.returnsales.txtClientVat.value		= document.form1.clientvat.value;
	opener.document.returnsales.txtRSaVatChk.value		= document.form1.clientvat.value;
	
	window.opener.SetMaturityDate();
	opener.document.returnsales.txtClientCode.focus();
	//opener.document.returnsales.txtClientCode.blur();
	opener.document.returnsales.submit();

}
else if (FormOption == 6) {
	opener.document.returnsales.txtClientCode.value		= document.form1.code1.value;
	opener.document.returnsales.txtClientName.value		= document.form1.desc1.value;
	opener.document.returnsales.txtPaymentTerms.value	= document.form1.paymentterms.value;
	opener.document.returnsales.txtClientVat.value		= document.form1.clientvat.value;
	
	window.opener.SetMaturityDate();
	opener.document.returnsales.txtClientCode.focus();
	//opener.document.returnsales.txtClientCode.blur();
	opener.document.returnsales.submit();

}
else if (FormOption == 7) {
	opener.document.salesjv.txtClientCode.value	= document.form1.code1.value;
	opener.document.salesjv.txtClientName.value	= document.form1.desc1.value;
	opener.document.salesjv.txtClientVat.value	= document.form1.clientvat.value;
	opener.document.salesjv.txtVatChk.value		= document.form1.clientvat.value;
	opener.document.salesjv.txtClientCode.focus();
	opener.document.salesjv.txtClientCode.blur();
	opener.document.salesjv.submit();

}
else if (FormOption == 8) {
	opener.document.production.txtClientCode.value	= document.form1.code1.value;
	opener.document.production.txtclientname.value	= document.form1.desc1.value;
	opener.document.production.txtClientCode.focus();
	opener.document.production.txtClientCode.blur();
	//opener.document.production.submit();

}
else
	if(FormOption == 9){
		opener.document.getElementById('client_code').value = document.form1.code1.value;
		opener.document.getElementById('client_name').value = document.form1.desc1.value;
		opener.document.getElementById('Rma_Remark').value = document.form1.desc1.value;
		opener.document.getElementById('Client_Email').value = document.form1.client_technicalemail.value;
		opener.document.getElementById('RMA_ClientTelephone').value = document.form1.client_telephone.value;
		opener.document.getElementById('txtemail').value = document.form1.client_email.value;
	}
	else
	if(FormOption == 10){
		opener.document.getElementById('client_code').value = document.form1.code1.value;
		opener.document.getElementById('client_name').value = document.form1.desc1.value;
	}
	else
	if(FormOption == 11){
		opener.document.getElementById('fromclient').value = document.form1.code1.value;
		opener.document.getElementById('fromclient1').value = document.form1.desc1.value;
	}
	else
	if(FormOption == 12){
		opener.document.getElementById('toclient').value = document.form1.code1.value;
		opener.document.getElementById('toclient1').value = document.form1.desc1.value;
	}
	else
	if(FormOption == 13){
		opener.document.getElementById('Client_Code').value = document.form1.code1.value;
		opener.document.getElementById('Client_Name').value = document.form1.desc1.value;
		opener.document.getElementById('Currency_Code').value = document.form1.curCode.value;
		opener.document.getElementById('Client_Name').readOnly = true;
		opener.document.getElementById('client_status').innerHTML = document.getElementById('client_status').value;
	}
	else
	if(FormOption == 14){
		opener.document.getElementById('Client_Code_<?php echo $_GET['invoicing_id']; ?>').value = document.form1.code1.value;
		opener.document.getElementById('Client_Name_<?php echo $_GET['invoicing_id']; ?>').value = document.form1.desc1.value;
		opener.document.getElementById('Currency_Code_<?php echo $_GET['invoicing_id']; ?>').value = document.form1.curCode.value;
		opener.document.getElementById('Client_Name_<?php echo $_GET['invoicing_id']; ?>').readOnly = true;
		opener.document.getElementById('client_status_<?php echo $_GET['invoicing_id']; ?>').innerHTML = document.getElementById('client_status').value;
		
	}
	else
	if(FormOption == 15){
		opener.document.getElementById('from_client_code').value = document.form1.code1.value;
		opener.document.getElementById('from_client_name').value = document.form1.desc1.value;
		opener.document.getElementById('from_client_name').readOnly = true;
		opener.document.getElementById('to_client_code').value = document.form1.code1.value;
		opener.document.getElementById('to_client_name').value = document.form1.desc1.value;
		opener.document.getElementById('to_client_name').readOnly = true;
	}
	else
	if(FormOption == 16){
		opener.document.getElementById('to_client_code').value = document.form1.code1.value;
		opener.document.getElementById('to_client_name').value = document.form1.desc1.value;
		opener.document.getElementById('to_client_name').readOnly = true;
	}
	else
	if(FormOption == 17){
		if (opener.document.getElementById('PClientCode')){
			opener.document.getElementById('PClientCode').value = document.form1.code1.value;
		}
		if (opener.document.getElementById('txtClientCode')) {
			opener.document.getElementById('txtClientCode').value = document.form1.code1.value;
			opener.document.getElementById('txtClientName').value = document.form1.desc1.value;
			opener.document.getElementById('txtClientName').readOnly = true;
		}
		
	}
	
	else if(FormOption == 18){
		opener.document.getElementById('fromclient').value = document.form1.code1.value;
		opener.document.getElementById('fromclientname').value = document.form1.desc1.value;
		
		if (opener.document.getElementById('toclient') && opener.document.getElementById('toclient').value == ''){
			opener.document.getElementById('toclient').value = document.form1.code1.value;
			opener.document.getElementById('toclientname').value = document.form1.desc1.value;		
		}
	}
	else if(FormOption == 19){
		opener.document.getElementById('toclient').value = document.form1.code1.value;
		opener.document.getElementById('toclientname').value = document.form1.desc1.value;
	}
	else if(FormOption == 20){
		if (opener.document.getElementById('olClientCode')){
			opener.document.getElementById('olClientCode').value = document.form1.code1.value;
			opener.document.getElementById('olClientDesc').value = document.form1.desc1.value;
			opener.document.getElementById('olClientRef').value = document.form1.clienttc.value;
		}
		if (opener.document.getElementById('lwClientCode')){
			opener.document.getElementById('lwClientCode').value = document.form1.code1.value;
			opener.document.getElementById('lwClientDesc').value = document.form1.desc1.value;
			opener.document.getElementById('lwClientRef').value = document.form1.clienttc.value;
		}
		
		if (opener.document.getElementById('lfClientCode')){
			opener.document.getElementById('lfClientCode').value = document.form1.code1.value;
			opener.document.getElementById('lfClientDesc').value = document.form1.desc1.value;
		}
		
		if (opener.document.getElementById('fromclient') && FormItem == 1){
			opener.document.getElementById('fromclient').value = document.form1.code1.value;
			opener.document.getElementById('fromclientdesc').value = document.form1.desc1.value;
		}
		if (opener.document.getElementById('toclient') && FormItem == 2){
			opener.document.getElementById('toclient').value = document.form1.code1.value;
			opener.document.getElementById('toclientdesc').value = document.form1.desc1.value;
		}
		
	}
	else if (FormOption == 21) {
	opener.document.salesorderjv.txtClientCode.value	= document.form1.code1.value;
	opener.document.salesorderjv.txtClientName.value	= document.form1.desc1.value;
	opener.document.salesorderjv.txtClientVat.value	= document.form1.clientvat.value;
	opener.document.salesorderjv.txtVatChk.value		= document.form1.clientvat.value;
	opener.document.salesorderjv.txtClientCode.focus();
	opener.document.salesorderjv.txtClientCode.blur();
	opener.document.salesorderjv.submit();

}
else if (FormOption == 22) {
	opener.document.project.project_clientcode.value	= document.form1.code1.value;
	opener.document.project.project_client.value	= document.form1.desc1.value;

}
self.close();
self.focus();
setTimeout("self.close();",10);

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="130"><span class="style2">Client Number </span></td>
      <td width="500"><span class="style2">Client Name </span></td>
      <td width="70"><span class="style2">Salesman</span></td>
    </tr>
<?php
$query1=" select ledger.Ledger_number, ledger.Ledger_name, clients.Client_PaymentTerms, clients.Client_CreditLimit, clients.Client_VAT, clients.Salesmen_Code, clients.currency_code as currency_code, clients.client_telephone1 as ClientTelephone, client_Email as ClientEmail, client_technicalemail as TechnicalEmail, clients.Client_EndofMonthPay, clients.Client_PaymentTerms, clients.Client_PaymentMode, clients.Client_paymentTerms2, clients.Client_CashMode ".
		"  from clients inner join ledger on clients.ledger_number = ledger.ledger_number ".
        " where 1 = 1";
if($is_percentage == 1){
	if($PLedger_number!=""){
$query1 = $query1." AND ledger.Ledger_number like ".GetSQLValueString($PLedger_number.'%','text')." ";
	}
	if($PLedger_name!=""){
$query1 = $query1." AND ledger.Ledger_name like ".GetSQLValueString('%'.$PLedger_name.'%','text')." ";
	}
}
else{
	if($PLedger_number!=""){
	$query1 = $query1." AND ledger.Ledger_number =  '".$PLedger_number."' ";
	}
	if($PLedger_name!=""){
	$query1 = $query1." AND ledger.Ledger_name = '".$PLedger_name."' ";
	}
}

$query1 = $query1." and ((ifNull(ledger.Ledger_inactive,0) = 0 and 1 = 1) Or (ifNull(ledger.Ledger_inactive,0) = 1 and ifNull(Date_Format(ledger.Ledger_inactivedate,'%Y%m%d'), $TransDate) >= $TransDate) )  ";
		
		
if($ledgerinclude== 1 && $ledgerexist > 0) {
	$query1 = $query1." And Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
								"              where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
								"              and (ledger_number is not null and ledger_number != '') ".
								"              and CLIENTS.LEDGER_NUMBER like concat(ledgerstk.ledger_number,'%') ".
								"            ) ";
}
if($ledgerinclude== 0 && $ledgerexist > 0){
	$query1 = $query1." And Not Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
								"                  where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
								"          	       and (ledger_number is not null and ledger_number != '') ".
								"                  and CLIENTS.LEDGER_NUMBER like concat(ledgerstk.ledger_number,'%') ".
								"                ) ";	
}
		$query1 = $query1." order by ledger.Ledger_number ";	  
		
		//ECHO  $query1;
$resultsearch=mysql_query($query1,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='') $startrange = 0;
if ($perpage=='')	 $perpage = 20;
		
$query1 = $query1." limit ".$startrange.",".$perpage;
	
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
	extract($rows);

	$ct+=1;

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

	?>
		<tr <?php echo $Color; ?>>
		  <td align="left"><span class="style3"><a href="List_SorClient.php?lcode=<?php echo $Ledger_number; ?>&Ledger_name=<?php echo $Ledger_name; ?>&frm=<?php echo $_GET['frm']; ?>&item=<?php echo $_GET['item']; ?>&curCode=<?php echo $currency_code; ?>&ClientTelephone=<?php echo $ClientTelephone; ?>&ClientEmail=<?php echo $ClientEmail; ?>&TechnicalEmail=<?php echo $TechnicalEmail; ?>&invoicing_id=<?php echo $_GET['invoicing_id']; ?>&Client_EndofMonthPay=<?php echo $Client_EndofMonthPay; ?>&Client_PaymentTerms=<?php echo $Client_PaymentTerms; ?>&Client_PaymentMode=<?php echo $Client_PaymentMode; ?>&Client_paymentTerms2=<?php echo $Client_paymentTerms2; ?>&Client_CashMode=<?php echo $Client_CashMode; ?>"><?php echo $Ledger_number; ?></a></span></td>
		  <td><span class="style3"><a href="List_SorClient.php?lcode=<?php echo $Ledger_number; ?>&Ledger_name=<?php echo $Ledger_name; ?>&frm=<?php echo $_GET['frm']; ?>&item=<?php echo $_GET['item']; ?>&curCode=<?php echo $currency_code; ?>&ClientTelephone=<?php echo $ClientTelephone; ?>&ClientEmail=<?php echo $ClientEmail; ?>&TechnicalEmail=<?php echo $TechnicalEmail; ?>&invoicing_id=<?php echo $_GET['invoicing_id']; ?>&Client_EndofMonthPay=<?php echo $Client_EndofMonthPay; ?>&Client_PaymentTerms=<?php echo $Client_PaymentTerms; ?>&Client_PaymentMode=<?php echo $Client_PaymentMode; ?>&Client_paymentTerms2=<?php echo $Client_paymentTerms2; ?>&Client_CashMode=<?php echo $Client_CashMode; ?>"><?php echo $Ledger_name; ?></a></span></td>
		  <td><span class="style3"><?php echo $Salesmen_Code; ?></span></td>
		</tr>
	<?php
	
}

echo"<tr><td colspan='3'>&nbsp;</td></tr>
	 <tr><td colspan='3'><table width='100%' border=0><tr><td width='270'>&nbsp;</td><td width='130' 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='130' 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='270'>&nbsp;</td></tr>";
echo"</table>";
echo"</td></tr>";
	
?>
  </table>
</form>
</body>
</html>
