<?php $PageSecurity = 15;
set_time_limit (900) ;
ini_set('session.bug_compat_42', 0);
$title = _('Monthly Attendance') ;
include('includes/fun-php.php'); 
include('includes/SQL_CommonFunctions.inc');
 
 if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);

?>


<?php


if(isset($_REQUEST['impsave']))
{
	if($_FILES['imprt']['name']!='')
	{
	//error_reporting(E_ALL);
   	$file = $_FILES['imprt']['tmp_name'];
   	$handle = fopen($file,'r');
   	$i = 0;
   	while(($row = fgetcsv($handle, 10000,",")) !== FALSE)
   	{
   		$emp_id = $s->getFieldValue('empmaster',"emp_code='".$row[0]."'",'id');
   		$site = $s->getFieldValue('locations',"locationname='".$row[2]."'",'loccode');

   		$date = explode("/", $row[3]);

   		$attendance_day = $date[0];
   		/*if($attendance_day<10)
   		{
   			$attendance_day = "0".$attendance_day;
   		}*/

   		$attendance_month = $date[1];
   		/*if($attendance_day<10)
   		{
   			$attendance_month = "0".$attendance_month;
   		}*/

   		$attendance_year = $date[2];

   		$attendance_date = $attendance_year."-".$attendance_month."-".$attendance_day;


	    $data_attendance['site']            	= $site;
	    $data_attendance['e_code']          	= $row[0];
	    $data_attendance['employee']       		= $emp_id;
	  	$data_attendance['attendance_date'] 	=	$attendance_date;
	  	$$data_attendance['created_date'] 		=	date('Y-m-d');
	  	$data_attendance['attendance_day'] 		=	$attendance_day;
	  	$data_attendance['attendance_month'] 	=	$attendance_month;
	  	$data_attendance['attendance_year'] 	=	$attendance_year;
	  	$data_attendance['pres_abs'] 			=	$row[4];

	  	$data_attendance['time_in'] 			=	$row[5];
	  	$data_attendance['time_out'] 			=	$row[6];
	  	$data_attendance['ot_hrs'] 				=	$row[7];

	  	$data_attendance['office_hours'] 		=	$row[8];
	  	$data_attendance['work_hrs'] 			=	$row[9];

	  	$data_attendance['created_by'] 			=	$_SESSION['UserID'];
	  	$data_attendance['companyId	'] 			=	$_SESSION['ACGCompanyId'];



	    if($i > 0)
   		{
   			/*echo "<pre>";
   			print_r($data_attendance);*/
   			$s->RecordInsert("daily_attendance",$data_attendance);
    	}
		$i++;
  	}
  	//die();
   	echo $msg="<br><font color='#009933' size='+1'>Attendance has been imported successfully.</font>";
    echo "<script>location.href='index.php?route=HRModule/Master/attendancebiometrix".$rurl."'</script>";

	}
} 
 ?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">

<h1>Monthly Attendance</h1>
<h4 align="center"><font color="#CC0033"><strong>*Please select a month to view attendance</strong></font></h4>
<?php
if($_REQUEST['proceed']!='')
{
 	 $s->pageLocation($target."&month=".$_REQUEST['month']."&years=".$_REQUEST['years']."&site=".$_REQUEST['site']."&empcode=".$_REQUEST['employee_code']);
}
?>




<form method="post" action="" enctype="multipart/form-data">
<table width="70%" border="0" align="center" cellpadding="3" cellspacing="0">
	<td><strong>Branch</strong></td>

<td width="35%">
							<select name="branch" id="branch">
								<option value="">Selected </option>
								<?php $locsql= DB_query("select * from locations where mst_companyid='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')",$db);
								while($locres=DB_fetch_array($locsql)) {  ?>
								<option value="<?php echo $locres['loccode']; ?>" <?php if($locres['loccode']==$_REQUEST['branch']){ echo "selected" ;} ?>><?php echo $locres['locationname']; ?></option> 
								<?php  } ?>
							</select>
						</td>
					</tr>
					<tr>
					
<tr>
	<td width="200"><strong>Import CSV File</strong></td>
	<td width="100"><input type="file" name="imprt" id="imprt" class="search"/>
	</td>
	<td align="left"><input type="submit" name="impsave" value="Save" class="search" /></td>
</tr>
<tr>
	<td colspan="3">&nbsp;</td>
</tr>
</table>
</form>




</div>
</div>
<script language="javascript" type="text/javascript">
/////////////manvi///////////////
function checkLeaves()
{

	var CL=document.getElementById("CL").value;
	var EL=document.getElementById("EL").value;
	var CS=document.getElementById("CS").value;
	var L1=0;var L2=0;var L3=0;
	var cnt=document.getElementById("rows").value;
	for(var i=0; i<cnt;i++)
	{
		var att=document.getElementById("pres_abs_"+i).value;
		if(att=='L')
		{
			var leave=document.getElementById("leave_code_"+i).value;
			if(leave=='CL'){L1++;}
			if(leave=='EL'){L2++;}
			if(leave=='cs'){L3++;}
		}
	}
	//alert("L1== "+L1);
	//alert("L2== "+L2);
	//alert("L3== "+L3);
	if(L1>CL){alert("Only "+CL+" casual leaves are remaining"); return false;}
	if(L2>EL){alert("Only "+EL+" earned leaves are remaining"); return false;}
	if(L3>CS){alert("Only "+CS+" casual leaves are remaining"); return false;}
	 //return false;
}





function check_selectedEmployee()
{
	if(document.getElementById("site").value=='0')
	{
		alert("Please Select The Branch First !");
		return false;
	}
	if(document.getElementById("employee_code").value=='')
	{
		alert("Please Select The Employee First !");
		return false;
	}
	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 chkemp=xmlhttp.responseText;
		if(chkemp=='1')
		{
			alert("You Cannot Proceed As DOJ Is Greater Than Current Month");
			return false;
		}
		else
		{
			document.myform.submit();
		}
	 //return false;
		//document.getElementById("div_id").innerHTML=xmlhttp.responseText;
	}
	}
	var current_month=parseInt(document.getElementById("month").value);
	var current_years=parseInt(document.getElementById("years").value);
	var EmpId=document.getElementById("employee_code").value;
	xmlhttp.open("GET","CheckDateOfJoiningForEmp.php?EMPID="+EmpId+"&CurrSAMonth="+current_month+"&CurrSAYear="+current_years,true);
	xmlhttp.send();
}





</script>

<script src="../js/function-ajax.js" type="text/javascript"></script>
<script type="text/javascript">
function hideDetails(str,i)
{
	chkHOday=document.getElementById("present_status_"+(i)).value;  
	
	if(str=='A')	
	{document.getElementById("leave_code_"+i).value='000';
	if((document.getElementById("present_status_"+(i+1)).value=='HO' ||document.getElementById("present_status_"+(i+1)).value=='WL') &&
	(document.getElementById("pres_abs_"+(i+2)).value=='A' ||document.getElementById("pres_abs_"+(i+2)).value=='L'))
	{
	//alert('AAAAAA');
	document.getElementById("pres_abs_"+(i+1)).value=document.getElementById("pres_abs_"+(i+2)).value;
	document.getElementById("time_in_"+(i+1)).value='';
	document.getElementById("time_out_"+(i+1)).value='';
	document.getElementById("leave_code_"+(i+1)).value='000';
	} 
	else if((document.getElementById("present_status_"+(i-1)).value=='HO' ||document.getElementById("present_status_"+(i-1)).value=='WL') &&
	(document.getElementById("pres_abs_"+(i-2)).value=='A' ||document.getElementById("pres_abs_"+(i-2)).value=='L'))
	{
	//alert('BBBBB');
	document.getElementById("pres_abs_"+(i-1)).value=document.getElementById("pres_abs_"+(i-2)).value;
	document.getElementById("leave_code_"+(i-1)).value='000';
	}
	
	document.getElementById("time_in_"+i).value='';
	document.getElementById("time_out_"+i).value='';
	document.getElementById("card_no_"+i).readOnly=true;
	document.getElementById("officework_hrs_"+i).readOnly=true;
	document.getElementById("work_hrs_"+i).readOnly=true;
	document.getElementById("ot_hrs_"+i).readOnly=true;
	document.getElementById("leave_code_"+i).readOnly=true;
	//		document.getElementById("other_official_tour_"+i).disabled=true;
	
	}
	else if(str=='L')
	{
		alert("You Can't Leave Apply From Here !");
		document.getElementById("pres_abs_"+i).value='0';
		return false;
	//
//	//alert("hiiiiii");
//	document.getElementById("leave_code_"+i).value='CL';
//	
//	if((document.getElementById("present_status_"+(i+1)).value=='HO' ||document.getElementById("present_status_"+(i+1)).value=='WL') &&
//	(document.getElementById("pres_abs_"+(i+2)).value=='A' ||document.getElementById("pres_abs_"+(i+2)).value=='L'))
//	{
//	//alert('LLLLLLLLLLL');
//	document.getElementById("pres_abs_"+(i+1)).value=document.getElementById("pres_abs_"+(i+2)).value;
//	document.getElementById("time_in_"+(i+1)).value='';
//	document.getElementById("time_out_"+(i+1)).value='';
//	document.getElementById("leave_code_"+i).value='CL';
//	} 
//	else if((document.getElementById("present_status_"+(i-1)).value=='HO' ||document.getElementById("present_status_"+(i-1)).value=='WL') &&
//	(document.getElementById("pres_abs_"+(i-2)).value=='A' ||document.getElementById("pres_abs_"+(i-2)).value=='L'))
//	{//alert('DDDDDDDDDD');
//	document.getElementById("pres_abs_"+(i-1)).value=document.getElementById("pres_abs_"+(i-2)).value;
//	document.getElementById("leave_code_"+i).value='CL';
//	//document.getElementById("leave_code_"+(i-1)).value='CL';
//	}
//	
//	document.getElementById("time_in_"+i).value='';
//	document.getElementById("time_out_"+i).value='';
//	document.getElementById("card_no_"+i).readOnly=true;
//	document.getElementById("officework_hrs_"+i).readOnly=true;
//	document.getElementById("work_hrs_"+i).readOnly=true;
//	document.getElementById("ot_hrs_"+i).readOnly=true;
//	document.getElementById("leave_code_"+i).readOnly=false;
//	
}
	else if(str=='P' )
	{
	
if(document.getElementById("present_status_"+(i+1)).value=='HO' || document.getElementById("present_status_"+(i+1)).value=='WL')
	{
	document.getElementById("pres_abs_"+(i+1)).value=document.getElementById("present_status_"+(i+1)).value;
	document.getElementById("time_in_"+(i+1)).value='';
	document.getElementById("time_out_"+(i+1)).value='';
	} 
	
	document.getElementById("time_in_"+i).value=document.getElementById("shift_start_"+i).value;
	document.getElementById("time_out_"+i).value=document.getElementById("shift_end_"+i).value;
	
	document.getElementById("card_no_"+i).readOnly=false;
	document.getElementById("shift_start_"+i).readOnly=false;
	document.getElementById("officework_hrs_"+i).readOnly=false;
	document.getElementById("work_hrs_"+i).readOnly=false;
	document.getElementById("ot_hrs_"+i).readOnly=false;
	document.getElementById("leave_code_"+i).readOnly=false;
	//		document.getElementById("other_official_tour_"+i).disabled=true;
	document.getElementById("leave_code_"+i).value='000';
	}
	else if(str=='official_tour')
	{
	document.getElementById("card_no_"+i).readOnly=true;
	document.getElementById("work_hrs_"+i).readOnly=true;
	document.getElementById("ot_hrs_"+i).readOnly=true;
	document.getElementById("leave_code_"+i).readOnly=true;
	//		document.getElementById("other_official_tour_"+i).disabled=false;
	
	}
	else if(str=='HO' || str=='WL' )
	{
	if(str=='HO')
	alert('Today is Holidays Do you want to Continue');
	if(str=='WL')
	alert('Today is Weekly Off Do you want to Continue');
	
	
	document.getElementById("time_in_"+i).value='';
	document.getElementById("time_out_"+i).value='';
	document.getElementById("card_no_"+i).readOnly=true;
	document.getElementById("officework_hrs_"+i).readOnly=true;
	document.getElementById("timein_AMPM_"+i).readOnly=true;
	document.getElementById("timeout_AMPM_"+i).readOnly=true;
	document.getElementById("shift_start_"+i).readOnly=true;
	document.getElementById("shift_end_"+i).readOnly=true;
	document.getElementById("time_in_"+i).readOnly=true;
	document.getElementById("time_out_"+i).readOnly=true;
	
	document.getElementById("work_hrs_"+i).readOnly=true;
	document.getElementById("ot_hrs_"+i).readOnly=true;
	document.getElementById("leave_code_"+i).readOnly=true;
	
	}
	else
	{
	document.getElementById("card_no_"+i).readOnly=false;
	document.getElementById("officework_hrs_"+i).readOnly=false;
	document.getElementById("timein_AMPM_"+i).readOnly=false;
	document.getElementById("timeout_AMPM_"+i).readOnly=false;
	document.getElementById("shift_start_"+i).readOnly=false;
	document.getElementById("shift_end_"+i).readOnly=false;
	document.getElementById("time_in_"+i).readOnly=false;
	document.getElementById("time_out_"+i).readOnly=false;
	document.getElementById("work_hrs_"+i).readOnly=false;
	document.getElementById("ot_hrs_"+i).readOnly=false;
	document.getElementById("leave_code_"+i).readOnly=false;
	//		document.getElementById("other_official_tour_"+i).disabled=true;
	
	}
	
	
	cur=document.getElementById("pres_abs_"+i).value;
	next=document.getElementById("pres_abs_"+(i-1)).value;
	prev=document.getElementById("pres_abs_"+(i-2)).value;  
	
	/* alert(prev);
	alert(next)	;
	alert(cur);	
	*/  	
	if((prev=='A'||prev=='L') && ( next=='WL' ||next=='HO' )  &&    (cur=='L' ||   cur=='A') )
	{
	document.getElementById("pres_abs_"+(i-1)).value=prev;
	document.getElementById("time_in_"+(i-1)).value='';
	document.getElementById("time_out_"+(i-1)).value='';
	document.getElementById("card_no_"+(i-1)).readOnly=true;
	document.getElementById("shift_start_"+(i-1)).readOnly=true;
	document.getElementById("officework_hrs_"+(i-1)).readOnly=true;
	document.getElementById("shift_end_"+(i-1)).readOnly=true;
	document.getElementById("time_in_"+(i-1)).readOnly=true;
	document.getElementById("time_out_"+(i-1)).readOnly=true;
	document.getElementById("work_hrs_"+(i-1)).readOnly=true;
	document.getElementById("ot_hrs_"+(i-1)).readOnly=true;
	document.getElementById("leave_code_"+(i-1)).readOnly=false;
	document.getElementById("leave_code_"+(i-1)).value=document.getElementById("leave_code_"+(i-2)).value;
	}
	else if((prev=='A'||prev=='L')    &&  (next=='L'|| next=='A')   &&    cur=='P' )
	{
	//document.getElementById("pres_abs_"+(i-1)).value=document.getElementById("present_status_"+(i-1)).value;
	document.getElementById("time_in_"+(i-1)).value='';
	document.getElementById("time_out_"+(i-1)).value='';
	document.getElementById("card_no_"+(i-1)).readOnly=true;
	document.getElementById("shift_start_"+(i-1)).readOnly=true;
	document.getElementById("officework_hrs_"+(i-1)).readOnly=true;
	document.getElementById("shift_end_"+(i-1)).readOnly=true;
	document.getElementById("time_in_"+(i-1)).readOnly=true;
	document.getElementById("time_out_"+(i-1)).readOnly=true;
	document.getElementById("work_hrs_"+(i-1)).readOnly=true;
	document.getElementById("ot_hrs_"+(i-1)).readOnly=true;
	document.getElementById("leave_code_"+(i-1)).readOnly=false;
	//document.getElementById("leave_code_"+(i-1)).value='000';
	}
	
}


var secondsPerMinute = 60;
var minutesPerHour = 60;

function convertSecondsToHHMMSS(intSecondsToConvert) {
var hours = convertHours(intSecondsToConvert);
var minutes = getRemainingMinutes(intSecondsToConvert);
minutes = (minutes == 60) ? "00" : minutes;
//var seconds = getRemainingSeconds(intSecondsToConvert);
return hours+":"+minutes;
}

function convertHours(intSeconds) {
var minutes = convertMinutes(intSeconds);
var hours = Math.floor(minutes/minutesPerHour);
return hours;
}
function convertMinutes(intSeconds) {
return Math.floor(intSeconds/secondsPerMinute);
}
function getRemainingSeconds(intTotalSeconds) {
return (intTotalSeconds%secondsPerMinute);
}
function getRemainingMinutes(intSeconds) {
var intTotalMinutes = convertMinutes(intSeconds);
return (intTotalMinutes%minutesPerHour);
}

function HMStoSec1(T) { // h:m:s
  var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 +  0 }
  
  
function calculate_OverTime(i,j)
{
  if(j=='WL' || j=='HO')
{
	document.getElementById("ot_hrs_"+i).value=document.getElementById("work_hrs_"+i).value;
}else{
var working=document.getElementById("time_out_"+i).value;
var office_work_hrs=document.getElementById("shift_end_"+i).value;
//var late_arrival=document.getElementById("late_arrival_"+i).value;
var time1 = HMStoSec1(working);
var time2 = HMStoSec1(office_work_hrs);
if(time1>time2)
{
	var diff = time1 - time2
	var  OTHOUR=convertSecondsToHHMMSS(diff);
	document.getElementById("ot_hrs_"+i).value=OTHOUR;
	}else
	{
	document.getElementById("ot_hrs_"+i).value=0;
	}
}	
  
//******************************************

}

function calculate_late_arrival(i)
{
	var InTime=document.getElementById("time_in_"+i).value;
	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];
	var InTime_AMPM=document.getElementById("timein_AMPM_"+i).value;
	
	var shiftTime=document.getElementById("shift_start_"+i).value;
	var shiftTimeBrk=shiftTime.split(':');
	var shiftTime_hour=shiftTimeBrk[0];
	var shiftTime_min=shiftTimeBrk[1];
	var shiftstart_AMPM=document.getElementById("shiftstart_AMPM_"+i).value;
	
	if(InTime_AMPM=='PM' && InTime_hour!='12')	
	{intimevalue=parseInt(InTime_hour)+12;}
	else if(InTime_AMPM=='AM' && InTime_hour=='12')
	{intimevalue=0;}
	else
	{intimevalue=parseInt(InTime_hour);}
	
	if(shiftstart_AMPM=='PM' && shiftTime_hour!='12')	
	{shifttimevalue=parseInt(shiftTime_hour)+12;}
	else if(shiftstart_AMPM=='AM' && shiftTime_hour=='12')
	{shifttimevalue=0;}
	else
	{shifttimevalue=parseInt(shiftTime_hour);}
	
	if(shiftstart_AMPM=='PM' && InTime_AMPM=='AM')
	{
	shifttimevalue_1=12-parseInt(shiftTime_hour);
	shifttimevalue=-(shifttimevalue_1);
	intimevalue=intimevalue;
	}
	
	if(intimevalue>shifttimevalue || intimevalue==shifttimevalue)
	{
	if(parseInt(shiftTime_min)>parseInt(InTime_min))
	{
	
	var Final_outHours=parseInt(intimevalue)-parseInt(shifttimevalue)-1;
	var intermediate_min=parseInt(shiftTime_min)-parseInt(InTime_min);
	var Final_outMin=60-parseInt(intermediate_min);
	}
	else if(parseInt(InTime_min)>parseInt(shiftTime_min))
	{
	
	var Final_outHours=parseInt(intimevalue)-parseInt(shifttimevalue);
	var Final_outMin=parseInt(InTime_min)-parseInt(shiftTime_min);
	}
	else
	if(parseInt(InTime_min)==parseInt(shiftTime_min))
	{
	var Final_outHours=parseInt(intimevalue)-parseInt(shifttimevalue);
	var Final_outMin="00";
	}
	}
	else
	{
	var Final_outHours='0';
	var Final_outMin='0';
	}
	
	var late_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
	document.getElementById("late_arrival_"+i).value=late_Hours; 
}



function calculate_workhours(i,j)
{
	var InTime=document.getElementById("time_in_"+i).value;
	var InTime_AMPM=document.getElementById("timein_AMPM_"+i).value;
	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];
	
	var OutTime=document.getElementById("time_out_"+i).value;
	var OutTime_AMPM=document.getElementById("timeout_AMPM_"+i).value;
	var OutTimeBrk=OutTime.split(':');
	var OutTime_hour=OutTimeBrk[0];
	var OutTime_min=OutTimeBrk[1];
	
	
	
	if(InTime_AMPM=='PM' && InTime_hour!='12')	
	{
		intimevalue=parseInt(InTime_hour,10)+parseInt(12,10);
	}
	else if(InTime_AMPM=='AM' && InTime_hour=='12')
	{
		intimevalue=0;
	}
	else
	{
		intimevalue=parseInt(InTime_hour);
	}
	
	if(OutTime_AMPM=='PM' && OutTime_hour!='12')	
	{
		outtimevalue=parseInt(OutTime_hour,10)+parseInt(12,10);
	}
	else if(OutTime_AMPM=='AM' && OutTime_hour=='12')
	{
		outtimevalue=0;
	}
	else
	{
		outtimevalue=parseInt(OutTime_hour);
	}
	
	
	
	if(InTime_AMPM=='PM' && OutTime_AMPM=='AM')
	{
	intimevalue_1=12-parseInt(InTime_hour);
	intimevalue=-(intimevalue_1)
	outtimevalue=outtimevalue;
	
	}
	if(InTime_AMPM=='PM' && OutTime_AMPM=='PM' )
	{
	var outtimevalue=parseInt(InTime_hour)-parseInt(OutTime_hour);
	var intimevalue=24;
	//alert(outtimevalue);alert(intimevalue);
	}
	
	if(InTime_AMPM=='AM' && OutTime_AMPM=='AM' )
	{
	
	
	if((parseInt(InTime_hour)>parseInt(OutTime_hour))){
	var outtimevalue=  parseInt(InTime_hour)-parseInt(OutTime_hour);
	}else{
	var outtimevalue= parseInt(OutTime_hour)-parseInt(InTime_hour);
	}
	
	var intimevalue=24;
	
	}
	
	
	if(parseInt(OutTime_min)>parseInt(InTime_min))
	{
	if(parseInt(intimevalue)>parseInt(outtimevalue))
	Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue) ;			  
	else			  
	Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
	
	
	
	Final_outMin=parseInt(OutTime_min)-parseInt(InTime_min);
	
	// alert(Final_outMin)
	
	
	}
	else if(parseInt(InTime_min)>parseInt(OutTime_min))
	{
	
	//alert(intimevalue);
	//alert(outtimevalue);
	
	if(parseInt(intimevalue)>parseInt(outtimevalue))
	Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue)-1;			  
	else			  
	Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
	
	//	var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
	var intermediate_min=parseInt(InTime_min)-parseInt(OutTime_min);
	var Final_outMin=60-parseInt(intermediate_min);
	}
	else if(parseInt(InTime_min)==parseInt(OutTime_min))
	{
	if(parseInt(outtimevalue)>parseInt(intimevalue)){
	var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
	var Final_outMin="00";}
	else
	{
	var Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue);
	var Final_outMin="00";
	}
	
	}	
	
	
	var Working_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
	
	
	
	if(Working_Hours!='NaN:NaN')		
	document.getElementById("work_hrs_"+i).value=Working_Hours; 
	else
	document.getElementById("work_hrs_"+i).value=0; 
	if(j=='WL') 
	{
	document.getElementById("ot_hrs_"+i).value=document.getElementById("work_hrs_"+i).value; 
	}
	
}

function calculate_workhoursNew(i,j)
{
	var InTime=document.getElementById("time_in_"+i).value;
	
	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];
	
	var OutTime=document.getElementById("time_out_"+i).value;
	
	var OutTimeBrk=OutTime.split(':');
	var OutTime_hour=OutTimeBrk[0];
	var OutTime_min=OutTimeBrk[1];
	
	
	
	if(InTime_hour!='12')	
	{
		intimevalue=parseInt(InTime_hour,10)+parseInt(12,10);
	}
	else if(InTime_hour=='12')
	{
		intimevalue=0;
	}
	else
	{
		intimevalue=parseInt(InTime_hour);
	}
	
	if(OutTime_hour!='12')	
	{
		outtimevalue=parseInt(OutTime_hour,10)+parseInt(12,10);
	}
	else if(OutTime_hour=='12')
	{
		outtimevalue=0;
	}
	else
	{
		outtimevalue=parseInt(OutTime_hour);
	}
	
	
	
	
	if(parseInt(OutTime_min)>parseInt(InTime_min))
	{
	if(parseInt(intimevalue)>parseInt(outtimevalue))
	Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue) ;			  
	else			  
	Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
	
	
	
	Final_outMin=parseInt(OutTime_min)-parseInt(InTime_min);
	
	// alert(Final_outMin)
	
	
	}
	else if(parseInt(InTime_min)>parseInt(OutTime_min))
	{
	
	//alert(intimevalue);
	//alert(outtimevalue);
	
	if(parseInt(intimevalue)>parseInt(outtimevalue))
	Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue)-1;			  
	else			  
	Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
	
	//	var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
	var intermediate_min=parseInt(InTime_min)-parseInt(OutTime_min);
	var Final_outMin=60-parseInt(intermediate_min);
	}
	else if(parseInt(InTime_min)==parseInt(OutTime_min))
	{
	if(parseInt(outtimevalue)>parseInt(intimevalue)){
	var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
	var Final_outMin="00";}
	else
	{
	var Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue);
	var Final_outMin="00";
	}
	
	}	
	
	
	var Working_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
	
	
	
	if(Working_Hours!='NaN:NaN')		
	document.getElementById("work_hrs_"+i).value=Working_Hours; 
	else
	document.getElementById("work_hrs_"+i).value=0; 
	if(j=='WL') 
	{
	document.getElementById("ot_hrs_"+i).value=document.getElementById("work_hrs_"+i).value; 
	}
	
}
function calculate_officeworkhoursNew(i)
{
    var InTime=document.getElementById("shift_start_"+i).value;
	
	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];

	var OutTime=document.getElementById("shift_end_"+i).value;
	
	var OutTimeBrk=OutTime.split(':');
	var OutTime_hour=OutTimeBrk[0];
	var OutTime_min=OutTimeBrk[1];
 		if(InTime_hour!='12')	
		{intimevalue=parseInt(InTime_hour)+12;}
		else if(InTime_hour=='12')
		{intimevalue=0;}
		else
		{intimevalue=parseInt(InTime_hour);}
		
		if(OutTime_hour!='12')	
		{outtimevalue=parseInt(OutTime_hour)+12;}
		else if(OutTime_hour=='12')
		{outtimevalue=0;}
		else
		{outtimevalue=parseInt(OutTime_hour);}
		
				
			if(parseInt(OutTime_min)>parseInt(InTime_min))
			{
				var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
				var Final_outMin=parseInt(OutTime_min)-parseInt(InTime_min);
			}
			else if(parseInt(InTime_min)>parseInt(OutTime_min))
			{
				var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
				var intermediate_min=parseInt(InTime_min)-parseInt(OutTime_min);
				var Final_outMin=60-parseInt(intermediate_min);
			}
			else
			if(parseInt(InTime_min)==parseInt(OutTime_min))
			{
				var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
				var Final_outMin="00";
			}
				 
		var Working_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
		 
		document.getElementById("officework_hrs_"+i).value=Working_Hours; 
		
	
}


function calculate_officeworkhours(i)
{
	var InTime=document.getElementById("time_in_"+i).value;
	var InTime_AMPM=document.getElementById("timein_AMPM_"+i).value;
	var InTimeBrk=InTime.split(':');
	var InTime_hour=InTimeBrk[0];
	var InTime_min=InTimeBrk[1];
	
	var OutTime=document.getElementById("time_out_"+i).value;
	var OutTime_AMPM=document.getElementById("timeout_AMPM_"+i).value;
	var OutTimeBrk=OutTime.split(':');
	var OutTime_hour=OutTimeBrk[0];
	var OutTime_min=OutTimeBrk[1];
	
	
	
	if(InTime_AMPM=='PM' && InTime_hour!='12')	
	{
		intimevalue=parseInt(InTime_hour,10)+parseInt(12,10);
	}
	else if(InTime_AMPM=='AM' && InTime_hour=='12')
	{
		intimevalue=0;
	}
	else
	{
		intimevalue=parseInt(InTime_hour);
	}
	
	if(OutTime_AMPM=='PM' && OutTime_hour!='12')	
	{
		outtimevalue=parseInt(OutTime_hour,10)+parseInt(12,10);
	}
	else if(OutTime_AMPM=='AM' && OutTime_hour=='12')
	{
		outtimevalue=0;
	}
	else
	{
		outtimevalue=parseInt(OutTime_hour);
	}
	
	
	
	if(InTime_AMPM=='PM' && OutTime_AMPM=='AM')
	{
	intimevalue_1=12-parseInt(InTime_hour);
	intimevalue=-(intimevalue_1)
	outtimevalue=outtimevalue;
	
	}
	if(InTime_AMPM=='PM' && OutTime_AMPM=='PM' )
	{
	var outtimevalue=parseInt(InTime_hour)-parseInt(OutTime_hour);
	var intimevalue=24;
	//alert(outtimevalue);alert(intimevalue);
	}
	
	if(InTime_AMPM=='AM' && OutTime_AMPM=='AM' )
	{
	
	
            if((parseInt(InTime_hour)>parseInt(OutTime_hour))){
            var outtimevalue=  parseInt(InTime_hour)-parseInt(OutTime_hour);
            }else{
            var outtimevalue= parseInt(OutTime_hour)-parseInt(InTime_hour);
            }

            var intimevalue=24;
	
	}
	
	
	if(parseInt(OutTime_min)>parseInt(InTime_min))
	{
	if(parseInt(intimevalue)>parseInt(outtimevalue))
	Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue) ;			  
	else			  
	Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
	
	
	
	Final_outMin=parseInt(OutTime_min)-parseInt(InTime_min);
	
	// alert(Final_outMin)
	
	
	}
	else if(parseInt(InTime_min)>parseInt(OutTime_min))
	{
	
	//alert(intimevalue);
	//alert(outtimevalue);
	
	if(parseInt(intimevalue)>parseInt(outtimevalue))
	Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue)-1;			  
	else			  
	Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
	
	//	var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue)-1;
	var intermediate_min=parseInt(InTime_min)-parseInt(OutTime_min);
	var Final_outMin=60-parseInt(intermediate_min);
	}
	else if(parseInt(InTime_min)==parseInt(OutTime_min))
	{
	if(parseInt(outtimevalue)>parseInt(intimevalue)){
	var Final_outHours=parseInt(outtimevalue)-parseInt(intimevalue);
	var Final_outMin="00";}
	else
	{
	var Final_outHours=parseInt(intimevalue)-parseInt(outtimevalue);
	var Final_outMin="00";
	}
	
	}	
	
	
	var Working_Hours=parseInt(Final_outHours)+":"+parseInt(Final_outMin);
	
	
	
	if(Working_Hours!='NaN:NaN')		
	document.getElementById("officework_hrs_"+i).value=Working_Hours; 
	else
document.getElementById("officework_hrs_"+i).value=0; 
	if(j=='WL') 
	{
	document.getElementById("officework_hrs_"+i).value=document.getElementById("work_hrs_"+i).value; 
	}
	
	 
	
	
}

function chk_employee()
{ 
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)
	{
	//alert(xmlhttp.responseText);
	  document.getElementById("employee_code").innerHTML=xmlhttp.responseText;
	}
	}
        var site=document.getElementById("site").value;
	var month=document.getElementById("month").value;
	var year=document.getElementById("years").value;
	
	xmlhttp.open("GET","check_data_hr.php?section=chk_employee&site="+site+"&month="+month+"&year="+year,true);
	xmlhttp.send();
}
</script>