using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class System_Settings_System_MultiLingual : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    MultiLingualEventData objMultiLingualEventData = new MultiLingualEventData();
    DataSet ds_MultiLingual = new DataSet();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    string source_Language;
    CultureInfo ci;
    cls_Common objCommon = new cls_Common();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objCommon.menu("Configuration", "75");
        }
        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"]);
        Session["Direction"] = rm.GetString("Direction", ci);
        if (!IsPostBack)
        {
            LoadStrings(ci);
            //Call to a function to implement the UserRights
            UserRights();
            fillLanguage();
            setActiveLanguage(Convert.ToInt32(Session["EmpAutoID"]));
            ddlLanguage.Focus();
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SelectLanguage") + "','" + rm.GetString("Alert_Selectedlanguagealreadycurrentlanguage") + "','" + rm.GetString("Alert_AreyousurechangeLanguage") + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            btn_Save.OnClientClick = "javascript:return MultiLingualValidation();";
        }
    }
    #endregion
    #region UserDefinedFunctions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("SystemSettings_MultiLingualSelection", ci);
        btn_Save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        lbl_Language.Text = rm.GetString("SystemSettings_MultiLingual_Language", ci);
        ddlLanguage.Items[0].Text = rm.GetString("Select", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //objMultiLingualEventData.InsertUpdateActiveLanguage(Convert.ToInt32(ddlLanguage.SelectedItem.Value));
        objMultiLingualEventData.InsertorUpdateEmployeeLingual(Convert.ToInt32(ddlLanguage.SelectedItem.Value), Convert.ToInt32(Session["EmpAutoID"]));
        //getActiveLanguage();
        //GenerateMenu();
        HttpContext.Current.Cache.Remove(Convert.ToString(Session["EmployeeId"]).ToUpper());
        Session.Clear();
        MessageWithRedirect(rm.GetString("LanguageSettings_SavedSuccess"), "../Index.aspx");
    }
    protected void GenerateMenu()
    {
        CultureInfo ci;
        Session["TopMenu"] = "";
        Session["MasterLeftMenu"] = "";
        Session["OrgLeftMenu"] = "";
        Session["ReportLeftMenu"] = "";
        Session["SystemLeftMenu"] = "";
        Session["TransLeftMenu"] = "";
        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;
        //if Standard Demo
        string strType = string.Empty;
        if (Session["Demo"] != null)
        {
            if (Session["Demo"].ToString() == "Standard")
            {
                strType = "Standard";
            }
        }
        //Top Menu
        cls_Common getmenu = new cls_Common();
        DataSet ds = new DataSet();
        Session["TopMenu"] = "<table border='0' cellpadding=0 cellspacing=0><tr><td width'10'>&nbsp;</td>";
        if (Convert.ToInt16(Session["userGroup_Id"]) != 0)
        {
            ds = getmenu.getUserMenu(0, Convert.ToInt16(Session["userGroup_Id"]), strType);
            for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
            {
                String txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
                Session["TopMenu"] += "<td align='center'><a href='../" + ds.Tables[0].Rows[iloop]["path"] + "' title='" + rm.GetString(txt, ci) + " '>" + rm.GetString(txt, ci) + " </a></td>";
                Session["TopMenu"] += "<td width='15' align='center'>|</td>";
            }
        }
        Session["TopMenu"] += "</tr></table>";
        ds.Clear();
        ds.Dispose();
        //End of Top Menu
        //Master Left menu
        ds = getmenu.getUserMenu(1, Convert.ToInt16(Session["userGroup_Id"]), strType);
        Session["MasterLeftMenu"] = "<span class='LeftMenuHeader'>" + rm.GetString("Menu_Masters", ci) + "</span>";
        for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
        {
            string txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
            Session["MasterLeftMenu"] += "<a class='leftMenu' href='" + ds.Tables[0].Rows[iloop]["path"] + "' title='" + rm.GetString(txt, ci) + " '>" + rm.GetString(txt, ci) + " </a>";
        }
        ds.Clear();
        ds.Dispose();
        //End of Master Left Menu
        //Transaction left menu
        ds = getmenu.getUserMenu(2, Convert.ToInt16(Session["userGroup_Id"]), strType);
        Session["TransLeftMenu"] = "<span class='LeftMenuHeader'>" + rm.GetString("Menu_Transactions", ci) + "</span>";
        for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
        {
            string txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
            Session["TransLeftMenu"] += "<a class='leftMenu' href='" + ds.Tables[0].Rows[iloop]["path"] + "' title='" + rm.GetString(txt, ci) + " '>" + rm.GetString(txt, ci) + " </a>";
        }
        ds.Clear();
        ds.Dispose();
        //End of Transaction Left Menu
        //Report left menu
        ds = getmenu.getUserMenu(3, Convert.ToInt16(Session["userGroup_Id"]), strType);
        Session["ReportLeftMenu"] = "<span class='LeftMenuHeader'>" + rm.GetString("Menu_Reports", ci) + "</span>";
        for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
        {
            string txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
            Session["ReportLeftMenu"] += "<a class='leftMenu' href='" + ds.Tables[0].Rows[iloop]["path"] + "' title='" + rm.GetString(txt, ci) + " '>" + rm.GetString(txt, ci) + " </a>";
        }
        ds.Clear();
        ds.Dispose();
        //End of Report Left Menu
        //Org setting left menu
        ds = getmenu.getUserMenu(4, Convert.ToInt16(Session["userGroup_Id"]), strType);
        Session["OrgLeftMenu"] = "<span class='LeftMenuHeader'>" + rm.GetString("Menu_Org_Settings", ci) + "</span>";
        for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
        {
            string txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
            Session["OrgLeftMenu"] += "<a class='leftMenu' href='" + ds.Tables[0].Rows[iloop]["path"] + "' title='" + rm.GetString(txt, ci) + " '>" + rm.GetString(txt, ci) + " </a>";
        }
        ds.Clear();
        ds.Dispose();
        //End of Org Setting Left MEnu
        //Sys Setting left menu            
        ds = getmenu.getUserMenu(5, Convert.ToInt16(Session["userGroup_Id"]), strType);
        Session["SystemLeftMenu"] = "<span class='LeftMenuHeader'>" + rm.GetString("Menu_System_Settings", ci) + "</span>";
        for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
        {
            string txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
            Session["SystemLeftMenu"] += "<a class='leftMenu' href='" + ds.Tables[0].Rows[iloop]["path"] + "' title='" + rm.GetString(txt, ci) + " '>" + rm.GetString(txt, ci) + " </a>";
        }
        ds.Clear();
        ds.Dispose();
        //End of Sys Setting Left Menu
    }
    private void getActiveLanguage()
    {
        ds_MultiLingual = objMultiLingualEventData.GetActiveLanguageDetails();
        Session["Session_ActiveLanguage"] = "";
        source_Language = "Resources." + Convert.ToString(ds_MultiLingual.Tables[0].Rows[0]["Resource_File_Name"]);
        Session["Session_ActiveLanguage"] = source_Language;
        //get the culture info to set the language
        CultureInfo ci;
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
    }
    /// <summary>
    /// Fill Language Details in dropdown list ddlLanguage
    /// </summary>
    private void fillLanguage()
    {
        ds_MultiLingual = objMultiLingualEventData.GetLanguageDetails();
        ddlLanguage.DataSource = ds_MultiLingual;
        ddlLanguage.DataTextField = "Language_Name";
        ddlLanguage.DataValueField = "Language_Id";
        ddlLanguage.DataBind();
        ddlLanguage.Items.Insert(0, rm.GetString("Select", ci));
        ds_MultiLingual.Clear();
        foreach (ListItem item in ddlLanguage.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    /// <summary>
    /// set Active Language
    /// </summary>
    private void setActiveLanguage(int EmpID)
    {
        ds_MultiLingual = objMultiLingualEventData.GetEmployeeLingual(EmpID);
        if (ds_MultiLingual.Tables.Count > 0)
        {
            if (ds_MultiLingual.Tables[0].Rows.Count > 0)
            {
                ddlLanguage.SelectedValue = Convert.ToString(ds_MultiLingual.Tables[0].Rows[0]["Language_Id"]);
                hdnLanguage.Value = ddlLanguage.SelectedIndex.ToString();
            }
            else
            {
                ddlLanguage.SelectedValue = "1";
                hdnLanguage.Value = "2";
            }
        }
        else
        {
            ddlLanguage.SelectedValue = "1";
            hdnLanguage.Value = "2";
        }
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(75, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("SystemLandingPage.aspx");
        }
    }
    #endregion
}