var serviceURLHR =localStorage['serviceURLHR'];
var hosturl    =localStorage['basicURL'];
//----------------------------------------------------------------------------------------------------//Listing
var ids = getUrlVars()["employees"];

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


///////////////////Auto Load Start/////////////////
$('.payroll-menu li').live('click',function(){
	$this = $(this);
	window.location.hash = $this.attr('data-page');
});
$(window).load(function(){
   	locationHashChanged();					   
});
			
//jQuery(window).hashchange( locationHashChanged );													
window.onhashchange = locationHashChanged;		 	

function locationHashChanged() {	

	hash=window.location.href.split('#')[1]	

	if(hash){
		
		$('.menu-selected').removeClass('menu-selected');			
		//var menuList = $('.payroll-menu');	
		
		if($('li[data-page="'+hash+'"]').length){
			
			//$('#light-pagination').pagination('updateItems',15);
			$('li[data-page="'+hash+'"]').addClass('menu-selected');		
			
			
			
			setTimeout(window[hash],100); // call current function	
			currentPage =hash; 
			
			
			
			$("#light-pagination").jui_pagination({
				currentPage: 1,
				visiblePageLinks: 5,
				totalPages: 103,
				onChangePage:window[hash],
			});
			
			
		}else{
			$('li[data-page="'+currentPage+'"]').addClass('menu-selected');
			if(hash){
				//window.location.hash =	currentPage;currentPage =hash; 
			}
			
			//currentPage =currentPage;
			
		}
	}else{
		window.location.hash = "Timesheet_show";
	}
}

///////////////////Auto Load End/////////////////


loadBranch();

function loadBranch(){
	$.getJSON(serviceURLHR + 'branch.php?action=load',function(data){
		branch = data.branch;
		
		$.each(branch, function(index, branch) {
									 
							$('#branch').append('<option value="'+branch.branch_id+'">'+branch.branch_name	+'</option>');
							
		});	
	});
}

loadBranchMusteroll();

function loadBranchMusteroll(){
	$.getJSON(serviceURLHR + 'branch.php?action=load',function(data){
		branch = data.branch;
		
		$.each(branch, function(index, branch) {
									 
							$('#branch_muster').append('<option value="'+branch.branch_id+'">'+branch.branch_name	+'</option>');
							
		});	
	});
}

function employeemusteroll_cpy(pagenum){ 
//alert(pagenum);
//alert(ids);

var month=$("#month_muster").val();

var year=$("#year_muster").val();
var employees=$("#employees").val();



//###########################################################################
if(ids && !employees){
	var brach;
	$.getJSON(serviceURLHR + "muster-roll-individual.php?act=getbranch&eid="+ids,function(data){
		branchid = data.emp.branchid;
		
		employeeselectbranch(branchid);
		
		$('#branch').val(branchid);
		
		setTimeout(function(){ $("#employees").val(ids) },5000);
		
	});
	var employees = ids;
}
///####################################################################

//alert(employees);
//var ids = 65;
//$("#Emp_Name").val() = employees;
if(month==null)
month=new Date().getMonth() + 1;if(year==null)year=new Date().getFullYear();
	$.ajax({
   url: serviceURLHR+"muster-roll-individual.php?page="+pagenum+"&month="+month+"&year="+year+"&employees="+employees,
   success: function(data){
	   $("#employeetimesheet" ).html(data);
   }
 });	
//pagination(pagenum);
selmon();
get_name(employees);
}

function employeemusteroll(pagenum){
	
var month=$("#month_muster").val();

var year=$("#year_muster").val();
var branch=$("#branch_muster").val();

window.location.href = "timesheet-individual.php?muster_month="+month+"&muster_year="+year+"&branch_id="+branch+"#Muster_Roll_show";
}


function employeeselectbranch(id){
	//alert(id);
	$('#employees').css("display","block");
	$.ajax({
   url: serviceURLHR+"dropdownvalueemployee.php?branchid="+id,
   dataType:"json",
   success: function(data){
	   
	   var employees = data.employees; 
	   $("#employees").html('');
	   $.each(employees,function(index,employees){
     	$("#employees").append('<option value="'+employees.e_id+'">'+employees.e_fname+'</option>');
	 })
	 
	 
   }
 });	
}




employeetimesheet(1);

function employeetimesheet(pagenum){ 
//alert(pagenum);
//alert(ids);




var month=$("#month").val();

var year=$("#year").val();
var employees=$("#employees").val();



//###########################################################################
if(ids && !employees){
	var brach;
	$.getJSON(serviceURLHR + "timesheet-individual.php?act=getbranch&eid="+ids,function(data){
		branchid = data.emp.branchid;
		
		employeeselectbranch(branchid);
		
		$('#branch').val(branchid);
		
		setTimeout(function(){ $("#employees").val(ids) },5000);
		
	});
	var employees = ids;
}
///####################################################################

//alert(employees);
//var ids = 65;
//$("#Emp_Name").val() = employees;
if(month==null)
month=new Date().getMonth() + 1;if(year==null)year=new Date().getFullYear();
	$.ajax({
   url: serviceURLHR+"timesheet-individual.php?page="+pagenum+"&month="+month+"&year="+year+"&employees="+employees,
   success: function(data){
	   $("#employeetimesheet" ).html(data);
   }
 });	
pagination(pagenum);
selmon();
get_name(employees);
}

function get_name(id){

 $.ajax({
   url: serviceURLHR+"getnames.php?action=load&employees="+id,
   dataType:"json",
   success: function(data){
	   emp_name=data.emp_name;
	   
	   $.each(emp_name,function(index,emp_name){
								
	    $("#Emp_Name" ).html(emp_name.e_fname+" ");
		})
	 
   }
 });
 //$("#Emp_Name").html(data);
}

function  pagination(pagenum)
{
var month=$("#month").val();
var year=$("#year").val();
var employees=$("#employees").val();


//var employees=65;
if(month==null)month=new Date().getMonth() + 1;if(year==null)year=new Date().getFullYear();
$.ajax({
   url: serviceURLHR+"timesheet-individualspagination.php?page="+pagenum+"&month="+month+"&year="+year+"&employees="+employees,
   success: function(data){
          $("#pagediv" ).html(data);
	 
   }
 });	
}
function gonextpage(page)
{ 
	CutlaryList(page);
}

function dropdownval(num,ing)
{
	$.ajax({
   url: serviceURLHR+"dropdownvalue.php?table=employee&field1=e_fname&field2=e_id&ingvalue="+ing,
   success: function(data){
     $("#employees").append(data);
   }
 });	
}

function Timesheet_show(){
	
	//$(".Emp_timesheet").css("display", "block");	
	$(".Muster_roll_Grid").css("display", "none");		
	$(".Emp_timesheet").css("display", "block");	
	$(".musteroll").css("display", "none");	
	window.location="timesheet-individual.php#Timesheet_show";
	
	removeCurrentPage();
	
}

function Muster_Roll_show(){
	$(".Emp_timesheet").css("display", "none");	
	$(".Muster_roll_Grid").css("display", "block");	
	$(".musteroll").css("display", "block");	
	window.location="timesheet-individual.php#Muster_Roll_show";

	removeCurrentPage();
}

function Attendance_show(){
	window.location="timesheet-employees-attendance.php#Attendance_show";
}

function Timesheetshow(){
	window.location="timesheet-individual.php#Timesheet_show";
}

function MusterRollshow(){
	window.location="timesheet-individual.php#Muster_Roll_show";
}

function timesheet_edit(){
	window.location="timesheet-adjustable-form.php#timesheet_edit";
}



function  download(pagenum)
{
	var empid = $('#employees').val();
	window.location = serviceURLHR+"downloadtimesheet.php?employees="+empid;
/*	alert(pagenum);
	$.ajax({
			type: "POST",
			url:serviceURLHR+"download.php",
			data: 'pagenum='+pagenum,
			success:function(msg){ 
			//$("#subject_div").html(msg);alert()
			//alert(msg);
				window.open('data:application/vnd.ms-excel,' + $('#dvData').html(msg));		
					}
			});*/
}

function  download_muster_roll(pagenum)
{
	var empid = $('#employees').val();
	
	var month=$("#month_muster").val();

var year=$("#year_muster").val();
var branch=$("#branch_muster").val();

//window.location.href = "timesheet-individual.php?muster_month="+month+"&muster_year="+year+"&branch_id="+branch+"#Muster_Roll_show";
	window.location = serviceURLHR+"downloadmusteroll.php?muster_month="+month+"&muster_year="+year+"&branch_id="+branch;
/*	alert(pagenum);
	$.ajax({
			type: "POST",
			url:serviceURLHR+"download.php",
			data: 'pagenum='+pagenum,
			success:function(msg){ 
			//$("#subject_div").html(msg);alert()
			//alert(msg);
				window.open('data:application/vnd.ms-excel,' + $('#dvData').html(msg));		
					}
			});*/
}
function curselmon()
{
	 var year=new Date().getFullYear();
	  var month=new Date().getMonth() + 1;
	  if(month=='01')month1='January';if(month=='02')month1='February';;if(month=='03')month1='March';if(month=='04')month1='April';if(month=='05')month1='May';
if(month=='06')month1='Jun';if(month=='07')month1='July';;if(month=='08')month1='August';if(month=='09')month1='September';if(month=='10')month1='October';
if(month=='11')month1='November';if(month=='12')month1='December';
var data=month1 +' '+ year;
 $("#selmonth").html(data);

}
function selmon(){
var month1='';
var month=$("#month").val();
var year=$("#year").val();	
if(month==null)month=new Date().getMonth() + 1;if(year==null)year=new Date().getFullYear();
if(month=='01')month1='January';if(month=='02')month1='February';;if(month=='03')month1='March';if(month=='04')month1='April';if(month=='05')month1='May';
if(month=='06')month1='Jun';if(month=='07')month1='July';;if(month=='08')month1='August';if(month=='09')month1='September';if(month=='10')month1='October';
if(month=='11')month1='November';if(month=='12')month1='December';
var data=month1 +' '+ year;
 $("#selmonth").html(data);
}
function selectmonthfn()
{ 
var obj=document.getElementById("month");  
 opt = document.createElement("option");
                opt.appendChild(document.createTextNode(value));
                opt.selected = selected;
                opt.value = '01';
                opt.text='January';
                obj.appendChild(opt);
}

