<?
session_start();
include("../opendb.php");
include("../test.php");
if($save)
{
$code=substr($subname,0,3).substr(time(),7,3).date("s"); 
 $code=str_replace(' ','-',$code);

$sql="insert into subgroupmaster values('$code','$gname','$subname','$tax')";
mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error());

print "<script> document.location='subgrmaster.php'</script>";
return;
}

?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:steelblue;color:white}
</style>
<script>
function valid()
{
 
 if(document.f1.subname.value.length<=0)
 {
   alert("Invalid sub group name.");
   document.f1.subname.focus();
   return false;
 }

}

var stax=[];

function settax()
{
  var x= document.f1.gname.selectedIndex ; 
  if(x>0)
	
    document.f1.tax.value=stax[document.f1.gname.selectedIndex];
   else
    document.f1.tax.value='';
}
</script>

<center>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='white'>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">
<table cellspacing=1 cellpadding=3 width='350'>
<th colspan=2><font size=3>SUB GROUP MASTER</th>
<tr><td>Group </td><td><select name='gname' size=1 onChange='settax()'>
<option></option>
<?
$gp=mysqli_query($db,"select * from groupmaster");
$i=1;
while($gprow=mysqli_fetch_array($gp))
{
print "<option>$gprow[gname]</option>";
print "<script>stax[$i]='$gprow[tax]'</script>";
$i++;
}
?>
</select>
</td>
    </tr>
<tr><td>Sub Group Name</td><td><input type=text name='subname' size=25></td></tr>
<tr><td>Tax</td><td><input type=text name='tax'  size=7>
</td></tr>
</td>
    </tr>

<th colspan=2><input type=submit value='Save' name='save'></td></tr>
</table>
</form>
</td></tr>
</table>
<br>
<table cellspacing=1 cellpadding=5 bgcolor='orange'>
<th>No <th>Group <th>Sub Group Name<th>Tax %<th>Edit<th>Del
<?
$i=1;
$res=mysqli_query($db,"select * from subgroupmaster order by subname");
while($row=mysqli_fetch_array($res))
{
 print "<tr bgcolor='white' onMouseOver=bgColor='b8d7b4'; onMouseOut=bgColor='white'>";
 print "<td>".$i++."</td><td>$row[gname]</td><td>$row[subname]</td>";
 print "<td align=right>$row[tax]</td><td><input type=button value='Edit' onClick=window.open('editsubgr.php?code=$row[code]',menubar=false,resizable=false)></td>";
 print "<td><input type=button value='Del' name='del' onClick=delfun('$row[code]')></td></tr>";
}
?>
</table>

<script>
 function delfun(x)
 {
  if (confirm('do you want to delete '+x+' ?'))
   document.location='delsubgrmaster.php?code='+x;
 }
 
   
</script>
