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

if($_REQUEST['Save'] && $_REQUEST['action']!='Edit')
{
  //$sql_get_oq="Select id from awd_question WHERE assmnt_ctgries_id='".$_REQUEST['assmnt_ctgries_id']."'
  // AND assmnt_area_dtls_id='".$_REQUEST['assassmt_area']."' AND assmnt_topic_dtls_id='".$_REQUEST['assassmt_topic']."'
   // AND comp_typ='".$_REQUEST['comp_typ']."' AND fin_year_id='".$_SESSION['financialYrId']."' 
   // AND CompanyId='".$_SESSION['ACGCompanyId']."'";

    $sql_get_oq="Select id from awd_question WHERE assmnt_area_dtls_id='".$_REQUEST['assassmt_area']."' AND assmnt_topic_dtls_id='".$_REQUEST['assassmt_topic']."'
    AND emp_typ='".$_REQUEST['comp_typ']."'";
  $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['assmnt_ctgries_id']=$_REQUEST['assmnt_ctgries_id'];
      $data['assmnt_area_dtls_id']=$_REQUEST['assassmt_area'];
      $data['assmnt_topic_dtls_id']=$_REQUEST['assassmt_topic'];
      $data['emp_typ']=$_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['ques_seq']);$i++)
	{
		if($_REQUEST['ques_seq'][$i]!='' && $_REQUEST['ques_code'][$i]!='' && $_REQUEST['ques_name'][$i]!='' 
      && $_REQUEST['ques_marks'][$i]!='')
		{
			$dataArr['question_id']=$last_id;
			$dataArr['ques_seq']=$_REQUEST['ques_seq'][$i];
			$dataArr['ques_code']=$_REQUEST['ques_code'][$i];
			$dataArr['ques_name']=$_REQUEST['ques_name'][$i];
			$dataArr['ques_marks']=$_REQUEST['ques_marks'][$i];
			$s->RecordInsert('awd_question_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);	
	$question_id=$RData['id'];	
	//$assmnt_ctgries_id=$RData['assmnt_ctgries_id'];
	$assmnt_area_dtls_id=$RData['assmnt_area_dtls_id'];
	$assmnt_topic_dtls_id=$RData['assmnt_topic_dtls_id'];
  $comp_typ=$RData['emp_typ'];
	if($assmnt_topic_dtls_id!=''){
	$topic_mark=$s->GetFieldValue('awd_member_assmnt_topic_dtls',"id='".$assmnt_topic_dtls_id."'" ,'total_marks');
    }
}	
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit')
{
   //$data['assmnt_ctgries_id']=$_REQUEST['assmnt_ctgries_id'];
   $data['assmnt_area_dtls_id']=$_REQUEST['assassmt_area'];
   $data['assmnt_topic_dtls_id']=$_REQUEST['assassmt_topic'];
   $data['emp_typ']=$_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['ques_seq']);$i++)
	{ 
    if($_REQUEST['ques_code'][$i]=='' && $_REQUEST['ques_name'][$i]=='' && $_REQUEST['ques_marks'][$i]==''){
    DB_query("DELETE FROM awd_question_dtls WHERE id='".$_REQUEST['question_dtls_id'][$i]."'",$db);
    }
		else if($_REQUEST['ques_seq'][$i]!='' && $_REQUEST['ques_code'][$i]!='' && $_REQUEST['ques_name'][$i]!='' && $_REQUEST['ques_marks'][$i]!='')
		{
    $SqlArr= DB_fetch_array(DB_query("SELECT id FROM awd_question_dtls WHERE question_id='".$_REQUEST['id']."' and id='".$_REQUEST['question_dtls_id'][$i]."'",$db));
    if($SqlArr['id']!=''){
    $dataArr['ques_seq']=$_REQUEST['ques_seq'][$i];
	  $dataArr['ques_code']=$_REQUEST['ques_code'][$i];
	  $dataArr['ques_name']=$_REQUEST['ques_name'][$i];
	  $dataArr['ques_marks']=$_REQUEST['ques_marks'][$i];
    $condd="question_id='".$_REQUEST['id']."' and id='".$_REQUEST['question_dtls_id'][$i]."'";
    $s->RecordEdit('awd_question_dtls',$dataArr,$condd);
     }else{
	  $dataArr['question_id']=$_REQUEST['id'];
	  $dataArr['ques_seq']=$_REQUEST['ques_seq'][$i];
	  $dataArr['ques_code']=$_REQUEST['ques_code'][$i];
	  $dataArr['ques_name']=$_REQUEST['ques_name'][$i];
	  $dataArr['ques_marks']=$_REQUEST['ques_marks'][$i];
	  $s->RecordInsert('awd_question_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_question_dtls WHERE question_id='".$_REQUEST['id']."'",$db);
	prnMsg( _('Record deleted successfully'),'success');
	$s->pageLocation('index.php?route=HRModule/Master/question&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}


?>
<link rel="stylesheet" href="css/new_mxerp.css">
<script type="text/javascript">


function validation123(){
       
        if(document.getElementById('comp_typ').value=='0')
        {
           alert("Please Select Employee Type");
           return false;
        }
        if(document.getElementById('assassmt_area').value=='0'){
           alert("Assessment Area");
           return false;

        }
        if(document.getElementById('assassmt_topic').value=='0'){
           alert("Assessment Topic");
           return false;

        }


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

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

            }
      for(var i=1;i<=20; i++){     

        if(document.getElementById("qcode_"+i).value!=''){   
           if(document.getElementById("qname_"+i).value=='')
           {
            alert( "Please fill  Name");
            document.getElementById("qname_"+i).focus();
            return false;
           }
           if(document.getElementById("qmarks_"+i).value=='')
           {
            alert( "Please fill total mark");
            document.getElementById("qmarks_"+i).focus();
            return false;
           }
        }
      
        }
  

   }   










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)
		{
        document.getElementById("assassmt_area").innerHTML=doc.responseText;
        document.getElementById("assassmt_topic").innerHTML='';
        document.getElementById("topic_mark").innerHTML='';
        document.getElementById("hid_topic_mk").innerHTML='';
		}
	}
doc.open("GET","check_data_awd.php?section=call_area_topic_creation&typ="+typ,true);
doc.send("null");
}
}
}
function call_topic(){
	//var catid=document.getElementById('assmnt_ctgries_id').value;
	var areaid=document.getElementById('assassmt_area').value;
  var typ=document.getElementById('comp_typ').value;

ajax();
   if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
		{ 
		document.getElementById("assassmt_topic").innerHTML=doc.responseText;
		}
	}
//doc.open("GET","check_data_awd.php?section=call_topic&catid="+catid+"&areaid="+areaid+"&typ="+typ,true);
doc.open("GET","check_data_awd.php?section=call_topic&areaid="+areaid+"&typ="+typ,true);
doc.send("null");
}
}
function call_topic_marks(id){
ajax();
   if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
		{ // alert(doc.responseText);
		document.getElementById("topic_mark").innerHTML=doc.responseText.trim();
		document.getElementById("hid_topic_mk").value=doc.responseText.trim();
		}
	}
doc.open("GET","check_data_awd.php?section=call_topic_marks&id="+id,true);
doc.send("null");
}
}
function get_oldquestion(topicId)
{
 // var aCatId=$("#assmnt_ctgries_id").val();
   var compId=$("#comp_typ").val();
   var areaId=$("#assassmt_area").val();
   $.ajax({
       url:"ajax_getold_question.php",
       type:"POST",
       //data:{aCatId:aCatId,compId:compId,areaId:areaId,topicId:topicId},
       data:{compId:compId,areaId:areaId,topicId:topicId},
       success:function(resultold_question){
            $("#oldyquestion").html(resultold_question);
       }

   });
  
}
function chk_marks_add(val){
  var countm = $(".get_mt").length;
  var oldm=$("#tot_mk").val();
  //alert(oldm);
   var qmark=oldm;
   var topicmk=document.getElementById('hid_topic_mk').value;
   for(var i=1;i<=countm; i++){
    //alert('qmarks_'+i);
    if((document.getElementById('qmarks_'+i).value!='')){
  var qmark=parseFloat(qmark)+parseFloat(document.getElementById('qmarks_'+i).value);
    if(parseFloat(qmark)>parseFloat(topicmk)){
   document.getElementById("markAlert_"+i).innerHTML='<font color="red">Total Cannot be greater then '+topicmk+'</font>';
  document.getElementById('qmarks_'+i).value='';
   }else{
   document.getElementById("markAlert_"+i).innerHTML='';
   }
  }
  }
}
function chk_marks(val){
  var countm = $(".get_mt").length;
   var qmark=0;
   var topicmk=document.getElementById('hid_topic_mk').value;
   for(var i=1;i<=countm; i++){
    //alert('qmarks_'+i);
   	if((document.getElementById('qmarks_'+i).value!='')){
  var qmark=parseFloat(qmark)+parseFloat(document.getElementById('qmarks_'+i).value);
    if(parseFloat(qmark)>parseFloat(topicmk)){
   document.getElementById("markAlert_"+i).innerHTML='<font color="red">Total Cannot be greater then '+topicmk+'</font>';
  document.getElementById('qmarks_'+i).value='';
   }else{
   document.getElementById("markAlert_"+i).innerHTML='';
   }
  }
  }
}
function chkvalid1(){
var flag=0;
for(var i=1;i<=20;i++){
   if(document.getElementById('qcode_'+i).value=='' && document.getElementById('qname_'+i).value=='' && document.getElementById('qmarks_'+i).value==''){
   	flag++;
   }
}
 if(document.getElementById('assmnt_ctgries_id').value=='0'){
   alert("Please Select Category");
   return false;
  }else if(document.getElementById('assassmt_area').value=='0' || document.getElementById('assassmt_area').value==''){
   alert("Please Select Area");
   return false;
  }else if(document.getElementById('assassmt_topic').value=='0' || document.getElementById('assassmt_area').value==''){
   alert("Please Select Topic");
   return false;
  }
  else if(flag==0){
   alert("Please Enter At least One Question with code and masks");
   return false;
  }
  else{
   return true;
  }
}

</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($question_id!="") { ?>
     <a href="<?php echo 'index.php?route=HRModule/Master/question&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'';?>"><button type="button">Add New</button></a>
    <? } ?>
  </p>

<form action="" method="post" onsubmit="return validation123();">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">   
  <td width="13%"><strong>Employee Type</strong> <font color="#FF0000">*</font></td>
  <td width="20%">   
  <select name="comp_typ" id="comp_typ" onchange="call_area(this.value)" style="width:98%;">
    <?php if($question_id!="") { ?>
  <option value="<?php echo $comp_typ;?>"><?php echo $comp_typ;?></option>
    <?php } else {?>
  <option value="0">---Select---</option>
  <option value="Permanent">Permanent</option>
  <option value="Contract" >Contract</option>
  <?php } ?>
 </select>
</td>
<td width="13%"></td>
<td width="20%"></td>
<td width="13%"></td>
<td></td>
  </tr>
  <tr class="row1">  
	  <td><strong>Assessment Area</strong> <font color="#FF0000">*</font></td>
    <td>
	    <select name="assassmt_area" id="assassmt_area" onchange="call_topic()" style="width:98%;"><!-- onchange="generate_reqno(this.value)"-->
    
    <?php if($assmnt_area_dtls_id!=''){
    	
	   $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{?>
    <option value="0">Select</option>
<?}   ?>
	</select></td>
	  <td><strong>Assessment Topic</strong> <font color="#FF0000">*</font></td>
    <td>
	<select name="assassmt_topic" id="assassmt_topic" onchange="call_topic_marks(this.value); get_oldquestion(this.value)" style="width:98%;"><!-- onchange="generate_reqno(this.value)"-->
    
    <?php if($assmnt_topic_dtls_id!=''){
    	
	$topicde=DB_query("select * from awd_member_assmnt_topic_dtls where id='".$assmnt_topic_dtls_id."'",$db);
	while($rowde=DB_fetch_array($topicde))
	{
	?>
	<option value="<?php echo $rowde['id']; ?>" <?php echo (($rowde['id']==$assmnt_topic_dtls_id)?'selected':'')?>><?php echo $rowde['topic_name']; ?></option>
	<?php } } else {?>
  <option value="0">Select</option>
  <?php } ?>
	</select></td>
	
<?php  if($topic_mark!="") { ?>
    <input type="hidden" name="hid_topic_mk"  id="hid_topic_mk" value="<?php echo $topic_mark; ?>">
   <td><strong>Mark</strong></td>
   <td><span id="topic_mark"><?php echo $topic_mark?></span></td>
<?php } else { ?>
 <input type="hidden" name="hid_topic_mk"  id="hid_topic_mk" value="<?php //echo $topic_mark; ?>">
   <td><strong>Mark</strong></td>
   <td><span id="topic_mark"><?php //echo $topic_mark; ?></span></td>
   <?php } ?>
 
  </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>Question <span style="color:red">*</span></th>
    		<th width="15%">Marks <span style="color:red">*</span></th>
    		<th width="20%"><strong></strong></th>
		</tr>
    		<?php if($_REQUEST['action']=='Edit'){
    			$k=1;
    			//echo "SELECT * FROM awd_question_dtls WHERE question_id='".$question_id."'";
    		$sqlassde = DB_query("SELECT * FROM awd_question_dtls WHERE question_id='".$question_id."'",$db);
    		$noData = DB_num_rows($sqlassde);
    		 while($RowData = DB_fetch_array($sqlassde)){
    		 	?>
    	<tr bgcolor="#fff">
    	<td><input type="hidden" name="question_dtls_id[]" value="<?php echo $RowData['id']?>">
        <input type="text" name="ques_seq[]" readonly value="<?php echo $RowData['ques_seq']?>" style="width:98%;"></td>
    	<td><input type="text" name="ques_code[]" id="qcode_<?php echo $k?>" value="<?php echo $RowData['ques_code']?>" style="width:98%;"></td>
    	<td><input type="text" name="ques_name[]" id="qname_<?php echo $k?>" value="<?php echo $RowData['ques_name']?>" style="width:98%;"></td>
    	<td><input type="text" name="ques_marks[]" class="get_mt" id="qmarks_<?php echo $k?>" value="<?php echo $RowData['ques_marks']?>" onkeyup="chk_marks(this.value)" style="width:98%;" autocomplete="off"></td>
    	<td><span id="markAlert_<?php echo $k;?>"></span></td>
        </tr>
    		 <?php $k++;} } 
    		 if($_REQUEST['action']=='Edit' && $noData>0){
    		 	$p=$noData+1;
    		 }else{
    		 	$p=1;
    		 }?>
        <tbody id="oldyquestion">
            <?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="ques_seq[]" readonly value="<?php echo $i?>" style="width:98%;"></td>
      <td><input type="text" name="ques_code[]" id="qcode_<?php echo $i?>" value="" style="width:98%;"></td>
      <td><input type="text" name="ques_name[]" id="qname_<?php echo $i?>" value="" style="width:98%;"></td>
      <td><input type="text" name="ques_marks[]" id="qmarks_<?php echo $i;?>" value="" onkeyup="chk_marks(this.value)" style="width:98%;" autocomplete="off"></td>
      <td><span id="markAlert_<?php echo $i;?>" style="display:none; width:98%;"></span></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="ques_seq[]" readonly value="<?php echo $i?>" style="width:98%;"></td>
    	<td><input type="text" name="ques_code[]" id="qcode_<?php echo $i?>" value="" style="width:98%;"></td>
    	<td><input type="text" name="ques_name[]" id="qname_<?php echo $i?>" value="" style="width:98%;"></td>
    	<td><input type="text" name="ques_marks[]" class="get_mt" id="qmarks_<?php echo $i;?>" value="" onkeyup="chk_marks(this.value)" style="width:98%;" autocomplete="off"></td>
    	<td><span id="markAlert_<?php echo $i;?>"></span></td>
    </tr>
    	<?php } } ?>
		<tr class="row1">
    <td colspan="6" 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_question.php') ?> 
</div></div>
