 <%@ Application Language="C#" %>
<%@ Import Namespace="Invensys.Televis.Common" %>
<%@ Import Namespace="Invensys.Televis.Common.Constants" %>
<%@ Import Namespace="Invensys.Televis.Presentation.Web.Common" %>
<%@ Import Namespace="Invensys.Televis.Business.CoreLibrary" %>

<script runat="server">    
    
    protected void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Logger.Info("TELEVIS WEB START"); 
        Logger.Info(this, "Application_Start() at " + DateTime.Now, null); 
    }

    protected void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
        Logger.Info("TELEVIS WEB END"); 
        Logger.Info(this, "Application_End() at " + DateTime.Now, null);

    }

    protected void Application_Error(object sender, EventArgs e) 
    {
        // Code that runs when an unhandled Error occurs
        Exception exception = Server.GetLastError();
        try
        {
            Logger.Error(this, "Application_Error at " + DateTime.Now, exception);
            if (HttpContext.Current.Session != null)
            {
                IEnumerator enumSessionItems = HttpContext.Current.Session.GetEnumerator();
                while (enumSessionItems.MoveNext())
                {
                    string sessionItem = (string)enumSessionItems.Current;
                    Logger.Info(this, "sessionItem: " + sessionItem, exception);
                    if (Session[SessionConstants.WEB_ERROR] != null)
                    {
                        // Show the Error
                        Exception ex = (Exception)Session[SessionConstants.WEB_ERROR];
                        Logger.Error(this, String.Concat(SessionConstants.WEB_ERROR, ": ", ex.Message), ex);
                    }
                }
            }
        }
        finally
        {
            Utilities.RedirectToErrorPage(Server.GetLastError());
        }
    }


    protected void Application_RequestBegin(object sender, EventArgs e)
    {
        //String paginaProvenienza = Request.UrlReferrer.ToString();
        //string paginaDestinazione = Request.Url.ToString();

        //if (paginaProvenienza != paginaDestinazione)
        //{            
        //    Session[SessionConstants.PREVIOUS_PAGE] = paginaProvenienza;
        //    Session[SessionConstants.DESTINATION_PAGE] = paginaDestinazione;        
        //}    
    }   


    protected void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
        String sessionId = HttpContext.Current.Session.SessionID.ToString();
		Logger.Info(this, "Session_Start() ID: " + sessionId + " at " + DateTime.Now, null);
        String userIPAddress = HttpContext.Current.Request.UserHostAddress;
        String userHostName  = HttpContext.Current.Request.UserHostName;
        String isLocal  = Convert.ToString(HttpContext.Current.Request.IsLocal);
		Logger.Info(this, "Request_IP_Address: " + userIPAddress + ", Request_HostName: " + userHostName + ", IsLocal: " + isLocal, null);
        IEnumerator enumSessionItems = Session.GetEnumerator();
        while (enumSessionItems.MoveNext())
        {
            String sessionItem = (string)enumSessionItems.Current;
            Logger.Info(this, "Session_Start_sessionItem: " + sessionItem, null);
        }

        // Checking the Browser Type: Mobile(PIE) or Desktop
        // http://blogs.msdn.com/iemobile/archive/2006/08/03/Detecting_IE_Mobile.aspx
        string userAgent = Request.UserAgent;
        if (userAgent.IndexOf("Windows CE") > -1 && 
             ( userAgent.IndexOf("PPC") > -1 ||
               userAgent.IndexOf("Smartphone") > -1 ||
               userAgent.IndexOf("IEMobile") > -1
             )
            )
        {
            Session[SessionConstants.IS_MOBILE_APPLICATION] = true;
            Logger.Info(this, "IS_MOBILE_APPLICATION");
        }
        else
        {
            Session[SessionConstants.IS_MOBILE_APPLICATION] = false;
            Logger.Info(this, "IS_DESKTOP_APPLICATION");
        }
    }

    protected void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        if (HttpContext.Current != null)
        {
            String sessionId = HttpContext.Current.Session.SessionID.ToString();
            Logger.Info(this, "Session_End ID: " + sessionId + " at " + DateTime.Now, null);
            IEnumerator enumSessionItems = Session.GetEnumerator();
            while (enumSessionItems.MoveNext())
            {
                string sessionItem = (string)enumSessionItems.Current;
                Logger.Info(this, "Session_Start_sessionItem:" + sessionItem, null);
            }

            TelevisPlant televisPlant = Utilities.GetTelevis();
            televisPlant.RemoveClientInformations(sessionId);
        }
        else
        {
            Logger.Info(this, "Session_End - HttpContext.Current==NULL", null);
        }
		Logger.Close();   
    }
       
</script>
