/// <summary>
        /// Save
        /// </summary>
        public void Save()
        {
            VacationsInformationTDS vacationsInformationChanges = (VacationsInformationTDS)Data.GetChanges();

            if (vacationsInformationChanges.RequestsInformation.Rows.Count > 0)
            {
                VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(vacationsInformationChanges);

                foreach (VacationsInformationTDS.RequestsInformationRow row in (VacationsInformationTDS.RequestsInformationDataTable)vacationsInformationChanges.RequestsInformation)
                {
                    int requestId = row.RequestID;
                    int employeeId = row.EmployeeID;
                    bool deleted = row.Deleted;
                    int companyId = row.COMPANY_ID;

                    // original values
                    DateTime originalStartDate = vacationsInformationRequestsInformationGateway.GetStartDateOriginal(requestId);
                    DateTime originalEndDate = vacationsInformationRequestsInformationGateway.GetEndDateOriginal(requestId);
                    double originalTotalPaidVacationDays = vacationsInformationRequestsInformationGateway.GetTotalPaidVacationDaysOriginal(requestId);
                    string originalState = vacationsInformationRequestsInformationGateway.GetStateOriginal(requestId);
                    string originalComments = vacationsInformationRequestsInformationGateway.GetCommentsOriginal(requestId);
                    string originalDetails = vacationsInformationRequestsInformationGateway.GetDetailsOriginal(requestId);
                    string originalRejectReason = vacationsInformationRequestsInformationGateway.GetRejectReasonOriginal(requestId);
                    string originalCancelReason = vacationsInformationRequestsInformationGateway.GetCancelReasonOriginal(requestId);

                    // new values
                    DateTime newStartDate = vacationsInformationRequestsInformationGateway.GetStartDate(requestId);
                    DateTime newEndDate = vacationsInformationRequestsInformationGateway.GetEndDate(requestId);
                    double newTotalPaidVacationDays = vacationsInformationRequestsInformationGateway.GetTotalPaidVacationDays(requestId);
                    string newState = vacationsInformationRequestsInformationGateway.GetState(requestId);
                    string newComments = vacationsInformationRequestsInformationGateway.GetComments(requestId);
                    string newDetails = vacationsInformationRequestsInformationGateway.GetDetails(requestId);
                    string newRejectReason = vacationsInformationRequestsInformationGateway.GetRejectReason(requestId);
                    string newCancelReason = vacationsInformationRequestsInformationGateway.GetCancelReason(requestId);

                    if (newState == "Rejected" || newState == "Cancelled")
                    {
                        VacationsInformationBasicInformationGateway vacationsInformationBasicInformationGateway = new VacationsInformationBasicInformationGateway();
                        vacationsInformationBasicInformationGateway.LoadByEmployeeIdYear(employeeId, originalStartDate.Year, companyId);
                        double oldTotalTakenVacationDays = vacationsInformationBasicInformationGateway.GetTotalVacationDays(employeeId, originalStartDate.Year) - vacationsInformationBasicInformationGateway.GetRemainingPayVacationDays(employeeId, originalStartDate.Year);

                        VacationsEmployeeMaxPaidVacations vacationsEmployeeMaxPaidVacations = new VacationsEmployeeMaxPaidVacations();
                        double newTotalTakenVacationDays = oldTotalTakenVacationDays - originalTotalPaidVacationDays;
                        vacationsEmployeeMaxPaidVacations.UpdateTotalTakenVacationDays(row.StartDate.Year, row.EmployeeID, newTotalTakenVacationDays);
                    }
                    else
                    {
                        if (originalState == newState)
                        {
                            VacationsInformationBasicInformationGateway vacationsInformationBasicInformationGateway = new VacationsInformationBasicInformationGateway();
                            vacationsInformationBasicInformationGateway.LoadByEmployeeIdYear(employeeId, originalStartDate.Year, companyId);
                            double oldTotalTakenVacationDays = vacationsInformationBasicInformationGateway.GetTotalVacationDays(employeeId, originalStartDate.Year) - vacationsInformationBasicInformationGateway.GetRemainingPayVacationDays(employeeId, originalStartDate.Year);

                            VacationsEmployeeMaxPaidVacations vacationsEmployeeMaxPaidVacations = new VacationsEmployeeMaxPaidVacations();
                            double newTotalTakenVacationDays = oldTotalTakenVacationDays + newTotalPaidVacationDays;
                            vacationsEmployeeMaxPaidVacations.UpdateTotalTakenVacationDays(row.StartDate.Year, row.EmployeeID, newTotalTakenVacationDays);
                        }
                    }

                    VacationRequests vacationRequests = new VacationRequests(null);
                    vacationRequests.UpdateDirect(requestId, employeeId, originalStartDate, originalEndDate, originalTotalPaidVacationDays, originalState, originalComments, originalDetails, originalRejectReason, originalCancelReason, deleted, companyId, requestId, employeeId, newStartDate, newEndDate, newTotalPaidVacationDays, newState, newComments, newDetails, newRejectReason, newCancelReason, deleted, companyId);
                }
            }
        }
        /// <summary>
        /// Save
        /// </summary>
        public void SaveForEdit(double newTakenDays)
        {
            VacationsInformationTDS vacationsInformationChanges = (VacationsInformationTDS)Data.GetChanges();

            if (vacationsInformationChanges.RequestsInformation.Rows.Count > 0)
            {
                VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(vacationsInformationChanges);

                foreach (VacationsInformationTDS.RequestsInformationRow row in (VacationsInformationTDS.RequestsInformationDataTable)vacationsInformationChanges.RequestsInformation)
                {
                    int requestId = row.RequestID;
                    int employeeId = row.EmployeeID;
                    bool deleted = row.Deleted;
                    int companyId = row.COMPANY_ID;

                    // original values
                    DateTime originalStartDate = vacationsInformationRequestsInformationGateway.GetStartDateOriginal(requestId);
                    DateTime originalEndDate = vacationsInformationRequestsInformationGateway.GetEndDateOriginal(requestId);
                    double originalTotalPaidVacationDays = vacationsInformationRequestsInformationGateway.GetTotalPaidVacationDaysOriginal(requestId);
                    string originalState = vacationsInformationRequestsInformationGateway.GetStateOriginal(requestId);
                    string originalComments = vacationsInformationRequestsInformationGateway.GetCommentsOriginal(requestId);
                    string originalDetails = vacationsInformationRequestsInformationGateway.GetDetailsOriginal(requestId);
                    string originalRejectReason = vacationsInformationRequestsInformationGateway.GetRejectReasonOriginal(requestId);
                    string originalCancelReason = vacationsInformationRequestsInformationGateway.GetCancelReasonOriginal(requestId);
                    bool originalDeleted = vacationsInformationRequestsInformationGateway.GetDeletedOriginal(requestId);

                    // new values
                    DateTime newStartDate = vacationsInformationRequestsInformationGateway.GetStartDate(requestId);
                    DateTime newEndDate = vacationsInformationRequestsInformationGateway.GetEndDate(requestId);
                    double newTotalPaidVacationDays = vacationsInformationRequestsInformationGateway.GetTotalPaidVacationDays(requestId);
                    string newState = vacationsInformationRequestsInformationGateway.GetState(requestId);
                    string newComments = vacationsInformationRequestsInformationGateway.GetComments(requestId);
                    string newDetails = vacationsInformationRequestsInformationGateway.GetDetails(requestId);
                    string newRejectReason = vacationsInformationRequestsInformationGateway.GetRejectReason(requestId);
                    string newCancelReason = vacationsInformationRequestsInformationGateway.GetCancelReason(requestId);
                    bool newDeleted = vacationsInformationRequestsInformationGateway.GetDeleted(requestId);

                    VacationsEmployeeMaxPaidVacations vacationsEmployeeMaxPaidVacations = new VacationsEmployeeMaxPaidVacations();
                    vacationsEmployeeMaxPaidVacations.UpdateTotalTakenVacationDays(row.StartDate.Year, row.EmployeeID, newTakenDays);

                    VacationRequests vacationRequests = new VacationRequests(null);
                    vacationRequests.UpdateDirect(requestId, employeeId, originalStartDate, originalEndDate, originalTotalPaidVacationDays, originalState, originalComments, originalDetails, originalRejectReason, originalCancelReason, originalDeleted, companyId, requestId, employeeId, newStartDate, newEndDate, newTotalPaidVacationDays, newState, newComments, newDetails, newRejectReason, newCancelReason, newDeleted, companyId);
                }
            }
        }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS - DATASET
 //
 /// <summary>
 /// LoadByRequestId
 /// </summary>
 /// <param name="requestId">requestId</param>
 /// <param name="companyId">companyId</param>
 public void LoadByRequestId(int requestId, int companyId)
 {
     VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(Data);
     vacationsInformationRequestsInformationGateway.LoadByRequestId(requestId, companyId);
 }
        // ////////////////////////////////////////////////////////////////////////
        // PRIVATE METHODS
        //
        private void LoadData(int employeeId, int year, int requestId)
        {
            VacationsInformationBasicInformationGateway vacationsInformationBasicInformationGateway = new VacationsInformationBasicInformationGateway(vacationsInformationTDS);
            VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(vacationsInformationTDS);

            tbxEmployee.Text = vacationsInformationBasicInformationGateway.GetEmployeeName(employeeId, year);
            tbxMax.Text = vacationsInformationBasicInformationGateway.GetTotalVacationDays(employeeId, year).ToString();
            tbxRemaining.Text = vacationsInformationBasicInformationGateway.GetRemainingPayVacationDays(employeeId, year).ToString();
            tbxComments.Text = vacationsInformationRequestsInformationGateway.GetComments(requestId);
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Tag page
                // ... for non vacation managers
                EmployeeGateway employeeGatewayManager = new EmployeeGateway();
                int employeeIdNow = employeeGatewayManager.GetEmployeIdByLoginId(Convert.ToInt32(Session["loginID"]));
                employeeGatewayManager.LoadByEmployeeId(employeeIdNow);

                if (employeeGatewayManager.GetIsVacationsManager(employeeIdNow))
                {
                    hdfIsVacationManager.Value = "True";
                }
                else
                {
                    hdfIsVacationManager.Value = "False";
                }

                // Security check
                if (hdfIsVacationManager.Value == "False")
                {
                    //if (!(Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_VACATIONS_EDIT"])))
                    //{
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    //}
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) && ((string)Request.QueryString["vacation_id"] == null) && ((string)Request.QueryString["request_id"] == null) && ((string)Request.QueryString["date_to_show"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in vacations_edit.aspx");
                }

                // Tag page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfVacationId.Value = (string)Request.QueryString["vacation_id"];
                hdfRequestId.Value = (string)Request.QueryString["request_id"];

                int requestId = Int32.Parse(hdfRequestId.Value);
                int companyId = Int32.Parse(hdfCompanyId.Value);

                ViewState["date_to_show"] = (string)Request.QueryString["date_to_show"];
                DateTime dateToShow = DateTime.Parse((string)Request.QueryString["date_to_show"]);

                // Initialize values
                DropDownList ddlVacationsFor = (DropDownList)tkrpbLeftMenuAllVacations.FindItemByValue("nbVacationsForDDL").FindControl("ddlVacationsFor");
                EmployeeList employeeList = new EmployeeList();
                string employeeTypeNow = employeeGatewayManager.GetType(employeeIdNow);
                if (employeeTypeNow.Contains("CA"))
                {
                    employeeList.LoadBySalariedEmployeeTypeAndAddItem(1, "CA", -1, "(All)");
                }
                else
                {
                    employeeList.LoadBySalariedEmployeeTypeAndAddItem(1, "US", -1, "(All)");
                }
                ddlVacationsFor.DataSource = employeeList.Table;
                ddlVacationsFor.DataValueField = "EmployeeID";
                ddlVacationsFor.DataTextField = "FullName";
                ddlVacationsFor.DataBind();
                ddlVacationsFor.SelectedValue = Session["ddlVacationsForSelectedValue"].ToString();

                // Load Data
                vacationsInformationTDS = new VacationsInformationTDS();
                vacationDaysInformation = new VacationsInformationTDS.DaysInformationDataTable();

                VacationsInformationDaysInformationGateway vacationsInformationDaysInformationGateway = new VacationsInformationDaysInformationGateway(vacationsInformationTDS);
                vacationsInformationDaysInformationGateway.LoadByRequestId(requestId, companyId);

                VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(vacationsInformationTDS);
                vacationsInformationRequestsInformationGateway.LoadByRequestId(requestId, companyId);

                hdfEmployeeId.Value = vacationsInformationRequestsInformationGateway.GetEmployeeID(requestId).ToString();
                int employeeId = Int32.Parse(hdfEmployeeId.Value);

                VacationsInformationBasicInformationGateway vacationsInformationBasicInformationGateway = new VacationsInformationBasicInformationGateway(vacationsInformationTDS);
                vacationsInformationBasicInformationGateway.LoadByEmployeeIdYear(employeeId, dateToShow.Year, companyId);

                Session["vacationsInformationTDS"] = vacationsInformationTDS;
                Session["vacationDaysInformation"] = vacationsInformationTDS.DaysInformation;

                LoadData(employeeId, dateToShow.Year, requestId);

                Page.DataBind();
            }
            else
            {
                vacationsInformationTDS = (VacationsInformationTDS)Session["vacationsInformationTDS"];
                vacationDaysInformation = (VacationsInformationTDS.DaysInformationDataTable)Session["vacationDaysInformation"];
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Set active toolbar
            mForm6 master = (mForm6)this.Master;
            master.ActiveToolbar = "LabourHours";

            // for non vacation managers
            if (hdfIsVacationManager.Value == "False")
            {
                // Validate left menu
                tkrpbLeftMenuAllVacations.Visible = false; // All Vacations
                tkrpbLeftMenuMyVacations.Visible = true; // My Vacations

                // Validate Top menu for Admin
                tkrmTop.Items[0].Visible = false; // Edit button

                // Validate reports
                tkrpbLeftMenuReports.Visible = false;
            }
            else
            {
                // Validate reports
                if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_VACATIONS_REPORTS"]))
                {
                    tkrpbLeftMenuReports.Visible = false;
                }
            }

            if (!(Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_VACATIONS_HOLIDAY_FULL_EDITING"])))
            {
                // Validate tools menu
                tkrpbLeftMenuTools.Visible = false;
                //tkrpbLeftMenuTools.Items[2].Visible = false;
                //tkrpbLeftMenuTools.Items[3].Visible = false;
            }

            VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(vacationsInformationTDS);
            string state = vacationsInformationRequestsInformationGateway.GetState(Int32.Parse(hdfRequestId.Value));

            if (state == "Rejected" || state == "Cancelled")
            {
                tkrmTop.Items[0].Visible = false;
            }

            tkrsVacations.SelectedDate = DateTime.Parse(ViewState["date_to_show"].ToString());
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                EmployeeGateway employeeGateway1 = new EmployeeGateway();
                int employeeIdNow = employeeGateway1.GetEmployeIdByLoginId(Convert.ToInt32(Session["loginID"]));
                employeeGateway1.LoadByEmployeeId(employeeIdNow);

                if (!employeeGateway1.GetIsVacationsManager(employeeIdNow))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) && ((string)Request.QueryString["date_to_show"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in vacations_approve_vacation_request.aspx");
                }

                //recibe opcionalmente el employee_id
                // Tag page
                int companyId = Int32.Parse(Session["companyID"].ToString());
                hdfCompanyId.Value = companyId.ToString();

                ViewState["rejected_by_id"] = employeeGateway1.GetEmployeIdByLoginId(Convert.ToInt32(Session["loginID"]));

                //recibe opcionalmente el employee_id y date_to_show
                if ((string)Request.QueryString["date_to_show"] == null)
                {
                    ViewState["date_to_show"] = DateTime.Now.ToString();
                }
                else
                {
                    ViewState["date_to_show"] = (string)Request.QueryString["date_to_show"];
                }

                if ((string)Request.QueryString["employee_id"] == null)
                {
                    hdfEmployeeId.Value = "-1";
                }
                else
                {
                    hdfEmployeeId.Value = (string)Request.QueryString["employee_id"];
                }

                EmployeeList employeeList = new EmployeeList();
                employeeList.LoadBySalariedAndAddItem(1, -1, "(All)");

                DropDownList ddlVacationsFor = (DropDownList)tkrpbLeftMenuAllVacations.FindItemByValue("nbVacationsForDDL").FindControl("ddlVacationsFor");
                ddlVacationsFor.DataSource = employeeList.Table;
                ddlVacationsFor.DataValueField = "EmployeeID";
                ddlVacationsFor.DataTextField = "FullName";
                ddlVacationsFor.DataBind();
                ddlVacationsFor.SelectedValue = Session["ddlVacationsForSelectedValue"].ToString();

                vacationsInformationTDS = new VacationsInformationTDS();
                VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(vacationsInformationTDS);

                vacationsInformationRequestsInformationGateway.LoadByState("For Approval", companyId);

                grdVacations.DataSource = vacationsInformationTDS.RequestsInformation;

                Session["vacationsInformationTDS"] = vacationsInformationTDS;

                Page.DataBind();

                // Check results
                if (vacationsInformationTDS.RequestsInformation.Rows.Count > 0)
                {
                    tdNoResults.Visible = false;
                    lblTotalRows.Visible = true;
                    lblTotalRows.Text = "Total Rows: " + vacationsInformationTDS.RequestsInformation.Rows.Count;
                }
                else
                {
                    tdNoResults.Visible = true;
                    lblTotalRows.Visible = false;
                }
            }
            else
            {
                vacationsInformationTDS = (VacationsInformationTDS)Session["vacationsInformationTDS"];
            }
        }
        private void SendMailEmployee(string newState)
        {
            if (newState == "Rejected")
            {
                // Update grid rows
                foreach (GridViewRow row in grdVacations.Rows)
                {
                    int requestId = Int32.Parse(grdVacations.DataKeys[row.RowIndex].Values["RequestID"].ToString());
                    if (((CheckBox)row.FindControl("cbxSelected")).Checked)
                    {
                        string rejectReason = ((TextBox)row.FindControl("tbxRejectReason")).Text;

                        // Get mail information
                        string mailTo = "";
                        string nameTo = "";
                        string body = "";
                        string subject = "";

                        VacationsInformationRequestsInformationGateway vacationsInformationRequestsInformationGateway = new VacationsInformationRequestsInformationGateway(vacationsInformationTDS);

                        int employeeId = vacationsInformationRequestsInformationGateway.GetEmployeeID(requestId);
                        int rejectedById = (int)ViewState["rejected_by_id"];

                        EmployeeGateway employeesGateway = new EmployeeGateway();
                        employeesGateway.LoadByEmployeeId(employeeId);

                        if (employeesGateway.Table.Rows.Count > 0)
                        {
                            // Assigned TeamMember
                            mailTo = employeesGateway.GetEMail(employeeId);
                            nameTo = employeesGateway.GetFirstName(employeeId) + " " + employeesGateway.GetLastName(employeeId);
                        }

                        subject = "Vacation request from " + vacationsInformationRequestsInformationGateway.GetStartDate(requestId).ToShortDateString() + " to " + vacationsInformationRequestsInformationGateway.GetEndDate(requestId).ToShortDateString();

                        // Mails body
                        body = body + "\nHi " + nameTo + ",\n\nThe following vacation request has been rejected: \n\n";
                        body = body + "\t - Vacation request from " + vacationsInformationRequestsInformationGateway.GetStartDate(requestId).ToShortDateString() + " to " + vacationsInformationRequestsInformationGateway.GetEndDate(requestId).ToShortDateString() + "\n";
                        body = body + "\t - Detail: " + vacationsInformationRequestsInformationGateway.GetDetails(requestId) + "\n";
                        body = body + "\t - Comment: " + rejectReason;

                        EmployeeGateway employeeRejected = new EmployeeGateway();
                        employeeRejected.LoadByEmployeeId(rejectedById);

                        if (employeeRejected.Table.Rows.Count > 0)
                        {
                            body = body + "\n" + "\t - Rejected by: " + employeeRejected.GetFirstName(rejectedById) + " " + employeeRejected.GetLastName(rejectedById);
                        }

                        //Send Mail
                        SendMail(mailTo, subject, body);
                    }
                }
            }
        }