protected override void OnPreRender(EventArgs e)
    {
        if (Visible)
        {
            if (pnlEdit.Visible)
            {
                // Register other scripts which are necessary in edit mode
                if (UseProgressScript)
                {
                    ScriptHelper.RegisterLoader(Page);
                }

                // Register script
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("function Delete_" + menuElem.ClientID + "(NodeID) { " + Page.ClientScript.GetPostBackEventReference(btnDelete, null) + "; } \n");
                sb.AppendLine("function " + formElem.ClientID + "_RefreshForm(){" + Page.ClientScript.GetPostBackEventReference(btnRefresh, "") + " }");

                // Register the scripts
                AddScript(sb.ToString());

                ScriptHelper.RegisterBootstrapScripts(Page);

                if (formElem.FieldControls != null)
                {
                    // Disable maximize plugin on HTML editors
                    var htmlControls = formElem.FormInformation.GetFields(FormFieldControlTypeEnum.HtmlAreaControl);

                    foreach (FormFieldInfo field in htmlControls)
                    {
                        Control       control    = formElem.FieldControls[field.Name];
                        CMSHtmlEditor htmlEditor = ControlsHelper.GetChildControl(control, typeof(CMSHtmlEditor)) as CMSHtmlEditor;
                        if (htmlEditor != null)
                        {
                            var parameters = new LiveSiteWidgetsParameters(CurrentDocument.NodeAliasPath, PortalContext.ViewMode)
                            {
                                IsInlineWidget = true
                            };
                            htmlEditor.Node = CurrentDocument;
                            htmlEditor.RemovePlugins.Add("maximize");
                            htmlEditor.Config["CurrentHash"]     = parameters.GetHashString();
                            htmlEditor.Config["CurrentViewMode"] = PortalContext.ViewMode.ToStringRepresentation();
                        }
                    }
                }

                if (!NewDocument && !NewCulture)
                {
                    formElem.Enabled = AllowSave;
                }
            }
        }

        base.OnPreRender(e);
    }
    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Do nothing
        }
        else
        {
            if (CurrentPageInfo != null)
            {
                PageInfo pi = CurrentPageInfo;

                // Make visible, visibility according to the current state will be set later (solves issue with changing visibility during postbacks)
                Visible = true;

                CMSPagePlaceholder parentPlaceHolder = PortalHelper.FindParentPlaceholder(this);

                // Nothing to render, nothing to do
                if ((!DisplayAddButton && !DisplayResetButton) ||
                    ((parentPlaceHolder != null) && (parentPlaceHolder.UsingDefaultPage || (parentPlaceHolder.PageInfo.DocumentID != pi.DocumentID))))
                {
                    Visible = false;
                    return;
                }

                var currentUser = MembershipContext.AuthenticatedUser;
                zoneType = WidgetZoneType.ToEnum <WidgetZoneTypeEnum>();


                // Check security
                if (((zoneType == WidgetZoneTypeEnum.Group) && !currentUser.IsGroupAdministrator(pi.NodeGroupID)) ||
                    ((zoneType == WidgetZoneTypeEnum.User || zoneType == WidgetZoneTypeEnum.Dashboard) && !AuthenticationHelper.IsAuthenticated()))
                {
                    Visible      = false;
                    resetAllowed = false;
                    return;
                }

                // Displaying - Editor zone only in edit mode, User/Group zone only in Live site/Preview mode
                if (((zoneType == WidgetZoneTypeEnum.Editor) && !PortalManager.ViewMode.IsOneOf(ViewModeEnum.Edit, ViewModeEnum.EditDisabled, ViewModeEnum.EditLive)) ||
                    (((zoneType == WidgetZoneTypeEnum.User) || (zoneType == WidgetZoneTypeEnum.Group)) && !PortalManager.ViewMode.IsOneOf(ViewModeEnum.LiveSite, ViewModeEnum.Preview)) ||
                    ((zoneType == WidgetZoneTypeEnum.Dashboard) && ((PortalManager.ViewMode != ViewModeEnum.DashboardWidgets) || (String.IsNullOrEmpty(PortalContext.DashboardName)))))
                {
                    Visible      = false;
                    resetAllowed = false;
                    return;
                }

                // Get current document
                TreeNode currentNode = DocumentHelper.GetDocument(pi.DocumentID, TreeProvider);
                if (((zoneType == WidgetZoneTypeEnum.Editor) && (!currentUser.CheckPrivilegeLevel(UserPrivilegeLevelEnum.Editor, SiteContext.CurrentSiteName) || (currentUser.IsAuthorizedPerDocument(currentNode, NodePermissionsEnum.Modify) == AuthorizationResultEnum.Denied))))
                {
                    Visible      = false;
                    resetAllowed = false;
                    return;
                }

                // If use checkin checkout enabled, check if document is checkout by current user
                if (zoneType == WidgetZoneTypeEnum.Editor)
                {
                    if (currentNode != null)
                    {
                        WorkflowManager wm = WorkflowManager.GetInstance(TreeProvider);
                        // Get workflow info
                        WorkflowInfo wi = wm.GetNodeWorkflow(currentNode);

                        // Check if node is under workflow and if use checkin checkout enabled
                        if ((wi != null) && (wi.UseCheckInCheckOut(SiteContext.CurrentSiteName)))
                        {
                            int checkedOutBy = currentNode.DocumentCheckedOutByUserID;

                            // Check if document is checkout by current user
                            if (checkedOutBy != MembershipContext.AuthenticatedUser.UserID)
                            {
                                Visible      = false;
                                resetAllowed = false;
                                return;
                            }
                        }
                    }
                }

                // Find widget zone
                PageTemplateInfo pti = pi.UsedPageTemplateInfo;

                // ZodeID specified directly
                if (!String.IsNullOrEmpty(WidgetZoneID))
                {
                    zoneInstance = pti.TemplateInstance.GetZone(WidgetZoneID);
                }

                // Zone not find or specified zone is not of correct type
                if ((zoneInstance != null) && (zoneInstance.WidgetZoneType != zoneType))
                {
                    zoneInstance = null;
                }

                // For delete all variants all zones are necessary
                if (parentPlaceHolder != null)
                {
                    var zones = parentPlaceHolder.WebPartZones;
                    if (zones != null)
                    {
                        foreach (CMSWebPartZone zone in zones)
                        {
                            if ((zone.ZoneInstance != null) && (zone.ZoneInstance.WidgetZoneType == zoneType))
                            {
                                zoneInstances.Add(zone.ZoneInstance);
                                if (zoneInstance == null)
                                {
                                    zoneInstance = zone.ZoneInstance;
                                }
                            }
                        }
                    }
                }

                // No suitable zones on the page, nothing to do
                if (zoneInstance == null)
                {
                    Visible      = false;
                    resetAllowed = false;
                    return;
                }

                // Adding is enabled
                if (DisplayAddButton)
                {
                    btnAddWidget.Visible = true;
                    btnAddWidget.Text    = GetAddWidgetButtonText();

                    int templateId = 0;
                    if (pi.UsedPageTemplateInfo != null)
                    {
                        templateId = pi.UsedPageTemplateInfo.PageTemplateId;
                    }

                    var dialogParams = new LiveSiteWidgetsParameters(pi.NodeAliasPath, ViewMode)
                    {
                        ZoneId     = zoneInstance.ZoneID,
                        TemplateId = templateId,
                    };
                    var hash = dialogParams.GetHashString();

                    addScript = (PortalContext.ViewMode == ViewModeEnum.EditLive ? "OEDeactivateWebPartBorder({ webPartSpanId: $cmsj('.OnSiteMenuTable').parent().attr('id').replace('OE_OE_', 'OE_')}, null );" : String.Empty) + "NewWidget(new zoneProperties('" + zoneInstance.ZoneID + "', '" + pi.NodeAliasPath + "', '" + templateId + "', undefined, undefined, undefined, undefined, '" + ViewMode + "', '" + hash + "')); return false;";
                    btnAddWidget.Attributes.Add("onclick", addScript);
                }

                // Reset is enabled
                if (DisplayResetButton)
                {
                    btnReset.Visible = true;
                    btnReset.Text    = GetResetButtonText();
                    btnReset.Click  += new EventHandler(btnReset_Click);

                    // Add confirmation if required
                    if (ResetConfirmationRequired)
                    {
                        btnReset.Attributes.Add("onclick", "if (!confirm(" + ScriptHelper.GetString(PortalHelper.LocalizeStringForUI("widgets.resetzoneconfirmtext")) + ")) return false;");
                    }
                }

                // Set the panel css clas with dependence on actions zone type
                switch (zoneType)
                {
                // Editor
                case WidgetZoneTypeEnum.Editor:
                    pnlWidgetActions.CssClass = "EditorWidgetActions";
                    break;

                // User
                case WidgetZoneTypeEnum.User:
                    pnlWidgetActions.CssClass = "UserWidgetActions";
                    break;

                // Group
                case WidgetZoneTypeEnum.Group:
                    pnlWidgetActions.CssClass = "GroupWidgetActions";
                    break;
                }
            }
        }
    }