// core START

var Chatbox_version = "Beta 1.4.8"

var Chatbox_no_auto_open, Chatbox_no_drag, Chatbox_no_scroll_adjust, Chatbox_no_auto_place

Array.prototype.existed = ArrayExisted
function ArrayExisted(target) {
  for (var i = 0; i < this.length; i++)
    if (this[i] == target)
      return (i+1)

  return false
}

// core END

var Chatbox_enabled = (w3c_dom || ie5)
// Chatbox_enabled = false

function w3c_chatDisplay(state) {
  if (!w3c_dom)
    return

  document.getElementById("CB_Lwindow_content0").style.display = (state) ? "inline" : "none"
}

// old functions START

function chatW_dimension(obj) {
  var s = obj.style

  var min = { w:s.pixelWidth, h:s.pixelHeight }
  if (obj.min_width)
    min.w = (obj.min_width > 0) ? obj.min_width : sw + obj.min_width
  if (obj.min_height)
    min.h = (obj.min_height > 0) ? obj.min_height : sh + obj.min_height

  var max = { w:s.pixelWidth, h:s.pixelHeight }
  if (obj.max_width)
    max.w = (obj.max_width > 0) ? obj.max_width : sw + obj.max_width
  if (obj.max_height)
    max.h = (obj.max_height > 0) ? obj.max_height : sh + obj.max_height

  if (min.h < 100)
    min.h = 100
  if (min.w < 100)
    min.w = 100
  if (max.h < 100)
    max.h = 100
  if (max.w < 100)
    max.w = 100

  return { min:min, max:max }
}

function chatW_minimize(n, forced_minimize) {
  var minB = document.getElementById("CB_SminimizeB" + n)
  var c_content = document.getElementById("CB_Lchat_content" + n)

  if (minB.innerText == Chatbox_buttons.normal) {
    if (forced_minimize)
      return

    minB.innerText = Chatbox_buttons.min
    chatW_resize(n, Chatbox_buttons.normal)
  }
  else {
    minB.innerText = Chatbox_buttons.normal
    document.getElementById("CB_SresizeB" + n).innerText = Chatbox_buttons.max

    document.getElementById("CB_Lwindow_content" + n).style.visibility = "hidden"
w3c_chatDisplay(0)
    c_content.style.pixelWidth = chatW_dimension(c_content).min.w

    chatW_fix(n)
    chatW_place(n)

    CB_Lchat_title.innerText = "Anime Theme Chatbox Mini"

    Chatbox_EnableUpdate(false)
  }
}

function chatW_resize(n, b) {
  var resizeB = document.getElementById("CB_SresizeB" + n)
  var w_content = document.getElementById("CB_Lwindow_content" + n)
  var c_content = document.getElementById("CB_Lchat_content" + n)

  if (!b)
    b = resizeB.innerText

  var h
  if (b == Chatbox_buttons.normal) {
    h = chatW_dimension(c_content).min.h
    resizeB.innerText = Chatbox_buttons.max
  }
  else {
    h = chatW_dimension(c_content).max.h
    resizeB.innerText = Chatbox_buttons.normal
  }
  c_content.style.pixelHeight = h

  if (w_content.style.visibility == "hidden") {
    document.getElementById("CB_SminimizeB" + n).innerText = Chatbox_buttons.min
    w_content.style.visibility = "inherit"
w3c_chatDisplay(1)

    c_content.style.pixelWidth = chatW_dimension(c_content).max.w

    Chatbox_EnableUpdate(true)
  }

  chatW_fix(n)
}

var w3c_dom_left_mod = ((w3c_dom) ? 4 : 0)

function chatW_place(n) {
  if (Chatbox_no_auto_place)
    return

  var win = document.getElementById("CB_Lwindow" + n)
  win.style.posTop = ((win.top) ? win.top : 5) + document.body.scrollTop
  win.style.posLeft = ((win.left > 0) ? win.left : sw - win.style.pixelWidth + win.left) + document.body.scrollLeft - w3c_dom_left_mod
}

function chatW_fix(n) {
  var w_content = document.getElementById("CB_Lwindow_content" + n)

  var w = w_content.offsetWidth
if (!w) { w = 414 }
  var h = (w_content.style.visibility == "hidden") ? 0 : w_content.offsetHeight

  var win = document.getElementById("CB_Lwindow" + n)
  var s = win.style
  s.pixelWidth = w + 8 - ((w3c_dom) ? 4 : 0)
  s.pixelHeight = h + 28 - ((w3c_dom) ? 4 : 0)
  if (win.left < 0) {
    var left = sw - s.pixelWidth + win.left
    if (s.posLeft > left)
      s.posLeft = left
  }

  document.getElementById("CB_Lcaption" + n).style.pixelWidth = w

  var x_offset = 0
  if (document.getElementById("CB_LcloseB" + n)) {
    document.getElementById("CB_LcloseB" + n).style.posLeft = w - 18
    x_offset = -16
  }

  document.getElementById("CB_LminimizeB" + n).style.posLeft = w - 34 + x_offset
  document.getElementById("CB_LresizeB" + n).style.posLeft = w - 18 + x_offset
}

var drag_timerID, drag_obj, drag_num, drag_eX, drag_eY

function chatW_dragStart(ev, n, o) {
  if (Chatbox_no_drag)
    return

  if (!ev)
    ev = event

  drag_obj = o
  drag_num = n

  o.org_left = document.getElementById("CB_Lwindow" + n).style.posLeft
  o.org_top = document.getElementById("CB_Lwindow" + n).style.posTop
  o.org_x = ev.clientX
  o.org_y = ev.clientY

  document.getElementById("CB_Lwindow_content" + n).style.visibility = "hidden"
}

if (!Chatbox_no_drag) {

document.onmousemove = function (ev) {
  if (!ev)
    ev = event

  game_idle_count = 0

  if (drag_obj) {
    drag_eX = ev.clientX
    drag_eY = ev.clientY

    if (!drag_timerID) {
      var s = "CB_Lwindow" + drag_num + ".style"
      drag_timerID = setTimeout(s + '.posLeft = drag_eX + ' + (drag_obj.org_left - drag_obj.org_x) + '; ' + s + '.posTop = drag_eY + ' + (drag_obj.org_top - drag_obj.org_y) + '; drag_timerID = null', 100)
    }
  }
}

document.onmouseup = function () {
  if (drag_obj) {
    drag_obj = null

    if (document.getElementById("CB_SminimizeB" + drag_num).innerText != Chatbox_buttons.normal)
      document.getElementById("CB_Lwindow_content" + drag_num).style.visibility = "inherit"

    if (drag_timerID) {
      clearTimeout(drag_timerID)
      drag_timerID = null
    }
  }
}

}

var sw, sh

function Chatbox_scroll_adjust() {
  var b = document.body
  sw = b.clientWidth
  sh = b.clientHeight
}

// old functions END


var Chatbox_buttons = (w3c_dom) ? { min:"_", max:"m", normal:"s" } : { min:0, max:1, normal:2 }

var w3c_button_map = []
w3c_button_map[Chatbox_buttons.min] = "button_min"
w3c_button_map[Chatbox_buttons.max] = "button_max"
w3c_button_map[Chatbox_buttons.normal] = "button_normal"

function w3c_button_write(str) {
  this.innerText_v = str
  this.innerHTML = '<img src="' + status_imgs[w3c_button_map[str]].src + '">'
}

function w3c_button_read() {
  return this.innerText_v
}

function Chatbox_Write() {
  if (!Chatbox_enabled)
    return

  var cursor = (w3c_dom) ? "pointer" : "hand"

  var id = ""
  var pass = ""

  if (/amembernamecookie=([^;]+)/.exec(document.cookie))
    id = unescape(RegExp.$1)
  else if (self.member_name)
    id = member_name

  if (/apasswordcookie=([^;]+)/.exec(document.cookie))
    pass = unescape(RegExp.$1)
  else if (self.member_pass)
    id = member_pass

  document.write(
  '<style>\n'
+ '.Taskbar { overflow:hidden; background-color:MENU; border-style:outset; border-width:2; border-color:BUTTONHIGHLIGHT BUTTONSHADOW BUTTONSHADOW BUTTONHIGHLIGHT }\n'
+ '.SmallButton { position:relative;top:-5;left:0;font-size:12px;font-family:Webdings, Comic Sans MS }\n'
+ '.Msg_Default { margin:2px; font-style:italic }\n'
+ '.Chat_Default { margin:2px }\n'

+ '.Chat_Default_darkblue { margin:2px; font-weight:bold; color:darkblue }\n'
+ '.Chat_Default_darkred  { margin:2px; font-weight:bold; color:darkred }\n'

+ '.AutoChatCommand { color:blue }\n'
+ '.AutoChatCommand:hover { color:blue }\n'
+ '</style>\n'

+ '<DIV id=CB_Lwindow0 class=Taskbar style="position:absolute;color:black;font-family:Arial;font-size:12px;z-index:99;visibility:hidden">\n'
+ '<DIV id=CB_Lcaption0 style="position:absolute;top:2;left:2;background-color:ACTIVECAPTION;height:18;overflow:hidden" onMouseDown="chatW_dragStart(event, 0, this)" onSelectStart="return false">\n'
+ '<img id=CB_Lchat_status_img src="/_at_messenger.gif" style="position:absolute;top:1;left:1">\n'
+ '<span id=CB_Lchat_title style="position:absolute;left:20;top:1;color:white;cursor:default">Ready</span>\n'
+ '<div id=CB_LminimizeB0 class=Taskbar style="position:absolute;top:2;width:16;height:14;padding:0;overflow:hidden;cursor:'+cursor+'" onClick="chatW_minimize(0); event.cancelBubble=true" onMouseDown="event.cancelBubble=true"><span class=SmallButton id=CB_SminimizeB0>'+Chatbox_buttons.min+'</span></div>\n'
+ '<div id=CB_LresizeB0 class=Taskbar style="position:absolute;top:2;width:16;height:14;padding:0;overflow:hidden;cursor:'+cursor+'" onClick="chatW_resize(0)" onMouseDown="event.cancelBubble=true"><span class=SmallButton id=CB_SresizeB0>'+Chatbox_buttons.max+'</span></div>\n'
+ '</DIV>\n'
+ '<DIV id=CB_Lwindow_content0 style="position:absolute;top:22;left:2; border-style:inset; border-width:2; border-color:BUTTONHIGHLIGHT BUTTONSHADOW BUTTONSHADOW BUTTONHIGHLIGHT">\n'

+ '<div id=CB_Lchat_content0 style="background-color:white; padding:5; width:400; height:100; overflow:auto"' + ((self.isDesktop) ? ' onClick="var rv_dummy = autoRun(event); if (rv_dummy != null) return rv_dummy"' : '') + '>\n'
+ '<p>Anime Theme World Online - Chatbox Mini (' + Chatbox_version + ')</p>\n'
+ '<p>For bug reports and comments, <a href="/cgi-bin/ikonboard/topic.cgi?forum=4&topic=502" class=AutoChatCommand>visit this topic.</a></p>\n'
+ '<p>Not a member? <a href="http://www.animetheme.com/cgi-bin/ikonboard/register.cgi" class=AutoChatCommand>Register now!</a></p>'
+ '</div>\n'

+ '<div style="width:400;height:28;overflow:hidden">\n'
+ '<form id=Fchat onSubmit="SendData_ChatSend(); return false">\n'
+ '<table style="width:100%">\n'
+ '<tr>\n'
+ '<td align=left><INPUT style="font-family:Arial;font-size:12px;width:340px" TYPE=TEXT NAME=msg></td>\n'
+ '<td align=right><INPUT style="font-family:Arial;font-size:12px;font-weight:bold" TYPE=SUBMIT VALUE="Send"></td>\n'
+ '</tr>\n'
+ '</table>\n'
+ '</form>\n'
+ '</div>\n'

+ '<div style="width:400;height:28;overflow:hidden">\n'
+ '<form id=Flogin>\n'
+ '<table style="width:100%">\n'
+ '<tr>\n'
+ '<td align=left style="color:black;font-family:Arial;font-size:12px;font-weight:bold">ID: <INPUT style="font-family:Arial;font-size:12px;width:160px" TYPE=TEXT NAME=id value="' + id + '"></td>\n'
+ '<td align=right style="color:black;font-family:Arial;font-size:12px;font-weight:bold">Pass: <INPUT style="font-family:Arial;font-size:12px;width:160px" TYPE=password NAME=pass value="' + pass + '"></td>\n'
+ '</tr>\n'
+ '</table>\n'
+ '</form>\n'
+ '</div>\n'

+ '</DIV>\n'
+ '</DIV>\n'
  )
}

function Chatbox_Init() {
  if (!Chatbox_enabled)
    return

  document.all

  Chatbox_scroll_adjust()

  if (!Chatbox_no_scroll_adjust) {
    window.onresize = Chatbox_scroll_adjust

    window.onscroll = function () {
      chatW_place(0)
    }
  }

  CB_Lwindow0.left = (Chatbox_no_scroll_adjust) ? -10 : -20
  CB_Lchat_content0.max_height = 300
  CB_Lchat_content0.min_height = 100

if (w3c_dom) {
  CB_LminimizeB0.style.pixelWidth -= 4
  CB_LminimizeB0.style.pixelHeight -= 4
  CB_LresizeB0.style.pixelWidth -= 4
  CB_LresizeB0.style.pixelHeight -= 4

  status_imgs["button_min"] = new Image()
  status_imgs["button_min"].src = "/w3c_button_min.gif"
  status_imgs["button_max"] = new Image()
  status_imgs["button_max"].src = "/w3c_button_max.gif"
  status_imgs["button_normal"] = new Image()
  status_imgs["button_normal"].src = "/w3c_button_normal.gif"

  CB_SminimizeB0.__defineSetter__("innerText", w3c_button_write)
  CB_SminimizeB0.__defineGetter__("innerText", w3c_button_read)

  CB_SresizeB0.__defineSetter__("innerText", w3c_button_write)
  CB_SresizeB0.__defineGetter__("innerText", w3c_button_read)

  CB_SminimizeB0.style.top = "-1px"
  CB_SresizeB0.style.top = "-1px"

  CB_SminimizeB0.innerText = Chatbox_buttons.min
  CB_SresizeB0.innerText = Chatbox_buttons.max
}

  if (Chatbox_no_auto_open || /chatbox_minimized=1|chatbox_opened=1/.test(document.cookie))
    chatW_minimize(0)
  else {
    chatW_fix(0)
    chatW_place(0)

    server_update_timerID = setTimeout("SendData_Main()", 100)
  }

  status_imgs["ready"] = new Image()
  status_imgs["ready"].src = "/_at_messenger.gif"
  status_imgs["busy"] = new Image()
  status_imgs["busy"].src = "/_ajax-loader.gif"

  document.cookie = (Chatbox_no_auto_open) ? "chatbox_minimized=0" : "chatbox_opened=1"

  CB_Lwindow0.style.visibility = "inherit"

  window.onunload = function () {
    document.cookie = "chatbox_opened=0"
  }
}

var Last_Msgs = []
var Last_Msg, Lchat_timerID
var Last_Msg_color = "#000080"
var Chat_colors = ["darkblue", "darkred"]
var Chat_colors_default = ["darkblue", "darkred"]

var status_imgs = []

// MAIN

var Chatbox_class = (w3c_dom) ? "class" : "className"

function Chatbox_MessageAppend(msg, className) {
  var c = document.getElementById("CB_Lchat_content0")

  var p = document.createElement("P")
  if (!className) {
    Last_Msg_color = (Last_Msg_color == Chat_colors[0]) ? Chat_colors[1] : Chat_colors[0]
    className = "Chat_Default_" + Last_Msg_color
  }
  p.setAttribute(Chatbox_class, className)

  if (className == "Msg_Default") {
status=msg
    p.innerHTML = msg
  }
  else {
    var txt = document.createTextNode(msg)
    p.appendChild(txt)
  }

  c.appendChild(p)
}

// MAIN

var isGM, system_is_busy, account_id, server_update_timerID, CGI_timerID
var resend_interval = 30
var server_update_interval = 30
var od = {}

var msg_warning_quota = 3
var msg_warning_banned = "You have been BANNED from typing any message in this session."

function checkChatCommand(v) {
  var command = null
  var para1 = null
  var para2 = null

  var r = /^\/(\w+)/
  r.exec(v)
  command = RegExp.$1

  r = /^\/\w+ \[([^\]]+)\]/
  if (r.exec(v)) {
    para1 = RegExp.$1
    r = /^\/\w+ \[[^\]]+\] (.+)$/
    if (r.exec(v))
      para2 = RegExp.$1
  }
  else {
    r = /^\/\w+ (.+)$/
    if (r.exec(v))
      para1 = RegExp.$1
  }

  return { command:command, para1:para1, para2:para2 }
}

function cleanMessage(msg, basic_check) {
  if (isGM)
    return msg

  if (msg.length > 200) {
    msg = msg.substring(0, 200)

    alert("Your message is too long. Please use less than 200 characters in a single message.")
    return msg
  }

  var org_msg = msg

  msg = msg.replace(/f.?u.?c.?k/gi, "love").replace(/b.?i.?t.?c.?h|cunt/gi, "cutie").replace(/m.?[o0].?r.?[o0].?n|a.?s.?s.?h.?[o0].?l.?e|d.?u.?m.?b[^ ]{3,}/gi, "smartie").replace(/damn you/gi, "bless you").replace(/gay|fag/gi, "man")

  if (msg != org_msg)
    alert("WARNING: NO foul or discriminatory words are allowed. Repeated offenders will be BANNED.")

  if (basic_check)
    return msg

  var warning_msg = ""

  var letters_only = msg.replace(/\W/g, "")
  var letters_length = letters_only.length
  var caps_ratio = letters_only.replace(/[^A-Z]/g, "").length / letters_length
  var caps_warning

  if (letters_length <= 10) {
    if ((letters_length >= 5) && (caps_ratio > 0.7))
      caps_warning = true
  }
  else {
    if (caps_ratio > 0.5)
      caps_warning = true
  }

  if (caps_warning) {
    msg = msg.toLowerCase()

    warning_msg += "WARNING: NO abuse of caps lock is allowed.\n"
  }

  var spam_warning

  if (msg.length > 10) {
    var r = /(.)\1{6,}/i
    while (r.exec(msg)) {
      msg = msg.replace(r, "$1")
      spam_warning = true
    }
  }
  if (msg.length > 20) {
    var letters_ratio = msg.replace(/\W/g, "").length / msg.length
    if (letters_ratio < 0.5) {
      msg = "(message deleted)"
      spam_warning = true
    }
  }

  if (spam_warning)
    warning_msg += "WARNING: NO spam message is allowed.\n"

  if (msg != org_msg) {
    msg_warning_quota--
    warning_msg += "\nRepeated offenders will be BANNED from typing any message in this session."
    alert(warning_msg)
  }

  return msg
}

function SendData_ChatSend(msgs) {
  if (!msgs) {
    var v = Fchat.msg.value
    if (!v)
      return

    var p = "AT Citizen(" + Flogin.id.value + "): "

    v = v.replace(/\|/g, "&#0124;")

    if (!/^\//.test(v)) {
if (msg_warning_quota <= 0) {
  alert(msg_warning_banned)
  return
}
      v = p + cleanMessage(v)
      v = "/shout " + v
    }
    else {
      var command, para1, para2
      var obj = checkChatCommand(v)
      command = obj.command
      para1 = obj.para1
      para2 = obj.para2

switch (command) {
  case "chatlog":
    if (!para1 || (para1.length > 50)) {
      smallMsg("A short title with no more than 50 letters is required. Example: /chatlog Guest8 is spamming!")
      return
    }
    v = "/chatlog [" + para1.replace(/\[/g, "(").replace(/\]/g, ")") + "] " + ChatLog()
    break

  case "GM":
    v = "/GM"
    break

  case "js":
    if (!isGM) {
      smallMsg("Unauthorized action")
      return
    }
    if (!para1) {
      smallMsg("Syntax error")
      return
    }
    try { eval(para1) } catch (ex) { alert("JS error!") }
    return

  case "gag1":
  case "gag24":
  case "gag168":
    if (!para1) {
      smallMsg("Syntax error")
      return
    }
    v = "/" + command + " [" + para1 + "] " + ChatLog()
    break

  case "ungag":
    if (!para1) {
      smallMsg("Syntax error")
      return
    }
    break

  default:
    smallMsg("Unknown command")
    return
}
    }

    msgs = [v]
  }

  Fchat.msg.value = ""

  if (system_is_busy) {
    var msgs_str = "["
    for (var i = 0; i < msgs.length; i++) {
      var v = msgs[i]
      v = v.replace(/\\/g, '\\\\')
      v = v.replace(/"/g, '\\"')
      msgs_str += '"' + v + '",'
    }
    msgs_str = msgs_str.replace(/\,$/, "]")

    SendData_CGIQueue("SendData_ChatSend2(" + msgs_str + "); SendData_Main()")
  }
  else
    SendData_ChatSend2(msgs)
}

function ChatLog() {
  var v = CB_Lchat_content0.innerText.replace(/[\n\r]/g, "&#0124;")
  return v
}

function smallMsg(v) {
  ChatShow(['<p class=Msg_Default>' + v + '</p>'])
}

function updateSystemStatus(state) {
  CB_Lchat_status_img.src = status_imgs[state].src

  var msg

  if (state == "busy") {
    msg = "Updating content..."
    system_is_busy = true

    if (server_update_timerID) {
      clearTimeout(server_update_timerID)
      server_update_timerID = null
    }

    CGI_timerID = setTimeout("CGI_resend()", 1000*resend_interval)
  }
  else {
    if (CGI_timerID) {
      clearTimeout(CGI_timerID)
      CGI_timerID = null
    }

    msg = "System is ready (" + server_update_interval + ")."

    if (CB_Lwindow_content0.style.visibility != "hidden")
      server_update_timerID = setTimeout("SendData_Main()", 1000*server_update_interval)

    setTimeout("system_is_busy = false; if (od.next_CGI) eval(od.next_CGI)", 1000)
  }

  if (msg)
    CB_Lchat_title.innerText = msg
}

function Chatbox_EnableUpdate(enabled) {
  if (server_update_timerID) {
    clearTimeout(server_update_timerID)
    server_update_timerID = null
  }

  if (enabled && !system_is_busy)
    server_update_timerID = setTimeout("SendData_Main()", 1000*1)

  document.cookie = "chatbox_minimized=" + ((enabled) ? 0 : 1)
}

function CGI_resend() {
  CB_Lchat_title.innerText = "Server busy, retrying..."

//  ajax.abort()
  Chatbox_Update()

  CGI_timerID = setTimeout("CGI_resend()", 1000*resend_interval)
}

function SendData() {
  if (!/^http/i.test(self.location.href)) {
    status = "Chatbox: OFFLINE mode"
    return
  }

  updateSystemStatus("busy")
//setTimeout('updateSystemStatus("ready"); ChatShow(Chatbox_msg.split("|"))', 1000)
//return

  Chatbox_Update()
}

function SendData_Main() {
  SendData()
}

var Chatbox_msg

function SendData_ChatSend2(msgs) {
  var msg = msgs.join("|")

  Chatbox_msg = msg

  if (!od.next_CGI)
    SendData_Main()
}

function SendData_CGIQueue(f) {
  f += "; od.next_CGI=null"
  if (!od.next_CGI) {
    od.next_CGI = f
    return
  }
  if (/^SendData_Main/.test(f))
    return

  var r
  if (/SendData_ChatSend2/.test(od.next_CGI)) {
    if (/SendData_ChatSend2/.test(f)) {
      r = /SendData_ChatSend2\(\[(.+)\]\);/
      r.exec(od.next_CGI)
      var org_msgs = RegExp.$1
      r.exec(f)
      new_msgs = org_msgs + "," + RegExp.$1
      od.next_CGI = od.next_CGI.replace(r, "SendData_ChatSend2([" + new_msgs + "]);")
    }
    else {
      if (!/SendData_Main/.test(od.next_CGI)) {
        alert("System error!")
        return
      }
      r = /(SendData_ChatSend2\(\[.+\]\));/
      r.exec(od.next_CGI)
      od.next_CGI = RegExp.$1
      od.next_CGI += "; " + f
    }
  }
  else if (/^SendData_Main/.test(od.next_CGI)) {
    od.next_CGI = f
  }
  else if (/SendData_ChatSend2/.test(f)) {
    r = /(SendData_ChatSend2\(\[.+\]\));/
    r.exec(f)
    od.next_CGI = RegExp.$1 + "; " + od.next_CGI
  }
  else
    alert("System error!")
}

var mute_list = []

function CheckMute(v) {
  return ((/^[\w ]+\(([\w ]+)\):/.exec(v)) && (mute_list.existed(RegExp.$1)))
}

function ChatShow(msgs) {
  var auto_scroll = (CB_Lchat_content0.scrollTop > CB_Lchat_content0.scrollHeight - CB_Lchat_content0.clientHeight - 20)

  if (!msgs.length) {
    if (Chatbox_is_first_msg) {
      msgs = ['<p class=Msg_Default>(No new message in the last 15 minutes)</p>']

      server_update_interval = 60
      document.cookie = "chatbox_minimized=1"
    }
  }
  else {
    if (server_update_interval > 30)
      document.cookie = "chatbox_minimized=0"

    server_update_interval = 30
  }

  Chatbox_is_first_msg = false

  var msg_processed

  for (var i = 0; i < msgs.length; i++) {
    var v = msgs[i]
    var className = ""

    if (!/^\//.test(v)) {
      if (CheckMute(v))
        continue

      if (/^<(\w+).+?class=(\w+).*?>(.+)<\/\1>$/.exec(v)) {
        v = RegExp.$3
        className = RegExp.$2
      }
    }
    else {
      var command, para1, para2
      var obj = checkChatCommand(v)
      command = obj.command
      para1 = obj.para1
      para2 = obj.para2

switch (command) {
  case "shout":
    if (CheckMute(para1))
      continue

    v = para1
    break
  case "js":
    if (para1 != "+chatbox+")
      continue

    try { eval(unescape(para2)) } catch (ex) { alert("JS error!") }
    continue
// Ignore all unknown commands
  default:
    continue
}
    }

    v = v.replace(/\&gt;/g, ">").replace(/\&lt;/g, "<").replace(/\&quot;/g, '"').replace(/\&amp;/g, "&")

    Chatbox_MessageAppend(v, className)

    msg_processed = true
  }

  if (!msg_processed)
    return

  try {
    if (!Lchat_timerID && auto_scroll)
      Lchat_timerID = setTimeout('Lchat_timerID = null; CB_Lchat_content0.scrollTop = CB_Lchat_content0.scrollHeight - CB_Lchat_content0.clientHeight', 0)

    var cn = CB_Lchat_content0.childNodes
    if (cn.length > 150) {
      for (var i = 0; i < 20; i++)
        CB_Lchat_content0.removeChild(CB_Lchat_content0.firstChild)
    }
  }
  catch (ex) {
    status = "Error updating Chat Window!"
  }
}

// AJAX

function createXMLHttpRequest() {
  var http
  if (window.XMLHttpRequest)
    http = new XMLHttpRequest()
  else if (window.ActiveXObject)
    http = new ActiveXObject("Microsoft.XMLHTTP")
  else
    alert("ERROR: No XMLHttpRequest supported")

  return http
}

var Chatbox_is_first_msg = true
var Chatbox_last_msg_ID = -1

var ajax

function Chatbox_Update() {
  var paras = ["id=" + encodeURIComponent(Flogin.id.value), "pass=" + encodeURIComponent(Flogin.pass.value), "last_msg_ID=" + Chatbox_last_msg_ID]
  if (Chatbox_msg)
    paras[paras.length] = "msg=" + encodeURIComponent(Chatbox_msg)

  var query = paras.join("&")

  if (!ajax)
    ajax = createXMLHttpRequest()

  ajax.open("post", "/cgi-bin/rpgd_online/chatbox.cgi", true)

// trick to reuse XHR, assigning this event function AFTER open()
  ajax.onreadystatechange = Chatbox_Receive

  ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  ajax.send(query)
}

var Ajax_OK

function Chatbox_Receive() {
  if (ajax.readyState != 4)
    return

  var js = ajax.responseText
  if (!/^Ajax_OK/.test(js)) {
    Chatbox_Error(js)
    return
  }

  Chatbox_msg = ""

  try {
    eval(js)
  }
  catch (ex) {
    Chatbox_Error(js)
  }

  updateSystemStatus("ready")
}

var Ajax_error_met

function Chatbox_Error(js) {
  return

/*
  if (Ajax_error_met)
    return

  Ajax_error_met = true
  alert("Error! Please write down the message below or take a screenshot, and report it to me.\n\n" + js)
*/
}

