   var BBEditors = new Array();
   var BBEditorClicked = false;
   var BBClipboard = '';

   function BBFindPosX(AObject)
   {
     var result = 0;
     if (AObject.offsetParent)
     {
       while (AObject.offsetParent)
       {
         if (AObject.currentStyle)
         lPosition = AObject.currentStyle["position"];
         else if (window.getComputedStyle){
         var lStyle = window.getComputedStyle(AObject, "");
         lPosition = lStyle.getPropertyValue("position");
         } else lPosition = "";
  
         result += AObject.offsetLeft;
         AObject = AObject.offsetParent;
       }
     }
     else if (AObject.x)
       result += AObject.x;
     return result;
   }

   function BBFindPosY(AObject)
   {
     var result = 0;
     if (AObject.offsetParent)
     {
       while (AObject.offsetParent)
       {
         if (AObject.currentStyle)
         lPosition = AObject.currentStyle["position"];
         else if (window.getComputedStyle){
         var lStyle = window.getComputedStyle(AObject, "");
         lPosition = lStyle.getPropertyValue("position");
         } else lPosition = "";
  
         result += AObject.offsetTop;
         AObject = AObject.offsetParent;
       }
     }
     else if (AObject.y)
       result += AObject.y;
     return result;
   }

  function BBEditorAddTag(AName, ABeginTag, AEndTag)
  {
    var lElem = document.getElementById(AName);

    if (document.selection)
    { //This is IE
      lText = document.selection.createRange().text;
      lElem.focus();
      document.selection.createRange().text = ABeginTag + lText + AEndTag;
    }
    else if ((lElem.selectionStart) || (lElem.selectionStart == '0'))
    { //This is Mozilla/Gecko
      lStart = lElem.selectionStart;
      lStop = lElem.selectionEnd;
      lText = lElem.value.substring(lStart, lStop);

      lSubst = ABeginTag + lText + AEndTag;
      lElem.value = lElem.value.substring(0, lStart) + lSubst + lElem.value.substring(lStop, lElem.value.length);
      lElem.focus();

      lStart += ABeginTag.length;
      if (lText.length > 0)
        lStart += lText.length + AEndTag.length;
      lElem.selectionStart = lStart;
      lElem.selectionEnd = lStart;
    }
    else
    {
      lElem.value = lElem.value + ABeginTag + AEndTag;
      lElem.focus();
    }
  }

  function BBEditorShow(AButton, AName)
  {
    var lElem = document.getElementById(AName);
    if (lElem.style.visibility != "visible")
    {
      BBEditorHideAll();
      lElem.style.top = (BBFindPosY(AButton) + AButton.offsetHeight + 5) + "px";
      lElem.style.left = BBFindPosX(AButton) + "px";
      lWidth = window.innerWidth;
      if (!(lWidth > 0))
        lWidth = document.body.clientWidth;
      if ((BBFindPosX(AButton) + lElem.clientWidth + 10 > lWidth) && (lWidth > 0))
        lElem.style.left = lWidth - (lElem.clientWidth + 30) + "px";
      lElem.style.visibility = "visible";
      document.onclick = BBEditorHideAll;
      BBEditors[BBEditors.length] = AName;
      BBEditorClicked = true;
    }
    else
      lElem.style.visibility = "hidden";
  }

  function BBEditorHide(AName)
  {
    var lElem = document.getElementById(AName);
    lElem.style.visibility = "hidden";
    document.onclick = "";
  }

  function BBEditorHideAll()
  {
    if (BBEditorClicked)
      BBEditorClicked = false;
    else
    {
      for(i=0;i<BBEditors.length;i++)
        BBEditorHide(BBEditors[i]);
      BBEditors = new Array();
    }
  }

  function BBEditorPreview(AName)
  {
    var lElem = document.getElementById(AName + '_preview');
    if (lElem.style.visibility != "visible")
    {
      BBEditorHideAll();
      lArea = document.getElementById(AName);
      lElem.style.top = BBFindPosY(lArea) + "px";
      lElem.style.left = BBFindPosX(lArea) + "px";
      lElem.style.width = lArea.offsetWidth + "px";
      lElem.style.height = lArea.offsetHeight + "px";
      lElem.style.visibility = "visible";
      lFrame = document.getElementById(AName + '_previewframe');
      lText = lArea.value;
      while (lText.indexOf("\n")>-1)
        lText=lText.replace("\n","\%0A\%0D")
      while (lText.indexOf("#")>-1)
        lText=lText.replace("#","%23")
      while (lText.indexOf("&")>-1)
        lText=lText.replace("&","%26")
      lFrame.src = "index.php?kelbbcodepreview=" + lText;
      document.onclick = BBEditorHideAll;
      BBEditors[BBEditors.length] = AName + '_preview';
      BBEditorClicked = true;
    }
    else
      lElem.style.visibility = "hidden";
  }

  function BBEditorCheck(AName)
  {
    var lElem = document.getElementById(AName + '_preview');
    if (lElem.style.visibility != "visible")
    {
      BBEditorHideAll();
      lArea = document.getElementById(AName);
      lElem.style.top = BBFindPosY(lArea) + "px";
      lElem.style.left = BBFindPosX(lArea) + "px";
      lElem.style.width = lArea.offsetWidth + "px";
      lElem.style.height = lArea.offsetHeight + "px";
      lElem.style.visibility = "visible";
      lFrame = document.getElementById(AName + '_previewframe');
      lText = lArea.value;
      while (lText.indexOf("\n")>-1)
        lText=lText.replace("\n","\%0A\%0D")
      while (lText.indexOf("#")>-1)
        lText=lText.replace("#","%23")
      while (lText.indexOf("&")>-1)
        lText=lText.replace("&","%26")
      lFrame.src = "index.php?kelaspell=" + lText;
      document.onclick = BBEditorHideAll;
      BBEditors[BBEditors.length] = AName + '_preview';
      BBEditorClicked = true;
    }
    else
      lElem.style.visibility = "hidden";
  }

  function BBEditorSmileys(AButton, AName, AForm)
  {
    BBEditorShow(AButton, AName);
    lFrame = document.getElementById(AName + 'frame');
    if (lFrame.src.indexOf("bbcode_smileyslist") <= 0)
      lFrame.src = 'index.php?page=bbcode_smileyslist&form=' + AForm;
  }

  function BBEditorRepository(AButton, AName, AForm)
  {
    BBEditorShow(AButton, AName);
    lFrame = document.getElementById(AName + 'frame');
    if (lFrame.src.indexOf("repository_images") <= 0)
      lFrame.src = 'index.php?page=repository_images&form=' + AForm;
  }

  function BBEditorWiki(AButton, AName, AForm)
  {
    BBEditorShow(AButton, AName);
    lFrame = document.getElementById(AName + 'frame');
    if (lFrame.src.indexOf("wiki_browse") <= 0)
      lFrame.src = 'index.php?page=wiki_browse&form=' + AForm;
  }

  function BBEditorCopy(AName)
  {
    var lElem = document.getElementById(AName);
    if (document.selection)
    { //This is IE
      lText = document.selection.createRange().text;
      BBClipboard = lText;
    }
    else if ((lElem.selectionStart) || (lElem.selectionStart == '0'))
    { //This is Mozilla/Gecko
      lStart = lElem.selectionStart;
      lStop = lElem.selectionEnd;
      lText = lElem.value.substring(lStart, lStop);
      BBClipboard = lText;
    }
    lElem.focus();
  }

  function BBEditorPaste(AName)
  {
    BBEditorAddTag(AName, BBClipboard, '');
    lElem.focus();
  }

  function BBEditorCut(AName)
  {
    BBEditorCopy(AName);
    var lElem = document.getElementById(AName);
    if (document.selection)
      document.selection.clear();
    else if ((lElem.selectionStart) || (lElem.selectionStart == '0'))
    { //This is Mozilla/Gecko
      lStart = lElem.selectionStart;
      lStop = lElem.selectionEnd;
      lText = lElem.value.substring(0, lStart) + lElem.value.substring(lStop, lElem.value.length);
      lElem.value = lText;
    }
    lElem.focus();
  }

  function BBEditorClear(AName)
  {
    var lElem = document.getElementById(AName);
    lElem.value = '';
  }

  function BBEditorSelectAll(AName)
  {
    var lElem = document.getElementById(AName);
    lElem.focus();
    lElem.select();
  }
