using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class pg_JoinWebSite : System.Web.UI.Page
{
    //******************************
    SqlConnection con;
    SqlCommand cmd;
    //******************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "عضویت در سایت";
        }
        catch
        {
        }
    }
    protected void btnRecieved_Click(object sender, EventArgs e)
    {
        try
        {
            System.Threading.Thread.Sleep(2000);
            bool userNameExists = false, passwordLength = false;
            string userName = tbUserName.Text;
            string pass1 = tbPass1.Text;
            string pass2 = tbPass2.Text;
            userNameExists = CheckUserName(userName);
            passwordLength = CheckPassword(pass1);
            if (!userNameExists)
            {
                imgUserNameStatus.ImageUrl = "~/images/ok.gif";
            }
            else
            {
                imgUserNameStatus.ImageUrl = "~/images/error.gif";
            }
            if (passwordLength)
            {
                imgPass1Status.ImageUrl = "~/images/ok.gif";
                imgPass2Status.ImageUrl = "~/images/ok.gif";
            }
            else
            {
                imgPass1Status.ImageUrl = "~/images/error.gif";
                imgPass2Status.ImageUrl = "~/images/error.gif";
            }
            imgUserNameStatus.Visible = true;
            imgPass1Status.Visible = true;
            imgPass2Status.Visible = true;
            if ((!userNameExists) && (passwordLength))
            {
                tbUserName.BackColor = System.Drawing.Color.LightGray;
                tbPass1.BackColor = System.Drawing.Color.LightGray;
                tbPass2.BackColor = System.Drawing.Color.LightGray;
                tbUserName.ReadOnly = true;
                tbPass1.ReadOnly = true;
                tbPass2.ReadOnly = true;
                btnOk.Enabled = true;
            }
        }
        catch
        {
        }
    }

    private bool CheckUserName(string UserName)
    {
        bool res = false;
        try
        {
            MembershipUserCollection membersCollection = Membership.GetAllUsers();
            MembershipUser[] members = new MembershipUser[membersCollection.Count];
            membersCollection.CopyTo(members, 0);
            for (int i = 0; i < members.Length; i++)
            {
                if (UserName == members[i].UserName)
                    res = true;
            }
        }
        catch
        {
        }
        return res;
    }

    private bool CheckPassword(string Pass)
    {
        bool res = false;
        try
        {
            if (Pass.Length >= 5)
                res = true;
            else
                res = false;
        }
        catch
        {
        }
        return res;
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            string userName = tbUserName.Text;
            string pass = tbPass1.Text;
            string email = tbEmailAddress.Text;
            MembershipCreateStatus mcu = new MembershipCreateStatus();
            MembershipUser newUser = Membership.CreateUser(userName, pass, email, "noquestion", "noanswer", true, out mcu);
            if (newUser == null)
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = C_myClass.GetErrorMessageCreateUser(mcu);
            }
            else
            {
                Roles.AddUserToRole(newUser.UserName, "user");
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = t_UsersInserCommand("نامشخص", "", "", newUser.UserName, "himself");
                    cmd.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
                finally
                {
                    con.Close();
                }
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ایجاد شد." + "<br />" +
                    "حال می توانید با نام کاربری و کلمه عبور خود وارد سایت شوید و پروفایلتان را ایجاد کنید.";
            }
        }
        catch
        {
        }
    }

    private string t_UsersInserCommand(string FullName, string Gender, string UserPhotoUrl, string UserName, string UserCreator)
    {
        string comm = "insert into t_Users (fullName, gender, userPhotoUrl, userName, userCreater) values (N'" +
            FullName + "', N'" +
            Gender + "', N'" +
            UserPhotoUrl + "', N'" +
            UserName + "', N'" +
            UserCreator + "')";
        return comm;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        try
        {
            btnOk.Enabled = false;
            imgUserNameStatus.Visible = false;
            imgPass1Status.Visible = false;
            imgPass2Status.Visible = false;
            tbUserName.Text = "";
            tbPass1.Text = "";
            tbPass2.Text = "";
            tbUserName.BackColor = System.Drawing.Color.White;
            tbPass1.BackColor = System.Drawing.Color.White;
            tbPass2.BackColor = System.Drawing.Color.White;
            tbUserName.ReadOnly = false;
            tbPass1.ReadOnly = false;
            tbPass2.ReadOnly = false;
        }
        catch
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("Default.aspx");
        }
        catch
        {
        }
    }
}
