<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
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($_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);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_CostCenterParent,0) As Module_CostCenterParent From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


$item   		 = $_GET['item']; 			 if ($item == ""){				$item 			= $_POST["item"];}
$frm	   		 = $_GET['frm']; 			 if ($frm == ""){				$frm		 	= $_POST["frm"];}
$index			 = $_GET['index']; 			 if ($index == "") 				$index 			= $_POST['index'];
$PCostcent_Code  = $_POST["PCostcent_Code"]; if ($PCostcent_Code == ""){	$PCostcent_Code = $_GET["PCostcent_Code"];}
$PCostcent_Name  = $_POST["PCostcent_Name"]; if ($PCostcent_Name == ""){	$PCostcent_Name = $_GET["PCostcent_Name"];}


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

	}

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

$queryExcluded = "select costcent_code from ".$_SESSION['dbLabel'].".costcentstk where Upper(user_account) = Upper('".$_SESSION['useraccount']."') and ifnull(costcent_include,0) = 0 ";
$resExcluded = mysql_query($queryExcluded, $connection);
$numExcluded = mysql_num_rows($resExcluded);

?>

<!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 Costcenters</title>
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<script type="text/javascript" language="javascript">

function clearform()
{
	document.form1.PCostcent_Code.value='';
	document.form1.PCostcent_Name.value='';
	document.form1.action='List_SorCostcenter.php';
	document.form1.submit();
}

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; font-size: 13px;}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="800" border="0" cellspacing="1" 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='30'/>
        <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;'/></td>
      <td bgcolor="#DDDDDD">
	 
 	<?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;'/>";
	}
	?>
		<input name="item" type="hidden" id="item" value="<?php echo $item; ?>" />
		<input name="frm" type="hidden" id="frm" value="<?php echo $frm; ?>" />
		<input name="index" type="hidden" value="<?php echo $index; ?>">
	  </td>
    </tr>
<?php if($_GET['post']=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var FormItem   = document.form1.item.value;
var vIndex	   = document.form1.index.value; 

if (FormOption == 1) 
	opener.document.salesorder.txtCostCentCode.value=document.form1.code1.value;
else if (FormOption == 2)
	opener.document.sales.txtCostCentCode.value=document.form1.code1.value;
else if (FormOption == 3)
	opener.document.sales.txtCostCentCode.value=document.form1.code1.value;	
else if (FormOption == 4)
	opener.document.trans.txtCostCentCode.value=document.form1.code1.value;		
else if (FormOption == 5)
	opener.document.cstjourdt.txtCostCentCodeDt.value=document.form1.code1.value;	
else if (FormOption == 6)
	opener.document.search.PCostCentCode.value=document.form1.code1.value;	
else if (FormOption == 7)
	opener.document.production.txtCostCentCode.value=document.form1.code1.value;
else if (FormOption == 8)
	opener.document.adjustment.txtCostCentCode.value=document.form1.code1.value;	
else if (FormOption == 9)
	opener.document.returnsales.txtCostCentCode.value=document.form1.code1.value;	
else if (FormOption == 10)
	opener.document.returnsales.txtCostCentCode.value=document.form1.code1.value;
else if (FormOption == 11)
	opener.document.getElementById('txtCostCentCode').value=document.form1.code1.value;	
else if (FormOption == 12)
	opener.document.purchasesjv.txtCostCentCode.value=document.form1.code1.value;
else if (FormOption == 13 && FormItem == 1) {
	opener.document.prepayment.txtCostCentCode.value=document.form1.code1.value;
	
	if (opener.document.getElementById('txtCostCentCodeD'))
		opener.document.prepayment.txtCostCentCodeD.value=document.form1.code1.value;
}	
else if (FormOption == 13 && FormItem == 2 && opener.document.getElementById('txtCostCentCodeD'))
	opener.document.prepayment.txtCostCentCodeD.value=document.form1.code1.value;
else if (FormOption == 14)
	opener.document.paymentrqst.txtCostCentCode.value=document.form1.code1.value;		
else if (FormOption == 15)
	opener.document.cstrqst.txtCostCentCodeDt.value=document.form1.code1.value;	
else if (FormOption == 16)
	opener.document.adjustmentdt.txtCostCentCodeDt.value=document.form1.code1.value;		
else if (FormOption == 17) {
	if (opener.document.form1) {
		opener.document.form1.fromcostcent.value=document.form1.code1.value;	
		if (opener.document.form1.fromcostcentname)
			opener.document.form1.fromcostcentname.value=document.form1.desc1.value;	
	} else if (opener.document.budget){
		opener.document.budget.fromcostcent.value=document.form1.code1.value;	
	}
}	
else if (FormOption == 18) {
	opener.document.form1.clientcostcent.value=document.form1.code1.value;	
	opener.document.form1.clientcostcentname.value=document.form1.desc1.value;	
}
else if (FormOption == 19)
	opener.document.purchasedt.txtCostCentCodeDt.value=document.form1.code1.value;		
else if (FormOption == 20)
	opener.document.salesorderdt.txtCostCentCodeDt.value=document.form1.code1.value;
else if (FormOption == 21)
	opener.document.salesdt.txtCostCentCodeDt.value=document.form1.code1.value;
else if (FormOption == 22)
	opener.document.returnsalesdt.txtCostCentCodeDt.value=document.form1.code1.value;
else if (FormOption == 23)
	opener.document.productiondt.txtCostCentCodeDt.value=document.form1.code1.value;
else if (FormOption == 25 && FormItem == 1){
	var idholder = opener.document.getElementById('costcentcodedt['+vIndex+']');
	
	opener.document.getElementById('costcentcodedt['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('costcentcodedt['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}
else if (FormOption == 25 && FormItem == 2){
	var idholder = opener.document.getElementById('costcentcodedt2['+vIndex+']');
	
	opener.document.getElementById('costcentcodedt2['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('costcentcodedt2['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}
else if (FormOption == 26)
	opener.document.journal.txtCostCentCode.value=document.form1.code1.value;
else if (FormOption == 27)
	opener.document.trans.txtCostCentCode.value=document.form1.code1.value;
else if (FormOption == 28 && FormItem == 1){
	var idholder = opener.document.getElementById('costcentcodedtfg['+vIndex+']');
	
	opener.document.getElementById('costcentcodedtfg['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('costcentcodedtfg['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}
else if (FormOption == 28 && FormItem == 2){
	var idholder = opener.document.getElementById('costcentcodedtrm['+vIndex+']');
	
	opener.document.getElementById('costcentcodedtrm['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('costcentcodedtrm['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}else if (FormOption == 29 && FormItem == 1){
	var idholder = opener.document.getElementById('fromcostcent');
	
	opener.document.getElementById('fromcostcent').value=document.form1.code1.value;
	if(opener.document.getElementById('fromcostcentdesc'))
		opener.document.getElementById('fromcostcentdesc').value=document.form1.desc1.value;
	opener.document.getElementById('fromcostcent').focus();
	fireTrigger(idholder,'change');
}else if (FormOption == 29 && FormItem == 2){
	var idholder = opener.document.getElementById('clientcostcent');
	
	opener.document.getElementById('clientcostcent').value=document.form1.code1.value;
	opener.document.getElementById('clientcostcentdesc').value=document.form1.desc1.value;
	opener.document.getElementById('clientcostcent').focus();
	fireTrigger(idholder,'change');
}else if (FormOption == 29 && FormItem == 3){
	var idholder = opener.document.getElementById('exclcostcent');
	
	opener.document.getElementById('exclcostcent').value=document.form1.code1.value;
	opener.document.getElementById('exclcostcentdesc').value=document.form1.desc1.value;
	opener.document.getElementById('exclcostcent').focus();
	fireTrigger(idholder,'change');
}else if (FormOption == 29 && FormItem == 4){
	var idholder = opener.document.getElementById('tocostcent');
	
	opener.document.getElementById('tocostcent').value=document.form1.code1.value;
	opener.document.getElementById('tocostcentdesc').value=document.form1.desc1.value;
	opener.document.getElementById('tocostcent').focus();
	fireTrigger(idholder,'change');
} else if (FormOption == 30 && FormItem == 1){
	var idholder = opener.document.getElementById('tocostcent');
	
	opener.document.getElementById('tocostcent').value=document.form1.code1.value;
	opener.document.getElementById('tocostcentdesc').value=document.form1.desc1.value;
	opener.document.getElementById('tocostcent').focus();
	fireTrigger(idholder,'change');
} else if (FormOption == 31)
	opener.document.porderjv.txtCostCentCode.value=document.form1.code1.value;
 else if (FormOption == 32)
	opener.document.production.txtCostCentCode.value=document.form1.code1.value;
 else if (FormOption == 33){
	if(FormItem == 1)
	opener.document.getElementById('costcentcodedt['+vIndex+']').value=document.form1.code1.value;
	else if(FormItem == 2)
	opener.document.getElementById('costcentcodedt2['+vIndex+']').value=document.form1.code1.value;
 }
 else if (FormOption == 34){
	opener.document.FinancialReportDt.costcentcode.value=document.form1.code1.value;
	opener.document.FinancialReportDt.costcentdesc.value=document.form1.desc1.value;
 }
 else if (FormOption == 35){
	opener.document.form1.fromcostcent.value=document.form1.code1.value;
	opener.document.form1.fromcostcentname.value=document.form1.desc1.value;
 }
 else if (FormOption == 36){
	opener.document.qtybudget.costcent_code.value=document.form1.code1.value;
} else if (FormOption == 37){
	opener.document.getElementById('costcentcode['+vIndex+']').value=document.form1.code1.value;
}
else if (FormOption == 38){
	opener.document.project.txtCostCentCode.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 (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
 			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " and ifnull(costcent.costcent_deactivate,0) = 0 ".
			   " and costcent.costcent_code like ".GetSQLValueString($PCostcent_Code.'%','text')." ".
			   " and costcent_name like ".GetSQLValueString('%'.$PCostcent_Name.'%','text')." ";
if ($numExcluded > 0)			   
	$querycostcent .= " and costcent.costcent_code not in (".$queryExcluded.")";

$querycostcent .= " order by costcent.costcent_code";
			   
$querycostcent1=" select distinct costcent.costcent_code, costcent_name ".
                " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.mastercostcenter,''),'%') ".
 			    " where user_account='".$_SESSION['useraccount']."' ".
				" and (loginstk.mastercostcenter is not null and loginstk.mastercostcenter != '') ".
				" and ifnull(costcent.costcent_deactivate,0) = 0 ".
			    " and costcent.costcent_code like ".GetSQLValueString($PCostcent_Code.'%','text')." ".
			    " and costcent_name like ".GetSQLValueString('%'.$PCostcent_Name.'%','text')." ";
if ($numExcluded > 0)			   
	$querycostcent1 .= " and costcent.costcent_code not in (".$queryExcluded.")";
				
 $querycostcent1 .=	" order by costcent.costcent_code"; 
				
	
if ($frm == 18)
	$costcentresult = mysql_query($querycostcent1,$connection) or die(mysql_error());
else
	$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 ".GetSQLValueString($PCostcent_Code.'%','text')." ".
				   " and costcent_name like ".GetSQLValueString('%'.$PCostcent_Name.'%','text')." ".
				   " and ifnull(costcent.costcent_deactivate,0) = 0 ";
	if ($numExcluded > 0)			   
		$querycostcent .= " and costcent.costcent_code not in (".$queryExcluded.")";			   
	$querycostcent .= " order by costcent_code ";			  
	$costcentresult=mysql_query($querycostcent,$connection) or die(mysql_error());				  
}
//echo 		$querycostcent;
//exit;	  
while($rows=mysql_fetch_array($costcentresult)){
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_SorCostcenter.php?lcode=<?php echo $costcent_code; ?>&frm=<?php echo $frm; ?>&item=<?php echo $item; ?>&index=<?php echo $index; ?>"><?php echo $costcent_code; ?></a></span></td>
      <td colspan="2"><span class="style3"><a href="List_SorCostcenter.php?lcode=<?php echo $costcent_code; ?>&frm=<?php echo $frm; ?>&item=<?php echo $item; ?>&index=<?php echo $index; ?>"><?php echo $costcent_name; ?></a></span></td>
      <td>&nbsp;</td>
    </tr>
<?php
}
?>
  </table>
</form>
</body>
</html>
