Imports System.Data.SqlClient

Public Class ChatNSMessages
        Inherits System.Web.UI.Page
        Protected WithEvents txtError As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtCurrentChatID As System.Web.UI.WebControls.TextBox
        Protected WithEvents tblNewMessages As System.Web.UI.WebControls.Table

#Region "Developer Credits"
        '=======================================================================================
        'AUTHOR                  : David Silverlight
        '
        'CLAIM TO FAME      : Head Geek at XMLPitstop.com and CoFounder of NonProfitWays.com
        '
        'EMAIL                      : HeadGeek@XMLPitstop.com
        '
        'SHAMELESS PLUG  :Yes I am a consultant who is always looking for cool projects.
        '
        'PHONE                    : (561) 212-5707  Coconut Grove, Florida
        '
        'COPYWRIGHT		: This demo is not copywrighted so feel free to use it however it may help you.  Credit is always appreciated,
        '                                  but not required.  I created it simply for the learning experience. If you have any suggestions on areas that 
        '                                  can be improved upon, please feel free to email me.  All credit for enhancements will be given to 
        '                                  those who suggest them.  Enjoy.
        '=======================================================================================
#End Region

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> 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
                Try
                        'Put user code to initialize the page here
                        Dim strChatID As String = Session("ChatID")
                        If Trim(strChatID) = "" Then
                                strChatID = "0"
                        End If

                        txtCurrentChatID.Text = "Chat: " & doGetChatDescription(strChatID)
                        doLoadTable(strChatID)
                Catch ex As Exception
                        'Handle error here
                End Try
        End Sub


        Function doLoadTable(ByVal strChatID)

                Try

                        Dim strMessages As String
                        Dim cn As New SqlConnection(ConfigurationSettings.AppSettings().Item("cnChat"))
                        Dim cmd As New SqlCommand()

                        cn.Open()
                        cmd.Connection = cn
                        cmd.CommandText = "exec sp_getNewChatMessage " & strChatID & ", 0"

                        Dim rdr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)



                        'Dim r As New TableRow()
                        Dim j As Integer = -1
                        Dim i As Integer = -1
                        Dim strMessage As String
                        Dim strMessageText As String
                        Dim objControl As New LiteralControl()

                        Dim r1 = New TableRow()
                        Dim c2 As New TableCell()
                        Dim r2 = New TableRow()
                        Dim c1 As New TableCell()
                        Dim c As New TableCell()
                        Dim r As New TableRow()
                        Dim strNickName As String = ""
                        While rdr.Read

                                'Add Name row
                                r = New TableRow()
                                c = New TableCell()
                                strNickName = rdr("FirstName")
                                strMessageText = "<font face=arial color=lightslategray size=2>" & strNickName & " says" & "</font>"
                                c.Controls.Add(New LiteralControl(strMessageText))
                                r.Cells.Add(c)
                                tblNewMessages.Rows.Add(r)

                                'Add Message Row
                                r = New TableRow()
                                c = New TableCell()
                                strMessageText = "&nbsp;&nbsp;&nbsp;&nbsp;"
                                strMessageText &= "<font face=arial color=darkred size=2>" & rdr("MessageText") & "</font>"
                                strMessageText = doAddEmoticons(strMessageText)
                                strMessageText = Replace(strMessageText, "blah", "<img src=images/emoticons/teeth_smile.gif>")

                                c.Controls.Add(New LiteralControl(strMessageText))
                                r.Cells.Add(c)
                                tblNewMessages.Rows.Add(r)
                        End While

                        cn.Close()
                        cn = Nothing

                        Return strMessages

                Catch ex As Exception
                        txtError.Text = ex.Message
                        Return "Error: " & ex.Message
                End Try

        End Function

        Function doAddEmoticons(ByVal strOrigMessage As String) As String

                Dim strImagePath As String = "../"
                Dim strMessage As String = strOrigMessage

                Try
                        strMessage = Replace(strMessage, ":)", "<img src=" & strImagePath & "images/emoticons/teeth_smile.gif>")
                        strMessage = Replace(strMessage, ":(", "<img src=" & strImagePath & "images/emoticons/angry_smile.gif>")
                        strMessage = Replace(strMessage, "(Y)", "<img src=" & strImagePath & "images/emoticons/thumbs_up.gif>")
                        strMessage = Replace(strMessage, "(N)", "<img src=" & strImagePath & "images/emoticons/thumbs_down.gif>")
                        strMessage = Replace(strMessage, "(B)", "<img src=" & strImagePath & "images/emoticons/beer_yum.gif>")
                        strMessage = Replace(strMessage, "(D)", "<img src=" & strImagePath & "images/emoticons/martini_shaken.gif>")
                        strMessage = Replace(strMessage, "(X)", "<img src=" & strImagePath & "images/emoticons/girl_handsacrossamerica.gif>")
                        strMessage = Replace(strMessage, "(Z)", "<img src=" & strImagePath & "images/emoticons/guy_handsacrossamerica.gif>")
                        strMessage = Replace(strMessage, ":-[)", "<img src=" & strImagePath & "images/emoticons/bat.gif>")
                        strMessage = Replace(strMessage, "(})", "<img src=" & strImagePath & "images/emoticons/girl_hug.gif>")
                        strMessage = Replace(strMessage, "({)", "<img src=" & strImagePath & "images/emoticons/dude_hug.gif>")
                        strMessage = Replace(strMessage, ":-)", "<img src=" & strImagePath & "images/emoticons/regular_smile.gif>")
                        strMessage = Replace(strMessage, ":-D)", "<img src=" & strImagePath & "images/emoticons/teeth_smile.gif>")
                        strMessage = Replace(strMessage, ":-O)", "<img src=" & strImagePath & "images/emoticons/omg_smile.gif>")
                        strMessage = Replace(strMessage, ":-P)", "<img src=" & strImagePath & "images/emoticons/tounge_smile.gif>")
                        strMessage = Replace(strMessage, ";-)", "<img src=" & strImagePath & "images/emoticons/wink_smile.gif>")
                        strMessage = Replace(strMessage, ":-()", "<img src=" & strImagePath & "images/emoticons/sad_smile.gif>")
                        strMessage = Replace(strMessage, "(P)", "<img src=" & strImagePath & "images/emoticons/confused_smile.gif>")
                        strMessage = Replace(strMessage, ":-|)", "<img src=" & strImagePath & "images/emoticons/whatchutalkingabout_smile.gif>")
                        strMessage = Replace(strMessage, ":'()", "<img src=" & strImagePath & "images/emoticons/cry_smile.gif>")
                        strMessage = Replace(strMessage, ":$)", "<img src=" & strImagePath & "images/emoticons/embaressed_smile.gif>")
                        strMessage = Replace(strMessage, "(H)", "<img src=" & strImagePath & "images/emoticons/shades_smile.gif>")
                        strMessage = Replace(strMessage, ":-@)", "<img src=" & strImagePath & "images/emoticons/angry_smile.gif>")
                        strMessage = Replace(strMessage, "(A)", "<img src=" & strImagePath & "images/emoticons/angel_smile.gif>")
                        strMessage = Replace(strMessage, "(6)", "<img src=" & strImagePath & "images/emoticons/devil_smile.gif>")
                        strMessage = Replace(strMessage, "(L)", "<img src=" & strImagePath & "images/emoticons/heart.gif>")
                        strMessage = Replace(strMessage, "(U)", "<img src=" & strImagePath & "images/emoticons/broken_heart.gif>")
                        strMessage = Replace(strMessage, "(K)", "<img src=" & strImagePath & "images/emoticons/kiss.gif>")
                        strMessage = Replace(strMessage, "(G)", "<img src=" & strImagePath & "images/emoticons/present.gif>")
                        strMessage = Replace(strMessage, "(F)", "<img src=" & strImagePath & "images/emoticons/rose.gif>")
                        strMessage = Replace(strMessage, "(W)", "<img src=" & strImagePath & "images/emoticons/wilted_rose.gif>")
                        strMessage = Replace(strMessage, "(P)", "<img src=" & strImagePath & "images/emoticons/camera.gif>")
                        strMessage = Replace(strMessage, "(~)", "<img src=" & strImagePath & "images/emoticons/film.gif>")
                        strMessage = Replace(strMessage, "(T)", "<img src=" & strImagePath & "images/emoticons/phone.gif>")
                        strMessage = Replace(strMessage, "(@)", "<img src=" & strImagePath & "images/emoticons/kittykay.gif>")
                        strMessage = Replace(strMessage, "(&)", "<img src=" & strImagePath & "images/emoticons/bowwow.gif>")
                        strMessage = Replace(strMessage, "(C)", "<img src=" & strImagePath & "images/emoticons/coffee.gif>")
                        strMessage = Replace(strMessage, "(I)", "<img src=" & strImagePath & "images/emoticons/lightbulb.gif>")
                        strMessage = Replace(strMessage, "(S)", "<img src=" & strImagePath & "images/emoticons/moon.gif>")
                        strMessage = Replace(strMessage, "(*)", "<img src=" & strImagePath & "images/emoticons/star.gif>")
                        strMessage = Replace(strMessage, "(8)", "<img src=" & strImagePath & "images/emoticons/musical_note.gif>")
                        strMessage = Replace(strMessage, "(E)", "<img src=" & strImagePath & "images/emoticons/envelope.gif>")
                        strMessage = Replace(strMessage, "(^)", "<img src=" & strImagePath & "images/emoticons/cake.gif>")
                        strMessage = Replace(strMessage, "(P)", "<img src=" & strImagePath & "images/emoticons/clock.gif>")
                        strMessage = Replace(strMessage, "(M)", "<img src=" & strImagePath & "images/emoticons/messenger.gif>")
                        strMessage = Replace(strMessage, "(markwilson)", "<img src=" & strImagePath & "images/emoticons/MarkWilsonEmot2.jpg>")
                        strMessage = Replace(strMessage, "(jeanine)", "<img src=" & strImagePath & "images/emoticons/JeanineEmot2.jpg>")
                        strMessage = Replace(strMessage, "(sherv)", "<img src=" & strImagePath & "images/emoticons/ShervEmot1.jpg>")
                        strMessage = Replace(strMessage, "(beer)", "<img src=" & strImagePath & "images/emoticons/ShervTableEmot2.jpg>")
                        strMessage = Replace(strMessage, "(kevin)", "<img src=" & strImagePath & "images/emoticons/KevEmot2.jpg>")
                        strMessage = Replace(strMessage, "(dave)", "<img src=" & strImagePath & "images/emoticons/DaveEmot2.jpg>")
                        strMessage = Replace(strMessage, "(jason)", "<img src=" & strImagePath & "images/emoticons/JasonEmot1.jpg>")
                        strMessage = Replace(strMessage, "(bullet)", "<img src=" & strImagePath & "images/emoticons/BulletEmot3.jpg>")
                        strMessage = Replace(strMessage, "(blue)", "<img src=" & strImagePath & "images/emoticons/BlueManEmot2.jpg>")



                Catch ex As Exception
                        Return strOrigMessage
                End Try
                Return strMessage
        End Function


        Function doGetChatDescription(ByVal strChatID)

                Try

                        Dim strMessages As String = ""
                        Dim strChatDescription As String = ""
                        'Dim strSQL As String = "Select * from ChatSessions where ChatSessionID = " & strChatID
                        Dim strSQL As String = "sp_getChatDescription " & strChatID
                        Dim cn = New SqlConnection(ConfigurationSettings.AppSettings().Item("cnChat"))
                        Dim cmd As New SqlCommand(strSQL, cn)
                        cn.Open()

                        Dim rdr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                        While rdr.Read
                                strChatDescription = rdr("ChatDescription")
                        End While

                        Return strChatDescription

                Catch ex As Exception
                        '    txtError.Text = Err.Description
                        Return "Error: " & ex.Message
                End Try

        End Function

End Class
