<script language="JavaScript"> 
function clear_text()
{
	document.getElementById("group_code").value= "";
}

function generateActivityCode(str)
{
	if (str.length=="")
	  {
	  document.getElementById("groupcode").innerHTML="";
	 
	  return;
	  }
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		//alert(xmlhttp.responseText);
		document.getElementById("shailesh").innerHTML=xmlhttp.responseText;
		//document.getElementById("groupcode1234").innerHTML=xmlhttp.responseText;
		}
	  }
 
xmlhttp.open("GET","get_activity_code.php?site="+str,true);
xmlhttp.send();
}
function validate()
{
	var group_name = $('#group_name').val();
	var inventorySubGroup = $('#inventorySubGroup1').val();
	var activity_name = $('#activity_name').val();
	var unit1 = $('#unit').val();
	var rate = $('#rate').val();
	var radio1 = $(':radio').is(':checked');
	
	//alert(group_name);
	//alert(inventorySubGroup);
	//alert(activity_name);
	//alert(unit1);
	//alert(rate);
	//alert(radio1);
	if(group_name == "")
	{
	alert("Select Activity Group");
	return false;
	}
	if(inventorySubGroup == "")
	{
	alert("Select Inventory Group");
	return false;
	}
	if(activity_name == "")
	{
	alert("Select Activity Name");
	return false;
	}
	if(unit1 == "")
	{
	alert("Select Unit");
	return false;
	}
	if(rate == "")
	{
	alert("Select Rate");
	return false;
	}
	if(radio1 == false)
	{
	alert("select status");
	return false;
	}
	return true;
}
function getgroupcode(str) 
{	 
	//salert(str);
	if (str.length=="")
	  {
	  document.getElementById("groupcode").innerHTML="";
	 
	  return;
	  }
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		//alert(xmlhttp.responseText);
		document.getElementById("groupcode123").innerHTML=xmlhttp.responseText;
		//document.getElementById("groupcode1234").innerHTML=xmlhttp.responseText;
		}
	  }
 
xmlhttp.open("GET","get_group_data.php?site="+str,true);
xmlhttp.send();
}
</script>
<?php
$AcgCompanyId = $_SESSION['ACGCompanyId'];
if(isset($_POST['save']))
{   

        $activityCodeRow=DB_fetch_array(DB_query("select Max(id) as ActivityId from sub_activity_defination",$db));   
		$ActivityId=$activityCodeRow['ActivityId'];
		$prefix=strtoupper(substr($_REQUEST['sub_activity_defination']['activity_name'],0,2));
		if($activityCodeRow['ActivityId']=='')
		{
		$postfix="-001";
		
		}
		else
		{
		$postfix=str_pad($activityCodeRow['ActivityId'],3,"0",STR_PAD_LEFT);
		$postfix="-".$postfix;
		
		
		}
		 $ActivituCode=$prefix.$postfix;
		$activity['group_name']=$_REQUEST['sub_activity_defination']['group_name'];
		$activity['group_code']=$_REQUEST['sub_activity_defination']['group_code'];
		$activity['inventory_group_id']=$_REQUEST['sub_activity_defination']['inventorySubGroup'];
		$activity['activity_name']=$_REQUEST['sub_activity_defination']['activity_name'];
		$activity['activity_code']= $ActivituCode;
		//$activity['display_order']=$_REQUEST['sub_activity_defination']['display_order'];
		$activity['unit']=$_REQUEST['sub_activity_defination']['unit'];
		$activity['rate']=$_REQUEST['sub_activity_defination']['rate'];
		//$activity['work_description']=$_REQUEST['sub_activity_defination']['work_description'];
		$activity['status']=$_REQUEST['sub_activity_defination']['status'];
		
		
		
		if($s->RecordInsert('sub_activity_defination',$activity))
		{
		
		}
		unset($_REQUEST['sub_activity_defination']);	
				//print_r($_REQUEST); exit;
		$_SESSION['success_msg']="Saved Successfully";	
		echo "<script>location.href='index.php?route=PMS/Maintenance/activity_defination_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
$sql=DB_query("select Max(display_order) from sub_activity_defination",$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_defination','id='.$_REQUEST['opn']));
extract($recEdit);		
}

if(isset($_REQUEST['update']))
{
$dataEdit['group_name']=$_REQUEST['sub_activity_defination']['group_name'];
$dataEdit['group_code']=$_REQUEST['sub_activity_defination']['group_code'];
$dataEdit['activity_name']=$_REQUEST['sub_activity_defination']['activity_name'];
$dataEdit['activity_code']=$_REQUEST['sub_activity_defination']['activity_code'];

$dataEdit['unit']=$_REQUEST['sub_activity_defination']['unit'];
$dataEdit['rate']=$_REQUEST['sub_activity_defination']['rate'];
$dataEdit['status']=$_REQUEST['save'];

	 $s->RecordEdit('sub_activity_defination',$dataEdit,"id=".$_REQUEST['opn']);
		$_SESSION['success_msg']="Update Successfully";
		echo "<script>location.href='index.php?route=PMS/Maintenance/activity_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">
<?php if($_REQUEST['action']=='')
{
?>


<table width="40%" border="1" cellspacing="1" cellpadding="2">
<tr>
  	<th align="left" valign="top"><span style="color:#FF0000;"> * </span>Product Group</th>
	<td align="left" valign="top">
		<select name="sub_activity_defination[inventorySubGroup]" style="width:100%;" id="inventorySubGroup1">
			<option value="">select</option>
			<?php
				$query = "SELECT * FROM stockcategory where CompanyId = '".$AcgCompanyId."'";
				$res = DB_query($query,$db) or die(DB_error());
				while($result = DB_fetch_array($res))
				{
			?>
				<option value="<?php echo $result['id'];?>"><?php echo $result['categorydescription'];?></option>
			<?php 
				}
			?>
		</select>
	</td>
  </tr>
<tr>
  <th align="left" valign="top" width="30%"><span style="color:#FF0000;"> * </span><strong>Activity Group  </strong></th>
    <td align="left" valign="top"><select  name="sub_activity_defination[group_name]" id="group_name"  onchange="getgroupcode(this.value);" style="width:100%;">
    <option value="">Select Activity Group</option>
    <?php $groupdata=DB_query("select * from sub_activity_group",$db);
	while($group=DB_fetch_array($groupdata))
	{
	$t='';
	if($group['id']==$group_name)
	{
		$t="selected";
	}
		?>	
    <option value="<?php echo $group['id']; ?>"<?php echo $t; ?>><?php echo $group['group_name']; ?></option>
    <?php } ?>	
    	
    </select></td>
  </tr>
  <tr>
   <th align="left" valign="top"><strong>Activity Group code</strong></th>
    <td align="left" valign="top" id="groupcode123"><input type="text" id="group_code" name="sub_activity_defination[group_code]" value="auto generated" size="30" readonly style="width:100%; margin-left:0px;"/>	</td>
  </tr>
  
  <tr>
   <th align="left" valign="top"><span style="color:#FF0000;"> * </span><strong>Activity Name  </strong></th>
    <td align="left" valign="top"><input type="text" name="sub_activity_defination[activity_name]" id="activity_name" value="<?php echo $activity_name;?>" size="30"  onblur="generateActivityCode(this.value);" style="width:100%;"/></td>
  </tr>
   <tr>
   <th align="left" valign="top"><strong>Activity Code  </strong></th>
    <td align="left" valign="top"><input type="text" name="sub_activity_defination[activity_code]" id="activity_code" value="Auto Generate" size="30"   style="width:100%; margin-left:0px;"/></td>
  </tr>
  
  <tr>
  	<th align="left" valign="top"><span style="color:#FF0000;"> * </span>Unit</th>
	<td align="left" valign="top" >
  		<select name="sub_activity_defination[unit]" id="unit" style="width:100%;" class="" >
  			<option value="">--Select--</option>
  				<?php
					$unit_master=DB_query("SELECT * FROM unitsofmeasure ",$db);
					while($unitRow=DB_fetch_array($unit_master)) {
  					$secl='';
					if($unitRow['unitid']==$unit)
  					$secl='selected';
  				?>
			<option value="<?php echo $unitRow['unitid']; ?>"<?php echo $secl; ?>><?php echo $unitRow['unitname']; ?></option>
				<?php } ?>
  		</select>
	</td>
  </tr>
 
  <tr>
  	<th align="left" valign="top"><span style="color:#FF0000;"> * </span>Rate</th>
	<td align="left" valign="top"><input type="text" name="sub_activity_defination[rate]" id="rate" mclass="rate" style="width:100%;"></td>
  </tr>
  <tr>
   <th align="left" valign="top"><strong>Status  </strong></th>
    <td align="left" valign="top"><input type="radio" name="sub_activity_defination[status]" id="status"  size="30"  value="Active" checked="checked" style="margin-left:12px;"/>Active
    <input type="radio" name="sub_activity_defination[status]" id="status"  size="30"  value="InActive" />InActive</td>
  </tr>
  <tr><td align="left" valign="top">&nbsp;</td>
    <td align="left" valign="top"><input class="search" type="submit" 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 class="search" type="reset" name="cancel" value="Reset" style="cursor:pointer;corsor:hand;" onclick="clear_text();" /><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" class="search" value="Cancel" /></a></td>
  </tr>
</table>

<?php }
else if($_REQUEST['action']=='edit')
{

?>
<table width="40%" border="1" align="center" cellpadding="2" cellspacing="1" style="border:#999999 1px solid;">

  <tr>
  <th width="30%" align="left" valign="top"><span style="color:#FF0000">*</span> <strong>Activity Group</strong></th>
    <td align="left" valign="top"><select  name="sub_activity_defination[group_name]" id="group_name"  onchange="getgroupcode(this.value);" style="width:100%;" >
    <option value="">Select Activity Group</option>
    <?php $groupdata=DB_query("select * from sub_activity_group",$db);
	while($group=DB_fetch_array($groupdata))
	{
	$t='';
	if($group['id']==$group_name)
	{
		$t="selected";
	}
		?>	
    <option value="<?php echo $group['id']; ?>"<?php echo $t; ?>><?php echo $group['group_name']; ?></option>
    <?php } ?>	
    	
    </select></td>
  </tr>
  <tr>
   <th align="left" valign="top"><span style="color:#FF0000">*</span><strong>Activity Group code</strong></th>
    <td align="left" valign="top" id="groupcode123" ><input type="text" value="<?php echo $group_code;?>" readonly="true" size="30" style="margin-left:0px;width:100%;" name="sub_activity_defination[group_code]"/></td>
  </tr>
  <tr>
  	<th align="left" valign="top">Product Group</th>
	<td align="left" valign="top">
		<select name="sub_activity_defination[inventorySubGroup]" style="width:100%;" id="inventorySubGroup1">
			<option value="">select</option>
			<?php
				$query = "SELECT * FROM stockcategory where CompanyId = '".$AcgCompanyId."'";
				$res = DB_query($query,$db) or die(DB_error());
				while($result = DB_fetch_array($res))
				{
				$t1='';
				if($inventory_group_id==$result['id'])
				{
				$t1="selected";
				}
			?>
				<option value="<?php echo $result['id'];?>" <?php echo $t1; ?>><?php echo $result['categorydescription'];?></option>
			<?php 
				}
			?>
		</select>
	</td>
  </tr>
  
  <tr>
   <th align="left" valign="top"><span style="color:#FF0000">*</span><strong>Activity Name</strong></th>
    <td align="left" valign="top"><input type="text" name="sub_activity_defination[activity_name]" id="activity_name" value="<?php echo $activity_name;?>" size="30"  onchange="generateActivityCode(this.value);" style="width:100%;"/></td>
  </tr>
   <tr>
   <th align="left" valign="top"><strong>Activity Code  </strong></th>
    <td align="left" valign="top" id="activity_code_gen" ><div id="shailesh"><input type="text" name="sub_activity_defination[activity_code]" value="<?php echo $activity_code;?>" readonly="true" size="30" style="margin-left:0px;width:100%;" /></div></td>
  </tr>
  <tr style="display:none;">
  	<th align="left" valign="top"><span style="color:#FF0000">*</span>Unit</th>
	<td align="left" valign="top" >
  		<select name="sub_activity_defination[unit]" id="unit" style="width:100%;" class="" >
  			<option value="">--Select--</option>
  				<?php
					$unit_master=DB_query("SELECT * FROM unitsofmeasure ",$db);
					while($unitRow=DB_fetch_array($unit_master)) {
  					$secl='';
					if($unitRow['unitid']==$unit)
  					$secl='selected';
  				?>
			<option value="<?php echo $unitRow['unitid']; ?>"<?php echo $secl; ?>><?php echo $unitRow['unitname']; ?></option>
				<?php } ?>
  		</select>
	</td>
  </tr>
  <tr style="display:none;">
   <th align="left" valign="top"><span style="color:#FF0000">*</span><strong>Rate  </strong></th>
    <td align="left" valign="top"><input type="text" name="sub_activity_defination[rate]" id="rate" value="<?php echo $rate; ?>" style="width:100%;" /></td>
  </tr>
  
  <tr>
   <th align="left" valign="top"><strong>Status</strong></th>
    <td align="left" valign="top">
      <input type="radio" name="sub_activity_defination[status]" id="status"  size="30"  value="Active" <?php if($status=='Active'){ ?> checked="checked" <?php }?>/>Active
    <input type="radio" name="sub_activity_defination[status]" id="status"  size="30"  value="InActive" <?php if($status=='InActive'){ ?> checked="checked" <?php }?>/>InActive</td>
  </tr>
  
  
	
  <tr><td align="left" valign="top">&nbsp;</td>
    <td align="left" valign="top"><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'; ?>"><?php if($_REQUEST['action']=='') {?><input type="reset" class="search" name="cancel" value="Reset" /><?php }?><a href="index.php?route=PMS/Maintenance/activity_defination_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>"><input type="button" name="back" value="Cancel" class="search"/></a></td>
  </tr>
</table>



<?php }?>
</form>
</div>


