protected Boolean ApplyChanges()
        {
            Boolean success = false;

            Boolean isModified = false;

            Boolean isValid = false;

            Dictionary <String, String> validationResponse;


            if (MercuryApplication == null)
            {
                return(false);
            }



            Mercury.Client.Core.Condition.Condition conditionUnmodified = (Mercury.Client.Core.Condition.Condition)Session[SessionCachePrefix + "ConditionUnmodified"];


            condition.Name = ConditionName.Text;

            condition.Description = ConditionDescription.Text;


            if (ConditionClassSelection.SelectedItem != null)
            {
                condition.ConditionClassId = Convert.ToInt64(ConditionClassSelection.SelectedItem.Value);

                condition.ConditionClassName = Convert.ToString(ConditionClassSelection.SelectedItem.Text);
            }

            else
            {
                condition.ConditionClassId = 0;

                condition.ConditionClassName = ConditionClassSelection.Text;
            }


            condition.Enabled = ConditionEnabled.Checked;

            condition.Visible = ConditionVisible.Checked;


            if (conditionUnmodified.Id == 0)
            {
                isModified = true;
            }

            if (!isModified)
            {
                isModified = !condition.IsEqual(conditionUnmodified);
            }


            validationResponse = condition.Validate();

            isValid = (validationResponse.Count == 0);


            if ((isModified) && (isValid))
            {
                if (!MercuryApplication.HasEnvironmentPermission(Mercury.Server.EnvironmentPermissions.ConditionManage))
                {
                    SaveResponseLabel.Text = "Permission Denied.";

                    return(false);
                }

                success = MercuryApplication.ConditionSave(condition);

                if (success)
                {
                    condition = MercuryApplication.ConditionGet(condition.Id, false);

                    Session[SessionCachePrefix + "Condition"] = condition;

                    Session[SessionCachePrefix + "ConditionUnmodified"] = condition.Copy();

                    SaveResponseLabel.Text = "Save Successful.";

                    InitializeAll();
                }

                else
                {
                    SaveResponseLabel.Text = "Unable to Save Condition.";

                    if (MercuryApplication.LastException != null)
                    {
                        SaveResponseLabel.Text = SaveResponseLabel.Text + " [" + MercuryApplication.LastException.Message + "]";
                    }

                    success = false;
                }
            }

            else if (!isModified)
            {
                SaveResponseLabel.Text = "No Changes Detected."; success = true;
            }

            else if (!isValid)
            {
                foreach (String validationKey in validationResponse.Keys)
                {
                    SaveResponseLabel.Text = "Invalid [" + validationKey + "]: " + validationResponse[validationKey];

                    break;
                }

                success = false;
            }

            return(success);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Int64 forConditionId = 0;


            if (MercuryApplication == null)
            {
                return;
            }


            if ((!MercuryApplication.HasEnvironmentPermission(Mercury.Server.EnvironmentPermissions.ConditionReview)) &&
                (!MercuryApplication.HasEnvironmentPermission(Mercury.Server.EnvironmentPermissions.ConditionManage)))
            {
                Response.Redirect("/PermissionDenied.aspx", true); return;
            }

            if (!Page.IsPostBack)
            {
                #region Initial Page Load

                if (Request.QueryString["ConditionId"] != null)
                {
                    forConditionId = Int64.Parse(Request.QueryString["ConditionId"]);
                }

                if (forConditionId != 0)
                {
                    condition = MercuryApplication.ConditionGet(forConditionId, false);

                    if (condition == null)
                    {
                        condition = new Mercury.Client.Core.Condition.Condition(MercuryApplication);
                    }

                    Page.Title = "Condition - " + condition.Name;
                }

                else
                {
                    condition = new Mercury.Client.Core.Condition.Condition(MercuryApplication);
                }

                InitializeAll();

                Session[SessionCachePrefix + "Condition"] = condition;

                Session[SessionCachePrefix + "ConditionUnmodified"] = condition.Copy();

                #endregion
            } // Initial Page Load

            else   // Postback

            {
                condition = (Mercury.Client.Core.Condition.Condition)Session[SessionCachePrefix + "Condition"];
            }

            ApplySecurity();

            return;
        }