<?php
session_start();

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


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
$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 distinct costcent_code, costcent_name from costcent ".
	        " where costcent_code ='".$_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(FA_CostCenter,0) As FA_CostCenter, ifNull(FA_CostCenterApp,0) As FA_CostCenterApp ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
//echo$querylog;
$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>
<?php if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){ ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php } else ?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Costcenters</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.form1.PCostcent_Code.value='';
  document.form1.PCostcent_Name.value=''

}
function opencostcenterform(){

 win = window.open('./Form_CostCenter.php?mode=insert', 'Form_CostCenter', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=600,top=100,left=100');		
	win.creator = self;
	win.focus();
}
</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: #999999}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
<input type='hidden' name='costcent' value='<?php echo $costcent; ?>'>
  <table width="800" border="0" align="center">
    <tr>
      <th colspan="5" bgcolor="#DDDDDD"><span class="style1"> COSTCENTERS </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Costcent  <br> Code / Name</span></td>
      <td bgcolor="#DDDDDD"><input type="text" name="PCostcent_Code" value="<?php echo $PCostcent_Code; ?>"  />
        <input name="code1" type="hidden" id="code1"  value="<?php echo $costcent_code; ?>">
      <td bgcolor="#DDDDDD"><input type="text" name="PCostcent_Name"  value="<?php echo $PCostcent_Name; ?>" size='25'/>
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $costcent_name; ?>" />
	    <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 
	//Gilbert 20-11-2013
	
	if($FA_CostCenterApp==1 && $FA_CostCenter==1){
		echo"<input type='button' name='addbtn' value='Add Costcenter' onclick='opencostcenterform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
	}
	?>
	
		</td>
      <td bgcolor="#DDDDDD">
	  <input name="cst" type="hidden" id="cst" value="<?php echo $_GET['cst']; ?>" />
	  <input name="item1" type="hidden" id="item1" value="<?php echo $_GET['item']; ?>" /></td>
    </tr>
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">

if (document.form1.cst.value=='')
{
	if (document.form1.item1.value==0)
		opener.document.transfer.costcent.value=document.form1.code1.value;
	else if (document.form1.item1.value==11)
		opener.document.transfercharges.txtChgCostcent.value=document.form1.code1.value;
	else
		opener.document.transfer.tocostcent.value=document.form1.code1.value;
}
else
if (document.form1.cst.value!='')
{
	if (document.form1.item1.value==0)
		opener.document.transferrequest.costcent.value=document.form1.code1.value;
	else
		opener.document.transferrequest.tocostcent.value=document.form1.code1.value;
}		
self.close();

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Costcent Code </span></td>
      <td width="411" colspan="2"><span class="style2">Costcent Name </span></td>
      <td width="57"><span class="style3"></span></td>
    </tr>
<?php
		
				  
//Costcenter List
$querycostcent=" select distinct costcent.costcent_code as costcent_code, costcent_name as costcent_name".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
 			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and costcent.costcent_code like '".$PCostcent_Code."%' ".
			   " and costcent_name like '%".$PCostcent_Name."%' ".
 			   " order by costcent.costcent_code ";
			  
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentrows   = mysql_num_rows($costcentresult);
if ($costcentrows == 0)
    $querycostcent=" select distinct costcent_code, costcent_name from costcent ".
	               " where costcent_code like '".$PCostcent_Code."%' ".
				   " and costcent_name like '%".$PCostcent_Name."%' ".
				   " order by costcent_code ";			  
$resultcostcent=mysql_query($querycostcent,$connection) or die(mysql_error());				  
			  
while($rows=mysql_fetch_array($resultcostcent)){
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"><a href="List_TrsCostcenter.php?cst=<?php echo $cst; ?>&item=<?php echo $item; ?>&lcode=<?php echo $costcent_code; ?>&item=<?php echo $_GET['item']; ?>"><?php echo $costcent_code; ?></a></td>
      <td colspan="2"><a href="List_TrsCostcenter.php?cst=<?php echo $cst; ?>&item=<?php echo $item; ?>&lcode=<?php echo $costcent_code; ?>&item=<?php echo $_GET['item']; ?>"><?php echo $costcent_name; ?></a></td>
 
	  <td>&nbsp;</td>
    </tr>
<?php
}
?>
  </table>
</form>
</body>
</html>
