Public Class CreateAccount2
    Inherits System.Web.UI.Page

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("Default.aspx")
    End Sub

    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
        If Not Page.IsValid Then
            Exit Sub
        End If

        If String.IsNullOrWhiteSpace(txtEmail.Text & txtPassword.Text & txtNameFirst.Text & txtNameLast.Text & txtPhone.Text) Then
            phIncomplete.Visible = True
            Exit Sub
        End If

        Try

            Dim account As New Logic.DriverAccount()
            With account
                .email = txtEmail.Text
                .phone = txtPhone.Text
                .nameFirst = txtNameFirst.Text
                .nameMiddle = txtNameMiddle.Text
                .nameLast = txtNameLast.Text
                .nameSuffix = txtNameSuffix.Text
            End With
            account.Create()
            account.ChangePassword(txtPassword.Text)
            SessionVars.DriverId(Session) = account.id


            If SessionVars.AppClientId(Session) > 0 Then
                Response.Redirect("/Driver2/StartApplication2.aspx?Clientid=" & SessionVars.AppClientId(Session), False)
            Else
                Response.Redirect("/Driver2/Dashboard2.aspx", False)
            End If
            'Response.Redirect("Dashboard2.aspx", False)

        Catch ex As InvalidOperationException
            If ex.Message = "The user already exists." Then
                phUserExists.Visible = True
            Else
                Dim errorId = Logic.Error.Log(ex)
                Response.Redirect("/Error.aspx?id=" & errorId)
            End If
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try

    End Sub

End Class