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

}


$item   		  = $_GET['item']; if ($item == ""){$item = $_POST["item"];}
$frm	   		  = $_GET['frm']; if ($frm == ""){$frm = $_POST["frm"];}
$index			  = $_GET['index']; if ($index == "") $index = $_POST['index'];
$PWarehouse_Code  = $_POST["PWarehouse_Code"]; if ($PWarehouse_Code == ""){$PWarehouse_Code = $_GET["PWarehouse_Code"];}
$PWarehouse_Name  = $_POST["PWarehouse_Name"]; if ($PWarehouse_Name == ""){$PWarehouse_Name = $_GET["PWarehouse_Name"];}
$warehouse_type   = $_GET['warehouse_type']; if ($warehouse_type == ""){$warehouse_type = $_POST["warehouse_type"];}
$parentWh 		  = $_GET['parentWh'];

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

	$query1=" select distinct warehouse_code, warehouse_name from warehouse ".
			" where warehouse_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(FI_Warehouse,0) As FI_Warehouse, ifNull(FI_WarehouseApp,0) As FI_WarehouseApp, ifnull(Login_location,0) as Login_location ".
          " 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 Warehouses</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(pwh)
{
	document.form1.PWarehouse_Code.value = '';
	document.form1.PWarehouse_Name.value = '';
	document.form1.action = 'List_SorWarehouse.php?parentWh='+pwh;
	document.form1.submit();
}

function openwarehouseform(){

 win = window.open('./Form_Warehouse.php?mode=insert', 'Form_Warehouse', '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;
	font-size: 13px;
}
.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="2" bgcolor="#DDDDDD"><span class="style1"> WAREHOUSES </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Warehouse  <br> Code / Name</span></td>
      <td bgcolor="#DDDDDD">
	    <input type="text" name="PWarehouse_Code" value="<?php echo $PWarehouse_Code; ?>"  />
        <input type="text" name="PWarehouse_Name"  value="<?php echo $PWarehouse_Name; ?>" size='25'/>
        <input name="code1" type="hidden" id="code1"  value="<?php echo $warehouse_code; ?>">
		<input name="desc1" type="hidden" id="desc1" value="<?php echo $warehouse_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("<?php if($warehouse_type=='shelf') echo $parentWh; ?>");'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; ?>">
		<input type='hidden' name="warehouse_type" value="<?php echo $warehouse_type; ?>" >
		
	<?php 
	//Gilbert 20-11-2013
	
	if($FI_WarehouseApp==1 && $FI_Warehouse==1){
		echo"<input type='button' name='addbtn' value='Add Warehouse' onclick='openwarehouseform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
	}
	?>
	  </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; 
var warehouse_type 	   = document.form1.warehouse_type.value; 
if (FormOption == 1) {
	if (FormItem == 1) {
		opener.document.production.txtWhCode.value=document.form1.code1.value;
		opener.document.production.txtWhDesc.value=document.form1.desc1.value;
	}
	else if (FormItem == 2) {
		opener.document.production.txtWhCodeRM.value=document.form1.code1.value;
		opener.document.production.txtWhDescRM.value=document.form1.desc1.value;
	}
	else if (FormItem == 3) {alert(document.form1.code1.value);
		opener.document.production.txtFromWhCode.value=document.form1.code1.value;
		
	}
	
}
else if (FormOption == 2){
	<?php if($warehouse_type != ''){ ?>
	if(warehouse_type=='warehouse'){
	var idholder = opener.document.getElementById('warehousecode['+vIndex+']');
	
	opener.document.getElementById('warehousecode['+vIndex+']').value=document.form1.code1.value;
	if(opener.document.getElementById('locationcode['+vIndex+']'))
	opener.document.getElementById('locationcode['+vIndex+']').value='';
	fireTrigger(idholder,'change');
	opener.document.getElementById('warehousecode['+vIndex+']').focus();
	} else{
		var idholder = opener.document.getElementById('locationcode['+vIndex+']');
	
	opener.document.getElementById('locationcode['+vIndex+']').value=document.form1.code1.value;
	fireTrigger(idholder,'change');
	opener.document.getElementById('locationcode['+vIndex+']').focus();
	}
	<?php } else {?>
	var idholder = opener.document.getElementById('warehousecode['+vIndex+']');
	
	opener.document.getElementById('warehousecode['+vIndex+']').value=document.form1.code1.value;
	fireTrigger(idholder,'change');
	opener.document.getElementById('warehousecode['+vIndex+']').focus();
	<?php } ?>
	//opener.document.getElementById('warehousename['+vIndex+']').value=document.form1.desc1.value;
	
	//$('#'.idholder).trigger("change");
	
}
else if (FormOption == 3){
	var idholder = opener.document.getElementById('warehousecodefg['+vIndex+']');
	
	opener.document.getElementById('warehousecodefg['+vIndex+']').value=document.form1.code1.value;
	//opener.document.getElementById('warehousenamefg['+vIndex+']').value=document.form1.desc1.value;
	
	//$('#'.idholder).trigger("change");
	fireTrigger(idholder,'change');
	opener.document.getElementById('warehousecodefg['+vIndex+']').focus();
}
else if (FormOption == 4){
	var idholder = opener.document.getElementById('warehousecoderm['+vIndex+']');
	
	opener.document.getElementById('warehousecoderm['+vIndex+']').value=document.form1.code1.value;
	//opener.document.getElementById('warehousenamerm['+vIndex+']').value=document.form1.desc1.value;
	
	//$('#'.idholder).trigger("change");
	fireTrigger(idholder,'change');
	opener.document.getElementById('warehousecoderm['+vIndex+']').focus();
}
else if (FormOption == 5){
	opener.document.getElementById('txtItemWarehouse').value=document.form1.code1.value;
	opener.document.getElementById('txtItemWarehouse').focus();
}
else if (FormOption == 6) {
	if (FormItem == 1) {
		opener.document.transfer.txtItemFromWH.value=document.form1.code1.value;
	}
	else if (FormItem == 2) {
		opener.document.transfer.txtItemToWH.value=document.form1.code1.value;
	}
	
}else if (FormOption == 7) {
	if (FormItem == 1) {
		opener.document.productiondt.txtWhCode.value=document.form1.code1.value;
		opener.document.productiondt.txtWhDesc.value=document.form1.desc1.value;
	}
	
	
}
else if (FormOption == 8) {
	if (FormItem == 1) {
		opener.document.getElementById('txtFromWhCode['+vIndex+']').value=document.form1.code1.value;
		
	}
}
	else if (FormOption == 9) {
	if (FormItem == 0) {
		opener.document.form1.fromwhcode.value=document.form1.code1.value;
		opener.document.form1.fromwhdesc.value=document.form1.desc1.value;
		
	}else if (FormItem == 1) {
		opener.document.form1.towhcode.value=document.form1.code1.value;
		opener.document.form1.towhdesc.value=document.form1.desc1.value;
		
	}
	
	
}
else if (FormOption == 10) {
	if (FormItem == 1) { 
		opener.document.search.PWarehouseCode.value=document.form1.code1.value;
	}
	
}
else if (FormOption == 11) {
	if (FormItem == 1) { 
		opener.document.getElementById('itemjour_location['+vIndex+']').value=document.form1.code1.value;
		//opener.document.getElementById('txtFromWhCode['+vIndex+']').value=document.form1.desc1.value;
				
	}
	
} 
else if (FormOption == 12){
	if (FormItem == 1) { 
		opener.document.getElementById('mat_warehouse').value=document.form1.code1.value;
		opener.document.getElementById('mat_warehousedesc').value=document.form1.desc1.value;
	} else {
		var idholder = opener.document.getElementById('warehousecodesf['+vIndex+']');
	
	opener.document.getElementById('warehousecodesf['+vIndex+']').value=document.form1.code1.value;
	//opener.document.getElementById('warehousenamerm['+vIndex+']').value=document.form1.desc1.value;
	
	//$('#'.idholder).trigger("change");
	fireTrigger(idholder,'change');
	opener.document.getElementById('warehousecodesf['+vIndex+']').focus();
	}
}

else if (FormOption == 13) {
	
	opener.document.getElementById('warehousecode['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('warehousedesc['+vIndex+']').value=document.form1.desc1.value;
	
	
} 

self.close();

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="150"><span class="style2">Warehouse Code </span></td>
      <td width="400"><span class="style2">Warehouse Name </span></td>
    </tr>
<?php

$querywarehouse= " select distinct warehouse.warehouse_code, warehouse_name ".
				 " from warehouse where warehouse_code in (select warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ";
if($PWarehouse_Code != '') $querywarehouse=$querywarehouse." and warehouse.warehouse_code like ".GetSQLValueString($PWarehouse_Code.'%','text')." ";
if($PWarehouse_Name != '') $querywarehouse=$querywarehouse." and warehouse.warehouse_name like ".GetSQLValueString('%'.$PWarehouse_Name.'%','text')." ";
if($Login_location == 1)  if($warehouse_type != '') $querywarehouse=$querywarehouse." and warehouse.warehouse_type = upper('$warehouse_type') ";				 
	$querywarehouse=$querywarehouse."  order by warehouse.warehouse_code";

$resultwarehouse = mysql_query($querywarehouse,$connection) or die(mysql_error());
$rows   = mysql_num_rows($resultwarehouse);
if ($rows == 0){
    $querywarehouse=" select distinct warehouse_code, warehouse_name from warehouse ".
					" where 1=1 ";
	if($PWarehouse_Code != '') $querywarehouse=$querywarehouse." and warehouse.warehouse_code like ".GetSQLValueString($PWarehouse_Code.'%','text')." ";
	if($PWarehouse_Name != '') $querywarehouse=$querywarehouse." and warehouse.warehouse_name like ".GetSQLValueString('%'.$PWarehouse_Name.'%','text')." ";
	if($Login_location == 1) if($warehouse_type != '') $querywarehouse=$querywarehouse." and warehouse.warehouse_type = upper('$warehouse_type') ";
	/* if($parentWh != '')
	$querywarehouse=$querywarehouse." and warehouse_parentId='$parentWh' "; */
$querywarehouse=$querywarehouse." order by warehouse_code ";
 }
	//echo $querywarehouse;	
$resultwarehouse=mysql_query($querywarehouse,$connection) or die(mysql_error());		
while($rows=mysql_fetch_array($resultwarehouse)){
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_SorWarehouse.php?lcode=<?php echo $warehouse_code; ?>&frm=<?php echo $frm; ?>&item=<?php echo $item; ?>&index=<?php echo $index; ?><?php if($warehouse_type != '') { ?>&warehouse_type=<?php echo $warehouse_type; }?>"><?php echo $warehouse_code; ?></a></span></td>
      <td ><span class="style3"><a href="List_SorWarehouse.php?lcode=<?php echo $warehouse_code; ?>&frm=<?php echo $frm; ?>&item=<?php echo $item; ?>&index=<?php echo $index; ?><?php if($warehouse_type != '') { ?>&warehouse_type=<?php echo $warehouse_type; }?>"><?php echo $warehouse_name; ?></a></span></td>
    </tr>
<?php
}
?>
  </table>
</form>
</body>
</html>
