protected void btnAssociate_Click(object sender, EventArgs e)
        {
            Page.Validate();

            if (Page.IsValid)
            {
                string associateCategory = tvCategoriesRoot.SelectedNode.Text;
                int libraryCategoryId = int.Parse(tvCategoriesRoot.SelectedNode.Value);

                ServiceRequestsManagerToolBasicInformation serviceRequestsManagerToolBasicInformation = new ServiceRequestsManagerToolBasicInformation(serviceRequestsManagerToolTDS);
                serviceRequestsManagerToolBasicInformation.UpdateLibraryCategoriesId(int.Parse(ViewState["serviceId"].ToString()), libraryCategoryId);

                Session["serviceRequestsManagerToolTDS"] = serviceRequestsManagerToolTDS;

                Response.Write("<script language='javascript'> {window.close();}</script>");
            }
        }
        protected void btnUnassociate_Click(object sender, EventArgs e)
        {
            SaveTemp();
            PostPageChanges();

            ServiceRequestsManagerToolBasicInformation serviceRequestsManagerToolBasicInformation = new ServiceRequestsManagerToolBasicInformation(serviceRequestsManagerToolTDS);
            serviceRequestsManagerToolBasicInformation.UpdateLibraryCategoriesId(int.Parse(hdfSelectedSRId.Value), null);

            ViewState["update"] = "no";

            btnUnassociate.Visible = false;
            btnAssociate.Visible = true;
            tbxCategoryAssocited.Text = "";
        }
        private void UpdateDatabase()
        {
            LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway(libraryTDSForServices);
            libraryFilesGateway.Update();

            DB.Open();
            DB.BeginTransaction();
            try
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int serviceId = Int32.Parse(hdfSelectedSRId.Value);

                ServiceRequestsManagerToolBasicInformation serviceRequestsManagerToolBasicInformation = new ServiceRequestsManagerToolBasicInformation(serviceRequestsManagerToolTDS);
                serviceRequestsManagerToolBasicInformation.Save(companyId);

                // Save costs information
                ServiceRequestsManagerToolCostInformation serviceRequestsManagerToolCostInformation = new ServiceRequestsManagerToolCostInformation(serviceRequestsManagerToolTDS);
                serviceRequestsManagerToolCostInformation.Save((int)serviceId, companyId);

                // Save notes information
                ServiceInformationServiceNoteGateway serviceInformationServiceNoteGateway = new ServiceInformationServiceNoteGateway(serviceInformationTDS);
                ServiceInformationServiceNote serviceInformationServiceNote = new ServiceInformationServiceNote(serviceInformationTDS);

                foreach (ServiceInformationTDS.NoteInformationRow rowNotes in (ServiceInformationTDS.NoteInformationDataTable)serviceInformationServiceNoteGateway.Table)
                {
                    if (!rowNotes.IsLIBRARY_FILES_IDNull())
                    {
                        if (rowNotes.LIBRARY_FILES_ID == 0 && rowNotes.FILENAME != "")
                        {
                            libraryFilesGateway.LoadByFileName(rowNotes.FILENAME, companyId);
                            int newLibraryFilesId = libraryFilesGateway.GetlibraryFilesId(rowNotes.FILENAME);

                            rowNotes.LIBRARY_FILES_ID = newLibraryFilesId;
                        }
                    }
                }

                // Save notes information
                serviceInformationServiceNote.Save(companyId);

                DB.CommitTransaction();

                // Store datasets
                serviceRequestsManagerToolTDS.AcceptChanges();
                Session["serviceRequestsManagerToolTDS"] = serviceRequestsManagerToolTDS;

                // Store datasets
                libraryTDSForServices.AcceptChanges();
                serviceInformationTDS.AcceptChanges();

                Session["serviceInformationTDSForManagerTool"] = serviceInformationTDS;
                Session["libraryTDSForServices"] = libraryTDSForServices;

                Session.Remove("libraryTDSForServices");
                Session.Remove("serviceInformationTDSForManagerTool");
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP2 - STEPS INFORMATION - METHODS
        //
        private void StepStepsInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "";

            // Load data for selected service request
            int serviceId = Int32.Parse(hdfSelectedSRId.Value);
            int companyId = Int32.Parse(hdfCompanyId.Value);

            ServiceRequestsManagerToolBasicInformation serviceRequestsManagerToolBasicInformation = new ServiceRequestsManagerToolBasicInformation(serviceRequestsManagerToolTDS);
            ServiceRequestsManagerToolCostInformation serviceRequestsManagerToolCostInformation = new ServiceRequestsManagerToolCostInformation(serviceRequestsManagerToolTDS);
            ServiceInformationServiceNote serviceInformationServiceNoteForEdit = new ServiceInformationServiceNote(serviceInformationTDS);

            if (Session["dialogOpenedServicesManagerTool"] == null)
            {
                // ... Load basic data
                serviceRequestsManagerToolBasicInformation.LoadByServiceId(serviceId, companyId);

                // ... Load costs
                serviceRequestsManagerToolCostInformation.LoadByServiceId(serviceId, companyId);

                //... Load notes
                serviceInformationServiceNoteForEdit.LoadByServiceId(serviceId, companyId);
            }
            else
            {
                ServiceRequestsManagerToolBasicInformationGateway serviceRequestsManagerToolBasicInformationGateway2 = new ServiceRequestsManagerToolBasicInformationGateway(serviceRequestsManagerToolBasicInformation.Data);

                Session.Remove("dialogOpenedServicesManagerTool");

                if (serviceRequestsManagerToolBasicInformationGateway2.GetAcceptedDateTime(serviceId).HasValue) ckbxAcceptSR.Checked = true;
                if (serviceRequestsManagerToolBasicInformationGateway2.GetStartWorkDateTime(serviceId).HasValue) ckbxStartSR.Checked = true;
                if (serviceRequestsManagerToolBasicInformationGateway2.GetUnitOutOfServiceDate(serviceId).HasValue) tkrdpStartWorkUnitOutOfServiceDate.SelectedDate = serviceRequestsManagerToolBasicInformationGateway2.GetUnitOutOfServiceDate(serviceId);
                tbxStartWorkStartMileage.Text = serviceRequestsManagerToolBasicInformationGateway2.GetStartWorkMileage(serviceId);
                if (serviceRequestsManagerToolBasicInformationGateway2.GetCompleteWorkDateTime(serviceId).HasValue) ckbxCompleteSR.Checked = true;
                if (serviceRequestsManagerToolBasicInformationGateway2.GetUnitBackInServiceDate(serviceId).HasValue) tkrdpCompleteWorkUnitBackInServiceDate.SelectedDate = serviceRequestsManagerToolBasicInformationGateway2.GetUnitBackInServiceDate(serviceId);
                tbxCompleteWorkCompleteMileage.Text = serviceRequestsManagerToolBasicInformationGateway2.GetCompleteWorkMileage(serviceId);
                tbxCompleteWorkDataDescription.Text = serviceRequestsManagerToolBasicInformationGateway2.GetCompleteWorkDetailDescription(serviceId);
                ckbxPreventableTPV.Checked = serviceRequestsManagerToolBasicInformationGateway2.GetCompleteWorkDetailPreventable(serviceId);
                tbxCompleteWorkDataLabourHours.Text = serviceRequestsManagerToolBasicInformationGateway2.GetCompleteWorkDetailTMLabourHours(serviceId).ToString();
            }

            ServiceRequestsManagerToolBasicInformationGateway serviceRequestsManagerToolBasicInformationGateway = new ServiceRequestsManagerToolBasicInformationGateway(serviceRequestsManagerToolBasicInformation.Data);

            // Validate panels and information
            string state = serviceRequestsManagerToolBasicInformationGateway.GetServiceStateOriginal(serviceId);
            hdfState.Value = state;
            hdfServiceNumber.Value = serviceRequestsManagerToolBasicInformationGateway.GetServiceNumberOriginal(serviceId);
            hdfServiceDescription.Value = serviceRequestsManagerToolBasicInformationGateway.GetServiceDescriptionOriginal(serviceId);

            string originalThirdsPartyVendor = serviceRequestsManagerToolBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
            int? unitId = null;
            if (serviceRequestsManagerToolBasicInformationGateway.GetUnitId(serviceId).HasValue)
            {
                unitId = (int)serviceRequestsManagerToolBasicInformationGateway.GetUnitId(serviceId);
                hdfUnitId.Value = unitId.ToString();
            }

            // ... Validate vehicle information
            if (unitId.HasValue)
            {
                UnitsGateway unitsGateway = new UnitsGateway();
                unitsGateway.LoadByUnitId((int)unitId, companyId);
                string unitType = unitsGateway.GetType((int)unitId);
                hdfUnitType.Value = unitType;
                int companyLevel = unitsGateway.GetCompanyLevelId((int)unitId);
                hdfCompanyLevel.Value = unitsGateway.GetCompanyLevelId((int)unitId).ToString();

                CompanyLevelGateway companyLevelGateway = new CompanyLevelGateway();
                companyLevelGateway.LoadByCompanyLevelId(companyLevel, companyId);
                hdfMileageUnitOfMeasurement.Value = companyLevelGateway.GetUnitsUnitOfMeasurement(companyLevel);
                lblStartWorkStartMileageUnitOfMeasurement.Text = hdfMileageUnitOfMeasurement.Value;
                lblCompleteWorkCompleteMileageUnitOfMeasurement.Text = hdfMileageUnitOfMeasurement.Value;

                if (unitType == "Vehicle")
                {
                    lblStartWorkStartMileage.Visible = true;
                    tbxStartWorkStartMileage.Visible = true;
                    lblCompleteWorkCompleteMileage.Visible = true;
                    tbxCompleteWorkCompleteMileage.Visible = true;
                    lblStartWorkStartMileageUnitOfMeasurement.Visible = true;
                    lblCompleteWorkCompleteMileageUnitOfMeasurement.Visible = true;
                }
                else
                {
                    lblStartWorkStartMileage.Visible = false;
                    tbxStartWorkStartMileage.Visible = false;
                    lblStartWorkStartMileageUnitOfMeasurement.Visible = false;
                    lblCompleteWorkCompleteMileage.Visible = false;
                    tbxCompleteWorkCompleteMileage.Visible = false;
                    lblCompleteWorkCompleteMileageUnitOfMeasurement.Visible = false;
                }
            }

            // ... Validate visible internal panels (complete work info)
            if (state == "Unassigned")
            {
                if (!rbtnAssignToThirdPartyVendor.Checked)
                {
                    pnlAssignTeamMember.Visible = true;
                    pnlAssignThirdPartyVendor.Visible = false;
                }
                else
                {
                    pnlAssignThirdPartyVendor.Visible = true;
                    pnlAssignTeamMember.Visible = false;
                }
            }
            else
            {
                bool teamMemberAssigned = serviceRequestsManagerToolBasicInformationGateway.GetAssignTeamMember(serviceId);
                if (teamMemberAssigned)
                {
                    pnlAssignTeamMember.Visible = true;
                    pnlAssignThirdPartyVendor.Visible = false;
                }
                else
                {
                    pnlAssignThirdPartyVendor.Visible = true;
                    pnlAssignTeamMember.Visible = false;
                }
            }

            // ... Validate panels
            switch (state)
            {
                case "Unassigned":
                    pnlAssignmentInformation.Visible = true;
                    acceptInformationSeparator.Visible = true;
                    pnlAcceptInformation.Visible = true;
                    startWorkSeparator.Visible = true;
                    pnlStartWorkInformation.Visible = true;
                    completeWorkSeparator.Visible = true;
                    pnlCompleteWorkInformation.Visible = true;
                    break;

                case "Assigned":
                    pnlAssignmentInformation.Visible = false;
                    acceptInformationSeparator.Visible = false;
                    pnlAcceptInformation.Visible = true;
                    startWorkSeparator.Visible = true;
                    pnlStartWorkInformation.Visible = true;
                    completeWorkSeparator.Visible = true;
                    pnlCompleteWorkInformation.Visible = true;
                    break;

                case "Rejected":
                    pnlAssignmentInformation.Visible = true;
                    acceptInformationSeparator.Visible = true;
                    pnlAcceptInformation.Visible = true;
                    startWorkSeparator.Visible = true;
                    pnlStartWorkInformation.Visible = true;
                    completeWorkSeparator.Visible = true;
                    pnlCompleteWorkInformation.Visible = true;
                    break;

                case "Accepted":
                    pnlAssignmentInformation.Visible = false;
                    acceptInformationSeparator.Visible = false;
                    pnlAcceptInformation.Visible = false;
                    startWorkSeparator.Visible = false;
                    pnlStartWorkInformation.Visible = true;
                    completeWorkSeparator.Visible = true;
                    pnlCompleteWorkInformation.Visible = true;
                    break;

                case "In Progress":
                    pnlAssignmentInformation.Visible = false;
                    acceptInformationSeparator.Visible = false;
                    pnlAcceptInformation.Visible = false;
                    startWorkSeparator.Visible = false;
                    pnlStartWorkInformation.Visible = false;
                    completeWorkSeparator.Visible = false;
                    pnlCompleteWorkInformation.Visible = true;
                    break;

                default:
                    throw new Exception("The option for " + wzServices.ActiveStep.Name + " step in services_manager_tool.Wizard_ActiveStepChanged function does not exist");
            }

            hdfPnlAcceptInformation.Value = pnlAcceptInformation.Visible.ToString();
            hdfPnlAssignmentInformation.Value = pnlAssignmentInformation.Visible.ToString();
            hdfPnlStartWorkInformation.Value = pnlStartWorkInformation.Visible.ToString();
            hdfPnlCompleteWorkInformation.Value = pnlCompleteWorkInformation.Visible.ToString();

            // ... Load resource library
            int? libraryCategoriesId = null; if (serviceRequestsManagerToolBasicInformationGateway.GetLibraryCategoriesId(serviceId).HasValue) libraryCategoriesId = (int)serviceRequestsManagerToolBasicInformationGateway.GetLibraryCategoriesId(serviceId);

            if (libraryCategoriesId.HasValue)
            {
                ViewState["libraryCategoriesId"] = (int)libraryCategoriesId;
                tbxCategoryAssocited.Text = GetFullCategoryName((int)libraryCategoriesId, companyId);
                btnAssociate.Visible = false;
                btnUnassociate.Visible = true;
            }
            else
            {
                tbxCategoryAssocited.Text = "";
                btnAssociate.Visible = true;
                btnUnassociate.Visible = false;
            }

            // Store session
            Session["serviceRequestsManagerToolTDS"] = serviceRequestsManagerToolTDS;
        }
        private void PostPageChanges()
        {
            ServiceRequestsManagerToolBasicInformationGateway serviceRequestsManagerToolBasicInformationGateway = new ServiceRequestsManagerToolBasicInformationGateway(serviceRequestsManagerToolTDS);

            // basic variables
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfSelectedSRId.Value);

            int? libraryCategoriesId = null; if (serviceRequestsManagerToolBasicInformationGateway.GetLibraryCategoriesId(serviceId).HasValue) libraryCategoriesId = (int)serviceRequestsManagerToolBasicInformationGateway.GetLibraryCategoriesId(serviceId);

            // Service state
            string serviceState = "Assigned";
            string checklistState = "In Progress";
            if (ckbxAcceptSR.Checked) serviceState = "Accepted";
            if (ckbxStartSR.Checked) serviceState = "In Progress";
            if (ckbxCompleteSR.Checked)
            {
                serviceState = "Completed";
                checklistState = "Healthy";
            }

            // Get Assignment information
            DateTime? assignDeadlineDate = null;
            bool assignTeamMember = false;
            int? assignTeamMemberId = null;
            string assignThirdPartyVendor = "";
            DateTime? assignDateTime = null;

            if (hdfPnlAssignmentInformation.Value == "True")
            {
                // If data is modified
                if (hdfDeadlineDate.Value != "") assignDeadlineDate = DateTime.Parse(hdfDeadlineDate.Value);

                if ((hdfAssignToMyself.Value == "True") || (hdfAssignToTeamMember.Value == "True"))
                {
                    assignTeamMember = true;
                    assignTeamMemberId = Int32.Parse(hdfTeamMemberId.Value);
                }

                if (hdfThirdPartyVendor.Value != "")
                {
                    assignThirdPartyVendor = hdfThirdPartyVendor.Value;
                }

                assignDateTime = DateTime.Now;
            }
            else
            {
                // If data is not modified
                assignTeamMember = serviceRequestsManagerToolBasicInformationGateway.GetAssignTeamMember(serviceId);
                assignTeamMemberId = serviceRequestsManagerToolBasicInformationGateway.GetAssignTeamMemberId(serviceId);
                assignThirdPartyVendor = serviceRequestsManagerToolBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
                assignDeadlineDate = serviceRequestsManagerToolBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
                assignDateTime = serviceRequestsManagerToolBasicInformationGateway.GetAssignmentDateTime(serviceId);
            }

            // Get acceptance information
            DateTime? acceptDateTime = null;
            DateTime? rejectDateTime = null;
            string rejectReason = "";

            if (hdfPnlAcceptInformation.Value == "True")
            {
                // If data is modified
                if (hdfAcceptSR.Value == "True")
                {
                    acceptDateTime = DateTime.Now;
                }
            }
            else
            {
                // If data is not modified
                acceptDateTime = serviceRequestsManagerToolBasicInformationGateway.GetAcceptedDateTime(serviceId);
                rejectDateTime = serviceRequestsManagerToolBasicInformationGateway.GetRejectDateTime(serviceId);
                rejectReason = serviceRequestsManagerToolBasicInformationGateway.GetRejectReason(serviceId);
            }

            // Get start work information
            DateTime? startWorkDateTime = null;
            DateTime? unitOutOfServiceDate = null;
            string unitOutOfServiceTime = "";
            string startWorkMileage = "";

            if (hdfPnlStartWorkInformation.Value == "True")
            {
                // If data is modified
                if (hdfStartSR.Value == "True")
                {
                    startWorkDateTime = DateTime.Now;
                    if (hdfUnitOutOfServiceDate.Value != "") unitOutOfServiceDate = DateTime.Parse(hdfUnitOutOfServiceDate.Value);
                    unitOutOfServiceTime = hdfUnitOutOfServiceTime.Value;
                    if (hdfStartMileage.Value != "") startWorkMileage = hdfStartMileage.Value;
                }
            }
            else
            {
                // If data is not modified
                startWorkDateTime = serviceRequestsManagerToolBasicInformationGateway.GetStartWorkDateTime(serviceId);
                unitOutOfServiceDate = serviceRequestsManagerToolBasicInformationGateway.GetUnitOutOfServiceDate(serviceId);
                unitOutOfServiceTime = serviceRequestsManagerToolBasicInformationGateway.GetUnitOutOfServiceTime(serviceId);
                startWorkMileage = serviceRequestsManagerToolBasicInformationGateway.GetStartWorkMileage(serviceId);
            }

            // Get complete work information
            DateTime? completeWorkDateTime = null;
            DateTime? unitBackInServiceDate = null;
            string unitBackInServiceTime = "";
            string completeWorkDetailDescription = "";
            bool completeWorkDetailPreventable = false;
            Decimal? completeWorkDetailTMLabourHours = null;
            Decimal? completeWorkDetailTMCost = null;
            string completeWorkInvoiceNumber = "";
            Decimal? completeWorkInvoiceAmount = null;
            string completeWorkMileage = "";

            if (hdfPnlCompleteWorkInformation.Value == "True")
            {
                // If data is modified
                if (ckbxCompleteSR.Checked)
                {
                    completeWorkDateTime = DateTime.Now;
                    if (hdfUnitBackInServiceDate.Value != "") unitBackInServiceDate = DateTime.Parse(hdfUnitBackInServiceDate.Value);
                    unitBackInServiceTime = hdfUnitBackInServiceTime.Value;
                    completeWorkDetailDescription = hdfCompleteWorkDescription.Value;
                    completeWorkDetailPreventable = Boolean.Parse(hdfCompleteWorkPreventable.Value);
                    if (hdfCompleteWorkLabourHours.Value != "") completeWorkDetailTMLabourHours = decimal.Parse(hdfCompleteWorkLabourHours.Value);
                    if (hdfCompleteWorkCosts.Value != "") completeWorkDetailTMCost = decimal.Parse(hdfCompleteWorkCosts.Value);
                    if (hdfInvoiceNumber.Value != "") completeWorkInvoiceNumber = hdfInvoiceNumber.Value;
                    if (hdfInvoiceAmount.Value != "") completeWorkInvoiceAmount = decimal.Parse(hdfInvoiceAmount.Value);
                    if (hdfCompleteWorkMileage.Value != "") completeWorkMileage = hdfCompleteWorkMileage.Value;
                }
            }
            else
            {
                // If data is not modified
                completeWorkDateTime = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkDateTime(serviceId);
                unitBackInServiceDate = serviceRequestsManagerToolBasicInformationGateway.GetUnitBackInServiceDate(serviceId);
                unitBackInServiceTime = serviceRequestsManagerToolBasicInformationGateway.GetUnitBackInServiceTime(serviceId);
                completeWorkDetailDescription = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
                completeWorkDetailPreventable = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
                completeWorkDetailTMLabourHours = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId);
                completeWorkDetailTMCost = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkDetailTMCost(serviceId);
                completeWorkInvoiceNumber = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumber(serviceId);
                completeWorkInvoiceAmount = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId);
                completeWorkMileage = serviceRequestsManagerToolBasicInformationGateway.GetCompleteWorkMileage(serviceId);
            }

            // Insert to dataset
            ServiceRequestsManagerToolBasicInformation serviceRequestsManagerToolBasicInformation = new ServiceRequestsManagerToolBasicInformation(serviceRequestsManagerToolTDS);
            serviceRequestsManagerToolBasicInformation.Update(serviceId, serviceState, assignDateTime, assignDeadlineDate, assignTeamMember, assignTeamMemberId, assignThirdPartyVendor, acceptDateTime, startWorkDateTime, unitOutOfServiceDate, unitOutOfServiceTime, completeWorkDateTime, unitBackInServiceDate, unitBackInServiceTime, completeWorkDetailDescription, completeWorkDetailPreventable, completeWorkDetailTMLabourHours, completeWorkDetailTMCost, completeWorkInvoiceNumber, completeWorkInvoiceAmount, startWorkMileage, completeWorkMileage, checklistState, false, companyId, libraryCategoriesId);

            // Store session
            Session["serviceRequestsManagerToolTDS"] = serviceRequestsManagerToolTDS;
        }