<?php  
$table="emp_leave_register";
$table1="emp_leave";
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$status=true;
$viewrurl="index.php?route=HRModule/view_leave_request&modid=".$modid."&frmid=".$frmid."";


function leave_allowed($empid,$type,$year,$eff_date,$leaveday)
{
	   global $db;
        $tot=0;
         
         //echo $eff_date;
          $eff_date=date('Y',strtotime($eff_date));
          $pre_year=$year-1;
        //echo $calLeaRecord = "Select between_date as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and approve_status='Y' and year(approved_date_from)='".$pre_year."' group by leave_type";	   
          $calLeaveRecord = "Select sum(total_approval_days) as total from emp_leave_apply where empid='".$empid."' 
          and leave_type='".$type."'  and approve_status='Y' and year(approved_date_from)='".$pre_year."' group by leave_type";	   
	      $ResultData = DB_query($calLeaveRecord,$db);
	      $RowData = DB_fetch_array($ResultData);

       
         $privious_opening = "Select opening as total_opening from hr_leave_opening where empid='".$empid."' 
          and leave_type='".$type."'   and opening_year='".$year."' group by leave_type";	   
	      $ResultData1 = DB_query($privious_opening,$db);
	      $RowData1 = DB_fetch_array($ResultData1);
		
          if($RowData1['total_opening']==''){ $opening_leave=0;}else { $opening_leave=$RowData1['total_opening'];}

        for($p=$eff_date;$p<=$year;$p++)
        {
        $leavedays=($leavedays+$leaveday); 
        }
        
        if($eff_date<$year){
        $tot= $leavedays-$RowData['total'];
        }
        if($eff_date==$year){
         $tot= $leaveday;  
        }

        return($tot+$opening_leave);
}
function leave_calculation($empid,$type,$year)
{
	global $db;
        $calLeaveRecord = "Select sum(total_approval_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and approve_status='Y' and year(approved_date_from)='".$year."' group by leave_type";	   
	$ResultData = DB_query($calLeaveRecord,$db);
	$RowData = DB_fetch_array($ResultData);
	return($RowData['total']);
}
function leave_calculationForApplied($empid,$type,$year)
{
	global $db;
	$calLeaveRecord = "Select sum(total_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and year(period_from)='".$year."' group by leave_type  ";	   
	$ResultData = DB_query($calLeaveRecord,$db);
	$RowData = DB_fetch_array($ResultData);
	return($RowData['total']);
}

function Cancel_leave_calculation($empid,$type,$year)
{
	global $db;
	$calLeaveRecord = "Select sum(total_approval_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and approve_status='C' and year(approved_date_from)='".$year."' group by leave_type  ";	   
	$ResultData = DB_query($calLeaveRecord,$db);
	$RowData = DB_fetch_array($ResultData);
	return($RowData['total']);
}


function dateDiff1($d1, $d2)
	{
		return round(abs(strtotime($d1)-strtotime($d2))/86400)+1;
	}  

//**************INSERT RERORD HERE***************START HERE********AS*************************************************



if($_REQUEST['period_from'])
{
	
	$financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
	 $sql="Select leave_request_no from emp_leave_apply where 
	 INSTR(`leave_request_no`, '$financial') > 0 order by leave_request_no desc limit 0,1";

	$result=DB_query($sql,$db);
	$myrow=DB_fetch_array($result);
	//echo DB_num_rows($result);
	if(DB_num_rows($result)>0)
	{
	//$Reqno=$myrow['Reqno'];
	//$req_part1=substr($Reqno,-4);
	$ExplBill=explode("/",$myrow['leave_request_no']);
	$maxval = $ExplBill[3];
	//echo $req_part1;
	$req_final=$maxval+1;
	//echo "<br>".$req_final;
	}
	else
	{
	$req_final="1";
	}
	
	$string=$_REQUEST['branchCode']."/".$financial."/".'LRN/';
	$final_string=$string.str_pad($req_final,4,"0",STR_PAD_LEFT);
	
	
	
	$startdate=FormatDateForSQL($_REQUEST['period_from']);
	$enddate=FormatDateForSQL($_REQUEST['period_to']);
	$year_s = date("Y",strtotime($startdate));
	$month_s = date("m",strtotime($startdate));
	$month_e = date("m",strtotime($enddate));
	$abscl = 0;
	$workingdays=cal_days_in_month(CAL_GREGORIAN, $month_s, $year_s);
	
	
	
	
	if($month_s!=$month_e)
	{
	$emp_leave_query=DB_query("SELECT * FROM emp_leave where emp_id='".$_REQUEST['empid']."' and leave_type='".$_REQUEST['leave_type']."'",$db);
	$emp_leave_row=DB_fetch_array($emp_leave_query);
	
	$emp_bm_query=DB_query("SELECT sum(pending_cl) AS pcl FROM cl_emp where emp_id='".$_REQUEST['empid']."'",$db);
	$emp_bm_row=DB_fetch_array($emp_bm_query);
	
	$ResultEmpABSDatamm=DB_query("Select period_to, period_from, MONTH(period_to) AS tomonth, MONTH(period_from) AS frommonth from emp_leave_apply where empid='".$_REQUEST['empid']."' AND MONTH(period_from)='".$month_s."' AND YEAR(period_from)='".$year_s."'",$db);
	if(DB_num_rows($ResultEmpABSDatamm)>0){
	while($RowEmpABSDatamm = DB_fetch_array($ResultEmpABSDatamm)){
	if($RowEmpABSDatamm['tomonth']==$RowEmpABSDatamm['frommonth']){
	$offdays = strtotime($RowEmpABSDatamm['period_to']) - strtotime($RowEmpABSDatamm['period_from']);
	$abscl +=($offdays/(60 * 60 * 24))+1;
	}else if($RowEmpABSDatamm['frommonth']==$month_s){
	$offdays = $workingdays - date('j', strtotime($RowEmpABSDatamm['period_from']));
	$abscl += $offdays+1;
	}else if($RowEmpABSDatamm['tomonth']==$month_s){
	$offdays = date('j', strtotime($RowEmpABSDatamm['period_from']))-1;
	$abscl += $offdays+1;
	}
	}
	}else{
	$abscl = 0;
	}
	$f_month = (($emp_leave_row['leave_days']/12)+$emp_bm_row['pcl'])-$abscl;
	$f_mday = ($workingdays - date('j', strtotime($startdate)))+1;
	if($f_mday>$f_month)
	{
	 /*?>echo "<script>alert('You take only ".$f_month." days Leave in month of ".date('F', strtotime($startdate)).".');</script>";<?php */
	 
	 
	 
	$difference=dateDiff1($startdate,$enddate);
	
	
	for($k=0;$k<$difference;$k++)
	{
		$date2= strtotime(date("Y-m-d", strtotime($startdate)) . " +$k day");
        $date11[]= date("Y-m-d",$date2);
	}
	
	$betweendate=implode(",",$date11);
	 $_REQUEST['NoOfLeaveDays'];
	
	
	$noOfDaysAllowed=$_REQUEST['NoOfLeaveDays'];
	
	if($difference>$noOfDaysAllowed)
	{
	echo "<script>alert('You Cannot take So Many Leave');</script>";
	
	}
	else
	{
	$data['empid']=$_REQUEST['empid'];
	$data['leave_request_no']=$final_string;
	$data['period_from']=FormatDateForSQL($_REQUEST['period_from']);
	$data['period_to']=FormatDateForSQL($_REQUEST['period_to']);
	$data['total_days']=$s->FinedDays($data['period_from'],$data['period_to']);
	$data['between_date']=$betweendate;
	$data['leave_reason']=$_REQUEST['leave_reason'];
	$data['leave_type']=$_REQUEST['leave_type'];
	//echo "<pre>";
	//print_r($data);
	//exit;
	if($data['period_from']>$data['period_to'])
	{
	$msgErr[]="From date can't be greater then To date";
	$status=false;
	
	}
	
	
	
	if($status)
	{
	$insert=$s->RecordInsert('emp_leave_apply', $data);
	if(!$insert){
	die("can't insert record");
	
	}
	
	}	
	}
	
	 
	 
	}
	}
	else
	{
	$difference=dateDiff1($startdate,$enddate);
	
	
	for($k=0;$k<$difference;$k++)
	{
		$date2= strtotime(date("Y-m-d", strtotime($startdate)) . " +$k day");
        $date11[]= date("Y-m-d",$date2);
	}
	
	$betweendate=implode(",",$date11);
	
	$noOfDaysAllowed=$_REQUEST['NoOfLeaveDays'];
	
	if($difference>$noOfDaysAllowed)
	{
	echo "<script>alert('You Cannot take So Many Leave');</script>";
	
	}
	else
	{
	$data['empid']=$_REQUEST['empid'];
	$data['leave_request_no']=$final_string;
	$data['period_from']=FormatDateForSQL($_REQUEST['period_from']);
	$data['period_to']=FormatDateForSQL($_REQUEST['period_to']);
	$data['total_days']=$s->FinedDays($data['period_from'],$data['period_to']);
	$data['between_date']=$betweendate;
	$data['leave_reason']=$_REQUEST['leave_reason'];
	$data['leave_type']=$_REQUEST['leave_type'];
	//echo "<pre>";
	//print_r($data);
	//exit;
	if($data['period_from']>$data['period_to'])
	{
	$msgErr[]="From date can't be greater then To date";
	$status=false;
	
	}
	
	
	
	if($status)
	{
	$insert=$s->RecordInsert('emp_leave_apply', $data);
	if(!$insert){
	die("can't insert record");
	
	}
	
	}	
	}
	}
$s->pageLocation("index.php?route=HRModule/Leave_management".$rurl); 	
}


//**************INSERT RERORD HERE***************END HERE********AS*************************************************



//**************UPDATE RERORD HERE***************START HERE********AS*************************************************


if($_REQUEST['checkboxEdit'])
{
	
	
	
	
	
	//$betweendate=implode(",",$date11);
	for($i=0;$i<count($_REQUEST['checkboxEdit']);$i++)
	{
		
		$cnt=$_REQUEST['checkboxEdit'][$i];
		$SD=FormatDateForSQL($_REQUEST['fromdate'][$cnt]);
		$ED=FormatDateForSQL($_REQUEST['todate'][$cnt]);
		$datediffA=dateDiff1($SD,$ED);
		for($k=0;$k<$datediffA;$k++)
		{
			$date2= strtotime(date("Y-m-d", strtotime($SD)) . " +$k day");
			$date11[]= date("Y-m-d",$date2);
		}
	
		$betweendate=implode(",",$date11);
		
		
		$dataArrE['period_from']=FormatDateForSQL($_REQUEST['fromdate'][$cnt]);
		$dataArrE['period_to']=FormatDateForSQL($_REQUEST['todate'][$cnt]);
		$dataArrE['total_days']=$_REQUEST['reqleave'][$cnt];
		$dataArrE['between_date']=$betweendate;
		$dataArrE['leave_reason']=$_REQUEST['leave_reason'][$cnt];
		$cond="id='".$_REQUEST['leaveappid'][$cnt]."'";
		//echo "<pre>";
		//print_r($dataArrE);
		
		$s->RecordEdit('emp_leave_apply',$dataArrE,$cond);
	}
	//exit;
	$s->pageLocation("index.php?route=HRModule/Leave_management".$rurl); 
}

//**************UPDATE RERORD HERE***************END HERE********AS*************************************************



if($_REQUEST['Apprved'])
{
	for($m=1;$m<=count($_REQUEST['Mst_Leave_ApplyId']);$m++)
	{
		if($_REQUEST['chkApproval'][$m]==1)
		{
			$date11='';
			$ApprFDateA=FormatDateForSQL($_REQUEST['ApprovePeriodFrom'][$m]);
			$ApprTDateA=FormatDateForSQL($_REQUEST['ApprovePeriodTo'][$m]);
			$datediffA=dateDiff1($ApprFDateA,$ApprTDateA);
			for($k=0;$k<$datediffA;$k++)
			{
				$date2= strtotime(date("Y-m-d", strtotime($ApprFDateA)) . " +$k day");
				$date11[]= date("Y-m-d",$date2);
            

			}
	
			$betweendate=implode(",",$date11);
			$data['approved_date_from']=FormatDateForSQL($_REQUEST['ApprovePeriodFrom'][$m]);
			$data['approved_date_to']=FormatDateForSQL($_REQUEST['ApprovePeriodTo'][$m]);
			$data['total_approval_days']=$_REQUEST['TotalApprovalDays'][$m];
			$data['between_date']=$betweendate;
			$data['approve_status']='Y';
			
			$cond="id='".$_REQUEST['Mst_Leave_ApplyId'][$m]."'";
			//echo "<pre>";
			//print_r($data);
			$s->RecordEdit('emp_leave_apply',$data,$cond);
			
			
			$empid=$s->getField('emp_leave_apply','id',$_REQUEST['Mst_Leave_ApplyId'][$m],'empid');
			
			$sql_dailyattandance=DB_query("select * from daily_attendance where employee='".$empid."'
			                       and attendance_date between '".FormatDateForSQL($_REQUEST['ApprovePeriodFrom'][$m])."' 
								   and '".FormatDateForSQL($_REQUEST['ApprovePeriodTo'][$m])."' ",$db);
			$check_daily=DB_num_rows($sql_dailyattandance);
			if($check_daily>0)
			{          
            $update=DB_query("update daily_attendance set pres_abs='L' where attendance_date  between '".FormatDateForSQL($_REQUEST['ApprovePeriodFrom'][$m])."' 
								   and '".FormatDateForSQL($_REQUEST['ApprovePeriodTo'][$m])."' and employee='".$empid."' ",$db);
			 
		    }	
		    else
		    {
              for($k1=0;$k1<$datediffA;$k1++)
				{
				$date2= strtotime(date("Y-m-d", strtotime($ApprFDateA)) . " +$k1 day");	
				$date21=date("Y-m-d",$date2);			              
	            $update=DB_query("insert into  daily_attendance set pres_abs='L' , attendance_date ='".$date21."' 
				,employee='".$empid."',site='".$_REQUEST['empsite'][$m]."'",$db);

				}

		    }	
			
		}
	}
	$s->pageLocation("index.php?route=HRModule/leave_approval_new".$rurl);
}
	
/************************************   Cancelled  Leave  *******************************************************/
if($_REQUEST['Cancelled'])
{
	for($m=1;$m<=count($_REQUEST['Mst_Leave_ApplyId']);$m++)
	{
		if($_REQUEST['chkApproval'][$m]==1)
		{
			$date11='';
			$ApprFDateA=FormatDateForSQL($_REQUEST['ApprovePeriodFrom'][$m]);
			$ApprTDateA=FormatDateForSQL($_REQUEST['ApprovePeriodTo'][$m]);
			$datediffA=dateDiff1($ApprFDateA,$ApprTDateA);
			for($k=0;$k<$datediffA;$k++)
			{
				$date2= strtotime(date("Y-m-d", strtotime($ApprFDateA)) . " +$k day");
				$date11[]= date("Y-m-d",$date2);
			}
	
			$betweendate=implode(",",$date11);
			$data['approved_date_from']=FormatDateForSQL($_REQUEST['ApprovePeriodFrom'][$m]);
			$data['approved_date_to']=FormatDateForSQL($_REQUEST['ApprovePeriodTo'][$m]);
			$data['total_approval_days']=$_REQUEST['TotalApprovalDays'][$m];
			$data['between_date']=$betweendate;
			$data['approve_status']='C';
			
			$cond="id='".$_REQUEST['Mst_Leave_ApplyId'][$m]."'";
			
			$s->RecordEdit('emp_leave_apply',$data,$cond);
			
			
		}
	}
	
//$s->pageLocation("index.php?route=HRModule/Leave_management".$rurl); 
}


?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Employee Leave Management</h1>

<div style="width:100%; ">
	<?php  	
	include("LeaveApproval.php");
	?>
</div>
</div>
</div>


<div style="display:none" id="div_id">

</div>





<div style="display:none" id="Apply_div">
<form method="post" id="myform" onsubmit="return validateadd()">
<input type="hidden" name="branchCode" id="branchCode"  />
<div id="fetchdata"></div>		
<table width="100%" border="0" align="right" cellspacing="0" cellpadding="3">
<tr>
	<td width="30%" align="right">Leave Type</td>
	<td id="LTYPE"><input type="hidden" name="leave_type" id="leave_type" value="<?php echo $leave_row['id']; ?>" />
	<input type="text" name="leave_type1" id="leave_type1" value="<?php echo $leave_row['leavename']; ?>" /></td>
</tr> 
<tr>
	<td align="right">Leave Period From</td>
	<td><input type="text" name="period_from" id="period_from" class="datepicker"  onchange="return LeaveJoin(this.value,this.id,<?php echo $_REQUEST['year']?>);"></td>
</tr>
<tr>
	<td align="right">Leave Period To</td>
	<td><input type="text" name="period_to" id="period_to" class="datepicker"  onchange="return LeaveTypeValue(<?php echo $_REQUEST['year']?>);" ></td>
</tr>
<tr>
	<td align="right">Leave Request</td>
	<td><input type="text" name="reqleave" id="reqleave" readonly="true"></td>
</tr>
<tr>
	<td align="right">Max Leave Allow</td>
	<td><input type="text" name="NoOfLeaveDays" id="NoOfLeaveDays" readonly="true"></td>
</tr>
<tr>
	<td align="right">Reason of Leave</td>
	<td><textarea name="leave_reason"> <?php echo $leave_reason; ?></textarea></td>
</tr> 
</table>
</form>
</div>


<div id="div_help_leave" style="display:none;"></div>
<style>
.main-dialog-class .ui-widget-header{background: url("/Images/your-background.png") repeat-x scroll 34px 42px #a4cf50;font-size:14px;border:0;text-transform:uppercase}
.main-dialog-class .ui-widget-content{background-image:none;background-color:#fff}
.ViewLeaveIcon
 {

  background-image:url(images/view-leave1.png);
  width:103px;
  height:31px;
  border-width:0;
  background-color: transparent;
  cursor: pointer;
 }
 
 .ApplyLeaveIcon
 {

  background-image:url(images/apl.png);
  width:103px;
  height:31px;
  border-width:0;
  background-color: transparent;
  cursor: pointer;
 }
</style>

<script type="text/javascript" language="javascript">
$.noConflict();

$(document).ready(function(){	
		$("#helpleave").click(function(){
			$("#div_help_leave").dialog({
				title:'Help',
				modal: true,
				resizable: false,
				width:400,
				height:300,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				buttons: {
					Exit: function()
					{
						$(this).dialog("close");
					}
				}
			});
		});
		
		
		
		
	
	});


$(document).ready(function(){	
		//var cnt=document.getElementById("count").value;
		$('input[name=viewleaveA]').click(function(){
		var tt=this.id;
		var tt1=tt.split("_");
		var tt2=tt1[1];
		var EmpId=document.getElementById("empId_"+tt2).value;
		var LeaveType=document.getElementById("LeaveType_"+tt2).value;
			$("#div_id").dialog({
				title:'Applied Leave Details',
				modal: true,
				resizable: false,
				width:910,
				height:350,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				dialogClass: 'main-dialog-class',
				buttons: [
        {
            id: "button-Update",
            text: "Update",
            click: function() {
                $("#editdatafor").closest("form").submit();
            }
        },
        {
            id: "button-Exit",
            text: "Exit",
            click: function() {
                $(this).dialog("close");
            }
        }
    ]
				
			});
		$("#button-Update").hide();
		ViewappliedLeave(EmpId,LeaveType);
		});
		
	
	});
	



function ViewappliedLeave(EmpId,LeaveType)
{

	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	 
		document.getElementById("div_id").innerHTML=xmlhttp.responseText.split("__");
		
	}
	}
	xmlhttp.open("GET","view_leave_request_new.php?EMPID="+EmpId+"&LT="+LeaveType,true);
	xmlhttp.send();
}



$(document).ready(function(){	
		//var cnt=document.getElementById("count").value;
		$('input[name=LeaveApplly]').click(function(){
		var tt=this.id;
		var tt1=tt.split("_");
		var tt2=tt1[1];
		var EmpId=document.getElementById("empId_"+tt2).value;
		var LeaveType=document.getElementById("LeaveType_"+tt2).value;
		$("#Apply_div").dialog({
				title:'Apply For Leave',
				modal: true,
				resizable: false,
				width:500,
				height:300,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				dialogClass: 'main-dialog-class',
				buttons: {
					Save: function()
					{
						$("#myform").closest("form").submit();
					},
					Exit: function()
					{
						$(this).dialog("close");
					}
				}
				
			});
		ApplyForLeave(EmpId,LeaveType);
		});
	});


function ApplyForLeave(EmpId,LeaveType)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{

		var data=xmlhttp.responseText.split("__");
		document.getElementById("fetchdata").innerHTML=data[0];
		document.getElementById("LTYPE").innerHTML=data[1];
	}
	}
	xmlhttp.open("GET","leave_request_new.php?EMPID="+EmpId+"&LT="+LeaveType,true);
	xmlhttp.send();
}








function LeaveJoin(str,strid,year)
{
	var today = new Date();
//var dd = today.getDate();
//var mm = today.getMonth()+1; //January is 0!

var CurrentYear= today.getFullYear();

//if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = dd+'/'+mm+'/'+yyyy;

			if(str=="")
			{
				alert("Please enter leave period from.");
				document.getElementById(strid).focus();
				return false;
			}
			var doj = document.getElementById("doj").value;
			if(doj=="")
			{
				alert("You not take leave. Please enter date of joining in Employee Master.");
				document.getElementById(strid).value="";
				document.getElementById(strid).focus();
				return false;
			}
			
			
			
			var f = str.split("/");
			var d = doj.split("/");
			
			var x=new Date();
			x.setFullYear(d[2],d[1]-1,d[0]);
			var y = new Date();
			y.setFullYear(f[2],f[1]-1,f[0]);
			
			if(year==f[2])
			{
			if (x>y)
  			{
  				alert("You not take leave before joining date. Your joining date is "+d[0]+"/"+d[1]+"/"+d[2]);
				document.getElementById(strid).value="";
				document.getElementById(strid).focus();
				return false;
  			}
			return true;
			}
			else
			{
				alert("You Can Apply Leave Only For Selected Year "+year);
				document.getElementById(strid).value="";
				//document.getElementById(strid).focus();
				return false;
			}
}
	
	
	
function LeaveJoinEdit(str,strid)
{
			var idA=strid.split("_");
			var i=idA[1];
			if(str=="")
			{
				alert("Please enter leave period from.");
				document.getElementById(strid).focus();
				return false;
			}
			
			var doj = document.getElementById("dojAE").value;
			if(doj=="")
			{
				alert("You not take leave. Please enter date of joining in Employee Master.");
				document.getElementById(strid).value="";
				document.getElementById(strid).focus();
				return false;
			}
			
			
			
			var f = str.split("/");
			var d = doj.split("/");
			
			var x=new Date();
			x.setFullYear(d[2],d[1]-1,d[0]);
			var y = new Date();
			y.setFullYear(f[2],f[1]-1,f[0]);
			
			
			if (x>y)
  			{
  				alert("You not take leave before joining date. Your joining date is "+d[0]+"/"+d[1]+"/"+d[2]);
				document.getElementById(strid).value="";
				document.getElementById(strid).focus();
				return false;
  			}
			
		/*if(document.getElementById("todate_"+i).value!='')
		{
			var date_to = document.getElementById("todate_"+i).value;
		}	
		
		if(document.getElementById("fromdate_"+i).value!='')
		{
			var date_from = document.getElementById("fromdate_"+i).value;
		}
	
	
		var FD=date_from.split("/");
		var x1=new Date();
		x1.setFullYear(FD[2],FD[1]-1,FD[0]);
		
		var TD = date_to.split("/");
		var y1 = new Date();
		y1.setFullYear(TD[2],TD[1]-1,TD[0]);
		if(x1>y1)
		{
			alert("Leave Period From Can't Less Than Leave Period To");
			document.getElementById("fromdate_"+i).value=document.getElementById("staticfrmdate_"+i).value;
			return false;
		}
		*/
		
		
			
			
			return true;
		
}	
			
function LeaveTypeValue(year)
{

	var today = new Date();
	var CurrentYear= today.getFullYear();
	var doj = document.getElementById("doj").value;
	if(document.getElementById("leave_type").value!='')
	{
		var str = document.getElementById("leave_type").value;
	}
	if(document.getElementById("period_from").value!='')
	{
		var date_from = document.getElementById("period_from").value;
	}
	else
	{
		alert("First Fill The Leave From Date !");
		document.getElementById("period_to").value='';
		return false;
	}
	
	
	if(document.getElementById("period_to").value!='')
	{
		var date_to = document.getElementById("period_to").value;
	}
	
	
	
	var FD = date_from.split("/");
	var TD = date_to.split("/");
	
	var x1=new Date();
	x1.setFullYear(FD[2],FD[1]-1,FD[0]);
	var y1 = new Date();
	y1.setFullYear(TD[2],TD[1]-1,TD[0]);
	
	
	if(year!=TD[2])
	{
		alert("You Can Apply Leave Only For Selected Year "+year);
		document.getElementById("period_to").value='';
		//document.getElementById(strid).focus();
		return false;
	}
	
	
	if(x1>y1)
	{
		alert("Leave Period From Can't Less Than Leave Period To");
		document.getElementById("period_from").value='';
		document.getElementById("period_to").value='';
		document.getElementById("reqleave").value='';
		document.getElementById("NoOfLeaveDays").value='';
		//document.getElementById("period_from").focus();
		return false;
	}
	if(window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		
		var data=xmlhttp.responseText.split("__");
		if(data[2]=='1')
		{
			alert("This Date Not Allowed For Leave Because You have Already Taken Some Leave Between These Date");
			document.getElementById("period_from").value='';
			document.getElementById("period_to").value='';
			return false;
		}
		else if(data[0]>data[1])
		{
			alert("Max Limit Exceeds");
			document.getElementById("period_from").value='';
			document.getElementById("period_to").value='';
			return false;
		}
		else
		{
			document.getElementById("reqleave").value=data[0];
			document.getElementById("NoOfLeaveDays").value=data[1];
		}
	}
	
	}
	var empid=document.getElementById("empid").value;
	xmlhttp.open("GET","getNoOfDays.php?q="+str+"&empid="+empid+"&dfrom="+date_from+"&dto="+date_to+"&doj="+doj,true);
	xmlhttp.send();
}




function LeaveTypeValueEdit(i)
{
	var doj = document.getElementById("dojAE").value;
	if(document.getElementById("leave_typeEdit").value!='')
	{
		var str = document.getElementById("leave_typeEdit").value;
	}
	
	if(document.getElementById("fromdate_"+i).value!='')
	{
		var date_from = document.getElementById("fromdate_"+i).value;
	}
	
	
	if(document.getElementById("todate_"+i).value!='')
	{
		var date_to = document.getElementById("todate_"+i).value;
	}
	
	
	var FD = date_from.split("/");
	var TD = date_to.split("/");
	
	var x1=new Date();
	x1.setFullYear(FD[2],FD[1]-1,FD[0]);
	var y1 = new Date();
	y1.setFullYear(TD[2],TD[1]-1,TD[0]);
	
	if(x1>y1)
	{
		alert("Leave Period From Can't Less Than Leave Period To");
		document.getElementById("fromdate_"+i).value=document.getElementById("staticfrmdate_"+i).value;
		document.getElementById("todate_"+i).value=document.getElementById("statictodate_"+i).value;
		//document.getElementById("period_from").focus();
		return false;
	}
	
	
	
	var editid=document.getElementById("leaveappid_"+i).value;
	
	var preleavereq=document.getElementById("reqleave1_"+i).value;
	
	
	
	if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//alert(xmlhttp.responseText);
			var data=xmlhttp.responseText.split("__");
			if(data[2]=='1')
			{
				alert("This Date Not Allowed For Leave Because You have Already Taken Some Leave Between These Date");
				document.getElementById("fromdate_"+i).value=document.getElementById("staticfrmdate_"+i).value;
				document.getElementById("todate_"+i).value=document.getElementById("statictodate_"+i).value;
				return false;
			}
			else
			{
				document.getElementById("reqleave_"+i).value=data[0];
				document.getElementById("NoOfLeaveDaysEdit").value=data[1];
			}
		}
		
	}
	var empid=document.getElementById("empidEdit").value;
	xmlhttp.open("GET","getNoOfDaysEdit.php?q="+str+"&empid="+empid+"&dfrom="+date_from+"&dto="+date_to+"&doj="+doj+"&Editid="+editid+"&pevlevereq="+preleavereq,true);
	xmlhttp.send();
}


function changestatus(i)
{
	var count=document.getElementById("cout").value;
	for(var k=0;k<count;k++)
	{
	if(k==i)
	{
		if(document.getElementById("checkboxEdit_"+i).checked==true)
		{
			$(function() {
		$( "#fromdate_"+i).datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
		$( "#todate_"+i).datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
 		 });
			document.getElementById("fromdateval_"+i).style.display='none';
			document.getElementById("fromdate_"+i).type='text';
			document.getElementById("todateval_"+i).style.display='none';
			document.getElementById("todate_"+i).type='text';
			document.getElementById("reqleaveEdit_"+i).style.display='none';
			document.getElementById("reqleave_"+i).type='text';
			document.getElementById("empleaveEditReason_"+i).style.display='none';
			document.getElementById("leave_reason_"+i).type='text';
			document.getElementById("checkboxEdit_"+k).disabled='';
			$("#button-Update").show();
		}
		else
		{
			
			document.getElementById("fromdateval_"+i).style.display='block';
			document.getElementById("fromdate_"+i).type='hidden';
			document.getElementById("todateval_"+i).style.display='block';
			document.getElementById("todate_"+i).type='hidden';
			document.getElementById("reqleaveEdit_"+i).style.display='block';
			document.getElementById("reqleave_"+i).type='hidden';
			document.getElementById("empleaveEditReason_"+i).style.display='block';
			document.getElementById("leave_reason_"+i).type='hidden';
			document.getElementById("checkboxEdit_"+k).disabled='';
			$("#button-Update").hide();
			for(var k=0;k<count;k++)
			{
				if(document.getElementById("approvalStatus_"+k).value!='Y')
				{
				document.getElementById("checkboxEdit_"+k).disabled='';
				}
			}
			
		}
	}
	else
	{
		document.getElementById("checkboxEdit_"+k).disabled='disabled';
	}
	}
}


function validateFormEdit()
{
	var count=document.getElementById("cout").value;
	var counter=0;
	for(var i=0;i<count;i++)
	{
		var chkbox=document.getElementById("checkboxEdit_"+i).checked;
		if(chkbox==true)
		{
			counter=1;
		}
	}
	if(counter=='0')
	{
		alert("Please Ckeck At Least One Leave For Update !");
		return false;
	}
}

function validateadd()
{
	if(document.getElementById("period_from").value=='')
	{
		alert("Please Select The From Date For Leave !");
		document.getElementById("period_from").focus();
		return false;
	}
	if(document.getElementById("period_to").value=='')
	{
		alert("Please Select The To Date For Leave !");
		document.getElementById("period_to").focus();
		return false;
	}
	var reqleave=document.getElementById("reqleave").value;
	var noofpendingleave=document.getElementById("NoOfLeaveDays").value;
	if(noofpendingleave<0)
	{
		alert("You Can't Take Leave For These Days Because You have Not More Leave");
		document.getElementById("period_from").value='';
		document.getElementById("period_to").value='';
		document.getElementById("reqleave").value='';
		document.getElementById("NoOfLeaveDays").value='';
		document.getElementById("period_from").focus();
		return false;
	}
}

function fillbranch(str)
{
	document.getElementById("branchCode").value=str;
}





</script>