protected void Page_Load(object sender, EventArgs e)
    {
        // Attach events
        btnAutoIndent.Click      += btnAutoIndent_Click;
        btnDelete.Click          += (btnDelete_Click);
        btnIndent.Click          += btnIndent_Click;
        btnUnindent.Click        += btnUnindent_Click;
        btnChangeOperator.Click  += new EventHandler(btnChangeOperator_Click);
        btnChangeParameter.Click += new EventHandler(btnChangeParameter_Click);
        btnMove.Click            += new EventHandler(btnMove_Click);
        btnCancel.Click          += new EventHandler(btnCancel_Click);
        btnSetParameter.Click    += new EventHandler(btnSetParameter_Click);
        btnViewCode.Click        += btnViewCode_Click;
        btnAddClause.Click       += new EventHandler(btnAddClause_Click);
        btnClearAll.Click        += btnClearAll_Click;
        txtFilter.TextChanged    += new EventHandler(btnFilter_Click);

        btnFilter.Text       = GetString("general.filter");
        btnSetParameter.Text = GetString("general.ok");
        btnCodeOK.Text       = GetString("general.ok");
        btnCancel.Text       = GetString("general.cancel");
        btnIndent.ScreenReaderDescription     = btnIndent.ToolTip = GetString("macros.macrorule.indent");
        btnUnindent.ScreenReaderDescription   = btnUnindent.ToolTip = GetString("macros.macrorule.unindent");
        btnAutoIndent.ScreenReaderDescription = btnAutoIndent.ToolTip = GetString("macros.macrorule.autoindent");
        btnDelete.ScreenReaderDescription     = btnDelete.ToolTip = GetString("general.delete");
        btnClearAll.ScreenReaderDescription   = btnClearAll.ToolTip = GetString("macro.macrorule.clearall");
        btnViewCode.ScreenReaderDescription   = btnViewCode.ToolTip = GetString("macros.macrorule.viewcode");

        btnIndent.OnClientClick     = "if (isNothingSelected()) { alert(" + ScriptHelper.GetString(GetString("macros.macrorule.nothingselected")) + "); return false; }";
        btnUnindent.OnClientClick   = "if (isNothingSelected()) { alert(" + ScriptHelper.GetString(GetString("macros.macrorule.nothingselected")) + "); return false; }";
        btnDelete.OnClientClick     = "if (isNothingSelected()) { alert(" + ScriptHelper.GetString(GetString("macros.macrorule.nothingselected")) + "); return false; } else { if (!confirm('" + GetString("macros.macrorule.deleteconfirmation") + "')) { return false; }}";
        btnAutoIndent.OnClientClick = "if (!confirm(" + ScriptHelper.GetString(GetString("macros.macrorule.deleteautoindent")) + ")) { return false; }";
        btnClearAll.OnClientClick   = "if (!confirm(" + ScriptHelper.GetString(GetString("macros.macrorule.clearall.confirmation")) + ")) { return false; }";

        lstRules.Attributes.Add("ondblclick", ControlsHelper.GetPostBackEventReference(btnAddClause, null));

        pnlViewCode.Visible = false;

        // Basic form
        formElem.SubmitButton.Visible = false;
        formElem.SiteName             = SiteContext.CurrentSiteName;

        titleElem.TitleText   = GetString("macros.macrorule.changeparameter");
        btnAddaClause.ToolTip = GetString("macros.macrorule.addclause");
        btnAddaClause.Click  += btnAddClause_Click;

        // Drop cue
        Panel pnlCue = new Panel();

        pnlCue.ID       = "pnlCue";
        pnlCue.CssClass = "MacroRuleCue";
        pnlCondtion.Controls.Add(pnlCue);

        pnlCue.Controls.Add(new LiteralControl(" "));
        pnlCue.Style.Add("display", "none");

        // Create drag and drop extender
        extDragDrop    = new DragAndDropExtender();
        extDragDrop.ID = "extDragDrop";
        extDragDrop.TargetControlID     = pnlCondtion.ID;
        extDragDrop.DragItemClass       = "MacroRule";
        extDragDrop.DragItemHandleClass = "MacroRuleHandle";
        extDragDrop.DropCueID           = pnlCue.ID;
        extDragDrop.OnClientDrop        = "OnDropRule";
        pnlCondtion.Controls.Add(extDragDrop);

        // Load the rule set
        if (!RequestHelper.IsPostBack())
        {
            if (ShowGlobalRules || !string.IsNullOrEmpty(RuleCategoryNames))
            {
                string where = (ShowGlobalRules ? "MacroRuleResourceName IS NULL OR MacroRuleResourceName = ''" : "");

                // Append rules module name condition
                if (!string.IsNullOrEmpty(RuleCategoryNames))
                {
                    bool          appendComma = false;
                    StringBuilder sb          = new StringBuilder();
                    string[]      names       = RuleCategoryNames.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string n in names)
                    {
                        string name = "'" + SqlHelper.GetSafeQueryString(n.Trim(), false) + "'";
                        if (appendComma)
                        {
                            sb.Append(",");
                        }
                        sb.Append(name);
                        appendComma = true;
                    }
                    where = SqlHelper.AddWhereCondition(where, "MacroRuleResourceName IN (" + sb.ToString() + ")", "OR");
                }

                // Append require context condition
                switch (DisplayRuleType)
                {
                case 1:
                    where = SqlHelper.AddWhereCondition(where, "MacroRuleRequiresContext = 0", "AND");
                    break;

                case 2:
                    where = SqlHelper.AddWhereCondition(where, "MacroRuleRequiresContext = 1", "AND");
                    break;
                }

                // Select only enabled rules
                where = SqlHelper.AddWhereCondition(where, "MacroRuleEnabled = 1");

                DataSet ds = MacroRuleInfoProvider.GetMacroRules(where, "MacroRuleDisplayName", 0, "MacroRuleID, MacroRuleDisplayName, MacroRuleDescription, MacroRuleRequiredData");
                if (!DataHelper.DataSourceIsEmpty(ds))
                {
                    MacroResolver resolver = MacroResolverStorage.GetRegisteredResolver(ResolverName);
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        bool add = true;
                        if (resolver != null)
                        {
                            // Check the required data, all specified data have to be present in the resolver
                            string requiredData = ValidationHelper.GetString(dr["MacroRuleRequiredData"], "");
                            if (!string.IsNullOrEmpty(requiredData))
                            {
                                string[] required = requiredData.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                                foreach (var req in required)
                                {
                                    if (!resolver.IsDataItemAvailable(req))
                                    {
                                        add = false;
                                        break;
                                    }
                                }
                            }
                        }

                        if (add)
                        {
                            var      ruleId = dr["MacroRuleID"].ToString();
                            ListItem item   = new ListItem(dr["MacroRuleDisplayName"].ToString(), ruleId);
                            lstRules.Items.Add(item);

                            // Save the tooltip
                            RulesTooltips[ruleId] = ResHelper.LocalizeString(ValidationHelper.GetString(dr["MacroRuleDescription"], ""));
                        }
                    }
                }
                if (lstRules.Items.Count > 0)
                {
                    lstRules.SelectedIndex = 0;
                }
            }
        }

        // Make sure that one user click somewhere else than to any rule, selection will disappear
        pnlCondtion.Attributes["onclick"] = "if (!doNotDeselect && !isCTRL) { jQuery('.RuleSelected').removeClass('RuleSelected'); document.getElementById('" + hdnSelected.ClientID + "').value = ';'; }; doNotDeselect = false;";

        LoadFormDefinition(false);

        // Set the default button for parameter edit dialog so that ENTER key works to submit the parameter value
        pnlParameterPopup.DefaultButton = btnSetParameter.ID;

        // Ensure correct edit dialog show/hide (because of form controls which cause postback)
        btnSetParameter.OnClientClick = "HideParamEdit();";
        btnCancel.OnClientClick       = "HideParamEdit();";
        if (ShowParameterEdit)
        {
            mdlDialog.Show();
        }

        if (!string.IsNullOrEmpty(hdnScroll.Value))
        {
            // Preserve scroll position
            ScriptHelper.RegisterStartupScript(this.Page, typeof(string), "MacroRulesScroll", "setTimeout('setScrollPosition()', 100);", true);
        }

        // Add tooltips to the rules in the list
        foreach (ListItem item in lstRules.Items)
        {
            if (RulesTooltips.ContainsKey(item.Value))
            {
                item.Attributes.Add("title", RulesTooltips[item.Value]);
            }
        }
    }