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


$query1="select grid_color1,grid_color2, ifnull(subgroup_autoIncrement,0) as subgroupautoIncrement  from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}

$str   	= $_GET['str']; 	if ($str == ""){	$str 	= $_POST["str"];}
$mode  	= $_GET['mode'];	if ($mode == ""){	$mode	= $_POST["mode"];}
$post  	= $_GET['post'];	if ($post == ""){	$post 	= $_POST["post"];}
$index	= $_GET['index']; 	if ($index == "") 	$index 	= $_POST['index'];
$groupcode	= $_GET['groupcode']; 	if ($groupcode == "") 	$groupcode 	= $_POST['groupcode'];

$PRef  = $_POST["PRef"]; if ($PRef == ""){$PRef = $_GET["PRef"];}

$subgroup_code1  = $_POST["subgroup_code1"]; if ($subgroup_code1 == ""){$subgroup_code1 = $_GET["subgroup_code1"];}
$subgroup_name1  = $_POST["subgroup_name1"]; if ($subgroup_name1 == ""){$subgroup_name1 = $_GET["subgroup_name1"];}


if($mode=='insert'){
	$query = "insert into SubGroup(SubGroup_Code,SubGroup_Name,Subgroup_CreationDate) values('".$subgroup_code1."','".$subgroup_name1."',now())";
	$result=mysql_query($query,$connection);
	$message  = mysql_error($connection); 
	$mode='';
	$PRef = $subgroup_code1;
	$subgroup_code1 = '';
	$subgroup_name1 = '';  
}





?>

<!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>List of SubGroups</title>
<script type="text/javascript" language="javascript">

function submitrecord(){
  document.form1.mode.value='insert';
  document.form1.action='List_Subgroup.php';
  document.form1.submit();
}


function clearform()
{
  document.form1.PRef.value='';
  document.form1.action='List_Subgroup.php';
  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="800" border="0" align="center">
    <tr>
      <th colspan="2" bgcolor="#DDDDDD"><span class="style1"> SubGroups</span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for SubGroup <br> Code / Name</span></td>
      <td bgcolor="#DDDDDD"><input type="text" name="PRef" value="<?php echo $PRef; ?>"  />
        <input name="code1" type="hidden" id="code1"  value="<?php echo $_GET['lcode']; ?>">
	   <input name="str" type="hidden"   value="<?php echo $str; ?>">
	
		<input name="mode" type="hidden"   value="<?php echo $mode; ?>">
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $_GET['desc1']; ?>" />
		<input name="index" type="hidden" value="<?php echo $index; ?>">
		<input name="groupcode" type="hidden" value="<?php echo $groupcode; ?>">
		<input name="subgroupautoIncrement" type="hidden" value="<?php echo $subgroupautoIncrement; ?>">
	    <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>
   
    </tr>
<?php if($post=="1") { ?>
	
		
<script type="text/javascript" language="javascript">
var vIndex	   = document.form1.index.value; 

if(document.form1.str.value=='1'){
	opener.document.items.subgroup_code.value=document.form1.code1.value;
	opener.document.items.subgroup_name.value=document.form1.desc1.value;
	if (document.form1.subgroupautoIncrement.value == 1) opener.document.items.subgroup_code.onchange();
}

else if(document.form1.str.value=='2'){
	opener.document.useraccess.subgroup_code.value=document.form1.code1.value;
	opener.document.useraccess.subgroup_name.value=document.form1.desc1.value;
}
else if(document.form1.str.value=='3'){
	opener.document.form1.p_subgroup.value=document.form1.code1.value;
	opener.document.form1.subgroup_name.value=document.form1.desc1.value;
}
else if(document.form1.str.value=='4'){
	opener.document.clientdt.subgroup_code.value=document.form1.code1.value;
	opener.document.clientdt.subgroup_name.value=document.form1.desc1.value;
}
else if(document.form1.str.value=='5'){
	if (opener.document.form1.fromsubgroup)
		opener.document.form1.fromsubgroup.value=document.form1.code1.value;
		
	if (opener.document.form1.fromsubgroup_code)
		opener.document.form1.fromsubgroup_code.value=document.form1.code1.value;

	opener.document.form1.fromsubgroup_name.value=document.form1.desc1.value;
}
else if(document.form1.str.value=='6'){
	if (opener.document.form1.tosubgroup)
		opener.document.form1.tosubgroup.value=document.form1.code1.value;
	
	if (opener.document.form1.tosubgroup_code)
	opener.document.form1.tosubgroup_code.value=document.form1.code1.value;

	opener.document.form1.tosubgroup_name.value=document.form1.desc1.value;
}
else if(document.form1.str.value=='7'){
	opener.document.getElementById("subgroup_code["+vIndex+"]").value=document.form1.code1.value;
}
else if(document.form1.str.value=='8'){
	opener.document.production.subgroup_code.value=document.form1.code1.value;
}
else if(document.form1.str.value=='9'){
	opener.document.getElementById('fromsubgroup').value=document.form1.code1.value;
	opener.document.getElementById('fromsubgroup_name').value=document.form1.desc1.value;
	
	if (opener.document.getElementById('tosubgroup').value == ''){	
		opener.document.getElementById('tosubgroup').value=document.form1.code1.value;
		opener.document.getElementById('tosubgroup_name').value=document.form1.desc1.value;
	}
}
else if(document.form1.str.value=='10'){
	opener.document.getElementById('tosubgroup').value=document.form1.code1.value;
	opener.document.getElementById('tosubgroup_name').value=document.form1.desc1.value;
}
else if(document.form1.str.value=='11'){
	opener.document.getElementById('subgroup_code').value=document.form1.code1.value;
}
else if(document.form1.str.value=='12'){
	opener.document.getElementById('category_subgroup').value=document.form1.code1.value;
	opener.document.getElementById('subGroup_Name').value=document.form1.desc1.value;
}
else if(document.form1.str.value=='13'){
	opener.document.form1.fromsubgroup.value=document.form1.code1.value;
	opener.document.form1.fromsubgroup_name.value=document.form1.desc1.value;
	opener.document.form1.tosubgroup.value=document.form1.code1.value;
	opener.document.form1.tosubgroup_name.value=document.form1.desc1.value;
}
else if(document.form1.str.value=='14'){
	opener.document.form1.tosubgroup.value=document.form1.code1.value;
	opener.document.form1.tosubgroup_name.value=document.form1.desc1.value;
}
self.close();

</script>

<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">SubGroup Code </span></td>
      <td width="411" colspan="1"><span class="style2">SubGroup Name </span></td>
  
    </tr>
	
	  <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2"><input type='text' name='subgroup_code1' id='subgroup_code1' style='width:100px' value='<?php echo $subgroup_code1; ?>'></span></td>
      <td width="411" colspan="1"><span class="style2"><input type='text' name='subgroup_name1' id='subgroup_name1' value='<?php echo $subgroup_name1; ?>'></span>
	  <input type='button' name='add' value='Save New SubGroup' onclick='submitrecord()'><?php echo "<font color=red size=2>".$message."</font>"; ?>
	  </td>
  
    </tr>
	
	
<?php
$query1=" select SubGroup_Code,SubGroup_name ".
		" from subgroup
		where (SubGroup_Code like"."('%".$PRef."%') or SubGroup_name like"."('%".$PRef."%'))
						and if('$groupcode' = '', 1=1, Group_Code = '$groupcode')
		 order by SubGroup_Code";
	

		
//echo$query1;
$result1=mysql_query($query1,$connection) or die(mysql_error());
$numrow = mysql_num_rows($result1);
if($numrow == 0){
	$query1=" select SubGroup_Code,SubGroup_name ".
		" from subgroup
		where SubGroup_Code like"."('%".$PRef."%') or SubGroup_name like"."('%".$PRef."%')
		order by SubGroup_Code";

	$result1=mysql_query($query1,$connection) or die(mysql_error());
	
}
while($rows=mysql_fetch_array($result1)){


$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; 
	
	
	?>>
	
	 <?php
		$rows[1]=ereg_replace("'","%27",$rows[1]);
			$subgroupcode = $rows[0];
			$desc = $rows[1];
			$rows[0] = rawurlencode($rows[0]);
			$rows[1] = rawurlencode($rows[1]);
		?>
		
		
      <td align="left"><a href="List_Subgroup.php?str=<?php echo $str; ?>&post=1&lcode=<?php echo $rows[0]; ?>&desc1=<?php echo $rows[1]; ?>&index=<?php echo $index; ?>"><?php echo $subgroupcode; ?></a></td>
      <td colspan="1"><a href="List_Subgroup.php?str=<?php echo $str; ?>&post=1&lcode=<?php echo $rows[0]; ?>&desc1=<?php echo $rows[1]; ?>&index=<?php echo $index; ?>"><?php echo $desc ; ?></a></td>

    </tr>
	
<?php }?>
  </table>
</form>
</body>
</html>
