<?php
session_start();

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

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

}

$query1="select ifnull(Module_CashRegisterAutoDisc,0) as Module_CashRegisterAutoDisc from module";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}

set_time_limit(0);


   
 if($_GET['lcode']!=""){

	$query1=" Select Ledger_Number, Ledger_Name, ifnull(CLIENT_DISCOUNTPER,0) as client_discountper ".
	        " From Clients Where Ledger_Number='".$_GET['lcode']."'"; //echo $query1;
	$result1=mysql_query($query1,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result1)){
    	extract($rows);
	}
	
    $post="1";
}  

$frm		= $_GET['frm']; if ($frm == ""){$frm = $_POST["frm"];}
$str 		= $_GET['str'];if ($str == ""){$str = $_POST["str"];}
$txtRetCurr = $_GET['txtRetCurr'];if ($txtRetCurr == ""){$txtRetCurr = $_POST["txtRetCurr"];}
$SRetId 	= $_GET['SRetId'];if ($SRetId == ""){$SRetId = $_POST["SRetId"];}
$PTotal    	= $_GET['PTotal'];if ($PTotal == ""){$PTotal = $_POST["PTotal"];}
$PLedgerNo 	= $_POST["PLedgerNo"]; if ($PLedgerNo == ""){$PLedgerNo = $_GET["PLedgerNo"];}		
$perpage   	= $_POST["perpage"]; if ($perpage == ""){$perpage = $_GET["perpage"];}		


?>
<!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>Select Credit</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  vFrm    		= document.form1.frm.value;
  vStr   	 	= document.form1.str.value;
  txtRetCurr   	= document.form1.txtRetCurr.value;
  SRetId    	= document.form1.SRetId.value;
  document.form1.PLedgerNo.value='';

  document.form1.action='List_CreditCards.php?PLedgerNo=&frm='+vFrm+'&str='+vStr+'&txtRetCurr='+txtRetCurr+'&SRetId='+SRetId;   
  document.form1.submit();

}
function SetNextPage(startrange,perpage){
	vFrm    	= document.form1.frm.value;
	vStr    	= document.form1.str.value;
	txtRetCurr  = document.form1.txtRetCurr.value;
	SRetId    	= document.form1.SRetId.value;
	
	document.form1.action='List_CreditCards.php?PLedgerNo=&frm='+vFrm+'&str='+vStr+'&txtRetCurr='+txtRetCurr+'&SRetId='+SRetId+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm    	= document.form1.frm.value;
	vStr    	= document.form1.str.value;
	txtRetCurr  = document.form1.txtRetCurr.value;
	SRetId    	= document.form1.SRetId.value;
	
	document.form1.action='List_CreditCards.php?PLedgerNo=&frm='+vFrm+'&str='+vStr+'&txtRetCurr='+txtRetCurr+'&SRetId='+SRetId+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}
function RefreshPage(){
	vFrm       = document.form1.frm.value;
	vStr       = document.form1.str.value;
	vPerPage   = document.form1.perpage.value;
	txtRetCurr  = document.form1.txtRetCurr.value;
	SRetId    	= document.form1.SRetId.value;
	
	document.form1.action='List_CreditCards.php?PLedgerNo=&frm='+vFrm+'&str='+vStr+'&txtRetCurr='+txtRetCurr+'&SRetId='+SRetId+'&startrange=0&perpage='+vPerPage;
	document.form1.submit();
}

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

<body>

<?php


echo "
<form id='form1' name='form1' method='post' action=''>
  <table width='560' border='0' cellspacing='1' align='center' style='table-layout:fixed'>
	<col width=130>
	<col width=435>";

echo "
    <tr>
      <th colspan='2' bgcolor='#DDDDDD'><span class='style1'> LIST OF CREDITS </span></th>
    </tr>
    <tr>
      <td bgcolor='#DDDDDD' > <span class='style2'>Search for Credit  <br> No. / Name</span></td>
      <td bgcolor='#DDDDDD' >
	    <input type='text' name='PLedgerNo' value='$PLedgerNo' style='width:160px; ' />
		<input type='hidden' name='code1' id='code1' value='$Ledger_Number'>
		<input type='hidden' name='desc1' id='desc1' value='$Ledger_Name'>
		<input type='hidden' name='client_discountper' id='client_discountper' value='$client_discountper'>
		<input type='hidden' name='Module_CashRegisterAutoDisc' id='Module_CashRegisterAutoDisc' value='$Module_CashRegisterAutoDisc'>
		<input type='hidden' name='txtRetCurr' id='txtRetCurr' value='$txtRetCurr'>
		<input type='hidden' name='SRetId' id='SRetId' value='$SRetId'>
		<input type='hidden' name='frm' value='$frm'>
		<input type='hidden' name='str' value='$str'>
		<input type='hidden' name='PTotal' value='$PTotal'>
	    <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;'/>
			
		<span class='style2'>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>	
    </tr>";
	


?>	
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var vStr       = document.form1.str.value;
var discmodule = document.form1.Module_CashRegisterAutoDisc.value;

if (FormOption == 1) {
	if(discmodule == 1){
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
			xmlhttp.onreadystatechange=function()
		{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			// console.log(xmlhttp.responseText);
			var res = xmlhttp.responseText.split("[BRK]"); 
			opener.document.retailpay.txtRetCreditCard.value=document.form1.code1.value;
			opener.document.retailpay.txtRetCreditName.value=document.form1.desc1.value;
			opener.document.retailpay.txtRetCreditCard.focus();
		
			opener.document.retailpay.txtRetCreditAmtB12.value = 0;
			opener.document.retailpay.txtRetCreditAmt.value	   = 0;
			opener.document.retailpay.txtRetCashAmtB12.value   = 0;
			opener.document.retailpay.txtRetCashAmt.value      = 0;
			opener.document.retailpay.txtRetCreditDisc.value      = client_discountper;
			opener.document.retailpay.txtRetCreditAmt.value = res[1];//document.form1.PTotal.value;
			window.opener.ConvertAmount('txtRetCreditAmt');
			opener.document.retailpay.submit();
			self.close();
			
			}
		}
	var txtRetCurr 			= document.form1.txtRetCurr.value;
	var SRetId 				= document.form1.SRetId.value;
	var client_discountper  = document.form1.client_discountper.value;
	
	xmlhttp.open("GET","Form_Retail_Actions.php?mode=adddiscount2&txtRetCurr="+txtRetCurr+"&SRetId="+SRetId+"&txtRetDiscountPer="+client_discountper,true);
	xmlhttp.send();
		
	} else { 
		opener.document.retailpay.txtRetCreditCard.value=document.form1.code1.value;
		opener.document.retailpay.txtRetCreditName.value=document.form1.desc1.value;
		opener.document.retailpay.txtRetCreditCard.focus();
		
		opener.document.retailpay.txtRetCreditAmtB12.value = 0;
		opener.document.retailpay.txtRetCreditAmt.value	   = 0;
		opener.document.retailpay.txtRetCashAmtB12.value   = 0;
		opener.document.retailpay.txtRetCashAmt.value      = 0;
		
		opener.document.retailpay.txtRetCreditAmt.value = document.form1.PTotal.value;
		window.opener.ConvertAmount('txtRetCreditAmt');
		opener.document.retailpay.submit();
		self.close();
	}
} 
else if (FormOption == 2) {
	if (vStr == 1) {
		opener.document.form1.fromcredit.value=document.form1.code1.value;
		opener.document.form1.fromcreditdesc.value=document.form1.desc1.value;
	} else {
		opener.document.form1.tocredit.value=document.form1.code1.value;
		opener.document.form1.tocreditdesc.value=document.form1.desc1.value;
	}
	self.close();
} 
else if (FormOption == 3) {
	opener.document.form1.txtLedgerClientnbr.value=document.form1.code1.value;
	self.close();
} 

else if (FormOption == 4) {
	opener.document.form1.txtRetCreditCard.value=document.form1.code1.value;
	opener.document.form1.txtRetCreditName.value=document.form1.desc1.value;
	self.close();
} 


</script>
<?php } ?>

<?php

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


$query = "";

/*$query = " Select Ledger.Ledger_Number As Ledger_Number, Ledger.Ledger_Name As Ledger_Name ".
		 " From Ledger Inner join CashType on Ledger.CashType_Code = CashType.CashType_Code ".
		 " Where (Ledger.Ledger_Number like '%".$PLedgerNo."%' or Ledger.Ledger_Name like '%".$PLedgerNo."%' ) ".
		 " And CashType.Journal_Type = 'RI' ".
		 " Order By Ledger.Ledger_Number ";*/
		 
$query = " Select Clients.Ledger_Number As Ledger_Number, Clients.Ledger_Name As Ledger_Name ".
		 " From Clients ".
		 " Where (Clients.Ledger_Number like '%".$PLedgerNo."%' or Clients.Ledger_Name like '%".$PLedgerNo."%' ) ".
		 " Order By Clients.Ledger_Number ";		 
				

			
//echo $query;//exit;	

if($post!="1") {
	
	$resultsearch=mysql_query($query,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($resultsearch);
	
	if ($startrange=='' || $PLedgerNo != '')
		$startrange = 0;
	if ($perpage=='')
		$perpage = 20;
		
	$query = $query." limit ".$startrange.",".$perpage;
	//echo $query;exit;
	$result1=mysql_query($query,$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."'";	
		}
		
			
		echo "<tr  $Color > 
				<td align='left' ><span class='style4'><a href='List_CreditCards.php?lcode=$Ledger_Number&frm=$frm&str=$str&PTotal=$PTotal&txtRetCurr=$txtRetCurr&SRetId=$SRetId'>$Ledger_Number</a></span></td>
 			    <td align='left'><span class='style4'>".$Ledger_Name."</span></td>
		     </tr>";


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

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


?>
  </table>
</form>
</body>
</html>
