protected void btnUpdateMailboxPlan_Click(object sender, EventArgs e)
        {
            Page.Validate(MainValidationGroup);

            if (!Page.IsValid)
                return;

            if (ViewState["MailboxPlanID"] == null)
                return;

            int mailboxPlanId = (int)ViewState["MailboxPlanID"];
            Providers.HostedSolution.Organization[] orgs = GetOrganizations();
            Providers.HostedSolution.ExchangeMailboxPlan plan;

            plan = ES.Services.ExchangeServer.GetExchangeMailboxPlan(orgs[0].Id, mailboxPlanId);

            if (plan.ItemId != orgs[0].Id)
            {
                messageBox.ShowErrorMessage("EXCHANGE_UNABLE_USE_SYSTEMPLAN");
                BindMailboxPlans();
                return;
            }



            plan = new Providers.HostedSolution.ExchangeMailboxPlan();
            plan.MailboxPlanId = (int)ViewState["MailboxPlanID"];
            plan.MailboxPlan = txtMailboxPlan.Text;
            plan.Archiving = RetentionPolicy;

            if (RetentionPolicy)
            {
            }
            else
            {
                plan.MailboxSizeMB = mailboxSize.QuotaValue;

                plan.IsDefault = false;
                plan.MaxRecipients = maxRecipients.QuotaValue;
                plan.MaxSendMessageSizeKB = maxSendMessageSizeKB.QuotaValue;
                plan.MaxReceiveMessageSizeKB = maxReceiveMessageSizeKB.QuotaValue;
                plan.EnablePOP = chkPOP3.Checked;
                plan.EnableIMAP = chkIMAP.Checked;
                plan.EnableOWA = chkOWA.Checked;
                plan.EnableMAPI = chkMAPI.Checked;
                plan.EnableActiveSync = chkActiveSync.Checked;
                plan.IssueWarningPct = sizeIssueWarning.ValueKB;
                if ((plan.IssueWarningPct == 0)) plan.IssueWarningPct = 100;
                plan.ProhibitSendPct = sizeProhibitSend.ValueKB;
                if ((plan.ProhibitSendPct == 0)) plan.ProhibitSendPct = 100;
                plan.ProhibitSendReceivePct = sizeProhibitSendReceive.ValueKB;
                if ((plan.ProhibitSendReceivePct == 0)) plan.ProhibitSendReceivePct = 100;
                plan.KeepDeletedItemsDays = daysKeepDeletedItems.ValueDays;
                plan.HideFromAddressBook = chkHideFromAddressBook.Checked;
                plan.AllowLitigationHold = chkEnableLitigationHold.Checked;
                plan.RecoverableItemsSpace = recoverableItemsSpace.QuotaValue;
                plan.RecoverableItemsWarningPct = recoverableItemsWarning.ValueKB;
                if ((plan.RecoverableItemsWarningPct == 0)) plan.RecoverableItemsWarningPct = 100;
                plan.LitigationHoldMsg = txtLitigationHoldMsg.Text.Trim();
                plan.LitigationHoldUrl = txtLitigationHoldUrl.Text.Trim();

                plan.EnableArchiving = chkEnableArchiving.Checked;

                plan.ArchiveSizeMB = archiveQuota.QuotaValue;
                plan.ArchiveWarningPct = archiveWarningQuota.ValueKB;
                if ((plan.ArchiveWarningPct == 0)) plan.ArchiveWarningPct = 100;

            }



            if (PanelSecurity.SelectedUser.Role == UserRole.Administrator)
                plan.MailboxPlanType = (int)ExchangeMailboxPlanType.Administrator;
            else
                if (PanelSecurity.SelectedUser.Role == UserRole.Reseller)
                    plan.MailboxPlanType = (int)ExchangeMailboxPlanType.Reseller;


            if ((orgs != null) & (orgs.GetLength(0) > 0))
            {
                int result = ES.Services.ExchangeServer.UpdateExchangeMailboxPlan(orgs[0].Id, plan);

                if (result < 0)
                {
                    messageBox.ShowErrorMessage("EXCHANGE_UPDATEPLANS");
                }
                else
                {
                    if (RetentionPolicy)
                    {
                        if (SaveTags(orgs[0].Id, mailboxPlanId))
                            messageBox.ShowSuccessMessage("EXCHANGE_UPDATEPLANS");
                    }
                    else
                        messageBox.ShowSuccessMessage("EXCHANGE_UPDATEPLANS");
                }

            }

            BindMailboxPlans();
        }
        protected void btnUpdateMailboxPlan_Click(object sender, EventArgs e)
        {
            Page.Validate(MainValidationGroup);

            if (!Page.IsValid)
            {
                return;
            }

            if (ViewState["MailboxPlanID"] == null)
            {
                return;
            }

            int mailboxPlanId = (int)ViewState["MailboxPlanID"];

            Providers.HostedSolution.Organization[]      orgs = GetOrganizations();
            Providers.HostedSolution.ExchangeMailboxPlan plan;

            plan = ES.Services.ExchangeServer.GetExchangeMailboxPlan(orgs[0].Id, mailboxPlanId);

            if (plan.ItemId != orgs[0].Id)
            {
                messageBox.ShowErrorMessage("EXCHANGE_UNABLE_USE_SYSTEMPLAN");
                BindMailboxPlans();
                return;
            }



            plan = new Providers.HostedSolution.ExchangeMailboxPlan();
            plan.MailboxPlanId = (int)ViewState["MailboxPlanID"];
            plan.MailboxPlan   = txtMailboxPlan.Text;
            plan.Archiving     = RetentionPolicy;

            if (RetentionPolicy)
            {
            }
            else
            {
                plan.MailboxSizeMB = mailboxSize.QuotaValue;

                plan.IsDefault               = false;
                plan.MaxRecipients           = maxRecipients.QuotaValue;
                plan.MaxSendMessageSizeKB    = maxSendMessageSizeKB.QuotaValue;
                plan.MaxReceiveMessageSizeKB = maxReceiveMessageSizeKB.QuotaValue;
                plan.EnablePOP               = chkPOP3.Checked;
                plan.EnableIMAP              = chkIMAP.Checked;
                plan.EnableOWA               = chkOWA.Checked;
                plan.EnableMAPI              = chkMAPI.Checked;
                plan.EnableActiveSync        = chkActiveSync.Checked;
                plan.IssueWarningPct         = sizeIssueWarning.ValueKB;
                if ((plan.IssueWarningPct == 0))
                {
                    plan.IssueWarningPct = 100;
                }
                plan.ProhibitSendPct = sizeProhibitSend.ValueKB;
                if ((plan.ProhibitSendPct == 0))
                {
                    plan.ProhibitSendPct = 100;
                }
                plan.ProhibitSendReceivePct = sizeProhibitSendReceive.ValueKB;
                if ((plan.ProhibitSendReceivePct == 0))
                {
                    plan.ProhibitSendReceivePct = 100;
                }
                plan.KeepDeletedItemsDays       = daysKeepDeletedItems.ValueDays;
                plan.HideFromAddressBook        = chkHideFromAddressBook.Checked;
                plan.AllowLitigationHold        = chkEnableLitigationHold.Checked;
                plan.RecoverableItemsSpace      = recoverableItemsSpace.QuotaValue;
                plan.RecoverableItemsWarningPct = recoverableItemsWarning.ValueKB;
                if ((plan.RecoverableItemsWarningPct == 0))
                {
                    plan.RecoverableItemsWarningPct = 100;
                }
                plan.LitigationHoldMsg = txtLitigationHoldMsg.Text.Trim();
                plan.LitigationHoldUrl = txtLitigationHoldUrl.Text.Trim();

                plan.EnableArchiving = chkEnableArchiving.Checked;

                plan.ArchiveSizeMB     = archiveQuota.QuotaValue;
                plan.ArchiveWarningPct = archiveWarningQuota.ValueKB;
                if ((plan.ArchiveWarningPct == 0))
                {
                    plan.ArchiveWarningPct = 100;
                }
            }



            if (PanelSecurity.SelectedUser.Role == UserRole.Administrator)
            {
                plan.MailboxPlanType = (int)ExchangeMailboxPlanType.Administrator;
            }
            else
            if (PanelSecurity.SelectedUser.Role == UserRole.Reseller)
            {
                plan.MailboxPlanType = (int)ExchangeMailboxPlanType.Reseller;
            }


            if ((orgs != null) & (orgs.GetLength(0) > 0))
            {
                int result = ES.Services.ExchangeServer.UpdateExchangeMailboxPlan(orgs[0].Id, plan);

                if (result < 0)
                {
                    messageBox.ShowErrorMessage("EXCHANGE_UPDATEPLANS");
                }
                else
                {
                    if (RetentionPolicy)
                    {
                        if (SaveTags(orgs[0].Id, mailboxPlanId))
                        {
                            messageBox.ShowSuccessMessage("EXCHANGE_UPDATEPLANS");
                        }
                    }
                    else
                    {
                        messageBox.ShowSuccessMessage("EXCHANGE_UPDATEPLANS");
                    }
                }
            }

            BindMailboxPlans();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PackageContext cntx = PackagesHelper.GetCachedPackageContext(PanelSecurity.PackageId);

                if (PanelRequest.GetInt("MailboxPlanId") != 0)
                {
                    Providers.HostedSolution.ExchangeMailboxPlan plan = ES.Services.ExchangeServer.GetExchangeMailboxPlan(PanelRequest.ItemID, PanelRequest.GetInt("MailboxPlanId"));
                    txtMailboxPlan.Text = plan.MailboxPlan;
                    RetentionPolicy     = plan.Archiving;

                    if (RetentionPolicy)
                    {
                        List <ExchangeMailboxPlanRetentionPolicyTag> tags = new List <ExchangeMailboxPlanRetentionPolicyTag>();
                        tags.AddRange(ES.Services.ExchangeServer.GetExchangeMailboxPlanRetentionPolicyTags(plan.MailboxPlanId));

                        ViewState["Tags"]   = tags;
                        gvPolicy.DataSource = tags;
                        gvPolicy.DataBind();
                    }
                    else
                    {
                        mailboxSize.QuotaValue             = plan.MailboxSizeMB;
                        maxRecipients.QuotaValue           = plan.MaxRecipients;
                        maxSendMessageSizeKB.QuotaValue    = plan.MaxSendMessageSizeKB;
                        maxReceiveMessageSizeKB.QuotaValue = plan.MaxReceiveMessageSizeKB;
                        chkPOP3.Checked                  = plan.EnablePOP;
                        chkIMAP.Checked                  = plan.EnableIMAP;
                        chkOWA.Checked                   = plan.EnableOWA;
                        chkMAPI.Checked                  = plan.EnableMAPI;
                        chkActiveSync.Checked            = plan.EnableActiveSync;
                        sizeIssueWarning.ValueKB         = plan.IssueWarningPct;
                        sizeProhibitSend.ValueKB         = plan.ProhibitSendPct;
                        sizeProhibitSendReceive.ValueKB  = plan.ProhibitSendReceivePct;
                        daysKeepDeletedItems.ValueDays   = plan.KeepDeletedItemsDays;
                        chkHideFromAddressBook.Checked   = plan.HideFromAddressBook;
                        chkEnableLitigationHold.Checked  = plan.AllowLitigationHold;
                        recoverableItemsSpace.QuotaValue = plan.RecoverableItemsSpace;
                        recoverableItemsWarning.ValueKB  = plan.RecoverableItemsWarningPct;
                        txtLitigationHoldMsg.Text        = plan.LitigationHoldMsg;
                        txtLitigationHoldUrl.Text        = plan.LitigationHoldUrl;

                        chkEnableArchiving.Checked            = plan.EnableArchiving;
                        archiveQuota.QuotaValue               = plan.ArchiveSizeMB;
                        archiveWarningQuota.ValueKB           = plan.ArchiveWarningPct;
                        chkEnableForceArchiveDeletion.Checked = plan.EnableForceArchiveDeletion;
                    }

                    locTitle.Text = plan.MailboxPlan;

                    this.DisableControls = true;
                }
                else
                {
                    if (cntx != null)
                    {
                        foreach (QuotaValueInfo quota in cntx.QuotasArray)
                        {
                            switch (quota.QuotaId)
                            {
                            case 77:
                                break;

                            case 365:
                                if (quota.QuotaAllocatedValue != -1)
                                {
                                    maxRecipients.QuotaValue = quota.QuotaAllocatedValue;
                                }
                                break;

                            case 366:
                                if (quota.QuotaAllocatedValue != -1)
                                {
                                    maxSendMessageSizeKB.QuotaValue = quota.QuotaAllocatedValue;
                                }
                                break;

                            case 367:
                                if (quota.QuotaAllocatedValue != -1)
                                {
                                    maxReceiveMessageSizeKB.QuotaValue = quota.QuotaAllocatedValue;
                                }
                                break;

                            case 83:
                                chkPOP3.Checked = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                chkPOP3.Enabled = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                break;

                            case 84:
                                chkIMAP.Checked = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                chkIMAP.Enabled = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                break;

                            case 85:
                                chkOWA.Checked = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                chkOWA.Enabled = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                break;

                            case 86:
                                chkMAPI.Checked = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                chkMAPI.Enabled = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                break;

                            case 87:
                                chkActiveSync.Checked = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                chkActiveSync.Enabled = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                break;

                            case 364:
                                daysKeepDeletedItems.ValueDays = quota.QuotaAllocatedValue;
                                daysKeepDeletedItems.RequireValidatorEnabled = true;
                                break;

                            case 420:
                                chkEnableLitigationHold.Checked = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                chkEnableLitigationHold.Enabled = Convert.ToBoolean(quota.QuotaAllocatedValue);
                                break;
                            }

                            sizeIssueWarning.ValueKB        = 95;
                            sizeProhibitSend.ValueKB        = 100;
                            sizeProhibitSendReceive.ValueKB = 100;
                            recoverableItemsWarning.ValueKB = 95;

                            RetentionPolicy = PanelRequest.GetBool("archiving", false);

                            if (!RetentionPolicy)
                            {
                                chkEnableArchiving.Checked  = true;
                                archiveQuota.QuotaValue     = cntx.Quotas[Quotas.EXCHANGE2013_ARCHIVINGSTORAGE].QuotaAllocatedValue;
                                archiveWarningQuota.ValueKB = 95;
                            }
                        }
                    }
                    else
                    {
                        this.DisableControls = true;
                    }
                }

                if (RetentionPolicy)
                {
                    UpdateTags();
                }

                locTitle.Text       = RetentionPolicy ? GetLocalizedString("locTitleArchiving.Text") : GetLocalizedString("locTitle.Text");
                secMailboxPlan.Text = RetentionPolicy ? GetLocalizedString("secMailboxPlanArchiving.Text") : GetLocalizedString("secMailboxPlan.Text");
                UserSpaceBreadcrumb bc = FindControlRecursive(Page, "breadcrumb") as UserSpaceBreadcrumb;
                if (bc != null)
                {
                    Label lbOrgCurPage = bc.FindControl("lbOrgCurPage") as Label;
                    if (lbOrgCurPage != null)
                    {
                        lbOrgCurPage.Text = locTitle.Text;
                    }
                }


                secMailboxFeatures.Visible = !RetentionPolicy;
                secMailboxGeneral.Visible  = !RetentionPolicy;
                secStorageQuotas.Visible   = !RetentionPolicy;
                secDeleteRetention.Visible = !RetentionPolicy;
                secLitigationHold.Visible  = !RetentionPolicy;

                secArchiving.Visible = !RetentionPolicy;

                secRetentionPolicyTags.Visible = RetentionPolicy;

                valRequireMailboxPlan.ValidationGroup = MainValidationGroup;
                btnAdd.ValidationGroup = MainValidationGroup;
            }
        }
        private void AddMailboxPlan()
        {
            try
            {
                Providers.HostedSolution.ExchangeMailboxPlan plan = new Providers.HostedSolution.ExchangeMailboxPlan();
                plan.MailboxPlan = txtMailboxPlan.Text;
                plan.Archiving   = RetentionPolicy;

                if (RetentionPolicy)
                {
                }
                else
                {
                    plan.MailboxSizeMB = mailboxSize.QuotaValue;

                    plan.IsDefault               = false;
                    plan.MaxRecipients           = maxRecipients.QuotaValue;
                    plan.MaxSendMessageSizeKB    = maxSendMessageSizeKB.QuotaValue;
                    plan.MaxReceiveMessageSizeKB = maxReceiveMessageSizeKB.QuotaValue;
                    plan.EnablePOP               = chkPOP3.Checked;
                    plan.EnableIMAP              = chkIMAP.Checked;
                    plan.EnableOWA               = chkOWA.Checked;
                    plan.EnableMAPI              = chkMAPI.Checked;
                    plan.EnableActiveSync        = chkActiveSync.Checked;
                    plan.IssueWarningPct         = sizeIssueWarning.ValueKB;
                    if ((plan.IssueWarningPct == 0))
                    {
                        plan.IssueWarningPct = 100;
                    }
                    plan.ProhibitSendPct = sizeProhibitSend.ValueKB;
                    if ((plan.ProhibitSendPct == 0))
                    {
                        plan.ProhibitSendPct = 100;
                    }
                    plan.ProhibitSendReceivePct = sizeProhibitSendReceive.ValueKB;
                    if ((plan.ProhibitSendReceivePct == 0))
                    {
                        plan.ProhibitSendReceivePct = 100;
                    }
                    plan.KeepDeletedItemsDays       = daysKeepDeletedItems.ValueDays;
                    plan.HideFromAddressBook        = chkHideFromAddressBook.Checked;
                    plan.AllowLitigationHold        = chkEnableLitigationHold.Checked;
                    plan.RecoverableItemsSpace      = recoverableItemsSpace.QuotaValue;
                    plan.RecoverableItemsWarningPct = recoverableItemsWarning.ValueKB;
                    if ((plan.RecoverableItemsWarningPct == 0))
                    {
                        plan.RecoverableItemsWarningPct = 100;
                    }
                    plan.LitigationHoldMsg = txtLitigationHoldMsg.Text.Trim();
                    plan.LitigationHoldUrl = txtLitigationHoldUrl.Text.Trim();

                    plan.EnableArchiving   = chkEnableArchiving.Checked;
                    plan.ArchiveSizeMB     = archiveQuota.QuotaValue;
                    plan.ArchiveWarningPct = archiveWarningQuota.ValueKB;
                    if ((plan.ArchiveWarningPct == 0))
                    {
                        plan.ArchiveWarningPct = 100;
                    }
                    plan.EnableForceArchiveDeletion = chkEnableForceArchiveDeletion.Checked;
                }

                int planId = ES.Services.ExchangeServer.AddExchangeMailboxPlan(PanelRequest.ItemID,
                                                                               plan);


                if (planId < 0)
                {
                    messageBox.ShowResultMessage(planId);
                    return;
                }

                List <ExchangeMailboxPlanRetentionPolicyTag> tags = ViewState["Tags"] as List <ExchangeMailboxPlanRetentionPolicyTag>;

                if (tags != null)
                {
                    foreach (ExchangeMailboxPlanRetentionPolicyTag tag in tags)
                    {
                        tag.MailboxPlanId = planId;
                        IntResult result = ES.Services.ExchangeServer.AddExchangeMailboxPlanRetentionPolicyTag(PanelRequest.ItemID, tag);
                        if (!result.IsSuccess)
                        {
                            messageBox.ShowMessage(result, "EXCHANGE_ADD_MAILBOXPLAN", null);
                            return;
                        }
                    }
                }


                Response.Redirect(EditUrl("ItemID", PanelRequest.ItemID.ToString(), RetentionPolicy ? "retentionpolicy" : "mailboxplans",
                                          "SpaceID=" + PanelSecurity.PackageId));
            }
            catch (Exception ex)
            {
                messageBox.ShowErrorMessage("EXCHANGE_ADD_MAILBOXPLAN", ex);
            }
        }
        private void BindTabs()
        {
            bool hideItems = false;

            UserInfo user = UsersHelper.GetUser(PanelSecurity.EffectiveUserId);

            PackageContext cntx = PackagesHelper.GetCachedPackageContext(PanelSecurity.PackageId);

            Providers.HostedSolution.ExchangeMailboxPlan plan = ES.Services.ExchangeServer.GetExchangeMailboxPlan(PanelRequest.ItemID, ES.Services.ExchangeServer.GetAccount(PanelRequest.ItemID, PanelRequest.AccountID).MailboxPlanId);

            if (user != null)
            {
                if ((user.Role == UserRole.User) & (Utils.CheckQouta(Quotas.EXCHANGE2007_ISCONSUMER, cntx)))
                {
                    hideItems = true;
                }
            }

            List <Tab> tabsList = new List <Tab>();

            tabsList.Add(CreateTab("edit_user", "Tab.General"));
            if (PanelRequest.Context == "JournalingMailbox")
            {
                tabsList.Add(CreateTab("journaling_mailbox_settings", "Tab.Settings"));
            }
            else
            {
                tabsList.Add(CreateTab("mailbox_settings", "Tab.Settings"));
            }
            if (!hideItems && PanelRequest.Context == "Mailbox")
            {
                tabsList.Add(CreateTab("mailbox_addresses", "Tab.Addresses"));
            }
            if (!hideItems && PanelRequest.Context == "Mailbox")
            {
                tabsList.Add(CreateTab("mailbox_mailflow", "Tab.Mailflow"));
            }
            if (!hideItems && PanelRequest.Context == "Mailbox")
            {
                tabsList.Add(CreateTab("mailbox_permissions", "Tab.Permissions"));
            }
            if (plan != null && plan.EnableAutoReply && PanelRequest.Context == "Mailbox")
            {
                tabsList.Add(CreateTab("mailbox_autoreply", "Tab.AutoReply"));
            }

            string instructions = ES.Services.ExchangeServer.GetMailboxSetupInstructions(PanelRequest.ItemID, PanelRequest.AccountID, false, false, false, " ");

            if (!string.IsNullOrEmpty(instructions) && PanelRequest.Context == "Mailbox")
            {
                tabsList.Add(CreateTab("mailbox_setup", "Tab.Setup"));
            }

            if (!hideItems && PanelRequest.Context == "Mailbox")
            {
                tabsList.Add(CreateTab("mailbox_mobile", "Tab.Mobile"));
            }
            //tabsList.Add(CreateTab("mailbddox_spam", "Tab.Spam"));

            if (Utils.CheckQouta(Quotas.EXCHANGE2007_DISTRIBUTIONLISTS, cntx))
            {
                tabsList.Add(CreateTab("mailbox_memberof", "Tab.MemberOf"));
            }

            // find selected menu item
            int idx = 0;

            foreach (Tab tab in tabsList)
            {
                if (String.Compare(tab.Id, SelectedTab, true) == 0)
                {
                    break;
                }
                idx++;
            }
            dlTabs.SelectedIndex = idx;

            dlTabs.DataSource = tabsList;
            dlTabs.DataBind();
        }
        private void AddMailboxPlan()
        {
            try
            {
                Providers.HostedSolution.ExchangeMailboxPlan plan = new Providers.HostedSolution.ExchangeMailboxPlan();
                plan.MailboxPlan = txtMailboxPlan.Text;
                plan.Archiving = RetentionPolicy;

                if (RetentionPolicy)
                {

                }
                else
                {
                    plan.MailboxSizeMB = mailboxSize.QuotaValue;

                    plan.IsDefault = false;
                    plan.MaxRecipients = maxRecipients.QuotaValue;
                    plan.MaxSendMessageSizeKB = maxSendMessageSizeKB.QuotaValue;
                    plan.MaxReceiveMessageSizeKB = maxReceiveMessageSizeKB.QuotaValue;
                    plan.EnablePOP = chkPOP3.Checked;
                    plan.EnableIMAP = chkIMAP.Checked;
                    plan.EnableOWA = chkOWA.Checked;
                    plan.EnableMAPI = chkMAPI.Checked;
                    plan.EnableActiveSync = chkActiveSync.Checked;
                    plan.IssueWarningPct = sizeIssueWarning.ValueKB;
                    if ((plan.IssueWarningPct == 0)) plan.IssueWarningPct = 100;
                    plan.ProhibitSendPct = sizeProhibitSend.ValueKB;
                    if ((plan.ProhibitSendPct == 0)) plan.ProhibitSendPct = 100;
                    plan.ProhibitSendReceivePct = sizeProhibitSendReceive.ValueKB;
                    if ((plan.ProhibitSendReceivePct == 0)) plan.ProhibitSendReceivePct = 100;
                    plan.KeepDeletedItemsDays = daysKeepDeletedItems.ValueDays;
                    plan.HideFromAddressBook = chkHideFromAddressBook.Checked;
                    plan.AllowLitigationHold = chkEnableLitigationHold.Checked;
                    plan.RecoverableItemsSpace = recoverableItemsSpace.QuotaValue;
                    plan.RecoverableItemsWarningPct = recoverableItemsWarning.ValueKB;
                    if ((plan.RecoverableItemsWarningPct == 0)) plan.RecoverableItemsWarningPct = 100;
                    plan.LitigationHoldMsg = txtLitigationHoldMsg.Text.Trim();
                    plan.LitigationHoldUrl = txtLitigationHoldUrl.Text.Trim();

                    plan.EnableArchiving = chkEnableArchiving.Checked;
                    plan.ArchiveSizeMB = archiveQuota.QuotaValue;
                    plan.ArchiveWarningPct = archiveWarningQuota.ValueKB;
                    if ((plan.ArchiveWarningPct == 0))
                    {
                        plan.ArchiveWarningPct = 100;
                    }
                    plan.EnableForceArchiveDeletion = chkEnableForceArchiveDeletion.Checked;
                }

                int planId = ES.Services.ExchangeServer.AddExchangeMailboxPlan(PanelRequest.ItemID,
                                                                                plan);


                if (planId < 0)
                {
                    messageBox.ShowResultMessage(planId);
                    return;
                }

                List<ExchangeMailboxPlanRetentionPolicyTag> tags = ViewState["Tags"] as List<ExchangeMailboxPlanRetentionPolicyTag>;

                if (tags!=null)
                {
                    foreach(ExchangeMailboxPlanRetentionPolicyTag tag in tags)
                    {
                        tag.MailboxPlanId = planId;
                        IntResult result = ES.Services.ExchangeServer.AddExchangeMailboxPlanRetentionPolicyTag(PanelRequest.ItemID, tag);
                        if (!result.IsSuccess)
                        {
                            messageBox.ShowMessage(result, "EXCHANGE_ADD_MAILBOXPLAN", null);
                            return;
                        }
                    }
                }


                Response.Redirect(EditUrl("ItemID", PanelRequest.ItemID.ToString(), RetentionPolicy ? "retentionpolicy" : "mailboxplans",
                    "SpaceID=" + PanelSecurity.PackageId));
            }
            catch (Exception ex)
            {
                messageBox.ShowErrorMessage("EXCHANGE_ADD_MAILBOXPLAN", ex);
            }
        }
        public void btnAddMailboxPlan_Click(object sender, EventArgs e)
        {
            if (!RetentionPolicy)
            {
                Page.Validate(MainValidationGroup);
            }

            if (!Page.IsValid)
            {
                return;
            }

            Providers.HostedSolution.ExchangeMailboxPlan plan = new Providers.HostedSolution.ExchangeMailboxPlan();
            plan.MailboxPlan = txtMailboxPlan.Text;
            plan.Archiving   = RetentionPolicy;

            if (RetentionPolicy)
            {
            }
            else
            {
                plan.MailboxSizeMB = mailboxSize.QuotaValue;

                plan.IsDefault               = false;
                plan.MaxRecipients           = maxRecipients.QuotaValue;
                plan.MaxSendMessageSizeKB    = maxSendMessageSizeKB.QuotaValue;
                plan.MaxReceiveMessageSizeKB = maxReceiveMessageSizeKB.QuotaValue;
                plan.EnablePOP               = chkPOP3.Checked;
                plan.EnableIMAP              = chkIMAP.Checked;
                plan.EnableOWA               = chkOWA.Checked;
                plan.EnableMAPI              = chkMAPI.Checked;
                plan.EnableActiveSync        = chkActiveSync.Checked;
                plan.EnableAutoReply         = chkAutoReplyEnabled.Checked;
                plan.IssueWarningPct         = sizeIssueWarning.ValueKB;
                if ((plan.IssueWarningPct == 0))
                {
                    plan.IssueWarningPct = 100;
                }
                plan.ProhibitSendPct = sizeProhibitSend.ValueKB;
                if ((plan.ProhibitSendPct == 0))
                {
                    plan.ProhibitSendPct = 100;
                }
                plan.ProhibitSendReceivePct = sizeProhibitSendReceive.ValueKB;
                if ((plan.ProhibitSendReceivePct == 0))
                {
                    plan.ProhibitSendReceivePct = 100;
                }
                plan.KeepDeletedItemsDays       = daysKeepDeletedItems.ValueDays;
                plan.HideFromAddressBook        = chkHideFromAddressBook.Checked;
                plan.AllowLitigationHold        = chkEnableLitigationHold.Checked;
                plan.RecoverableItemsSpace      = recoverableItemsSpace.QuotaValue;
                plan.RecoverableItemsWarningPct = recoverableItemsWarning.ValueKB;
                if ((plan.RecoverableItemsWarningPct == 0))
                {
                    plan.RecoverableItemsWarningPct = 100;
                }
                plan.LitigationHoldMsg = txtLitigationHoldMsg.Text.Trim();
                plan.LitigationHoldUrl = txtLitigationHoldUrl.Text.Trim();

                plan.EnableArchiving = chkEnableArchiving.Checked;

                plan.ArchiveSizeMB     = archiveQuota.QuotaValue;
                plan.ArchiveWarningPct = archiveWarningQuota.ValueKB;
                if ((plan.ArchiveWarningPct == 0))
                {
                    plan.ArchiveWarningPct = 100;
                }
                plan.IsForJournaling = chkIsForJournaling.Checked;
            }

            if (PanelSecurity.SelectedUser.Role == UserRole.Administrator)
            {
                plan.MailboxPlanType = (int)ExchangeMailboxPlanType.Administrator;
            }
            else
            if (PanelSecurity.SelectedUser.Role == UserRole.Reseller)
            {
                plan.MailboxPlanType = (int)ExchangeMailboxPlanType.Reseller;
            }

            Providers.HostedSolution.Organization[] orgs = GetOrganizations();


            if ((orgs != null) & (orgs.GetLength(0) > 0))
            {
                int planId = ES.Services.ExchangeServer.AddExchangeMailboxPlan(orgs[0].Id, plan);

                if (planId < 0)
                {
                    messageBox.ShowResultMessage(planId);
                    return;
                }

                if (RetentionPolicy)
                {
                    SaveTags(orgs[0].Id, planId);
                }
            }

            BindMailboxPlans();
        }