<?php 
if(isset($_REQUEST['save'])){
$pre=$s->getField('prefix_tbl','prefix_name','Vacancy Creation','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_job_created 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;} 
     DB_query("insert into hr_job_created(branch,job_type,vacancy_code,vac_date,approved_by,description,created_by,created_date,companyid) value ('".$_REQUEST['branch']."','".$_REQUEST['job_type']."','".$finalstr."','".FormatDateForSQL($_REQUEST['vdate'])."','".$_REQUEST['approved']."','".$_REQUEST['globaldesc']."','".$_SESSION['UserID']."','".date('Y-m-d')."','".$_SESSION['ACGCompanyId']."') ",$db); 
          $last=DB_Last_Insert_ID($db, hr_profile_master, id);
        
        foreach($_REQUEST['delitem'] as $key=>$val)
        {
        DB_query("insert into hr_job_created_details(parentid,designation,required,job_desc,profile)value('".$last."','".$_REQUEST['desig'][$key]."','".$_REQUEST['required'][$key]."','".$_REQUEST['job_desc'][$key]."','".$_REQUEST['profile'][$key]."')",$db);
        }
         $s->pageLocation("index.php?route=HRModule/vacancy_creation_index".$rurl);  
}   
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<form method="post" action="">
<h1><strong>Vacancy Creation</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" type="text" id="branch" style="width:98%;" onchange="javascript:ajax_get_dept(this,'branch')" >
        <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>Job Type</strong></td>
  <td width="20%">Permanent <input name="job_type" type="radio" id="job_type" onclick="javascript:ajax_get_dept(this,'jtype')" value="p" /> Contract <input name="job_type" type="radio" id="job_type1" value="c" onclick="javascript:ajax_get_dept(this,'jtype')" /></td>
  <td width="13%" style="text-align:right;"><strong>Vacancy Code</strong></td>
	<td><input type="text" name="Vacancy_no" style="width:98%;" value="Auto Generate" readonly /></td>        
</tr>
<?php // print_r($_SESSION); ?>
<tr class="row1">
  <td style="text-align:right;"><strong>Date</strong></td>
	<td><input type="text" style="width:98%;"name="vdate" value="<?php echo date('d/m/Y') ?>" id="vdate" class="datepicker" /></td>
	<td style="text-align:right;"><strong>Prepared By</strong></td>
	<td><input name="prepared" readonly type="text" id="prepared" value="<?php echo $_SESSION['UsersRealName'] ?>" style="width:98%;"/></td>
  <td style="text-align:right;"><strong>Approved By</strong></td>
  <td><input name="approved" type="text" id="approved" value="" style="width:98%;"/></td>       
</tr>
<tr class="row1">
  <td style="text-align:right;"><strong>Vacancy Desciption</strong></td>
  <td colspan="3"><textarea rows="3" cols="50" style="width:99%; height:60px;" name="globaldesc" id="globaldesc"></textarea></td>
	<td style="text-align:right;"><strong>Same for All</strong></td>
  <td><input type="checkbox" onclick="javascript:samedesc()" id="copydesc" name="copydesc" /></td>
</tr>
</table>
    <!--<div style="float:left; padding-left:  10px;" ><button type="button" onclick="javascript:addRow('vacancy_row')">Add Row</button>&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" onclick="javascript:deleteRowOrder('vacancy_row')">Delete Row</button></div>-->
    <br>
    <!--<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
  <input type="hidden" name="" value="0" id="edit_cnt" />
    <input type="hidden" name="" value="0" id="rowcount1" />-->
    <div id="det_part"></div> 
</form>
</div>
</div>
<script>
function addRow(tableID) 
{		
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var row1=document.getElementById("rowcount1").value;
	var rr=parseInt(row1)+1;
	document.getElementById("rowcount1").value=rr;
	document.getElementById("edit_cnt").value=rr;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	
	//var newCount=parseInt(rowCount)+parseInt(edit_cnt)-1;
	var newCount=parseInt(rowCount);
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	newcell.className='tabledata1';
	$('.datepicker').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	//newcell.childNodes[0].value = "";
	//var nod_id = newcell.childNodes[0].id;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	//newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+newCount;	
	//newcell.childNodes[0].name = newcell.childNodes[0].name;
	
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id =nod_id[0]+'_'+rr;
	
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id =nod_id[0]+'_'+rr;
	
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}	
}

function deleteRowOrder(tableID) 
{
	try 
	{
	var table = document.getElementById(tableID);
	//alert(table);
	var rowCount = table.rows.length;
	//alert(rowCount);
	for(var i=0; i<rowCount; i++) 
	{
	var row = table.rows[i];
	var chkbox = row.cells[0].childNodes[0];
	if(null != chkbox && true == chkbox.checked) 
	{
	if(rowCount <= 1) 
	{
	alert("Cannot delete all the rows.");
	break;
	}
	table.deleteRow(i);
	rowCount--;
	i--;
	}
	}
	document.getElementById("hiditemcount").value=(rowCount);		}
	catch(e) 
	{
	alert(e);
	}
}
function ajax_get_dept(str,type)
{   
        var type;
        if(document.getElementById("job_type1").checked ==false)
            jtype=document.getElementById("job_type").value;
        else if(document.getElementById("job_type").checked ==false)
            jtype=document.getElementById("job_type1").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)
            {
              // alert(ajax.responseText);
                document.getElementById("det_part").innerHTML=ajax.responseText;
            }
    }
   var vdate=document.getElementById("vdate").value; 
   if(type=="branch")
       {
          // alert("ajax_getdepartment_avail.php?branch="+str.value+"&vdate="+vdate);
     ajax.open("POST","ajax_getdepartment_avail.php?branch="+str.value+"&vdate="+vdate,true);
       }
    else
        {
            var branch=document.getElementById("branch").value; 
          //  alert("ajax_getdepartment_avail.php?branch="+branch+"&vdate="+vdate+"&type="+jtype);
    ajax.open("POST","ajax_getdepartment_avail.php?branch="+branch+"&vdate="+vdate+"&type="+jtype,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 remove_attr(j)
{
if(document.getElementById("delitem_"+j).checked==true) 
    {
        if(document.getElementById("copydesc").checked==true){
            document.getElementById("job_desc_"+j).value=document.getElementById("globaldesc").value;
            document.getElementById("required_"+j).removeAttribute("readonly");
        }
        else{
        document.getElementById("required_"+j).removeAttribute("readonly");
        document.getElementById("job_desc_"+j).removeAttribute("readonly");
        }
    }
else if(document.getElementById("delitem_"+j).checked==false)
    {
        document.getElementById("required_"+j).value="";
        document.getElementById("job_desc_"+j).value="";
        document.getElementById("required_"+j).setAttribute("readonly","true");
        document.getElementById("job_desc_"+j).setAttribute("readonly","true");
    }
}

function samedesc()
{ 
    var count=parseInt(document.getElementById("childcount").value);
    var flag=0;
    if(document.getElementById("copydesc").checked==true){
     for(var i=0;i<count;i++){
         if(document.getElementById("delitem_"+i).checked==true)
             { flag=1; break; }
         else
             flag=0;
     }
     if(document.getElementById("globaldesc").value==""){
         flag=2;
     }
    if(flag==0)
        {   alert("Checked Atleast Single Job");
            document.getElementById("copydesc").checked=false ; }
   if(flag==2)
          { alert("Please Enter Vacancy Description");
            document.getElementById("copydesc").checked=false ; }
    else if(flag==1)
        {
          for(var i=0;i<count;i++)
          {
              
            if(document.getElementById("delitem_"+i).checked==true)
             {
                 
              document.getElementById("job_desc_"+i).value=document.getElementById("globaldesc").value;
              document.getElementById("job_desc_"+i).setAttribute("readonly","true");
               document.getElementById("globaldesc").setAttribute("readonly","true");
             }
          }
        }
     }
     else
     {
     document.getElementById("globaldesc").removeAttribute("readonly");
         for(var i=0;i<count;i++)
          {
              if(document.getElementById("delitem_"+i).checked==true)
             {
                document.getElementById("job_desc_"+i).value="";  
                document.getElementById("job_desc_"+i).removeAttribute("readonly");
             }
          }
     }
}
</script>