using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
public partial class Masters_TA_UI_Master_EmployeeCategoryAdd : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    string strSetAlert;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = 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);
        //Set UserRights
        UserRights();
        // focus to first control
        txtCategory.Focus();
        if (!IsPostBack)
        {
            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("EmployeeMaster", "94");
        }
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(94, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        int CategoryID = 0;
        CategoryID = Convert.ToInt32(Request.QueryString["CatID"]);
        string CategoryName = Convert.ToString(txtCategory.Text.Trim().Replace("'", "''"));
        DataSet ds = new DataSet();
        CategoryEventData Cat = new CategoryEventData();
        ds = Cat.CheckDuplicate("I", CategoryName, CategoryID);
        bool IsFlexible = false;
        string dMonToSatWorkHrs = Convert.ToString(txt_MonToSatWorkHrs.Text.Trim());
        string dWeekDayWorkHrs = Convert.ToString(txt_WeekdayWorkHrs.Text.Trim());
        string[] str1;

        decimal DcmTotWeekHours = Convert.ToDecimal(dMonToSatWorkHrs.Replace(':', '.'));
        decimal DcmWeekDayHours = Convert.ToDecimal(dWeekDayWorkHrs.Replace(':', '.'));
        if (DcmTotWeekHours <= 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_MinimumMandatoryCumulativeWorkHrs", ci) + "');", true);
            return;
        }
        if (DcmTotWeekHours > 168)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_MinimumMandatoryCumulativeWorkHrs168", ci) + "');", true);
            return;
        }

        str1 = Convert.ToString(txt_WeekdayWorkHrs.Text.Trim()).Split(':');
        if (str1.Length == 2)
        {
            if (str1[0].Length != 2)
            {
                string str = "<script language=\"javascript\">";
                //str = str + "alert('Select Any Record');";
                str = str + "alert('" + rm.GetString("Alert_EntervalidDailyMinimumWorkingHrs", ci) + "');";
                str = str + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, false);
                return;
            }
            if (str1[1].Length != 2)
            {
                string str = "<script language=\"javascript\">";
                //str = str + "alert('Select Any Record');";
                str = str + "alert('" + rm.GetString("Alert_EntervalidDailyMinimumWorkingHrs", ci) + "');";
                str = str + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, false);
                return;
            }
        }
        else
        {
            string str = "<script language=\"javascript\">";
            //str = str + "alert('Select Any Record');";
            str = str + "alert('" + rm.GetString("Alert_EntervalidDailyMinimumWorkingHrs", ci) + "');";
            str = str + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, false);
            return;
        }
        //if (DcmWeekDayHours <= 0)
        //{
        //    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Working Hours should not be Lesser than 0');", true);
        //    return;
        //}
        if (DcmWeekDayHours > 24)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_DailyMinimumWorkingHrsshouldbelesserthan24Hrs", ci) + "');", true);
            return;
        }
        if (rbtnFlexibleYes.Checked == true)
        {
            IsFlexible = true;
        }
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_EmployeeCategoryalreadyexists", ci) + "');", true);
            }
            else
            {
                Cat.InsertUpdate("I", txtCategory.Text.Trim().Replace("'", "''"), Convert.ToString(Session["EmpAutoID"]), CategoryID, IsFlexible, dMonToSatWorkHrs, dWeekDayWorkHrs);
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_EmployeeCategorysavedsuccessfully", ci) + "');";
                strScript = strScript + "location.replace('TA_UI_Master_EmployeeCategorylist.aspx');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            }
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Category.Text = rm.GetString("Master_Category", ci);
        lbl_Header.Text = rm.GetString("Master_Category", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lbl_MonToSatWorkHrs.Text = rm.GetString("Master_Category_MondayToSaturday", ci);
        lbl_WeekdayWorkHrs.Text = rm.GetString("Master_Category_WeekDay", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("Validator_EnterCategory", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Alert_EnterMinimumMandatoryCumulative", ci);
        RequiredFieldValidator4.ErrorMessage = rm.GetString("Alert_EnterDailyMinimumWorkingHrs", ci);
        lblFlexibleTimings.Text = rm.GetString("FlexibleTimings", ci);
        rbtnFlexibleYes.Text = rm.GetString("Yes", ci);
        rbtnFlexibleNo.Text = rm.GetString("Grid_False", ci);
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_EmployeeCategorylist.aspx");
    }
}
