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 digioz;

public partial class controls_Registration : System.Web.UI.UserControl
{
    public UserProfile coUserProfile = new UserProfile();
    private PortalSettings coPortalSettings;
    public bool lbValidSelectionFlag = false;

    private void SetMonthCombo()
    {
        cboMonth.Items.Clear();
        cboMonth.Items.Add("");
        cboMonth.Items.Add("January");
        cboMonth.Items.Add("February");
        cboMonth.Items.Add("March");
        cboMonth.Items.Add("April");
        cboMonth.Items.Add("May");
        cboMonth.Items.Add("June");
        cboMonth.Items.Add("July");
        cboMonth.Items.Add("August");
        cboMonth.Items.Add("September");
        cboMonth.Items.Add("October");
        cboMonth.Items.Add("November");
        cboMonth.Items.Add("December");
    }

    private void SetDayCombo()
    {
        int i = 0;

        cboDay.Items.Clear();
        cboDay.Items.Add("");

        for (i = 1; i < 32; i++)
        {
            cboDay.Items.Add(Convert.ToString(i));
        }
    }

    private void SetYearCombo()
    {
        int i = 0;
        int liCurrentYear = DateTime.Now.Year;

        cboYear.Items.Clear();
        cboYear.Items.Add("");

        for (i = liCurrentYear; i > 1900; i--)
        {
            cboYear.Items.Add(Convert.ToString(i));
        }
    }

    private void LoadUserProfileObject()
    {
        coUserProfile.Username = txtUsername.Text;
        coUserProfile.Password = txtPassword.Text;
        coUserProfile.Permission = "0";
        coUserProfile.Email = txtEmail.Text;
        if (cboMonth.SelectedIndex > 0 && cboDay.SelectedIndex > 0 && cboYear.SelectedIndex > 0)
        {
            coUserProfile.Birthday = Convert.ToDateTime(cboMonth.SelectedIndex + "/" + cboDay.SelectedValue + "/" + cboYear.SelectedValue);
        }
        if (chkDoNotDisplayAgeInProfile.Checked) { coUserProfile.HideBirthday = true; } else { coUserProfile.HideBirthday = false; }
        coUserProfile.Address = txtAddress.Text;
        coUserProfile.Address2 = txtAddress2.Text;
        coUserProfile.City = txtCity.Text;
        coUserProfile.State = txtState.Text;
        coUserProfile.Zip = txtZip.Text;
        coUserProfile.Country = txtCountry.Text;
        coUserProfile.Signature = txtSignature.Text;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string lsIPFilter = "LoginFilters" + Setting.DELIMITER + "IP" + Setting.DELIMITER + Utility.GetVisitor().IP;
            Setting loIPFilter = SettingsManager.GetSettingByName(lsIPFilter);
            if (loIPFilter != null && loIPFilter.Enabled)
            {
                btnCheck.Enabled = false;
                btnSave.Enabled = false;
            }
        }

        SetDayCombo();
        SetYearCombo();
        SetMonthCombo();
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();

        cboMonth.SelectedIndex = 0;
        cboDay.SelectedIndex = 0;
        cboYear.SelectedIndex = 0;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        string lsUserNameFilter = "LoginFilters" + Setting.DELIMITER + "UserName" + Setting.DELIMITER + txtUsername.Text;
        Setting loUserNameFilter = SettingsManager.GetSettingByName(lsUserNameFilter);
        if ((loUserNameFilter == null || !loUserNameFilter.Enabled) &&
            UserManagement.IsUsernameAvailable(txtUsername.Text))
        {
            lblUsernameAvailable.ForeColor = System.Drawing.Color.Green;
            lblUsernameAvailable.Text = "Available!";
        }
        else
        {
            lblUsernameAvailable.ForeColor = System.Drawing.Color.Red;
            lblUsernameAvailable.Text = "Not Available!";
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        lblLoginFilterError.Text = String.Empty;
        lblImageVerifierError.Text = String.Empty;
        lblRequiredEmailError.Text = String.Empty;
        lblRequiredPasswordError.Text = String.Empty;
        lblBirthDateError.Text = String.Empty;

        string
           lsUserNameFilter = "LoginFilters" + Setting.DELIMITER + "UserName" + Setting.DELIMITER + txtUsername.Text,
           lsEmailFilter = "LoginFilters" + Setting.DELIMITER + "Email" + Setting.DELIMITER + txtEmail.Text;
        Setting
            loUserNameFilter = SettingsManager.GetSettingByName(lsUserNameFilter),
            loEmailFilter = SettingsManager.GetSettingByName(lsEmailFilter);

        if (loUserNameFilter != null && loUserNameFilter.Enabled)
        {
            lblLoginFilterError.Text = "Invalid User Name!";
            return;
        }
        if (loEmailFilter != null && loEmailFilter.Enabled)
        {
            lblLoginFilterError.Text = "Invalid Email Address!";
            return;
        }
        //if (ImageVerifier1.Text != txtImgVerifyCode.Text)
        //{
        //    lblImageVerifierError.Text = "You have entered a wrong code. Please enter the new one correctly.";
        //    return;
        //}
        if (txtEmail.Text.Length <= 0)
        {
            lblRequiredEmailError.Text = "Email Address is a required field and can not be empty!";
            return;
        }
        if (txtPassword.Text.Length <= 0)
        {
            lblRequiredPasswordError.Text = "Password is a required field and can not be empty!";
            return;
        }
        if (txtPassword.Text != txtVerify.Text)
        {
            lblRequiredPasswordError.Text = "Password Verify does not match initial password!";
            return;
        }
        if (cboMonth.SelectedIndex > 0 && cboDay.SelectedIndex > 0 && cboYear.SelectedIndex > 0)
        {
            lbValidSelectionFlag = true;
        }
        else if (cboMonth.SelectedIndex <= 0 && cboDay.SelectedIndex <= 0 && cboYear.SelectedIndex <= 0)
        {
            lbValidSelectionFlag = true;
        }
        else
        {
            lbValidSelectionFlag = false;
        }
        if (lbValidSelectionFlag == false)
        {
            lblBirthDateError.Text = "Your birthdate selection is not complete, please make month, day and year seletions or leave all blank.";
            return;
        }
        else
        {
            if (UserManagement.IsUsernameAvailable(txtUsername.Text) == false)
            {
                lblUsernameAvailable.ForeColor = System.Drawing.Color.Red;
                lblUsernameAvailable.Text = "Not Available!";
                return;
            }
            else
            {
                lblUsernameAvailable.ForeColor = System.Drawing.Color.Red;
                lblUsernameAvailable.Text = "Not Available!";

                LoadUserProfileObject();
                UserProfileManagement.SaveUserProfile(coUserProfile, "New");

                lblImageVerifierError.ForeColor = System.Drawing.Color.Green;
                lblImageVerifierError.Text = "Username Created!";

                Response.Redirect("Default.aspx");
            }
        }
    }
}
