protected void cvStartMileage_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            string unitType = unitsGateway.GetType((int)unitId);

            if (unitType == "Vehicle")
            {
                if (tbxPnlStartWorkStartMileage.Text == "")
                {
                    args.IsValid = false;
                }
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Set active toolbar
            mForm6 master = (mForm6)this.Master;
            master.ActiveToolbar = "FleetManagement";

            // For error message
            hdfErrorFieldList.Value = "";

            // Validate left menu if the user has admin permission
            if (Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_ADMIN"]))
            {
                tkrpbLeftMenuAllServiceRequests.Visible = true;
                tkrpbLeftMenuMyServiceRequests.Visible = false;
                tkrpbLeftMenuTools.Visible = true;
            }
            else
            {
                tkrpbLeftMenuAllServiceRequests.Visible = false;
                tkrpbLeftMenuMyServiceRequests.Visible = true;
                tkrpbLeftMenuTools.Visible = false;
            }

            // Tabs validation
            // ... Validate vehicle info
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            string unitType = unitsGateway.GetType((int)unitId);
            int companyLevel = unitsGateway.GetCompanyLevelId((int)unitId);

            if (unitType != "Vehicle")
            {
                lblGeneralMileage.Visible = false;
                tbxGeneralMileage.Visible = false;
                lblGeneralMileageUnitOfMeasurement.Visible = false;
                lblStartWorkDataStartMileage.Visible = false;
                tbxStartWorkDataStartMileage.Visible = false;
                lblStartWorkDataMileageUnitOfMeasurement.Visible = false;
                lblStartWorkDataMileageUnitOfMeasurementReadOnly.Visible = false;
                lblCompleteWorkDataCompleteMileage.Visible = false;
                tbxCompleteWorkDataCompleteMileage.Visible = false;
                lblCompleteWorkDataMileageUnitOfMeasurement.Visible = false;
                lblCompleteWorkDataMileageUnitOfMeasurementReadOnly.Visible = false;
            }
            else
            {
                // .. For Vehicles
                lblGeneralMileage.Visible = true;
                tbxGeneralMileage.Visible = true;
                lblStartWorkDataStartMileage.Visible = true;
                tbxStartWorkDataStartMileage.Visible = true;
                lblCompleteWorkDataCompleteMileage.Visible = true;
                tbxCompleteWorkDataCompleteMileage.Visible = true;
                lblGeneralMileageUnitOfMeasurement.Visible = true;
                lblStartWorkDataMileageUnitOfMeasurement.Visible = true;
                lblStartWorkDataMileageUnitOfMeasurementReadOnly.Visible = true;
                lblCompleteWorkDataMileageUnitOfMeasurementReadOnly.Visible = true;
            }

            // Assign Tab
            // ... Assigned To
            hdfServiceState.Value = serviceInformationBasicInformationGatewayForId.GetServiceState(serviceId);
            string state = hdfServiceState.Value;

            if (state == "Unassigned")
            {
                // ... Princial Panels
                pnlAssignmentData.Visible = false;
                pnlAssignmentDataReadOnly.Visible = true;

                // ... ...Subpanels
                pnlAssignedTo.Visible = false;
                pnlAssignedToReadOnly.Visible = false;
                pnlAssignmentAccept.Visible = false;
                pnlAssignmentReject.Visible = false;

                pnlAssignmentAcceptReadOnly.Visible = true;
                pnlAssignmentRejectReadOnly.Visible = true;
            }

            if ((state == "Assigned") || (state == "Rejected"))
            {
                // ... ... Principal Panels
                pnlAssignmentData.Visible = true;
                pnlAssignmentDataReadOnly.Visible = false;

                // ... ...Subpanels
                pnlAssignedTo.Visible = true;
                pnlAssignedToReadOnly.Visible = false;
                pnlAssignmentAccept.Visible = false;
                pnlAssignmentReject.Visible = false;

                pnlAssignmentAcceptReadOnly.Visible = false;
                pnlAssignmentRejectReadOnly.Visible = false;
            }
            else
            {
                if ((state == "Accepted") || (state == "In Progress") || (state == "Completed"))
                {
                    // ... ... Principal Panels
                    pnlAssignmentData.Visible = true;
                    pnlAssignmentDataReadOnly.Visible = false;

                    // ... ...Subpanels
                    pnlAssignedTo.Visible = false;
                    pnlAssignedToReadOnly.Visible = true;
                    pnlAssignmentAccept.Visible = false;
                    pnlAssignmentReject.Visible = false;
                    pnlAssignmentAcceptReadOnly.Visible = false;
                    pnlAssignmentRejectReadOnly.Visible = false;
                }
            }

            // ... Assignation result
            if ((state == "Accepted") || (state == "In Progress") || (state == "Completed"))
            {
                // ... ... Principal Panels
                pnlAssignmentData.Visible = true;
                pnlAssignmentDataReadOnly.Visible = false;

                // ... ...Subpanels
                pnlAssignedTo.Visible = false;
                pnlAssignedToReadOnly.Visible = true;
                pnlAssignmentAccept.Visible = true;
                pnlAssignmentReject.Visible = false;
                pnlAssignmentAcceptReadOnly.Visible = false;
                pnlAssignmentRejectReadOnly.Visible = false;
            }

            // StartWork tab
            if ((state == "Unassigned") || (state == "Assigned") || (state == "Rejected") || (state == "Accepted"))
            {
                pnlStartWorkDataReadOnly.Visible = true;
                pnlStartWorkData.Visible = false;
            }
            else
            {
                pnlStartWorkDataReadOnly.Visible = false;
                pnlStartWorkData.Visible = true;
            }

            // Complete Work tab
            if ((state == "Unassigned") || (state == "Assigned") || (state == "Rejected") || (state == "Accepted") || (state == "In Progress"))
            {
                pnlCompleteWorkDataReadOnly.Visible = true;
                pnlCompleteWorkData.Visible = false;
            }
            else
            {
                pnlCompleteWorkDataReadOnly.Visible = false;
                pnlCompleteWorkData.Visible = true;
            }

            // ... Validate for assignated person
            if (serviceInformationBasicInformationGatewayForId.GetToTeamMember(serviceId))
            {
                pnlTeamMemberAssigned.Visible = true;
                pnlThirdPartyVendorAssigned.Visible = false;
                pnlTeamMemberAssignedReadOnly.Visible = true;
                pnlThirdPartyVendorAssignedReadOnly.Visible = false;
            }
            else
            {
                pnlTeamMemberAssigned.Visible = false;
                pnlThirdPartyVendorAssigned.Visible = true;
                pnlTeamMemberAssignedReadOnly.Visible = false;
                pnlThirdPartyVendorAssignedReadOnly.Visible = true;

            }
        }
        protected void cvGeneralDataMileage_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            if (ckbxMtoDto.Checked)
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int serviceId = Int32.Parse(hdfServiceId.Value);

                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
                int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

                UnitsGateway unitsGateway = new UnitsGateway();
                unitsGateway.LoadByUnitId((int)unitId, companyId);
                string unitType = unitsGateway.GetType((int)unitId);

                if (unitType == "Vehicle")
                {
                    if (tbxGeneralMileage.Text == "")
                    {
                        args.IsValid = false;
                        hdfErrorFieldList.Value = hdfErrorFieldList.Value + ", Mileage (General)";
                    }
                }
            }
        }
        /// <summary>
        /// Save
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void Save(int companyId)
        {
            ServiceInformationTDS servicesInformationChanges = (ServiceInformationTDS)Data.GetChanges();

            if (servicesInformationChanges.BasicInformation.Rows.Count > 0)
            {
                ServiceInformationBasicInformationGateway serviceInformationBasicInformationGateway = new ServiceInformationBasicInformationGateway(servicesInformationChanges);

                // Update services
                foreach (ServiceInformationTDS.BasicInformationRow basicInformationRow in (ServiceInformationTDS.BasicInformationDataTable)servicesInformationChanges.BasicInformation)
                {
                    // Unchanged values
                    int serviceId = basicInformationRow.ServiceID;
                    string number = serviceInformationBasicInformationGateway.GetServiceNumber(serviceId);
                    DateTime dateTime_ = serviceInformationBasicInformationGateway.GetDateTime_(serviceId);
                    int? unitId = null; if (serviceInformationBasicInformationGateway.GetUnitID(serviceId).HasValue) unitId = serviceInformationBasicInformationGateway.GetUnitID(serviceId);
                    string type = serviceInformationBasicInformationGateway.GetType(serviceId);
                    int ownerId = serviceInformationBasicInformationGateway.GetOwnerID(serviceId);

                    // Original values
                    bool originalMtoDto = serviceInformationBasicInformationGateway.GetMtoDtoOriginal(serviceId);
                    string originalDescription = serviceInformationBasicInformationGateway.GetServiceDescriptionOriginal(serviceId);
                    string originalState = serviceInformationBasicInformationGateway.GetServiceStateOriginal(serviceId);
                    DateTime? originalAssignDateTime = null; if (serviceInformationBasicInformationGateway.GetAssignmentDateTimeOriginal(serviceId).HasValue) originalAssignDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAssignmentDateTimeOriginal(serviceId);
                    DateTime? originalAssignDeadlineDate = null; if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDateOriginal(serviceId).HasValue) originalAssignDeadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDateOriginal(serviceId);
                    bool originalAssignTeamMember = serviceInformationBasicInformationGateway.GetToTeamMemberOriginal(serviceId);
                    int? originalAssignTeamMemberID = null; if (serviceInformationBasicInformationGateway.GetAssignTeamMemberIdOriginal(serviceId).HasValue) originalAssignTeamMemberID = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberIdOriginal(serviceId);
                    string originalAssignThirdPartyVendor = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendorOriginal(serviceId);
                    DateTime? originalAcceptDateTime = null; if (serviceInformationBasicInformationGateway.GetAcceptedDateTimeOriginal(serviceId).HasValue) originalAcceptDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAcceptedDateTimeOriginal(serviceId);
                    DateTime? originalRejectDateTime = null; if (serviceInformationBasicInformationGateway.GetRejectedDateTimeOriginal(serviceId).HasValue) originalRejectDateTime = (DateTime)serviceInformationBasicInformationGateway.GetRejectedDateTimeOriginal(serviceId);
                    string originalRejectReason = serviceInformationBasicInformationGateway.GetRejectedReasonOriginal(serviceId);
                    DateTime? originalStartWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetStartWorkDateTimeOriginal(serviceId).HasValue) originalStartWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetStartWorkDateTimeOriginal(serviceId);
                    DateTime? originalStartWorkOutOfServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDateOriginal(serviceId).HasValue) originalStartWorkOutOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDateOriginal(serviceId);
                    string originalStartWorkOutOfServiceTime = serviceInformationBasicInformationGateway.GetUnitOutOfServiceTimeOriginal(serviceId);
                    DateTime? originalCompleteWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDateTimeOriginal(serviceId).HasValue) originalCompleteWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetCompleteWorkDateTimeOriginal(serviceId);
                    DateTime? originalCompleteWorkBackToServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDateOriginal(serviceId).HasValue) originalCompleteWorkBackToServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDateOriginal(serviceId);
                    string originalCompleteWorkBackToServiceTime = serviceInformationBasicInformationGateway.GetUnitBackInServiceTimeOriginal(serviceId);
                    string originalCompleteWorkDetailDescription = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescriptionOriginal(serviceId);
                    bool originalCompleteWorkDetailPreventable = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventableOriginal(serviceId);
                    decimal? originalCompleteWorkDetailTMLabourHours = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHoursOriginal(serviceId).HasValue) originalCompleteWorkDetailTMLabourHours = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHoursOriginal(serviceId);
                    decimal? originalCompleteWorkDetailTMCost = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCostOriginal(serviceId).HasValue) originalCompleteWorkDetailTMCost = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCostOriginal(serviceId);
                    string originalCompleteWorkDetailTPVInvoiceNumber = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumberOriginal(serviceId);
                    decimal? originalCompleteWorkDetailTPVInvoiceAmout = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmoutOriginal(serviceId).HasValue) originalCompleteWorkDetailTPVInvoiceAmout = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmoutOriginal(serviceId);
                    string originalNotes = serviceInformationBasicInformationGateway.GetNotesOriginal(serviceId);
                    int? originalRuleId = null; if (serviceInformationBasicInformationGateway.GetRuleIdOriginal(serviceId).HasValue) originalRuleId = (int)serviceInformationBasicInformationGateway.GetRuleIdOriginal(serviceId);
                    string originalMileage = serviceInformationBasicInformationGateway.GetMileageOriginal(serviceId);
                    string originalStartWorkMileage = serviceInformationBasicInformationGateway.GetStartWorkMileageOriginal(serviceId);
                    string originalCompleteWorkMileage = serviceInformationBasicInformationGateway.GetCompleteWorkMileageOriginal(serviceId);
                    bool originalDeleted = serviceInformationBasicInformationGateway.GetDeletedOriginal(serviceId);
                    int? originalLibraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesIdOriginal(serviceId).HasValue) originalLibraryCategoriesId = serviceInformationBasicInformationGateway.GetLibraryCategoriesIdOriginal(serviceId).Value;

                    // New variables
                    bool newMtoDto = serviceInformationBasicInformationGateway.GetMtoDto(serviceId);
                    string newDescription = serviceInformationBasicInformationGateway.GetServiceDescription(serviceId);
                    string newState = serviceInformationBasicInformationGateway.GetServiceState(serviceId);
                    DateTime? newAssignDateTime = null; if(serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId).HasValue) newAssignDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAssignmentDateTime(serviceId);
                    DateTime? newAssignDeadlineDate = null; if (serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId).HasValue) newAssignDeadlineDate = (DateTime)serviceInformationBasicInformationGateway.GetAssignedDeadlineDate(serviceId);
                    bool newAssignTeamMember = serviceInformationBasicInformationGateway.GetToTeamMember(serviceId);
                    int? newAssignTeamMemberId = null; if (serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId).HasValue) newAssignTeamMemberId = (int)serviceInformationBasicInformationGateway.GetAssignTeamMemberId(serviceId);
                    string newAssignThirdPartyVendor = serviceInformationBasicInformationGateway.GetAssignedThirdPartyVendor(serviceId);
                    DateTime? newAcceptDateTime = null; if(serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId).HasValue) newAcceptDateTime = (DateTime)serviceInformationBasicInformationGateway.GetAcceptedDateTime(serviceId);
                    DateTime? newRejectDateTime = null; if(serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId).HasValue) newRejectDateTime = (DateTime)serviceInformationBasicInformationGateway.GetRejectedDateTime(serviceId);
                    string newRejectReason = serviceInformationBasicInformationGateway.GetRejectedReason(serviceId);
                    DateTime? newStartWorkDateTime = null;  if( serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId).HasValue) newStartWorkDateTime = (DateTime) serviceInformationBasicInformationGateway.GetStartWorkDateTime(serviceId);
                    DateTime? newStartWorkOutOfServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId).HasValue) newStartWorkOutOfServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitOutOfServiceDate(serviceId);
                    string newStartWorkOutOfServiceTime = serviceInformationBasicInformationGateway.GetUnitOutOfServiceTime(serviceId);
                    DateTime? newCompleteWorkDateTime = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId).HasValue) newCompleteWorkDateTime = (DateTime)serviceInformationBasicInformationGateway.GetCompleteWorkDateTime(serviceId);
                    DateTime? newCompleteWorkBackToServiceDate = null; if (serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId).HasValue) newCompleteWorkBackToServiceDate = (DateTime)serviceInformationBasicInformationGateway.GetUnitBackInServiceDate(serviceId);
                    string newCompleteWorkBackToServiceTime = serviceInformationBasicInformationGateway.GetUnitBackInServiceTime(serviceId);
                    string newCompleteWorkDetailDescription = serviceInformationBasicInformationGateway.GetCompleteWorkDetailDescription(serviceId);
                    bool newCompleteWorkDetailPreventable = serviceInformationBasicInformationGateway.GetCompleteWorkDetailPreventable(serviceId);
                    decimal? newCompleteWorkDetailTMLabourHours = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId).HasValue) newCompleteWorkDetailTMLabourHours = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMLabourHours(serviceId);
                    decimal? newCompleteWorkDetailTMCost = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCost(serviceId).HasValue) newCompleteWorkDetailTMCost = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTMCost(serviceId);
                    string newCompleteWorkDetailTPVInvoiceNumber = serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceNumber(serviceId);
                    decimal? newCompleteWorkDetailTPVInvoiceAmout = null; if (serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId).HasValue) newCompleteWorkDetailTPVInvoiceAmout = (decimal)serviceInformationBasicInformationGateway.GetCompleteWorkDetailTPVInvoiceAmout(serviceId);
                    string newNotes = serviceInformationBasicInformationGateway.GetNotes(serviceId);
                    int? newRuleId = null; if (serviceInformationBasicInformationGateway.GetRuleId(serviceId).HasValue) newRuleId = (int)serviceInformationBasicInformationGateway.GetRuleId(serviceId);
                    string newMileage = serviceInformationBasicInformationGateway.GetMileage(serviceId);
                    string newStartWorkMileage = serviceInformationBasicInformationGateway.GetStartWorkMileage(serviceId);
                    string newCompleteWorkMileage = serviceInformationBasicInformationGateway.GetCompleteWorkMileage(serviceId);
                    int? newLibraryCategoriesId = null; if (serviceInformationBasicInformationGateway.GetLibraryCategoriesId(serviceId).HasValue) newLibraryCategoriesId = serviceInformationBasicInformationGateway.GetLibraryCategoriesId(serviceId).Value;
                    bool newDeleted = serviceInformationBasicInformationGateway.GetDeleted(serviceId);

                    // ... Update
                    UpdateService(serviceId, number, dateTime_, originalMtoDto, originalDescription, unitId, type, originalState, ownerId, originalAssignDateTime, originalAssignDeadlineDate, originalAssignTeamMember, originalAssignTeamMemberID, originalAssignThirdPartyVendor, originalAcceptDateTime, originalRejectDateTime, originalRejectReason, originalStartWorkDateTime, originalStartWorkOutOfServiceDate, originalStartWorkOutOfServiceTime, originalCompleteWorkDateTime, originalCompleteWorkBackToServiceDate, originalCompleteWorkBackToServiceTime, originalCompleteWorkDetailDescription, originalCompleteWorkDetailPreventable, originalCompleteWorkDetailTMLabourHours, originalCompleteWorkDetailTMCost, originalCompleteWorkDetailTPVInvoiceNumber, originalCompleteWorkDetailTPVInvoiceAmout, originalDeleted, companyId, originalNotes, originalRuleId, originalMileage, originalStartWorkMileage, originalCompleteWorkMileage, originalLibraryCategoriesId, number, dateTime_, newMtoDto, newDescription, unitId, type, newState, ownerId, newAssignDateTime, newAssignDeadlineDate, newAssignTeamMember, newAssignTeamMemberId, newAssignThirdPartyVendor, newAcceptDateTime, newRejectDateTime, newRejectReason, newStartWorkDateTime, newStartWorkOutOfServiceDate, newStartWorkOutOfServiceTime, newCompleteWorkDateTime, newCompleteWorkBackToServiceDate, newCompleteWorkBackToServiceTime, newCompleteWorkDetailDescription, newCompleteWorkDetailPreventable, newCompleteWorkDetailTMLabourHours, newCompleteWorkDetailTMCost, newCompleteWorkDetailTPVInvoiceNumber, newCompleteWorkDetailTPVInvoiceAmout, newDeleted, companyId, newNotes, newRuleId, newMileage, newStartWorkMileage, newCompleteWorkMileage, newLibraryCategoriesId);

                    // Update checklist
                    if (type == "Checklist")
                    {
                        // ... Original values
                        DateTime? originalAssociatedChecklistLastService = serviceInformationBasicInformationGateway.GetAssociatedChecklistLastServiceOriginal(serviceId);
                        DateTime? originalAssociatedChecklistNextDue = serviceInformationBasicInformationGateway.GetAssociatedChecklistNextDueOriginal(serviceId);
                        bool originalAssociatedChecklistDone = serviceInformationBasicInformationGateway.GetAssociatedChecklistDoneOriginal(serviceId);
                        bool originalAssociatedChecklistDeleted = serviceInformationBasicInformationGateway.GetAssociatedChecklistDeletedOriginal(serviceId);
                        int originalAssociatedChecklistCompanyId = serviceInformationBasicInformationGateway.GetAssociatedChecklistCompanyIdOriginal(serviceId);
                        string originalAssociatedChecklistRuleState = serviceInformationBasicInformationGateway.GetAssociatedChecklistRuleStateOriginal(serviceId);

                        // ... New values
                        DateTime? newAssociatedChecklistLastService = serviceInformationBasicInformationGateway.GetAssociatedChecklistLastService(serviceId);
                        DateTime? newAssociatedChecklistNextDue = serviceInformationBasicInformationGateway.GetAssociatedChecklistNextDue(serviceId);
                        bool newAssociatedChecklistDone = serviceInformationBasicInformationGateway.GetAssociatedChecklistDone(serviceId);
                        bool newAssociatedChecklistDeleted = serviceInformationBasicInformationGateway.GetAssociatedChecklistDeleted(serviceId);
                        int newAssociatedChecklistCompanyId = serviceInformationBasicInformationGateway.GetAssociatedChecklistCompanyId(serviceId);
                        string newAssociatedChecklistRuleState = serviceInformationBasicInformationGateway.GetAssociatedChecklistRuleState(serviceId);

                        // ... Update
                        Checklist checklist = new Checklist();
                        checklist.UpdateDirect((int)unitId, (int)originalRuleId, originalAssociatedChecklistLastService, originalAssociatedChecklistNextDue, originalAssociatedChecklistDone, originalAssociatedChecklistRuleState, originalAssociatedChecklistDeleted, originalAssociatedChecklistCompanyId, (int)unitId, (int)originalRuleId, newAssociatedChecklistLastService, newAssociatedChecklistNextDue, newAssociatedChecklistDone, newAssociatedChecklistRuleState, originalAssociatedChecklistDeleted, originalAssociatedChecklistCompanyId);
                    }
                }
            }
        }
        private void TagPage()
        {
            hdfCompanyId.Value = Session["companyID"].ToString();
            hdfServiceId.Value = Request.QueryString["service_id"];
            hdfActiveTab.Value = Request.QueryString["active_tab"].ToString();

            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway();
            serviceInformationBasicInformationGatewayForId.LoadByServiceId(serviceId, companyId);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            hdfUnitType.Value = unitsGateway.GetType((int)unitId);
            hdfCompanyLevel.Value = unitsGateway.GetCompanyLevelId((int)unitId).ToString();

            int companyLevel = Int32.Parse(hdfCompanyLevel.Value);
            CompanyLevelGateway companyLevelGateway = new CompanyLevelGateway();
            companyLevelGateway.LoadByCompanyLevelId(companyLevel, companyId);
            hdfMileageUnitOfMeasurement.Value = companyLevelGateway.GetUnitsUnitOfMeasurement(companyLevel);
            lblPnlStartWorkStartMileageUnitOfMeasurement.Text = hdfMileageUnitOfMeasurement.Value;
            lblPnlCompleteWorkCompleteMileageUnitOfMeasurement.Text = hdfMileageUnitOfMeasurement.Value;
        }
        private void UpdateDatabase()
        {
            // Get ids
            int serviceId = Int32.Parse(hdfServiceId.Value);
            string serviceType = hdfFmType.Value;
            int companyId = Int32.Parse(hdfCompanyId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            string unitType = unitsGateway.GetType((int)unitId);

            // Delete
            DB.Open();
            DB.BeginTransaction();
            try
            {
                ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
                serviceInformationBasicInformation.DeleteDirect(serviceId, unitType, (int)unitId, companyId);

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Set active toolbar
            mForm6 master = (mForm6)this.Master;
            master.ActiveToolbar = "FleetManagement";

            // Validate left menu if the user has admin permission
            if (Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_ADMIN"]))
            {
                tkrpbLeftMenuAllServiceRequests.Visible = true;
                tkrpbLeftMenuMyServiceRequests.Visible = false;
                tkrpbLeftMenuTools.Visible = true;
            }
            else
            {
                tkrpbLeftMenuAllServiceRequests.Visible = false;
                tkrpbLeftMenuMyServiceRequests.Visible = true;
                tkrpbLeftMenuTools.Visible = false;
            }

            // Tabs validation
            // ... Validate vehicle info
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int serviceId = Int32.Parse(hdfServiceId.Value);

            ServiceInformationBasicInformationGateway serviceInformationBasicInformationGatewayForId = new ServiceInformationBasicInformationGateway(serviceInformationTDS);
            int? unitId = serviceInformationBasicInformationGatewayForId.GetUnitID(serviceId);

            UnitsGateway unitsGateway = new UnitsGateway();
            unitsGateway.LoadByUnitId((int)unitId, companyId);
            string unitType = unitsGateway.GetType((int)unitId);
            int companyLevel = unitsGateway.GetCompanyLevelId((int)unitId);

            if (unitType != "Vehicle")
            {
                lblGeneralMileage.Visible = false;
                tbxGeneralMileage.Visible = false;
                lblGeneralMileageUnitOfMeasurement.Visible = false;
                lblStartWorkDataStartMileage.Visible = false;
                tbxStartWorkDataStartMileage.Visible = false;
                lblStartWorkDataMileageUnitOfMeasurement.Visible = false;
                lblCompleteWorkDataCompleteMileage.Visible = false;
                tbxCompleteWorkDataCompleteMileage.Visible = false;
                lblCompleteWorkDataMileageUnitOfMeasurement.Visible = false;
            }
            else
            {
                lblGeneralMileage.Visible = true;
                tbxGeneralMileage.Visible = true;
                lblStartWorkDataStartMileage.Visible = true;
                tbxStartWorkDataStartMileage.Visible = true;
                lblCompleteWorkDataCompleteMileage.Visible = true;
                tbxCompleteWorkDataCompleteMileage.Visible = true;
                lblGeneralMileageUnitOfMeasurement.Visible = true;
                lblStartWorkDataMileageUnitOfMeasurement.Visible = true;
                lblCompleteWorkDataMileageUnitOfMeasurement.Visible = true;
            }

            // ... Validate Assignation result
            hdfServiceState.Value = serviceInformationBasicInformationGatewayForId.GetServiceState(serviceId);
            string state = hdfServiceState.Value;

            if ((state == "Accepted") || (state == "In Progress") || (state == "Completed"))
            {
                pnlAssignmentAccept.Visible = true;
                pnlAssignmentReject.Visible = false;
            }
            else
            {
                if (state == "Rejected")
                {
                    pnlAssignmentAccept.Visible = false;
                    pnlAssignmentReject.Visible = true;
                }
                else
                {
                    pnlAssignmentAccept.Visible = false;
                    pnlAssignmentReject.Visible = false;
                }
            }

            // ... Validate for assignated person
            if (serviceInformationBasicInformationGatewayForId.GetToTeamMember(serviceId))
            {
                pnlTeamMemberAssigned.Visible = true;
                pnlThirdPartyVendorAssigned.Visible = false;
            }
            else
            {
                pnlTeamMemberAssigned.Visible = false;
                pnlThirdPartyVendorAssigned.Visible = true;
            }

            // Validations for Top Menu
            // ... Get user and service data
            // ... ... Get logged employeeId
            int loginId = Int32.Parse(hdfLoginId.Value);
            bool serviceAdmin = Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_ADMIN"]);
            EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
            int employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);

            // ... ... Get ownerId,  assignTeamMemberId
            int ownerId = serviceInformationBasicInformationGatewayForId.GetOwnerID(serviceId);
            int? assignTeamMemberId = null; if (serviceInformationBasicInformationGatewayForId.GetAssignTeamMemberId(serviceId).HasValue) assignTeamMemberId = (int)serviceInformationBasicInformationGatewayForId.GetAssignTeamMemberId(serviceId);

            // ... Delete option
            if (((employeeId == ownerId) || (serviceAdmin)) && (IsDeletedSR(serviceId)))
            {
                tkrmTop.Items[1].Visible = true;
            }
            else
            {
                tkrmTop.Items[1].Visible = false;
            }

            // ... Assign option
            if ((serviceAdmin) && ((state == "Unassigned") || (state == "Rejected")))
            {
                tkrmTop.Items[2].Visible = true;
            }
            else
            {
                tkrmTop.Items[2].Visible = false;
            }

            // ... User options
            tkrmTop.Items[3].Visible = false; // Accept
            tkrmTop.Items[4].Visible = false; // Reject
            tkrmTop.Items[5].Visible = false; // Start Work
            tkrmTop.Items[6].Visible = false; // Complete Work

            // ... Accept/Reject options
            if (((employeeId == assignTeamMemberId)|| (serviceAdmin)) && (state == "Assigned"))
            {
                tkrmTop.Items[3].Visible = true;
                tkrmTop.Items[4].Visible = true;
                tkrmTop.Items[5].Visible = false;
                tkrmTop.Items[6].Visible = false;
            }

            // ... Start work option
            if (((employeeId == assignTeamMemberId)|| (serviceAdmin)) && (state == "Accepted"))
            {
                tkrmTop.Items[3].Visible = false;
                tkrmTop.Items[4].Visible = false;
                tkrmTop.Items[5].Visible = true;
                tkrmTop.Items[6].Visible = false;
            }

            // ... Start work option
            if (((employeeId == assignTeamMemberId) || (serviceAdmin)) && (state == "In Progress"))
            {
                tkrmTop.Items[3].Visible = false;
                tkrmTop.Items[4].Visible = false;
                tkrmTop.Items[5].Visible = false;
                tkrmTop.Items[6].Visible = true;
            }

            // ... Last Search or Dashboard
            tkrmTopNavigation.Items[2].Visible = true; // Last search
            tkrmTopNavigation.Items[3].Visible = false; // Dashboard

            if (hdfDashboard.Value == "True")
            {
                tkrmTopNavigation.Items[2].Visible = false; // Last search
                tkrmTopNavigation.Items[3].Visible = true; // Dashboard
            }
        }