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_TitleFirstModify : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    string strSetAlert;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Master_Title objTitle = new cls_Master_Title();
    LeaveEventData objLeavePolicy = new LeaveEventData();
    cls_Common objClassCommon = new cls_Common();
    CultureInfo ci;
    DataSet ds_Title = new DataSet();
    DataSet ds_Checkuser = new DataSet();
    ShiftEvenData objShift = new ShiftEvenData();
    int TitleId, Count, EmpID;
    string gstrUserGroupName = string.Empty;
    int gradeMasterExists = 0;
    #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();
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        TitleId = Convert.ToInt32(Request.QueryString["TitleId"].ToString());
        try
        {
            EmpID = Convert.ToInt32(Session["EmpAutoID"].ToString());
        }
        catch (Exception ex)
        {
            Response.Redirect("../Index.aspx");
            Response.Write(ex.ToString());
        }
        gradeMasterExists = objClassCommon.CheckPageExistsInLicense(228);
        if (gradeMasterExists == 0)
        {
            trStatus.Style.Add("display", "none");
        }
        if (!IsPostBack)
        {
            fillPolicyddl();
            fillGradeddl();
            //Added by Manjula.P on 06-Jan-2014 for binding company or levels details 
            ddlCompany.Focus();
            CompanyBinding();
            LoadTitle(TitleId);
            txt_TitleName.Focus();
            btn_Modify.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");

        //}
    }
    #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);
        }
    }
    private void LoadTitle(int id)
    {
        ds_Title = objTitle.SelectTitleFirstDetailsById(id, gstrUserGroupName);

        if (ds_Title.Tables[0].Rows.Count > 0)
        {
            txt_TitleName.Text = Server.HtmlDecode(ds_Title.Tables[0].Rows[0]["TitleName"].ToString());
            txt_TitleDesc.Text = Server.HtmlDecode(ds_Title.Tables[0].Rows[0]["TitleDescription"].ToString());
            ddl_Grade.SelectedValue = ds_Title.Tables[0].Rows[0]["GradeID"].ToString();
            ddl_Policy.SelectedValue = ds_Title.Tables[0].Rows[0]["LeavePolicyId"].ToString();
            ddlCompany.SelectedValue = Convert.ToString(ds_Title.Tables[0].Rows[0]["LevelID"]);
        }
        ds_Title.Dispose();
    }
    //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);
        }
    }
    //fills Grade dropdown
    private void fillGradeddl()
    {
        string userGroupName = string.Empty;
        DataSet ds_Grade = new DataSet();
        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();
        }
        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);
        btn_Modify.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);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(17, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region ButtonEvents
    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;
            }
        }
    }
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        string strTitleName = Server.HtmlEncode(txt_TitleName.Text.Trim().Replace("'", "''"));
        string strTitleDesc = Server.HtmlEncode(txt_TitleDesc.Text.Trim().Replace("'", "''"));
        int intLevelID = 0;
        if (ddlCompany.SelectedValue != "0")
        {
            intLevelID = Convert.ToInt32(ddlCompany.SelectedValue);
        }
        if (intLevelID != 0)
        {
            Count = objTitle.CheckTilteName_Updates(txt_TitleName.Text.Trim().Replace("'", "''"), TitleId, 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.UpdateTitleFirstDetails(strTitleName, strTitleDesc, intLevelID, intPolicyID, intGradeID, EmpID, TitleId);
                string str = "alert('" + rm.GetString("Alert_Designationmodifiedsuccessfully", ci) + "');location.replace('TA_UI_Master_TitleFirstList.aspx');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
            else
            {
                base.Message(rm.GetString("Alert_DesingationNameAlreadyExits", ci));
            }
        }
        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");
        //Response.Redirect("TA_UI_Master_TitleFirstView.aspx?TitleId=" + Request.QueryString["TitleId"].ToString());
    }
    #endregion
}