    Imports System
    Imports System.Data
    Imports System.Web
    Imports System.Text
    Imports System.Web.UI.WebControls
    Imports ASPNET.StarterKit.TimeTracker.BusinessLogicLayer

    Namespace ASPNET.StarterKit.TimeTracker.Web

        '*********************************************************************
        '
        ' ProjectList.aspx
        '
        ' Report launching page for the project and resource reports.
        '
        '*********************************************************************

        Public Class Reports
            Inherits System.Web.UI.Page
            Protected ProjectList As System.Web.UI.WebControls.ListBox
            Protected UserList As System.Web.UI.WebControls.ListBox
            Protected WithEvents GenProjectRpt As System.Web.UI.WebControls.Button
            Protected WithEvents GenResourceRpt As System.Web.UI.WebControls.Button
            Protected StartDate As System.Web.UI.WebControls.TextBox
            Protected EndDate As System.Web.UI.WebControls.TextBox
            Protected ProjectListRequiredFieldValidator As System.Web.UI.WebControls.RequiredFieldValidator
            Protected UserListRequiredFieldValidator As System.Web.UI.WebControls.RequiredFieldValidator

            Private _pageIndex As String = String.Empty

    #Region " Web Form Designer Generated Code "

            'This call is required by the Web Form Designer.
             Private Sub InitializeComponent()

            End Sub

            Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
                'CODEGEN: This method call is required by the Web Form Designer
                'Do not modify it using the code editor.
                InitializeComponent()
            End Sub

    #End Region

            Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                ' Ensure that the visiting user has access to view the current page
                If TTSecurity.IsInRole(TTUser.UserRoleAdminPMgr) = False Then
                    Response.Redirect("AccessDenied.aspx?Index=-1", True)
                End If

                Dim startingDate As DateTime = DateTime.Today
                Dim endingDate As DateTime = DateTime.Today

                _pageIndex = Request.QueryString("Index")

                If Not Me.IsPostBack Then
                    ProjectList.DataSource = Project.GetProjects(TTSecurity.GetUserID(), TTSecurity.GetUserRole())
                    ProjectList.DataTextField = "Name"
                    ProjectList.DataValueField = "ProjectID"
                    ProjectList.DataBind()

                    UserList.DataSource = TTUser.GetUsers(TTSecurity.GetUserID(), TTSecurity.GetUserRole())
                    UserList.DataTextField = "Name"
                    UserList.DataValueField = "UserID"
                    UserList.DataBind()

                    BusinessLogicLayer.TimeEntry.FillCorrectStartEndDates(DateTime.Today, startingDate, endingDate)
                    StartDate.Text = startingDate.ToShortDateString()
                    EndDate.Text = endingDate.ToShortDateString()
                End If
            End Sub 'Page_Load

            '*********************************************************************
            '
            ' Converts values contained in the provided ListItemsCollection to a
            ' comma-delimited string.
            '
            '*********************************************************************

            Private Function BuildValueList(ByVal items As ListItemCollection) As String
                ' Build up list of IDs of selected users
                Dim idList As New StringBuilder()
                Dim item As ListItem
                For Each item In items
                    If item.Selected Then
                        If idList.ToString() <> String.Empty Then
                            idList.Append(",")
                        End If
                        idList.Append(item.Value.ToString())
                    End If
                Next item

                Return idList.ToString()
            End Function 'BuildValueList

            '*********************************************************************
            '
            ' Launches the project report using the selected projects as input.
            '
            '*********************************************************************

            Private Sub GenProjectRpt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GenProjectRpt.Click
                ProjectListRequiredFieldValidator.Validate()

                ' Go to the project report page using the selected project IDs
                If ProjectListRequiredFieldValidator.IsValid Then
                    Server.Transfer(("ProjectReport.aspx?IDs=" + BuildValueList(ProjectList.Items) + "&index=" + _pageIndex))
                End If
            End Sub 'GenProjectRpt_Click

            '*********************************************************************
            '
            ' Launches the resource report using the selected consultants and date range as input.
            '
            '*********************************************************************

            Private Sub GenResourceRpt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GenResourceRpt.Click
                UserListRequiredFieldValidator.Validate()

                ' Go to the resource report page using the selected user IDs
                If UserListRequiredFieldValidator.IsValid Then
                    Response.Redirect("ResourceReport.aspx?IDs=" + BuildValueList(UserList.Items) + "&Start=" + StartDate.Text + "&End=" + EndDate.Text + "&index=" + _pageIndex, False)
                End If
            End Sub 'GenResourceRpt_Click
        End Class 'Reports
    End Namespace 'ASPNET.StarterKit.TimeTracker.Web