Imports System.IO
Imports System.Threading

Public Class DriverMaster
    Inherits System.Web.UI.MasterPage

    Property CurrentDriver As Logic.DriverAccount
    Property CurrentPage As String

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Try
            If My.Settings.httpsOnly Then
                _SecurePage()
            End If

            Dim uri As New Uri(Request.Url.AbsoluteUri)
            CurrentPage = Path.GetFileName(uri.LocalPath).ToLower()

            Dim driverId = SessionVars.DriverId(Session)
            If driverId > 0 Then
                CurrentDriver = New Logic.DriverAccount(driverId)
            Else
                If String.IsNullOrEmpty(SessionVars.PostLoginUrl(Session)) Then
                    SessionVars.PostLoginUrl(Session) = Request.Url.ToString()
                End If
				Response.Redirect("/Driver/CreateAccount.aspx")
			End If

        Catch ex As ThreadAbortException
            ' nop
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
    End Sub

    Private Sub btnDashboard_Click() Handles btnDashboard.Click
        Response.Redirect("/Driver/Dashboard.aspx")
    End Sub

    Private Sub btnLogout_Click() Handles btnLogout.Click
        Response.Redirect("/Driver/Logout.aspx")
    End Sub

    ' Make us use https
    Private Sub _SecurePage()
        Dim ServerName As String = Server.UrlEncode(Request.ServerVariables.Item("SERVER_NAME"))
        Dim FilePath As String = Request.Url.PathAndQuery
        If Not Request.IsSecureConnection Then
            Response.Redirect("https://" & ServerName & FilePath)
        End If
    End Sub

End Class