protected void Page_Init(object sender, EventArgs e)
    {
        RegisterThisId();

        #region Init Controls
        //Since Controls are placed inside the PXForm we need to manually find them and init the referencing them variables:
        divMessage = PXFormView1.FindControl("divMessage") as Control;
        lblMessage = PXFormView1.FindControl("lblMessage") as Label;
        txtSearch  = PXFormView1.FindControl("txtSearch") as PXTextEdit;
        btnSearch  = PXFormView1.FindControl("btnSearch") as PXButton;
        filterrow  = PXFormView1.FindControl("filterrow") as HtmlTableRow;

        var pnlResults = PXFormView1.FindControl("pnlResults") as PXSmartPanel;
        resultsTable       = pnlResults.FindControl("resultsTable") as HtmlTable;
        lblFullTextWarning = pnlResults.FindControl("lblFullTextWarning") as Label;
        linkPrev           = pnlResults.FindControl("linkPrev") as LinkButton;
        linkNext           = pnlResults.FindControl("linkNext") as LinkButton;

        divTips                   = PXFormView1.FindControl("divTips") as HtmlGenericControl;
        lblSearchTips             = PXFormView1.FindControl("lblSearchTips") as Label;
        liCheckSpelling           = PXFormView1.FindControl("liCheckSpelling") as HtmlGenericControl;
        liSimplifyQuery           = PXFormView1.FindControl("liSimplifyQuery") as HtmlGenericControl;
        liTryOtherWords           = PXFormView1.FindControl("liTryOtherWords") as HtmlGenericControl;
        lblSearchTips.Text        = PXMessages.LocalizeNoPrefix(Messages.SearchTips);
        liCheckSpelling.InnerText = PXMessages.LocalizeNoPrefix(Messages.CheckSpelling);
        liSimplifyQuery.InnerText = PXMessages.LocalizeNoPrefix(Messages.SimplifyQuery);
        liTryOtherWords.InnerText = PXMessages.LocalizeNoPrefix(Messages.TryOtherWords);
        lblActiveModule           = PXFormView1.FindControl("lblActiveModule") as PXLabel;
        btnClearActiveModule      = PXFormView1.FindControl("btnClearActiveModule") as PXButton;
        btnShowList               = PXFormView1.FindControl("btnShowList") as PXButton;
        cellList                  = PXFormView1.FindControl("cellList") as HtmlTableCell;
        filtertable               = PXFormView1.FindControl("filtertable") as HtmlTable;
        #endregion

        lblMessage.Text = PXMessages.LocalizeNoPrefix(Messages.SpecifySearchRequest);

        activeModule = Request.Params["am"];
        Guid am;
        if (Guid.TryParse(activeModule, out am))
        {
            comboBoxLookupList = SearchService.BuildComboList(am);
        }
        else
        {
            comboBoxLookupList = SearchService.BuildComboList(null);
        }

        searchType = comboBoxLookupList[0].Type;

        string query = Request.Params["query"];
        string st    = Request.Params["st"];
        if (!string.IsNullOrEmpty(st))
        {
            Enum.TryParse <SearchService.SearchLookupType>(Request.Params["st"], true, out searchType);
        }

        if (am == Guid.Empty)
        {
            if (searchType == SearchService.SearchLookupType.ActiveModule)
            {
                searchType = SearchService.SearchLookupType.AllEntities;
            }
            else if (searchType == SearchService.SearchLookupType.ActiveWiki)
            {
                searchType = SearchService.SearchLookupType.AllHelp;
            }
        }

        txtSearch.Text = Request.Params["query"];

        if (query == null || string.IsNullOrEmpty(query.Trim()))
        {
            divMessage.Visible = true;
        }
        else
        {
            divMessage.Visible = false;
        }

        if (searchType == SearchService.SearchLookupType.ActiveModule ||
            searchType == SearchService.SearchLookupType.ActiveWiki ||
            searchType == SearchService.SearchLookupType.Files || searchType == SearchService.SearchLookupType.Screen)
        {
            filtertable.Visible  = true;
            lblActiveModule.Text = comboBoxLookupList.Single(a => a.Type.Equals(searchType)).Name;
        }
        else
        {
            filtertable.Visible = false;
        }

        SearchService.SearchLookupType newSt = searchType;
        if (searchType == SearchService.SearchLookupType.ActiveModule || searchType == SearchService.SearchLookupType.Files || searchType == SearchService.SearchLookupType.Screen)
        {
            newSt = SearchService.SearchLookupType.AllEntities;
        }

        if (searchType == SearchService.SearchLookupType.ActiveWiki)
        {
            newSt = SearchService.SearchLookupType.AllHelp;
        }

        UriBuilder urlClear = new UriBuilder(new Uri(HttpContext.Current.Request.GetExternalUrl(), this.ResolveUrl(Request.RawUrl)));
        btnClearActiveModule.NavigateUrl = urlClear.Path + string.Format("?query={0}&am={1}&st={2}", query, activeModule, newSt);

        btnClearActiveModule.RenderAsButton = false;
        btnShowList.RenderAsButton          = false;
        //txtSearch.BorderColor = Color.Transparent;

        foreach (SearchService.SearchLookupItem item in comboBoxLookupList)
        {
            bool addActiveModule = true;
            bool addActiveWiki   = true;
            bool addAllEntity    = true;
            bool addAllHelp      = true;


            if (searchType == SearchService.SearchLookupType.ActiveModule)
            {
                addActiveModule = false;
                addAllEntity    = false;
            }

            if (searchType == SearchService.SearchLookupType.ActiveWiki)
            {
                addActiveWiki = false;
                addAllHelp    = false;
            }

            if (searchType == SearchService.SearchLookupType.AllEntities)
            {
                addAllEntity = false;
            }

            if (searchType == SearchService.SearchLookupType.AllHelp)
            {
                addAllHelp = false;
            }

            //bool addItem = true;

            if (item.Type == SearchService.SearchLookupType.ActiveModule && !addActiveModule)
            {
                continue;
            }
            if (item.Type == SearchService.SearchLookupType.ActiveWiki && !addActiveWiki)
            {
                continue;
            }
            if (item.Type == SearchService.SearchLookupType.AllEntities && !addAllEntity)
            {
                continue;
            }
            if (item.Type == SearchService.SearchLookupType.AllHelp && !addAllHelp)
            {
                continue;
            }

            UriBuilder url         = new UriBuilder(new Uri(HttpContext.Current.Request.GetExternalUrl(), this.ResolveUrl(Request.RawUrl)));
            string     navigateUrl = url.Path + string.Format("?query={0}&am={1}&st={2}", query, activeModule, item.Type);

            //var link = new HyperLink() { CssClass = "comboItem", NavigateUrl = navigateUrl, Text = item.Name };
            //cellList.Controls.Add(link);

            PXButton button = new PXButton();
            button.ApplyStyleSheetSkin(this.Page);
            button.RenderAsButton                        = false;
            button.TextAlign                             = HorizontalAlign.Left;
            button.CssClass                              = "comboItem";
            button.Text                                  = item.Name;
            button.Styles.Hover.Cursor                   = WebCursor.Hand;
            button.NavigateUrl                           = navigateUrl;
            button.AutoCallBack.Command                  = "navigate";
            button.AutoCallBack.Enabled                  = true;
            button.AutoCallBack.Behavior.PostData        = PostDataMode.Container;
            button.AutoCallBack.Behavior.RepaintControls = RepaintMode.All;
            button.AutoCallBack.Behavior.ContainerID     = "PXFormView1";
            button.AutoCallBack.Behavior.BlockPage       = true;
            button.ID = "Button_" + item.Type;
            cellList.Controls.Add(button);
        }
    }