Imports System.Data.SqlClient
Public Class ChatLogin
        Inherits System.Web.UI.Page
        Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
        Protected WithEvents btSignIn As System.Web.UI.WebControls.Button
        Protected WithEvents txtValidFirstName As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtValidSignin As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtUserNameNew As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtPassWordNew As System.Web.UI.WebControls.TextBox
        Protected WithEvents Textbox3 As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtFirstNameNew As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtLastNameNew As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtScreenNameNew As System.Web.UI.WebControls.TextBox
        Protected WithEvents btnAddNew As System.Web.UI.WebControls.Button
        Protected WithEvents Textbox1 As System.Web.UI.WebControls.TextBox
        Protected WithEvents lblError As System.Web.UI.WebControls.Label
        Protected WithEvents lblWelcome As System.Web.UI.WebControls.Label
        Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
        Dim objChatBLL As New Chat_BLL()

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
                'CODEGEN: This method call is required by the Web Form Designer
                'Do not modify it using the code editor.
                InitializeComponent()
        End Sub

#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                'Put user code to initialize the page here
        End Sub

        Private Sub btSignIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSignIn.Click

                Dim strUserName As String = txtUsername.Text
                Dim strPassWord As String = txtPassword.Text
                Dim intUserID As Integer

                If Trim(strUserName) = "" Then
                        lblError.Text = "UserName is a required field"
                ElseIf Trim(strPassWord) = "" Then
                        lblError.Text = "PassWord is a required field"
                Else

                        Dim rdr As SqlDataReader = objChatBLL.LoginChatUser(strUserName, strPassWord)
                        If rdr.Read Then
                                Session("CurrentUser") = CStr(rdr("UserID"))
                                Session("FirstName") = rdr("FirstName")
                                Session("LastName") = rdr("Lastname")
                                Session("ScreenName") = rdr("ScreenName")
                                Response.Redirect("ChatWelcome.aspx")
                        Else
                                lblError.Text = "UserName/Password was not found in the database.  If this is your first time chatting, please sign in  under the section labeled 'New to Chatty Ways?'"
                        End If
                End If

        End Sub

        Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
                Dim strUserName As String = txtUserNameNew.Text
                Dim strPassWord As String = txtPassWordNew.Text
                Dim strFirstName As String = txtFirstNameNew.Text
                Dim strLastname As String = txtLastNameNew.Text
                Dim strScreenName As String = txtScreenNameNew.Text
                Dim intUserID As Integer

                If Trim(strUserName) = "" Then
                        lblError.Text = "UserName is a required field"
                ElseIf Trim(strFirstName) = "" Then
                        lblError.Text = "FirstName is a required field"
                ElseIf Trim(strLastname) = "" Then
                        lblError.Text = "LastName is a required field"
                ElseIf Trim(strScreenName) = "" Then
                        lblError.Text = "ScreenName is a required field"

                Else

                        intUserID = objChatBLL.AddChatUser(strUserName, strPassWord, strFirstName, strLastname, strScreenName)
                        Select Case intUserID
                                Case -1
                                        lblError.Text = "UserName/Password already exists in the database.  Please select a different UserName/PassWord and try again."
                                Case -2
                                        lblError.Text = "ScreenName already exists in the database.  Please select a different Screen Name and try again"
                                Case Else
                                        'success
                                        Session("CurrentUser") = CStr(intUserID)
                                        Session("FirstName") = strFirstName
                                        Session("LastName") = strLastname
                                        Session("ScreenName") = strScreenName
                                        Response.Redirect("ChatWelcome.aspx")

                        End Select
                End If

        End Sub
End Class
