<?php
header("Content-type: text/html; charset=windows-1256");
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");

$vZone = $_GET["vZone"]; if ($vZone == ""){$vZone = $_POST["vZone"];}   
$vType = $_GET["vType"]; if ($vType == ""){$vType = $_POST["vType"];}   
$subgroup_code = $_GET["subgroup_code"]; if ($subgroup_code == ""){$subgroup_code = $_POST["subgroup_code"];}   
$subgroup_codeParent = $_POST["subgroup_codeParent"]; if ($subgroup_codeParent == ""){$subgroup_codeParent = $_GET["subgroup_codeParent"];}   

//Read From CashRegister Table
$queryCashRegister=" Select ifNull(Module_CashRegisterFastItem,0) As Module_CashRegisterFastItem ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
				 //echo$queryCashRegister;
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

if($vZone=='subgrouparea'){

	//Type indicates which button is pressed / Main:main subgroup   Root:root button   Back:previous level of subgroups
	if($vType=='root'){
		$query="select subgroup_code,parent_code, subgroup_color, subgroup_picture,button_dimwidth,button_dimheight from subgroup where upper(subgroup_type)=upper('PLU') and (parent_code is null or parent_code='') order by subgroup_code";
		$vSkipActionButtons=1;
		$SelSubGroup = '';
	}
	else if($vType=='main')	{
		$query="select subgroup_code,parent_code, subgroup_color, subgroup_picture,button_dimwidth,button_dimheight from subgroup where parent_code='".$subgroup_code."'  order by subgroup_order";
		$SelSubGroup = $subgroup_code;
	}	
	
	else if($vType=='back')	{
		$query="Select parent_code, subgroup_code, subgroup_color, subgroup_picture,button_dimwidth,button_dimheight from subgroup where subgroup_code='".$subgroup_code."'  order by subgroup_order";
		$result=mysql_query($query,$connection);
		$row=mysql_fetch_row($result);
		$parent_code=$row[0];
		
		if($parent_code==''){
			$query="select subgroup_code,parent_code, subgroup_color, subgroup_picture,button_dimwidth,button_dimheight from subgroup where upper(subgroup_type)=upper('PLU') and (parent_code is null or parent_code='')  order by subgroup_order";
			//if back button leads to the first level of subgroup,the back and root are not showen
			$vSkipActionButtons=1;
		}
		else	
		$query="select subgroup_code,parent_code, subgroup_color, subgroup_picture,button_dimwidth,button_dimheight from subgroup where parent_code='".$parent_code."'  order by subgroup_order";
		
		$SelSubGroup = $parent_code;
	}
	
	$result=mysql_query($query,$connection);
	$numrow=mysql_num_rows($result);
	
	
	echo"<table border='0' style='table-layout:fixed;' cellspacing=0 cellpadding=0>";
	echo"<tr>";	
	$i=0;
	
	if($numrow>0){
		while($row=mysql_fetch_row($result)){
			
			if($i==0){
				
				if($vType=='back'){
					$subgroup_codeParent=$row[1];
				}	
				else
				 $subgroup_codeParent=$subgroup_code;
				echo"<input type='hidden' name='subgroup_codeParent' value='$subgroup_codeParent'>";
			}
			
			$subquery="select subgroup_code from subgroup where parent_code='".$row[0]."'";
			$resultsub=mysql_query($subquery,$connection);
			$numrow=mysql_num_rows($resultsub);
			
			$vZone='subgrouparea';
			if ($Module_CashRegisterFastItem == 1) {
				if($i%8==0) echo"</tr><tr>";
			} else {	
				if($i%8==0) echo"</tr><tr>";
			}
			
			$ItemButton_Color   = $row[2];
			$ItemButton_Picture = $row[3];
			
			$ButtonColor = '';
			if ($ItemButton_Picture != '') {
				$ButtonLabel = '';
			}
			if ($ItemButton_Color != '') {
				$ButtonColor = "background-color:#".$ItemButton_Color;
			}
			
			if($row[4] != '' && $row[4] != 0) $dimw = $row[4]."px";
			else $dimw = "70px";
			if($row[5] != '' && $row[5] != 0) $dimh = $row[5]."px";
			else $dimh = "55px";
			
			if ($ItemButton_Picture != '') {
				echo "<td width='60px'><button type='button' id='btnSub' name='btnSub' onclick=\"showSubgroup('$row[0]','$vZone','main');return false;\" class='subgroupbuttons_1' style='$ButtonColor' value='$row[0]'><img src='GetButtonImage.php?option=subgroup&id=$row[0]' style='width:50px; height:40px;'></img></button></td>";
			} else {			
				echo "<td width='60px'><input type='button' id='btnSub' name='btnSub' onclick=\"showSubgroup('$row[0]','$vZone','main');return false;\" class='subgroupbuttons' style='$ButtonColor;width:$dimw; height:$dimh;' value='$row[0]' ><span><span></span></span></button></td>";
			}
			
			$i=$i+1;
		}
		//echo $subgroup_code;
		if($SelSubGroup != ''){
		$subquery="select item_code,substr(item_description,1,20), Item_Color, Item_Picture, ifnull(item_isNotAvailable,0) from items where subgroup_code='".$SelSubGroup."'";
		
		$subresult=mysql_query($subquery,$connection);
		$subnumrow=mysql_num_rows($subresult);
		
		echo"</tr><tr>";
		if($vSkipActionButtons!='1'){
			echo"<td><input type='button' id='BackButton'  style='width:$dimw; height:$dimh;' onclick=\"showSubgroup('$subgroup_codeParent','subgrouparea','back');return false;\" class='subgroupbuttons1' value='Back'><span><span></span></span></button></td>
				 <td><input type='button' id='RootButton'  style='width:$dimw; height:$dimh;' onclick=\"showSubgroup('$subgroup_codeParent','subgrouparea','root');return false;\" class='subgroupbuttons1' value='Root'><span><span></span></span></button></td>"; 
		}		 
		
		$i=2;
		while($rowsub=mysql_fetch_row($subresult)){
			//if($i%10==0) echo"</tr><tr>";
			$disabledButton = '';
			$addX = '';
				
			if ($Module_CashRegisterFastItem == 1) {
				if($i%8==0) echo"</tr><tr>";
			} else {	
				if($i%8==0) echo"</tr><tr>";
			}
			$querypar="select button_dimwidth,button_dimheight from subgroup where subgroup_code='".$subgroup_codeParent."'  order by subgroup_order";
			$respar = mysql_query($querypar,$connection);
			$rwpar = mysql_fetch_row($respar);
			
			if($rwpar[0] != '' && $rwpar[0] != 0) $dimw = $rwpar[0]."px";
			else $dimw = "70px";
			if($rwpar[1] != '' && $rwpar[1] != 0) $dimh = $rwpar[1]."px";
			else $dimh = "55px";
			
			$ItemButton_Color   = $rowsub[2];
			$ItemButton_Picture = $rowsub[3];
			
			$ButtonColor = '';
			if ($ItemButton_Picture != '') {
				$ButtonLabel = '';
			}
			if ($ItemButton_Color != '') {
				$ButtonColor = "background-color:#".$ItemButton_Color;
			}	
			
			if($rowsub[4] == 1){
				$disabledButton = "disabled";
				$addX = "<div style='position: absolute;font-size: 80px;color: black;opacity: 0.5;text-align: center;display: inline-block;width: 95px;height: 80;'>X</div>";
			}
			
			if ($ItemButton_Picture != '') {
				echo "<td width='60px'><div style='position:relative;'>$addX<button type='button' id='btnSub' name='btnSub' onclick=\"Fillitem('$rowsub[0]','$SelSubGroup');return false;\" class='itembuttons' style='$ButtonColor' value='$rowsub[1]' $disabledButton><img src='GetButtonImage.php?option=item&id=$rowsub[0]' style='width:40px; height:40px;'></img></button></div></td>";
			} else {
				echo "<td width='60px'><div style='position:relative;'>$addX<input type='button' id='btnSub' name='btnSub' onclick=\"Fillitem('$rowsub[0]','$SelSubGroup');return false;\" class='itembuttons' style='$ButtonColor;width:$dimw; height:$dimh;' value='$rowsub[1]' $disabledButton><span><span></span></span></button></div></td>";
			}
			
			$i=$i+1;
		}
		//echo"</tr>";
	}
	}
	else{
		$vZone='subgrouparea';
		$subgroup_codeParent=$subgroup_code;
				
		echo"<input type='hidden' name='subgroup_codeParent' value='$subgroup_codeParent'>";
		$subquery="select item_code,substr(item_description,1,20), Item_Color, Item_Picture, ifnull(item_isNotAvailable,0) from items where subgroup_code='".$subgroup_code."'";
		
		$subresult=mysql_query($subquery,$connection);
		$subnumrow=mysql_num_rows($subresult);
		
		//echo"<table border='0' style='table-layout:fixed;' cellspacing=0 cellpadding=0>";
		//echo"<tr>";	
		$querypar="select button_dimwidth,button_dimheight from subgroup where subgroup_code='".$subgroup_codeParent."'  order by subgroup_order";
		$respar = mysql_query($querypar,$connection);
		$rwpar = mysql_fetch_row($respar);
		
		if($rwpar[0] != '' && $rwpar[0] != 0) $dimw = $rwpar[0]."px";
		else $dimw = "70px";
		if($rwpar[1] != '' && $rwpar[1] != 0) $dimh = $rwpar[1]."px";
		else $dimh = "55px";
		
		echo"</tr><tr>";
			//if ($subnumrow > 0) {
				//if($vSkipActionButtons!='1'){
					echo"<td><input type='button' id='BackButton' style='width:$dimw; height:$dimh;' onclick=\"showSubgroup('$subgroup_codeParent','subgrouparea','back');return false;\" class='subgroupbuttons1' value='Back'><span><span></span></span></button></td>
						 <td><input type='button' id='RootButton' style='width:$dimw; height:$dimh;' onclick=\"showSubgroup('$subgroup_codeParent','subgrouparea','root');return false;\" class='subgroupbuttons1' value='Root'><span><span></span></span></button></td>"; 
				//}
			//}	
				
			
			$i=2;
			while($rowsub=mysql_fetch_row($subresult)){
				//if($i%10==0) echo"</tr><tr>";
				$disabledButton = '';
				$addX = '';
				
				if ($Module_CashRegisterFastItem == 1) {
					if($i%8==0) echo"</tr><tr>";
				} else {	
					if($i%8==0) echo"</tr><tr>";
				}
				
				$ItemButton_Color   = $rowsub[2];
				$ItemButton_Picture = $rowsub[3];
				
				$ButtonColor = '';
				if ($ItemButton_Picture != '') {
					$ButtonLabel = '';
				}
				if ($ItemButton_Color != '') {
					$ButtonColor = "background-color:#".$ItemButton_Color;
				}	
				
				$querypar="select button_dimwidth,button_dimheight from subgroup where subgroup_code='".$subgroup_codeParent."'  order by subgroup_order";
				$respar = mysql_query($querypar,$connection);
				$rwpar = mysql_fetch_row($respar);
				
				if($rwpar[0] != '' && $rwpar[0] != 0) $dimw = $rwpar[0]."px";
				else $dimw = "70px";
				if($rwpar[1] != '' && $rwpar[1] != 0) $dimh = $rwpar[1]."px";
				else $dimh = "55px";
				
				if($rowsub[4] == 1){
					$disabledButton = "disabled";
					$addX = "<div style='position: absolute;font-size: 80px;color: black;opacity: 0.5;text-align: center;display: inline-block;width: 95px;height: 80;'>X</div>";
				}
				if ($ItemButton_Picture != '') {
					echo "<td width='60px'><div style='position:relative;'>$addX<button type='button' id='btnSub' name='btnSub' onclick=\"Fillitem('$rowsub[0]','$SelSubGroup');return false;\" class='itembuttons' style='$ButtonColor' value='$rowsub[1]' $disabledButton><img src='GetButtonImage.php?option=item&id=$rowsub[0]' style='width:40px; height:40px;'></img></button></div></td>";
				} else {
					echo "<td width='60px'><div style='position:relative;'>$addX<input type='button' id='btnSub' name='btnSub' onclick=\"Fillitem('$rowsub[0]','$subgroup_code');return false;\" class='itembuttons' style='$ButtonColor;width:$dimw; height:$dimh;' value='$rowsub[1]' $disabledButton><span><span></span></span></button></div></td>";
				}
				
				$i=$i+1;
			}
			//echo"</tr>";
	}
	
	/*if($vSkipActionButtons!='1'){
		echo"<td><input type='button' id='BackButton' onclick=\"showSubgroup('$subgroup_codeParent','subgrouparea','back');return false;\" class='subgroupbuttons1' value='Back'><span><span></span></span></button></td>
			 <td><input type='button' id='RootButton' onclick=\"showSubgroup('$subgroup_codeParent','subgrouparea','root');return false;\" class='subgroupbuttons1' value='Root'><span><span></span></span></button></td>"; 
	}*/
	echo"</tr>";
	echo"</table>";		
}



?>