var objGetMyCalendars = null;
var CutShiftDate;
var EmployeeCode;
var EmpLeavePolicy;
var EmpEvent;

//************** Hariharan.J // 23-03-2024 ***********  Individual Team Members Selected View ********
function ajaxGetmyCalendars(ShiftDate) {
    debugger;
    if (window.ActiveXObject) {
        try {
            objGetMyCalendars = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                objGetMyCalendars = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e1) {
                objGetMyCalendars = null;
            }
        }
    } else if (window.XMLHttpRequest) {
        try {
            objGetMyCalendars = new XMLHttpRequest("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                objGetMyCalendars = new XMLHttpRequest("Microsoft.XMLHTTP");
            } catch (e1) {
                objGetMyCalendars = null;
            }
        }
    }
    if (objGetMyCalendars != null) {
        debugger;
        var k = Math.floor(Math.random() * 111);
        if (window.ActiveXObject) {
            objGetMyCalendars.onreadystatechange = Process_Response_GetMyCalendar;
            //var url = window.location.pathname.split("/").slice(-1);
            objGetMyCalendars.open("GET", "GetMyCalendarData.aspx?ShiftDate=" + ShiftDate, false);
            objGetMyCalendars.send(null);
        } else if (window.XMLHttpRequest) {
            //var url = window.location.pathname.split("/").slice(-1);
            objGetMyCalendars.open("GET", "GetMyCalendarData.aspx?ShiftDate=" + ShiftDate , false);
            objGetMyCalendars.send(null);
            objGetMyCalendars.onreadystatechange = Process_Response_GetMyCalendars();
        }
    }
    return false;
}


function Process_Response_GetMyCalendars() {

    debugger;
    if (objGetMyCalendars.readyState == 4) {
        if (objGetMyCalendars.status == 200) {

            var retval = objGetMyCalendars.responseText;
            if (retval == "") {
                $('#ctl00_BodyContentPH_txt_ShiftDates').text('');
                $('#ctl00_BodyContentPH_txt_ShiftDetailss').text('');
                $('#ctl00_BodyContentPH_txt_FirstIntimes').text('');
                $('#ctl00_BodyContentPH_txt_LastOuttimes').text('');
                $('#ctl00_BodyContentPH_txt_WorkHourss').text('');
                $('#ctl00_BodyContentPH_txt_LateHourss').text('');
                $('#ctl00_BodyContentPH_txt_EarlyExitHourss').text('');
                $('#ctl00_BodyContentPH_txt_Lunchhourss').text('');
                $('#ctl00_BodyContentPH_txt_Attendancedetailss').text('');
                $('#ctl00_BodyContentPH_lbl_ShiftDefineHourss').text('');
                $('#ctl00_BodyContentPH_txt_Presents').text('');
                $('#ctl00_BodyContentPH_txt_AbsentDayss').text('');
                $('#ctl00_BodyContentPH_txt_LeaveDayss').text('');
                $('#ctl00_BodyContentPH_txt_Holidayss').text('');
                $('#ctl00_BodyContentPH_txt_WeeklyOffDayss').text('');
                $('#ctl00_BodyContentPH_lblTotaldayss').text('');
                $('#ctl00_BodyContentPH_txt_MonthNames').text('');
            }
            else {
                if (retval.length > 0 && retval != "Fail") {
                    var arr1 = retval.split('#');
                    var arr = arr1[0].split(',');
                    var arr2 = arr1[1].split(',');
                    $('#ctl00_BodyContentPH_txt_ShiftDates').text(arr[0]);
                    $('#ctl00_BodyContentPH_txt_ShiftDetailss').text(arr[1]);
                    $('#ctl00_BodyContentPH_txt_FirstIntimes').text(arr[3]);
                    $('#ctl00_BodyContentPH_txt_LastOuttimes').text(arr[4]);
                    $('#ctl00_BodyContentPH_txt_WorkHourss').text(arr[2]);
                    $('#ctl00_BodyContentPH_txt_LateHourss').text(arr[5]);
                    $('#ctl00_BodyContentPH_txt_EarlyExitHourss').text(arr[6]);
                    $('#ctl00_BodyContentPH_txt_Lunchhourss').text(arr[7]);
                    $('#ctl00_BodyContentPH_txt_Attendancedetailss').text(arr[8]);
                    $('#ctl00_BodyContentPH_lbl_ShiftDefineHourss').text(arr[9]);
                    $('#ctl00_BodyContentPH_txt_BrackHrss').text(arr[10]);
                    $('#ctl00_BodyContentPH_txt_Presents').text(arr2[0]);
                    $('#ctl00_BodyContentPH_txt_AbsentDayss').text(arr2[1]);
                    $('#ctl00_BodyContentPH_txt_LeaveDayss').text(arr2[2]);
                    $('#ctl00_BodyContentPH_txt_Holidayss').text(arr2[3]);
                    $('#ctl00_BodyContentPH_txt_WeeklyOffDayss').text(arr2[4]);
                    $('#ctl00_BodyContentPH_lblTotaldayss').text(arr2[5]);
                    $('#ctl00_BodyContentPH_txt_MonthNames').text(arr2[6]);

                }
            }
        }
        else if (objGetMyCalendars.status == 404) {
            //var url = window.location.pathname.split("/").slice(-1);
            objGetMyCalendars.open("GET", "../GetMyCalendarData.aspx?ShiftDate=" + ShiftDate, false);
            objGetMyCalendars.send(null);
            objGetMyCalendars.onreadystatechange = Process_Response_GetMyCalendar();
        }
    }
}

function GetIndividualCalendars() {
    debugger;
    if (window.ActiveXObject) {
        try {
            GetIndividualCalendars = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetIndividualCalendars = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e1) {
                GetIndividualCalendars = null;
            }
        }
    } else if (window.XMLHttpRequest) {
        try {
            GetIndividualCalendars = new XMLHttpRequest("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetIndividualCalendars = new XMLHttpRequest("Microsoft.XMLHTTP");
            } catch (e1) {
                GetIndividualCalendars = null;
            }
        }
    }
    if (GetIndividualCalendars != null) {
        var k = Math.floor(Math.random() * 111);
        if (window.ActiveXObject) {
            GetIndividualCalendars.onreadystatechange = Process_Response_GetIndividualCalendars;
            //var url = window.location.pathname.split("/").slice(-1);
            GetIndividualCalendars.open("GET", "GetMyCalendarData.aspx?individualUser=Yes", false);
            GetIndividualCalendars.send(null);
        } else if (window.XMLHttpRequest) {
            debugger;
            //var url = window.location.pathname.split("/").slice(-1);
            GetIndividualCalendars.open("GET", "GetMyCalendarData.aspx?individualUsers=Yes", false);
            GetIndividualCalendars.send(null);
            //GetIndividualCalendar.onreadystatechange = Process_Response_GetIndividualCalendar();
            GetIndividualCalendars.onreadystatechange = Process_Response_GetIndividualCalendars();
        }
    }
    return false;
}






function Process_Response_GetIndividualCalendars() {
    debugger;
    if (GetIndividualCalendars.readyState == 4) {
        if (GetIndividualCalendars.status == 200) {

            var retval = GetIndividualCalendars.responseText;
            if (retval == "") {
                $('#calendars').fullCalendar({
                    header: {
                        right: 'prev,next today',
                        center: 'title',
                        left: ''
                    },
                    buttonText: {
                        today: 'Today',
                        month: 'month',
                        week: 'week',
                        day: 'day'
                    }
                //    ,
                //    event:
                //{
                //    title: 'Apply',
                //    start: ArrMyCalendarDay[2],
                //    icon: "child",
                //    color: '#269ac3',
                //    textColor: '#ffffff',
                //    center: true,
                //}

                });
            }
            else {
                debugger;
                if (retval.length > 0 && retval != "Fail") {
                    if (retval != "") {
                        $('#tablist a[href="#ctl00_BodyContentPH_tab3"]').tab('show');
                        $('html, body').animate({ scrollTop: 0 }, 'slow');
                        var ArrMyCalendarMonth = retval.split('%');
                        var ArrMyCalendarDay = [];
                        var DataPoints = [];
                        for (var i = 0; i < ArrMyCalendarMonth.length; i++) {
                            ArrMyCalendarDay = ArrMyCalendarMonth[i].split(',');
                            var date = ArrMyCalendarDay[2];

                            var shiftDates = ArrMyCalendarDay[2].split('#');
                            var todate;
                            if (shiftDates.length > 1) {
                                date = shiftDates[0];
                                todate = shiftDates[1];
                            }
                            else {
                                date = ArrMyCalendarDay[2];
                                todate = shiftDates[0];
                            }

                            var Datesplit = date.split('-');
                            var d = Datesplit[2],
                                m = Datesplit[1],
                                y = Datesplit[0];
                            if (ArrMyCalendarDay[5] == ArrMyCalendarDay[6]) {
                                if (ArrMyCalendarDay[5] == "Present") {
                                    debugger;
                                    DataPoints.push(

                                        {
                                            title: 'Apply',
                                            start: ArrMyCalendarDay[2],
                                            //icon: "child",
                                            color: '#269ac3',
                                            textColor: '#ffffff',
                                            center: true,
                                            tooltip: ("CLA-Pending")

                                        },
                                        {
                                            title: ArrMyCalendarDay[8],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: "arrow-circle-o-up",
                                            textColor: (ArrMyCalendarDay[10] != 0) ? '#FF5151' : "",
                                            tooltip: (ArrMyCalendarDay[10] != 0) ? "Late In : " + Math.floor(ArrMyCalendarDay[10] / 60) + " hr(s) " + ArrMyCalendarDay[10] % 60 + " min(s)" : ""
                                        },
                                        {
                                            title: ArrMyCalendarDay[9],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: "arrow-circle-o-down",
                                            textColor: (ArrMyCalendarDay[11] != 0) ? '#FF5151' : "",
                                            tooltip: (ArrMyCalendarDay[11] != 0) ? "Early Out : " + Math.floor(ArrMyCalendarDay[11] / 60) + " hr(s) " + ArrMyCalendarDay[11] % 60 + " min(s)" : ""
                                        },
                                        {
                                            title: ArrMyCalendarDay[4],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: "clock-o"
                                        },
                                        {
                                            title: ArrMyCalendarDay[5],
                                            start: ArrMyCalendarDay[2],
                                            color: '#008247',
                                            textColor: "#fff",
                                            center: true,

                                        }
                                    );
                                }
                                    //Implemented by Manikandan.R on 30-03-2018 02.27 PM
                                else if (ArrMyCalendarDay[5] == "Weekly Off") {
                                    DataPoints.push(
                                        {
                                            title: ArrMyCalendarDay[3],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                        },
                                        {
                                            title: ArrMyCalendarDay[8],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: (ArrMyCalendarDay[8] != "") ? "arrow-circle-o-up" : "",
                                        },
                                        {
                                            title: ArrMyCalendarDay[9],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: (ArrMyCalendarDay[9] != "") ? "arrow-circle-o-down" : "",
                                        },
                                        {
                                            title: ArrMyCalendarDay[4],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: (ArrMyCalendarDay[9] != "") ? "clock-o" : ""
                                        },
                                        {
                                            title: ArrMyCalendarDay[5],
                                            start: ArrMyCalendarDay[2],
                                            color: '#02749E', textColor: "#fff",
                                        });
                                }
                                else if (ArrMyCalendarDay[5] == "Holiday") {

                                    DataPoints.push(
                                        {
                                            title: ArrMyCalendarDay[3],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                        },
                                        {
                                            title: ArrMyCalendarDay[8],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: (ArrMyCalendarDay[8] != "") ? "arrow-circle-o-up" : "",
                                        },
                                        {
                                            title: ArrMyCalendarDay[9],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: (ArrMyCalendarDay[9] != "") ? "arrow-circle-o-down" : "",
                                        },
                                        {
                                            title: ArrMyCalendarDay[4],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: (ArrMyCalendarDay[9] != "") ? "clock-o" : ""
                                        },
                                        {
                                            title: ArrMyCalendarDay[5],
                                            start: ArrMyCalendarDay[2],
                                            color: '#FF7F00', textColor: "#fff",
                                        });
                                }
                                else if (ArrMyCalendarDay[5] == "OnDuty-Approve") {
                                    debugger;
                                    DataPoints.push(
                                        {
                                            title: ArrMyCalendarDay[5],
                                            start: date,
                                            end: todate,
                                            color: '#269ac3',
                                            textColor: '#ffffff',
                                            center: true,
                                            tooltip: ("OD")
                                        });
                                    //{
                                    //    title: ArrMyCalendarDay[8],
                                    //    start: date,
                                    //    end: todate,
                                    //    center: true,
                                    //    icon: (ArrMyCalendarDay[8] != "") ? "arrow-circle-o-up" : "",
                                    //},
                                    //{
                                    //    title: ArrMyCalendarDay[9],
                                    //    start: date,
                                    //    end: todate,
                                    //    center: true,
                                    //    icon: (ArrMyCalendarDay[9] != "") ? "arrow-circle-o-down" : "",
                                    //},
                                    //{
                                    //    title: ArrMyCalendarDay[4],
                                    //    start: date,
                                    //    end: todate,
                                    //    center: true,
                                    //    icon: (ArrMyCalendarDay[9] != "") ? "clock-o" : ""
                                    //},
                                    //{
                                    //    title: ArrMyCalendarDay[5],
                                    //    start: date,
                                    //    end: todate,
                                    //    color: '#D0457B', textColor: "#fff",
                                    //});
                                }

                                else if (ArrMyCalendarDay[5] == "Absent") {
                                    DataPoints.push(
                                        {
                                            title: ArrMyCalendarDay[3],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                        },
                                        {
                                            title: ArrMyCalendarDay[8],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: "arrow-circle-o-up",
                                        },
                                        {
                                            title: ArrMyCalendarDay[9],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: "arrow-circle-o-down"
                                        },
                                        {
                                            title: ArrMyCalendarDay[4],
                                            start: ArrMyCalendarDay[2],
                                            center: true,
                                            icon: "clock-o"
                                        },
                                        {
                                            title: ArrMyCalendarDay[5],
                                            start: ArrMyCalendarDay[2],
                                            color: '#FF5151',
                                            textColor: "#fff",
                                        });
                                }
                                else if (ArrMyCalendarDay[5] != null && ArrMyCalendarDay[5] != "") {
                                    debugger;
                                    DataPoints.push(
                                        {
                                            title: ArrMyCalendarDay[5],
                                            start: date,
                                            end: todate,
                                            color: '#0276e3',
                                            textColor: '#ffffff',
                                            center: true
                                            //tooltip: ("CLA-Pending")
                                        });
                                    //{
                                    //    title: ArrMyCalendarDay[8],
                                    //    start: date,
                                    //    end: todate,
                                    //    center: true,
                                    //    icon: (ArrMyCalendarDay[8] != "") ? "arrow-circle-o-up" : "",
                                    //},
                                    //{
                                    //    title: ArrMyCalendarDay[9],
                                    //    start: date,
                                    //    end: todate,
                                    //    center: true,
                                    //    icon: (ArrMyCalendarDay[9] != "") ? "arrow-circle-o-down" : "",
                                    //},
                                    //{
                                    //    title: ArrMyCalendarDay[4],
                                    //    start: date,
                                    //    end: todate,
                                    //    center: true,
                                    //    icon: (ArrMyCalendarDay[9] != "") ? "clock-o" : ""
                                    //},
                                    //{
                                    //    title: ArrMyCalendarDay[5],
                                    //    start: date,
                                    //    end: todate,
                                    //    color: '#D0457B', textColor: "#fff",
                                    //});
                                }
                                else {
                                    //DataPoints.push(
                                    //    {

                                    //        title: ArrMyCalendarDay[3],
                                    //        start: ArrMyCalendarDay[2],
                                    //        center: true,
                                    //    },
                                    //    {
                                    //        title: ArrMyCalendarDay[8],
                                    //        start: ArrMyCalendarDay[2],
                                    //        center: true,
                                    //        icon: (ArrMyCalendarDay[8] != "") ? "arrow-circle-o-up" : "",
                                    //    },
                                    //    {
                                    //        title: ArrMyCalendarDay[9],
                                    //        start: ArrMyCalendarDay[2],
                                    //        center: true,
                                    //        icon: (ArrMyCalendarDay[9] != "") ? "arrow-circle-o-down" : "",
                                    //    },
                                    //    {
                                    //        title: ArrMyCalendarDay[4],
                                    //        start: ArrMyCalendarDay[2],
                                    //        center: true,
                                    //        icon: (ArrMyCalendarDay[9] != "") ? "clock-o" : ""
                                    //    },
                                    //    {
                                    //        title: ArrMyCalendarDay[5],
                                    //        start: ArrMyCalendarDay[2],
                                    //        color: '#FF5151', textColor: "#fff",
                                    //    });
                                }
                            }
                            else {
                                DataPoints.push(

                                    {
                                        title: ArrMyCalendarDay[3],
                                        start: ArrMyCalendarDay[2],
                                        center: true,
                                    },
                                    {
                                        title: ArrMyCalendarDay[8],
                                        start: ArrMyCalendarDay[2],
                                        center: true,
                                        icon: (ArrMyCalendarDay[8] != "") ? "arrow-circle-o-up" : "",
                                    },
                                    {
                                        title: ArrMyCalendarDay[9],
                                        start: ArrMyCalendarDay[2],
                                        center: true,
                                        icon: (ArrMyCalendarDay[9] != "") ? "arrow-circle-o-down" : "",
                                    },
                                    {
                                        title: ArrMyCalendarDay[4],
                                        start: ArrMyCalendarDay[2],
                                        center: true,
                                        icon: (ArrMyCalendarDay[9] != "") ? "clock-o" : ""
                                    },
                                    {
                                        title: ArrMyCalendarDay[5].substring(0, 1) + " / " + ArrMyCalendarDay[6],
                                        start: ArrMyCalendarDay[2],
                                        //rendering: 'background',
                                        color: '#A9A9A9', textColor: "#fff",
                                    });
                            }
                        }
                    }
                    else {
                        var date = new Date();
                        var d = date.getDate(),
                            m = date.getMonth(),
                            y = date.getFullYear();
                        var DataPoints = [];
                    }
                    $('#calendars').fullCalendar({
                        header: {
                            left: 'prev,next today week',
                            center: 'title',
                            right: 'month,basicWeek,basicDay'
                        },
                        buttonText: {
                            today: 'Today',
                            month: 'Month',
                            week: 'Week',
                            day: 'Day'
                        },
                        editable: true,
                        selectable: false,
                        select: function (start, end, allDay) {
                            $('#createEventModal').modal('show');
                            $('#fromdate').val(start);
                            $('#todate').val(end);
                        }
                        //,
                        //dayClick: function (date, jsEvent, view) {
                        //    debugger;
                        //    var formatdate;
                        //    formatdate = date.format();
                        //    ajaxGetmyCalendar(formatdate);
                        //    var eventExist;
                        //    eventExist = IsDateHasEvent(formatdate);

                        //    if (eventExist == "") {
                        //        NewEventApplication(formatdate, eventExist);
                        //    }
                        //    else {
                        //        alert('Event Already Exist');
                        //        return false;
                        //    }

                        //} 
                        ,
                        eventClick: function (event) {
                            debugger;
                            var formatdate;
                            formatdate = event.start.format();
                            ajaxGetmyCalendar(formatdate);
                            var LeavePolicyId;
                            LeavePolicyId = CheckLeavePolicy(formatdate);
                            var arrPolicy = LeavePolicyId.split(',');
                            var eventExist;
                            //var eventExists;
                            eventExist = IsDateHasEvent(formatdate);
                            // eventExists = IsDateHasEvents(formatdate);
                            //var arrEvents = eventExists(',');
                            // AddLeaveEvent(formatdate,eventExist);
                            var arrEvent = eventExist.split(',');
                            $('#ctl00_BodyContentPH_hdnSelectedEmployee').val(arrPolicy[1]);
                            $('#ctl00_BodyContentPH_hdnEmployeeId').val(arrPolicy[1]);
                            $('#ctl00_BodyContentPH_hdnApplicationYear').val(arrPolicy[2]);
                            $('#ctl00_BodyContentPH_chkBorrowLeave').hide();
                            if ((LeavePolicyId != 0 || LeavePolicyId != "")) {

                                let currentDate = new Date().toJSON().slice(0, 10);

                                if ((arrPolicy[3] != "true" || arrPolicy[3] == "") && formatdate >= currentDate) {
                                    let today = new Date().toISOString().slice(0, 10);
                                    //if (formatdate >= today) {
                                    var dateFormat;
                                    if ($('#ctl00_BodyContentPH_hdnDateFormat').val() == 'dd-MMM-yyyy') {
                                        var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                                        var dformat = formatdate.split('-');
                                        dateFormat = dformat[2] + "-" + month[dformat[1] - 1] + "-" + dformat[0];
                                        $('#ctl00_BodyContentPH_txtFromDate').val(dateFormat);
                                        $('#ctl00_BodyContentPH_txtToDate').val(dateFormat);
                                    }
                                    $('#ctl00_BodyContentPH_txtLeaveDate').val(dateFormat);

                                    if (arrEvent != "") {
                                        LoadApplication(dateFormat, arrEvent);
                                    }
                                    else {
                                        LeaveApplicationAddClear(formatdate);
                                    }
                                    var modal = $("#LeaveModal");
                                    modal.modal();
                                    // AddLeaveEvent(formatdate);

                                }

                            }

                            else {
                                alert('Leave Policy not allocated');

                            }
                            return false;

                        },
                        events:
                            DataPoints,
                        eventOverlap: false,
                        eventRender: function (event, element) {
                            element.attr('title', event.tooltip);
                            if (event.icon) {
                                element.find(".fc-title").prepend("<i class='fa fa-" + event.icon + "'></i>&nbsp;&nbsp;&nbsp;");
                            }

                        }

                    });
                    $('#submitButton').on('click', function () {
                        // Clear modal inputs
                        $('.modal').find('input').val('');

                        // hide modal
                        $('.modal').modal('hide');
                    });

                }
            }
        }
        else if (GetIndividualCalendars.status == 404) {
            GetIndividualCalendars.open("GET", "../GetMyCalendarData.aspx?individualUser=Yes", false);
            GetIndividualCalendars.send(null);
            GetIndividualCalendars.onreadystatechange = Process_Response_GetIndividualCalendar();
        }
    }
}




function IsDateHasEvent(date) {
    if (window.ActiveXObject) {
        try {
            GetIndividualCalendars = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetIndividualCalendars = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e1) {
                GetIndividualCalendars = null;
            }
        }
    } else if (window.XMLHttpRequest) {
        try {
            GetIndividualCalendars = new XMLHttpRequest("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetIndividualCalendars = new XMLHttpRequest("Microsoft.XMLHTTP");
            } catch (e1) {
                GetIndividualCalendars = null;
            }
        }
    }
    if (GetIndividualCalendars != null) {
        var k = Math.floor(Math.random() * 111);
        if (window.ActiveXObject) {
            //GetIndividualCalendar.onreadystatechange = LeavePolicyId;
            //var url = window.location.pathname.split("/").slice(-1);
            GetIndividualCalendars.open("GET", "GetMyCalendarData.aspx?chkDateEvent=" + date, false);
            GetIndividualCalendars.send(null);
            EmpEvent = GetIndividualCalendars.responseText;
        } else if (window.XMLHttpRequest) {
            //var url = window.location.pathname.split("/").slice(-1);
            GetIndividualCalendars.open("GET", "GetMyCalendarData.aspx?chkDateEvent=" + date, false);
            GetIndividualCalendars.send(null);
            EmpEvent = GetIndividualCalendars.responseText;
            //GetIndividualCalendar.onreadystatechange = LeavePolicyId;
        }
    }
    return EmpEvent;
}

function NewEventApplication(formatdate, eventExist) {
    debugger;
    var LeavePolicyId;
    LeavePolicyId = CheckLeavePolicy(formatdate);
    var arrPolicy = LeavePolicyId.split(',');
    var arrEvent = eventExist.split(',');
    $('#ctl00_BodyContentPH_hdnSelectedEmployee').val(arrPolicy[1]);
    $('#ctl00_BodyContentPH_hdnEmployeeId').val(arrPolicy[1]);
    $('#ctl00_BodyContentPH_hdnApplicationYear').val(arrPolicy[2]);
    $('#ctl00_BodyContentPH_chkBorrowLeave').hide();

    if (LeavePolicyId != 0 || LeavePolicyId != "") {
        let today = new Date().toISOString().slice(0, 10);
        if (formatdate >= today) {

            var dateFormat;
            if ($('#ctl00_BodyContentPH_hdnDateFormat').val() == 'dd-MMM-yyyy') {
                var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                var dformat = formatdate.split('-');
                dateFormat = dformat[2] + "-" + month[dformat[1] - 1] + "-" + dformat[0];
                $('#ctl00_BodyContentPH_txtFromDate').val(dateFormat);
                $('#ctl00_BodyContentPH_txtToDate').val(dateFormat);
            }
            $('#ctl00_BodyContentPH_txtLeaveDate').val(dateFormat);
            $('#ctl00_BodyContentPH_TxtOnDuty').val(dateFormat);
            if (arrEvent != "") {
                LoadApplication(dateFormat, arrEvent);
            }
            LeaveApplicationAddClear(formatdate);
            var modal = $("#LeaveModal");
            modal.modal();
           // AddLeaveEvent(formatdate);
        }
    }
    else {
        alert('Leave \ Permisssion cannot Apply');
    }
}


function LoadApplication(date, EventData) {
    debugger;
    if (EventData[0] != "") {
        if (EventData[0] == "Leave") {
            $('#ctl00_BodyContentPH_ddl_Type').val(EventData[0]);
            $('#ctl00_BodyContentPH_ddl_Type').attr('disabled', true);
        }
    }
    else {
        $('#ctl00_BodyContentPH_ddl_Type').val(0);
    }
    if (EventData[1] != "") {
        $('#ctl00_BodyContentPH_ddl_LeaveType').hide();
        $('#ctl00_BodyContentPH_txt_LeaveType').show();
        $('#ctl00_BodyContentPH_txt_LeaveType').val(EventData[1]);
        $('#ctl00_BodyContentPH_ddl_LeaveType').attr('disabled', true);

    }
    if (EventData[2] != "") {
        $('#ctl00_BodyContentPH_ddl_SessionFrom').val(EventData[2]);
        $('#ctl00_BodyContentPH_ddl_SessionFrom').attr('disabled', true);
    }
    if (EventData[3] != "") {
        $('#ctl00_BodyContentPH_ddl_SessionTo').val(EventData[3]);
        $('#ctl00_BodyContentPH_ddl_SessionTo').attr('disabled', true);

    }

    if (EventData[4] != "") {
        $('#ctl00_BodyContentPH_txt_Numberofdays').val(EventData[4]);
    }
    if (EventData[5] != "") {
        $('#ctl00_BodyContentPH_txtReason').val(EventData[5]);
        $('#ctl00_BodyContentPH_txtReason').attr('Enabled', false);
    }
    $('#ctl00_BodyContentPH_div_fileUpload').show();
    $('#ctl00_BodyContentPH_btnLeaveAdd').hide();
    $('#ctl00_BodyContentPH_btnCancel').hide();
    $('#ctl00_BodyContentPH_btnDelete').show();
    $('#ctl00_BodyContentPH_btnDelete').text = 'delete';
    $('#ctl00_BodyContentPH_chkBorrowLeave').hide();
    $('#ctl00_BodyContentPH_lblRemainingNumberofLeave').hide();
    $('#ctl00_BodyContentPH_txtRemainingNoOfLeave').hide();
    $('#ctl00_BodyContentPH_hdnLeaveApplicationID').val(EventData[6]);

}

function LeaveApplicationAddClear(date) {
    $('#ctl00_BodyContentPH_ddl_SessionFrom').selectedIndex = 0;
    $('#ctl00_BodyContentPH_ddl_SessionTo').selectedIndex = 0;
    $('#ctl00_BodyContentPH_txt_Numberofdays').val('');
    $('#ctl00_BodyContentPH_ddl_ddl_Type').selectedIndex = 0;
    if ($('#ctl00_BodyContentPH_ddl_ddl_LeaveType').length != null) {
        for (var count = $('#ctl00_BodyContentPH_ddl_ddl_LeaveType').length - 1; count > -1; count--) {

            $('#ctl00_BodyContentPH_ddl_LeaveType').options[count] = null;
        }
    }
    //var newOptSelect = new Option(Select,'-Select-',false,false);
    $('#ctl00_BodyContentPH_ddl_LeaveType').val(1);
}

function CheckLeavePolicy(date) {
    if (window.ActiveXObject) {
        try {
            GetIndividualCalendars = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetIndividualCalendars = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e1) {
                GetIndividualCalendars = null;
            }
        }
    } else if (window.XMLHttpRequest) {
        try {
            GetIndividualCalendars = new XMLHttpRequest("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetIndividualCalendars = new XMLHttpRequest("Microsoft.XMLHTTP");
            } catch (e1) {
                GetIndividualCalendars = null;
            }
        }
    }
    if (GetIndividualCalendars != null) {
        var k = Math.floor(Math.random() * 111);
        if (window.ActiveXObject) {
            //GetIndividualCalendar.onreadystatechange = LeavePolicyId;
            //var url = window.location.pathname.split("/").slice(-1);
            GetIndividualCalendars.open("GET", "GetMyCalendarData.aspx?chkLeavePolicyDate=" + date, false);
            GetIndividualCalendars.send(null);
            EmpLeavePolicy = GetIndividualCalendars.responseText;
        } else if (window.XMLHttpRequest) {
            //var url = window.location.pathname.split("/").slice(-1);
            GetIndividualCalendars.open("GET", "GetMyCalendarData.aspx?chkLeavePolicyDate=" + date, false);
            GetIndividualCalendars.send(null);
            EmpLeavePolicy = GetIndividualCalendars.responseText;
            //GetIndividualCalendar.onreadystatechange = LeavePolicyId;
        }
    }
    return EmpLeavePolicy;
}


// Team Calendar
function GetTeamCalendar(EmployeeID) {
    EmployeeCode = EmployeeID;
    if (window.ActiveXObject) {
        try {
            GetTeamCalendar = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetTeamCalendar = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e1) {
                GetTeamCalendar = null;
            }
        }
    } else if (window.XMLHttpRequest) {
        try {
            GetTeamCalendar = new XMLHttpRequest("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                GetTeamCalendar = new XMLHttpRequest("Microsoft.XMLHTTP");
            } catch (e1) {
                GetTeamCalendar = null;
            }
        }
    }
    if (GetTeamCalendar != null) {
        var k = Math.floor(Math.random() * 111);
        if (window.ActiveXObject) {
            GetTeamCalendar.onreadystatechange = Process_Response_GetTeamCalendar;
            //var url = window.location.pathname.split("/").slice(-1);
            GetTeamCalendar.open("GET", "GetMyCalendarData.aspx?Team=Yes", false);
            GetTeamCalendar.send(null);
        } else if (window.XMLHttpRequest) {
            //var url = window.location.pathname.split("/").slice(-1);
            GetTeamCalendar.open("GET", "GetMyCalendarData.aspx?Team=Yes", false);
            GetTeamCalendar.send(null);
            GetTeamCalendar.onreadystatechange = Process_Response_GetTeamCalendar();
        }
    }
    return false;
}

function Process_Response_GetTeamCalendar() {
    if (GetTeamCalendar.readyState == 4) {
        if (GetTeamCalendar.status == 200) {
            var retval = GetTeamCalendar.responseText;
            if (retval == "") {
                $('#Teamcalendar').fullCalendar({
                    header: {
                        right: 'prev,next today',
                        center: 'title',
                        left: ''
                    },
                    buttonText: {
                        today: 'Today',
                        month: 'month',
                        week: 'week',
                        day: 'day'
                    },
                });
            }
            else {
                if (retval.length > 0 && retval != "Fail") {

                    $('#tablist a[href="#ctl00_BodyContentPH_tab2"]').tab('show');
                    $('html, body').animate({ scrollTop: 0 }, 'slow');
                    if (retval != "") {
                        var ArrTotalEmployee = retval.split('~');
                        var ArrMyCalendarMonth = ArrTotalEmployee[0].split('%');
                        var ArrMyCalendarDay = [];
                        var DataPoints = [];
                        if (ArrMyCalendarMonth[0] != "") {
                            for (var i = 0; i < ArrMyCalendarMonth.length; i++) {

                                ArrMyCalendarDay = ArrMyCalendarMonth[i].split(',');
                                var date = ArrMyCalendarDay[0];
                                if (ArrMyCalendarDay[1] == "P - 0 / 0" && ArrMyCalendarDay[2] == "A - 0 / 0" && ArrMyCalendarDay[3] == "L - 0 / 0") {
                                    if (ArrMyCalendarDay[4] == "W - 0 / 0") {
                                        var SplitData = ArrMyCalendarDay[4].split('-');
                                        var SplitCount = SplitData[1].split('/');
                                        if (ArrTotalEmployee[1].trim() == SplitCount[0].trim() && ArrTotalEmployee[1].trim() == SplitCount[1].trim()) {
                                            DataPoints.push({
                                                title: "Weekly Off",
                                                start: date,
                                                url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                color: '#02749E',
                                                textColor: '#FFF',
                                                center: true,
                                            });
                                        }
                                        else {
                                            DataPoints.push(
                                                {
                                                    title: ArrMyCalendarDay[1],
                                                    start: date,
                                                    url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                    color: '#33aae6',//#FFF
                                                    textColor: '#FFF',
                                                    tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                    center: true,

                                                },
                                                {
                                                    title: ArrMyCalendarDay[2],
                                                    start: date,
                                                    url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                    color: '#FF5151',//#FFF
                                                    textColor: '#FFF',
                                                    tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                    center: true,
                                                },
                                                {
                                                    title: ArrMyCalendarDay[3],
                                                    start: date,
                                                    url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                    color: '#D0457B',//#FFF
                                                    textColor: '#FFF',
                                                    tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                    center: true,
                                                }
                                            );

                                        }
                                    }
                                    else if (ArrMyCalendarDay[5] == "H - 0 / 0") {
                                        var SplitData = ArrMyCalendarDay[5].split('-');
                                        var SplitCount = SplitData[1].split('/');
                                        if (ArrTotalEmployee[1].trim() == SplitCount[0].trim() && ArrTotalEmployee[1].trim() == SplitCount[1].trim()) {
                                            DataPoints.push(
                                                {
                                                    title: "Holiday",
                                                    start: date,
                                                    url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                    color: '#FF7F00',
                                                    textColor: '#FFF',
                                                    center: true,
                                                });
                                        }
                                        else {
                                            DataPoints.push(
                                                {
                                                    title: ArrMyCalendarDay[1],
                                                    start: date,
                                                    url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                    color: '#33aae6',//#FFF
                                                    textColor: '#FFF',
                                                    tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                    center: true,
                                                },
                                                {
                                                    title: ArrMyCalendarDay[2],
                                                    start: date,
                                                    url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                    color: '#FF5151',//#FFF
                                                    textColor: '#FFF',
                                                    tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                    center: true,
                                                },
                                                {
                                                    title: ArrMyCalendarDay[3],
                                                    start: date,
                                                    url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                    color: '#D0457B',//#FFF
                                                    textColor: '#FFF',
                                                    tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                    center: true,
                                                }
                                            );
                                        }
                                    }
                                    else {
                                        DataPoints.push(
                                            {
                                                title: ArrMyCalendarDay[1],
                                                start: date,
                                                url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                color: '#33aae6',//#FFF
                                                textColor: '#FFF',
                                                tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                center: true,
                                            },
                                            {
                                                title: ArrMyCalendarDay[2],
                                                start: date,
                                                url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                color: '#FF5151',//#FFF
                                                textColor: '#FFF',
                                                tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                center: true,
                                            },
                                            {
                                                title: ArrMyCalendarDay[3],
                                                start: date,
                                                url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                                color: '#D0457B',//#FFF
                                                textColor: '#FFF',
                                                tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                                center: true,
                                            }
                                        );
                                    }
                                }
                                else {
                                    DataPoints.push(
                                        {
                                            title: ArrMyCalendarDay[1],
                                            start: date,
                                            url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                            color: '#33aae6',//#FFF
                                            textColor: '#FFF',
                                            tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                            center: true,
                                        },
                                        {
                                            title: ArrMyCalendarDay[2],
                                            start: date,
                                            url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                            color: '#FF5151',//#FFF
                                            textColor: '#FFF',
                                            tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                            center: true,
                                        },
                                        {
                                            title: ArrMyCalendarDay[3],
                                            start: date,
                                            url: 'Reports/TA_UI_Report_ReportViewer.aspx?ReportName=MonthlyAttendance&MyEmpAutoID=' + EmployeeCode + '&MonthlydataLoadingVer=0&dtFrom=' + date + '&dtTo=' + date + '&empid=&shft=0&EmpName=&Level=0&EmpCategory=0&EmpType=0&ParentLevelId=0&UserGroupName=SuperAdmin&GradeLevel=0&MinWorkHours=&SwipeCard=&Designation=0&Gender=Both',
                                            color: '#D0457B',//#FFF
                                            textColor: '#FFF',
                                            tooltip: 'Present - First Session / Second Session \nAbsent - First Session / Second Session \nLeave - First Session / Second Session',
                                            center: true,
                                        }
                                        
                                    );
                                }
                            }
                            $('#Teamcalendar').fullCalendar({

                                header: {
                                    left: 'prev,next today week',
                                    center: 'title',
                                    right: 'month,basicWeek,basicDay'
                                },
                                buttonText: {
                                    today: 'Today',
                                    month: 'month',
                                    week: 'week',
                                    day: 'day'
                                },
                                events: DataPoints,
                                eventRender: function (event, element) {
                                    element.attr('title', event.tooltip);
                                },
                                eventClick: function (event) {
                                    if (event.url != null) {
                                        window.open(event.url, 'gcalevent', 'left=10px, top=60px, width=1000px, height=600px, scrollbars=yes, status=no, resizable=yes');
                                        return false;
                                    }
                                }

                            });
                        }
                        else {
                            $('#Teamcalendar').fullCalendar({
                                header: {
                                    right: 'prev,next today',
                                    center: 'title',
                                    left: ''
                                },
                                buttonText: {
                                    today: 'Today',
                                    month: 'month',
                                    week: 'week',
                                    day: 'day'
                                },
                            });
                        }
                    }
                }
            }
        }
        else if (GetTeamCalendar.status == 404) {
            GetTeamCalendar.open("GET", "../GetMyCalendarData.aspx?Team=Yes", false);
            GetTeamCalendar.send(null);
            GetTeamCalendar.onreadystatechange = Process_Response_GetTeamCalendar();
        }
    }
}
