using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.UI;

namespace KstCmsBase.WebPage
{
    public class ValiPage:BasePage
    {
         /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rLogin();
            InitPageErr();
           
     
        }


        /// <summary>
        /// 异步回发时，系统发生异常错误（或人工thow new 出异常，格式化信息输出，以免不能正确提示信息。
        /// 面页要继承此类或此类的派生类，并且页面要包含“ScriptManager1”控件。
        /// </summary>
        private void InitPageErr()
        {
            ScriptManager Sm = (ScriptManager)Page.FindControl("ScriptManager1");
            if (Sm != null)
            {
            
            
                StringBuilder JsStr = new StringBuilder();
                JsStr.Append("\r\nSys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender,e)\r\n");
                JsStr.Append("{\r\n");
                JsStr.Append("e.set_errorHandled(true);\r\n");
                JsStr.Append("if(e.get_error())\r\n");
                JsStr.Append("alert(e.get_error().message.replace(\"Sys.WebForms.PageRequestManagerServerErrorException:\",\"\"));\r\n");
                JsStr.Append("});\r\n");


                ScriptManager.GetCurrent(this).AllowCustomErrorsRedirect = false;
                ScriptManager.GetCurrent(this).EnablePartialRendering = true;
                if (!ClientScript.IsStartupScriptRegistered("AjaxMessge"))
                    ClientScript.RegisterStartupScript(GetType(), "AjaxMessge", "<script language=\"javascript\" type=\"text/javascript\">" + JsStr.ToString

() + "</script>");
                ScriptManager.GetCurrent(this).AsyncPostBackError += new EventHandler<AsyncPostBackErrorEventArgs>(ScriptManagerPage_AsyncPostBackError);
            }
        }


        protected void ScriptManagerPage_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            StringBuilder  ErrSB = new StringBuilder(e.Exception.Message);
            ErrSB.Replace("\"", "'");
            ErrSB.Replace("\r\n", "\\r\\n");
            ScriptManager.GetCurrent(this).AsyncPostBackErrorMessage = ErrSB.ToString();
        }


        /// <summary>
        /// 同一用户不能同时登陆
        /// </summary>
        private void rLogin()
        {
            Hashtable hOnline = (Hashtable)Application["Online"];
            //String LoginCookiec = Request.Cookies.Get("portalroles").Value;
            String LoginCookiec = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            if (hOnline != null)
            {
                IDictionaryEnumerator idE = hOnline.GetEnumerator();
                while (idE.MoveNext())
                {
                    if (idE.Key != null && idE.Key.ToString().Equals(Session.SessionID))
                    {
                        //already login 

                        if (idE.Value != null && LoginCookiec.Equals(idE.Value.ToString()))
                        {
                            this.Page.ClientScript.RegisterStartupScript(GetType(), "ExitSystem",
                                "<script language=\"javascript\" type=\"text/javascript\">\r\n" +
                                "if(confirm(\"你的帐号已在别处登录，你被强制退出！\\r\\n是否重新登录系统？\")){\r\n" +
                                "window.parent.top.location='" + SysRootUrl + "/KstCMS_Manager/login.aspx';} else {window.top.location=\"" + SysRootUrl + "/KstCMS_Manager/Logout.aspx\";}\r\n</script>");

                            hOnline.Remove(Session.SessionID);
                            Application.Lock();
                            Application["Online"] = hOnline;
                            Application.UnLock();
                            System.Web.Security.FormsAuthentication.SignOut();
                            return;
                        }
                        break;
                    }
                }
            }
        }
    }
}
