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

$perpage   			= $_GET['perpage']; 			if ($perpage == "")			 {	$perpage 			= $_POST["perpage"];			}
$startrange 		= $_GET['startrange']; 			if ($startrange == "")		 {	$startrange 		= $_POST["startrange"];			}

$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 * from items where item_code='".$_GET['lcode']."'";
	$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>Select Items</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.form1.PItem_Description.value='';
  document.form1.PItem_Code.value='';

}
function SetNextPage(startrange,perpage){
	vFrm    = document.form1.item1.value;
	vitemtype = document.form1.itemtype.value;
	vindex = document.form1.index.value;
	
	document.form1.action='select_item.php?PItem_Code=&lcode=&item='+vFrm+'&index='+vindex+'&itemtype='+vitemtype+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}


function SetPrvPage(startrange,perpage){
	vFrm       = document.form1.item1.value;
	vitemtype = document.form1.itemtype.value;
	vindex = document.form1.index.value;
	
	document.form1.action='select_item.php?PItem_Code=&lcode=&item='+vFrm+'&index='+vindex+'&itemtype='+vitemtype+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}


function RefreshPage(){
	vFrm       = document.form1.item1.value;
	vitemtype = document.form1.itemtype.value;
	vPerPage   = document.form1.perpage.value;
	vindex = document.form1.index.value;
	
	document.form1.action='select_item.php?PItem_Code=&lcode=&item='+vFrm+'&index='+vindex+'&itemtype='+vitemtype+'&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;
}
.style3 {color: #999999}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="850" border="0" align="center">
    <tr>
      <th colspan="5" bgcolor="#DDDDDD"><span class="style1"> ITEMS </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD" width="136"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Item  <br> Code / Desc.</span></td>
      <td bgcolor="#DDDDDD" width="100"><input type="text" name="PItem_Code" value="<?php echo $PItem_Code; ?>"  />
	  <input name="itemtype" type="hidden" id="itemtype"  value="<?php echo $itemtype; ?>">
        <input name="code1" type="hidden" id="code1"  value="<?php echo $Item_Code; ?>">
		<input name="Item_UnitSalesDesc" type="hidden" id="Item_UnitSalesDesc"  value="<?php echo $Item_UnitSalesDesc; ?>"></td>
      <td bgcolor="#DDDDDD" width="450"><input type="text" name="PItem_Description"  value="<?php echo $PItem_Description; ?>" size='35'/>
	    <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;'/>
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $Item_Description; ?>" /></td>
      <td bgcolor="#DDDDDD" width="64"><input name="item1" type="hidden" id="item1" value="<?php echo $_GET['item']; ?>" />
										<input name="index" type="hidden" id="index" value="<?php echo $_GET['index']; ?>" />
	  </td>
    </tr>
<?php
	echo "<tr bgcolor='#DDDDDD'><td></td>";
	echo "<td>	<span style='font-size: 11px; color: #999999; font-weight: bold;'>Result 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></td><td></td></tr>";
?>
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">
x=document.form1;
item1=x.item1.value; 
index=x.index.value;
if(item1=="1"){
	opener.document.form1.fromitem.value=document.form1.code1.value; 
	opener.document.form1.fromitemdesc.value=document.form1.desc1.value;
	opener.document.form1.toitem.value=document.form1.code1.value;
	opener.document.form1.toitemdesc.value=document.form1.desc1.value;
	self.close();
}else if(item1=="2"){
	opener.document.form1.toitem.value=document.form1.code1.value;
	opener.document.form1.toitemdesc.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="3"){
	opener.document.form1.fromitemRM.value=document.form1.code1.value;
	opener.document.form1.fromitemdescRM.value=document.form1.desc1.value;
	opener.document.form1.toitemRM.value=document.form1.code1.value;
	opener.document.form1.toitemdescRM.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="4"){
	opener.document.form1.toitemRM.value=document.form1.code1.value;
	opener.document.form1.toitemdescRM.value=document.form1.desc1.value;
	self.close();
}else if(item1=="5"){
	opener.document.itemtrans.fromitem.value=document.form1.code1.value;
	opener.document.itemtrans.fromitemdesc.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="6"){
	opener.document.Setup.fromitem.value=document.form1.code1.value;
	opener.document.Setup.fromitemdesc.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="7"){
	opener.document.Setup.toitem.value=document.form1.code1.value;
	opener.document.Setup.toitemdesc.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="8"){
	opener.document.clientdt.item_code.value=document.form1.code1.value;
	opener.document.clientdt.item_description.value=document.form1.desc1.value;
	self.close();
}else if(item1=="9"){
	opener.document.search.itemcode.value=document.form1.code1.value;
	self.close();
}else if(item1=="10"){
	opener.document.rmcalculate.itemcode.value=document.form1.code1.value;
	opener.document.rmcalculate.itemdesc.value=document.form1.desc1.value;
	opener.document.rmcalculate.fgqty.value=1;
	self.close();
} else if(item1=="11"){
	opener.document.items.itemcopy.value=document.form1.code1.value;
	opener.document.items.itemcopydesc.value=document.form1.desc1.value;
	self.close();
} else if(item1=="12"){
	opener.document.itemauction.item_code.value=document.form1.code1.value;
	opener.document.itemauction.item_desc.value=document.form1.desc1.value;
	opener.document.itemauction.txtitemunit.value=document.form1.Item_UnitSalesDesc.value;
	self.close();
} else if(item1=="13"){
	opener.document.supplierdt.item_code.value=document.form1.code1.value;
	opener.document.supplierdt.item_desc.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="14"){
	opener.document.qtybudget.fromitem.value=document.form1.code1.value;
	opener.document.qtybudget.fromitemdesc.value=document.form1.desc1.value;
	if (opener.document.qtybudget.toitem.value == ''){
		opener.document.qtybudget.toitem.value=document.form1.code1.value;
		opener.document.qtybudget.toitemdesc.value=document.form1.desc1.value;
	}
	self.close();
}
else if(item1=="15"){
	opener.document.qtybudget.toitem.value=document.form1.code1.value;
	opener.document.qtybudget.toitemdesc.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="16"){
	opener.document.getElementById('itemcode['+index+']').value=document.form1.code1.value;
	self.close();
}
else if(item1=="17"){
	opener.document.transForm.fromitem.value=document.form1.code1.value;
	opener.document.transForm.fromitemdesc.value=document.form1.desc1.value;
	
	if (opener.document.transForm.toitem.value == ''){
		opener.document.transForm.toitem.value=document.form1.code1.value;
		opener.document.transForm.toitemdesc.value=document.form1.desc1.value;
	}
	self.close();
}
else if(item1=="18"){
	opener.document.transForm.toitem.value=document.form1.code1.value;
	opener.document.transForm.toitemdesc.value=document.form1.desc1.value;
	self.close();
}
else if(item1=="19"){
	opener.document.getElementById('pItem_code['+index+']').value=document.form1.code1.value;
	opener.document.getElementById('pItem_desc['+index+']').value=document.form1.desc1.value;
	self.close();
}
else if(item1=="20"){
	opener.document.getElementById('item_code').value=document.form1.code1.value;
	opener.document.getElementById('item_description').value=document.form1.desc1.value;
	self.close();
}
else if(item1=="21"){
	opener.document.getElementById('item_code').value=document.form1.code1.value;
	self.close();
}
</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td><span class="style2">Item Code </span></td>
      <td colspan="2"><span class="style2">Item Description </span></td>
      <td><span class="style2"> Group Code</span></td>
    </tr>
<?php
$query1=" select Item_Code, Item_Description, Group_code, Item_UnitSalesDesc as unitdesc from items ".
        " where Item_Code like '%".$PItem_Code."%' ".
		" and Item_Description like '%".$PItem_Description."%' ".
		" and (('".$itemtype."' = 'productionFG' and ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 1 or ifNull(Items.Item_SemiFinished,0) = 1 or ifNull(Items.Item_ReverseProduction,0) = 1) ) or ".
		"	   ('".$itemtype."' = 'productionRM' and ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0) ) or ".
		"      ('".$itemtype."' = ''  and 1 = 1))  ";
		
$query2=" select Item_Code, Item_Description, Group_code, Item_UnitSalesDesc as unitdesc from items ".
        " where Item_Code like '%".$PItem_Code."%' ".
		" and Item_Description like '%".$PItem_Description."%'  ".
        " and items.group_code In (select group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ".
		" and (('".$itemtype."' = 'productionFG' and ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 1 or ifNull(Items.Item_SemiFinished,0) = 1 or ifNull(Items.Item_ReverseProduction,0) = 1) ) or ".
		"	   ('".$itemtype."' = 'productionRM' and ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0) ) or ".
		"      ('".$itemtype."' = ''  and 1 = 1))  ";
 if ($item == '5') {
	$query1 = $query1." and item_code in (select item_code from packdt)"; 
	$query2 = $query2." and item_code in (select item_code from packdt) "; 
} 
$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
			" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist   = mysql_num_rows($resultgroup); 
//echo $query1;
//echo $query2;


if ($groupexist == 0)
  $result1=mysql_query($query1,$connection) or die(mysql_error());
else
  $result1=mysql_query($query2,$connection) or die(mysql_error());

$totalrows = mysql_num_rows($result1);
	
if ($startrange=='') $startrange = 0;
if ($perpage=='')	 $perpage = 20;
	
$query1 = $query1." limit ".$startrange.",".$perpage; 
$query2 = $query2." limit ".$startrange.",".$perpage; 


 
 

if ($groupexist == 0)
  $result1=mysql_query($query1,$connection) or die(mysql_error());
else
  $result1=mysql_query($query2,$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"><a href="select_item.php?lcode=<?php echo $Item_Code; ?>&item=<?php echo $_GET['item']; ?>&index=<?php echo $_GET['index']; ?>"><?php echo $Item_Code; ?></a></td>
      <td align="left" colspan="2"><a href="select_item.php?lcode=<?php echo $Item_Code; ?>&item=<?php echo $_GET['item']; ?>&index=<?php echo $_GET['index']; ?>"><?php echo $Item_Description; ?></a></td>
      <td align="left"><?php echo $Group_code; ?>&nbsp;</td>
    </tr>
<?php
}

echo"<tr><td colspan='6'>&nbsp;</td></tr>
		 <tr><td colspan='6'><table width='100%' border=0><tr><td width='270'>&nbsp;</td><td width='110' 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='110' 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>";
?>
  </table>
</form>
</body>
</html>
