<?php
$PageSecurity = 15;
if(isset($_REQUEST['save']))
{
   
  $pre=$s->getField('prefix_tbl','prefix_name','Profile Master','prefix_code');
  //print_r($_SESSION);
  $strt_yr= substr($_SESSION['financialYrStart'],2,2);
  $end_yr=substr($_SESSION['financialYrEnd'],2,2);
  $str= substr($_REQUEST['branch'],0,3);
   $yr=$strt_yr."-".$end_yr;
   $str=$pre.'/'.substr($_REQUEST['branch'],0,3).'/'.$yr;
 $res=DB_fetch_array(DB_query("select id from hr_profile_master ORDER BY  id desc limit 1",$db));
 $finalstr='';
  if($res['id']=="")
      $finalstr=$pre.'/'.substr($_REQUEST['branch'],0,3).'/'.$yr.'/1';
      else
      {$cntr= $res['id']+1;   $finalstr=$pre.'/'.substr($_REQUEST['branch'],0,3).'/'.$yr.'/'.$cntr;}
      
     $data=explode('**',$_REQUEST['designation']);
     
   DB_query("insert into hr_profile_master(branch,department,designation,job_type,profile_code,pdate,profile_description,valid_from,valid_to,created_by,created_date,companyid) value ('".$_REQUEST['branch']."','".$data[1]."','".$data[0]."','".$_REQUEST['job_type']."','".$finalstr."','".FormatDateForSQL($_REQUEST['pdate'])."','".$_REQUEST['profile_desc']."','".FormatDateForSQL($_REQUEST['validfrom'])."','".FormatDateForSQL($_REQUEST['validto'])."','".$_SESSION['UserID']."','".date('Y-m-d')."','".$_SESSION['ACGCompanyId']."') ",$db);
  $last=DB_Last_Insert_ID($db, hr_profile_master, id);
foreach($_REQUEST['profile_value'] as $key=>$val)
    {
  $profile_str=implode(',',$_REQUEST['profile_value'][$key]);
 $type_str=implode(',',$_REQUEST['type'][$key]);
DB_query("insert into hr_profile_master_details(masterid,criteria_form_id,value,box_type)value('".$last."','".$key."','".$profile_str."','".$type_str."')",$db);
}
$s->pageLocation("index.php?route=HRModule/profile_master_index".$rurl);  
      }

?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<form method="post" action="" onsubmit="javascript:return validate()">
<h1><strong>Profile Master</strong></h1>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr class="row1">
    <td width="13%" style="text-align:right;"><strong>Branch</strong></td>
    <td width="20%"><select name="branch" id="branch" style="width:98%;">
        <option value="">Select</option>
	<?php
		$loc_sql = "SELECT * FROM locations WHERE loccode in ('".$_SESSION['AssignedBranch']."')";
		$loc_result = DB_query($loc_sql,$db);
		while($locRow = DB_fetch_array($loc_result)){
	?>
		<option value="<?php echo $locRow['loccode']?>" <?php echo ($locRow['loccode']==$_POST['branch']) ?"selected" :"" ;?>><?php echo $locRow['locationname']?></option>
	<?php }  ?>
      </select></td>
    <td width="13%" style="text-align:right;"><strong>Profile Code</strong></td>
    <td width="20%"><input type="text" value="Auto Generate" readonly  style="width:98%;" /></td>        
	  <td width="13%" style="text-align:right;"><strong>Date</strong></td>
	  <td><input type="text" value="<?php echo date('d/m/Y')?>" name="pdate" readonly  class="datepicker" style="width:98%;" /> </td>
</tr>
<tr class="row1">
    <td style="text-align:right;"><strong>Profile Description</strong></td>
    <td><textarea name="profile_desc"  id="profile_desc" style="width:99%; height:60px;"></textarea></td>
    <td style="text-align:right;"><strong>Valid From</strong></td>
    <td><input type="text" value="<?php echo date('d/m/Y')?>" readonly name="validfrom" class="datepicker" style="width:98%;"></td>        
	  <td style="text-align:right;"><strong>To</strong></td>
	  <td><input type="text" value="" readonly name="validto" class="datepicker" style="width:98%;"></td>
</tr>
<tr class="row1">
    <td style="text-align:right;"><strong>Designation</strong></td>
	  <td><select name="designation" type="text" id="designation" style="width:98%;" onchange="javascript:getdepartment(this)" >
          <option value="">Select</option>
	<?php
		$dept_sql = "SELECT id,designame,mst_departmentid FROM desigmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."'";
		$dept_result = DB_query($dept_sql,$db);
		while($deptRow = DB_fetch_array($dept_result)){
	?>
		<option value="<?php echo $deptRow['id']."**".$deptRow['mst_departmentid'] ; ?>" <?php echo ($deptRow['id']==$_POST['designation']) ?"selected" :"" ;?>><?php echo $deptRow['designame']?></option>
	<?php } ?>
	</select></td>
	<td style="text-align:right;"><strong>Department</strong></td>
	<td><input name="dept" readonly type="text" id="dept" value="<?php echo $_POST['desigcode']?>" style="width:98%;"/></td>
  <td style="text-align:right;"><strong>Job Type</strong></td>
  <td>&nbsp;&nbsp;Permanent <input name="job_type" type="radio" onclick="javasscript:getparameter(this);" id="job_type" value="p" /> &nbsp;Contract <input name="job_type" type="radio" id="job_type1" value="c"  onclick="javasscript:getparameter(this);" /></td>
</tr>
<tr class="row1">
  <td colspan="6" align="center"><input type="button" name="save" value="Save" class="search" /></td>
  </tr>
</table>
<br>
<style type="text/css">
#amc-tab{}
  #amc-tab table{border:none; }
  #amc-tab table tr td{border-right:#808080 1px solid; border-bottom:#808080 1px solid;}
  #amc-tab table tr td:last-child{border-right:none;}
  #amc-tab table tr:last-child td{border-bottom:none;}
  #amc-tab table tr td > table tr td{border-right:#808080 1px solid; border-bottom:#808080 1px solid;}
  #amc-tab table tr td > table tr td:last-child{border-right:none;}
  #amc-tab table tr td > table tr:last-child td{border-bottom:none;}
</style>
<div id="all_data">
   </div> 
    
</form>
<?php
include('includes/footer.inc');
?>
<style type="text/css" >
.button{ background-color:#4D767D;
color: #FFFFFF;
height:25px;
margin:0px 5px 0px 5px;
border:1px solid #000000;
}
.link{ color:#9900CC; font-family:"Comic Sans MS";
}
</style>

<script>
    
function validate()
{
  
   var  flag=0;
    var branch=document.getElementById("branch").value;
    if(branch=="")
    { alert("Please select the Branch");     return false;}
    if(document.getElementById("job_type").checked==false && document.getElementById("job_type1").checked==false) { alert("Please checked the job type");     return false; }
}

    function getdepartment(str)
{
   /* if(str.value=="0")
        {
            document.getElementById("dept").value="All";
        }
        else{ */
    var ajax;
    if(window.XMLHttpRequest)
    {
        ajax=new XMLHttpRequest();
    }
   else
    {
        ajax=new ActiveXObject("Microsoft.XMLHTTP");
    }
    ajax.onreadystatechange=function(){
        if(ajax.readyState==4 && ajax.status==200)
            {
               
                document.getElementById("dept").value=ajax.responseText;
            }
    }
    
   var dept=str.value.split("**");
   
    ajax.open("POST","ajax_getdepartment.php?did="+dept[1],true);
    ajax.send();
    //    }
}

    
function getparameter(str)
{
    if(document.getElementById("branch").value=="")
        {
         alert("Select the branch First ");  
         if(document.getElementById('job_type1').checked==true){
             document.getElementById('job_type1').checked=false
         }else if(document.getElementById('job_type').checked==true)
          {   
         document.getElementById('job_type').checked=false
        }
        }
    else if(document.getElementById("designation").value=="")
        {
             alert("Select the designation First ");
            if(document.getElementById('job_type1').checked==true){
             document.getElementById('job_type1').checked=false
         }else if(document.getElementById('job_type').checked==true)
          {   
         document.getElementById('job_type').checked=false
        }
        }
    else{
    var ajax;
    var branch=document.getElementById("branch").value;
    var desig=document.getElementById("designation").value.split("**");
    if(window.XMLHttpRequest)
    {
        ajax=new XMLHttpRequest();
    }
   else
    {
        ajax=new ActiveXObject("Microsoft.XMLHTTP");
    }
    ajax.onreadystatechange=function(){
        if(ajax.readyState==4 && ajax.status==200)
            {
               // alert(ajax.responseText);
                document.getElementById("all_data").innerHTML=ajax.responseText;
            }
    }
   //alert("ajax_getjobpararmeter_profile.php?branch="+branch+"&desig="+desig[0]+"&type="+str.value);
    ajax.open("POST","ajax_getjobpararmeter_profile.php?branch="+branch+"&desig="+desig[0]+"&type="+str.value,true);
    ajax.send();
    }
}

function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57) )
            return false;
         else   
          return true;
      
      }
      
function getlablefields(str,j)
{
   var detid=document.getElementById("detid_"+j).value
   var ajax;
    if(window.XMLHttpRequest)
    {
        ajax=new XMLHttpRequest();
    }
   else
    {
        ajax=new ActiveXObject("Microsoft.XMLHTTP");
    }
    ajax.onreadystatechange=function(){
        if(ajax.readyState==4 && ajax.status==200)
            {
              
                document.getElementById("labcnt_"+j).innerHTML=ajax.responseText;
            }
    }
  
    ajax.open("POST","ajax_getjobpararmeter.php?lablecount="+str.value+"&detid="+detid,true);
    ajax.send();
}
</script>