using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
/****************************************************************************************************************************
* Project Name    : TimeCheck                    Started Date  : 12-Aug-2007
* Module          : Leave Application            Last Modified : 14-Aug-2007
* Programmer      : Janakaraj.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_TA_UI_Transaction_Enchasement : Base.BasePage
{
    ResourceManager rm;
    string EmpId;
    //string UserName;
    DataSet ds_LeaveType = new DataSet();
    DataSet ds_RemainigNoLeaves = new DataSet();
    DataSet ds_EmployeeGroupName = new DataSet();
    DataSet ds_EmployeeDetails = new DataSet();
    CultureInfo ci;
    LeaveApplicationEventData obj = new LeaveApplicationEventData();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    EncashmentEventData ObjEncashmentEventData = new EncashmentEventData();
    int employeeId, LeaveTypeId;
    cls_Common objClassCommon = new cls_Common();
    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(ci);
        // Alert Message 28-1-2011
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_LeaveType", ci) + "','" + rm.GetString("Alert_RemainingNoOfdaysShouldNotBeEmptyZero", ci) + "','" + rm.GetString("Alert_DayEmpty", ci) + "','" + rm.GetString("Alert_ReasonShouldNotbeEmpty", ci) + "','" + rm.GetString("Alert_NumberOfDaysAppliedShouldbegreaterthanRemainingnumberofDays", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_NumberOfDaysAppliedShouldbefractionof5", ci) + "','" + rm.GetString("Alert_Norecordsfound", ci) + "','" + rm.GetString("Alert_Errorinretrivingdetails", ci) + "','" + rm.GetString("lblSelectEmployee", ci) + "','" + rm.GetString("Select", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        if (!Page.IsPostBack)
        {
            objClassCommon.menu("LeaveTransaction", "76");
            DataSet dsEmployeeGroup = new DataSet();
            ds_EmployeeGroupName = obj.GetEmployeeGroupName(Convert.ToInt32(Session["EmpAutoId"]));
            employeeId = Convert.ToInt32(Session["EmpAutoId"]);
            if (ds_EmployeeGroupName.Tables[0].Rows.Count > 0)
            {
                if (ds_EmployeeGroupName.Tables[0].Rows[0]["GroupName"].ToString() == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
                {
                    divEmployees.Visible = false;
                    divAdmin.Visible = true;
                    PlaceHolder1.Visible = true;
                }
                else
                {
                    divEmployees.Visible = true;
                    PlaceHolder1.Controls.Clear();
                    divAdmin.Visible = false;
                    //Display Current User Name and Employee Code
                    ds_EmployeeDetails = objEmployeeEventData.Select_EmployeeDetailsNewbyId(Convert.ToInt32(Session["EmpAutoId"]));
                    if (ds_EmployeeDetails.Tables[0].Rows.Count > 0)
                    {
                        lbl_EmpName.Text = ds_EmployeeDetails.Tables[0].Rows[0]["Employee_FirstName"].ToString();
                        lbl_EmpCode.Text = ds_EmployeeDetails.Tables[0].Rows[0]["Employee_code_Id"].ToString();
                        FillLeaveType();
                        hdnEmployeeId.Value = Session["EmpAutoId"].ToString();
                    }
                }
            }
            else
            {
                divEmployees.Visible = true;
                PlaceHolder1.Controls.Clear();
                divAdmin.Visible = false;
                ds_EmployeeDetails = objEmployeeEventData.Select_EmployeeDetailsNewbyId(Convert.ToInt32(Session["EmpAutoId"]));
                if (ds_EmployeeDetails.Tables[0].Rows.Count > 0)
                {
                    lbl_EmpName.Text = ds_EmployeeDetails.Tables[0].Rows[0]["Employee_FirstName"].ToString();
                    lbl_EmpCode.Text = ds_EmployeeDetails.Tables[0].Rows[0]["Employee_code_Id"].ToString();
                    FillLeaveType();
                }
                hdnEmployeeId.Value = Session["EmpAutoId"].ToString();
            }
        }
    }
    private void FillLeaveType()
    {
        //Fill LeaveType Dropdown
        try
        {
            EmpId = Convert.ToString(employeeId);
            ds_LeaveType = obj.SelectLeavetypeByEncashment(EmpId);
            ddl_Leavetype.DataTextField = "LeaveType";
            ddl_Leavetype.DataValueField = "LeaveType_Id";
            ddl_Leavetype.DataSource = ds_LeaveType;
            ddl_Leavetype.DataBind();
            foreach (ListItem item in ddl_Leavetype.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
            //ddl_Leavetype.Items.Insert(0, "-Select-");
        }
        catch
        {
            //
        }
    }
    //Display Reaminig Number of Leavedays for a Particular Leave Type
    protected void ddl_Leavetype_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            //Update Year closure table
            EmpId = Convert.ToString(employeeId);
            //if (ddl_Leavetype.Text != "-Select-")
            if (ddl_Leavetype.SelectedValue != "-Select-")
            {
                ds_RemainigNoLeaves = obj.GetRemainingNumberOfLeave(EmpId, Convert.ToInt16(ddl_Leavetype.SelectedValue));
                if (ds_RemainigNoLeaves.Tables[0].Rows.Count > 0)
                {
                    //lbl_Numberofdays.Text = ds_RemainigNoLeaves.Tables[0].Rows[0][0].ToString();
                    txt_RemainingNoOfDays.Text = ds_RemainigNoLeaves.Tables[0].Rows[0][0].ToString();
                    hdnRemainingNoOfDays.Value = ds_RemainigNoLeaves.Tables[0].Rows[0][0].ToString();
                }
            }
            else
            {
                //lbl_Numberofdays.Text = "";
                txt_RemainingNoOfDays.Text = "0";
                hdnRemainingNoOfDays.Value = "0";
            }
        }
        catch
        {
            //
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            double RemainingDays = Convert.ToDouble(hdnRemainingNoOfDays.Value);
            double NumberOfDaysApplied = Convert.ToDouble(txt_Numberofdays.Text);
            if (NumberOfDaysApplied > RemainingDays)
            {
                //lbl_Error.Text = "Number of Days applied should lesser than remaining leaves";
                //base.Message(rm.GetString("Alert_NumberofDaysappliedshouldlesserthanremainingleaves",ci));
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_NumberofDaysappliedshouldlesserthanremainingleaves", ci) + "');", true);
                return;
            }
            if (NumberOfDaysApplied <= 0)
            {
                //lbl_Error.Text = "Number of Days applied should be greater than zero";
                //base.Message(rm.GetString("Msg_NumberofDaysappliedshouldbegreaterthanzero", ci));
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_NumberofDaysappliedshouldbegreaterthanzero", ci) + "');", true);
                return;
            }
            EmpId = hdnEmployeeId.Value;
            if (hdnLeaveTypeId.Value == "")
            {
                //lbl_Error.Text="Select Leave Type";
                //base.Message(rm.GetString ("Alert_LeaveType",ci));
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_LeaveType", ci) + "');", true);
                return;
            }
            else
            {
                LeaveTypeId = Convert.ToInt16(hdnLeaveTypeId.Value);
            }
            try
            {
                obj.updateYearclosureEncashment(EmpId, Convert.ToDouble(txt_Numberofdays.Text), LeaveTypeId);
                ObjEncashmentEventData.InsertEncashmentDetails(Convert.ToInt32(EmpId), LeaveTypeId, Convert.ToDouble(txt_Numberofdays.Text), txtReason.Text);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            lbl_EmpCode.Text = "";
            lbl_EmpName.Text = "";
            //lbl_Numberofdays.Text = "";
            txt_RemainingNoOfDays.Text = "";
            hdnRemainingNoOfDays.Value = "";
            txt_Numberofdays.Text = "";
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Msg_Encashmentsavedsuccessfully", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Transaction_Encashment.aspx');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            //Response.Redirect("TA_UI_Transaction_Encashment.aspx");
            // base.MessageWithRedirect(rm.GetString("Msg_Encashmentsavedsuccessfully	Encashment saved successfully",ci), "TA_UI_Transaction_Encashment.aspx");
            //base.MessageWithRedirect(rm.GetString("Msg_Encashmentsavedsuccessfully", ci), "TA_UI_Transaction_Encashment.aspx");
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_Encashmentsavedsuccessfully", ci), "TA_UI_Transaction_Encashment.aspx") + "')", true);
        }
        catch
        {
            //
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Transaction_Encashment.aspx");
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("UserGroups_Encashment", ci);
        lbl_Code.Text = rm.GetString("Ecashmnent_Empcode", ci);
        lbl_LeaveType.Text = rm.GetString("lbl_leavetype", ci);
        lbl_Name.Text = rm.GetString("email_EmployeeName", ci);
        lbl_Nodays.Text = rm.GetString("Encashment_Remainingdays", ci);
        lbl_Numberofdaysapp.Text = rm.GetString("Encashment_Nodaysapplied", ci);
        lbl_EmployeeNames.Text = rm.GetString("email_EmployeeName", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Encashment_Nodaysapplied", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lbl_Reason.Text = rm.GetString("email_Reason", ci);
        btn_Save.Text = "&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Back.Text = "&nbsp;" + rm.GetString("btn_Back", ci);
        btnSearch.Text = "&nbsp;" + rm.GetString("btn_Search", ci);
        RangeValidator1.ErrorMessage = rm.GetString("Alert_NumberofDaysshouldbetween1and365", ci);
        ddlEmployeeNames.Items[0].Text = rm.GetString("Select", ci);
        //ddl_Leavetype.Items[0].Text = rm.GetString("Select", ci);
        lblLoading.Text = rm.GetString("ProgressBarText", ci);
    }
}
