/// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdatePlan_Click(object sender, EventArgs e)
        {
            BaseActivesyncPolicy policy = new BaseActivesyncPolicy();

            ExchCmds powershell = null;
            try
            {
                // Fill in custom object
                policy.CompanyCode = "";
                policy.DisplayName = txtDisplayName.Text;
                policy.Description = txtDescription.Text;
                policy.AllowNonProvisionableDevice = cbAllowNonProvisionableDevices.Checked;

                if (!string.IsNullOrEmpty(txtRefreshInterval.Text))
                    policy.DevicePolicyRefreshInterval = int.Parse(txtRefreshInterval.Text);
                else
                    policy.DevicePolicyRefreshInterval = 0;

                //
                // PASSWORD TAB
                //
                policy.DevicePasswordEnabled = cbRequirePassword.Checked;
                if (policy.DevicePasswordEnabled)
                {
                    policy.AlphanumericDevicePasswordRequired = cbRequireAlphaNumericPassword.Checked;

                    if (!string.IsNullOrEmpty(txtMinimumNumberOfCharacterSets.Text))
                        policy.MinDevicePasswordComplexCharacters = int.Parse(txtMinimumNumberOfCharacterSets.Text);
                    else
                        policy.MinDevicePasswordComplexCharacters = 0;

                    policy.PasswordRecoveryEnabled = cbEnablePasswordRecovery.Checked;
                    policy.RequireDeviceEncryption = cbRequireEncryption.Checked;
                    policy.RequireStorageCardEncryption = cbRequireEncryptionOnStorageCard.Checked;
                    policy.AllowSimpleDevicePassword = cbAllowSimplePassword.Checked;

                    if (!string.IsNullOrEmpty(txtNumberOfFailedAttemptsAllowed.Text))
                        policy.MaxDevicePasswordFailedAttempts = int.Parse(txtNumberOfFailedAttemptsAllowed.Text);
                    else
                        policy.MaxDevicePasswordFailedAttempts = 0;

                    if (!string.IsNullOrEmpty(txtMinimumPasswordLength.Text))
                        policy.MinDevicePasswordLength = int.Parse(txtMinimumPasswordLength.Text);
                    else
                        policy.MinDevicePasswordLength = 0;

                    if (!string.IsNullOrEmpty(txtInactivityTimeout.Text))
                        policy.MaxInactivityTimeDeviceLock = int.Parse(txtInactivityTimeout.Text);
                    else
                        policy.MaxInactivityTimeDeviceLock = 0;

                    if (!string.IsNullOrEmpty(txtPasswordExpiration.Text))
                        policy.DevicePasswordExpiration = int.Parse(txtPasswordExpiration.Text);
                    else
                        policy.DevicePasswordExpiration = 0;

                    if (!string.IsNullOrEmpty(txtEnforcePasswordHistory.Text))
                        policy.DevicePasswordHistory = int.Parse(txtEnforcePasswordHistory.Text);
                    else
                        policy.DevicePasswordHistory = 0;
                }


                //
                // SYNC SETTINGS
                //
                policy.MaxCalendarAgeFilter = ddlPastCalendarItems.SelectedValue;
                policy.MaxEmailAgeFilter = ddlPastEmailItems.SelectedValue;

                if (!string.IsNullOrEmpty(txtLimitEmailSize.Text))
                    policy.MaxEmailBodyTruncationSize = int.Parse(txtLimitEmailSize.Text);
                else
                    policy.MaxEmailBodyTruncationSize = 0;

                policy.RequireManualSyncWhenRoaming = cbAllowDirectPushWhenRoaming.Checked;
                policy.AllowHTMLEmail = cbAllowHTMLEmail.Checked;
                policy.AttachmentsEnabled = cbAllowAttachmentDownload.Checked;

                if (policy.AttachmentsEnabled)
                {
                    if (!string.IsNullOrEmpty(txtMaximumAttachmentSize.Text))
                        policy.MaxAttachmentSize = int.Parse(txtMaximumAttachmentSize.Text);
                    else
                        policy.MaxAttachmentSize = 0;
                }

                //
                // DEVICE
                //
                policy.AllowStorageCard = cbAllowRemovableStorage.Checked;
                policy.AllowCamera = cbAllowCamera.Checked;
                policy.AllowWiFi = cbAllowWiFi.Checked;
                policy.AllowIrDA = cbAllowInfrared.Checked;
                policy.AllowInternetSharing = cbAllowInternetSharing.Checked;
                policy.AllowRemoteDesktop = cbAllowRemoteDesktop.Checked;
                policy.AllowDesktopSync = cbAllowDesktopSync.Checked;
                policy.AllowBluetooth = ddlAllowBluetooth.SelectedValue;
                policy.AllowTextMessaging = cbAllowTextMessaging.Checked;

                //
                // DEVICE APPLICATIONS
                //
                policy.AllowBrowser = cbAllowBrowser.Checked;
                policy.AllowConsumerEmail = cbAllowConsumerMail.Checked;
                policy.AllowUnsignedApplications = cbAllowUnsignedApplications.Checked;
                policy.AllowUnsignedInstallationPackages = cbAllowUnsignedInstallPackages.Checked;
                


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

                // Check if we are creating a new policy or updating existing
                if (ddlActiveSyncPlan.SelectedIndex > 0)
                {
                    policy.ASID = int.Parse(ddlActiveSyncPlan.SelectedValue);

                    // Update Exchange
                    powershell.Update_ActiveSyncPolicy(policy, Config.ExchangeVersion);

                    // Update SQL database
                    SQLExchange.UpdateExchangeActiveSyncPolicy(policy);

                    // Update notification
                    notification1.SetMessage(controls.notification.MessageType.Success, Resources.LocalizedText.NotificationSuccessUpdateAS + policy.DisplayName);
                }
                else
                {
                    // Create new policy
                    powershell.New_ActiveSyncPolicy(policy, Config.ExchangeVersion);

                    // Add to SQL database
                    SQLExchange.AddExchangeActiveSyncPolicy(policy);

                    // Update notification
                    notification1.SetMessage(controls.notification.MessageType.Success, Resources.LocalizedText.NotificationSuccessCreatedAS + policy.DisplayName);
                }

            }
            catch (Exception ex)
            {
                notification1.SetMessage(controls.notification.MessageType.Error, ex.Message);
            }
            finally
            {
                if (powershell != null)
                    powershell.Dispose();

                // Refresh view
                GetPolicies();
            }
        }
        private void CreateNewPolicy()
        {
            ExchCmds powershell = null;

            try
            {
                BaseActivesyncPolicy policy = new BaseActivesyncPolicy();

                // Compile list of general settings
                policy.DisplayName = txtDisplayName.Text;
                policy.Description = txtDescription.Text;

                // Compile list of basic settings
                policy.AllowBluetooth = ddlAllowBluetooth.SelectedValue;
                policy.AllowBrowser = cbAllowBrowser.Checked;
                policy.AllowCamera = cbAllowCamera.Checked;
                policy.AllowConsumerEmail = cbAllowConsumerMail.Checked;
                policy.AllowDesktopSync = cbAllowDesktopSync.Checked;
                policy.AllowInternetSharing = cbAllowInternetSharing.Checked;
                policy.AllowSimpleDevicePassword = cbAllowSimplePassword.Checked;
                policy.AllowTextMessaging = cbAllowTextMessaging.Checked;
                policy.AllowWiFi = cbAllowWIFI.Checked;
                policy.DevicePasswordEnabled = cbPasswordEnabled.Checked;
                policy.AlphanumericDevicePasswordRequired = cbAlphanumericPwdRequired.Checked;
                policy.MaxDevicePasswordFailedAttempts = ddlMaxFailedPasswordAttempts.SelectedValue;
                policy.AllowHTMLEmail = cbAllowHTMLEmail.Checked;
                policy.AllowIrDA = cbAllowInfrared.Checked;
                policy.AllowNonProvisionableDevice = cbAllowNonProvisionable.Checked;
                policy.AllowPOPIMAPEmail = cbAllowPOPIMAP.Checked;
                policy.AllowRemoteDesktop = cbAllowRemoteDesktop.Checked;
                policy.AllowSMIMEEncryptionAlgorithmNegotiation = ddlAllowSMIMEEncryptionAlgorithmNeg.SelectedValue;
                policy.AllowSMIMESoftCerts = cbAllowSMIME.Checked;
                policy.AllowStorageCard = cbAllowStorageCard.Checked;
                policy.AllowUnsignedApplications = cbAllowUnsignedApps.Checked;
                policy.AllowUnsignedInstallationPackages = cbAllowUnsignedInstallPackages.Checked;
                policy.AttachmentsEnabled = cbAttachmentsEnabled.Checked;
                policy.DeviceEncryptionEnabled = cbDeviceEncryptionEnabled.Checked;
                policy.DevicePasswordExpiration = txtPasswordExpiration.Text;
                policy.DevicePolicyRefreshInterval = txtPolicyRefreshInterval.Text;
                policy.MaxAttachmentSize = txtMaxAttachmentSize.Text;
                policy.MaxCalendarAgeFilter = ddlMaxCalendarAgeFilter.SelectedValue;
                policy.MaxEmailAgeFilter = ddlMaxEmailAgeFilter.SelectedValue;
                policy.PasswordRecoveryEnabled = cbPasswordRecovery.Checked;
                policy.RequireDeviceEncryption = cbRequireDeviceEncryption.Checked;
                policy.RequireEncryptedSMIMEMessages = cbRequireEncryptedSMIMEMsg.Checked;
                policy.RequireEncryptionSMIMEAlgorithm = ddlRequireEncryptedSMIMEAlgorithm.SelectedValue;
                policy.RequireSignedSMIMEMessages = cbRequireSignedSMIMEMsg.Checked;
                policy.RequireSignedSMIMEAlgorithm = ddlRequireSignedSMIMEMsg.SelectedValue;
                policy.RequireManualSyncWhenRoaming = cbRequireManualSyncRoaming.Checked;
                policy.RequireStorageCardEncryption = cbRequireStorageCardEncryption.Checked;

                policy.MaxEmailHTMLBodyTruncationSize = txtMaxHTMLBodyTruncSize.Text;
                policy.MaxEmailBodyTruncationSize = txtMaxEmailBodyTruncSize.Text;

                int minComplexChar = 0;
                int.TryParse(txtMinPwdComplexChar.Text, out minComplexChar);
                policy.MinDevicePasswordComplexCharacters = minComplexChar;
                
                int inactivityLock = 0;
                int.TryParse(txtMaxInactivityLock.Text, out inactivityLock);
                policy.MaxInactivityTimeDeviceLock = inactivityLock;

                int passwordHistory = 0;
                int.TryParse(txtPasswordHistory.Text, out passwordHistory);
                policy.DevicePasswordHistory = passwordHistory;

                int minPwdLength = 0;
                int.TryParse(txtMinPwdLength.Text, out minPwdLength);
                policy.MinDevicePasswordLength = minPwdLength;

                // Exchange 2013
                policy.AllowApplePushNotifications = cbAllowApplePushNotifications.Checked;

                // Initilaze powershell
                powershell = new ExchCmds(Retrieve.ExchangeUri, Retrieve.Username, Retrieve.Password, Retrieve.ExchangeKerberos, Retrieve.PrimaryDC);

                // Insert into Exchange
                powershell.New_ActiveSyncPolicy(policy, CPContext.SelectedCompanyCode, Retrieve.ExchangeVersion);

                // Insert into SQL
                SQLExchange.AddExchangeActiveSyncPolicy(CPContext.SelectedCompanyCode, txtDisplayName.Text, txtDescription.Text);

                // Notify
                notification1.SetMessage(controls.notification.MessageType.Success, "Successfully added new activesync policy");
            }
            catch (Exception ex)
            {
                notification1.SetMessage(controls.notification.MessageType.Error, "Error creating activesync policy: " + ex.Message);
            }
            finally
            {
                if (powershell != null)
                    powershell.Dispose();

                // Refresh view
                GetPolicies();
            }
        }