Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
'Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
'Imports CharterDotCom.CommonEntities.Library.EntityG2BServices
Imports CharterDotCom.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library

Public Class Controls_ManageEmailAccount
	Inherits System.Web.UI.UserControl

	Public Event Saved()
	Public Event Cancelled()

	Public Property AccountID() As String
		Get
			If ViewState(Me.ID & "_AccountID") Is Nothing Then
				Return String.Empty
			Else
				Return CType(ViewState(Me.ID & "_AccountID"), String)
			End If
		End Get

		Set(ByVal value As String)
			ViewState(Me.ID & "_AccountID") = value
		End Set
	End Property

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
            vldAddressRequired.ValidationGroup = Me.ID
            vldUserNameSpecialChar.ValidationGroup = Me.ID
            vldPasswordSpecialChar.ValidationGroup = Me.ID
            vldPassword2SpecialChar.ValidationGroup = Me.ID
			vldPasswordRequired.ValidationGroup = Me.ID
			vldPassword2Required.ValidationGroup = Me.ID
			vldPassword2Compare.ValidationGroup = Me.ID
			btnSave.ValidationGroup = Me.ID
		End If

		If AccountID = String.Empty Then
			lblTitle.Text = "Create New Email Account"
			pnlAddress.Visible = True
		Else
			lblTitle.Text = "Edit Password"
			pnlAddress.Visible = False
		End If
	End Sub

	Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
		vldCustom.IsValid = True

		If Page.IsValid Then
			
			If AccountID = String.Empty Then
                If AccountManager.Instance.AddEmailAccount(String.Format("{0}@charter.net", txtAddress.Text.ToLower()), txtPassword.Text, "") Then
                    RaiseEvent Saved()
                Else
                    vldCustom.ErrorMessage = "Your new email address cannot be created. Please try again with a different address."
                    vldCustom.IsValid = False
                End If
			Else
                If AccountManager.Instance.ChangeAccountEmailPassword(AccountID.ToLower(), txtPassword.Text) Then
                    RaiseEvent Saved()
                Else
                    vldCustom.ErrorMessage = "Your new password cannot be saved. Please try again with a different password."
                    vldCustom.IsValid = False
                End If
			End If
		End If
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
		RaiseEvent Cancelled()
	End Sub
End Class
