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_MasterLeaveTypeModify : Base.BasePage
{
    #region variable Declaration
    ResourceManager rm;
    CultureInfo ci;
    LeaveEventData objLeaveType = new LeaveEventData();
    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    DataSet ds_Checkuser = new DataSet();
    DataSet ds_LeaveType = new DataSet();
    int LeaveType;
    int Count, EmployeeID;
    string gstrUserGroupName = string.Empty;
    string strSetAlert;
    #endregion
    #region PageLoading
    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.ToInt32(Session["EmpAutoID"].ToString());
        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();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        txt_LeaveType.Focus();
        LeaveType = Convert.ToInt32(Request.QueryString["LeaveId"].ToString());
        if (!IsPostBack)
        {
            BindingEmployeeCategoryandType();
            LoadLeaveType(LeaveType);
            btn_save.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);
            objCommon.menu("LeaveMasters", "195");
        }
        trCountFactor.Attributes.Add("style", "display:''");
    }
    #endregion
    #region Button Events
    protected void btn_save_Click(object sender, EventArgs e)
    {
        Count = objLeaveType.CheckLeaveType_Update(txt_LeaveType.Text.Trim().Replace("'", "''"), LeaveType);
        if (rbtnCountFactor.SelectedValue == "1")
        {
            string intCountFactorCount = objLeaveType.CheckCountFacorLeaveTypes(LeaveType);
            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)
        {
            objLeaveType.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,
                LeaveType);
            string strRedirectPage = "TA_UI_MasterLeaveTypeList.aspx";
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_LeaveTypemodifiedsuccessfully", ci) + "');";
            strScript = strScript + "location.replace('" + strRedirectPage + "');";
            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_MasterLeaveTypeView.aspx?LeaveId=" + Request.QueryString["LeaveId"].ToString());
        Response.Redirect("TA_UI_MasterLeaveTypeList.aspx");
    }
    #endregion
    #region User Defined Functions
    private void LoadLeaveType(int id)
    {
        ds_LeaveType = objLeaveType.SelectLeaveTypeDetailsById(id, gstrUserGroupName);
        if (ds_LeaveType.Tables[0].Rows.Count > 0)
        {
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["LeaveType"]) != string.Empty)
                txt_LeaveType.Text = Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["LeaveType"]);
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["LeaveDescription"]) != string.Empty)
                txt_LeaveDesc.Text = Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["LeaveDescription"]);
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["EmployeeCategorys"]) != string.Empty)
                ddlEmpCatagory.SelectedValue = Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["EmployeeCategorys"]);
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["EmployeeTypes"]) != string.Empty)
                ddlEmpType.SelectedValue = Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["EmployeeTypes"]);
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["Gender"]) != string.Empty)
                ddlGender.SelectedValue = Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["Gender"]);
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["Religions"]) != string.Empty)
                ddlReligion.SelectedValue = Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["Religions"]);
            if (ds_LeaveType.Tables[0].Rows[0]["CountFactor"].ToString() == "True")
            {
                rbtnCountFactor.Items[0].Selected = true;
            }
            else
            {
                rbtnCountFactor.Items[1].Selected = true;
            }
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["IsProrataRequired"]) != string.Empty)
                chkboxProrating.Checked = Convert.ToBoolean(ds_LeaveType.Tables[0].Rows[0]["IsProrataRequired"]);
            if (Convert.ToString(ds_LeaveType.Tables[0].Rows[0]["IsDocProofRequired"]) != string.Empty)
                chkboxDocProof.Checked = Convert.ToBoolean(ds_LeaveType.Tables[0].Rows[0]["IsDocProofRequired"]);
        }
    }
    #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
    #region Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderLeaveType.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);
    }
    #endregion
    #region Check UserRights
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(195, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #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
}