'' asp code Generated by Basic Processor
'' Original Page Name: C:\gmTestBed\FMStocks\src\FMSWeb\ASP\_NewAccount.asp

Imports System.Web
NameSpace FMSWeb
Public Partial class __NewAccount : Inherits System.Web.UI.Page
   Public _t_head1 As FMSWeb._t_head
   Public _t_begin1 As FMSWeb._t_begin
   Public _t_end1 As FMSWeb._t_end

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      _t_head1 = New FMSWeb._t_head()
      _t_begin1 = New FMSWeb._t_begin()
      _t_end1 = New FMSWeb._t_end()
      _t_begin1.Include_Load(_t_head1)
      _t_end1.Include_Load(_t_head1)
   End Sub
   Dim obj As New FMStocks_Bus.Account
   Public Email As String = ""
   Public FirstName As String = ""
   Public LastName As String = ""
   Public Password As String = ""
   Public MainPrompt As String = ""
   ' Examines the form and creates a new account if the fields look valid.
   ' Redirects back to the login page if successful.
   Public Sub ProcessPage()
      ' Get the form values (if any)
      Email = Strings.Trim(HttpContext.Current.Request.Form("Email"))
      FirstName = Strings.Trim(HttpContext.Current.Request.Form("FirstName"))
      LastName = Strings.Trim(HttpContext.Current.Request.Form("LastName"))
      Password = Strings.Trim(HttpContext.Current.Request.Form("Password"))
      'First we need to see if the user is trying to input information
      If (HttpContext.Current.Request.Form("CreateAccount") = "Yes") Then
         'Check to see that user filled out form correctly
         If (IsFormValid() = True) Then
            'Update account returns a 1 if the transaction completed and a 0 if the transaction failed
            If (CreateAccount() <> 0) Then
               HttpContext.Current.Response.Redirect("default.aspx?NewAccount=1&login=" & Email)
            End If
         Else
            MainPrompt = "<FONT COLOR=Black>" & FMSWeb._strings.FILL_ALL_FIELDS & "</FONT>"
         End If
      End If
   End Sub
   ' Perform server-side validation on the fields before they're passed to
   ' the middle-tier. It would be possible to insert a call here to some
   ' business logic to see if the e-mail account is already in use.
   Public Function IsFormValid() As Boolean
      IsFormValid = True
      If (String.IsNullOrEmpty(Email) Or String.IsNullOrEmpty(LastName) Or String.IsNullOrEmpty(FirstName) Or String.IsNullOrEmpty(Password)) Then
         IsFormValid = False
      End If
   End Function
   ' Creates the business-logic component that actually creates a new
   ' user account. If everything works, the new AccountID is returned.
   ' All exceptions are handled.
   Public Function CreateAccount() As Integer
      On Error Resume Next
      CreateAccount = 0                  ' assume it fails
      Dim AccountID As Integer = 0
      AccountID = obj.Add(FirstName,LastName,Password,Email)
      If (Err.Number = -2147217900 Or AccountID = 0) Then
         ' This ADO error message corresponds to the message thrown by the database
         ' when a unique index constraint is violated. Since trying to create
         ' an account with a duplicate e-mail is common, we'll handle the message
         ' gracefully and tell the user to choose another account.
         MainPrompt = FMSWeb._strings.NEWACCOUNT_EXISTING_EMAIL
         ' Since we're handling a "normal" message, you wouldn't normally want to
         ' emit the error message using the global function below. We'll leave it
         ' in here for demonstration purposes.
         _t_head1.BuildErrorMessage()
         Exit Function
      ElseIf (Err.Number <> 0) Then
         ' Emit a general error message. This could occur if the database is down or paused.
         MainPrompt = FMSWeb._strings.DATABASE_ERROR
         _t_head1.BuildErrorMessage()
         Exit Function
      End If
      'If we've gotten this far, everything went okay.  Set the function to the AccountID, indicating success.
      CreateAccount = AccountID
   End Function
End Class
End Namespace
