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_EmployeeCategoryModify : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    cls_Common objcls_Common = new cls_Common();
    string strSetAlert;
    DataSet ds_Checkuser = new DataSet();
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        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);
        //Set UserRights
        UserRights();
        if (!IsPostBack)
        {
            if (Request.QueryString["CatID"] != null)
            {
                int CategoryID;
                CategoryID = Convert.ToInt32(Request.QueryString["CatID"]);
                CategoryEventData Cat = new CategoryEventData();
                DataSet ds = new DataSet();
                ds = Cat.SelectCategory(CategoryID);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            if (ds.Tables[0].Rows[0]["CategoryName"] != DBNull.Value)
                            {
                                txtCategory.Text = Convert.ToString(ds.Tables[0].Rows[0]["CategoryName"]);
                                txt_MonToSatWorkHrs.Text = Convert.ToString(ds.Tables[0].Rows[0]["MonToSat_WorkHrs"]).ToString();
                                txt_WeekdayWorkHrs.Text = Convert.ToString(ds.Tables[0].Rows[0]["WeekDay_WorkHrs"]).ToString();
                                if (ds.Tables[0].Rows[0]["IsFlexible"] != DBNull.Value)
                                    if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsFlexible"]))
                                    {
                                        rbtnFlexibleYes.Checked = true;
                                    }
                                    else
                                    {
                                        rbtnFlexibleNo.Checked = true;
                                    }
                                string selectScript = objcls_Common.SelectAll(txtCategory);
                                RegisterStartupScript("SelectScript", selectScript);
                            }
                        }
                    }
                }
            }
            // focus to first control
            txtCategory.Focus();
            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);
            objcls_Common.menu("EmployeeMaster", "94");
        }
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(94, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
        else
        {
            //   
        }
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["CatID"] != null)
        {
            int CategoryID;
            CategoryID = Convert.ToInt32(Request.QueryString["CatID"]);
            string CategoryName = Convert.ToString(txtCategory.Text.Trim().Replace("'", "''"));
            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;
            }
            DataSet ds = new DataSet();
            CategoryEventData Cat = new CategoryEventData();
            ds = Cat.CheckDuplicate("U", CategoryName, CategoryID);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //lblError.Text = "Employee Category Already Exists"; '" + rm.GetString("Alert_EmployeeCategorydeletedsuccessfully", ci) + "')
                    //base.Message(rm.GetString("Alert_EmployeeCategoryalreadyexists",ci));
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_EmployeeCategoryalreadyexists", ci) + "');", true);
                }
                else
                {
                    Cat.InsertUpdate("U", txtCategory.Text.Trim().Replace("'", "''"), Convert.ToString(Session["EmpAutoID"]), CategoryID, IsFlexible, dMonToSatWorkHrs,dWeekDayWorkHrs);
                    //string str = "alert('" + rm.GetString("Alert_EmployeeCategorysavedsuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeCategorylist.aspx')";
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                    string strScript = "";
                    strScript = strScript + "<script language='javascript'>";
                    strScript = strScript + "alert('" + rm.GetString("Alert_EmployeeCategoryModifiedsuccessfully", ci) + "');";
                    strScript = strScript + "location.replace('TA_UI_Master_EmployeeCategorylist.aspx');";
                    strScript = strScript + "</script>";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                }
            }
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Category.Text = rm.GetString("Master_Category", ci);
        lbl_MonToSatWorkHrs.Text = rm.GetString("Master_Category_MondayToSaturday", ci);
        lbl_WeekdayWorkHrs.Text = rm.GetString("Master_Category_WeekDay", 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);
        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");
    }
}
