/// <summary>
        /// Sets the value for whether or not the operator complies with the performance standard
        /// </summary>
        /// <param name="PermitKey"></param>
        /// <param name="Section"></param>
        /// <param name="Comply"></param>
        /// <returns></returns>
        public ActionResult Comply(int PermitKey, string Section, bool Comply)
        {
            try
            {
                ENUMBLL.PerformanceStandardType type = ENUMBLL.PerformanceStandardType.FiveHundreds;
                if (Section.StartsWith("17.24.5")) { type = ENUMBLL.PerformanceStandardType.FiveHundreds; }
                if (Section.Contains("17.24.6")) { type = ENUMBLL.PerformanceStandardType.SixHundreds; }
                if (Section.Contains("17.24.7")) { type = ENUMBLL.PerformanceStandardType.SevenHundreds; }
                if (Section.Contains("17.24.8")) { type = ENUMBLL.PerformanceStandardType.EightHundreds; }
                if (Section.Contains("17.24.9")) { type = ENUMBLL.PerformanceStandardType.NineHundreds; }

                if (Comply)
                    PerformanceStandardBLL.SetPerformanceStandard(PermitKey, Section, type);
                else
                    PerformanceStandardBLL.RemovePerformanceStandard(PermitKey, Section);

                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Your update was saved");
                result.Data.Add("Section", Section);
                result.Data.Add("Comply", Comply);
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
 public ActionResult DeleteUnapprovedLetter(int CorrespondenceId)
 {
     try
     {
         CorrespondenceBLL.DeleteUnapprovedCorrespondence(CorrespondenceId, DirectoryMapping.CorrespondenceFolder);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The letter was deleted");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
 public ActionResult UpdateAnnouncement(string Announcement)
 {
     try
     {
         TOCBLL.SetAnnouncement(Announcement);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The announcement has been updated");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
        public ActionResult Delete(int Id)
        {
            try
            {
                MapBLL.Delete(Id, DirectoryMapping.AttachmentContentFolder);

                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The map was deleted");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
 public ActionResult EditSubSection(RuleSubSectionDO RuleSubSection)
 {
     try
     {
         RuleBLL.Save(RuleSubSection);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The SubSection was updated");
         result.Data.Add("SubSection", RuleSubSection);
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
 public ActionResult DeletePermitRegistration(int Id)
 {
     try
     {
         bool val = PermitRegistrationBLL.Delete(Id);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The user permissions were deleted");
         result.Data.Add("PermitRegistrationID", Id);
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
        public ActionResult ApproveLetter(int PermitKey, int CorrespondenceId)
        {
            try
            {
                CorrespondenceBLL.ApproveCorrespondence(CorrespondenceId, myCoalUser.UserToken, DirectoryMapping.AttachmentContentFolder);

                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The letter was approved");
                result.Data.Add("CorrespondenceId", CorrespondenceId);
                result.Data.Add("LetterUrl", Url.Action("PermitCorrespondence", "ePermitCorrespondence", new { PermitKey = PermitKey, CorrespondenceId = CorrespondenceId }));
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        /// <summary>
        /// Adds or removes a layer on a map
        /// </summary>
        /// <param name="MapAttachmentId"></param>
        /// <param name="LayerId"></param>
        /// <param name="PermitKey"></param>
        /// <param name="Checked"></param>
        /// <returns></returns>
        public ActionResult AssociateMapLayers(int MapAttachmentId, string LayerId, int PermitKey, bool Checked)
        {
            try
            {
                if (Checked)
                    MapBLL.SetMapLayer(MapAttachmentId, LayerId, PermitKey);
                else
                    MapBLL.RemoveMapLayer(MapAttachmentId, LayerId);

                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Map layer added");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult AddMidPermitReview(MidPermitReviewVM model)
        {
            if(!ModelState.IsValid)
                return PartialView(model);

            try
            {
                myCoalUser user = myCoalUser.GetInstance();
                PermitDO newApplication = PermitBLL.CreateApplication(model.SelectedPermitKey, ENUMBLL.ApplicationTypes.MidPermitReview, model.ApplicationDescription, model.ApplicationNumber, user.RegistrationId, DirectoryMapping.AttachmentContentFolder);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "A new Mid Permit Review application has been created");
                result.Data.Add("PermitKey", newApplication.PermitKey);
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult EditPermitRegistration(PermitRegistrationBO model)
        {
            try
            {
                // permit coordinators don't see the coordinator role, therefore it must be preserved when the post.
                if (myCoalUser.GetInstance().IsInRole(CoalRoles.PermitCoordinator))
                {
                    PermitRegistrationBO original = PermitRegistrationBLL.GetPermitRegistration(model.PermitRegistration.PermitRegistrationID);
                    model.PermitRegistration.Coordinator = original.PermitRegistration.Coordinator;
                }

                PermitRegistrationBLL.Save(model.PermitRegistration);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The user permissions were saved");
                result.Data.Add("PermitRegistration", model.PermitRegistration);
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
 public ActionResult UpdatePermitAccessCode(string oldAccessCode, string newAccessCode)
 {
     try
     {
         PermitDO permit = PermitBLL.GetPermitByAccessCode(oldAccessCode);
         permit.AccessCode = newAccessCode;
         PermitBLL.Save(permit, myCoalUser.GetInstance().RegistrationId);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The permit access code was updated");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
 public ActionResult Permissions(SecurityVM model)
 {
     try
     {
         RegistrationBLL.Save(model.Registration.Registration.RegistrationID, model.Registration.Registration.RegistrationStatusID, model.Roles);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The user's permissions were set");
         return Json(result);
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("", ex.Message);
         return PartialView(model);
     }
 }
        public ActionResult MyInfo(RegistrationVM model)
        {
            model.Registration.StateID = model.States.Where(s => s.Selected).FirstOrDefault().Value;
            model.Registration.UserToken = myCoalUser.UserToken;

            if (!ModelState.IsValid)
                return PartialView("MyInfo", model);

            try
            {

                // the system owner can update anybody's registration, but anyone else can only update their own
                // therefore only lookup a user by id when the owner is performing the task to prevent a user from maliciously
                // chaning their RegistrationId before posting the form thereby updating someone else's data
                RegistrationDO data;
                if (myCoalUser.GetInstance().IsInRole("owner"))
                    data = RegistrationBLL.GetRegistration(model.Registration.RegistrationID);
                else
                    data = RegistrationBLL.GetRegistrationByUserToken(myCoalUser.UserToken);

                // restrict the update to the visible elements on the form
                data.Address1 = model.Registration.Address1;
                data.City = model.Registration.City;
                data.CompanyName = model.Registration.CompanyName;
                data.CountryCode = model.Registration.CountryCode;
                data.Email = model.Registration.Email;
                data.FirstName = model.Registration.FirstName;
                data.LastName = model.Registration.LastName;
                data.Phone = model.Registration.Phone;
                data.PhoneExtension = model.Registration.PhoneExtension;
                data.RegistrationDescription = model.Registration.RegistrationDescription;
                data.StateID = model.Registration.StateID;
                data.Title = model.Registration.Title;
                data.Zipcode = model.Registration.Zipcode;

                RegistrationBLL.Save(data);

                // refresh the cookie values
                myCoalUser.ResetProfileCookie();

                // return ok to let the javascript clien tknow the update went well
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "User profile information was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
 public ActionResult WildlifeSurveyDelete(int PermitKey, int Id)
 {
     try
     {
         WildlifeSurveyBLL.Delete(Id);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The record was deleted");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
        public ActionResult WildlifeSurveyEdit(int PermitKey, WildlifeSurveyVM model)
        {
            if (!ModelState.IsValid)
                return PartialView("WildlifeSurveyAddEdit", model);

            try
            {
                ThreatenedAndEndangeredSpeciesDO data = model.GetDataObject();
                WildlifeSurveyBLL.Save(data);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Contact and address was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult PremineLandUseAdd(PremineLandUseVM model)
        {
            if (!ModelState.IsValid)
                return PartialView("PremineLandUseAddEdit", model);

            try
            {
                PremineLandUseDO data = model.WriteDataToTables();
                PremineLandUseBLL.Save(data);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Premine Land Use was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult VegetationSurveyEdit(int PermitKey, VegetationSurveyVM model)
        {
            if (!ModelState.IsValid)
                return PartialView("VegetationSurveyAddEdit", model);

            try
            {
                VegetationCommunityTypeDO data = model.GetDataObject();
                VegetationSurveyBLL.Save(data);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Vegetation Community Type was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult ResolveDeficiency(int Id)
        {
            try
            {
                DeficiencyBLL.UpdateStatus(Id, ENUMBLL.DeficiencyStatusType.Resolved);

                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The deficiency has been resolved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult CulturalResourcesEdit(int PermitKey, CulturalResourcesAddEditVM model)
        {
            if (!ModelState.IsValid)
                return PartialView("CulturalResourcesAddEdit", model);

            try
            {
                CulturalResourceManagementDO data = model.WriteResourceManagementDataToTables();
                CulturalResourcesBLL.SaveCulturalResourceManagement(data);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Cultural Resource was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
 public ActionResult SoilHandlingPlanRedistributionThicknessDelete(int PermitKey, int Id)
 {
     try
     {
         SoilHandlingPlanBLL.DeleteRedistributionThickness(Id);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The record was deleted");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
 public ActionResult SoilHandlingPlanProposedSoilSalvageDepthDelete(int PermitKey, int Id)
 {
     try
     {
         SoilHandlingPlanBLL.DeleteProposedSoilSalvageDepth(Id);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The record was deleted");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
 public ActionResult ReclamationPostmineRevegetationDelete(int PermitKey, int Id)
 {
     try
     {
         PostmineLandUseBLL.Delete(Id);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The record was deleted");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
 public ActionResult UpdateInstructions(int Id, string Instructions)
 {
     try
     {
         int TocID = Id;
         bool ret = TOCBLL.SaveInstruction(TocID, Instructions);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The instructions have been updated");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
        public ActionResult RevertToDraftConfirmed(int PermitKey)
        {
            if (!Judge.AllowRevert)
                return RedirectToAction("InvalidRequest", "ePermitError", new { PermitKey = PermitKey });

            try
            {
                PermitBLL.Revert(PermitKey);
                AjaxResult result;
                result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The Permit has been reverted to Draft.");

                return Json(result);

            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult ReclamationPostmineRevegetationEdit(int PermitKey, ReclamationPostmineRevegetationVM model)
        {
            if (!ModelState.IsValid)
                return PartialView("ReclamationPostmineRevegetationAddEdit", model);

            try
            {
                PostmineLandUseDO data = model.WriteDataToTables();
                PostmineLandUseBLL.Save(data);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Postmine Land Use was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult Withdrawn(WithdrawPermitVM Model)
        {
            if (!ModelState.IsValid)
                return PartialView(Model);

            try
            {

                AjaxResult result;

                if (Model.Withdrawn.Value)
                {
                    PermitBLL.ApproveWithdraw(Model.PermitKey);
                    result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The Permit has been Withdrawn.");
                }
                else
                {
                    PermitBLL.DenyWithdraw(Model.PermitKey);
                    result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The Withdraw has been Denied.");
                }

                return Json(result);

            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
        public ActionResult SoilHandlingPlanProposedSoilSalvageDepthEdit(int PermitKey, ProposedSoilSalvageDepthVM model)
        {
            if (!ModelState.IsValid)
                return PartialView("SoilHandlingPlanProposedSoilSalvageDepthAddEdit", model);

            try
            {
                ProposedSoilSalvageDepthDO data = model.WriteSoilSalvageDepth();
                SoilHandlingPlanBLL.SaveProposedSoilSalvageDepth(data);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Soil Salvage Depth was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
 public ActionResult AddToFinalDeficiency(int Id)
 {
     try
     {
         DeficiencyBLL.CopyToFinal(Id);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The deficiency has been copied to final");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }
        public ActionResult SoilHandlingPlanRedistributionThicknessEdit(int PermitKey, RedistributionThicknessVM model)
        {
            if (!ModelState.IsValid)
                return PartialView("SoilHandlingPlanRedistributionThicknessAddEdit", model);

            try
            {
                RedistributionThicknessDO data = model.WriteRedistributionThickness();
                SoilHandlingPlanBLL.SaveRedistributionThickness(data);
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "Redistribution Thickness was saved");
                return Json(result);
            }
            catch (Exception ex)
            {
                AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
                return Json(result);
            }
        }
 public ActionResult CulturalResourcesDelete(int PermitKey, int Id)
 {
     try
     {
         CulturalResourcesBLL.DeleteCulturalResourceManagement(Id);
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.OK, "The record was deleted");
         return Json(result);
     }
     catch (Exception ex)
     {
         AjaxResult result = new AjaxResult(AjaxResult.AjaxStatus.ERROR, ex.Message);
         return Json(result);
     }
 }