using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                     Started Date  : 12-Aug-2007
* Module          : Title Master                                  Last Modified : 14-Aug-2007
* Programmer      : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Master_TA_UI_Master_TitleFirstAdd : Base.BasePage
{
    #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
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        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");
        }
        ////if Standar Edition set the visibility to false
        //if (Session["Edition"].ToString() == "1")
        //{
        //    trStatus.Style.Add("display", "none");
        //    RequiredFieldValidator3.IsValid = true;
        //}
    }
    #endregion
    #region UserDefinedfunctions
    /// <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 = rm.GetString("Designation_DesignationName", ci);
        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) + "');location.replace('TA_UI_Master_TitleFirstList.aspx');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
            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
}
