<script>
function validate()
{
	var groupName = document.getElementById("group_name").value;
	if(groupName == "")
	{
	alert("Enter Activity Group");
	document.getElementById("group_name").focus();
	return false;
	}
	

	if ( ( document.getElementById("statusA").checked == false ) && ( document.getElementById("statusI").checked== false ) )
	{
	alert("Select Status");
	return false;
	}
	
	//return true;
}
</script>

<?php
if(isset($_POST['save']))
{       if(isset($_REQUEST['sub_activity_group']['group_name']))
		{
			$group_name = $_REQUEST['sub_activity_group']['group_name'];
			$group_code = $_REQUEST['sub_activity_group']['group_code'];
			$display_order = $_REQUEST['sub_activity_group']['display_order'];
			$status = $_REQUEST['sub_activity_group']['status'];
			
			
			/*************************   Auto Generate Code     ****************************/
			
			      $prefix=strtoupper(substr($_REQUEST['sub_activity_group']['group_name'],0,2));
				
					 $sql = "SELECT group_code FROM sub_activity_group ORDER BY id DESC LIMIT 0,1";
					  
					  $result = DB_query($sql,$db);
					  $numrows = DB_num_rows($result);
					 if($numrows>0)
					 {
							 $i=0;
							 $strmax = "";
							 $myrow=DB_Fetch_array($result);
							 
						 
								
							
							if($numrows>0)
							{ 
							 $data=explode("-",$myrow['group_code']); 
							 $data1=$data[1]+1;
							 $postfix=str_pad($data1,3,"0",STR_PAD_LEFT);
							 $strChar = $prefix."-".$postfix;
							}
					  }
					  else
					  {
						$strChar = $prefix."-001";
					  }
					  
					$_REQUEST['sub_activity_group']['group_name']=$strChar;
					$group_code = $_REQUEST['sub_activity_group']['group_name'];
				   
			
			}
			
			$query1 = "INSERT INTO sub_activity_group values('','".$group_name."','".$group_code."','".$display_order."','".$status."')";
			$res = DB_query($query1,$db) or die(DB_error());		unset($_REQUEST['sub_activity_group']);	
					//print_r($_REQUEST); exit;
			$_SESSION['success_msg']="Saved Successfully";	
			echo "<script>location.href='index.php?route=SubContractors/Masters/activity_group_defination_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
			
			
			
			/*****************************************************************************/
			
	/*		
			
			$prefix=strtoupper(substr($_REQUEST['sub_activity_group']['group_name'],0,2));
			$query = "SELECT group_code FROM sub_activity_group ORDER BY group_code DESC LIMIT 0,1";
			$res = DB_query($query,$db) or die(DB_error());
			$result = DB_fetch_array($res,$db);
			$post = $result['group_code'];
			if(DB_num_rows($res,$db)>0)
			{
				++$post;
				$postfix=str_pad($post,3,"0",STR_PAD_LEFT);
				$groupCode = $prefix."".$postfix;
				//echo $groupCode;
			}
			else
			{
				$groupCode = $prefix."001";
			}
		
			$group_code = $groupCode;
		}
		var_dump($_REQUEST['sub_activity_group']);die;
		$query1 = "INSERT INTO sub_activity_group values('','".$group_name."','".$group_code."','".$display_order."','".$status."')";
		$res = DB_query($query1,$db) or die(DB_error());
		//extract($_REQUEST['sub_activity_group']);
		//if($s->RecordInsert('sub_activity_group',$_REQUEST['sub_activity_group']))
		//{
		
		//}
		unset($_REQUEST['sub_activity_group']);	
				//print_r($_REQUEST); exit;
		$_SESSION['success_msg']="Saved Successfully";	
		echo "<script>location.href='index.php?route=SubContractors/Masters/activity_group_defination_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
*/

$sql=DB_query("select Max(display_order) from sub_activity_group",$db);
$check=DB_fetch_array($sql);
if($check['Max(display_order)']==null)
{
$num=1;	
}
else {
	$num=$check['Max(display_order)']+1;
}

if($_REQUEST['action']=='edit' and $_REQUEST['opn']!='')
{
$recEdit=DB_fetch_array($s->GetRecordById('sub_activity_group','id='.$_REQUEST['opn']));
extract($recEdit);		
}
if(isset($_REQUEST['update']))
{
	 $s->RecordEdit('sub_activity_group',$_REQUEST['sub_activity_group'],"id=".$_REQUEST['opn']);
		$_SESSION['success_msg']="Update Successfully";
		echo "<script>location.href='index.php?route=SubContractors/Masters/activity_group_defination_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<style>
.label{
background-color: #B7B7B7;
    color: #000000;
    font-size: 10.5px;
    font-weight: bold !important;
    padding: 5px 10px;
    text-align: left;
}
</style>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Activity Group</h1>
<div align="center">
<form action="" method="post">
<table width="60%" border="1" cellspacing="2" cellpadding="2" align="center" ><tr><td>
<table width="100%" border="1" cellspacing="2" cellpadding="2" class="">
  <tr>
  <td width="39%" class="label"><strong>Activity Group  </strong></td>
    <td width="61%"><font color="#FF0000" size="3"> * </font><input type="text" name="sub_activity_group[group_name]" id="group_name" size="30" style="width:300px;"/></td>
  </tr>
  <tr>
   <td width="39%" class="label"><strong>Activity Group code</strong></td>
    <td width="61%"><font color="#FF0000" size="3"> * </font><input type="text" name="sub_activity_group[group_code]" id="group_code" value="auto generate" size="30" readonly style="width:300px;"/></td>
  </tr>
  <tr>
   <td width="39%" class="label"><strong>Status  </strong></td>
    <td width="61%"><font color="#FF0000" size="3"> * </font><input type="radio" name="sub_activity_group[status]" id="statusA"  size="30"  value="Active" <?php if($status=='Active'){ ?> checked="checked" <?php }?>/>Active
    <input type="radio" name="sub_activity_group[status]" id="statusI"  size="30"  value="InActive" <?php if($status=='InActive'){ ?> checked="checked" <?php }?>/>InActive</td>
  </tr>
  
  
	
  <tr><td>&nbsp;</td>
    <td align="center"><input type="submit" class="search" value="<?php if($_REQUEST['action']=='edit') echo 'update'; else echo 'save'; ?>" name="<?php if($_REQUEST['action']=='edit') echo 'update'; else echo 'save'; ?>" onclick="return validate();" style="cursor:pointer;corsor:hand;"><input type="reset" class="search" name="cancel" value="Reset" /><a href="index.php?route=SubContractors/Masters/Other_Addition_Master_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>" style="cursor:pointer;corsor:hand;text-decoration:none;"><input type="button" name="back" value="Cancel" class="search"/ style="cursor:pointer;corsor:hand;"></a></td>
  </tr>
</table>
</td></tr></table>
</form>
</div>