<?php
session_start();

include("config.inc.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");



$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($_GET['lcode']!=""){

$query1="select customer_officeId, office_address,customer_code from Customer_Offices 
		where customer_code='".$_GET['lcode']."' and customer_officeId='".$_GET['lcode2']."'";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
	extract($rows);
}

$post="1";
}


?>

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

function clearform()
{
	document.form1.Pcustomercode.value='';
	document.form1.code1.value='';
	document.form1.code2.value='';
	document.form1.desc2.value='';
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	
	document.form1.action='List_SorCustomerBranches.php?Pcustomercode=&lcode=&frm='+vFrm+'&item1='+vItem;
	
	document.form1.submit();
}

function SetNextPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vmainoffice 	   = document.form1.mainoffice.value;
	
	document.form1.action='List_SorCustomerBranches.php?Pcustomercode=&lcode=&frm='+vFrm+'&item1='+vItem+'&mainoffice='+vmainoffice+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vmainoffice 	   = document.form1.mainoffice.value;
	
	document.form1.action='List_SorCustomerBranches.php?Pcustomercode=&lcode=&frm='+vFrm+'&item1='+vItem+'&mainoffice='+vmainoffice+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}
function RefreshPage(){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vPerPage   = document.form1.perpage.value;
	vmainoffice 	   = document.form1.mainoffice.value;
	
	document.form1.action='List_SorCustomerBranches.php?Pcustomercode=&lcode=&frm='+vFrm+'&item1='+vItem+'&mainoffice='+vmainoffice+'&startrange=0&perpage='+vPerPage;
	document.form1.submit();
}

function SubmitForm() {
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	Pcustomercode = document.form1.Pcustomercode.value;
	vPerPage   = document.form1.perpage.value;
	vmainoffice 	   = document.form1.mainoffice.value;
	
	document.form1.action='List_SorCustomerBranches.php?Pcustomercode=&lcode=&frm='+vFrm+'&item1='+vItem+'&mainoffice='+vmainoffice+'&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 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
	font-size: 13px;
}
.style3 {color: #999999; 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="4" bgcolor="#DDDDDD"><span class="style1"> Branches </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Branch  <br> Customer Code / Branch Address</span></td>
      <td bgcolor="#DDDDDD" colspan="2">
		<input type="text" name="Pcustomercode" value="<?php echo $Pcustomercode; ?>" style="width:100px;" />
		<input type="text" name="Pofficeaddress"  value="<?php echo $Pofficeaddress; ?>" style="width:200px;"/>
		 <input name="code1" type="hidden" id="code1"  value="<?php echo $Pcustomercode; ?>">
        <input name="code2" type="hidden" id="code2" value="<?php echo $customer_officeId; ?>" />
		<input name="desc2" type="hidden" id="desc2" value="<?php echo $office_address; ?>" />
	    <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

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 20-11-2013
	
	
	?>
	
		<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="mainoffice" type="hidden" id="mainoffice" value="<?php echo $_GET['mainoffice']; ?>" />
    </tr>
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var item1 = document.form1.item1.value;
if (FormOption == 1){
	opener.document.visit.txtBranchOfficeId.value=document.form1.code2.value;
	opener.document.visit.txtBranchOffice.value=document.form1.desc2.value;
	opener.document.visit.txtBranchOffice.focus();
	
	opener.document.visit.submit();
}

self.close();

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Customer Code</span></td>
      <td width="200" colspan="2"><span class="style2">Customer Name </span></td>
	  <td width="411" colspan="2"><span class="style2">Project Description</span></td>
  </tr>
<?php
/*$query1=" select ledger.Ledger_number, ledger.Ledger_name ".
		" from salesmen inner join ledger on salesmen.ledger_number = ledger.ledger_number ".
        " where ledger.Ledger_number like ".GetSQLValueString($PLedger_number.'%','text')." ".
		" and ledger.Ledger_name like ".GetSQLValueString('%'.$PLedger_name.'%','text')." ".
		" and ((ifNull(ledger.Ledger_inactive,0) = 0 and 1 = 1) Or (ifNull(ledger.Ledger_inactive,0) = 1 and ifNull(ledger.Ledger_inactivedate, $TransDate) >= $TransDate) )  ".
		" order by ledger.Ledger_number ";*/
		
$query1=" select customer_officeId,office_address,p.Customer_Code as customer_code,Customer_Name
		from Customer_Offices p
		left join Customer c on c.Customer_Code=p.customer_code
		where if( '$Pcustomercode'='',1=1,p.customer_code like '%$Pcustomercode%') 
			and if('$mainoffice' = '' ,mainoffice_id is not null ,mainoffice_id='$mainoffice')
			and if( '$Pofficeaddress'='',1=1,office_address like '%$Pofficeaddress%')
			order by p.customer_code ";
//echo  $query1;
//exit;
$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_SorCustomerBranches.php?lcode=<?php echo $customer_code; ?>&lcode2=<?php echo $customer_officeId; ?>&mainoffice=<?php echo $_GET['mainoffice']; ?>&frm=<?php echo $_GET['frm']; ?>&item=<?php echo $_GET['item']; ?>"><?php echo $customer_code; ?></a></span></td>
		  <td colspan="2"><span class="style3"><a href="List_SorCustomerBranches.php?lcode=<?php echo $customer_code; ?>&lcode2=<?php echo $customer_officeId; ?>&mainoffice=<?php echo $_GET['mainoffice']; ?>&frm=<?php echo $_GET['frm']; ?>&item=<?php echo $_GET['item']; ?>"><?php echo $Customer_Name; ?></a></span></td>
		  <td colspan="2"><span class="style3"><a href="List_SorCustomerBranches.php?lcode=<?php echo $customer_code; ?>&lcode2=<?php echo $customer_officeId; ?>&mainoffice=<?php echo $_GET['mainoffice']; ?>&frm=<?php echo $_GET['frm']; ?>&item=<?php echo $_GET['item']; ?>"><?php echo $office_address; ?></a></span></td>
		</tr>
	<?php
}

echo"<tr><td colspan='4'>&nbsp;</td></tr>
	 <tr><td colspan='4'><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>
