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          : Terminal Master                                  Last Modified : 14-Aug-2007
* Programmer      : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_TerminalModify : Base.BasePage
{
    #region VariableDeclration
    ResourceManager rm;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Common objCommon = new cls_Common();
    DataSet ds_Checkuser = new DataSet();
    CultureInfo ci;
    string strSetAlert, gstrUserGroupName = string.Empty;
    int intTerminal_ID = 0, EmployeeID = 0;
    TerminalEventData objTerminalEventData = new TerminalEventData();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        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);
        EmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
        UserRights();
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        intTerminal_ID = Convert.ToInt32(Request.QueryString["Terminal_Id"]);
        txt_TerId.Focus();
        if (!IsPostBack)
        {
            int Count = objTerminalEventData.GetTerminalMappingCount(intTerminal_ID);
            if (Count > 0)
            {
                txt_TerId.Enabled = false;
            }
            DataSet ds_RawDataFormats = new DataSet();
            ds_RawDataFormats = objTerminalEventData.SelectRawDataFormats();
            if (ds_RawDataFormats.Tables[0].Rows.Count > 0)
            {
                ddl_DataFormat.DataSource = ds_RawDataFormats;
                ddl_DataFormat.DataTextField = "Raw_Data_Format";
                ddl_DataFormat.DataValueField = "TerminalDatatype_ID";
                ddl_DataFormat.DataBind();
                ddl_DataFormat.Items.Insert(0, "-Select-");
            }
            else
            {
                ddl_DataFormat.Items.Clear();
                ddl_DataFormat.Items.Insert(0, "-Select-");
            }
            foreach (ListItem item in ddl_DataFormat.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
            GetTerminalDetails();
            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);
            objCommon.menu("Terminal", "224");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_Terminal.aspx");
    }
    protected void btn_modify_Click(object sender, EventArgs e)
    {
        if (ddl_DataFormat.Text == "-Select-")
        {
            base.Message(rm.GetString("Alert_PleaseselectRawDataFormat", ci));
        }
        else
        {
            int count = objTerminalEventData.Check_duplicateTerminalupdate(intTerminal_ID, txt_TerId.Text.Trim().Replace("'", "''"), txt_TerminalName.Text.Trim().Replace("'", "''"));
            if (count == 0)
            {
                objTerminalEventData.UpdateTerminalDetails(intTerminal_ID, txt_TerId.Text.Trim().Replace("'", "''"), txt_TerminalName.Text.Trim().Replace("'", "''"), txt_TerminalDescription.Text.Trim().Replace("'", "''"), Convert.ToInt32(ddl_DataFormat.SelectedValue), EmployeeID);
                string strRedirectPage = "TA_UI_Master_Terminal.aspx";
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_Terminalmodifiedsuccessfully", 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_TerminalIDAlreadyExists", ci) + "');", true);
            }
        }
    }
    #endregion
    #region UserDefinedFunctions
    private void GetTerminalDetails()
    {
        DataSet ds_TerminalDetails = new DataSet();
        ds_TerminalDetails = objTerminalEventData.SelectTerminalDetailsBYId(intTerminal_ID, gstrUserGroupName);
        if (ds_TerminalDetails.Tables.Count > 0)
        {
            if (ds_TerminalDetails.Tables[0].Rows.Count > 0)
            {
                txt_TerId.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["Terminal_ReaderID"]).Trim();
                txt_TerminalName.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["Terminal_Name"]).Trim();
                txt_TerminalDescription.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["Terminal_Description"]).Trim();
                ddl_DataFormat.SelectedValue = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["TerminalDatatype_ID"]);
            }
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_TerminalId.Text = rm.GetString("TerminalMaster_Terminal_Id", ci);
        lbl_TerminalName.Text = rm.GetString("TerminalMaster_Terminal_Name", ci);
        lbl_TerminalDescription.Text = rm.GetString("TerminalMaster_Terminal_Desc", ci); ;
        lbl_RawDataFormat.Text = rm.GetString("TerminalDataType_RawDataFormat", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        RequiredFieldValidator2.ErrorMessage = rm.GetString("Validator_Terminal_TerminalId", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Validator_Terminal_TerminalName", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("Validator_Terminal_RawDataFormat", ci);
        lbl_TerminalMaster.Text = rm.GetString("Masters_Terminal_TerminalMaster", ci);
        btn_modify.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(10, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
}