using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Masters_GetEmailIds : Base.BasePage
{
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    ResourceManager rm;
    CultureInfo ci;
    protected override void Page_Load(object sender, EventArgs e)
    {
        Session.LCID = 1033;
        
        //this.ucOrganizationLevel.ehClick += new EventHandler(this.btnPageButton_Click);
        // session checking and reset date format
        base.Page_Load(sender, e);
        loadcss();
        grv_Employee.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (Session["Session_ActiveLanguage"] == null)
        {
            string strScript1 = "<script language='javascript' type='text/javascript'>";
            strScript1 += "alert('Session Expired...');self.close();";
            strScript1 += " </script>";
            this.RegisterStartupScript("closeScript1", strScript1);
            return;
        }
        if (Session["Session_ActiveLanguage"] == null)
        {
            string str = "<script language=\"javascript\">";
            str = str + "opener.focus();";
            //str = str + "alert(opener.location+'?fl=1&sid=';";
            str = str + "opener.location.href = opener.location + '?fl=1&sid=" + Session.SessionID + "';";
            str = str + "self.close();";
            str = str + "</script>";
            Response.Write(str);
        }
        else
        {
            string source_Language;
            source_Language = Session["Session_ActiveLanguage"].ToString();
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            //aspnetForm.Attributes.CssStyle.Add("Direction", rm.GetString("Direction", ci));
            ci = Thread.CurrentThread.CurrentCulture;
            // Call the function LoadStrings to load the Label Names from the Resource Files
            LoadStrings(ci);
        }
        if (!IsPostBack)
        {
            string str = Request.QueryString["MailType"].ToString();
            string strScript = "<script language='javascript' type='text/javascript'>";
            strScript += "GetEmailList('" + str.ToString() + "');";
            strScript += " </script>";
            this.RegisterStartupScript("setScript", strScript);
            if (Request.QueryString["MailType"] == "CC")
            {
                btnAddCC.Text = "&nbsp;&nbsp;" + rm.GetString("lbl_AddCC", ci);
            }
            else
            {
                btnAddCC.Text = "&nbsp;&nbsp;" + rm.GetString("lbl_AddTO", ci);
            }
            Session["SelectAllStatus"] = null;
        }
        btn_Back.Attributes.Add("onclick", "parent.$('#myModal').modal('hide');return false;");
        EmployeeDataBind();
        
    }
    protected void butGo_Click(object sender, EventArgs e)
    {
        EmployeeDataBind();
    }
    protected void grv_Employee_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            if (hdnSelectRow.Value.ToString() == string.Empty)
            {
                //string[] employeeid = hdnSelectRow.Value.Split(',');
                //Label EmployeeCodeID = (Label)grv_Employee.FindRowCellTemplateControl(e.VisibleIndex, null, "lbl_EmpCodeId");
                //HtmlInputCheckBox ChkBxItem = (HtmlInputCheckBox)grv_Employee.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
                //foreach (string ID in employeeid)
                //{
                //    if (ID == EmployeeCodeID.Text.ToString())
                //    {
                //        ChkBxItem.Checked = true;
                //    }
                //}
            }
            //CheckBox deleteChkBxItem = (CheckBox)grv_Employee.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            //string strEmailId = ((Label)grv_Employee.FindRowCellTemplateControl(e.VisibleIndex, null, "EmailId")).Text;
            //if (strEmailId == string.Empty)
            //{
            //    deleteChkBxItem.Enabled = false;
            //    deleteChkBxItem.ToolTip = "No Email Id for this Employee";
            //}
            //else
            //{
            //    deleteChkBxItem.Enabled = true;
            //    deleteChkBxItem.ToolTip = string.Empty;
            //}
        }
    }
    /// <summary>
    /// Bind Employee Name along with Code Id in the List Box based on the Organization Level,
    /// Employee Name and Employee Id
    /// </summary>
    /// 
    public void EmployeeDataBind()
    {
        DataSet dsEmployee = new DataSet();
        int gintLevelID = 0;
        gintLevelID = Convert.ToInt32(Session["parentLevelId"]);
        //if (gintLevelID == 0)
        //{
        //    string strMsg;
        //    strMsg = "<script language='javascript'>";
        //    strMsg = strMsg + "alert('" + rm.GetString("Alert_Select_Company", ci) + "');";
        //    strMsg = strMsg + "</script>";
        //    Page.RegisterStartupScript("Display Message", strMsg);
        //    return;
        //}
        //else
        //{
        dsEmployee = objEmployeeEventData.SelectEmployeeEmailId(gintLevelID.ToString(), "", "", Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]));
        if (dsEmployee.Tables.Count > 0)
        {
            if (dsEmployee.Tables[0].Rows.Count > 0)
            {
                // Load Employee in the Employee Grid
                grv_Employee.DataSource = dsEmployee;
                grv_Employee.DataBind();
            }
            else
            {
                grv_Employee.DataSource = null;
                grv_Employee.DataBind();
            }
        }
        else
        {
            grv_Employee.DataSource = null;
            grv_Employee.DataBind();
        }
        //}
    }
    private void LoadStrings(CultureInfo ci)
    {
        butGo.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Go", ci);
        btn_Ok.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Ok", ci);
        btn_Back.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        btnAddCC.Text = "&nbsp;&nbsp;" + rm.GetString("lbl_AddCC", ci);
        btnClear.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Clear", ci);
        grv_Employee.Columns[2].Caption = rm.GetString("email_EmployeeId", ci);
        grv_Employee.Columns[3].Caption = rm.GetString("email_EmployeeName", ci);
        grv_Employee.Columns[4].Caption = rm.GetString("Master_Designation", ci);
        grv_Employee.Columns[5].Caption = rm.GetString("EmployeeMaster_EmailId", ci);
    }

    private void loadcss()
    {
        CultureInfo ci;
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        //if Standard Demo
        htmlGetEmail.Attributes.Add("dir", rm.GetString("Direction", ci));
        grv_Employee.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
            styleselect2.Href = "../plugins/select2/select2.min-rtl.css";
            styleGrid.Href = "../bootstrap/css/GridView-rtl.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew-rtl.css";
        }
        else
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            styleGrid.Href = "../bootstrap/css/GridView.css";
            stylefonts.Href = "../dist/css/fonts.css";
            styleselect2.Href = "../plugins/select2/select2.min-rtl.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew.css";
        }
    }
    protected void btnAddCC_Click(object sender, EventArgs e)
    {
        DataSet dsEmployeeEmail = new DataSet();
        if (hdnSelectRow.Value != string.Empty)
        {
            dsEmployeeEmail = objEmployeeEventData.GetEmployeeEmailIds(hdnSelectRow.Value);
            if (dsEmployeeEmail.Tables[0].Rows.Count > 0)
            {
                txtAddCC.Text = dsEmployeeEmail.Tables[0].Rows[0]["Email"].ToString();
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtAddCC.Text = string.Empty;
        hdnSelectRow.Value = string.Empty;
    }
    protected void btn_Ok_Click(object sender, EventArgs e)
    {
        if (txtAddCC.Text != string.Empty)
        {
            string[] strEmailList;
            string strEmailIds = string.Empty;
            strEmailList = txtAddCC.Text.Split(';');
            foreach (string strEmailID in strEmailList)
            {
                if (!Regex.IsMatch(strEmailID, @"^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|'((?=[\x01-\x7f])[^'\\]|\\[\x01-\x7f])*'\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|'((?=[\x01-\x7f])[^'\\]|\\[\x01-\x7f])*')@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$"))
                {
                    if (strEmailID != string.Empty)
                    {
                        string strScript1 = "<script language='javascript' type='text/javascript'>";
                        strScript1 += "alert('Invalid Email -  " + strEmailID + "');";
                        strScript1 += " </script>";
                        this.RegisterStartupScript("Incorrect", strScript1);
                        return;
                    }
                }
                else if (strEmailID != string.Empty)
                {
                    if (strEmailIds == string.Empty)
                    {
                        strEmailIds = strEmailID;
                    }
                    else
                    {
                        strEmailIds = strEmailIds + ";" + strEmailID;
                    }
                }
            }
            txtAddCC.Text = strEmailIds;
        }
        string strScript = "<script language=\"javascript\">";
        if (Request.QueryString["MailType"] == "CC")
        {
            strScript = strScript + "{window.top.parent.document.getElementById('ctl00_BodyContentPH_txtCCemailID').value='" + txtAddCC.Text + "';}";
        }
        else
        {
            strScript = strScript + "{window.top.parent.document.getElementById('ctl00_BodyContentPH_txtToemailID').value='" + txtAddCC.Text + "';}";
        }
        strScript = strScript + "parent.$('#myModal').modal('hide');";
        strScript = strScript + "</script>";
        Response.Write(strScript);


    }
    protected void btnPageButton_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "Loaddropdown();", true);
    }  
}
