using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                    Started Date  : 12-Aug-2007
* Module          : Permission                   Last Modified : 14-Aug-2007
* Programmer      : Prabakaran.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_NumberofHoursCount : Base.BasePage
{
    string FromTime, EndTime;
    // Double intervalHours, MaxPermissionHours;
    DataSet ds_MaxNoOfHours;
    string EmpID;
    int EmpAutoID;
    int gintWeeklyOffHolidayCount = 0;
    int StartYear, EndYear;
    bool blnCheckBreak = false;
    bool blnBreak_NotAllow = false;
    string MaxPermitHours, gstrDateFormat, strFromTime, strToTime, gstrSwipeCardNo, gstrDateOfJoining, gstrTimeFormat, strPermissionDate;
    PermissionEventData objPermissionEventData = new PermissionEventData();
    cls_Common objClassCommon = new cls_Common();
    MonthClosureEventData objMonthClosure = new MonthClosureEventData();
    ShiftExchangeEventData objShiftExchangeEventData = new ShiftExchangeEventData();
    HolidayEventData objHolidayEventData = new HolidayEventData();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        //Session.LCID = 1033;
        FromTime = Convert.ToString(Request.QueryString["FromTime"]);
        EndTime = Convert.ToString(Request.QueryString["EndTime"]);
        strPermissionDate = Convert.ToString(Request.QueryString["PermissionDate"]);
        EmpAutoID = Convert.ToInt32(Request.QueryString["EmpID"]);
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        gstrTimeFormat = Session["MTimeFormat"].ToString();
        bool blnDateTime = true;
        blnDateTime = DateAndTimeConvertion(gstrDateFormat, strPermissionDate, "D");
        if (blnDateTime == false)
        {
            Response.Clear();
            Response.Write("InvalidPermissionDate");
            Response.End();
            return;
        }
        blnDateTime = true;
        blnDateTime = DateAndTimeConvertion(gstrDateFormat + " " + gstrTimeFormat, FromTime, "B");
        if (blnDateTime == true)
        {
            blnDateTime = DateTimeCheck(gstrDateFormat + " " + gstrTimeFormat, FromTime);
        }
        if (blnDateTime == false)
        {
            Response.Clear();
            Response.Write("InvalidFromDate");
            Response.End();
            return;
        }
        blnDateTime = true;
        blnDateTime = DateAndTimeConvertion(gstrDateFormat + " " + gstrTimeFormat, EndTime, "B");
        if (blnDateTime == true)
        {
            blnDateTime = DateTimeCheck(gstrDateFormat + " " + gstrTimeFormat, EndTime);
        }
        if (blnDateTime == false)
        {
            Response.Clear();
            Response.Write("InvalidToDate");
            Response.End();
            return;
        }
        strPermissionDate = objClassCommon.DateConvertion(gstrDateFormat, strPermissionDate);
        DataSet ds_Year = new DataSet();
        ds_Year = objClassCommon.GetYearsetting();
        if (ds_Year.Tables[0].Rows.Count <= 0)
        {
        }
        else
        {
            StartYear = Convert.ToInt32(ds_Year.Tables[0].Rows[0]["StartYear"]);
            EndYear = Convert.ToInt32(ds_Year.Tables[0].Rows[0]["EndYear"]);
            ds_Year.Dispose();
        }
        int LeaveStartYear = DateTime.Parse(strPermissionDate).Year;
        int LeaveEndYear = DateTime.Parse(strPermissionDate).Year;
        if ((LeaveStartYear >= StartYear && LeaveStartYear <= EndYear) && (LeaveEndYear >= StartYear && LeaveEndYear <= EndYear))
        {
        }
        else
        {
            Response.Clear();
            Response.Write("InvalidYear");
            Response.End();
            return;
        }
        DataSet dsEmp = new DataSet();
        dsEmp = objPermissionEventData.GetEmployeeCodeID(EmpAutoID);
        if (dsEmp != null)
        {
            if (dsEmp.Tables.Count > 0)
            {
                if (dsEmp.Tables[0].Rows.Count > 0)
                {
                    if (dsEmp.Tables[0].Rows[0]["Employee_code_Id"] != DBNull.Value)
                    {
                        EmpID = Convert.ToString(dsEmp.Tables[0].Rows[0]["Employee_code_Id"]);
                        gstrSwipeCardNo = Convert.ToString(dsEmp.Tables[0].Rows[0]["SwipeCardNo"]);
                        gstrDateOfJoining = Convert.ToString(dsEmp.Tables[0].Rows[0]["DateOfJoining"]);
                    }
                }
            }
        }
        int WeeklyOffHolidayCount = 0;
        //WeeklyOffHolidayCount = CheckDays(FromTime, EndTime);
        WeeklyOffHolidayCount = CheckDays(strPermissionDate, strPermissionDate);
        if (WeeklyOffHolidayCount > 0)
        {
            Response.Clear();
            Response.Write("WeeklyOffHoliday");
            Response.End();
        }
        if (FromTime != "" && EndTime != "")
        {
            //Convert Date Format to MM/dd/yyyy Format
            //Convert Date Format to MM/dd/yyyy Format
            strFromTime = objClassCommon.DateTimeConvertion((gstrDateFormat + " " + gstrTimeFormat), FromTime, "B");
            strToTime = objClassCommon.DateTimeConvertion((gstrDateFormat + " " + gstrTimeFormat), EndTime, "B");
            if (DateTime.Parse(DateTime.Parse(gstrDateOfJoining).ToShortDateString()) <= DateTime.Parse(DateTime.Parse(strPermissionDate).ToShortDateString()))
            {
                //Leave dates Applicable
            }
            else
            {
                //Leave dates not applicable
                Response.Clear();
                Response.Write("DateOfJoining");
                Response.End();
            }
            TimeDifference(strFromTime, strToTime);
        }
        else
        {
            Response.Clear();
            Response.Write("FillBothTime");
            Response.End();
        }
    }
    public bool DateTimeCheck(string Format, string strDateTime)
    {
        if (Format.Length != strDateTime.Length)
        {
            return false;
        }
        return true;
    }
    public bool DateAndTimeConvertion(string Format, string strDateTime, string FormatType)
    {
        bool blnDateAndTime = true;
        try
        {
            if (strDateTime == "")
            {
                return false;
            }
            // Getting Month
            int indexMonth = -2;
            int intMonthNoOfChars = 0;
            string strMonth = "";
            int indexMMM = Format.IndexOf(@"MMM");
            if (indexMMM == -1)
            {
                int indexMM = Format.IndexOf(@"MM");
                if (indexMM >= 0)
                {
                    indexMonth = indexMM;
                    intMonthNoOfChars = 2;
                }
            }
            else
            {
                indexMonth = indexMMM;
                intMonthNoOfChars = 3;
            }
            if (indexMonth != -2)
            {
                strMonth = strDateTime.Substring(indexMonth, intMonthNoOfChars);
            }
            // Getting Date
            int indexDate = -2;
            int intDateNoOfChars = 0;
            string strDate = "";
            int indexDD = Format.IndexOf(@"dd");
            if (indexDD >= 0)
            {
                indexDate = indexDD;
                intDateNoOfChars = 2;
            }
            if (indexDate != -2)
            {
                strDate = strDateTime.Substring(indexDate, intDateNoOfChars);
            }
            // Getting Year
            int indexYear = -2;
            int intYearNoOfChars = 0;
            string strYear = "";
            int indexYYYY = Format.IndexOf(@"yyyy");
            if (indexYYYY == -1)
            {
                int indexYY = Format.IndexOf(@"yy");
                if (indexYY >= 0)
                {
                    indexYear = indexYY;
                    intYearNoOfChars = 2;
                }
            }
            else
            {
                indexYear = indexYYYY;
                intYearNoOfChars = 4;
            }
            if (indexYear != -2)
            {
                strYear = strDateTime.Substring(indexYear, intYearNoOfChars);
            }
            string strFinalDate = "";
            if (indexMMM >= 0)
            {
                strFinalDate = strDate + "/" + strMonth + "/" + strYear;
            }
            else
            {
                strFinalDate = strMonth + "/" + strDate + "/" + strYear;
            }
            DateTime dtmFinalDate = new DateTime();
            try
            {
                dtmFinalDate = DateTime.Parse(strFinalDate);
            }
            catch (Exception ex)
            {
                //throw ex;
                return false;
            }
            // Getting Hours
            int indexHours = -2;
            int inHourstNoOfChars = 0;
            string strHours = "00";
            int indexhh = Format.IndexOf(@"hh");
            if (indexhh >= 0)
            {
                indexHours = indexhh;
                inHourstNoOfChars = 2;
            }
            else
            {
                int indexHH = Format.IndexOf(@"HH");
                if (indexHH >= 0)
                {
                    indexHours = indexHH;
                    inHourstNoOfChars = 2;
                }
            }
            if (indexHours != -2)
            {
                strHours = strDateTime.Substring(indexHours, inHourstNoOfChars);
            }
            // Getting Minutes
            int indexMins = -2;
            int intMinsNoOfChars = 0;
            string strMins = "00";
            int indexmm = Format.IndexOf(@"mm");
            if (indexmm >= 0)
            {
                indexMins = indexmm;
                intMinsNoOfChars = 2;
            }
            if (indexMins != -2)
            {
                strMins = strDateTime.Substring(indexMins, intMinsNoOfChars);
            }
            // Getting Seconds
            int indexSecs = -2;
            int intSecsNoOfChars = 0;
            string strSeconds = "00";
            int indexss = Format.IndexOf(@"ss");
            if (indexss >= 0)
            {
                indexSecs = indexss;
                intSecsNoOfChars = 2;
            }
            if (indexSecs != -2)
            {
                strSeconds = strDateTime.Substring(indexSecs, intSecsNoOfChars);
            }
            // Getting AM or PM - If 12 Hours Format
            int indexAMPM = -2;
            int intAMPMNoOfChars = 0;
            string strAMPM = "";
            int indextt = Format.IndexOf(@"tt");
            if (indextt >= 0)
            {
                indexAMPM = indextt;
                intAMPMNoOfChars = 2;
            }
            if (indexAMPM != -2)
            {
                strAMPM = strDateTime.Substring(indexAMPM, intAMPMNoOfChars);
            }
            string strTime = "";
            if (indextt >= 0)
            {
                strTime = strHours + ":" + strMins + ":" + strSeconds + " " + strAMPM;
            }
            else
            {
                strTime = strHours + ":" + strMins + ":" + strSeconds;
            }
            DateTime dtmFinalTime = new DateTime();
            try
            {
                dtmFinalTime = DateTime.Parse(strTime);
            }
            catch (Exception ex)
            {
                //throw ex;
                return false;
            }
            if (FormatType == "D")
            {
                blnDateAndTime = true;
            }
            else if (FormatType == "T")
            {
                blnDateAndTime = true;
            }
            else if (FormatType == "B")
            {
                blnDateAndTime = true;
            }
            else
            {
                blnDateAndTime = true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
        return blnDateAndTime;
    }
    public void TimeDifference(string FromTime, string EndTime)
    {
        //TimeSpan tsStartTime = DateTime.Parse(FromTime).TimeOfDay;
        //TimeSpan tsEndTime = DateTime.Parse(EndTime).TimeOfDay;
        TimeSpan tsDiff = new TimeSpan();
        TimeSpan tsBreak1_StartHour = new TimeSpan();
        TimeSpan tsBreak1_EndHour = new TimeSpan();
        TimeSpan tsBreak1_Hours = new TimeSpan();
        TimeSpan tsBreak2_StartHour = new TimeSpan();
        TimeSpan tsBreak2_EndHour = new TimeSpan();
        TimeSpan tsBreak2_Hours = new TimeSpan();
        TimeSpan tsBreak3_StartHour = new TimeSpan();
        TimeSpan tsBreak3_EndHour = new TimeSpan();
        TimeSpan tsBreak3_Hours = new TimeSpan();
        DateTime dtmIn = DateTime.Parse(FromTime);
        DateTime dtmOut = DateTime.Parse(EndTime);
        DateTime dtmPermissionDate = DateTime.Parse(strPermissionDate);
        tsDiff = dtmOut.Subtract(dtmIn);
        if (tsDiff.Hours < 0 || tsDiff.Minutes < 0 || tsDiff.Milliseconds < 0 || tsDiff.TotalHours < 0)
        {
            Response.Clear();
            Response.Write("Fail");
            Response.End();
            return;
        }
        else if (tsDiff.Days > 0)
        {
            Response.Clear();
            Response.Write("SelectValidHours");
            Response.End();
            return;
        }
        if (EmpID != "")
        {
            DataSet ds = new DataSet();
            string PermissionDate = "";
            int mon = dtmPermissionDate.Month;
            int dat = dtmPermissionDate.Day;
            int yea = dtmPermissionDate.Year;
            string ShiftStartTime = "";
            string ShiftEndTime = "";
            DateTime dtShift_BreakStartTime1 = new DateTime();
            DateTime dtShift_BreakEndTime1 = new DateTime();
            DateTime dtShift_BreakStartTime2 = new DateTime();
            DateTime dtShift_BreakEndTime2 = new DateTime();
            DateTime dtShift_BreakStartTime3 = new DateTime();
            DateTime dtShift_BreakEndTime3 = new DateTime();
            string strDefaultStartDate = string.Empty;
            string strBreakStart1 = string.Empty;
            string strBreakEnd1 = string.Empty;
            string strBreakStart2 = string.Empty;
            string strBreakEnd2 = string.Empty;
            string strBreakStart3 = string.Empty;
            string strBreakEnd3 = string.Empty;
            bool Is24Hrs = false;
            //strDefaultStartDate = DateTime.Parse(FromTime).ToShortDateString();
            strDefaultStartDate = DateTime.Parse(strPermissionDate).ToShortDateString();
            PermissionDate = Convert.ToString(mon) + "/" + Convert.ToString(dat) + "/" + Convert.ToString(yea);
            ds = objPermissionEventData.GetShiftID(Convert.ToString(EmpID), yea, mon, dat);
            int ShitfID = 0;
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0][0] != DBNull.Value)
                        {
                            ShitfID = Convert.ToInt16(ds.Tables[0].Rows[0][0]);
                            if (ShitfID == 0)
                            {
                                Response.Clear();
                                Response.Write("ShiftNotAllocated");
                                Response.End();
                                return;
                            }
                            DataSet dsShift = new DataSet();
                            dsShift = objPermissionEventData.GetShiftTime(ShitfID);
                            if (dsShift != null)
                            {
                                if (dsShift.Tables.Count > 0)
                                {
                                    if (dsShift.Tables[0].Rows.Count > 0)
                                    {
                                        if (dsShift.Tables[0].Rows[0]["shift_StartTime"] != DBNull.Value)
                                        {
                                            ShiftStartTime = Convert.ToString(dsShift.Tables[0].Rows[0]["shift_StartTime"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["shift_EndTime"] != DBNull.Value)
                                        {
                                            ShiftEndTime = Convert.ToString(dsShift.Tables[0].Rows[0]["shift_EndTime"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"].ToString() != string.Empty)
                                        {
                                            dtShift_BreakStartTime1 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"]));
                                            strBreakStart1 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"].ToString() != string.Empty)
                                        {
                                            dtShift_BreakEndTime1 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"]));
                                            strBreakEnd1 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"].ToString() != string.Empty)
                                        {
                                            dtShift_BreakStartTime2 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"]));
                                            strBreakStart2 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"].ToString() != string.Empty)
                                        {
                                            dtShift_BreakEndTime2 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"]));
                                            strBreakEnd2 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"].ToString() != string.Empty)
                                        {
                                            dtShift_BreakStartTime3 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"]));
                                            strBreakStart3 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"].ToString() != string.Empty)
                                        {
                                            dtShift_BreakEndTime3 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"]));
                                            strBreakEnd3 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"]);
                                        }
                                        if (dsShift.Tables[0].Rows[0]["24HrsShift"] != DBNull.Value && dsShift.Tables[0].Rows[0]["24HrsShift"].ToString() != string.Empty)
                                        {
                                            Is24Hrs = Convert.ToBoolean(dsShift.Tables[0].Rows[0]["24HrsShift"]);
                                        }
                                        //included by sashma on 21-Oct-2010
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"].ToString() != string.Empty)
                                        {
                                            if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"].ToString() != string.Empty)
                                            {
                                                tsBreak1_StartHour = Convert.ToDateTime(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"].ToString()).TimeOfDay;
                                                tsBreak1_EndHour = Convert.ToDateTime(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"].ToString()).TimeOfDay;
                                                tsBreak1_Hours = tsBreak1_EndHour.Subtract(tsBreak1_StartHour);
                                            }
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"].ToString() != string.Empty)
                                        {
                                            if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"].ToString() != string.Empty)
                                            {
                                                tsBreak2_StartHour = Convert.ToDateTime(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"].ToString()).TimeOfDay;
                                                tsBreak2_EndHour = Convert.ToDateTime(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"].ToString()).TimeOfDay;
                                                tsBreak2_Hours = tsBreak2_EndHour.Subtract(tsBreak2_StartHour);
                                            }
                                        }
                                        if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"].ToString() != string.Empty)
                                        {
                                            if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"].ToString() != string.Empty)
                                            {
                                                tsBreak3_StartHour = Convert.ToDateTime(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"].ToString()).TimeOfDay;
                                                tsBreak3_EndHour = Convert.ToDateTime(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"].ToString()).TimeOfDay;
                                                tsBreak3_Hours = tsBreak3_EndHour.Subtract(tsBreak3_StartHour);
                                            }
                                        }
                                        if (ShiftStartTime.Substring(6, 2) == "PM" && ShiftEndTime.Substring(6, 2) == "AM")
                                        {
                                            if (strBreakStart1 != string.Empty)
                                            {
                                                if (strBreakStart1.Substring(6, 2) == "AM")
                                                {
                                                    dtShift_BreakStartTime1.AddDays(1);
                                                }
                                            }
                                            if (strBreakStart2 != string.Empty)
                                            {
                                                if (strBreakStart2.Substring(6, 2) == "AM")
                                                {
                                                    dtShift_BreakStartTime2.AddDays(1);
                                                }
                                            }
                                            if (strBreakStart3 != string.Empty)
                                            {
                                                if (strBreakStart3.Substring(6, 2) == "AM")
                                                {
                                                    dtShift_BreakStartTime3.AddDays(1);
                                                }
                                            }
                                            if (strBreakEnd1 != string.Empty)
                                            {
                                                if (strBreakEnd1.Substring(6, 2) == "AM")
                                                {
                                                    dtShift_BreakEndTime1.AddDays(1);
                                                }
                                            }
                                            if (strBreakEnd2 != string.Empty)
                                            {
                                                if (strBreakEnd2.Substring(6, 2) == "AM")
                                                {
                                                    dtShift_BreakEndTime2.AddDays(1);
                                                }
                                            }
                                            if (strBreakEnd3 != string.Empty)
                                            {
                                                if (strBreakEnd3.Substring(6, 2) == "AM")
                                                {
                                                    dtShift_BreakEndTime3.AddDays(1);
                                                }
                                            }
                                        }
                                        if (strBreakStart1 != string.Empty && strBreakEnd1 != string.Empty)
                                        {
                                            //if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime1 && DateTime.Parse(FromTime) <= dtShift_BreakEndTime1) || (DateTime.Parse(EndTime) >= dtShift_BreakStartTime1 && DateTime.Parse(EndTime) <= dtShift_BreakEndTime1))
                                            //{
                                            //    blnCheckBreak = true;
                                            //}
                                            //if (((dtShift_BreakStartTime1 >= DateTime.Parse(FromTime) && dtShift_BreakStartTime1 <= DateTime.Parse(EndTime)) || (dtShift_BreakEndTime1 >= DateTime.Parse(FromTime) && dtShift_BreakEndTime1 <= DateTime.Parse(EndTime))))
                                            //{
                                            //    blnCheckBreak = true;
                                            //}
                                            if (DateTime.Parse(FromTime) < dtShift_BreakStartTime1 && DateTime.Parse(EndTime) > dtShift_BreakEndTime1)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (DateTime.Parse(FromTime) < dtShift_BreakStartTime1 && DateTime.Parse(EndTime) == dtShift_BreakEndTime1)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (DateTime.Parse(FromTime) == dtShift_BreakStartTime1 && DateTime.Parse(EndTime) > dtShift_BreakEndTime1)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (blnCheckBreak != true)
                                            {
                                                //if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime2 || DateTime.Parse(FromTime) <= dtShift_BreakStartTime2) && (DateTime.Parse(EndTime) <= dtShift_BreakEndTime2 && DateTime.Parse(EndTime) >= dtShift_BreakStartTime2))
                                                if ((DateTime.Parse(FromTime) > dtShift_BreakStartTime1 && DateTime.Parse(FromTime) < dtShift_BreakEndTime1))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                                if ((DateTime.Parse(EndTime) > dtShift_BreakStartTime1 && DateTime.Parse(EndTime) < dtShift_BreakEndTime1))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                                if ((DateTime.Parse(FromTime) == dtShift_BreakStartTime1 && DateTime.Parse(EndTime) == dtShift_BreakEndTime1))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                            }
                                        }
                                        if (strBreakStart2 != string.Empty && strBreakEnd2 != string.Empty)
                                        {
                                            //if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime2 && DateTime.Parse(FromTime) <= dtShift_BreakEndTime2) || (DateTime.Parse(EndTime) >= dtShift_BreakStartTime2 && DateTime.Parse(EndTime) <= dtShift_BreakEndTime2))
                                            //{
                                            //    blnCheckBreak = true;
                                            //}
                                            //if (((dtShift_BreakStartTime2 >= DateTime.Parse(FromTime) && dtShift_BreakStartTime2 <= DateTime.Parse(EndTime)) || (dtShift_BreakEndTime2 >= DateTime.Parse(FromTime) && dtShift_BreakEndTime2 <= DateTime.Parse(EndTime))))
                                            //{
                                            //    blnCheckBreak = true;
                                            //}
                                            //Include by sashma on 21-Oct-2010
                                            if (DateTime.Parse(FromTime) < dtShift_BreakStartTime2 && DateTime.Parse(EndTime) > dtShift_BreakEndTime2)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (DateTime.Parse(FromTime) < dtShift_BreakStartTime2 && DateTime.Parse(EndTime) == dtShift_BreakEndTime2)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (DateTime.Parse(FromTime) == dtShift_BreakStartTime2 && DateTime.Parse(EndTime) > dtShift_BreakEndTime2)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (blnCheckBreak != true)
                                            {
                                                //if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime2 || DateTime.Parse(FromTime) <= dtShift_BreakStartTime2) && (DateTime.Parse(EndTime) <= dtShift_BreakEndTime2 && DateTime.Parse(EndTime) >= dtShift_BreakStartTime2))
                                                if ((DateTime.Parse(FromTime) > dtShift_BreakStartTime2 && DateTime.Parse(FromTime) < dtShift_BreakEndTime2))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                                if ((DateTime.Parse(EndTime) > dtShift_BreakStartTime2 && DateTime.Parse(EndTime) < dtShift_BreakEndTime2))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                                if ((DateTime.Parse(FromTime) == dtShift_BreakStartTime2 && DateTime.Parse(EndTime) == dtShift_BreakEndTime2))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                            }
                                        }
                                        if (strBreakStart3 != string.Empty && strBreakEnd3 != string.Empty)
                                        {
                                            //if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime3 && DateTime.Parse(FromTime) <= dtShift_BreakEndTime3) || (DateTime.Parse(EndTime) >= dtShift_BreakStartTime3 && DateTime.Parse(EndTime) <= dtShift_BreakEndTime3))
                                            //{
                                            //    blnCheckBreak = true;
                                            //}
                                            //if (((dtShift_BreakStartTime3 >= DateTime.Parse(FromTime) && dtShift_BreakStartTime3 <= DateTime.Parse(EndTime)) || (dtShift_BreakEndTime3 >= DateTime.Parse(FromTime) && dtShift_BreakEndTime3 <= DateTime.Parse(EndTime))))
                                            //{
                                            //    blnCheckBreak = true;
                                            //}
                                            if (DateTime.Parse(FromTime) < dtShift_BreakStartTime3 && DateTime.Parse(EndTime) > dtShift_BreakEndTime3)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (DateTime.Parse(FromTime) < dtShift_BreakStartTime3 && DateTime.Parse(EndTime) == dtShift_BreakEndTime3)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (DateTime.Parse(FromTime) == dtShift_BreakStartTime3 && DateTime.Parse(EndTime) > dtShift_BreakEndTime3)
                                            {
                                                blnCheckBreak = true;
                                            }
                                            if (blnCheckBreak != true)
                                            {
                                                //if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime2 || DateTime.Parse(FromTime) <= dtShift_BreakStartTime2) && (DateTime.Parse(EndTime) <= dtShift_BreakEndTime2 && DateTime.Parse(EndTime) >= dtShift_BreakStartTime2))
                                                if ((DateTime.Parse(FromTime) > dtShift_BreakStartTime3 && DateTime.Parse(FromTime) < dtShift_BreakEndTime3))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                                if ((DateTime.Parse(EndTime) > dtShift_BreakStartTime3 && DateTime.Parse(EndTime) < dtShift_BreakEndTime3))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                                if ((DateTime.Parse(FromTime) == dtShift_BreakStartTime3 && DateTime.Parse(EndTime) == dtShift_BreakEndTime3))
                                                {
                                                    blnBreak_NotAllow = true;
                                                }
                                            }
                                        }
                                        if (blnBreak_NotAllow == true)
                                        {
                                            Response.Clear();
                                            Response.Write("InBetweenBreakHours");
                                            Response.End();
                                            return;
                                        }
                                        string tempShiftStartTime = string.Empty;
                                        if (ShiftStartTime.Length == 5)
                                        {
                                            tempShiftStartTime = ShiftStartTime.Substring(0, 2);
                                            if (Convert.ToInt32(tempShiftStartTime) < 12)
                                            {
                                                if (Convert.ToInt32(tempShiftStartTime) == 0)
                                                {
                                                    ShiftStartTime = "12:" + ShiftStartTime.Substring(3, 2) + " " + "AM";
                                                }
                                                else
                                                {
                                                    ShiftStartTime = ShiftStartTime + " " + "AM";
                                                }
                                            }
                                            else
                                            {
                                                ShiftStartTime = ShiftStartTime + " " + "PM";
                                            }
                                        }
                                        string tempstrShiftEndTime = string.Empty;
                                        if (ShiftEndTime.Length == 5)
                                        {
                                            tempstrShiftEndTime = ShiftEndTime.Substring(0, 2);
                                            if (Convert.ToInt32(tempstrShiftEndTime) < 12)
                                            {
                                                if (Convert.ToInt32(tempstrShiftEndTime) == 0)
                                                {
                                                    ShiftEndTime = "12:" + ShiftEndTime.Substring(3, 2) + " " + "AM";
                                                }
                                                else
                                                {
                                                    ShiftEndTime = ShiftEndTime + " " + "AM";
                                                }
                                            }
                                            else
                                            {
                                                ShiftEndTime = ShiftEndTime + " " + "PM";
                                            }
                                        }
                                        //string strShiftStartTime = Convert.ToString(PermissionDate) + " " + ShiftStartTime;
                                        //string strShiftEndTime = Convert.ToString(PermissionDate) + " " + ShiftEndTime;
                                        string strShiftStartTime = Convert.ToString(PermissionDate) + " " + ShiftStartTime;
                                        string strShiftEndTime = string.Empty;
                                        DateTime dtShiftStartTime;
                                        DateTime dtShiftEndTime;
                                        //Shift Checking
                                        string TempShiftStart = "";
                                        string TempShiftEnd = "";
                                        TempShiftStart = ShiftStartTime.Substring(6, 2);
                                        TempShiftEnd = ShiftEndTime.Substring(6, 2);
                                        if (TempShiftStart == "PM" && TempShiftEnd == "AM")
                                        {
                                            strShiftEndTime = Convert.ToString(DateTime.Parse(PermissionDate).AddDays(1).ToShortDateString()) + " " + ShiftEndTime;
                                        }
                                        else
                                        {
                                            strShiftEndTime = Convert.ToString(PermissionDate) + " " + ShiftEndTime;
                                        }
                                        dtShiftStartTime = DateTime.Parse(strShiftStartTime);
                                        if (Is24Hrs)
                                        {
                                            dtShiftEndTime = DateTime.Parse(strShiftEndTime).AddDays(1);
                                        }
                                        else
                                        {
                                            dtShiftEndTime = DateTime.Parse(strShiftEndTime);
                                        }
                                        if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime)
                                        {
                                        }
                                        else
                                        {
                                            Response.Clear();
                                            Response.Write("InValidHours");
                                            Response.End();
                                            return;
                                        }
                                        if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime)
                                        {
                                        }
                                        else
                                        {
                                            Response.Clear();
                                            Response.Write("InValidHours");
                                            Response.End();
                                            return;
                                        }
                                        //if (TempShiftStart == "AM" && TempShiftEnd == "PM")
                                        //{
                                        //    if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime)
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //    if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime)
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //}
                                        //if (TempShiftStart == "PM" && TempShiftEnd == "AM")
                                        //{
                                        //    if (Convert.ToString(dtmIn).Substring(Convert.ToString(dtmIn).Length - 2, 2) == "AM")
                                        //    {
                                        //        dtmIn = dtmIn.AddDays(1);
                                        //    }
                                        //    if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime.AddDays(1))
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //    if (Convert.ToString(dtmOut).Substring(Convert.ToString(dtmOut).Length - 2, 2) == "AM")
                                        //    {
                                        //        dtmOut = dtmOut.AddDays(1);
                                        //    }
                                        //    if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime.AddDays(1))
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //}
                                        //if (TempShiftStart == "AM" && TempShiftEnd == "AM")
                                        //{
                                        //    if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime)
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //    if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime)
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //}
                                        //if (TempShiftStart == "PM" && TempShiftEnd == "PM")
                                        //{
                                        //    if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime)
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //    if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime)
                                        //    {
                                        //    }
                                        //    else
                                        //    {
                                        //        Response.Clear();
                                        //        Response.Write("InValidHours");
                                        //        Response.End();
                                        //        return;
                                        //    }
                                        //}
                                        //end Shift Checking
                                    }
                                }
                            }
                        }
                        else
                        {
                            Response.Clear();
                            Response.Write("ShiftNotAllocated");
                            Response.End();
                            return;
                        }
                    }
                }
            }
        }
        string s = tsDiff.ToString();
        //tsDiff = tsEndTime.Subtract(tsStartTime);
        int sReturn = s.LastIndexOf(':');
        string RemainHours = s.Remove(sReturn);
        ds_MaxNoOfHours = objPermissionEventData.GetMaxNoOfHours_ByLeavePolicy(Convert.ToInt32(Session.Contents["EmpAutoId"]));
        if (ds_MaxNoOfHours.Tables[0].Rows.Count != 0)
        {
            foreach (DataRow dRow in ds_MaxNoOfHours.Tables[0].Rows)
            {
                if (dRow["Permission_MaxHrs_Month"] != null)
                {
                    MaxPermitHours = Convert.ToString(dRow["Permission_MaxHrs_Month"]);
                }
            }
            //string[] MaxPermissionHours = MaxPermitHours.Split(':');
            //MaxPermitHours = MaxPermissionHours[0] + "." + MaxPermissionHours[1];
        }
        // TimeSpan intervalHours = new TimeSpan();
        // intervalHours = DateTime.Parse(MaxPermitHours).Subtract(DateTime.Parse(s));
        Response.Clear();
        //Change on 21-Aug_08
        // Response.Write(Math.Round(tsDiff.TotalHours,1) + "," + MaxPermitHours);
        if (blnCheckBreak == true)
        {
            TimeSpan tsDifference = new TimeSpan();
            tsDifference = tsDiff.Subtract(tsBreak1_Hours + tsBreak2_Hours + tsBreak3_Hours);
            string strHours = tsDifference.ToString();
            int intReturn = strHours.LastIndexOf(':');
            string strRemainHours = strHours.Remove(intReturn);
            Response.Write(strRemainHours + "," + "InBetweenBreakHours_DeductHours");
        }
        else
        {
            Response.Write(RemainHours + "," + MaxPermitHours);
        }
        Response.End();
    }
    //old code 
    // public void TimeDifference(string FromTime, string EndTime)
    //{
    //    //TimeSpan tsStartTime = DateTime.Parse(FromTime).TimeOfDay;
    //    //TimeSpan tsEndTime = DateTime.Parse(EndTime).TimeOfDay;
    //    TimeSpan tsDiff = new TimeSpan();
    //    DateTime dtmIn = DateTime.Parse(FromTime);
    //    DateTime dtmOut = DateTime.Parse(EndTime);
    //    tsDiff = dtmOut.Subtract(dtmIn);
    //    if (tsDiff.Hours < 0 || tsDiff.Minutes < 0 || tsDiff.Milliseconds < 0 || tsDiff.TotalHours<0 )
    //    {
    //        Response.Clear();
    //        Response.Write("Fail");
    //        Response.End();
    //        return;        
    //    }
    //    else if (tsDiff.Days > 0)
    //    {
    //        Response.Clear();
    //        Response.Write("SelectValidHours");
    //        Response.End();
    //        return; 
    //    }
    //    if (EmpID != "")
    //    {
    //        DataSet ds = new DataSet();
    //        string PermissionDate = "";
    //        int mon = dtmIn.Month;
    //        int dat = dtmIn.Day;
    //        int yea = dtmIn.Year;
    //        string ShiftStartTime = "";
    //        string ShiftEndTime = "";
    //        DateTime dtShift_BreakStartTime1 =new DateTime();
    //        DateTime dtShift_BreakEndTime1 = new DateTime();
    //        DateTime dtShift_BreakStartTime2 = new DateTime();
    //        DateTime dtShift_BreakEndTime2 = new DateTime();
    //        DateTime dtShift_BreakStartTime3 = new DateTime();
    //        DateTime dtShift_BreakEndTime3 = new DateTime();
    //        string strDefaultStartDate = string.Empty;
    //        string strBreakStart1 = string.Empty;
    //        string strBreakEnd1 = string.Empty;
    //        string strBreakStart2 = string.Empty;
    //        string strBreakEnd2 = string.Empty;
    //        string strBreakStart3 = string.Empty;
    //        string strBreakEnd3 = string.Empty;
    //        strDefaultStartDate = DateTime.Parse(FromTime).ToShortDateString();
    //        PermissionDate = Convert.ToString(mon) + "/" + Convert.ToString(dat) + "/" + Convert.ToString(yea);
    //        ds = objPermissionEventData.GetShiftID(Convert.ToString(EmpID), yea, mon, dat);
    //        int ShitfID = 0;
    //        if (ds != null)
    //        {
    //            if (ds.Tables.Count > 0)
    //            {
    //                if (ds.Tables[0].Rows.Count > 0)
    //                {
    //                    if (ds.Tables[0].Rows[0][0] != DBNull.Value)
    //                    {
    //                        ShitfID = Convert.ToInt16(ds.Tables[0].Rows[0][0]);
    //                        if (ShitfID==0)
    //                        {
    //                             Response.Clear();
    //                            Response.Write("ShiftNotAllocated");
    //                            Response.End();
    //                            return;
    //                        }
    //                        DataSet dsShift = new DataSet();
    //                        dsShift = objPermissionEventData.GetShiftTime(ShitfID);
    //                        if (dsShift != null)
    //                        {
    //                            if (dsShift.Tables.Count > 0)
    //                            {
    //                                if (dsShift.Tables[0].Rows.Count > 0)
    //                                {
    //                                    if (dsShift.Tables[0].Rows[0]["shift_StartTime"] != DBNull.Value)
    //                                    {
    //                                        ShiftStartTime = Convert.ToString(dsShift.Tables[0].Rows[0]["shift_StartTime"]);
    //                                    }
    //                                    if (dsShift.Tables[0].Rows[0]["shift_EndTime"] != DBNull.Value)
    //                                    {
    //                                        ShiftEndTime = Convert.ToString(dsShift.Tables[0].Rows[0]["shift_EndTime"]);
    //                                    }
    //                                    if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"].ToString() != string.Empty)
    //                                    {
    //                                        dtShift_BreakStartTime1 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"]));
    //                                        strBreakStart1 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime1"]);
    //                                    }
    //                                    if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"].ToString() != string.Empty)
    //                                    {
    //                                        dtShift_BreakEndTime1 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"]));
    //                                        strBreakEnd1 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime1"]);
    //                                    }
    //                                    if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"].ToString() != string.Empty)
    //                                    {
    //                                        dtShift_BreakStartTime2 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"]));
    //                                        strBreakStart2 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime2"]);
    //                                    }
    //                                    if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"].ToString() != string.Empty)
    //                                    {
    //                                        dtShift_BreakEndTime2 = DateTime.Parse(strDefaultStartDate + " " +  Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"]));
    //                                        strBreakEnd2 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime2"]);
    //                                    }
    //                                    if (dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"].ToString() != string.Empty)
    //                                    {
    //                                        dtShift_BreakStartTime3 = DateTime.Parse(strDefaultStartDate + " " +  Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"]));
    //                                        strBreakStart3 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakStartTime3"]);
    //                                    }
    //                                    if (dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"] != DBNull.Value && dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"].ToString() != string.Empty)
    //                                    {
    //                                        dtShift_BreakEndTime3 = DateTime.Parse(strDefaultStartDate + " " + Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"]));
    //                                        strBreakEnd3 = Convert.ToString(dsShift.Tables[0].Rows[0]["Shift_BreakEndTime3"]);
    //                                    }
    //                                    if (ShiftStartTime.Substring(6, 2) == "PM" && ShiftEndTime.Substring(6, 2) == "AM")
    //                                    {
    //                                        if (strBreakStart1 != string.Empty)
    //                                        {
    //                                            if (strBreakStart1.Substring(6, 2) == "AM")
    //                                            {
    //                                                dtShift_BreakStartTime1.AddDays(1);
    //                                            }
    //                                        }
    //                                        if (strBreakStart2 != string.Empty)
    //                                        {
    //                                            if (strBreakStart2.Substring(6, 2) == "AM")
    //                                            {
    //                                                dtShift_BreakStartTime2.AddDays(1);
    //                                            }
    //                                        }
    //                                        if (strBreakStart3 != string.Empty)
    //                                        {
    //                                            if (strBreakStart3.Substring(6, 2) == "AM")
    //                                            {
    //                                                dtShift_BreakStartTime3.AddDays(1);
    //                                            }
    //                                        }
    //                                        if (strBreakEnd1  != string.Empty)
    //                                        {
    //                                            if (strBreakEnd1.Substring(6, 2) == "AM")
    //                                            {
    //                                                dtShift_BreakEndTime1.AddDays(1);
    //                                            }
    //                                        }
    //                                        if (strBreakEnd2 != string.Empty)
    //                                        {
    //                                            if (strBreakEnd2.Substring(6, 2) == "AM")
    //                                            {
    //                                                dtShift_BreakEndTime2.AddDays(1);
    //                                            }
    //                                        }
    //                                        if (strBreakEnd3 != string.Empty)
    //                                        {
    //                                            if (strBreakEnd3.Substring(6, 2) == "AM")
    //                                            {
    //                                                dtShift_BreakEndTime3.AddDays(1);
    //                                            }
    //                                        }
    //                                    }
    //                                    if (strBreakStart1 != string.Empty && strBreakEnd1 != string.Empty)
    //                                    {
    //                                        if((DateTime.Parse(FromTime) >= dtShift_BreakStartTime1 && DateTime.Parse(FromTime) <= dtShift_BreakEndTime1) || (DateTime.Parse(EndTime) >= dtShift_BreakStartTime1 && DateTime.Parse(EndTime) <= dtShift_BreakEndTime1))
    //                                        { 
    //                                            blnCheckBreak=true;
    //                                        }
    //                                        if (((dtShift_BreakStartTime1 >= DateTime.Parse(FromTime) && dtShift_BreakStartTime1 <= DateTime.Parse(EndTime)) || (dtShift_BreakEndTime1 >= DateTime.Parse(FromTime) && dtShift_BreakEndTime1 <= DateTime.Parse(EndTime))))
    //                                        {
    //                                            blnCheckBreak = true;
    //                                        }
    //                                    }
    //                                    if (strBreakStart2 != string.Empty && strBreakEnd2 != string.Empty)
    //                                    {
    //                                        if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime2 && DateTime.Parse(FromTime) <= dtShift_BreakEndTime2) || (DateTime.Parse(EndTime) >= dtShift_BreakStartTime2 && DateTime.Parse(EndTime) <= dtShift_BreakEndTime2))
    //                                        {
    //                                            blnCheckBreak = true;
    //                                        }
    //                                        if (((dtShift_BreakStartTime2 >= DateTime.Parse(FromTime) && dtShift_BreakStartTime2 <=DateTime.Parse(EndTime)) || ( dtShift_BreakEndTime2 >= DateTime.Parse(FromTime) && dtShift_BreakEndTime2 <=DateTime.Parse(EndTime) )))
    //                                        {
    //                                            blnCheckBreak = true;
    //                                        }
    //                                    }
    //                                    if (strBreakStart3 != string.Empty && strBreakEnd3 != string.Empty)
    //                                    {
    //                                        if ((DateTime.Parse(FromTime) >= dtShift_BreakStartTime3 && DateTime.Parse(FromTime) <= dtShift_BreakEndTime3) || (DateTime.Parse(EndTime) >= dtShift_BreakStartTime3 && DateTime.Parse(EndTime) <= dtShift_BreakEndTime3))
    //                                        {
    //                                            blnCheckBreak = true;
    //                                        }
    //                                        if (((dtShift_BreakStartTime3 >= DateTime.Parse(FromTime) && dtShift_BreakStartTime3 <= DateTime.Parse(EndTime)) || (dtShift_BreakEndTime3 >= DateTime.Parse(FromTime) && dtShift_BreakEndTime3 <= DateTime.Parse(EndTime))))
    //                                        {
    //                                            blnCheckBreak = true;
    //                                        }
    //                                    }
    //                                    string tempShiftStartTime = string.Empty;
    //                                    if (ShiftStartTime.Length == 5)
    //                                    {
    //                                        tempShiftStartTime=ShiftStartTime.Substring(0, 2);
    //                                        if (Convert.ToInt32(tempShiftStartTime) < 12)
    //                                        {
    //                                            if (Convert.ToInt32(tempShiftStartTime) == 0)
    //                                            {
    //                                                ShiftStartTime = "12:" + ShiftStartTime.Substring(3, 2) + " " +"AM"; 
    //                                            }
    //                                            else
    //                                            {
    //                                                ShiftStartTime = ShiftStartTime + " " + "AM";
    //                                            }
    //                                        }
    //                                        else
    //                                        {
    //                                            ShiftStartTime = ShiftStartTime + " " + "PM";
    //                                        }
    //                                    }
    //                                    string tempstrShiftEndTime = string.Empty;
    //                                    if (ShiftEndTime.Length == 5)
    //                                    {
    //                                        tempstrShiftEndTime = ShiftEndTime.Substring(0,2);
    //                                        if (Convert.ToInt32(tempstrShiftEndTime) < 12)
    //                                        {
    //                                            if (Convert.ToInt32(tempstrShiftEndTime) == 0)
    //                                            {
    //                                                ShiftEndTime = "12:" + ShiftEndTime.Substring(3, 2) + " " + "AM";
    //                                            }
    //                                            else
    //                                            {
    //                                                ShiftEndTime = ShiftEndTime + " " + "AM";
    //                                            }
    //                                        }
    //                                        else
    //                                        {
    //                                            ShiftEndTime = ShiftEndTime + " " + "PM";
    //                                        }
    //                                    }
    //                                    string strShiftStartTime = Convert.ToString(PermissionDate) + " " + ShiftStartTime;
    //                                    string strShiftEndTime = Convert.ToString(PermissionDate) + " " + ShiftEndTime;
    //                                    DateTime dtShiftStartTime;
    //                                    DateTime dtShiftEndTime;
    //                                    //Shift Checking
    //                                    string TempShiftStart = "";
    //                                    string TempShiftEnd = "";
    //                                    TempShiftStart = ShiftStartTime.Substring(6, 2);
    //                                    TempShiftEnd = ShiftEndTime.Substring(6, 2);
    //                                    dtShiftStartTime = DateTime.Parse(strShiftStartTime);
    //                                    dtShiftEndTime = DateTime.Parse(strShiftEndTime);
    //                                    if (TempShiftStart == "AM" && TempShiftEnd == "PM")
    //                                    {
    //                                        if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime)
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                        if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime)
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                    }
    //                                    if (TempShiftStart == "PM" && TempShiftEnd == "AM")
    //                                    {
    //                                        if (Convert.ToString(dtmIn).Substring(Convert.ToString(dtmIn).Length - 2, 2) == "AM")
    //                                        {
    //                                            dtmIn = dtmIn.AddDays(1);
    //                                        }
    //                                        if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime.AddDays(1))
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                        if (Convert.ToString(dtmOut).Substring(Convert.ToString(dtmOut).Length - 2, 2) == "AM")
    //                                        {
    //                                            dtmOut = dtmOut.AddDays(1);
    //                                        }
    //                                        if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime.AddDays(1))
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                    }
    //                                    if (TempShiftStart == "AM" && TempShiftEnd == "AM")
    //                                    {
    //                                        if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime)
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                        if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime)
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                    }
    //                                    if (TempShiftStart == "PM" && TempShiftEnd == "PM")
    //                                    {
    //                                        if (dtmIn >= dtShiftStartTime && dtmIn <= dtShiftEndTime)
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                        if (dtmOut >= dtShiftStartTime && dtmOut <= dtShiftEndTime)
    //                                        {
    //                                        }
    //                                        else
    //                                        {
    //                                            Response.Clear();
    //                                            Response.Write("InValidHours");
    //                                            Response.End();
    //                                            return;
    //                                        }
    //                                    }
    //                                    //end Shift Checking
    //                                }
    //                            }
    //                        }
    //                    }
    //                    else
    //                    {
    //                        Response.Clear();
    //                        Response.Write("ShiftNotAllocated");
    //                        Response.End();
    //                        return;
    //                    }
    //                }
    //            }
    //        }
    //    }
    //     string s = tsDiff.ToString();
    //    //tsDiff = tsEndTime.Subtract(tsStartTime);
    //   int sReturn = s.LastIndexOf(':');
    //   string RemainHours=s.Remove(sReturn);
    //    ds_MaxNoOfHours = objPermissionEventData.GetMaxNoOfHours_ByLeavePolicy(Convert.ToInt32(Session.Contents["EmpAutoId"]));
    //    if (ds_MaxNoOfHours.Tables[0].Rows.Count != 0)
    //    {
    //        foreach (DataRow dRow in ds_MaxNoOfHours.Tables[0].Rows)
    //        {
    //            if (dRow["Permission_MaxHrs_Month"] != null)
    //            {
    //                MaxPermitHours = Convert.ToString(dRow["Permission_MaxHrs_Month"]);
    //            }
    //        }
    //        //string[] MaxPermissionHours = MaxPermitHours.Split(':');
    //        //MaxPermitHours = MaxPermissionHours[0] + "." + MaxPermissionHours[1];
    //    }
    //   // TimeSpan intervalHours = new TimeSpan();
    //    // intervalHours = DateTime.Parse(MaxPermitHours).Subtract(DateTime.Parse(s));
    //    Response.Clear();
    //     //Change on 21-Aug_08
    //   // Response.Write(Math.Round(tsDiff.TotalHours,1) + "," + MaxPermitHours);
    //    if (blnCheckBreak == true)
    //    {
    //        Response.Write(RemainHours + "," + "InBetweenBreakHours");
    //    }
    //    else
    //    {
    //        Response.Write(RemainHours + "," + MaxPermitHours);
    //    }
    //        Response.End();
    //}
    public int CheckDays(string FromTime, string EndTime)
    {
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        //FromTime = objClassCommon.DateConvertion(gstrDateFormat, FromTime);
        //EndTime = objClassCommon.DateConvertion(gstrDateFormat, EndTime);
        DateTime dtmFromDate = DateTime.Parse(FromTime);
        DateTime dtmToDate = DateTime.Parse(EndTime);
        for (DateTime dtmDate = dtmFromDate; dtmDate <= dtmToDate; dtmDate = dtmDate.AddDays(1))
        {
            int intShiftId = 0;
            DataTable dtTempShift = new DataTable();
            dtTempShift = objShiftExchangeEventData.SelectTemporaryShiftId(EmpID, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
            DataTable dtActualShift = new DataTable();
            dtActualShift = objShiftExchangeEventData.SelectPermanentShiftId(EmpID, dtmDate.Year.ToString(), dtmDate.Month.ToString(), dtmDate.Day.ToString());
            if (dtTempShift.Rows.Count > 0)
            {
                if (dtTempShift.Rows[0][0] != DBNull.Value)
                {
                    intShiftId = int.Parse(dtTempShift.Rows[0][0].ToString());
                }
            }
            if (intShiftId == 0)
            {
                if (dtActualShift.Rows.Count > 0)
                {
                    if (dtActualShift.Rows[0][0] != DBNull.Value)
                    {
                        intShiftId = int.Parse(dtActualShift.Rows[0][0].ToString());
                    }
                }
            }
            int count = 0;
            count = CheckAdditionalWeekOff(dtmDate, gstrSwipeCardNo);
            if (count == 1)
            {
                intShiftId = -1;
            }
            //Check Whether the date is holiday
            DataTable dtHoliday = new DataTable();
            dtHoliday = objHolidayEventData.CheckDateForHoliday(dtmDate.ToShortDateString(), EmpID);
            if (dtHoliday.Rows.Count > 0 || intShiftId == -1)
            {
                gintWeeklyOffHolidayCount = 1;
            }
        }
        return gintWeeklyOffHolidayCount;
    }
    public int CheckAdditionalWeekOff(DateTime dtmDate, string EmployeeId)
    {
        DateTime startOfMonth;
        string strStartOfMonth;
        int DaysToAdd, NthWeek = 0, TargetDayOfWeek = 0;
        string flag = "NE";
        try
        {
            DataTable dtAdditionalWeekOff = new DataTable();
            dtAdditionalWeekOff = objMonthClosure.GetAdditionalWeekOff(EmployeeId, dtmDate.ToShortDateString());
            if (dtAdditionalWeekOff.Rows.Count > 0)
            {
                foreach (DataRow drRow in dtAdditionalWeekOff.Rows)
                {
                    if (drRow["Addl_WeekOff_Day"] != DBNull.Value && drRow["Addl_WeekOff_Day"].ToString() != string.Empty)
                    {
                        TargetDayOfWeek = GetWeekDay(drRow["Addl_WeekOff_Day"].ToString());
                    }
                    if (drRow["Addl_WeekOff_Week"] != DBNull.Value && drRow["Addl_WeekOff_Week"].ToString() != string.Empty)
                    {
                        string strTargetDayOfWeek = drRow["Addl_WeekOff_Week"].ToString();
                        if (strTargetDayOfWeek == "First Week")
                        {
                            NthWeek = 1;
                        }
                        else if (strTargetDayOfWeek == "Second Week")
                        {
                            NthWeek = 2;
                        }
                        else if (strTargetDayOfWeek == "Third Week")
                        {
                            NthWeek = 3;
                        }
                        else if (strTargetDayOfWeek == "Fourth Week")
                        {
                            NthWeek = 4;
                        }
                        else if (strTargetDayOfWeek == "Fifth Week")
                        {
                            NthWeek = 5;
                        }
                    }
                    if (NthWeek != 0 && TargetDayOfWeek != 0)
                    {
                        strStartOfMonth = dtmDate.Month + "/01/" + dtmDate.Year;
                        startOfMonth = DateTime.Parse(strStartOfMonth);
                        string WeekDayName = startOfMonth.DayOfWeek.ToString();
                        int WeekDay = GetWeekDay(WeekDayName);
                        DaysToAdd = (7 - (WeekDay + (7 - TargetDayOfWeek)) % 7) % 7;
                        DateTime dtmResult = startOfMonth.AddDays(DaysToAdd + ((NthWeek - 1) * 7));
                        if (dtmResult == dtmDate)
                        {
                            flag = "E";
                        }
                    }
                }
            }
            if (flag == "E")
            {
                return (1);
            }
            else
            {
                return (0);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public int GetWeekDay(string WeekDayName)
    {
        int intWeekDay = 0;
        switch (WeekDayName)
        {
            case "Sunday":
                intWeekDay = 1;
                break;
            case "Monday":
                intWeekDay = 2;
                break;
            case "Tuesday":
                intWeekDay = 3;
                break;
            case "Wednesday":
                intWeekDay = 4;
                break;
            case "Thursday":
                intWeekDay = 5;
                break;
            case "Friday":
                intWeekDay = 6;
                break;
            case "Saturday":
                intWeekDay = 7;
                break;
        }
        return (intWeekDay);
    }
}
