protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeCategoryTree();
                int count = RelatedProductDataSource.CountForProduct(_Product.Id);
                if (count == 0)
                {
                    count = RelatedProductDataSource.CountForChildProduct(_Product.Id);
                }
                if (count == 0)
                {
                    Filter.SelectedValue = "4";
                }
                else
                {
                    Filter.SelectedValue = "0";
                }
                SearchButton_Click(null, null);
            }

            if (_DisplayCategorySearch)
            {
                // DISPLAY AUTO COMPLETE FOR CATEGORY SEARCH OPTION
                string js = PageHelper.GetAutoCompleteScript("../../CategorySuggest.ashx", CategoryAutoComplete, HiddenSelectedCategoryId, "Key", "Value");

                ScriptManager.RegisterStartupScript(MainContentAjax, this.GetType(), "CATEGORY_SUGGEST", js, true);
                CategoryAutoComplete.Visible = true;
                CategoryList.Visible         = false;
            }
        }
        protected CrossSellState GetCrossSellState(int firstProductId, int secondProductId)
        {
            int masterCount = RelatedProductDataSource.GetRelatedCount(firstProductId, secondProductId);
            int childCount  = RelatedProductDataSource.GetRelatedCount(secondProductId, firstProductId);

            CrossSellState crossSellState = CrossSellState.Unlinked;

            if (masterCount > 0 && childCount > 0)
            {
                crossSellState = CrossSellState.CrossLinked;
            }
            else
            if (masterCount > 0 && childCount == 0)
            {
                crossSellState = CrossSellState.LinksTo;
            }
            else
            if (masterCount == 0 && childCount > 0)
            {
                crossSellState = CrossSellState.LinkedFrom;
            }
            return(crossSellState);
        }
        protected void AdjustCrossSellLinking(int firstProductId, int secondProductId, CrossSellState crossSellState)
        {
            RelatedProduct toReleatedProduct   = RelatedProductDataSource.Load(firstProductId, secondProductId);
            RelatedProduct fromReleatedProduct = RelatedProductDataSource.Load(secondProductId, firstProductId);

            switch (crossSellState)
            {
            case CrossSellState.Unlinked:

                if (toReleatedProduct != null)
                {
                    toReleatedProduct.Delete();
                }

                if (fromReleatedProduct != null)
                {
                    fromReleatedProduct.Delete();
                }

                break;

            case CrossSellState.CrossLinked:

                if (toReleatedProduct == null)
                {
                    toReleatedProduct = new RelatedProduct(firstProductId, secondProductId);
                    toReleatedProduct.Save();
                }

                if (fromReleatedProduct == null)
                {
                    fromReleatedProduct = new RelatedProduct(secondProductId, firstProductId);
                    fromReleatedProduct.Save();
                }

                break;

            case CrossSellState.LinksTo:
                if (toReleatedProduct == null)
                {
                    toReleatedProduct = new RelatedProduct(firstProductId, secondProductId);
                    toReleatedProduct.Save();
                }
                if (fromReleatedProduct != null)
                {
                    fromReleatedProduct.Delete();
                }
                break;

            case CrossSellState.LinkedFrom:
                if (toReleatedProduct != null)
                {
                    toReleatedProduct.Delete();
                }
                if (fromReleatedProduct == null)
                {
                    fromReleatedProduct = new RelatedProduct(secondProductId, firstProductId);
                    fromReleatedProduct.Save();
                }
                break;
            }
        }