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          : News and Events                             Last Modified : 14-Aug-2007
* Programmer      : Senthil Kumar NP
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
****************************************************************************************************************************/
public partial class Organization_Settings_Organization_News : Base.BasePage
{
    #region Global Variable Declaration
    ResourceManager rm;
    CultureInfo ci;
    string strFromdate, strTodate, gstrDateFormat;
    cls_Common objCommon = new cls_Common();
    #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)
        {
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            objCommon.menu("Others", "53");
        }
    }
    #endregion

    #region Button Click Events
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            NewsEventData newsIns = new NewsEventData();
            int active = 0;
            int leftPos = 0;
            int rightPos = 0;
            string strFileName = "";
            string title = "";
            string description = "";
            DataSet dsYearSettingResult = new DataSet();
            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);
            DateTime dtFromDate = Convert.ToDateTime(FromDate);
            if (dt2 < dt1)
            {
                lblError.Text = rm.GetString("TemporaryWeekDaySetting_PastDate", ci);
                return;
            }
            else if (dtFromDate > dt2)
            {
                lblError.Text = rm.GetString("TemporaryWeekDaySetting_FromDateToDate", ci);
                return;
            }
            dsYearSettingResult = newsIns.CheckthedateYearSettings(dtFromDate, dt2);
            if (dsYearSettingResult.Tables[0].Rows[0]["RESULT"].ToString() == "0")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert(' Invalid Fromdate or Todate');", true);
                return;
            }
            if (rbtnNews.Checked)
            {
                if (this.cbActive.Checked)
                {
                    active = 1;
                }
                if (this.rbleftNews.Checked)
                {
                    leftPos = 1;
                }
                if (this.rbRightNews.Checked)
                {
                    rightPos = 1;
                }
                newsIns.insertNews(FromDate, ToDate, Server.HtmlEncode(this.txtTitle.Text.Trim().Replace("'", "''")), Server.HtmlEncode(this.txtDescription.Text.Trim().Replace("'", "''")), active, strFileName, leftPos, rightPos, 0, 0, Convert.ToString(Session["EmpAutoID"]), "I", 0);
            }
            else
            {
                if (this.cbActive.Checked)
                {
                    active = 1;
                }
                if (this.rbleftNews.Checked)
                {
                    leftPos = 1;
                }
                if (this.rbRightNews.Checked)
                {
                    rightPos = 1;
                }
                if (fp_Banner.FileName != "")
                {
                    strFileName = fp_Banner.FileName;
                }
                else
                {
                }
                newsIns.insertNews(FromDate, ToDate, title, description, active, strFileName, leftPos, rightPos, 0, 0, Convert.ToString(Session["EmpAutoID"]), "I", 0);
                DataSet ds = new DataSet();
                ds = newsIns.selectAllNews();
                int c = ds.Tables[0].Rows.Count - 1;
                string fileName = ds.Tables[0].Rows[c]["NewsEventImage"].ToString();
                if (fp_Banner.FileName != "")
                {
                    string strSource = fp_Banner.PostedFile.FileName;
                    string strDestination = AppDomain.CurrentDomain.BaseDirectory + "Banners\\" + fileName;
                    File.Copy(strSource, strDestination);
                }
            }
            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

    #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);
        lblImage.Text = rm.GetString("NewsEvents_Image", ci);
        lblDivNews.Text = rm.GetString("NewsEvents_News", ci);
        lblDivBanner.Text = rm.GetString("rbtn_Banner", 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);
    }
    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"]), "addPrv");
        if (ds.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("OrganizationLandingPage.aspx");
        }
        else
        {
        }
    }
    #endregion
}
