Imports CharterDotCom.CommonEntities.Library

Partial Class DocumentProperties
    Inherits System.Web.UI.UserControl

    Public Event DocumentUpdated(ByVal folderID As Integer, ByVal documentID As Integer)

    Public WriteOnly Property FolderID() As Integer

        Set(ByVal value As Integer)

            ViewState(Me.ID & Constants.FOLDER_VIEWSTATE) = value

        End Set

    End Property

    Public WriteOnly Property DocumentID() As Integer

        Set(ByVal value As Integer)

            ViewState(Me.ID & Constants.DOCUMENT_VIEWSTATE) = value

            If value > Constants.Documents.NewDocument Then

                Dim documentManager As New DMSAdmin.DocumentManagerBL
                documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials

                Dim document As DMSAdmin.BLHTMLDocument = documentManager.GetDocument(value)

                titleTextBox.Text = document.Title
                summaryTextBox.Text = document.Summary
                keyWordsTextBox.Text = document.KeyWords
                FCKeditor.Value = document.HTML
                lblID.Text = "ID: " & document.ID


                binaryDocLink.NavigateUrl = "http://" & Request.ServerVariables("server_name") & "/GETBINARY.xbin?ID=" & document.ID
                binaryDocLink.ImageUrl = "../../Admin/CharterDocumentManagement/images/view.png"
            Else

                titleTextBox.Text = ""
                summaryTextBox.Text = ""
                keyWordsTextBox.Text = ""
                FCKeditor.Value = ""
            End If

            Dim enabled As Boolean = (value <> Constants.Documents.NoDocument)
            titleTextBox.Enabled = enabled
            summaryTextBox.Enabled = enabled
            keyWordsTextBox.Enabled = enabled
            submitButton.Enabled = enabled
            cancelButton.Enabled = enabled
        End Set

    End Property

    Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitButton.Click

        Try
            Dim documentTitle As String = titleTextBox.Text.Trim()
            Dim documentSummary As String = summaryTextBox.Text.Trim()
            Dim documentHTML As String = FCKeditor.Value.Trim()
            Dim keyWords As String = keyWordsTextBox.Text.Trim()
            If documentTitle.Length > 0 And documentSummary.Length > 0 And documentHTML.Length > 0 Then

                Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))
                Dim documentID As Integer = CInt(ViewState(Me.ID & Constants.DOCUMENT_VIEWSTATE))

                Dim document As DMSAdmin.BLHTMLDocument = New DMSAdmin.BLHTMLDocument
                With document
                    .ID = documentID
                    .Title = documentTitle
                    .Summary = documentSummary
                    .HTML = documentHTML
                    .KeyWords = keyWords
                End With

                Dim documentManager As New DMSAdmin.DocumentManagerBL
                documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials
                If documentID = Constants.Documents.NewDocument Then
                    Dim page As AdminPage = CType(Me.Page, AdminPage)
                    documentID = documentManager.AddDocument(folderID, document, page.CurrentUserID)
                Else
                    Dim page As AdminPage = CType(Me.Page, AdminPage)
                    documentManager.UpdateDocument(document, page.CurrentUserID)
                End If

                RaiseEvent DocumentUpdated(folderID, documentID)
            End If
        Catch ex As Exception

      Throw
    End Try

    End Sub

  Protected Sub cancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cancelButton.Click

    Try
      Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))
      Dim documentID As Integer = CInt(ViewState(Me.ID & Constants.DOCUMENT_VIEWSTATE))
      Me.FolderID = folderID
      Me.DocumentID = documentID
    Catch ex As Exception
      Throw
    End Try

  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    FCKeditor.ImageBrowserURL = "VersionImage.aspx"
  End Sub
End Class
