<?php   
$PageSecurity = 1;
include('includes/AccountGroupMaster_Class.php');
$table='awd_member_assmnt_area';
date_default_timezone_set("Asia/Calcutta");
if($_REQUEST['Save'] && $_REQUEST['action']!='Edit')
{
//$sql_get_oq="Select id from awd_member_assmnt_topic WHERE assmnt_ctgries_id='".$_REQUEST['assmnt_ctgries_id']."' AND assmnt_area_dtls_id='".$_REQUEST['assassmt_area']."' AND comp_typ='".$_REQUEST['comp_typ']."' AND fin_year_id='".$_SESSION['financialYrId']."' AND CompanyId='".$_SESSION['ACGCompanyId']."'";
  //$query_get_oq=DB_query($sql_get_oq,$db);
  //$num_exist_que=DB_num_rows($query_get_oq);
//if($num_exist_que>0)
//{
  //  $exist_que_rows=DB_fetch_array($query_get_oq);
   // $last_id=$exist_que_rows['id'];
//}else{

$data['emp_type']=$_REQUEST['comp_typ'];
$data['CompanyId']=$_SESSION['ACGCompanyId'];
$data['Created_date']=date('Y-m-d');
$data['Created_by']=$_SESSION['UserID'];
$s->RecordInsert($table,$data);
$last_id=DB_Last_Insert_ID($db,$table,'id');
//}
for($i=0;$i<count($_REQUEST['area_seq']);$i++)
	{
		if($_REQUEST['area_seq'][$i]!='' && $_REQUEST['area_code'][$i]!='' && $_REQUEST['area_name'][$i]!='' && $_REQUEST['total_marks'][$i]!=0)
		{
			$dataArr['assmnt_subctgries_area_id']=$last_id;
			$dataArr['area_seq']=$_REQUEST['area_seq'][$i];
			$dataArr['area_code']=$_REQUEST['area_code'][$i];
			$dataArr['area_name']=$_REQUEST['area_name'][$i];
			$dataArr['total_marks']=$_REQUEST['total_marks'][$i];
			$s->RecordInsert('awd_member_assmnt_area_dtls',$dataArr);//INSERT INTO CHIELD TABLE
		}
	}
//$s->pageLocation('index.php?route=Voucher_Master&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}
	
if(!$_REQUEST['Save'] &&   $_REQUEST['action']=='Edit')
{
	$condition="id='".$_REQUEST['id']."'";
	$allready=$s->GetRecord($table,$condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);	
	$assmnt_subcat_id=$RData['id'];	
	$assmnt_ctgries_id=$RData['assmnt_ctgries_id'];
	$comp_typ=$RData['emp_type'];
	
}	
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit')
{
  // $data['assmnt_ctgries_id']=$_REQUEST['assmnt_ctgries_id'];
   $data['emp_type']=$_REQUEST['comp_typ'];
   $data['modified_date']=date('Y-m-d');
   $data['modified_by']=$_SESSION['ACGCompanyId'];
    $cond="id='".$_REQUEST['id']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	for($i=0;$i<count($_REQUEST['area_seq']);$i++)
	{
    if($_REQUEST['area_code'][$i]=='' && $_REQUEST['area_name'][$i]=='' && $_REQUEST['total_marks'][$i]==''){
      DB_query("DELETE FROM awd_member_assmnt_area_dtls  WHERE id='".$_REQUEST['area_dtls_id'][$i]."'",$db);
        }
	else if($_REQUEST['area_seq'][$i]!='' && $_REQUEST['area_code'][$i]!='' && $_REQUEST['area_name'][$i]!='' && $_REQUEST['total_marks'][$i]!='')
		{
    $SqlArr= DB_fetch_array(DB_query("SELECT id FROM awd_member_assmnt_area_dtls WHERE assmnt_subctgries_area_id='".$_REQUEST['id']."' and id='".$_REQUEST['area_dtls_id'][$i]."'",$db));
    if($SqlArr['id']!=''){
      $dataArr['area_seq']=$_REQUEST['area_seq'][$i];
	  $dataArr['area_code']=$_REQUEST['area_code'][$i];
	  $dataArr['area_name']=$_REQUEST['area_name'][$i];
	  $dataArr['total_marks']=$_REQUEST['total_marks'][$i];
      $condd="assmnt_subctgries_area_id='".$_REQUEST['id']."' and id='".$_REQUEST['area_dtls_id'][$i]."'";
      $s->RecordEdit('awd_member_assmnt_area_dtls',$dataArr,$condd);
     }else{
	  $dataArr['assmnt_subctgries_area_id']=$_REQUEST['id'];
	  $dataArr['area_seq']=$_REQUEST['area_seq'][$i];
	  $dataArr['area_code']=$_REQUEST['area_code'][$i];
	  $dataArr['area_name']=$_REQUEST['area_name'][$i];
	  $dataArr['total_marks']=$_REQUEST['total_marks'][$i];
	  $s->RecordInsert('awd_member_assmnt_area_dtls',$dataArr);
		}
		}
	}
	if(!$row)
	{
	   die('Error'.DB_error());
	}
  prnMsg( _('Record Updated successfully'),'success');
}

if($_REQUEST['action']=='del')
{
	$condition="id='".$_REQUEST['id']."'";
	$s->DeleteRecord($table,$condition);
	DB_query("DELETE FROM awd_member_assmnt_area_dtls WHERE assmnt_subctgries_area_id='".$_REQUEST['id']."'",$db);
	prnMsg( _('Record deleted successfully'),'success');
	$s->pageLocation('index.php?route=HRModule/Master/assessmt_subcat_area&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<script type="text/javascript">
var doc=null;
function ajax(){
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  doc=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  doc=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
function sam_code(){
var sam_cd=document.getElementById('sample_code').value;
ajax();
   if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
		{
		
		if(doc.responseText=='Not Available'){
		document.getElementById("sc").innerHTML='<font color="red">The Sample Code <STRONG>'+sam_cd+'</STRONG> is already in use.</font>';
		document.getElementById('sample_code').value='';
		}else{
		document.getElementById("sc").innerHTML='<img src="accepted.png" align="absmiddle"> <font color="Green"> Available </font>';
		}
		}
	}

doc.open("GET","check_data_bsm.php?section=sample_code&sam_cd="+sam_cd,false);

doc.send("null");
}
}
function sam_name(){
var sam_nm=document.getElementById('sample_name').value;
ajax();
   if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
		{
		if(doc.responseText=='Not Available'){
		document.getElementById("sn").innerHTML='<font color="red">The Type Name <STRONG>'+sam_nm+'</STRONG> is already in use.</font>';
		document.getElementById('sample_name').value='';
		}else{
		document.getElementById("sn").innerHTML='<img src="accepted.png" align="absmiddle"> <font color="Green"> Available </font>';
		}
		}
	}
doc.open("GET","check_data_bsm.php?section=sample_name_master&sam_nm="+sam_nm,false);

doc.send("null");
}
}
function chkvalid()
{
if(document.getElementById("assess_code").value=='')
{
alert("Must Fill Code");
document.getElementById("assess_code").focus();
return false;

}
if(document.getElementById("assess_name").value=='')
{
alert("Must Fill Name");
document.getElementById("assess_name").focus();
return false;
}

return true;

}
function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       } 

   function validation(){

   			if(document.getElementById("area_code_1").value ==''){

   						alert("Please fill at least one row");
   						return false;

   					}
   		for(var i=1;i<=15; i++){   			
   			if(document.getElementById("area_code_"+i).value !=''){
   				

   				 if(document.getElementById("area_name_"+i).value=='')
   				 {
   				 	alert( "Please fill Area Name");
   				 	document.getElementById("area_name_"+i).focus();
   				 	return false;
   				 }
   				 if(document.getElementById("total_marks_"+i).value=='')
   				 {
   				 	alert( "Please fill total mark");
   				 	document.getElementById("total_marks_"+i).focus();
   				 	return false;
   				 }
   			}
      
        }
  

   }   
</script>
<div class="body_contain">
<div class="tab_div"> 
<form action="" method="post" OnSubmit="return validation();">

              <h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name");?></h1>
<?php /*?><h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name");?></h1><?php */?>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
    <!-- <td width="15%"><label for="datepicker" class="col-lg-12 control-label text-right">Assessment Category<font color="#FF0000">*</font>
    </label></td>
    <td width="35%">
    <?php $cat_sql=DB_query("SELECT id,name FROM awd_member_assmnt_ctgries WHERE CompanyId='".$_SESSION['ACGCompanyId']."' and cat_type='".$_SESSION['AssmntCategory']."' ORDER BY seq",$db);?>
	<select name="assmnt_ctgries_id" id="assmnt_ctgries_id" class="form-control input-sm col-lg-2">onchange="generate_reqno(this.value)"
	<?php while($cat_row=DB_fetch_array($cat_sql)){?>
	<option value="<?php echo $cat_row['id'];?>"<?php echo ($assmnt_ctgries_id==$cat_row['id']?'selected':'')?>><?php echo $cat_row['name'];?></option>
	<?php } ?></select></td>-->
	<td width="13%"><strong>Employee Type</strong></td>
	<td width="35%">
    <?php //$comp_sql=DB_query("SELECT id,typ_name FROM awd_member_cmpny_types WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND typ_status='1'",$db);?>
	<select name="comp_typ" id="comp_typ"><!-- onchange="generate_reqno(this.value)"-->
	<?php// while($comp_row=DB_fetch_array($comp_sql)){?>
	<!--<option value="<?php echo $comp_row['id'];?>"<?php echo ($comp_typ==$comp_row['id']?'selected':'')?>><?php echo $comp_row['typ_name'];?></option>-->
	<option value="Permanent" <?php  if($comp_typ=="Permanent") { echo "selected";} ?>>Permanent</option>
	<option value="Contract" <?php  if($comp_typ=="Contract") { echo "selected";} ?>>Contract</option>
	<?php// } ?></select></td>
	<td></td>
  </tr>
</table>
<br />
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border:#808080 1px solid; border-collapse:collapse;">
    	<tr>
		<th width="5%"><strong>Seq. No. <span style="color:red"></span></strong></th>
    	<th width="25%"><strong>Code</strong> <span style="color:red">*</span></th>
    	<th><strong>Name</strong> <span style="color:red">*</span></th>
    	<th width="7%"><strong>Total Marks</strong><span style="color:red">*</span></th>
    	</tr>
    	<?php if($_REQUEST['action']=='Edit'){ 
    		$sqlassde = DB_query("SELECT * FROM awd_member_assmnt_area_dtls WHERE assmnt_subctgries_area_id='".$assmnt_subcat_id."'",$db); $noData = DB_num_rows($sqlassde); 
    		$j=1;
    		while($RowData = DB_fetch_array($sqlassde)){?>
    	<tr>
    	<td><input type="hidden" name="area_dtls_id[]"  value="<?php echo $RowData['id']?>">
    		<input type="text" name="area_seq[]" readonly value="<?php echo $RowData['area_seq']?>"></td>
    	<td><input type="text" name="area_code[]" id="area_code_<?php echo $j;?>" value="<?php echo $RowData['area_code']?>"></td>
    	<td><input type="text" name="area_name[]" id="area_name_<?php echo $i;?>" value="<?php echo $RowData['area_name']?>"></td>
    	<td><input type="text" name="total_marks[]"  id="total_marks_<?php echo $i;?>"value="<?php echo $RowData['total_marks']?>" onkeypress="return isNumberKey(event)"></td>
        </tr>
    		 <?php  $j++;} } 
    		 if($_REQUEST['action']=='Edit' && $noData>0){
    		 	$p=$noData+1;
    		 }else{
    		 	$p=1;
    		 }?>
    	<?php  for($i=$p;$i<=15;$i++)
				{
		// $sqlassmaster = DB_query("SELECT * FROM awd_member_assmnt_ctgries_dtls WHERE assmnt_subctgries_area_id='".$assmnt_subcat_id."'",$db);
		?>
    	<tr bgcolor="#fff">
    	<td><input type="text" name="area_seq[]" readonly id = "area_seq_<?php echo $i;?>"  value="<?php echo $i;?>"></td>
    	<td><input type="text" name="area_code[]"  value=""  id="area_code_<?php echo $i;?>"></td>
    	<td><input type="text" name="area_name[]" value="" id="area_name_<?php echo $i;?>"></td>
    	<td><input type="text" name="total_marks[]" value=""  id="total_marks_<?php echo $i;?>" onkeypress="return isNumberKey(event)"></td>
    </tr>
    	<?php } ?>
		<tr class="row1">
    <td colspan="4" style="text-align:center;"> 
     <?php if($_REQUEST['action'] == 'Edit') {?>
      <input type="submit" name="Save" value="Save" class="search" onclick="return chkvalid();" <?php if($_SESSION['frmEdit']=='0'){?>disabled<?php } ?>>
      <?php } else {?>
      <input type="submit" name="Save" value="Save" class="search" onclick="return chkvalid();" <?php if($_SESSION['frmcreate']=='0'){?>disabled<?php } ?>>
    <?php } ?></td>
  </tr>
    </table>
</form>
    <?php include('Viewassessmt_areaindex.php') ?> 

</div></div>