<?php   
$PageSecurity = 1;
include('includes/AccountGroupMaster_Class.php');
$table='awd_member_assmnt_topic';

if($_REQUEST['Save'] && $_REQUEST['action']!='Edit')
{

$data['assmnt_area_dtls_id']=$_REQUEST['assassmt_area'];
//$data['fin_year_id']=$_SESSION['financialYrId'];
$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['topic_seq']);$i++)
	{
		if($_REQUEST['topic_seq'][$i]!='' && $_REQUEST['topic_code'][$i]!='' && $_REQUEST['topic_name'][$i]!='' && $_REQUEST['total_marks'][$i]!='')
		{
			$dataArr['assmnt_subctgries_topic_id']=$last_id;
			$dataArr['topic_seq']=$_REQUEST['topic_seq'][$i];
			$dataArr['topic_code']=$_REQUEST['topic_code'][$i];
			$dataArr['topic_name']=$_REQUEST['topic_name'][$i];
			$dataArr['total_marks']=$_REQUEST['total_marks'][$i];
			$s->RecordInsert('awd_member_assmnt_topic_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'];
	$assmnt_area_dtls_id=$RData['assmnt_area_dtls_id'];
	$comp_typ=$RData['comp_typ'];
}	
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit')
{  
   $data['assmnt_ctgries_id']=$_REQUEST['assmnt_ctgries_id'];
   $data['assmnt_area_dtls_id']=$_REQUEST['assassmt_area'];
   $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['topic_seq']);$i++)
	{    
	if($_REQUEST['topic_code'][$i]=='' && $_REQUEST['topic_name'][$i]=='' && $_REQUEST['total_marks'][$i]==''){
      DB_query("DELETE FROM awd_member_assmnt_topic_dtls WHERE id='".$_REQUEST['topic_dtls_id'][$i]."'",$db);
        }
	else if($_REQUEST['topic_seq'][$i]!='' && $_REQUEST['topic_code'][$i]!='' && $_REQUEST['topic_name'][$i]!='' && $_REQUEST['total_marks'][$i]!='')
		{
    $SqlArr= DB_fetch_array(DB_query("SELECT id FROM awd_member_assmnt_topic_dtls WHERE assmnt_subctgries_topic_id='".$_REQUEST['id']."' and id='".$_REQUEST['topic_dtls_id'][$i]."'",$db));
    if($SqlArr['id']!=''){
      $dataArr['topic_seq']=$_REQUEST['topic_seq'][$i];
	  $dataArr['topic_code']=$_REQUEST['topic_code'][$i];
	  $dataArr['topic_name']=$_REQUEST['topic_name'][$i];
	  $dataArr['total_marks']=$_REQUEST['total_marks'][$i];
      $condd="assmnt_subctgries_topic_id='".$_REQUEST['id']."' and id='".$_REQUEST['topic_dtls_id'][$i]."'";
      $s->RecordEdit('awd_member_assmnt_topic_dtls',$dataArr,$condd);
     }else{
	  $dataArr1['assmnt_subctgries_topic_id']=$_REQUEST['id'];
	  $dataArr1['topic_seq']=$_REQUEST['topic_seq'][$i];
	  $dataArr1['topic_code']=$_REQUEST['topic_code'][$i];
	  $dataArr1['topic_name']=$_REQUEST['topic_name'][$i];
	  $dataArr1['total_marks']=$_REQUEST['total_marks'][$i];
	  $s->RecordInsert('awd_member_assmnt_topic_dtls',$dataArr1);
		}
	}
	}
	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_topic_dtls WHERE assmnt_subctgries_topic_id='".$_REQUEST['id']."'",$db);
	prnMsg( _('Record deleted successfully'),'success');
	$s->pageLocation('index.php?route=HRModule/Master/assessmt_subcat_topic&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 call_area(id){
//var cat_id=document.getElementById('assmnt_ctgries_id').value;
var typ=document.getElementById('comp_typ').value;	
//if(cat_id!='' && typ!=''){
  if(typ!=''){
ajax();
   if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
		{
		//alert(doc.responseText);
		document.getElementById("assassmt_area").innerHTML=doc.responseText;
		}
	}
//doc.open("GET","check_data_awd.php?section=call_area_topic_creation&id="+cat_id+"&typ="+typ,true);
doc.open("GET","check_data_awd.php?section=call_area_topic_creation&typ="+typ,true);
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("assmnt_ctgries_id").value=='' || document.getElementById("assmnt_ctgries_id").value=='0')
{
alert("Must Select Category");
document.getElementById("assmnt_ctgries_id").focus();
return false;

}
if(document.getElementById("comp_typ").value=='0' || document.getElementById("comp_typ").value=='')
{
alert("Must Select Company Type");
document.getElementById("comp_typ").focus();
return false;

}
if(document.getElementById("assassmt_area").value=='')
{
alert("Must Select Area");
document.getElementById("assassmt_area").focus();
return false;
}

return true;

}
function get_oldtopic(areaId)
{
  var aCatId=$("#assmnt_ctgries_id").val();
   var compId=$("#comp_typ").val();
   var areaId=$("#assassmt_area").val();
   $.ajax({
       url:"ajax_getold_topic.php",
       type:"POST",
       data:{aCatId:aCatId,compId:compId,areaId:areaId},
       success:function(resultold_topic){
            $("#oldytopic").html(resultold_topic);
       }

   });
  
}
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("topic_code_1").value ==''){

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

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

           if(document.getElementById("topic_name_"+i).value=='')
           {
            alert( "Please fill  Name");
            document.getElementById("topic_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"> 
              <h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name");?></h1>
              <p align="left">              
               <?php  if($assmnt_area_dtls_id!=''){?>
                <a href="<?php echo 'index.php?route=HRModule/Master/assessmt_subcat_topic&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'';?>"><button type="button">Add New</button></a>
               <? } ?>
              </p>
<?php /*?><h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name");?></h1><?php */?>
<form action="" method="post" onsubmit="return validation()">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
	<td width="13%"><strong>Employee Type</strong><font color="#FF0000">*</font></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="call_area(this.value)"><!-- onchange="generate_reqno(this.value)"-->
	
	<?php 

  
  if($assmnt_area_dtls_id!=''){
   
    //  $area=DB_fetch_array(DB_query("select id from awd_member_assmnt_area where emp_type='Permanent' order by id",$db));
  $areadee=DB_fetch_array(DB_query("select assmnt_subctgries_area_id from awd_member_assmnt_area_dtls where id='".$assmnt_area_dtls_id."'",$db));
  $areaa=DB_query("select emp_type from awd_member_assmnt_area where id='".$areadee['assmnt_subctgries_area_id']."' order by id ",$db);

while($rowdea=DB_fetch_array($areaa))
  {
  ?>
  <option value="<?php echo $rowdea['emp_type']; ?>"><?php echo $rowdea['emp_type']; ?></option>
  <?php } }
  else{
?>
<option value="Permanent">Permanent</option>
  <option value="Contract">Contract</option>
  <?php } ?></select></td>
	<td width="13%"><strong>Assessment Area</strong> <font color="#FF0000">*</font></td>
  <td>
	<select name="assassmt_area" id="assassmt_area">

    <!-- onchange="get_oldtopic(this.value)"-->
    <!-- onchange="generate_reqno(this.value)"-->
   
    <?php if($assmnt_area_dtls_id!=''){
    //	$area=DB_fetch_array(DB_query("select id from awd_member_assmnt_area where emp_type='Permanent' order by id",$db));
	$areade=DB_query("select * from awd_member_assmnt_area_dtls where id='".$assmnt_area_dtls_id."'",$db);
while($rowde=DB_fetch_array($areade))
	{
	?>
<option value="<?php echo $rowde['id']; ?>" <?php echo (($rowde['id']==$assmnt_area_dtls_id)?'selected':'')?>><?php echo $rowde['area_name']; ?></option>
	<?php } }
  else{

  $areade=DB_query("select B.* from awd_member_assmnt_area_dtls B inner join awd_member_assmnt_area A on A.id=B.assmnt_subctgries_area_id
   where A.emp_type='Permanent'",$db);
while($rowde=DB_fetch_array($areade))
  {
  ?>
<option value="<?php echo $rowde['id']; ?>" <?php echo (($rowde['id']==$assmnt_area_dtls_id)?'selected':'')?>><?php echo $rowde['area_name']; ?></option>
  <?php }
  }
  ?>
	</select></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%">Seq. No.</th>
    		<th width="15%">Code <span style="color:red">*</span></th>
    		<th>Name <span style="color:red">*</span></th>
    		<th width="10%">Total Marks <span style="color:red">*</span></th>
      </tr>
    		<?php if($_REQUEST['action']=='Edit'){
    		$sqlassde = DB_query("SELECT * FROM awd_member_assmnt_topic_dtls WHERE assmnt_subctgries_topic_id='".$assmnt_subcat_id."'",$db);
    		$noData = DB_num_rows($sqlassde);
        $j=1;
    		 while($RowData = DB_fetch_array($sqlassde)){?>
    	<tr>
    	<td><input type="hidden" name="topic_dtls_id[]" value="<?php echo $RowData['id']?>">
    		  <input type="text" name="topic_seq[]"  readonly value="<?php echo $RowData['topic_seq']?>"></td>
    	<td><input type="text" name="topic_code[]"  id="topic_code_<?php echo $j;?>" value="<?php echo $RowData['topic_code']?>"></td>
    	<td><input type="text" name="topic_name[]"  id="topic_name_<?php echo $j;?>" value="<?php echo $RowData['topic_name']?>"></td>
    	<td><input type="text" name="total_marks[]" id="total_marks_<?php echo $j;?>" onkeypress="return isNumberKey(event)" value="<?php echo $RowData['total_marks']?>"></td>
        </tr>
    		 <?php $j++; } } 
    		 if($_REQUEST['action']=='Edit' && $noData>0){
    		 	$p=$noData+1;
    		 }else{
    		 	$p=1;
    		 }?>
    		<tbody id="oldytopic">
            <?php  
             if($_REQUEST['action']!='Edit')
             {
            for($i=$p;$i<=20;$i++)
              {
          // $sqlassmaster = DB_query("SELECT * FROM awd_member_assmnt_ctgries_dtls WHERE assmnt_subctgries_area_id='".$assmnt_subcat_id."'",$db);
          ?>
            <tr>
            <td><input type="text" name="topic_seq[]" readonly value="<?php echo $i?>"></td>
            <td><input type="text" name="topic_code[]" id="topic_code_<?php echo $i?>" value=""></td>
            <td><input type="text" name="topic_name[]"  id="topic_name_<?php echo $i?>" value=""></td>
            <td><input type="text" name="total_marks[]"  id="total_marks_<?php echo $i?>" onkeypress="return isNumberKey(event)" value=""></td>
          </tr>
            <?php } }?>
        </tbody>
    	<?php  
       if($_REQUEST['action']=='Edit')
       {
      for($i=$p;$i<=20;$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="topic_seq[]"  readonly value="<?php echo $i?>"></td>
    	<td><input type="text" name="topic_code[]" id="topic_code_<?php echo $i?>" value=""></td>
    	<td><input type="text" name="topic_name[]" id="topic_name_<?php echo $i?>" value=""></td>
    	<td><input type="text" name="total_marks[]"  id="total_marks_<?php echo $i?>"onkeypress="return isNumberKey(event)" value=""></td>
    </tr>
    	<?php } }?>
		<tr class="row1">
    <td colspan="4" style="text-align:center;"> 
     <?php if($_REQUEST['action'] == 'Edit') {?>
      <input type="submit" name="Save" value="Update" 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_topicindex.php') ?> 
</div></div>