using System;
using System.Data;
using System.Globalization;
using System.IO;
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_TerminalAdd : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Common objCommon = new cls_Common();
    DataSet ds_Checkuser = new DataSet();
    CultureInfo ci;
    TerminalEventData objTerminalEventData = new TerminalEventData();
    string strTerminalDescription = "", strTerminalName = "", strTerminalID = "", strSetAlert;
    int EmployeeID, intRawDataFormat = 0;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        txt_TerId.Focus();
        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"]);
        if (!IsPostBack)
        {
            UserRights();
            objCommon.menu("Terminal", "224");
            LoadStrings(ci);
            BindDataFormat();
            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);
        }
        EmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
    }
    private void BindDataFormat()
    {
        DataSet ds_RawDataFormats = new DataSet();
        ds_RawDataFormats = objTerminalEventData.SelectRawDataFormats();
        if (ds_RawDataFormats.Tables[0].Rows.Count > 0)
        {
            ddl_DataFormat.Items.Clear();
            ddl_DataFormat.DataSource = ds_RawDataFormats;
            ddl_DataFormat.DataTextField = "Raw_Data_Format";
            ddl_DataFormat.DataValueField = "TerminalDatatype_ID";
            ddl_DataFormat.DataBind();
            ListItem liSelect = new ListItem();
            liSelect.Value = "-Select-";
            liSelect.Text = rm.GetString("Select", ci);
            ddl_DataFormat.Items.Insert(0, liSelect);
            foreach (ListItem item in ddl_DataFormat.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        else
        {
            ddl_DataFormat.Items.Clear();
            ddl_DataFormat.Items.Insert(0, "-Select-");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_Terminal.aspx");
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (ddl_DataFormat.Text == "-Select-")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_PleaseselectRawDataFormat", ci) + "');", true);
        }
        else
        {
            strTerminalID = txt_TerId.Text.Trim().Replace("'", "''");
            strTerminalName = txt_TerminalName.Text.Trim().Replace("'", "''");
            strTerminalDescription = txt_TerminalDescription.Text.Trim().Replace("'", "''");
            intRawDataFormat = Convert.ToInt32(ddl_DataFormat.SelectedValue);
            int count = objTerminalEventData.Check_duplicateTerminal(strTerminalID, strTerminalName);
            if (count == 0)
            {
                bool isAllowTerminalAdd = CheckNoOfTerminalForLicense();
                if (isAllowTerminalAdd)
                {
                    objTerminalEventData.InsertTerminalDetails(strTerminalID, strTerminalName, strTerminalDescription, intRawDataFormat, EmployeeID);
                    string strScript = "";
                    strScript = strScript + "<script language='javascript'>";
                    strScript = strScript + "alert('" + rm.GetString("Alert_Terminalsavedsuccessfully", ci) + "');";
                    strScript = strScript + "location.replace('TA_UI_Master_Terminal.aspx');";
                    strScript = strScript + "</script>";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_TerminalIDNameAlreadyExists", ci) + "');", true);
            }
        }
    }
    #endregion
    #region UserDefinedFunctions
    private bool CheckNoOfTerminalForLicense()
    {
        DemoVersion objDemoVersion = new DemoVersion();
        bool allowEmployeeAdd = false;
        string[] fileArr = Directory.GetFiles(Server.MapPath("../LicenseFile"), "*.lic");
        int arrCount = fileArr.Length;
        if (arrCount > 0)
        {
            string licenseFileName = Convert.ToString(fileArr[arrCount - 1]);
            FileStream licenseFileStream = new FileStream(licenseFileName, FileMode.Open);
            StreamReader licenseFileStreamReader = new StreamReader(licenseFileStream);
            string encryptedLicFileInfo = licenseFileStreamReader.ReadToEnd();
            licenseFileStreamReader.Close();
            licenseFileStream.Close();
            string decryptedLicFileInfo = string.Empty;
            try
            {
                decryptedLicFileInfo = Encryption.Decrypt(encryptedLicFileInfo, "");
            }
            catch
            {
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "alert('" + rm.GetString("Alert_Licenseinformationarenotcorrect", ci) + "');";
                strScript += "location.href='../Index.aspx';";
                strScript += " </script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                return false;
            }
            string[] decryptedLicFileInfoArr = decryptedLicFileInfo.Split('#');
            string basicLicInfo = Convert.ToString(decryptedLicFileInfoArr[0]);
            string[] basicLicInfoArr = basicLicInfo.Split(',');
            int noOfEmployeeLicFile = Convert.ToInt32(basicLicInfoArr[8]);
            allowEmployeeAdd = objDemoVersion.DVCheckTerminalCreation("License", noOfEmployeeLicFile);
            if (allowEmployeeAdd)
            {
                return true;
            }
            else
            {
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "alert('" + rm.GetString("Alert_LicensenotallowtoAddmoreTerminal", ci) + "');";
                strScript += " </script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                return false;
            }
        }
        else
        {
            return true;
        }
    }
    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_save.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"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
}