<form method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
	<td width="12%" align="right">Company</td>
	<td width="35%"><select name="companyid" id="companyid" >
			<option value="">Select</option>
	<?php $SQLComp=DB_query("select * from companies where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	while($CompId=db_fetch_array($SQLComp)){?>
			<option value="<?php echo $CompId['CompanyId']; ?>"<?php echo($_REQUEST['companyid']==$CompId['CompanyId']?'selected':''); ?>><?php echo $CompId['CompanyName']; ?></option>
	<?php }?>
		</select></td>
	<td width="20%" align="right">Branch</td>
	<td><select name="branch" id="branch" >
			<option value="">Select</option>
	<?php $SQLBra=DB_query("SELECT * FROM locations where mst_companyid='".$_SESSION['ACGCompanyId']."'",$db);
	while($BranId=db_fetch_array($SQLBra)){?>
			<option value="<?php echo $BranId['loccode']; ?>"<?php echo($_REQUEST['branch']==$BranId['loccode']?'selected':''); ?>><?php echo $BranId['locationname']; ?></option>
	<?php }?>
		</select></td>
</tr>
<tr class="row1">
	<td align="right">Department</td>
	<td><select name="department" id="department" >
			<option value="">Select</option>
	<?php $SQLDept=DB_query("SELECT * FROM deptmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	while($DeptId=db_fetch_array($SQLDept)){?>
			<option value="<?php echo $DeptId['id']; ?>"<?php echo($_REQUEST['department']==$DeptId['id']?'selected':''); ?>><?php echo $DeptId['deptname']; ?></option>
	<?php }?>
		</select></td>
	<td align="right">Employee</td>
	<td><select name="employee" id="employee" >
			<option value="">Select</option>
	<?php $SQLEmp=DB_query("SELECT * FROM empmaster where CompanyId='".$_SESSION['ACGCompanyId']."' order by emp_name",$db);
	while($EmpId=db_fetch_array($SQLEmp)){?>
			<option value="<?php echo $EmpId['id']; ?>"<?php echo($_REQUEST['employee']==$EmpId['id']?'selected':''); ?>><?php echo $EmpId['emp_name']; ?></option>
	<?php  }?>
	</select></td>
</tr>
<tr class="row1">
	<td colspan="4" style="text-align:center;"><input type="submit" name="Go" value="Search" class="search" /></td>
</tr>
</table>

<p valign="top" style="text-align:right; padding-right:20px;"><input type="checkbox" name="checkall" id="checkall" onclick="getallcheck(this.id);">Check All</p>
</form>
<form method="post">
<?php 
if($_REQUEST['Go'])
{
	if($_REQUEST['companyid']!='')
	{
		$filter[]="emp.CompanyId='".$_REQUEST['companyid']."'";
	}
	if($_REQUEST['branch']!='')
	{
		$filter[]="emp.emp_site='".$_REQUEST['branch']."'";
	}
	if($_REQUEST['department']!='')
	{
		$filter[]="emp.department='".$_REQUEST['department']."'";
	}
	if($_REQUEST['employee']!='')
	{
		$filter[]="emp.id='".$_REQUEST['employee']."'";
	}
	if($filter!='')
	{
		$filter1=implode(" AND ",$filter);
		  $sqlEmpl="SELECT emp.id AS EMPId,emp.emp_name,empleave.* FROM empmaster emp
INNER JOIN emp_leave_apply empleave
ON (emp.id=empleave.empid) where $filter1 GROUP BY emp.id order by emp.emp_name";
	}
}
else
{
$sqlEmpl="SELECT emp.id AS EMPId,emp.emp_name,emp_site ,empleave.* FROM empmaster emp
INNER JOIN emp_leave_apply empleave
ON (emp.id=empleave.empid) where emp.CompanyId='".$_SESSION['ACGCompanyId']."' AND empleave.approve_status='N' GROUP BY emp.id order by emp.emp_name";
}
$DBqeEmp=DB_query($sqlEmpl,$db);
//echo $ft=DB_num_rows($DBqeEmp);
//exit;
$p=1;
while($EmpdetailsA=DB_fetch_array($DBqeEmp)){?>

	<h3>Employee Name : <?php echo $EmpdetailsA['emp_name'];?></h3>

		<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
		<tr>
			<th width="5%">S. No.</th>
			<th>Leave Request No.</th>
			<th width="12%">From</th>
			<th width="12%">To</th>
			<th width="10%">Leave Type</th>
			<th width="14%">Approve From</th>
			<th width="14%">Approve To</th>
			<th width="12%">Total Approval Days</th>
			<th width="5%">Select</th>
		</tr>
		<?php $Z=0; 
		$unappleaved=DB_query("SELECT id,leave_request_no,period_from,period_to,leave_type,total_days,from_half_day,to_half_day FROM emp_leave_apply where empid='".$EmpdetailsA['EMPId']."' 
			and approve_status='N'",$db); 
		while($LeaveEmpDetals=DB_fetch_array($unappleaved))
			{

                  
				?>
		<tr>
			<td><?php echo $Z+1; ?></td>
			<td><?php echo $LeaveEmpDetals['leave_request_no']; ?>
			<input type="hidden" name="Mst_Leave_ApplyId[<?php echo $p; ?>]" id="Mst_Leave_ApplyId" value="<?php echo $LeaveEmpDetals['id']; ?>" />
			<input type="hidden" name="empsite[<?php echo $p; ?>]" value="<?php echo $EmpdetailsA['emp_site'] ?>">
			<input type="hidden" name="hid_totDays[<?php echo $p; ?>]" id="hid_totDays_<?php echo $p; ?>" value="<?php echo $LeaveEmpDetals['total_days']; ?>" />
			</td>
			<td><?php echo ConvertSQLDate($LeaveEmpDetals['period_from']); ?>
			<input type="hidden" name="pfrom[<?php echo $p; ?>]" id="prom_<?php echo $p; ?>" value="<?php echo ConvertSQLDate($LeaveEmpDetals['period_from']);?>" />
			</td>
			<td><?php echo ConvertSQLDate($LeaveEmpDetals['period_to']); ?>
			<input type="hidden" name="pto[<?php echo $p; ?>]" id="pto_<?php echo $p; ?>" value="<?php echo ConvertSQLDate($LeaveEmpDetals['period_to']);?>" />
			</td>
			<td><?php echo $s->GetFieldValue('leavemaster',"id='".$LeaveEmpDetals['leave_type']."'",'leavename'); ?>
			<input type="hidden" name="Mst_Leave_TypeId[<?php echo $p; ?>]" id="Mst_Leave_TypeId_<?php echo $p; ?>" value="<?php echo $LeaveEmpDetals['leave_type']; ?>" />
			</td>
			<td><input type="text" name="ApprovePeriodFrom[<?php echo $p; ?>]" id="ApprovePeriodFrom_<?php echo $p; ?>" onchange="CheckFromdateForApproval(this.value,<?php echo $p; ?>)"  class="datepicker" /></td>
			<td><input type="text" name="ApprovePeriodTo[<?php echo $p; ?>]" id="ApprovePeriodTo_<?php echo $p; ?>" onchange="CheckTodateForApproval(this.value,<?php echo $p; ?>)"  class="datepicker" /></td>
			<td><input type="text" name="TotalApprovalDays[<?php echo $p; ?>]" id="TotalApprovalDays_<?php echo $p; ?>"   /></td>
			<td><input type="checkbox" name="chkApproval[<?php echo $p; ?>]" class="appr" id="chkApproval_<?php echo $p; ?>" value="1" onclick="getdetails(this.id)"  /></td>
			<input type="hidden" name="daystobeapprove[<?php echo $p; ?>]"  id="daystobeapprove_<?php echo $p; ?>" value="<?php echo $LeaveEmpDetals['total_days']; ?>">
		</tr>
		<?php $Z++;$p++;}?>
		</table>

<?php } ?>
<input type="hidden" id="count" value="<?php echo $p; ?>" />
<?php if($Z>0) {?>
<p align="center"><input type="submit" name="Apprved" value="Approve" class="search" onclick="return checkvalidate()" /> <input type="submit" name="Cancelled" value="Reject" class="search" onclick="return checkvalidate()" /></p>
<?php }?>
</form>
<script type="text/javascript" language="javascript">
$('.datepicker').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
function checkvalidate()
{
	var count=document.getElementById("count").value;
	var flag=0;
	for(var k=1;k<count;k++)
	{
		if(document.getElementById("chkApproval_"+k).checked==true)
		{
			flag=1;
			if(document.getElementById("ApprovePeriodFrom_"+k).value=='')
			{
				alert("Please Select The From Date For Approval");
				document.getElementById("ApprovePeriodFrom_"+k).focus();
				return false;
			}
			if(document.getElementById("ApprovePeriodTo_"+k).value=='')
			{
				alert("Please Select The To Date For Approval");
				document.getElementById("ApprovePeriodTo_"+k).focus();
				return false;
			}
		}
	}
	if(flag==0)
	{
		alert("Please Check At Least One For Approval !");
		return false;
	}
	
}

function CheckFromdateForApproval(fdate,id)
{
	var aapliedFromdate=document.getElementById("prom_"+id).value;
	var aapliedTodate=document.getElementById("pto_"+id).value;
	
	var AFD = aapliedFromdate.split("/");
	var ATD = aapliedTodate.split("/");
	
	var AFD1=new Date();
	AFD1.setFullYear(AFD[2],AFD[1]-1,AFD[0]);
	var ATD1 = new Date();
	ATD1.setFullYear(ATD[2],ATD[1]-1,ATD[0]);
	
	var APPRD=fdate.split("/");
	var APPRD1=new Date();
	APPRD1.setFullYear(APPRD[2],APPRD[1]-1,APPRD[0]);
	
	if(APPRD1<AFD1 || APPRD1>ATD1)
	{
		alert("You can't Take Approval From Date Less Than Applied From Date OR Greater Than Applied To Date");
		document.getElementById("ApprovePeriodFrom_"+id).value='';
		document.getElementById("ApprovePeriodTo_"+id).value='';
		return false;
	}
	
}

function CheckTodateForApproval(tdate,id)
{
	var ApprovalFromdate=document.getElementById("ApprovePeriodFrom_"+id).value;
	var aapliedTodate=document.getElementById("pto_"+id).value;
	  var hid_totDays=document.getElementById("hid_totDays_"+id).value;
	
	var APPFD = ApprovalFromdate.split("/");
	var ATD = aapliedTodate.split("/");
	
	var APPFD1=new Date();// Approval From date
	APPFD1.setFullYear(APPFD[2],APPFD[1]-1,APPFD[0]);
	var ATD1 = new Date();
	ATD1.setFullYear(ATD[2],ATD[1]-1,ATD[0]);
	
	var APPRTD=tdate.split("/");
	var APPRTD1=new Date();//Appproval To date;
	APPRTD1.setFullYear(APPRTD[2],APPRTD[1]-1,APPRTD[0]);
	

	
	if(APPRTD1<APPFD1 || APPRTD1>ATD1)
	{
		alert("You can't Take Approval To Date Less Than Approval From Date OR Greater Than Applied To Date");
		document.getElementById("ApprovePeriodTo_"+id).value='';
		return false;
	}
	else
	{
		if(hid_totDays==''){
	     var oneDay=1000*60*60*24;
		 var diff =Math.ceil((APPRTD1.getTime()-APPFD1.getTime())/oneDay);
		  var diff1=diff+1;
		 document.getElementById("TotalApprovalDays_"+id).value=diff1;
		}
		else
		{
			 document.getElementById("TotalApprovalDays_"+id).value=hid_totDays;
		}
	}
	
	
	
}

function getallcheck(str)
{
	 if(document.getElementById(""+str).checked==true)
	 { 
          $j('input.appr:checkbox').attr('checked', true);
           $j(".appr").each(function()
           {
                var q=($j(this).attr('id'));
                 getdetails(q);
           });
	 }
	 else
	 {
       $j('input.appr:checkbox').attr('checked', false);
        $j(".appr").each(function()
         {
           var q=($j(this).attr('id'));
           getdetails(q);
         });
	 }
}
function getdetails(str)
{
	
	 var id=str.split("_");
	  var apprfrom_date=$("#prom_"+id[1]).val();
	  var apprto_date=$("#pto_"+id[1]).val();
	  var tobeappr=$("#daystobeapprove_"+id[1]).val();
	 if(document.getElementById(""+str).checked==true)
	 {
         $("#ApprovePeriodFrom_"+id[1]).val(apprfrom_date);
         $("#ApprovePeriodTo_"+id[1]).val(apprto_date);
          $("#TotalApprovalDays_"+id[1]).val(tobeappr);
	 }
	 else
	 {
	 	$("#ApprovePeriodFrom_"+id[1]).val('');
        $("#ApprovePeriodTo_"+id[1]).val('');
        $("#TotalApprovalDays_"+id[1]).val('');

	 }


}
</script>