/// <summary>
        /// Enables public folders for a company
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEnablePublicFolders_Click(object sender, EventArgs e)
        {
            ADGroups groups = null;
            ExchCmds powershell = null;

            try
            {
                // Initialize
                groups = new ADGroups(Config.Username, Config.Password, Config.PrimaryDC);
                powershell = new ExchCmds(Config.ExchangeURI, Config.Username, Config.Password, Config.ExchangeConnectionType, Config.PrimaryDC);

                // Get company code
                string companyCodeWithSpaces = CPContext.SelectedCompanyCode;
                string companyCodeWithoutSpaces = companyCodeWithSpaces.Replace(" ", string.Empty);

                // Create groups
                groups.Create(Retrieve.GetCompanyExchangeOU, "PublicFolderAdmins@" + companyCodeWithoutSpaces, "Public Folder Administrators", true, ADGroups.GroupType.Universal, false);
                groups.Create(Retrieve.GetCompanyExchangeOU, "PublicFolderUsers@" + companyCodeWithoutSpaces, "Public Folder Users", true, ADGroups.GroupType.Universal, false);

                // Modify membership
                groups.ModifyMembership("PublicFolderAdmins@" + companyCodeWithoutSpaces, "Admins@" + companyCodeWithoutSpaces, System.DirectoryServices.AccountManagement.IdentityType.Name,
                     System.DirectoryServices.AccountManagement.IdentityType.Name, false, false);
                groups.ModifyMembership("PublicFolderUsers@" + companyCodeWithoutSpaces, "AllUsers@" + companyCodeWithoutSpaces, System.DirectoryServices.AccountManagement.IdentityType.Name,
                     System.DirectoryServices.AccountManagement.IdentityType.Name, false, false);

                // Enable security groups as distribution groups so we can add to public folders
                powershell.Enable_DistributionGroup(companyCodeWithoutSpaces, "PublicFolderAdmins@" + companyCodeWithoutSpaces, true);
                powershell.Enable_DistributionGroup(companyCodeWithoutSpaces, "PublicFolderUsers@" + companyCodeWithoutSpaces, true);

                // Create public folder
                powershell.New_PublicFolder(companyCodeWithSpaces);

                // Remove the default permissions
                powershell.Remove_PublicFolderDefaultPermissions(companyCodeWithSpaces, Config.ExchangeVersion);

                // Add permissions
                powershell.Add_PublicFolderClientPermission(companyCodeWithSpaces);

                // Update the database
                SQLPublicFolders.Update_PublicFolderForCompany(companyCodeWithSpaces, true);

                // Set new view
                panelDisablePublicFolders.Visible = false;
                panelEnablePublicFolders.Visible = false;
                panelEditPublicFolders.Visible = true;

                // Get public folders
                GetPublicFolders();
            }
            catch (Exception ex)
            {
                notification1.SetMessage(controls.notification.MessageType.Error, "Could not enable public folders: " + ex.Message);
            }
            finally
            {
                if (powershell != null)
                    powershell.Dispose();

                if (groups != null)
                    groups.Dispose();
            }
        }
        private void AddCompany()
        {
            ADOrgUnit org = null;
            ADGroups groups = null;

            try
            {
                // Distinguished name the user put in
                string dn = txtDistinguishedName.Text.Trim();

                // Initialize our Active Directory connection object
                org = new ADOrgUnit(Config.Username, Config.Password, Config.PrimaryDC);
                groups = new ADGroups(Config.Username, Config.Password, Config.PrimaryDC);

                Company company = org.GetOU(dn);
                company.ResellerCode = CPContext.SelectedResellerCode;
                company.CompanyCode = company.CompanyName;
                company.Street = "";
                company.City = "";
                company.State = "";
                company.Country = "";
                company.ZipCode = "";
                company.PhoneNumber = "";
                company.Website = "";
                company.Description = "";
                company.AdministratorsName = "";
                company.AdministratorsEmail = "";

                // Check that some of the domains are not already in use
                if (company.Domains != null)
                {
                    foreach (string domain in company.Domains)
                    {
                        if (SQLDomains.DoesDomainExist(domain))
                            throw new Exception(Resources.LocalizedText.ImportCompanyError1 + domain);
                    }
                }

                // Store the company code with no whitespaces
                // We don't want to end up creating security groups with whitespaces in them
                string companyCodeWithRemovedWhitespace = company.CompanyCode.Replace(" ", string.Empty);

                // Make sure our other OU's exist
                org.CreateOU(company.DistinguishedName, "Exchange");
                org.CreateOU(company.DistinguishedName, "Applications");

                // Check for the users OU if specified
                if (!string.IsNullOrEmpty(Config.UsersOU))
                    org.CreateOU(company.DistinguishedName, Config.UsersOU);

                // Make sure our groups exists
                groups.Create(company.DistinguishedName, "Admins@" + companyCodeWithRemovedWhitespace, "", true, ADGroups.GroupType.Global, false);
                groups.Create(company.DistinguishedName, "AllTSUsers@" + companyCodeWithRemovedWhitespace, "", true, ADGroups.GroupType.Global, false);
                groups.Create(company.DistinguishedName, "AllUsers@" + companyCodeWithRemovedWhitespace, "", true, ADGroups.GroupType.Global, false);

                // Add the new AllTSUsers group for this company to the parent group
                groups.ModifyMembership("AllTSUsers@Hosting", "AllTSUsers@" + companyCodeWithRemovedWhitespace, System.DirectoryServices.AccountManagement.IdentityType.Name, System.DirectoryServices.AccountManagement.IdentityType.Name, false);

                // Remove Authenticated User rights
                org.RemoveAuthUsersRights(company.DistinguishedName);
                org.RemoveAuthUsersRights("OU=Exchange," + company.DistinguishedName);
                org.RemoveAuthUsersRights("OU=Applications," + company.DistinguishedName);

                // Add rights We want to strip the
                org.AddReadRights(company.DistinguishedName, "AllUsers@" + companyCodeWithRemovedWhitespace);
                org.AddReadRights("OU=Exchange," + company.DistinguishedName, "AllUsers@" + companyCodeWithRemovedWhitespace);
                org.AddReadRights("OU=Applications," + company.DistinguishedName, "AllUsers@" + companyCodeWithRemovedWhitespace);

                // Good! No Errors so lets insert this into SQL
                SQLCompanies.AddCompany(company);

                // Now lets add the domains
                if (company.Domains != null)
                {
                    foreach (string domain in company.Domains)
                    {
                        SQLDomains.AddDomain(company.CompanyCode, domain.Trim());
                    }
                }

                // Redirect to companies
                Response.Redirect("~/companies.aspx", false);
            }
            catch (Exception ex)
            {
                notification1.SetMessage(controls.notification.MessageType.Error, ex.ToString());
            }
            finally
            {
                if (groups != null)
                    groups.Dispose();

                if (org != null)
                    org.Dispose();
            }
        }
        /// <summary>
        /// Starts the import process
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnImportSave_Click(object sender, EventArgs e)
        {
            ADUsers adUsers = null;
            ADGroups adGroups = null;

            try
            {
                string companyCode = CPContext.SelectedCompanyCode; // Get the current company code that is selected

                string[] selectedUsers = GetSelectedUsers();
                if (selectedUsers != null && selectedUsers.Length > 0)
                {
                    // Check the limits
                    if (CheckUserLimit(selectedUsers.Length))
                        throw new Exception("You have selected too many users and it would have caused you to go over the user limit set for your company. Please select fewer users and try again.");

                    // Check mailbox limits if checkbox is checked
                    if (cbUsersAlreadyEnabledExchange.Checked && CheckMailboxLimit(selectedUsers.Length))
                        throw new Exception("You have selected too many users that have mailboxes and it would have caused you to go over the mailbox limit set for your company. Please select fewer users and try again.");

                    // Initialize our Active Directory object
                    adUsers = new ADUsers(Config.Username, Config.Password, Config.PrimaryDC);
                    adGroups = new ADGroups(Config.Username, Config.Password, Config.PrimaryDC);

                    // Pull detailed information from ActiveDirectory on the selected users
                    List<ADUser> adUsersInfo = adUsers.GetAllUsers(Retrieve.GetCompanyOU, selectedUsers.ToList(), true);

                    // Validate the domains for the users login matches a domain in this company
                    ValidateDomains(adUsersInfo);

                    // Now insert into SQL
                    foreach (ADUser u in adUsersInfo)
                    {
                        // Set the company code of the user because it won't have it
                        u.CompanyCode = companyCode;

                        // Make sure the user belongs to the AllUsers@ group
                        adGroups.ModifyMembership("AllUsers@" + companyCode.Replace(" ", string.Empty), u.UserPrincipalName, 
                            System.DirectoryServices.AccountManagement.IdentityType.Name, System.DirectoryServices.AccountManagement.IdentityType.UserPrincipalName, 
                            false, false);

                        SQLUsers.AddUser(u);
                    }

                    // Now check if we are doing mailboxes
                    if (cbUsersAlreadyEnabledExchange.Checked)
                    {
                        ImportExchangeUsers(adUsersInfo);
                    }
                }

                // Update notification
                notification1.SetMessage(controls.notification.MessageType.Success, "Successfully imported users.");
            }
            catch (Exception ex)
            {
                notification1.SetMessage(controls.notification.MessageType.Error, "Error importing users: " + ex.Message);
            }
            finally
            {
                if (adGroups != null)
                    adGroups.Dispose();

                if (adUsers != null)
                    adUsers.Dispose();
            }

            // Retrieve a new list of useres
            GetAllMissingUsers();
        }