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          : Leave Master                                Last Modified : 14-Aug-2007
* Programmer      : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Master_TA_UI_MasterLeaveTypeAdd : Base.BasePage
{
    #region Variable Declaration
    ResourceManager rm;
    cls_Common objCommon = new cls_Common();
    CultureInfo ci;
    LeaveEventData objLeave = new LeaveEventData();
    int Count;
    Int64 EmployeeID = 0;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    string strSetAlert;
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    #endregion
    #region Page Load
    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"]);
        EmployeeID = Convert.ToInt64(Session["EmpAutoID"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (!IsPostBack)
        {
            // LoadStrings Function fills the Label from the Resource File
            LoadStrings(ci);
            //Set UserRights
            UserRights();
            txt_LeaveType.Focus();
            btn_save.OnClientClick = "if(Page_ClientValidate()) return confirm('" + rm.GetString("Alert_ConfirmSave", ci) + "');return false;"; ;
            BindingEmployeeCategoryandType();
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("MaxLengthh", strSetAlert);
            objCommon.menu("LeaveMasters", "195");
        }
    }
    #endregion
    #region Button Events
    protected void btn_save_Click(object sender, EventArgs e)
    {
        Count = objLeave.CheckLeaveType_Insert(txt_LeaveType.Text.Trim().Replace("'", "''"));
        //Added by Manjula.P on 29-Jan-2014 for checking leave types count factor
        if (rbtnCountFactor.SelectedValue == "1")
        {
            string intCountFactorCount = objLeave.CheckCountFacorLeaveTypes(0);
            if (intCountFactorCount != string.Empty)
            {
                string[] strLeave = intCountFactorCount.Split(',');
                if (Convert.ToInt32(strLeave[0]) > 0)
                {
                    string strScript = rm.GetString("Alert_CountFactorLeaveTypesAlreadyDefined", ci) + " for the Leave Type " + Convert.ToString(strLeave[1]);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + strScript + "');", true);
                    return;
                }
            }
        }
        //***********************************************************************
        if (Count == 0)
        {
            objLeave.InsertLeaveTypeDetails(txt_LeaveType.Text.Trim().Replace("'", "''"),
                txt_LeaveDesc.Text.Trim().Replace("'", "''"), EmployeeID, 
                Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(ddlGender.SelectedItem.Text), Convert.ToInt32(ddlReligion.SelectedValue), Convert.ToInt32(rbtnCountFactor.SelectedValue), (chkboxProrating.Checked) ? 1 : 0, (chkboxDocProof.Checked) ? 1 : 0, 0);
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_LeaveTypesavedsuccessfully", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_MasterLeaveTypeList.aspx');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_LeaveTypeAlreadyExists", ci) + "');", true);
        }
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_MasterLeaveTypeList.aspx");
    }
    #endregion
    #region User Defined Functions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("lbl_leavetype", ci);
        lbl_LeaveType.Text = rm.GetString("lbl_leavetype", ci);
        lbl_Des.Text = rm.GetString("LeaveType_LeaveDesc", ci);
        lbl_Man.Text = rm.GetString("IndicatesMandatoryFields", ci);
        Req_LeaveType.ErrorMessage = rm.GetString("Validator_LeaveType_LeaveType", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Validator_LeaveType_LeaveDescription", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblCountFactor.Text = rm.GetString("lblCountFactor", ci);
        rbtnCountFactor.Items[0].Text = rm.GetString("ShiftSetting_Yes", ci);
        rbtnCountFactor.Items[1].Text = rm.GetString("ShiftSetting_No", ci);
        lblGender.Text = rm.GetString("EmployeeMaster_Gender", ci);
        lblReligion.Text = rm.GetString("EmployeeMaster_Religion", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(195, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #region BindingEmployeeCategoryandType.
    /// <summary>
    /// Bindings the employee category and type.
    /// </summary>
    private void BindingEmployeeCategoryandType()
    {
        // employee category binding
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployeeEventData.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
            foreach (ListItem item in ddlEmpCatagory.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        // employee type binding
        DataSet dsType = new DataSet();
        dsType = objEmployeeEventData.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
            foreach (ListItem item in ddlEmpType.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        DataSet dsReligion = new DataSet();
        dsReligion = objEmployeeEventData.SelectReligion();
        if (dsReligion != null)
        {
            if (dsReligion.Tables[0].Rows.Count > 0)
            {
                ddlReligion.DataSource = dsReligion;
                ddlReligion.DataTextField = "Name";
                ddlReligion.DataValueField = "Id";
                ddlReligion.DataBind();
            }
            dsReligion.Dispose();
            foreach (ListItem item in ddlReligion.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    #endregion
    protected void ddlEmpCatagory_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        DropdownTooltip(ddl);
    }
    protected void ddlEmpType_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        DropdownTooltip(ddl);
    }
    protected void ddlReligion_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        DropdownTooltip(ddl);
    }
    private void DropdownTooltip(DropDownList ddl)
    {
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    }
    #endregion
}