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;
public partial class Organization_Settings_Organization_NewsEdit : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    NewsEventData news = new NewsEventData();
    string strFromdate, gstrDateFormat, strTodate;
    cls_Common objCommon = new cls_Common();
    int divId;
    string strLocation = "";
    #endregion

    #region Page Load Event
    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);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EnterDate", ci) + "','" + rm.GetString("Alert_EnterToDate", ci) + "','" + rm.GetString("Alert_EntertheTitle", ci) + "','" + rm.GetString("Alert_EntertheDescription", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_SelectanImageFile", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        UserRights();
        if (!IsPostBack)
        {
            objCommon.menu("Others", "53");
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            txtDate.Focus();
            if (Request.QueryString["nId"] != "" && Request.QueryString["nId"] != null)
            {
                int nId = Convert.ToInt16(Request.QueryString["nId"]);
                Session["nId"] = nId;
                DataSet ds = new DataSet();
                ds = news.getNews(nId);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["NewsEventFromDate"] != DBNull.Value)
                    {
                        this.txtDate.Text = (DateTime.Parse(ds.Tables[0].Rows[0]["NewsEventFromDate"].ToString())).ToString(Session["MDateFormat"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["NewsEventToDate"] != DBNull.Value)
                    {
                        this.txtToDate.Text = (DateTime.Parse(ds.Tables[0].Rows[0]["NewsEventToDate"].ToString())).ToString(Session["MDateFormat"].ToString());
                    }
                    string newsTitle = ds.Tables[0].Rows[0]["NewsEventTitle"].ToString();
                    if (newsTitle.Length == 0)
                    {
                        this.rbtnBanner.Checked = true;
                        divId = 2;
                        if (Convert.ToInt16(ds.Tables[0].Rows[0]["NewsEventActive"]) == 1)
                        {
                            this.cbActive.Checked = true;
                        }
                        else
                        {
                            this.cbActive.Checked = false;
                        }
                        if (Convert.ToInt16(ds.Tables[0].Rows[0]["LeftNewsEvent"]) == 1)
                        {
                            this.rbleftNews.Checked = true;
                        }
                        else
                        {
                            this.rbleftNews.Checked = false;
                        }
                        if (Convert.ToInt16(ds.Tables[0].Rows[0]["RightNewsEvent"]) == 1)
                        {
                            this.rbRightNews.Checked = true;
                        }
                        else
                        {
                            this.rbRightNews.Checked = false;
                        }
                        if (ds.Tables[0].Rows[0]["NewsEventImage"] != DBNull.Value)
                        {
                            ViewState["strLocation"] = "";
                            string fileName = ds.Tables[0].Rows[0]["NewsEventImage"].ToString();
                            strLocation = AppDomain.CurrentDomain.BaseDirectory + "Banners\\" + fileName;
                            ViewState["strLocation"] = strLocation;
                            if (System.IO.File.Exists(strLocation))
                            {
                                this.imgBanner.ImageUrl = strLocation;
                            }
                            else
                            {
                            }
                        }
                    }
                    else
                    {
                        this.rbtnNews.Checked = true;
                        this.txtTitle.Text = Server.HtmlDecode(newsTitle);
                        this.txtDescription.Text = Server.HtmlDecode(Convert.ToString(ds.Tables[0].Rows[0]["NewsEventDescription"]));
                        divId = 1;
                        if (Convert.ToInt16(ds.Tables[0].Rows[0]["NewsEventActive"]) == 1)
                        {
                            this.cbActive.Checked = true;
                        }
                        else
                        {
                            this.cbActive.Checked = false;
                        }
                        if (Convert.ToInt16(ds.Tables[0].Rows[0]["LeftNewsEvent"]) == 1)
                        {
                            this.rbleftNews.Checked = true;
                        }
                        else
                        {
                            this.rbleftNews.Checked = false;
                        }
                        if (Convert.ToInt16(ds.Tables[0].Rows[0]["RightNewsEvent"]) == 1)
                        {
                            this.rbRightNews.Checked = true;
                        }
                        else
                        {
                            this.rbRightNews.Checked = false;
                        }
                    }
                }
                else
                {
                }
            }
        }
        string strScript = "<script language='javascript' type='text/javascript'>";
        strScript += " ShowMe('" + divId + "')";
        strScript += " </script>";
        this.RegisterStartupScript("setDivId", strScript);
    }
    #endregion

    #region User Defined Functions
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("UserGroups_NewsEvents", ci);
        lbl_NewsBanner.Text = rm.GetString("NewsEvents_NewsBanner", ci);
        lbl_Active.Text = rm.GetString("rbtn_EmployeeMaster_Active", ci);
        lbl_Date.Text = rm.GetString("OnDuty_InDate", ci);
        lbl_Des.Text = rm.GetString("Designation_DesignationDesc", ci);
       // lbl_Man.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lbl_Position.Text = rm.GetString("NewsEvents_Position", ci);
        lbl_Title.Text = rm.GetString("NewsEvents_Title", ci);
        RequiredFieldValidator2.ErrorMessage = rm.GetString("Reqfiled_Title", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("rfv_EnterDescription", ci);
        //RequiredFieldValidator4.ErrorMessage = rm.GetString("Req_FileUpload", ci);
        rbleftNews.Text = rm.GetString("RblLeftNewsEvents", ci);
        rbRightNews.Text = rm.GetString("RblRightNewsEvents", ci);
        rbtnNews.Text = rm.GetString("NewsEvents_News", ci);
        rbtnBanner.Text = rm.GetString("rbtn_Banner", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblToDate.Text = rm.GetString("OnDuty_OutDate", ci);
        CustomValidator1.ErrorMessage = rm.GetString("Validator_Holidays_FromDate", ci);
        cus_ComDate.ErrorMessage = rm.GetString("Msg_GrearterThanFrmDate", ci);
        CustomValidator2.ErrorMessage = rm.GetString("Validator_Holidays_ToDate2", ci);
        lblImage.Text = rm.GetString("NewsEvents_Image", ci);
        lblDivNews.Text = rm.GetString("NewsEvents_News", ci);
        lblDivBanner.Text = rm.GetString("rbtn_Banner", ci);
        RequiredFieldValidator2.ErrorMessage = rm.GetString("Reqfiled_Title", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("rfv_EnterDescription", ci);
        //RequiredFieldValidator4.ErrorMessage = rm.GetString("rfv_Selectimage", ci);
    }
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtDate.Text;
            strTodate = txtToDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtToDate = DateTime.Parse(strTodate);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
                string strAlert = rm.GetString("Alert_FromDateShouldLesser");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", "alert('" + strAlert + "');", true);
            }
        }
        catch
        {
        }
    }
    public void CompareCurrentdate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;

            if (day >= 0)
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
                string strAlert = rm.GetString("Alert_From_Date_Should_not_be_lesser_than_current_date", ci);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", "alert('" + strAlert + "');", true);
            }
        }
        catch
        {
        }
    }
    public void CompareTodate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = txtToDate.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day >= 0)
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
                string strAlert = rm.GetString("Alert_To_Date_Should_not_be_lesser_than_current_date", ci);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", "alert('" + strAlert + "');", true);
            }
        }
        catch
        {
        }
    }
    private void UserRights()
    {
        Cls_CheckUser chck = new Cls_CheckUser();
        DataSet ds = chck.checkRights(53, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("Organization_NewsView.aspx");
        }
        else
        {
        }
    }
    #endregion

    #region Button Click Events
    protected void btn_save_Click(object sender, EventArgs e)
    {
        int active = 0;
        int leftPos = 0;
        int rightPos = 0;
        string strFileName = "";
        if (this.cbActive.Checked)
        {
            active = 1;
        }
        if (this.rbleftNews.Checked)
        {
            leftPos = 1;
        }
        if (this.rbRightNews.Checked)
        {
            rightPos = 1;
        }
        if (fp_Banner.FileName != "")
        {
            strLocation = Convert.ToString(ViewState["strLocation"]);
            if (System.IO.File.Exists(strLocation))
            {
                System.IO.File.Delete(strLocation);
            }
            else
            {
            }
            strFileName = fp_Banner.FileName;
            int findex = fp_Banner.FileName.LastIndexOf(".");
            string strReplace = strFileName.Substring(0, findex);
            strFileName = strFileName.Replace(strReplace, Convert.ToString(Session["nId"]));
        }
        else
        {
        }
        string gstrDateFormat;
        string FromDate, ToDate;
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        FromDate = objCommon.DateConvertion(gstrDateFormat, this.txtDate.Text);
        ToDate = objCommon.DateConvertion(gstrDateFormat, this.txtToDate.Text);
        DateTime dt1 = DateTime.Now.Date;
        DateTime dt2 = Convert.ToDateTime(ToDate);
        if (dt2 < dt1)
        {
            lblError.Text = rm.GetString("TemporaryWeekDaySetting_PastDate", ci);
            return;
        }
        news.insertNews(FromDate, ToDate, Server.HtmlEncode(this.txtTitle.Text), Server.HtmlEncode(this.txtDescription.Text), active, strFileName, leftPos, rightPos, 0, 0, Convert.ToString(Session["EmpAutoID"]), "U", Convert.ToInt16(Session["nId"]));
        if (fp_Banner.FileName != "")
        {
            string strSource = fp_Banner.PostedFile.FileName;
            string strDestination = AppDomain.CurrentDomain.BaseDirectory + "Banners\\" + strFileName;
            File.Copy(strSource, strDestination);
        }
        Session["nId"] = null;
        string str = "alert('" + rm.GetString("Alert_NewsEvents_Saved_Successfully", ci) + ".');location.replace('Organization_NewsView.aspx');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Organization_NewsView.aspx");
    }
    #endregion
}
