﻿Imports System.Data
Imports System.Data.SqlClient
Imports HashLibrary

Partial Class activate
    Inherits System.Web.UI.Page
    Public ErrorText As String = ""

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ErrorText = ""
        lblError.Visible = False
        divRegistered.Visible = False
        divActivated.Visible = False
        If Not Page.IsPostBack Then

            If Not IsNothing(Request.QueryString("g")) Then
                'ACTIVATE ACCOUNT NOW
                Dim activationcode As String = Request.QueryString("g")
                Dim ErrorMessage As String = ""
                If ActivateAccount(activationcode, ErrorMessage) Then
                    divActivated.Visible = True
                Else
                    lblError.InnerText = ErrorMessage
                    divError.Visible = True
                End If
            Else
                'USER JUST REGISTERED
                divRegistered.Visible = True
            End If

        End If
    End Sub

    Private Function ActivateAccount(ByVal activationcode As String, ByRef ErrorMessage As String) As Boolean
        Try
            Dim dt As New DataTable
         Using con As SqlConnection = New SqlConnection(functions.GetConnectionString)
            Using cmd As SqlCommand = New SqlCommand("USERS_spACTIVATE", con)
               cmd.CommandType = CommandType.StoredProcedure
               cmd.Parameters.AddWithValue("@activationcode", activationcode)
               If con.State = ConnectionState.Closed Then con.Open()

               Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                  da.Fill(dt)
               End Using
            End Using
         End Using
         If Not IsNothing(dt) AndAlso dt.Rows.Count > 0 AndAlso dt.Columns(0).ColumnName = "ERROR" Then
            ErrorMessage = dt.Rows(0)("ERROR").ToString
            Return False
         ElseIf IsNothing(dt) Then
            ErrorMessage = "Unable to activate account. Please try again later."
            Return False
         Else
            Return True
         End If
      Catch ex As Exception
            ErrorMessage = "Unable to activate account. Please try again later."
            Return False
        End Try
    End Function

End Class
