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

namespace FMSWeb
{
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using VBNET=Microsoft.VisualBasic;
using gmRTL.Core;

public partial class __NewAccount : System.Web.UI.Page
{
   public FMSWeb._t_head _t_head1 = null;
   public FMSWeb._t_begin _t_begin1 = null;
   public FMSWeb._t_end _t_end1 = null;

   protected void Page_Load(object sender, EventArgs e)
   {
      _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);
   }
   private FMStocks_Bus.Account obj = new FMStocks_Bus.Account();
   public string Email = "";
   public string FirstName = "";
   public string LastName = "";
   public string Password = "";
   public string MainPrompt = "";
   //  Examines the form and creates a new account if the fields look valid.
   //  Redirects back to the login page if successful.
   public void ProcessPage()
   {
      //  Get the form values (if any)
      Email = HttpContext.Current.Request.Form["Email"].TrimB();
      FirstName = HttpContext.Current.Request.Form["FirstName"].TrimB();
      LastName = HttpContext.Current.Request.Form["LastName"].TrimB();
      Password = HttpContext.Current.Request.Form["Password"].TrimB();
      // First we need to see if the user is trying to input information
      if (HttpContext.Current.Request.Form["CreateAccount"] == "Yes")
      {
         // Check to see that user filled out form correctly
         if (IsFormValid().ToBool())
         {
            // Update account returns a 1 if the transaction completed and a 0 if the transaction failed
            if (CreateAccount().ToInt() != 0)
            {
               HttpContext.Current.Response.Redirect("default.aspx?NewAccount=1&login=" + Email);
            }
         }
         else
         {
            MainPrompt = "<FONT COLOR=Black>" + FMSWeb._strings.FILL_ALL_FIELDS + "</FONT>";
         }
      }
   }
   //  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 bool IsFormValid()
   {
      bool IsFormValid = false;

      IsFormValid = true;
      if (String.IsNullOrEmpty(Email) || String.IsNullOrEmpty(LastName) || String.IsNullOrEmpty(FirstName) || String.IsNullOrEmpty(Password))
      {
         IsFormValid = false;
      }
      return IsFormValid;
   }
   //  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 int CreateAccount()
   {
      int CreateAccount = 0;

      // UPGRADE_TODO: OnErrorResumeNext();
      CreateAccount = 0;     //  assume it fails
      int AccountID = 0;
      AccountID = obj.Add(FirstName,LastName,Password,Email);
      if (gmRTL.Core.GlobalException.Instance.Number == -2147217900 || AccountID == 0)
      {
         //  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();
         return CreateAccount;
      }
      else if (gmRTL.Core.GlobalException.Instance.Number != 0)
      {
         //  Emit a general error message. This could occur if the database is down or paused.
         MainPrompt = FMSWeb._strings.DATABASE_ERROR;
         _t_head1.BuildErrorMessage();
         return CreateAccount;
      }
      // If we've gotten this far, everything went okay.  Set the function to the AccountID, indicating success.
      CreateAccount = AccountID;
      return CreateAccount;
   }
}
}
