' This code is client-side Dhtml script that is downloaded into IE.
' It is not intended to be run from the command line.


Dim g_bBackCompatToolbarHooks
g_bBackCompatToolbarHooks = False

Sub EnableCompatibleToolbarHooks
    g_bBackCompatToolbarHooks = True
End Sub

Sub DisableCompatibleToolbarHooks
    g_bBackCompatToolbarHooks = False
End Sub

Function ShouldRunToolbarHooks
    ShouldRunToolbarHooks = g_bBackCompatToolbarHooks
End Function

Dim dictShiftContent
Set dictShiftContent = CreateObject("Scripting.Dictionary")

Sub AddShiftContentReference( ByVal strSourceActiveXEditor, ByVal targetInputElement )

   dictShiftContent.Item( strSourceActiveXEditor ) = targetInputElement

End Sub

Sub ShiftActiveXPlaceholderContent
    Dim keySourceControl, valueTargetElement
    
    
    For each keySourceControl in dictShiftContent.keys
        valueTargetElement = dictShiftContent.Item(keySourceControl)
        
        document.all(valueTargetElement).value = document.all(keySourceControl).html

    Next
End Sub

Sub UpdateButton( byval pActiveHtmlEditor, byval strItemName, byval strTrueValue )
    if pActiveHtmlEditor.dom.queryCommandEnabled(strItemName) = true then
        If StrComp(pActiveHtmlEditor.dom.queryCommandValue(strItemName), strTrueValue)=0 Then
            pActiveHtmlEditor.ToolbarState.Item(strItemName).Checked = true
        Else
            pActiveHtmlEditor.ToolbarState.Item(strItemName).Checked = false
        End If
        pActiveHtmlEditor.ToolbarState.Item(strItemName).Enabled = true
    else
        pActiveHtmlEditor.ToolbarState.Item(strItemName).Enabled = false
    end if
End Sub

Sub UpdateEnabledButton( byval pActiveHtmlEditor, byval strItemName )
    pActiveHtmlEditor.ToolbarState.Item(strItemName ).Enabled = pActiveHtmlEditor.dom.queryCommandEnabled(strItemName )
End Sub

Function HtmlToExternalFontSize( byval nFontSize )
    If nFontSize <= 1 Then
        HtmlToExternalFontSize = 8
    ElseIf nFontSize = 2 Then
        HtmlToExternalFontSize = 10
    ElseIf nFontSize = 3 Then
        HtmlToExternalFontSize = 12
    ElseIf nFontSize = 4 Then
        HtmlToExternalFontSize = 14
    ElseIf nFontSize = 5 Then
        HtmlToExternalFontSize = 18
    ElseIf nFontSize = 6 Then
        HtmlToExternalFontSize = 24
    ElseIf nFontSize = 7 Then
        HtmlToExternalFontSize = 36
    End If
End Function

Function ExternalToHtmlFontSize( byval nFontSize )
    If nFontSize > 30 Then
        ExternalToHtmlFontSize = 7
    ElseIf nFontSize > 20 Then
        ExternalToHtmlFontSize = 6
    ElseIf nFontSize > 16 Then
        ExternalToHtmlFontSize = 5
    ElseIf nFontSize > 12 Then
        ExternalToHtmlFontSize = 4
    ElseIf nFontSize > 10 Then
        ExternalToHtmlFontSize = 3
    ElseIf nFontSize > 8 Then
        ExternalToHtmlFontSize = 2
    else
        ExternalToHtmlFontSize = 1
    End If
End Function


Sub UpdateComboBox(byval pActiveHtmlEditor, byval strItemName )
    if pActiveHtmlEditor.dom.queryCommandEnabled(strItemName) = true then
        pActiveHtmlEditor.ToolbarState.Item(strItemName).Enabled = true
        If IsNull(pActiveHtmlEditor.dom.queryCommandValue(strItemName)) Then
            pActiveHtmlEditor.ToolbarState.Item(strItemName).ComboBoxSel = ""
        else
            pActiveHtmlEditor.ToolbarState.Item(strItemName).ComboBoxSel = pActiveHtmlEditor.dom.queryCommandValue(strItemName)
        End If
    else
        pActiveHtmlEditor.ToolbarState.Item(strItemName).Enabled = false
        pActiveHtmlEditor.ToolbarState.Item(strItemName).ComboBoxSel = ""
    end if
End Sub

sub SetHeaderBarColor( byval pActiveHtmlEditor, byval strColor )
    ' in case capability bar was turned off, ignore any error
    on error resume next
    pActiveHtmlEditor.OuterHTMLElement.parentElement.parentElement.parentElement.children(0).children(0).bgColor = strColor
end sub

Sub ToolbarInterface_OnChange( byval pActiveHtmlEditor, byval bstrId, byval pItem )
    Dim bChecked

    Select Case bstrId
    Case "GotFocus"
        SetHeaderBarColor pActiveHtmlEditor, "#aeaeae"
    Case "LostFocus"
        SetHeaderBarColor pActiveHtmlEditor, "#dedfde"
    Case "UpdateUI"
        pActiveHtmlEditor.ToolbarState.Item("HtmlEdit").Checked = pActiveHtmlEditor.EditingSource

        UpdateEnabledButton pActiveHtmlEditor, "Cut"
        UpdateEnabledButton pActiveHtmlEditor, "Copy"
        UpdateEnabledButton pActiveHtmlEditor, "Paste"

        pActiveHtmlEditor.ToolbarState.Item("Undo").Enabled = pActiveHtmlEditor.CanUndo
        pActiveHtmlEditor.ToolbarState.Item("Redo").Enabled = pActiveHtmlEditor.CanRedo

        if pActiveHtmlEditor.EditingSource = false then
            UpdateComboBox pActiveHtmlEditor, "FontName"
            UpdateComboBox pActiveHtmlEditor, "FormatBlock"

            if pActiveHtmlEditor.dom.queryCommandEnabled("FontSize") = true then
                pActiveHtmlEditor.ToolbarState.Item("FontSize").ComboBoxSel = CStr(HtmlToExternalFontSize(pActiveHtmlEditor.dom.queryCommandValue("FontSize")))
                pActiveHtmlEditor.ToolbarState.Item("FontSize").Enabled = true
            else
                pActiveHtmlEditor.ToolbarState.Item("FontSize").Enabled = false
                pActiveHtmlEditor.ToolbarState.Item("FontSize").ComboBoxSel = ""
            end if

            UpdateButton pActiveHtmlEditor, "Bold", "True"
            UpdateButton pActiveHtmlEditor, "Italic", "True"
            UpdateButton pActiveHtmlEditor, "Underline", "True"

            UpdateButton pActiveHtmlEditor, "JustifyLeft", "True"
            UpdateButton pActiveHtmlEditor, "JustifyCenter", "True"
            UpdateButton pActiveHtmlEditor, "JustifyRight", "True"

            UpdateButton pActiveHtmlEditor, "InsertOrderedList", "True"
            UpdateButton pActiveHtmlEditor, "InsertUnorderedList", "True"
        End If

        If ShouldRunToolbarHooks Then
            ' call external Customizable Hook
            Call OnToolbarUpdate( pActiveHtmlEditor, pActiveHtmlEditor.EditingSource )
        End If
        
    Case "FormatBlock"
        pActiveHtmlEditor.dom.execCommand "FormatBlock", false, pItem.CurrentString
        UpdateComboBox pActiveHtmlEditor, "FormatBlock"
    Case "FontSize"
        pActiveHtmlEditor.dom.execCommand "FontSize", false, ExternalToHtmlFontSize(pItem.CurrentString)
        pActiveHtmlEditor.ToolbarState.Item("FontSize").ComboBoxSel = HtmlToExternalFontSize(pActiveHtmlEditor.dom.queryCommandValue("FontSize"))
    Case "FontName"
        pActiveHtmlEditor.dom.execCommand "FontName", false, pItem.CurrentString
        UpdateComboBox pActiveHtmlEditor, "FontName"

    Case "HtmlEdit"
        bChecked = not pActiveHtmlEditor.EditingSource
        If ( bChecked ) Then
            Call WBC_cleanHtml( pActiveHtmlEditor.dom )
        End If
        pActiveHtmlEditor.EditingSource = bChecked
        pActiveHtmlEditor.ToolbarState.Item("HtmlEdit").Checked = bChecked

        pActiveHtmlEditor.ToolbarState.Item("FormatBlock").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("FontName").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("FontSize").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("ForeColor").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("BackColor").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("InsertImage").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("InsertVideo").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("InsertAttachment").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("JustifyLeft").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("JustifyRight").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("JustifyCenter").Enabled = not bChecked

        pActiveHtmlEditor.ToolbarState.Item("Outdent").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("Indent").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("InsertTable").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("InsertOrderedList").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("InsertUnorderedList").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("Bold").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("Italic").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("Underline").Enabled = not bChecked
        pActiveHtmlEditor.ToolbarState.Item("CreateLink").Enabled = not bChecked
    case "SpellCheck"
        dim pState, elm
        window.status = "Checking spelling..."
        set pState = document.ToolbarInterface.CreateSpellCheckState
        for each elm in document.all
            if pState.CheckPlaceholderSpelling(elm) = true then
                window.status = "Spell check cancelled"
                exit sub
            end if
        next
        window.status = "Spell check complete"
    Case "Undo"
        pActiveHtmlEditor.Undo
        pActiveHtmlEditor.ToolbarState.Item("Undo").Enabled = pActiveHtmlEditor.CanUndo
        pActiveHtmlEditor.ToolbarState.Item("Redo").Enabled = pActiveHtmlEditor.CanRedo
    Case "Redo"
        pActiveHtmlEditor.Redo
        pActiveHtmlEditor.ToolbarState.Item("Undo").Enabled = pActiveHtmlEditor.CanUndo
        pActiveHtmlEditor.ToolbarState.Item("Redo").Enabled = pActiveHtmlEditor.CanRedo
    Case "Bold"
        pActiveHtmlEditor.dom.execCommand "Bold"
        UpdateButton pActiveHtmlEditor, "Bold", "True"
    Case "Italic"
        pActiveHtmlEditor.dom.execCommand "Italic"
        UpdateButton pActiveHtmlEditor, "Italic", "True"
    Case "Underline"
        pActiveHtmlEditor.dom.execCommand "Underline"
        UpdateButton pActiveHtmlEditor, "Underline", "True"

    Case "ForeColor"
        Call WBC_setColor( pActiveHtmlEditor.dom, "ForeColor" )
    Case "BackColor"
        Call WBC_setColor( pActiveHtmlEditor.dom, "BackColor" )

    Case "JustifyLeft"
        pActiveHtmlEditor.dom.execCommand "JustifyLeft"
        UpdateButton pActiveHtmlEditor, "JustifyLeft", "True"
    Case "JustifyCenter"
        pActiveHtmlEditor.dom.execCommand "JustifyCenter"
        UpdateButton pActiveHtmlEditor, "JustifyCenter", "True"
    Case "JustifyRight"
        pActiveHtmlEditor.dom.execCommand "JustifyRight"
        UpdateButton pActiveHtmlEditor, "JustifyRight", "True"

    Case "InsertOrderedList"
        pActiveHtmlEditor.dom.execCommand "InsertOrderedList"
        UpdateButton pActiveHtmlEditor, "InsertOrderedList", "True"
    Case "InsertUnorderedList"
        pActiveHtmlEditor.dom.execCommand "InsertUnorderedList"
        UpdateButton pActiveHtmlEditor, "InsertUnorderedList", "True"

    Case "Outdent"
        pActiveHtmlEditor.dom.execCommand "Outdent"
        UpdateButton pActiveHtmlEditor, "Outdent", "True"
    Case "Indent"
        pActiveHtmlEditor.dom.execCommand "Indent"
        UpdateButton pActiveHtmlEditor, "Indent", "True"

    case "Cut"
        pActiveHtmlEditor.dom.execCommand "Cut"
    case "Copy"
        pActiveHtmlEditor.dom.execCommand "Copy"
    case "Paste"
        pActiveHtmlEditor.dom.execCommand "Paste"
    case else

        If ShouldRunToolbarHooks Then
            ' call external Customizable Hook
            Call OnToolbarEvent( pActiveHtmlEditor, bstrId, pItem, pActiveHtmlEditor.EditingSource )
        End If
        
    End Select
End Sub

dim bInitializedToolbar
bInitializedToolbar = false

Sub InitializeToolbar
    If bInitializedToolbar = true Then
        exit Sub
    End If
    bInitializedToolbar = true

    Dim format_toolbar
    set format_toolbar = document.ToolbarInterface.Toolbars.CreateToolbar("Formatting")

    Dim edit_toolbar
    set edit_toolbar = document.ToolbarInterface.Toolbars.CreateToolbar("Editing")

    format_toolbar.IconWidth = 16
    format_toolbar.IconHeight = 15
    format_toolbar.ButtonWidth = 18
    format_toolbar.ButtonHeight = 18
    format_toolbar.Break = false

    edit_toolbar.IconWidth = 16
    edit_toolbar.IconHeight = 15
    edit_toolbar.ButtonWidth = 18
    edit_toolbar.ButtonHeight = 18

    dim myhtmlstyle
    dim myfont
    dim myfontsize
    dim i


    set myhtmlstyle = format_toolbar.AddComboBox("FormatBlock", "HTML Style", 100)
    myhtmlstyle.AllowTextEdit = false
    myhtmlstyle.Sort = False

    dim arrStyles, strStyle
    arrStyles = document.ToolbarInterface.AvailableStyles

    for each strStyle in arrStyles
        myhtmlstyle.AddString strStyle
    next

    myhtmlstyle.CurrentString = "Normal"

    set myfont = format_toolbar.AddComboBox("FontName", "Font Face", 150)
    myfont.AllowTextEdit = false

    dim arrFonts, strFont
    arrFonts = document.ToolbarInterface.AvailableFonts
    for each strFont in arrFonts
        myfont.AddString strFont
    next

    myfont.CurrentString = "Times New Roman"

    set myfontsize = format_toolbar.AddComboBox("FontSize", "Font Size", 80)
    myfontsize.AllowTextEdit = false
    myfontsize.Sort = False
    myfontsize.CurrentString = "12"

    myfontsize.AddString "8"
    myfontsize.AddString "10"
    myfontsize.AddString "12"
    myfontsize.AddString "14"
    myfontsize.AddString "18"
    myfontsize.AddString "24"
    myfontsize.AddString "36"

    call format_toolbar.AddSeperator

    format_toolbar.AddButton "InsertImage", "", "image.gif", IDS_UI_TOOLTIP_ACTIVEX_INSERT_IMAGE        
    format_toolbar.AddButton "InsertVideo", "", "video.gif", IDS_UI_TOOLTIP_ACTIVEX_INSERT_VIDEO        
    format_toolbar.AddButton "InsertAttachment", "", "attachment.gif", IDS_UI_TOOLTIP_ACTIVEX_INSERT_ATTACHMENT     
 
    call format_toolbar.AddSeperator

    format_toolbar.AddButton "InsertTable", "", "table.gif", IDS_UI_TOOLTIP_ACTIVEX_INSERT_TABLE
    format_toolbar.AddButton "CreateLink", "", "wlink.gif", IDS_UI_TOOLTIP_ACTIVEX_EDIT_HYPERLINK

    call format_toolbar.AddSeperator
    format_toolbar.AddCheckButton "HtmlEdit", "", "HTMLEdit.gif", IDS_UI_TOOLTIP_ACTIVEX_EDIT_SOURCE

    edit_toolbar.AddButton "Cut", "", "cut.gif", IDS_UI_TOOLTIP_ACTIVEX_CUT
    edit_toolbar.AddButton "Copy", "", "copy.gif", IDS_UI_TOOLTIP_ACTIVEX_COPY
    edit_toolbar.AddButton "Paste", "", "paste.gif", IDS_UI_TOOLTIP_ACTIVEX_PASTE

    call edit_toolbar.AddSeperator
    edit_toolbar.AddButton "Undo", "", "undo.gif", IDS_UI_TOOLTIP_ACTIVEX_UNDO
    edit_toolbar.AddButton "Redo", "", "redo.gif", IDS_UI_TOOLTIP_ACTIVEX_REDO
    call edit_toolbar.AddSeperator

    set p = edit_toolbar.AddButton("SpellCheck", "", "spell.gif", IDS_UI_TOOLTIP_ACTIVEX_SPELL_CHECK)
    p.Global = true
    p.Enabled = document.ToolbarInterface.CanSpellCheck

    call edit_toolbar.AddSeperator
    edit_toolbar.AddCheckButton "Bold", "", "bold.gif", IDS_UI_TOOLTIP_ACTIVEX_BOLD
    edit_toolbar.AddCheckButton "Italic", "", "italic.gif", IDS_UI_TOOLTIP_ACTIVEX_ITALIC
    edit_toolbar.AddCheckButton "Underline", "", "under.gif", IDS_UI_TOOLTIP_ACTIVEX_UNDERLINE
    call edit_toolbar.AddSeperator

    edit_toolbar.AddButton "ForeColor", "", "tpaint.gif", IDS_UI_TOOLTIP_ACTIVEX_FOREGROUND_COLOR
    edit_toolbar.AddButton "BackColor", "", "parea.gif", IDS_UI_TOOLTIP_ACTIVEX_BACKGROUND_COLOR
    call edit_toolbar.AddSeperator

    edit_toolbar.AddCheckButton "JustifyLeft", "",  "aleft.gif", IDS_UI_TOOLTIP_ACTIVEX_ALIGN_LEFT
    edit_toolbar.AddCheckButton "JustifyCenter", "", "center.gif", IDS_UI_TOOLTIP_ACTIVEX_ALIGN_CENTER
    edit_toolbar.AddCheckButton "JustifyRight", "", "aright.gif", IDS_UI_TOOLTIP_ACTIVEX_ALIGN_RIGHT
    call edit_toolbar.AddSeperator      

    edit_toolbar.AddCheckButton "InsertOrderedList", "", "nlist.gif", IDS_UI_TOOLTIP_ACTIVEX_NUMBERED_LIST
    edit_toolbar.AddCheckButton "InsertUnorderedList", "", "blist.gif", IDS_UI_TOOLTIP_ACTIVEX_BULLETED_LIST
    call edit_toolbar.AddSeperator      

    edit_toolbar.AddButton "Outdent", "", "ileft.gif", IDS_UI_TOOLTIP_ACTIVEX_DECREASE_INDENT
    edit_toolbar.AddButton "Indent", "", "iright.gif", IDS_UI_TOOLTIP_ACTIVEX_INCREASE_INDENT

    If ShouldRunToolbarHooks Then
        ' Call External Customizable Hook
        Call OnToolbarInitialize( document.ToolbarInterface )
    End If

    document.ToolbarInterface.ShowToolbars
End Sub
