using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        divPageMessage.Visible = false;

        if (Page.IsPostBack == false)
        {
            Initialize();
        }
    }

    private void Initialize()
    {

    }

    private string CheckValidationForUserNameAndPassword
        (DataSetTableAdapters.UsersTableAdapter oUserTableAdapter,
        DataSet.UsersDataTable oUserDataTable, string username, string email)
    {
        string strErrorMessage = string.Empty;

        oUserTableAdapter.FillUserByUserName(oUserDataTable, username);

        if (oUserDataTable.Count != 0)
        {
            if (string.IsNullOrEmpty(strErrorMessage) == false)
            {
                strErrorMessage += "<br/>";
            }

            strErrorMessage +=
                "Username Is Already Exist.Please Choose Another One.";
        }

        oUserTableAdapter.FillUserByEmail(oUserDataTable, email);
        if (oUserDataTable.Count != 0)
        {
            if (string.IsNullOrEmpty(strErrorMessage) == false)
            {
                strErrorMessage += "<br/>";
            }

            strErrorMessage +=
                 "Email Is Already Exist.Please Choose Another One.";
        }

        return (strErrorMessage);
    }

    protected virtual void DisplayErrorMessage(string message)
    {
        divPageMessage.Visible = true;
        litPageMessage.Text =
            string.Format("<div class ='error'>{0}</div>", message);
    }

    protected virtual void DisplayWarningMessage(string message)
    {
        divPageMessage.Visible = true;
        litPageMessage.Text =
            string.Format("<div class ='warning'>{0}</div>", message);
    }

    protected virtual void DisplayInformationMessage(string message)
    {
        divPageMessage.Visible = true;
        litPageMessage.Text =
            string.Format("<div class ='information'>{0}</div>", message);
    }



    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DataSet.UsersDataTable oUserDataTable =
                new DataSet.UsersDataTable();

            DataSetTableAdapters.UsersTableAdapter oUserTableAdapter =
                new DataSetTableAdapters.UsersTableAdapter();

            string strErrorMessage =
                CheckValidationForUserNameAndPassword
                (oUserTableAdapter, oUserDataTable, txtUserName.Text, txtMail.Text);

            if (string.IsNullOrEmpty(strErrorMessage) == false)
            {
                DisplayErrorMessage(strErrorMessage);
                return;
            }

            DataSet.UsersRow oUserRow =
                oUserDataTable.NewUsersRow();

            oUserRow.IsUserActive = true;
            oUserRow.Email = txtMail.Text;
            oUserRow.UserName = txtUserName.Text;
            oUserRow.Password = txtPassword.Text;

            oUserDataTable.AddUsersRow(oUserRow);
            oUserTableAdapter.Update(oUserDataTable);

            string strInformationMessage =
                "Your Registration Was Completed SuccessFully  . . .";
            DisplayInformationMessage(strInformationMessage);
        }
    }
}