using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Data;

public partial class Transactions_PopUp_DesignationAdd : System.Web.UI.Page
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    cls_Master_Title objTitle = new cls_Master_Title();
    LeaveEventData objLeavePolicy = new LeaveEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objClassCommon = new cls_Common();
    int Count, EmpID;
    string strSetAlert;
    int gradeMasterExists = 0;
    ShiftEvenData objShift = new ShiftEvenData();
    string gstrUserGroupName = string.Empty;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        //base.Page_Load(sender, e);
        //get the culture info to set the language
        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;
        //site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        // LoadStrings Function fills the Label from the Resource File
        LoadStrings(ci);
        //Set UserRights
        UserRights();
        try
        {
            EmpID = Convert.ToInt32(Session["EmpAutoID"].ToString());
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        catch (Exception ex)
        {
            Response.Redirect("../Index.aspx");
            Response.Write(ex.ToString());
        }
        gradeMasterExists = objClassCommon.CheckPageExistsInLicense(228);
        if (gradeMasterExists == 0)
        {
            trStatus.Style.Add("display", "none");
            RequiredFieldValidator3.IsValid = true;
        }
        if (!IsPostBack)
        {
            fillPolicyddl();
            fillGradeddl();
            txt_TitleName.Focus();
            ddlCompany.Focus();
            CompanyBinding();
            btn_add.OnClientClick = "if(Page_ClientValidate()) return confirm('" + rm.GetString("Alert_ConfirmSave", ci) + "');return false;";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("MaxLengthh", strSetAlert);
            objClassCommon.menu("EmployeeMaster", "17");
        }

        loadcss();
    }
    #region UserDefinedfunctions

    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;
        htmlShiftAllocation.Attributes.Add("dir", rm.GetString("Direction", ci));
        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";
            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";
            styleselect2.Href = "../plugins/select2/select2.min.css";
            stylefonts.Href = "../dist/css/fonts.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew.css";
        }
    }
    /// <summary>
    /// Added by Manjula.P on 06-Jan-2014 for binding company or levels details 
    /// </summary>
    private void CompanyBinding()
    {
        DataSet dsCompany = new DataSet();
        dsCompany = objShift.SelectCompany(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), 0);
        if (dsCompany != null)
        {
            if (dsCompany.Tables[0].Rows.Count > 0)
            {
                ddlCompany.DataSource = dsCompany;
                ddlCompany.DataTextField = "CompanyName";
                ddlCompany.DataValueField = "CompanyId";
                ddlCompany.DataBind();
            }
        }
        foreach (ListItem item in ddlCompany.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    //fills Leave Policy dropdown
    private void fillPolicyddl()
    {
        DataSet ds_Policy = new DataSet();
        ds_Policy = objLeavePolicy.SelectAllLeavePolicy(string.Empty);
        if (ds_Policy.Tables[0].Rows.Count > 0)
        {
            ddl_Policy.DataSource = ds_Policy;
            ddl_Policy.DataTextField = "PolicyName";
            ddl_Policy.DataValueField = "Policy_Id";
            ddl_Policy.DataBind();
        }
        ds_Policy.Dispose();
        foreach (ListItem item in ddl_Policy.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    private void fillGradeddl()
    {
        string userGroupName = string.Empty;
        DataSet ds_Grade = new DataSet();
        //if (Session["Edition"].ToString() == "1")
        if (gradeMasterExists == 0)
        {
            objTitle.CheckGradeNamesStandard();
            ds_Grade = objTitle.SelectGradesStandard(userGroupName);
        }
        else
        {
            ds_Grade = objTitle.SelectGrades(userGroupName);
        }
        if (ds_Grade.Tables[0].Rows.Count > 0)
        {
            ddl_Grade.DataSource = ds_Grade;
            ddl_Grade.DataTextField = "Grade_Name";
            ddl_Grade.DataValueField = "Grade_Id";
            ddl_Grade.DataBind();
            //if (Session["Edition"].ToString() == "1")
            if (gradeMasterExists == 0)
            {
                ddl_Grade.SelectedIndex = 1;
            }
        }
        ds_Grade.Dispose();
        foreach (ListItem item in ddl_Grade.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_DesignationName.Text = "Designation Name";

        lbl_DesignationDescription.Text = rm.GetString("Designation_DesignationDesc", ci);
        lbl_GradeName.Text = rm.GetString("Grade_GradeName", ci);
        lbl_LeavePolicy.Text = rm.GetString("Employee_LeavePolicy", ci);
        lbl_IndicatesMandatoryFieds.Text = rm.GetString("IndicatesMandatoryFields", ci);
        ReqValTitleName.ErrorMessage = rm.GetString("Validator_Designation_DesignationName", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("Validator_Designation_Grade", ci);
        //lbl_HeaderDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        //btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        ddl_Grade.Items[0].Text = rm.GetString("Select", ci);
        ddl_Policy.Items[0].Text = rm.GetString("Select", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(17, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_add_Click(object sender, EventArgs e)
    {
        string strTitleName = Server.HtmlEncode(txt_TitleName.Text.Trim().Replace("'", "''"));
        string strTitleDesc = Server.HtmlEncode(txt_TitleDesc.Text.Trim().Replace("'", "''"));
        //Adding by MAnjula.P on 06-Jan-2014
        int intLevelID = 0;
        if (ddlCompany.SelectedValue != "0")
        {
            intLevelID = Convert.ToInt32(ddlCompany.SelectedValue);
        }
        if (intLevelID != 0)
        {
            Count = objTitle.CheckTilteName_Insert(txt_TitleName.Text.Trim().Replace("'", "''"), intLevelID);
            int intGradeID = Convert.ToInt32(ddl_Grade.SelectedValue);
            int intPolicyId = 0;
            if (ddl_Policy.SelectedValue != "-Select-")
            {
                intPolicyId = Convert.ToInt32(ddl_Policy.SelectedValue);
            }
            if (Count == 0)
            {
               
                objTitle.InsertTitleFirstDetails(strTitleName, strTitleDesc, intLevelID, intPolicyId, intGradeID, EmpID);
                string str = "alert('" + rm.GetString("Alert_Designationsavedsuccessfully", ci) + "');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                //Response.Redirect("../Masters/TA_UI_Master_EmployeeAdd.aspx?desgnignationId=" + 1);
                //base.MessageWithRedirect(rm.GetString("Alert_Employeedeletedsuccessfully", ci), "TA_UI_Master_Employee.aspx");
            }
            else
            {
                string str = "alert('" + rm.GetString("Alert_DesingationNameAlreadyExits", ci) + "');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
        }
        else
        {
            ddlCompany.Focus();
            string str = "alert('" + rm.GetString("Alert_Select_Company", ci) + "');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
    }
    //protected void btn_cancel_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("TA_UI_Master_TitleFirstList.aspx");
    //}
    #endregion
    #region DropDownEvents
    protected void ddl_Grade_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    }
    #endregion

}