using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NBear.Common;
using Aspxcn.function;
using System.IO;

public partial class _Default : NBear.Web.UI.Page
{
    //返回页面显示数
    protected Int32 PageSize
    {
        get
        {
            //设置默认标题显示页数
            return 15;
        }
    }

    protected Int32 ReplyPageSize
    {
        get
        {
            //设置默认回复显示页数
            return 5;
        }
    }

    //返回当前用户权限名称
    protected string GetUserName
    {
        get
        {
            Response.Expires = -1;
            if (string.IsNullOrEmpty(User.Identity.Name))
            {
                return "guest";
            }
            else
            {
                return User.Identity.Name;
            }
        }
    }

    protected UbbCode ubb;
    protected static Aspxcn.ServiceImpls.MessageService cmd = new Aspxcn.ServiceImpls.MessageService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //保存排序状态
            ViewState["sortName"] = "IsTop desc,_Date desc";

            if (Request.QueryString["uid"] != null)
            {
                ViewTopic(Request.QueryString["uid"].ToString());
            }
            else
            {
                LoadMessagePage("1", catalogName.CommandArgument,"0");
            }
            //检测用户登录状态的处理
            LanguageName.Text = HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"].ToLower().Split(',')[0];
            UserIP.Text = HttpContext.Current.Request.ServerVariables["REMOTE_HOST"].ToLower();
            Browers.Text = HttpContext.Current.Request.Browser.Type.ToString();
            if (GetUserName != "guest")
            {
                loginPanel.Visible = false;
                userInfoPanel.Visible = true;
                guestPost.Visible = false;
                ViewUserName.Text = GetUserName;
                regUser.Text = "修改资料";
                FindPass.Text = "修改密码";
                regUser.OnClientClick = "showModalDialog('changeInfo.aspx',window,'dialogWidth:250pt;dialogHeight:250pt;status:0');";
                FindPass.OnClientClick = "showModalDialog('changePass.aspx',window,'dialogWidth:250pt;dialogHeight:250pt;status:0');";
            }
            if (Request.Cookies["AspxCnAdmin"] != null)
            {
                getCatalogList("admin");
            }
            else
            {
                getCatalogList("user");
            }
            getClassList(userCatalogList.SelectedValue);
            SerialNumberValidator1.Create();
        }
    }

    //根据读从数据库取到的主题及类别生成jdmenu动态菜单
    protected override void OnInit(EventArgs e)
    {
        Aspxcn.Entities.Catalog[] Catalogarrary = cmd.GetCatalog("admin");
        HtmlGenericControl ul = new HtmlGenericControl("ul");
        ul.Attributes["class"] = "jd_menu";
        for (int i = 0; i < Catalogarrary.Length; i++)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            Anthem.LinkButton btn = new Anthem.LinkButton();
            btn.Text = Catalogarrary[i].Name + " &raquo;";
            btn.CommandName = Catalogarrary[i].Name;
            btn.CommandArgument = Catalogarrary[i].id.ToString();
            btn.ID = "menulist"+i.ToString();
            btn.ToolTip = "全部分类";
            btn.Command += new CommandEventHandler(this.Select_Catalog);
            li.Controls.Add(btn);
            Aspxcn.Entities.Class[] Classarrary = cmd.GetClass(Catalogarrary[i].id);
            if (i == 0)
            {
                catalogName.Text = Catalogarrary[i].Name;
                catalogName.CommandName = Catalogarrary[i].Name;
                catalogName.CommandArgument = Catalogarrary[i].id.ToString();
                catalogName.Command += new CommandEventHandler(this.catalogName_Command);
                className.CommandArgument = "0";
                className.Text = "全部分类";
            }
            HtmlGenericControl ul2 = new HtmlGenericControl("ul");
            if (Classarrary.Length > 0)
            {
               
                for (int j = 0; j < Classarrary.Length; j++)
                {
                    HtmlGenericControl li2 = new HtmlGenericControl("li");
                    Anthem.LinkButton btn2 = new Anthem.LinkButton();
                    btn2.Text = Classarrary[j].Name;
                    btn2.CommandName = Catalogarrary[i].id.ToString();
                    btn2.CommandArgument = Classarrary[j].id.ToString();
                    btn2.Command += new CommandEventHandler(this.Select_Class);
                    li2.Controls.Add(btn2);
                    ul2.Controls.Add(li2);
                }
            }
            li.Controls.Add(ul2);
            ul.Controls.Add(li);
        }
        Menulist.Controls.Add(ul);
        base.OnInit(e);
    }

    //返回分类列表
    protected void getClassList(string catalogid)
    {
        userClassList.Items.Clear();
        Aspxcn.Entities.Class[] Classarrary = cmd.GetClass(Int32.Parse(catalogid));
        for (int i = 0; i < Classarrary.Length; i++)
        {
            userClassList.Items.Add(new ListItem(Classarrary[i].Name, Classarrary[i].id.ToString()));
        }
    }

    //返回主题列表
    protected void getCatalogList(string user)
    {
        userCatalogList.Items.Clear();
        Aspxcn.Entities.Catalog[] Catalogarrary;
        if (user=="admin")
        {
            Catalogarrary = cmd.GetCatalog("admin");
        }
        else
        {
            Catalogarrary = cmd.GetCatalog("user");
        }
        for (int i = 0; i < Catalogarrary.Length; i++)
        {
            userCatalogList.Items.Add(new ListItem(Catalogarrary[i].Name, Catalogarrary[i].id.ToString()));
        }
    }

    //重载-用于大小类别的转换显示
    protected void LoadMessagePage(string pageID, string catalogName,string className)
    {
        try
        {
            topicCount.Text = cmd.GetMaxPage(Int32.Parse(catalogName), Int32.Parse(className)).ToString();
            maxPage.Text = requestMaxpage(Int32.Parse(topicCount.Text));
            if ( Int32.Parse(maxPage.Text) < Int32.Parse(pageID))
            {
                pageID = maxPage.Text;
            }

            curPage.Text = pageID;
            jump.Text = pageID;
            ListGrid.DataSource = cmd.GetAllContent(this.PageSize, Int32.Parse(pageID), Int32.Parse(catalogName), Int32.Parse(className), ViewState["sortName"].ToString());
            ListGrid.DataBind();
        }
        catch
        {
            ListGrid.DataBind();
        }
    }

    //重载-用于搜索状态的显示
    protected void LoadMessagePage(string pageID,string catalogName,string className,string searchtext)
    {
        try
        { 
            if (searchType.SelectedValue == "1")
            {
                topicCount.Text = cmd.GetMaxPage(Int32.Parse(catalogName), Int32.Parse(className), searchtext.Trim(),"topic").ToString();
                maxPage.Text = requestMaxpage(Int32.Parse(topicCount.Text));

                if (Int32.Parse(maxPage.Text) < Int32.Parse(pageID))
                {
                    pageID = maxPage.Text;
                }
                curPage.Text = pageID;
                jump.Text = pageID;

                ListGrid.DataSource = cmd.GetAllContent(this.PageSize, Int32.Parse(pageID), Int32.Parse(catalogName), Int32.Parse(className), searchtext,"topic", ViewState["sortName"].ToString());
                ListGrid.DataBind();
            }
            else
            {
                topicCount.Text = cmd.GetMaxPage(Int32.Parse(catalogName), Int32.Parse(className), searchtext.Trim(),"user").ToString();
                maxPage.Text = requestMaxpage(Int32.Parse(topicCount.Text));

                if (Int32.Parse(maxPage.Text) < Int32.Parse(pageID))
                {
                    pageID = maxPage.Text;
                }
                curPage.Text = pageID;
                jump.Text = pageID;
                ListGrid.DataSource = cmd.GetAllContent(this.PageSize, Int32.Parse(pageID), Int32.Parse(catalogName), Int32.Parse(className), searchtext,"user", ViewState["sortName"].ToString());
                ListGrid.DataBind();
            }
            
        }
        catch
        {
            ListGrid.DataBind();
        }
    }

    //加载收藏夹
    protected void LoadFavPage(string UserName)
    {
        topicCount.Text = cmd.GetFavMaxPage(UserName).ToString();
        maxPage.Text = "1";
        curPage.Text="1";

        className.Text = "收藏夹";

        Aspxcn.Entities.fav[] favlist= cmd.GetFavContent(this.PageSize,UserName);
        
        int rs=favlist.Length;
        Aspxcn.Entities.Lists[] ls =new Aspxcn.Entities.Lists[rs];
        for (int i = 0; i <=rs-1 ; i++)
        {
            ls[i] = favlist[i].Lists;
        }
        ListGrid.DataSource = ls;
        ListGrid.DataBind();
    }

    //加载贴子
    protected void loadTopic(Int32 listid, Int32 pageID, bool pageClick)
    {
        ubb = new UbbCode();
        topicCount.Text = (cmd.GetMaxPage(Int32.Parse(showListText.CommandArgument))+1).ToString();
        maxPage.Text = requestMaxReplypage(Int32.Parse(topicCount.Text)-1);
        
        if (maxPage.Text == "0")
            maxPage.Text = "1";

        if (Int32.Parse(maxPage.Text) < pageID)
        {
            pageID = Int32.Parse(maxPage.Text);
        }

        curPage.Text = pageID.ToString();
        jump.Text = pageID.ToString();

        try
        {
            if (!pageClick)
            {
                ShowTopic.DataSource = cmd.GetTitle(listid);
                ShowTopic.DataBind();
            }
            showReply.DataSource = cmd.GetReplyPage(listid,this.ReplyPageSize,pageID);
            cmd.Update_Topic(listid, "hits");
            showReply.DataBind();
        }
        catch
        {
            ShowTopic.DataBind();
            showReply.DataBind();
        }
    }

    //处理提交内容
    protected void AddBtn_Click(object sender, EventArgs e)
    {
        try
        {
            switch (AddBtn.Text)
            {
                case "发表主题":
                    if (!loginPanel.Visible)
                    {
                        Aspxcn.Entities.Lists content = new Aspxcn.Entities.Lists();
                        content._Date = DateTime.Now;
                        content.IsTop = false;
                        content._Content = Server.HtmlEncode(wipeScript(postContent.Text));
                        content.Type = 1;
                        content.Catalog = Int32.Parse(userCatalogList.SelectedValue);
                        content.Class = Int32.Parse(userClassList.SelectedValue);
                        content._Title = userTitle.Text;
                        content.IP = Page.Request.UserHostAddress;
                        content.UserName = GetUserName;
                        content.Email = Membership.GetUser(GetUserName).Comment;
                        userTitle.Text = "";
                        cmd.insert_Content(content);
                        ChangeView();
                        LoadMessagePage(curPage.Text, userCatalogList.SelectedValue, userClassList.SelectedValue);
                        Anthem.Manager.AddScriptForClientSideEval("alert('您的主题已经成功发表!')");
                        postContent.Text = "";
                        postContentpanel.AutoUpdateAfterCallBack = true;
                    }
                    else
                    {
                        if (SerialNumberValidator1.CheckSN(imageValidator.Text))
                        {
                            Aspxcn.Entities.Lists content = new Aspxcn.Entities.Lists();
                            content._Date = DateTime.Now;
                            content.IsTop = false;
                            content._Content = Server.HtmlEncode(wipeScript(postContent.Text));
                            content.Type = 1;
                            content.Catalog = Int32.Parse(userCatalogList.SelectedValue);
                            content.Class = Int32.Parse(userClassList.SelectedValue);
                            content._Title = userTitle.Text;
                            content.IP = Page.Request.UserHostAddress;

                            if (GuestName.Text == "")
                                GuestName.Text = "匿名";

                            content.UserName = "[访客]" + GuestName.Text;
                            content.Email = guestEmail.Text;
                            userTitle.Text = "";
                            cmd.insert_Content(content);
                            ChangeView();
                            LoadMessagePage(curPage.Text, userCatalogList.SelectedValue, userClassList.SelectedValue);
                            Anthem.Manager.AddScriptForClientSideEval("alert('您的主题已经成功发表!')");
                            postContent.Text = "";
                            postContentpanel.AutoUpdateAfterCallBack = true;
                            SerialNumberValidator1.Create();
                        }
                        else
                        {
                            Anthem.Manager.AddScriptForClientSideEval("alert('验证码不正确,请重新尝试!')");
                            imageValidator.Text = "";
                        }
                    }

                    break;
                case "回复主题":
                    if (!loginPanel.Visible)
                    {
                        Aspxcn.Entities.Reply replyTopic = new Aspxcn.Entities.Reply();
                        replyTopic._Content = Server.HtmlEncode(wipeScript(postContent.Text));
                        replyTopic._Title = userTitle.Text;
                        replyTopic._DateTime = DateTime.Now;
                        replyTopic.Email = Membership.GetUser(GetUserName).Email;
                        replyTopic.TopIc_ID = Int32.Parse(showListText.CommandArgument);
                        replyTopic.UserName = GetUserName;
                        replyTopic.IP = Page.Request.UserHostAddress;
                        cmd.insert_ReplyContent(replyTopic);
                        cmd.Update_Topic(Int32.Parse(showListText.CommandArgument), "Reply");
                        loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(requestMaxReplypage(Int32.Parse(topicCount.Text))), true);
                        userTitle.Text = "";
                        postContent.Text = "";
                        postContentpanel.AutoUpdateAfterCallBack = true;
                        Anthem.Manager.AddScriptForClientSideEval("alert('您的回复已经成功发表!')");
                    }
                    else
                    {
                        if (SerialNumberValidator1.CheckSN(imageValidator.Text))
                        {
                            Aspxcn.Entities.Reply replyTopic = new Aspxcn.Entities.Reply();
                            replyTopic._Content = Server.HtmlEncode(wipeScript(postContent.Text));
                            replyTopic._Title = userTitle.Text;
                            replyTopic._DateTime = DateTime.Now;
                            replyTopic.Email = guestEmail.Text;
                            replyTopic.TopIc_ID = Int32.Parse(showListText.CommandArgument);
                            if (GuestName.Text == "")
                                GuestName.Text = "匿名";
                            replyTopic.UserName = "[访客]" + GuestName.Text;
                            replyTopic.IP = Page.Request.UserHostAddress;

                            cmd.insert_ReplyContent(replyTopic);
                            cmd.Update_Topic(Int32.Parse(showListText.CommandArgument), "Reply");

                            loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(requestMaxReplypage(Int32.Parse(topicCount.Text))), true);
                            userTitle.Text = "";
                            postContent.Text = "";
                            postContentpanel.AutoUpdateAfterCallBack = true;
                            SerialNumberValidator1.Create();
                            Anthem.Manager.AddScriptForClientSideEval("alert('您的回复已经成功发表!')");
                        }
                        else
                        {
                            Anthem.Manager.AddScriptForClientSideEval("alert('验证码不正确,请重新尝试!')");
                            imageValidator.Text = "";
                        }
                    }
                    break;
                case "修改主题":
                    cmd.Update_Content(showListText.CommandArgument, userTitle.Text, Server.HtmlEncode(wipeScript(postContent.Text)), userCatalogList.SelectedValue, userClassList.SelectedValue);
                    loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(curPage.Text), false);
                    userTitle.Text = "";
                    postContent.Text = "";
                    postContentpanel.AutoUpdateAfterCallBack = true;
                    userTitleEmptyCheck.Enabled = false;
                    AddBtn.Text = "回复主题";
                    Anthem.Manager.AddScriptForClientSideEval("alert('已经成功修改主题!')");

                    break;
                case "修改回复":
                    cmd.Update_Reply(Int32.Parse(AddBtn.CommandArgument), userTitle.Text, Server.HtmlEncode(wipeScript(postContent.Text)));
                    loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(curPage.Text), false);
                    userTitle.Text = "";
                    postContent.Text = "";
                    postContentpanel.AutoUpdateAfterCallBack = true;
                    AddBtn.Text = "回复主题";
                    AddBtn.CommandArgument = "";
                    Anthem.Manager.AddScriptForClientSideEval("alert('已经成功修改回复!')");
                    break;
                default:
                    break;
            }
        }
        catch
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('数据提交出现错误，请重试!')");
           
        }
    }

    //处理类别选择事件
    protected void Select_Catalog(object sender, CommandEventArgs e)
    {
        catalogName.Text = e.CommandName;
        catalogName.CommandArgument = e.CommandArgument.ToString();
        if (className.Text == "搜索结果")
        {
            className.Text = "全部分类";
            className.CommandName = "全部分类";
            className.CommandArgument = "1";
        }
        showbottom.Visible = false;
        ListGrid.Visible = true;
        showListText.Visible = false;
        returnlist.Visible = false;
        ShowTopic.Visible = false;
        showReply.Visible = false;
        try
        {
            userCatalogList.SelectedValue = e.CommandArgument.ToString();
            getClassList(userCatalogList.SelectedValue);
        }
        catch
        {
        }
        AddBtn.Text = "发表主题";
        publish.Text = "发表主题";
        SearchText.Text = "";
        className.Text = "全部分类";
        className.CommandName = "全部分类";
        className.CommandArgument = "0";
        LoadMessagePage("1", e.CommandArgument.ToString(), className.CommandArgument);
    }

    //选择分类
    protected void Select_Class(object sender, CommandEventArgs e)
    {
        showbottom.Visible = false;
        ListGrid.Visible = true;
        showListText.Visible = false;
        returnlist.Visible = false;
        ShowTopic.Visible = false;
        showReply.Visible = false;
        AddBtn.Text = "发表主题";
        publish.Text = "发表主题";
        SearchText.Text = "";
        className.Text = (sender as LinkButton).Text;
        className.CommandArgument = e.CommandArgument.ToString();
        catalogName.Text = cmd.GetCatalogName(Int32.Parse(e.CommandName));
        catalogName.CommandArgument=e.CommandName;
        
        try
        {
            userCatalogList.SelectedValue = e.CommandName ;
            getClassList(userCatalogList.SelectedValue);
            userClassList.SelectedValue = e.CommandArgument.ToString();
        }
        catch
        {

        }
        LoadMessagePage("1", e.CommandName, e.CommandArgument.ToString());
    }

    //转换显示
    protected void ChangeView()
    {
        className.Text = userClassList.SelectedItem.Text;
        className.CommandName = userClassList.SelectedItem.Text;
        className.CommandArgument = userClassList.SelectedValue;
        catalogName.Text = userCatalogList.SelectedItem.Text;
        catalogName.CommandArgument = userCatalogList.SelectedValue;
        if (!userTitleEmptyCheck.Enabled)
            userTitleEmptyCheck.Enabled = true;
    }

    //列表更新操作
    protected void ListGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
        }
    }

    //返回按键操作
    protected void Return_List(object sender, CommandEventArgs e)
    {
        showbottom.Visible = false;
        ListGrid.Visible = true;
        showListText.Visible = false;
        returnlist.Visible = false;
        ShowTopic.DataBind();
        showReply.DataBind();
        ShowTopic.Visible = false;
        showReply.Visible = false;
        AddBtn.Text = "发表主题";
        publish.Text = "发表主题";
        SearchText.Text = "";
        userTitleEmptyCheck.Enabled = true;
        //if (Request.Cookies["AspxCnAdmin"] != null || className.CommandArgument != "1" & GetUserName != "guest")
        //{
            //userClassList.SelectedValue = className.CommandArgument;
        //}
        //Anthem.Manager.AddScriptForClientSideEval("alert('" + className.CommandArgument + "')");
        LoadMessagePage(e.CommandArgument.ToString(), catalogName.CommandArgument, className.CommandArgument);
    }

    //处理搜索事件
    protected void Search_Command(object sender, CommandEventArgs e)
    {
        if (SearchText.Text != string.Empty & showListText.Visible == false)
        {
            className.Text = "搜索结果";
            LoadMessagePage("1", catalogName.CommandArgument, className.CommandArgument, Server.HtmlEncode(SearchText.Text.Trim()));
        }
    }

    //上一页
    protected void prv_Click(object sender, EventArgs e)
    {
        if (!showListText.Visible)
        {
            if (SearchText.Text == string.Empty)
            {
                LoadMessagePage(Convert.ToString(Int32.Parse(curPage.Text) - 1), catalogName.CommandArgument, className.CommandArgument);

            }
            else
            {
                LoadMessagePage(Convert.ToString(Int32.Parse(curPage.Text) - 1), catalogName.CommandArgument, className.CommandArgument, SearchText.Text);
            }
        }
        else
        {
            loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(curPage.Text) - 1, true);

        }
    }

    //下一页
    protected void next_Click(object sender, EventArgs e)
    {
        if (!showListText.Visible)
        {

            if (SearchText.Text == string.Empty)
            {
                LoadMessagePage(Convert.ToString(Int32.Parse(curPage.Text) + 1), catalogName.CommandArgument, className.CommandArgument);
            }
            else
            {
                LoadMessagePage(Convert.ToString(Int32.Parse(curPage.Text) + 1), catalogName.CommandArgument, className.CommandArgument, SearchText.Text);
            }
        }
        else
        {
            loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(curPage.Text) + 1, true);
        }

    }

    //首页
    protected void index_Click(object sender, EventArgs e)
    {
        if (!showListText.Visible)
        {
            if (SearchText.Text == string.Empty)
            {
                LoadMessagePage("1", catalogName.CommandArgument, className.CommandArgument);
            }
            else
            {
                LoadMessagePage("1", catalogName.CommandArgument, className.CommandArgument, SearchText.Text);
            }
        }
        else
        {
            loadTopic(Int32.Parse(showListText.CommandArgument),1, true);

        }
    }

    //尾页
    protected void last_Click(object sender, EventArgs e)
    {
        if (!showListText.Visible)
        {
            if (SearchText.Text == string.Empty)
            {
                LoadMessagePage(maxPage.Text, catalogName.CommandArgument, className.CommandArgument);
            }
            else
            {
                LoadMessagePage(maxPage.Text, catalogName.CommandArgument, className.CommandArgument, SearchText.Text);
            }
        }
        else
        {
            loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(maxPage.Text), true);
        }
    }

    //跳转页面
    protected void GO_Click(object sender, EventArgs e)
    {
        if (!showListText.Visible)
        {
            if (SearchText.Text == string.Empty)
            {
                LoadMessagePage(jump.Text, catalogName.CommandArgument, className.CommandArgument);
            }
            else
            {
                LoadMessagePage(jump.Text, catalogName.CommandArgument, className.CommandArgument, SearchText.Text);
            }
        }
        else
        {
            loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(jump.Text), true);

        }
    }


    //排序处理
    protected void ListGrid_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["sortName"].ToString() == (e.SortExpression + " desc"))
        {
            ViewState["sortName"] = e.SortExpression;
        }
        else
        {
            ViewState["sortName"] = e.SortExpression + " desc";
        }

        if (SearchText.Text == string.Empty)
        {
            LoadMessagePage(curPage.Text, catalogName.CommandArgument, className.CommandArgument);
        }
        else
        {
            LoadMessagePage(curPage.Text, catalogName.CommandArgument, className.CommandArgument, SearchText.Text);
        }
    }

    //页面重载
    protected void Page_PreRender(object sender, EventArgs e)
    {

        if (Int32.Parse(curPage.Text) < Int32.Parse(maxPage.Text))
        {
            next.Enabled = true;
            last.Enabled = true;
            next1.Enabled = true;
            next1.EnableCallBack = true;
            last1.Enabled = true;
            last1.EnableCallBack = true;
        }
        else
        {
            next.Enabled = false;
            last.Enabled = false;
            next1.Enabled = false;
            next1.EnableCallBack = false;
            last1.Enabled = false;
            last1.EnableCallBack = false;

        }

        if (Int32.Parse(curPage.Text) > 1)
        {
            prv.Enabled = true;
            index.Enabled = true;
            prv1.Enabled = true;
            prv1.EnableCallBack = true;
            index1.Enabled = true;
            index1.EnableCallBack = true;
        }
        else
        {
            prv.Enabled = false;
            index.Enabled = false;
            prv1.Enabled = false;
            prv1.EnableCallBack = false;
            index1.Enabled = false;
            index1.EnableCallBack = false;
        }
    }


    //删除记录
    protected void DeleteBtn_Click(object sender, GridViewDeleteEventArgs e)
    {
        if ((ListGrid.Rows[e.RowIndex].FindControl("UserName") as LinkButton).Text == GetUserName||className.Text=="收藏夹" )
        {
            if (className.Text == "收藏夹")
            {
         
                cmd.Delete_Fav(int.Parse(ListGrid.DataKeys[e.RowIndex].Value.ToString()));
                LoadFavPage(GetUserName);
            }
            else
            {
                cmd.Delete_Content(int.Parse(ListGrid.DataKeys[e.RowIndex].Value.ToString()));

                if (SearchText.Text == string.Empty)
                {
                    LoadMessagePage(curPage.Text, catalogName.CommandArgument, className.CommandArgument);
                }
                else
                {
                    LoadMessagePage(curPage.Text, catalogName.CommandArgument, className.CommandArgument, SearchText.Text.Trim());
                }
            }
            
        }
        else
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('您无权删除别人发表的主题!')");
        }
    }

    //清除内空输入框
    protected void Button1_Click(object sender, EventArgs e)
    {
        //System.Threading.Thread.Sleep(5000);
        postContent.Text = "";
        postContentpanel.AutoUpdateAfterCallBack = true;
        //postContent.UpdateAfterCallBack = true;
    }

    protected void FavBtn_Command(object sender, CommandEventArgs e)
    {
        LoadFavPage(GetUserName);
        SearchText.Text = "";
    }

    protected void favorite_Command(object sender, CommandEventArgs e)
    {
        if (cmd.GetFavPost(User.Identity.Name) <= 15)
        {
            Aspxcn.Entities.fav Add_Fav = new Aspxcn.Entities.fav();
            Add_Fav.favID = Convert.ToInt32(e.CommandArgument);
            Add_Fav.UserName = GetUserName;
            Add_Fav.Add_DateTime = DateTime.Now;
            cmd.insert_Fav(Add_Fav);
        }
        else
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('添加失败!您的收藏夹已经超过15条记录的限制,请删除旧记录后再重新尝试。')");
        }

    }

    //显示帖子状态图标
    public string ShowStatusImg(DateTime returnDate, bool istop, Int32 Reply)
    {
        string temp = "";
        if (DateTime.Now - returnDate < TimeSpan.Parse("3"))
        {
            temp = "images/status_new.gif";
        }
        else if (DateTime.Now - returnDate > TimeSpan.Parse("30"))
        {
            temp = "images/status_old.gif";
        }
        else
        {
            temp = "images/status_normal.gif";
        }

        if (Reply > 10)
        {
            temp = "images/status_hot.gif";
        }

        if (istop)
        {
            temp = "images/status_top.gif";
        }
        return temp;
    }

    
    protected void ListTitle_Command(object sender, CommandEventArgs e)
    {
        ViewTopic(e.CommandArgument.ToString());
    }

    //显示指定帖子
    protected void ViewTopic(string ListID)
    {
        ListGrid.DataBind();
        ListGrid.Visible = false;
        showbottom.Visible = true;
        showListText.Visible = true;
        showListText.CommandArgument = ListID;
        returnlist.Visible = true;
        returnlist.CommandArgument = curPage.Text;
        AddBtn.Text = "回复主题";
        publish.Text = "回复主题";
        ShowTopic.Visible = true;
        showReply.Visible = true;
        if (userTitleEmptyCheck.Enabled)
            userTitleEmptyCheck.Enabled = false;

        loadTopic(Int32.Parse(ListID),1,false);
    }

    protected void showListText_Click(object sender, EventArgs e)
    {
        if (showListText.Text == "[隐藏回复]")
        {
            showReply.Visible = false;
            showListText.Text = "[显示回复]";
        }
        else
        {
            showReply.Visible = true;
            showListText.Text = "[隐藏回复]";
        }

    }

    //用户登录操作
    protected void LoginButton_Click(object sender, ImageClickEventArgs e)
    {
        if (Membership.ValidateUser(Login1.UserName.Trim(), Login1.Password))
        {
            FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
            loginPanel.Visible = false;
            userInfoPanel.Visible = true; ;
            guestPost.Visible = false;
            regUser.Text = "修改资料";
            FindPass.Text = "修改密码";
            ViewUserName.Text = Login1.UserName;
            Anthem.Manager.RegisterClientScriptInclude("Key", "css/user.js");
            regUser.OnClientClick = "showModalDialog('changeInfo.aspx',window,'dialogWidth:250pt;dialogHeight:250pt;status:0');";
            FindPass.OnClientClick = "showModalDialog('changePass.aspx',window,'dialogWidth:250pt;dialogHeight:250pt;status:0');";
            if (Membership.GetUser(Login1.UserName).Comment == "admin")
            {
                Response.Cookies["AspxCnAdmin"].Value = Login1.UserName.Trim();
                getCatalogList("admin");
            }
            else
            {
                getCatalogList("user");
            }
            
            getClassList(userCatalogList.SelectedValue);
        }
        else
        {
            (Login1.FindControl("FailureText") as Label).Text = "用户名或密码不正确，请重试！";
        }
    }

    //用户注销操作
    protected void loginOut_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        this.Page.Response.Clear();
        this.Page.Response.StatusCode = 200;
        loginPanel.Visible = true;
        userInfoPanel.Visible = false;
        guestPost.Visible = true;
        regUser.Text = "注册新用户";
        FindPass.Text = "忘记密码";
        regUser.OnClientClick = "showModalDialog('reg.aspx','info','dialogWidth:250pt;dialogHeight:250pt;status:0');";
        FindPass.OnClientClick = "showModalDialog('findPassword.aspx','pass','dialogWidth:250pt;dialogHeight:250pt;status:0');";
        if (Request.Cookies["AspxCnAdmin"]!=null)
        {
            Response.Cookies["AspxCnAdmin"].Expires = DateTime.Now;
        }
        getCatalogList("user");
        getClassList(userCatalogList.SelectedValue);
    }

    //验证码转换显示
    protected void changeImg_Click(object sender, EventArgs e)
    {
        SerialNumberValidator1.Create();    
    }

    //返回回复页数
    protected string requestMaxReplypage(Int32 rowCount)
    {
        if (rowCount % this.ReplyPageSize == 0)
        {
            return Convert.ToString(rowCount / this.ReplyPageSize);
        }
        else
        {
            return Convert.ToString((rowCount / this.ReplyPageSize) + 1);
        }
    }
    //返回主题页数
    protected string requestMaxpage(Int32 rowCount)
    {
        if (rowCount % this.PageSize == 0)
        {
            return Convert.ToString(rowCount / this.PageSize);
        }
        else
        {
            return Convert.ToString((rowCount / this.PageSize) + 1);
        }
    }

    //显示管理员选项
    protected bool showEditCheck(string userName)
    {
        if (userName == GetUserName | Request.Cookies["AspxCnAdmin"] != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected void TopicEdit_Command(object sender, CommandEventArgs e)
    {
        AddBtn.Text = "修改主题";
        Aspxcn.Entities.Lists temp=cmd.GetTopic(Convert.ToInt32(e.CommandArgument));
        postContent.Text = Server.HtmlDecode(temp._Content);
        postContentpanel.AutoUpdateAfterCallBack = true;
        userTitle.Text =temp._Title;
        try
        {
            userCatalogList.SelectedValue = temp.Catalog.ToString();
            getClassList(userCatalogList.SelectedValue);
            userClassList.SelectedValue = temp.Class.ToString();
        }
        catch
        {
        }
        if(!userTitleEmptyCheck.Enabled)
            userTitleEmptyCheck.Enabled=true;
    }

    //删除主题操作
    protected void delTopic_Command(object sender, CommandEventArgs e)
    {
        cmd.Delete_Content(Convert.ToInt32(e.CommandArgument));
        showbottom.Visible = false;
        ListGrid.Visible = true;
        showListText.Visible = false;
        returnlist.Visible = false;
        ShowTopic.Visible = false;
        showReply.Visible = false;
        AddBtn.Text = "发表主题";
        publish.Text = "发表主题";
        SearchText.Text = "";
        userTitleEmptyCheck.Enabled = true;
        LoadMessagePage(returnlist.CommandArgument.ToString(), catalogName.CommandArgument, className.CommandArgument.ToString());        
    }

    protected void ReplyEdit_Command(object sender, CommandEventArgs e)
    {
        AddBtn.Text = "修改回复";
        AddBtn.CommandArgument = e.CommandArgument.ToString();
        Aspxcn.Entities.Reply temp = cmd.GetReply(Convert.ToInt32(e.CommandArgument));
        postContent.Text = Server.HtmlDecode(temp._Content);
        userTitle.Text = temp._Title;
        postContentpanel.AutoUpdateAfterCallBack = true;
    }

    //删除回复操作
    protected void delReply_Command(object sender, CommandEventArgs e)
    {
        cmd.Delete_Reply(Convert.ToInt32(e.CommandArgument),Int32.Parse(showListText.CommandArgument));
        loadTopic(Int32.Parse(showListText.CommandArgument), Int32.Parse(requestMaxReplypage(Int32.Parse(topicCount.Text)-1)), true);
    }

    //显示即时信息操作
    protected void getMsgBtn_Click(object sender, EventArgs e)
    {
        if (GetUserName!="guest")
        {
            int msgCount = cmd.GetMsgRead(GetUserName);
            if (msgCount>0)
            {
                Anthem.Manager.AddScriptForClientSideEval("getMessagebox('<center>您有<font color=red>" + msgCount.ToString() + "</font>封未读短信息</center>');");
            }
            else
            {
                Anthem.Manager.AddScriptForClientSideEval("getMessagebox_false();");
            }
        }
    }

    //搜索操作
    protected void User_topic_list_Click(object sender, EventArgs e)
    {
        SearchText.Text = ViewUserName.Text.Trim();
        searchType.SelectedValue="2";
        className.Text = "搜索结果";
        LoadMessagePage("1", catalogName.CommandArgument, className.CommandArgument, Server.HtmlEncode(SearchText.Text.Trim()));
    }

    //置顶操作
    protected void GoTop_command(object sender, CommandEventArgs e)
    {
        cmd.Update_Topic(Convert.ToInt32(e.CommandArgument), "istop");
        
        if ((ShowTopic.Items[0].FindControl("GoTop") as LinkButton).Text == "置顶")
        {
            (ShowTopic.Items[0].FindControl("GoTop") as LinkButton).Text = "解除置顶";
        }
        else
        {
            (ShowTopic.Items[0].FindControl("GoTop") as LinkButton).Text = "置顶";
        }
        Anthem.Manager.AddScriptForClientSideEval("alert('操作成功!')");
    }

    //精华操作
    protected void recommend_command(object sender, CommandEventArgs e)
    {
        cmd.Update_Content(e.CommandArgument.ToString(), "1");
        Anthem.Manager.AddScriptForClientSideEval("alert('操作成功!')");
    }

    protected void userCatalogList_SelectedIndexChanged(object sender, EventArgs e)
    {
        getClassList(userCatalogList.SelectedValue);
    }

    protected void catalogName_Command(object sender, CommandEventArgs e)
    {
        className.Text = "全部分类";
        className.CommandArgument = "0";
        LoadMessagePage("1", e.CommandArgument.ToString(), className.CommandArgument);
    }

    //过滤所有script标记
    protected string wipeScript(string html)
    {
        System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        html = regex1.Replace(html, ""); //过滤<script></script>标记
        html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
        html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
        html = regex4.Replace(html, ""); //过滤iframe
        html = regex5.Replace(html, ""); //过滤frameset
        return html;
    }
}