private void TryLoadAdvertisingBanner()
	{
        int id = ChargeId;
        if (id <= 0)
        {
            _editorMode = BXAdminPageEditorMode.Creation;
            _charge = new BXAdvertisingBanner();
			if(ChargeSpaceCode.Length > 0)
			{
				BXAdvertisingSpaceCollection c = BXAdvertisingSpace.GetList(
					new BXFilter(new BXFilterItem(BXAdvertisingSpace.Fields.Code, BXSqlFilterOperators.Equal, ChargeSpaceCode)),
					null,
					new BXSelect(BXSelectFieldPreparationMode.Normal, BXAdvertisingSpace.Fields.Id),
					new BXQueryParams(BXPagingOptions.Top(1)));

				if(c.Count > 0)
					_charge.SpaceId = c[0].Id;
			}
        }
        else
        {
            _editorMode = BXAdminPageEditorMode.Modification;
            if ((_charge = BXAdvertisingBanner.GetById(id, BXTextEncoder.EmptyTextEncoder)) == null)
            {
                _errorMessage = string.Format(GetMessageRaw("Error.UnableToFindAdvertisingBanner"), id);
                _editorError = AdvertisingBannerEditorError.IsNotFound;
                return;
            }
        }

        if (!IsPostBack)
        {
            abCode.Text = _charge.Code;
            abActive.Checked = _charge.Active;

            DateTime? rotationStart = _charge.DateOfRotationStart;
            //if (rotationStart.HasValue)
            //    AdvertisingBannerRotationPeriod.StartDate = rotationStart.Value;
            //else
            //    AdvertisingBannerRotationPeriod.SetEmptyStartDate();

            if (rotationStart.HasValue)
                abRotationPeriodStartTbx.Text = rotationStart.Value.ToString("d");


            DateTime? rotationFinish = _charge.DateOfRotationFinish;
            //if (rotationFinish.HasValue)
            //    AdvertisingBannerRotationPeriod.EndDate = rotationFinish.Value;
            //else
            //    AdvertisingBannerRotationPeriod.SetEmptyStartDate();
            if (rotationFinish.HasValue)
                abRotationPeriodFinishTbx.Text = rotationFinish.Value.ToString("d");

            if (_charge.SpaceId <= 0)
            {
                if (abSpace.Items.Count > 0)
                    abSpace.SelectedIndex = 0;
            }
            else
            {
                ListItem item = abSpace.Items.FindByValue(_charge.SpaceId.ToString());
                if (item != null)
                    item.Selected = true;
            }

            abWeight.Text = _charge.Weight.ToString();

            ListItem contentTypeItem = abContentType.Items.FindByValue(_charge.ContentType.ToString("d"));
            
            if (contentTypeItem != null)
                contentTypeItem.Selected = true;

            abName.Text = _charge.Name;
            abDescription.Text = _charge.Description;
            abXmlId.Text = _charge.XmlId;
            if (_charge.ContentType == BXAdvertisingBannerContentType.Image)
                abImageContentFile.FileId = _charge.ContentFileId;
            else if (_charge.ContentType == BXAdvertisingBannerContentType.Flash)
            {
                abFlashVersion.Text = _charge.FlashVersion;
                abFlashContentFile.FileId = _charge.ContentFileId;
                abFlashAltImageFile.FileId = _charge.FlashAltImageFileId;
            }
            else if (_charge.ContentType == BXAdvertisingBannerContentType.Silverlight)
            {
                abSilverlightVersion.Text = _charge.FlashVersion;
                abSLContentFile.FileId = _charge.ContentFileId;
                abSLAltImageFile.FileId = _charge.FlashAltImageFileId;
            }
            
            //abFlashDynamicCreation.Checked = _charge.FlashDynamicCreation;
            

            
            //abFlashUseCustomUrl.Checked = _charge.FlashUseCustomUrl;
            abHtmlCode.StartMode = _charge.TextContentType == BXAdvertisingBannerTextContentType.Plain ? BXWebEditor.StartModeType.PlainText : BXWebEditor.StartModeType.HTMLVisual;
            abHtmlCode.Content = _charge.TextContent;

            abLinkUrl.Text = _charge.LinkUrl;
            LinkTargetFromCharge(BXAdvertisingBannerContentType.Image);
            abToolTip.Text = _charge.ToolTip;

            abFlashLinkUrl.Text = _charge.LinkUrl;
            LinkTargetFromCharge(BXAdvertisingBannerContentType.Flash);
            abFlashToolTip.Text = _charge.ToolTip;

            abRotationWeekSchedule.HourSpans.Clear();
            BXAdvertisingBannerWeekScheduleHourSpanCollection bannerHourSpans = _charge.RotationHourSpans;
            if(bannerHourSpans.Count > 0)
            {
                BXWeekScheduleHourSpanControl[] hourSpanControls = new BXWeekScheduleHourSpanControl[bannerHourSpans.Count];
                for (int i = 0; i < bannerHourSpans.Count; i++)
                    abRotationWeekSchedule.HourSpans.Add(new BXWeekScheduleHourSpanControl(bannerHourSpans[i].Item));
            }
            else if (_charge.IsNew)
                abRotationWeekSchedule.HourSpans.Add(new BXWeekScheduleHourSpanControl(new BXWeekScheduleHourSpan(0, 168)));

            ListItemCollection siteItems = abSites.Items;
            string[] siteIds = _charge.GetSiteIds();
            foreach (string siteId in siteIds)
            {
                ListItem li = siteItems.FindByValue(siteId);
                if (li != null)
                    li.Selected = true;
            }
            StringBuilder bannerUrlTemplatePermitted = new StringBuilder(),
                bannerUrlTemplateNotPermitted = new StringBuilder();
            BXAdvertisingBannerUrlTemplateCollection bannerUrlTemplates = _charge.RotationUrlTemplates;
            foreach (BXAdvertisingBannerUrlTemplate bannerUrlTemplate in bannerUrlTemplates)
            {
                if(bannerUrlTemplate.IsPermitted)
                    bannerUrlTemplatePermitted.AppendLine(bannerUrlTemplate.UrlTemplate);
                else
                    bannerUrlTemplateNotPermitted.AppendLine(bannerUrlTemplate.UrlTemplate);
            }
            AdvertisingPermittedForRotationUrlTemplates.Text = bannerUrlTemplatePermitted.ToString();
            AdvertisingNotPermittedForRotationUrlTemplates.Text = bannerUrlTemplateNotPermitted.ToString();

            ListItemCollection userRolesItems = abUserRoles.Items;
            int[] userRoleIds = _charge.GetVisitorRoleIds();
            foreach (int userRoleId in userRoleIds)
            {
                ListItem li = userRolesItems.FindByValue(userRoleId.ToString());
                if (li != null)
                    li.Selected = true;
            }

            EnableRotationForVisitorRoles.Items.FindByValue(_charge.EnableRotationForVisitorRoles ? "Y" : "N").Selected = true;

            abEnableFixedRotation.Checked = _charge.IsNew ? true : _charge.EnableFixedRotation;
            //abEnableUniformRotationVelocity.Checked = _charge.EnableUniformRotationVelocity;
            RotationModeFromChange();
            if (_charge.MaxVisitorCount != 0)
                abMaxVisitorCount.Text = _charge.MaxVisitorCount.ToString();
            if (_charge.MaxDisplayCountPerVisitor != 0)
                abMaxDisplayCountPerVisitor.Text = _charge.MaxDisplayCountPerVisitor.ToString();
            if (_charge.MaxDisplayCount != 0)
                abMaxDisplayCount.Text = _charge.MaxDisplayCount.ToString();

            abEnableRedirectionCount.Checked = _charge.EnableRedirectionCount;
            if (_charge.MaxRedirectionCount != 0)
                abMaxRedirectionCount.Text = _charge.MaxRedirectionCount.ToString();
            if (_charge.ContentFileId > 0)
            {
                hfSLWidth.Value = _charge.ContentFile.Width.ToString();
                hfSLHeight.Value = _charge.ContentFile.Height.ToString();
            }
        }
        else
        {
            _charge.Code = abCode.Text;
            _charge.Active = abActive.Checked;

            //if (!AdvertisingBannerRotationPeriod.IsStartDateEmpty())
            //    _charge.DateOfRotationStart = AdvertisingBannerRotationPeriod.StartDate;
            //else
            //    _charge.DateOfRotationStart = null;

            //DateTime dt;
            //if (!string.IsNullOrEmpty(abRotationPeriodStartTbx.Text.Trim()) && DateTime.TryParse(abRotationPeriodStartTbx.Text.Trim(), out dt))
            //    _charge.DateOfRotationStart = dt;
           // else
           //     _charge.DateOfRotationStart = null;

            if (!string.IsNullOrEmpty(abRotationPeriodStartTbx.Text.Trim()))
            {
                DateTime startDate;
                if (DateTime.TryParse(abRotationPeriodStartTbx.Text.Trim(), out startDate))
                    _charge.DateOfRotationStart = startDate;
                else
                {
                    _errorMessage = string.Format(GetMessage("Message.CouldnotParseDate"), abRotationPeriodStartTbx.Text.Trim());
                    _editorError = EditorMode == BXAdminPageEditorMode.Creation ? AdvertisingBannerEditorError.Creation : AdvertisingBannerEditorError.Modification;
                    return;
                }
            }
            else
                _charge.DateOfRotationStart = null;

            if (!string.IsNullOrEmpty(abRotationPeriodFinishTbx.Text.Trim()))
            {
                DateTime finishDate;
                if (DateTime.TryParse(abRotationPeriodFinishTbx.Text.Trim(), out finishDate))
                    _charge.DateOfRotationFinish = finishDate;
                else
                {
                    _errorMessage = string.Format(GetMessage("Message.CouldnotParseDate"), abRotationPeriodFinishTbx.Text.Trim());
                    _editorError = EditorMode == BXAdminPageEditorMode.Creation ? AdvertisingBannerEditorError.Creation : AdvertisingBannerEditorError.Modification;
                    return;
                }
            }
            //DateTime dt;
            //if (!AdvertisingBannerRotationPeriod.IsEndDateEmpty())
            //    _charge.DateOfRotationFinish = AdvertisingBannerRotationPeriod.EndDate;
            //else
            //    _charge.DateOfRotationFinish = null;
            //if (!string.IsNullOrEmpty(abRotationPeriodFinishTbx.Text.Trim()) && DateTime.TryParse(abRotationPeriodFinishTbx.Text.Trim(), out dt))
            //    _charge.DateOfRotationFinish = dt;
            //else
            //    _charge.DateOfRotationFinish = null;

            if (abSpace.SelectedItem == null)
                _charge.SpaceId = 0;
            else
            {
                try
                {
                    _charge.SpaceId = Convert.ToInt32(abSpace.SelectedItem.Value);
                }
                catch (Exception /*exception*/)
                {
                    _charge.SpaceId = 0;
                }
            }

            try
            {
                _charge.Weight = Convert.ToInt32(abWeight.Text);
            }
            catch (Exception /*exc*/) 
            {
                _charge.Weight = BXAdvertisingBanner.DefaultWeightValue;
            }

            _charge.Name = abName.Text;
            _charge.Description = abDescription.Text;
            _charge.XmlId = abXmlId.Text;

            if (!_charge.IsNew)
            {
                bool aboutContentFileDeletion = false;
                if (_charge.ContentType == BXAdvertisingBannerContentType.Image)
                    aboutContentFileDeletion = abImageContentFile.AboutFileDeletion;
                else if (_charge.ContentType == BXAdvertisingBannerContentType.Flash)
                    aboutContentFileDeletion = abFlashContentFile.AboutFileDeletion;
                else if (_charge.ContentType == BXAdvertisingBannerContentType.Silverlight)
                    aboutContentFileDeletion = abSLContentFile.AboutFileDeletion;

                if (aboutContentFileDeletion)
                {
                    BXFile contentFile = _charge.ContentFile;
                    if (contentFile != null)
                        _charge.ContentFile = null;
                }
            }

            //BXFile contentFile = abImageContentFile.File;
            //if (contentFile != null)
            //{
            //    _charge.ContentFile = contentFile;
            //    if (contentFile.ContentType.ToUpperInvariant().StartsWith("IMAGE", StringComparison.InvariantCulture))
            //         contentType = BXAdvertisingBannerContentType.Image;
            //     else if (string.Equals(contentFile.ContentType.ToUpperInvariant(), "APPLICATION/X-SHOCKWAVE-FLASH", StringComparison.InvariantCulture))
            //         contentType = BXAdvertisingBannerContentType.Flash;  
            //}
            int flashVer = 0;
            string sVer = String.Empty;
            if (abContentType.SelectedItem != null)
            {
                BXAdvertisingBannerContentType? userContentType = null;
                try
                {
                    userContentType = (BXAdvertisingBannerContentType)Enum.Parse(typeof(BXAdvertisingBannerContentType), abContentType.SelectedItem.Value);
                }
                catch (Exception /*exception*/)
                {
                }

                if (userContentType.HasValue)
                {
                    BXFile contentFile = null;
                    if (userContentType.Value == BXAdvertisingBannerContentType.Image)
                    {
                        contentFile = abImageContentFile.File;
                        _charge.FlashWMode = abFlashWMode.Text;
                        if (contentFile != null)
                        {
                            if (!contentFile.ContentType.ToUpperInvariant().StartsWith("IMAGE", StringComparison.InvariantCulture))
                            {
                                contentFile = null;
                                _errorMessage = GetMessage("Message.ContentTypeContradictFile");
                                _editorError = EditorMode == BXAdminPageEditorMode.Creation ? AdvertisingBannerEditorError.Creation : AdvertisingBannerEditorError.Modification;
                                abImageContentFile.FileId = _charge.ContentFileId;
                            }
                            if (contentFile != null)
                            {
                                contentFile.Save();
                                abImageContentFile.FileId  = contentFile.Id;
                            }
                        }
                    }
                    else if (userContentType.Value == BXAdvertisingBannerContentType.Flash)
                    {
                        _charge.FlashWMode = abFlashWMode.Text;
                        contentFile = abFlashContentFile.File;
                        if (contentFile != null)
                        {
                            if (!contentFile.ContentType.ToUpperInvariant().StartsWith("APPLICATION/X-SHOCKWAVE-FLASH", StringComparison.InvariantCulture))
                            {
                                contentFile = null;
                                _errorMessage = GetMessage("Message.ContentTypeContradictFile");
                                _editorError = EditorMode == BXAdminPageEditorMode.Creation ? AdvertisingBannerEditorError.Creation : AdvertisingBannerEditorError.Modification;
                                abFlashContentFile.FileId = _charge.ContentFileId;
                            }
                            if (contentFile != null)
                            {
                                contentFile.Save();
                                abFlashContentFile.FileId = contentFile.Id;
                            }
                        }
                        try
                        {
                            flashVer = Convert.ToInt32(abFlashVersion.Text);
                            _charge.FlashVersion = flashVer.ToString();
                        }
                        catch (Exception /*exc*/)
                        {
                            _charge.FlashVersion = string.Empty;
                        }

                    }
                    else if (userContentType.Value == BXAdvertisingBannerContentType.Silverlight)
                    {
                        contentFile = abSLContentFile.File;
                        if (contentFile != null)
                        {
                            if (!contentFile.ContentType.ToUpperInvariant().StartsWith("APPLICATION/OCTET-STREAM", StringComparison.InvariantCulture))
                            {
                                contentFile = null;
                                _errorMessage = GetMessage("Message.ContentTypeContradictFile");
                                _editorError = EditorMode == BXAdminPageEditorMode.Creation ? AdvertisingBannerEditorError.Creation : AdvertisingBannerEditorError.Modification;
                                abSLContentFile.FileId = _charge.ContentFileId;
                            }
                            if (contentFile != null)
                            {
                                contentFile.Save();
                                abSLContentFile.FileId = contentFile.Id;
                            }


                        }

                        _charge.FlashVersion = abSilverlightVersion.Text;
                    }

                    if (contentFile != null)
                        _charge.ContentFile = contentFile;
                    else
                    {
                        if (_charge.ContentType != userContentType.Value)
                            _charge.ContentFile = null;
                        _charge.ContentType = userContentType.Value;
                    }
                }
            }

            _charge.TextContentType = abHtmlCode.StartMode == BXWebEditor.StartModeType.HTMLVisual ? BXAdvertisingBannerTextContentType.Html : BXAdvertisingBannerTextContentType.Plain;

            
            //_charge.FlashDynamicCreation = abFlashDynamicCreation.Checked;





            if (abFlashAltImageFile.AboutFileDeletion || abSLAltImageFile.AboutFileDeletion)
                _charge.FlashAltImageFile = null;
            else if (abFlashAltImageFile.File != null)
                _charge.FlashAltImageFile = abFlashAltImageFile.File;
            else if (abSLAltImageFile.File != null)
                _charge.FlashAltImageFile = abSLAltImageFile.File;
            //_charge.FlashUseCustomUrl = abFlashUseCustomUrl.Checked;
           
            _charge.TextContent = abHtmlCode.Content;

            switch (_charge.ContentType)
            {
                case BXAdvertisingBannerContentType.Image:
                    _charge.LinkUrl = abLinkUrl.Text;
                    _charge.ToolTip = abToolTip.Text;
                    break;
                case BXAdvertisingBannerContentType.Flash:
                    _charge.LinkUrl = abFlashLinkUrl.Text;
                    _charge.ToolTip = abFlashToolTip.Text;
                    break;
                case BXAdvertisingBannerContentType.Silverlight:
                    _charge.LinkUrl = abFlashLinkUrl.Text;
                    _charge.ToolTip = abFlashToolTip.Text;
                    break;
            }

            LinkTargetToCharge();

            _charge.RotationHourSpans.Clear();
            foreach (BXWeekScheduleHourSpanControl hourSpan in abRotationWeekSchedule.HourSpans)
                _charge.RotationHourSpans.Add(new BXAdvertisingBannerWeekScheduleHourSpan(hourSpan.Item));

            List<string> siteIdList = new List<string>();
            ListItemCollection siteItems = abSites.Items;
            foreach (ListItem siteItem in siteItems)
            {
                if (!siteItem.Selected)
                    continue;
                siteIdList.Add(siteItem.Value);
            }
            _charge.SetSiteIds(siteIdList.ToArray());

            _charge.RotationUrlTemplates.Clear();
            _charge.RotationUrlTemplates.AddRange(LoadUrlTemplateArrayFromString(AdvertisingPermittedForRotationUrlTemplates.Text, true));
            _charge.RotationUrlTemplates.AddRange(LoadUrlTemplateArrayFromString(AdvertisingNotPermittedForRotationUrlTemplates.Text, false));

            List<int> roleIdList = new List<int>();
            ListItemCollection userRoleItems = abUserRoles.Items;
            foreach (ListItem userRoleItem in userRoleItems)
            {
                if (!userRoleItem.Selected)
                    continue;
                roleIdList.Add(Convert.ToInt32(userRoleItem.Value));
            }
            _charge.SetVisitorRoleIds(roleIdList.ToArray());
            _charge.EnableRotationForVisitorRoles = EnableRotationForVisitorRoles.SelectedItem != null ? string.Equals(EnableRotationForVisitorRoles.SelectedItem.Value, "Y", StringComparison.Ordinal) : false;

            _charge.EnableFixedRotation = abEnableFixedRotation.Checked;
            //_charge.EnableUniformRotationVelocity = abEnableUniformRotationVelocity.Checked;
            RotationModeToCharge();

            if (_charge.EnableFixedRotation)
            {
                try
                {
                    int val = Convert.ToInt32(abMaxDisplayCount.Text);
                    _charge.MaxDisplayCount = val >= 0 ? val : 0;
                }
                catch (Exception/*exc*/)
                {
                    _charge.MaxDisplayCount = 0;
                }

                try
                {
                    int val = Convert.ToInt32(abMaxVisitorCount.Text);
                    _charge.MaxVisitorCount = val >= 0 ? val : 0;
                }
                catch (Exception/*exc*/)
                {
                    _charge.MaxVisitorCount = 0;
                }

                try
                {
                    int val = Convert.ToInt32(abMaxDisplayCountPerVisitor.Text);
                    _charge.MaxDisplayCountPerVisitor = val >= 0 ? val : 0;
                }
                catch (Exception/*exc*/)
                {
                    _charge.MaxDisplayCountPerVisitor = 0;
                }
            }

            _charge.EnableRedirectionCount = abEnableRedirectionCount.Checked;
            if (_charge.EnableRedirectionCount)
            {
                try
                {
                    int val = Convert.ToInt32(abMaxRedirectionCount.Text);
                    _charge.MaxRedirectionCount = val >= 0 ? val : 0;
                }
                catch (Exception/*exc*/)
                {
                    _charge.MaxRedirectionCount = 0;
                }
            }
        }
	}
    public AdvertisingBannerWrapper(BXAdvertisingBanner charge, BXAdminPage parentPage)
    {
        if (charge == null)
            throw new ArgumentNullException("charge");

        if (parentPage == null)
            throw new ArgumentNullException("parentPage");

        _charge = charge;
        _parentPage = parentPage;
    }