var LastMsg = ''
var LastOnlineCount = ''
var PostMsgXmlHttp
var PmTo = ''
var LastMsgType = 0

// message exchange system

lastrequest = 0
firstrequest = true
requesting = false
idlecount = 0
wonline = ''
wonlinearray = new Array()
wonlinechannelarray = new Array()
cvid = ''
cvmsgs = ''
firstmessage = new Array()
lastmessage = 0
noflood = false
t = 0;
channels = new Array()

function am(id, message, userid, username, title, time, pm, deleted, channel) { // add message, called by PHP script only
  if (!channel) channel = 1
  if (!document.getElementById('chatchannel' + channel)) return null
  if (document.getElementById('msg' + id + 'container')) return null

  if ((!firstrequest) && (channel == currentchannel)) PlaySound(1)
  
  //if (cvmsgs == '') document.getElementById('chatview').innerHTML = ''
  
  if (!deleted) {
    cvm = cvmsgs.split(',')
    from = Math.max(cvm.length - 11, 0)
    cvmsgs = ''
    dontadd = false
    for (i = from; i < cvm.length; i++) {
      if (cvmsgs != '') cvmsgs = cvmsgs + ','
      cvmsgs = cvmsgs + cvm[i]
      if (id == cvm[i]) dontadd = true
    }
    if (dontadd) return false
    if (cvmsgs != '') cvmsgs = cvmsgs + ','
    cvmsgs = cvmsgs + id
  }
  
  firstmessage[channel] = Math.min(firstmessage[channel], id)
  lastmessage = Math.max(lastmessage, id)
  
  pmstyle = ' border-style: solid;'
  pmtext = ''
  if (pm) {
    pmstyle = ' border-style: dashed;'
    pmtext = ' (to ' + pm + ')'
  }
    
  showdelete = (loggedinuserid == userid) || (loggedinuserpermissions > 1)
  del = ''
  if (showdelete) del = ' (<a href=\"javascript:void(null);\" onclick=\"DeleteMessage(' + id + ');\">x</a>)'
  
  if (deleted) {
    deleted = ' faded'
  } else {
    deleted = ''
  }
  
  posthtml = ''
  posthtml = posthtml + '<div title="Posted on ' + time + '" id="msg' + id + 'container" class=\"bgc_light' + deleted + '\" style="margin: 10px 10px 10px 10px; width: 580px;' + pmstyle + '">'
  posthtml = posthtml + '<table border=0 cellspacing=1 cellpadding=2 width=100%>'
  posthtml = posthtml + '<tr>'
  posthtml = posthtml + '<td valign=center align=left><a href="viewprofile.php?mid=' + userid + '" title="View ' + username + '\'s profile" target="_blank"><b>' + username + '</b></a>' + pmtext + '</td>'
  posthtml = posthtml + '<td valign=center align=right>' + title + del
  posthtml = posthtml + '</td>'
  posthtml = posthtml + '</tr>'
  posthtml = posthtml + '<tr>'
  posthtml = posthtml + '<td colspan=2 class="bgc_light" style="border-top-style: solid;">'
  posthtml = posthtml + message
  posthtml = posthtml + '</td>'
  posthtml = posthtml + '</tr>'
  posthtml = posthtml + '</table>'
  posthtml = posthtml + '</div>'
  
  if (userid == 1) {
    posthtml = ''
    posthtml = posthtml + '<div title="Posted on ' + time + '" id="msg' + id + 'container" style="border: white 0px none; margin: 10px 10px 10px 10px; width: 580px;' + pmstyle + '">'
    posthtml = posthtml + '<center><i>' + message + '</i></center>'
    posthtml = posthtml + '</div>'
  }
  
  var chatview_innerhtml = document.getElementById('chatchannel' + channel).innerHTML
  document.getElementById('chatchannel' + channel).innerHTML = ''
  chatview_innerhtml = posthtml + chatview_innerhtml
  document.getElementById('chatchannel' + channel).innerHTML = chatview_innerhtml

  idlecount = 0
}

function amb(id, message, userid, username, title, time, pm, deleted, channel) { // add message on the bottom of the list (for TakeMeBack thingy), called by PHP script only
  if (!channel) channel = 1
  if (!document.getElementById('chatchannel' + channel)) return null

  firstmessage[channel] = Math.min(firstmessage[channel], id)
  lastmessage = Math.max(lastmessage, id)
  
  pmstyle = ' border-style: solid;'
  pmtext = ''
  if (pm) {
    pmstyle = ' border-style: dashed;'
    pmtext = ' (to ' + pm + ')'
  }
  
  showdelete = (loggedinuserid == userid) || (loggedinuserpermissions > 1)
  del = ''
  if (showdelete) del = ' (<a href=\"javascript:void(null);\" onclick=\"DeleteMessage(' + id + ');\">x</a>)'
  
  if (deleted) {
    deleted = ' faded'
  } else {
    deleted = ''
  }
  
  posthtml = ''
  posthtml = posthtml + '<div title="Posted on ' + time + '" id="msg' + id + 'container" class=\"bgc_light' + deleted + '\" style="margin: 10px 10px 10px 10px; width: 580px;' + pmstyle + '">'
  posthtml = posthtml + '<table border=0 cellspacing=1 cellpadding=2 width=100%>'
  posthtml = posthtml + '<tr>'
  posthtml = posthtml + '<td valign=center align=left><a href="viewprofile.php?mid=' + userid + '" title="View ' + username + '\'s profile" target="_blank"><b>' + username + '</b></a>' + pmtext + '</td>'
  posthtml = posthtml + '<td valign=center align=right>' + title + del
  posthtml = posthtml + '</td>'
  posthtml = posthtml + '</tr>'
  posthtml = posthtml + '<tr>'
  posthtml = posthtml + '<td colspan=2 class="bgc_light" style="border-top-style: solid;">'
  posthtml = posthtml + message
  posthtml = posthtml + '</td>'
  posthtml = posthtml + '</tr>'
  posthtml = posthtml + '</table>'
  posthtml = posthtml + '</div>'
  
  if (userid == 1) {
    posthtml = ''
    posthtml = posthtml + '<div title="Posted on ' + time + '" id="msg' + id + 'container" style="border: white 0px none; margin: 10px 10px 10px 10px; width: 580px;' + pmstyle + '">'
    posthtml = posthtml + '<center><i>' + message + '</i></center>'
    posthtml = posthtml + '</div>'
  }

  document.getElementById('chatchannel' + channel).innerHTML += posthtml
  
  // note: no idlecount reset!
}

function rm(id) { // remove message, called by PHP script only
  cvmsgs = ',' + cvmsgs + ','
  eval('cvmsgs = cvmsgs.replace(/,' + id + ',/, \',\')')
  if (cvmsgs != '') cvmsgs = cvmsgs.substring(1, cvmsgs.length-1)
  
  m = document.getElementById('msg' + id + 'container')
  if (m) {
    if (loggedinuserpermissions > 1) {
      m.className = 'bgc_light faded'
    } else {
      m.style.display = 'none'
    }
  }

  idlecount = 0
}

function ac(channel, name) {  // add channel, called by PHP script only
  channels[channel] = name
  firstmessage[channel] = 2000000000;
  
  if (channel == currentchannel) {
    display = 'block'
  } else {
    display = 'none'
  }

  if (!document.getElementById('chatchannel' + channel)) {
    document.getElementById('chatchannels').innerHTML += '<div id="chatchannel' + channel + '" style="display: ' + display + ';"></div>'
  }
}

function uc() {  // update channel list, called by PHP script only
  setTimeout('uc_()', 200) // this fixes some strange bug... i think
}

function ao(userid, username, channel) {  // add online, called by PHP script only
  wonlinearray[userid] = username
  wonlinechannelarray[userid] = channel
  
  if ((!firstrequest) && (channel == currentchannel)) PlaySound(2)

  idlecount = 0
}

function co(userid, channel) {  // change channel online, called by PHP script only
  wonlinechannelarray[userid] = channel
  
  if ((!firstrequest) && (channel == currentchannel)) PlaySound(2)

  idlecount = 0
}

function ro(userid) { // remove online, called by PHP script only
  wonlinearray[userid] = ''
  wonlinechannelarray[userid] = ''
  
  idlecount = 0
}

function uwo() { // update who's online field, called by PHP script only
  setTimeout('uwo_()', 200) // don't you just hate bugs?
}


function uc_() {
  var ihtml = ''
  for (var i = 0; i < channels.length; i++) {
    if (channels[i]) {
      if (i == currentchannel) {
        ihtml += '<div align=left style="margin-bottom: 5px; font-size: 8pt;"><a href="javascript:void(null);" onclick="switchchannel(' + i + ');"><b>' + channels[i] + '</b></a></div>'
      } else {
        ihtml += '<div align=left style="margin-bottom: 5px; font-size: 8pt;"><a href="javascript:void(null);" onclick="switchchannel(' + i + ');">' + channels[i] + '</a></div>'
      }
    }
  }
  document.getElementById('channels').innerHTML = ihtml
}

function uwo_() {
  wonline = ''
  var ihtmlthischannel = ''
  var ihtmlotherchannel = ''
  for (var i = wonlinearray.length - 1; i > 0; i--) {
    if (wonlinearray[i]) {
      if (wonlinechannelarray[i] == currentchannel) {
        ihtmlthischannel += '<div align=left style="margin-bottom: 5px; font-size: 8pt;"><a href="viewprofile.php?mid=' + i + '">' + wonlinearray[i] + '</a></div>'
      } else {
        ihtmlotherchannel += '<div align=left style="margin-bottom: 5px; font-size: 8pt;" title="Channel: ' + channels[wonlinechannelarray[i]] + '"><a href="viewprofile.php?mid=' + i + '">' + wonlinearray[i] + '</a></div>'
      }
      if (wonline != '') wonline += ';'
      wonline += i + ',' + wonlinechannelarray[i]
    }
  }
  if (ihtmlthischannel == '') ihtmlthischannel = '<div align=left style="margin-bottom: 5px; font-size: 8pt;" class="grayed">No one</div>'
  if (ihtmlotherchannel == '') {
    document.getElementById('whosonline').innerHTML = '<div align=left style="margin-bottom: 5px; font-size: 8pt;">' + channels[currentchannel] + ':</div>' + ihtmlthischannel
  } else {
    document.getElementById('whosonline').innerHTML = '<div align=left style="margin-bottom: 5px; font-size: 8pt;">' + channels[currentchannel] + ':</div>' + ihtmlthischannel + '<div align=left style="margin-top: 10px; margin-bottom: 5px; font-size: 8pt;">Other channels:</div>' + ihtmlotherchannel
  }
}

function Loop() { // main looping function
  setTimeout('Loop()', 6250)
  idlecount++
  RequestChatInfo()
}

function RequestChatInfo() { // refresh chatroom request
  if (requesting && xmlHttp) xmlHttp.abort()
  requesting = true
  req = 'chatroom_v2_request.php?lm=' + lastmessage + '&o=' + wonline + '&t=' + t
  AJAXrequest(req, RecievedChatInfoResult);

  if (xmlHttp == null) {
    document.getElementById('chatchannels').innerHTML = 'Insufficient browser support for chatroom. Sorry!'
  }
}

function TakeMeBack() { // the archive-alike thingy
  if (requesting && xmlHttp) xmlHttp.abort()
  requesting = true
  req = 'chatroom_v2_request.php?lm=' + lastmessage + '&o=' + wonline + '&action=tmb&ch=' + currentchannel + '&fm=' + firstmessage[currentchannel] + '&t=' + t
  AJAXrequest(req, RecievedChatInfoResult);

  if (xmlHttp == null) {
    document.getElementById('chatchannels').innerHTML = 'Insufficient browser support for chatroom. Sorry!'
  }
}

function RecievedChatInfoResult() { // refresh chatroom result
  if (xmlHttp.readyState != 4) return false
  
  if (firstrequest) document.getElementById('chatchannels').innerHTML = ''
  
  eval(xmlHttp.responseText)
  
  uc()
  uwo()
  
  requesting = false
  firstrequest = false
}

// posting & removing

function PostMessage() { // post message UI
  ok = true
  if (LastMsgType == 2) ok = confirm('Your last post was a PM. Are you sure you want to post globally?')
  if (!ok) return
  PostMessageCore(document.getElementById('message').value)
  LastMsgType = 1
}

function PostMessageTo() { // post private message UI
  message = document.getElementById('message').value
  to = prompt('To who do you want to send the PM?', PmTo)
  if (to == null) return
  if ((to == '') && (PmTo == '')) return
  if ((to == '') && (PmTo != '')) to = PmTo
  PmTo = to
  message = message + '[pm=' + to + ']'
  PostMessageCore(message)
  LastMsgType = 2
}

function PostMessageCore(message) { // post message request
  if (!noflood) {
    noflood = true
    setTimeout('noflood = false;', 1000)
    data = 'message=' + encodeURIComponent(message)
    PostMsgXmlHttp=GetXmlHttpObject()
    if (PostMsgXmlHttp==null) {
      messageform.submit()
      return
    }
    PostMsgXmlHttp.onreadystatechange = PostMessageAnswer
    PostMsgXmlHttp.open('POST', 'chatroom_v2_request.php?lm=' + lastmessage + '&o=' + wonline + '&action=post&ch=' + currentchannel + '&t=' + t, true)
    PostMsgXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
    PostMsgXmlHttp.setRequestHeader('Content-Length', data.length)
    PostMsgXmlHttp.send(data)
  
    document.getElementById('statusfield').innerHTML = 'Sending message...'
  }
}

function DeleteMessage(message) { // delete message request
  if (!confirm('Are you sure you want to remove this post?')) return null

  PostMsgXmlHttp=GetXmlHttpObject()
  if (PostMsgXmlHttp==null) {
    messageform.submit()
    return
  }
  PostMsgXmlHttp.onreadystatechange = PostMessageAnswer
  PostMsgXmlHttp.open('GET', 'chatroom_v2_request.php?lm=' + lastmessage + '&o=' + wonline + '&action=delete&msg=' + message + '&t=' + t, true)
  PostMsgXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
  PostMsgXmlHttp.send(null)
  
  document.getElementById('statusfield').innerHTML = 'Deleting message...'
}

function PostMessageAnswer() { // post & delete message result
  if (PostMsgXmlHttp.readyState != 4) return null
  
  eval(PostMsgXmlHttp.responseText)
  
  if (wonline == '') {
    document.getElementById('whosonline').innerHTML = '<b>Who\'s online</b><br><br>No one is online right now.'
  }
}

function clearscreen() {
  if (!document.getElementById('chatchannel' + currentchannel)) return false
  document.getElementById('chatchannel' + currentchannel).innerHTML = ''
  firstmessage[currentchannel] = 2000000000
}

// channels

function switchchannel(newchannel) {
  if (!document.getElementById('chatchannel' + newchannel)) return null
  if (!channels[newchannel]) return null
  
  if (document.getElementById('chatchannel' + currentchannel)) document.getElementById('chatchannel' + currentchannel).style.display = 'none'
  if (document.getElementById('chatchannel' + newchannel)) document.getElementById('chatchannel' + newchannel).style.display = 'block'
  
  currentchannel = newchannel
  if (loggedinuserid != '') {
    wonlinechannelarray[loggedinuserid] = newchannel;
  }
  uc_()
  uwo_()
  
  if (requesting && xmlHttp) xmlHttp.abort()
  requesting = true
  req = 'chatroom_v2_request.php?lm=' + lastmessage + '&o=' + wonline + '&nc=' + newchannel + '&t=' + t
  AJAXrequest(req, RecievedChatInfoResult);
  if (xmlHttp == null) {
    document.getElementById('chatchannels').innerHTML = 'Insufficient browser support for chatroom. Sorry!'
  }
}

// hotkeys

function HotKeyHandler(evt) {
  evt = (evt) ? evt : ((event) ? event : null)
  if (evt == null) return
  if ((evt.keyCode == 13) && (evt.ctrlKey) && (!evt.altKey) && (!evt.shiftKey)) {
    PostMessage()
  }
  if ((evt.keyCode == 13) && (evt.ctrlKey) && (evt.altKey) && (!evt.shiftKey)) {
    PostMessageTo()
  }
  if ((evt.keyCode == 13) && (!evt.ctrlKey) && (!evt.altKey) && (evt.shiftKey)) {
    document.getElementById('message').focus()
  }
}

// sounds

function PlaySound(which) {
  if (which == '1') {
    if (Sound == 1) PlaySoundCore('MP3Player/MP3/newmsg_soft.mp3')
    if (Sound == 2) PlaySoundCore('MP3Player/MP3/newmsg_loud.mp3')
  } 
  if (which == '2') {
    if (Sound == 1) PlaySoundCore('MP3Player/MP3/online_soft.mp3')
    if (Sound == 2) PlaySoundCore('MP3Player/MP3/online_loud.mp3')
  }
}

function PlaySoundCore(filename) {
  if (!document.getElementById('ep_player')) return false
  document.getElementById('ep_player').EP_loadMP3('<location>' + filename + '</location><creator>Song Creator</creator><title>Song Title</title>')
  document.getElementById('ep_player').EP_play()
}

function ChangeSoundLevel() {
  Sound--
  if (Sound == -1) Sound = 2
  if (Sound == 0) document.getElementById('SoundLevel').innerHTML = 'off'
  if (Sound == 1) document.getElementById('SoundLevel').innerHTML = 'soft'
  if (Sound == 2) document.getElementById('SoundLevel').innerHTML = 'loud'
  setCookie("htf_ChatroomSoundLevel", Sound, getExpDate(712, 0, 0));
}

// end function declaration

document.onkeyup = HotKeyHandler;




// editor (cached version taken from Comment functions)

function emoticon(text, id) {
  e = document.getElementById('message' + id)
  if (!e) return false

  // COPYRIGHT NOTICE
  // 
  // Part of this function is inspired by a javascript for text insertion by the
  // phpMyAdmin group. phpMyAdmin is distributed under the GNU GPL:
  // http://www.phpmyadmin.net/documentation/LICENSE
  //
  // http://www.phpmyadmin.net/
  
  if (document.selection) {
    e.focus()
    sel = document.selection.createRange()
    sel.text = sel.text + text
  } else if (e.selectionStart || e.selectionStart == '0') {
    var startPos = e.selectionStart
    var endPos = e.selectionEnd
    e.value = e.value.substring(0, endPos) + text + e.value.substring(endPos, e.value.length)
    e.selectionStart = endPos + text.length
    e.selectionEnd = endPos + text.length
  } else {
    e.value += text
  }
  e.focus()
}

function surround(opentag, closetag, id) {
  e = document.getElementById('message' + id)
  if (!e) return false

  // COPYRIGHT NOTICE
  // 
  // Part of this function is inspired by a javascript for text insertion by the
  // phpMyAdmin group. phpMyAdmin is distributed under the GNU GPL:
  // http://www.phpmyadmin.net/documentation/LICENSE
  //
  // http://www.phpmyadmin.net/
  
  if (document.selection) {
    e.focus()
    sel = document.selection.createRange()
    sel.text = opentag + sel.text + closetag
  } else if (e.selectionStart || e.selectionStart == '0') {
    var startPos = e.selectionStart
    var endPos = e.selectionEnd
    e.value = e.value.substring(0, startPos) + opentag + e.value.substring(startPos, endPos) + closetag + e.value.substring(endPos, e.value.length)
    if (startPos - endPos == 0) {
      e.selectionStart = startPos + opentag.length
      e.selectionEnd = startPos + opentag.length
    } else {
      e.selectionStart = startPos
      e.selectionEnd = endPos + opentag.length + closetag.length
    }
  } else {
    e.value += opentag + closetag
  }
  e.focus()
}

function getSelectionLength(id) {
  e = document.getElementById('message' + id)
  if (!e) return false

  // COPYRIGHT NOTICE
  // 
  // Part of this function is inspired by a javascript for text insertion by the
  // phpMyAdmin group. phpMyAdmin is distributed under the GNU GPL:
  // http://www.phpmyadmin.net/documentation/LICENSE
  //
  // http://www.phpmyadmin.net/
  
  if (document.selection) {
    e.focus()
    sel = document.selection.createRange()
    return sel.text.length
  } else if (e.selectionStart || e.selectionStart == '0') {
    var startPos = e.selectionStart
    var endPos = e.selectionEnd
    return endPos - startPos
  } else {
    return 0
  }
}

var editorsize = new Array()

function validatereply() {
  id = 1
  while (document.getElementById('message' + id)) {
    text = document.getElementById('message' + id).value.trim()
    err = false
    if (text.length == 0) err = 'You have to enter a message'
    if (text.length > 50000) err = 'Your message is too long'
    if (document.getElementById('submitbutton' + id)) {
      if (err == false) {
        document.getElementById('submitbutton' + id).value = 'Submit'
        document.getElementById('submitbutton' + id).disabled = false
      } else {
        document.getElementById('submitbutton' + id).value = err
        document.getElementById('submitbutton' + id).disabled = true
      }
    }
    postpmupdate(id)
    
    id++
  }
  setTimeout('validatereply()', 500)
}

function PromptColor(id) {
  var html = '<div style="margin: 5px 3px 3px 5px;">';
  colors = Array(
  '000000', '800000', '804000', '808000', '408000', '008000', '008040',
  '404040', 'FF0000', 'FF8000', 'FFFF00', '80FF00', '00FF00', '00FF80',
  '808080', 'FF8080', 'FFC080', 'FFFF80', 'C0FF80', '80FF80', '80FFC0',
  'C0C0C0', 'FF80C0', 'FF80FF', 'C080FF', '8080FF', '80C0FF', '80FFFF',
  'FFFFFF', 'FF0080', 'FF00FF', '8000FF', '0000FF', '0080FF', '00FFFF',
  '',       '800040', '800080', '400080', '000080', '004080', '008080')
  for (i = 0; i < 42; i++) {
    html = html + '<a href="javascript:Color(\'#' + colors[i] + '\', \'' + id + '\');"><img src="Images/Templates/' + GlobalStyle + '/Editor/ColorSquare.gif" border=0 style="background-color: ' + colors[i] + '; margin: 0px 2px 2px 0px;"></a>'
  }
  html = html + '</div>'
  Popup('ColorButton' + id, html, '110px', '', 'visible', 'center')
}

function Color(color, id) {
  if (color == '#') color = 'transparent'
  HidePopup()
  surround('[color=' + color + ']', '[/color]', id)
}

function PromptBackgroundColor(id) {
  var html = '<div style="margin: 5px 3px 3px 5px;">'
  var colors = Array(
  '000000', '800000', '804000', '808000', '408000', '008000', '008040',
  '404040', 'FF0000', 'FF8000', 'FFFF00', '80FF00', '00FF00', '00FF80',
  '808080', 'FF8080', 'FFC080', 'FFFF80', 'C0FF80', '80FF80', '80FFC0',
  'C0C0C0', 'FF80C0', 'FF80FF', 'C080FF', '8080FF', '80C0FF', '80FFFF',
  'FFFFFF', 'FF0080', 'FF00FF', '8000FF', '0000FF', '0080FF', '00FFFF',
  '',       '800040', '800080', '400080', '000080', '004080', '008080')
  for (i = 0; i < 42; i++) {
    if (colors[i] == '') {
      html = html + '<img src="Images/Templates/' + GlobalStyle + '/Editor/ColorSquare.gif" border=0 style="margin: 0px 2px 2px 0px; visibility: hidden;">'
    } else {
      html = html + '<a href="javascript:BackgroundColor(\'#' + colors[i] + '\', \'' + id + '\');"><img src="Images/Templates/' + GlobalStyle + '/Editor/ColorSquare.gif" border=0 style="background-color: ' + colors[i] + '; margin: 0px 2px 2px 0px;"></a>'
    }
  }
  html += '</div>'
  Popup('BGButton' + id, html, '110px', '', 'visible', 'center')
}

function BackgroundColor(color, id) {
  if (color == '#') color = 'transparent'
  HidePopup()
  surround('[background=' + color + ']', '[/background]', id)
}

function PromptFont(id) {
  var html = '';
  var fonts = Array('Arial', 'Arial Black', 'Arial Narrow', 'Book Antiqua', 'Century Gothic', 'Comic Sans MS', 'Courier New', 'Fixedsys', 'Franklin Gothic Medium', 'Garamond', 'Georgia', 'Impact', 'Lucida Console', 'Lucida Sans Unicode', 'Microsoft Sans Serif', 'Palatino Linotype', 'System', 'Tahoma', 'Times New Roman', 'Trebuchet MS', 'Verdana');
  for (i = 0; i < fonts.length; i++) {
    html = html + '<div class="bgc_verylight" style="padding: 2px 2px 0px 2px; height: 20px; font-family: ' + fonts[i] + ';" onclick="Font(\'' + fonts[i] + '\', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';">' + fonts[i] + '</div>'
  }
  Popup('FontButton' + id, html, '170px', '200px', 'auto', 'left')
}

function Font(font, id) {
  HidePopup()
  surround('[font=' + font + ']', '[/font]', id)
}

function PromptSize(id) {
  var html = '';
  for (i = 1; i < 8; i++) {
    html = html + '<div class="bgc_verylight" style="padding: 0px 2px 0px 8px;" align=left onclick="Size(' + i + ', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';"><font size=' + i + '>' + i + '</font></div>'
  }
  html = html + '<hr width=70 class="faded" align=center>'
  html = html + '<div class="bgc_verylight" style="padding: 0px 2px 0px 2px;" align=left onclick="HidePopup();surround(\'[sup]\', \'[/sup]\', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';"><i>x</i><sup>superscript</sup></div>'
  html = html + '<div class="bgc_verylight" style="padding: 0px 2px 0px 2px;" align=left onclick="HidePopup();surround(\'[sub]\', \'[/sub]\', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';"><i>x</i><sub>subscript</sub></div>'
  Popup('SizeButton' + id, html, '80px', '', 'auto', 'left')
}

function Size(size, id) {
  HidePopup()
  surround('[size=' + size + ']', '[/size]', id);
}

function PromptAlign(id) {
  var html = '';
  html = html + '<div class="bgc_verylight" style="padding: 0px 2px 0px 2px;" align=left onclick="HidePopup();surround(\'[align=left]\',   \'[/align]\', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';">Left</div>'
  html = html + '<div class="bgc_verylight" style="padding: 0px 2px 0px 2px;" align=left onclick="HidePopup();surround(\'[align=center]\', \'[/align]\', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';">Center</div>'
  html = html + '<div class="bgc_verylight" style="padding: 0px 2px 0px 2px;" align=left onclick="HidePopup();surround(\'[align=right]\',  \'[/align]\', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';">Right</div>'
  html = html + '<div class="bgc_verylight" style="padding: 0px 2px 0px 2px;" align=left onclick="HidePopup();surround(\'[align=justify]\',\'[/align]\', \'' + id + '\');" onmouseover="this.className=\'bgc_veryverylight\';" onmouseout="this.className=\'bgc_verylight\';">Justify</div>'
  Popup('AlignButton' + id, html, '80px', '', 'auto', 'left')
}

function PromptUrl(id) {
  val = prompt('Please enter the URL of the link.', 'http://');
  if (!val) {
    return;
  }
  if (getSelectionLength(id) > 0) {
    surround('[url=' + val + ']', '[/url]', id);
  } else {
    emoticon('[url=' + val + ']' + val + '[/url]', id);
  }
}

function PromptImage(id) {
  val = prompt('Please enter the URL to the image. You can not upload images here.', 'http://');
  if (!val) {
    return;
  }
  emoticon('[img]' + val + '[/img]', id);
}

function Popup(button, text, width, height, overflow, align) {
  popup = document.getElementById('commentPopupDiv')
  popupbg = document.getElementById('commentPopupDivBackground')
  popupbg2 = document.getElementById('commentPopupDivBackground2')
  
  popup.style.height = height
  popup.style.width = width
  popup.style.overflow = overflow
  popup.style.textAlign = align
  popup.innerHTML = text
  popupbg.style.height = (Math.max((window.innerHeight || 0), document.body.clientHeight, document.documentElement.clientHeight, document.body.scrollHeight, document.documentElement.scrollHeight) - 5) + 'px'
  popupbg2.style.width = '20px'

  if (document.selection) {
    popup.style.left = (XPos(button) + 0) + 'px'
    popup.style.top = (YPos(button) + 22) + 'px'
    popupbg2.style.left = (XPos(button) + 1) + 'px'
    popupbg2.style.top = (YPos(button) + 20) + 'px'
  } else {
    popup.style.left = XPos(button) + 'px'
    popup.style.top = (YPos(button) + 22) + 'px'
    popupbg2.style.left = (XPos(button) + 1) + 'px'
    popupbg2.style.top = (YPos(button) + 20) + 'px'
  }
  
  popup.style.display = 'block'
  popupbg.style.display = 'block'
  popupbg2.style.display = 'block'
}

function HidePopup() {
  document.getElementById('commentPopupDiv').style.display = 'none'
  document.getElementById('commentPopupDivBackground').style.display = 'none'
  document.getElementById('commentPopupDivBackground2').style.display = 'none'
}
