var UAgent   = navigator.userAgent.toLowerCase();
var IsSafari = ((UAgent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var IsIE     = ((UAgent.indexOf('msie') != -1) && (!IsOpera) && (!IsSafari) && (!IsWebTV));
var isIE4    = ((IsIE) && (UAgent.indexOf("msie 4.") != -1));
var isMoz    = (navigator.product == 'Gecko');
var IsNS     = ((UAgent.indexOf('compatible') == -1) && (UAgent.indexOf('mozilla') != -1) && (!IsOpera) && (!IsWebTV) && (!IsSafari));
var IsNS4    = ((IsNS) && (parseInt(navigator.appVersion) == 4));
var IsOpera  = (UAgent.indexOf('opera') != -1);
var IsKon    = (UAgent.indexOf('konqueror') != -1);
var IsWebTV  = (UAgent.indexOf('webtv') != -1);
var IsWin    = ((UAgent.indexOf("win") != -1) || (UAgent.indexOf("16bit") !=- 1));
var IsMac    = ((UAgent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc."));
var UAVers   = parseInt(navigator.appVersion);

var BBCode   = new Array();

function SetCookie(sName, sValue) {
  document.cookie = sName + "=" + escape(sValue) + "; expires=Mon, 31 Dec 2019 23:59:59 UTC; path=/";
}

function GetCookie(sName) {
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++) {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) {
      return unescape(aCrumb[1]);
    }
  }
  return '';
}

function smile(obText, theSmilie) {
  bbcodeDoInsert(obText, " " + theSmilie + " ", "", "", "", false, true);
}

function arraySize(arr) {
  for (i = 0; i < arr.length; i++) {
    if ((arr[i] == "undefined") || (arr[i] == "") || (arr[i] == null))
    return i;
  }
  return arr.length;
}

function arrayPush(arr, value) {
  arr[arraySize(arr)] = value;
}

function bbcodeURL(obText) {
  if (typeof(obText) == 'undefined') {
    return;
  }

  var strTitle = "Title";
  var strURL = "http://";

  if ((UAVers >= 4) && IsIE && IsWin) {
    var sel = document.selection;
    var rng = sel.createRange();
    rng.colapse;
    if (rng != null) {
      if (sel.type == "Text") {
        strTitle = rng.text;
      }
    }
  }
  else if (obText.selectionEnd) {
    var ss = obText.selectionStart;
    var st = obText.scrollTop;
    var es = obText.selectionEnd;

    if (es <= 2) {
      es = obText.textLength;
    }

    if (es > ss) {
      strTitle = (obText.value).substring(ss, es);
    }
  }

  var s = prompt("URL:\n\n", strURL);
  if (s) {
    strURL = s;
  }
  else {
    return;
  }
  var s = prompt("URL Name:\n\n", strTitle);
  if (s) {
    strTitle = s;
  }
  else {
    return;
  }
  bbcodeDoInsert(obText, '[URL=]', '[/URL]', strURL, strTitle, true);
  obText.focus();
}

function bbcodeColorWindow(obText) {
  if (typeof(obText) == 'undefined') {
    return;
  }

  if (bbcodeClose(obText, '[COLOR=]')) {
    obText.focus();
    return;
  }

  var left = (screen.availWidth-352) / 2
  var top = (screen.availHeight-372) / 2

  var colorWin = window.open('/getcolor.html', obText.id, 'top=' + top +',left=' + left + ',height=372,width=352,status=no,toolbar=no,menubar=no,location=no');
  colorWin.focus();
}

function bbcodeColor(obText, color) {
  if (typeof(obText) == 'undefined') {
    return;
  }
  bbcodeDoInsert(obText, '[COLOR=]', '[/COLOR]', color);
  window.focus();
  obText.focus();
}


function bbcodeFontSizeWindow(obText) {
  if (typeof(obText) == 'undefined') {
    return;
  }

  if (bbcodeClose(obText, '[SIZE=]')) {
    obText.focus();
    return;
  }

  var left = (screen.availWidth-220) / 2
  var top = (screen.availHeight-160) / 2

  var fontSizeWin = window.open('/getfontsize.html', obText.id, 'top=' + top +',left=' + left + ',height=160,width=220,status=0,toolbar=no,menubar=no,location=no');
  fontSizeWin.focus();
}

function bbcodeFontSize(obText, fontSize) {
  if (typeof(obText) == 'undefined') {
    return;
  }
  bbcodeDoInsert(obText, '[SIZE=]', '[/SIZE]', fontSize);
  window.focus();
  obText.focus();
}

function bbcodeFontNameWindow(obText) {
  if (typeof(obText) == 'undefined') {
    return;
  }

  if (bbcodeClose(obText, '[FONT=]')) {
    obText.focus();
    return;
  }

  var left = (screen.availWidth-220) / 2
  var top = (screen.availHeight-110) / 2

  var fontNameWin = window.open('/getfont.html', obText.id, 'top=' + top +',left=' + left + ',height=110,width=220,status=no,toolbar=no,menubar=no,location=no');
  fontNameWin.focus();
}

function bbcodeFontName(obText, fontName) {
  if (typeof(obText) == 'undefined') {
    return;
  }
  bbcodeDoInsert(obText, '[FONT=]', '[/FONT]', fontName);
  window.focus();
  obText.focus();
}

function bbcodeSmileWindow(obText) {
  if (typeof(obText) == 'undefined') {
    return;
  }
  var left = (screen.availWidth-320) / 2
  var top = (screen.availHeight-600) / 2

  var fontNameWin = window.open('/getsmile.html', obText.id, 'top=' + top +',left=' + left + ',height=600,width=320,status=no,toolbar=no,menubar=no,location=no');
  fontNameWin.focus();
}


function bbcodeClose(obText, strTag) {
  if (typeof(obText) == 'undefined') {
    return;
  }

  var isClose = false;
  // IE
  if ((UAVers >= 4) && IsIE && IsWin) {
    if (obText.isTextEdit) {
      for (i=BBCode.length - 1;i>=0;i--) {
        if ((BBCode[i][0] == strTag) && (!BBCode[i][2])) {
          BBCode[i][2] = true;
          obText.value += BBCode[i][1];
          isClose = true;
        }
      }
    }
  }
  // MOZZY
  else {
    for (i=BBCode.length - 1;i>=0;i--) {
      if ((BBCode[i][0] == strTag) && (!BBCode[i][2])) {
        BBCode[i][2] = true;
        obText.value += BBCode[i][1];
        isClose = true;
      }
    }
  }
  return isClose;
}

function bbcodeCloseAll(obText) {
  if (typeof(obText) == 'undefined') {
    return;
  }

  // IE
  if ((UAVers >= 4) && IsIE && IsWin) {
    if (obText.isTextEdit) {
      for (i=BBCode.length - 1;i>=0;i--) {
        if (!BBCode[i][2]) {
          BBCode[i][2] = true;
          obText.value += BBCode[i][1];
        }
      }
    }
  }
  // MOZZY
  else {
    for (i=BBCode.length - 1;i>=0;i--) {
      if (!BBCode[i][2]) {
        BBCode[i][2] = true;
        obText.value += BBCode[i][1];
      }
    }
  }
  obText.focus();
}

function bbcodeDoInsert(obText, strTag,  strCloseTag, strParam, strText, isClose, isSingle) {
  if (typeof(obText) == 'undefined') {
    return;
  }
  var IsClose = true;

  var IsInsert = false;
  var TheSelection = false;
  var ItemPosition = 0;

  // IE
  if ((UAVers >= 4) && IsIE && IsWin) {
    if (obText.isTextEdit) {
      obText.focus();
      var sel = document.selection;
      var rng = sel.createRange();
      rng.colapse;

      if (rng != null) {
        if (sel.type == "Text") {
          arrayPush(BBCode, new Array(strTag, strCloseTag, true));
          if (strParam) {
            strTag = strTag.replace(']', strParam + ']');
          }
          if (strText) {
            rng.text = strTag + strText + strCloseTag;
          }
          else {
            rng.text = strTag + rng.text + strCloseTag;
          }
          return;
        }
        else {
          for (i=0; i<BBCode.length;i++) {
            if ((BBCode[i][0] == strTag) && (!BBCode[i][2])) {
              ItemPosition = i;
              IsInsert = true;
            }
          }

          if (IsInsert) {
            for (i=BBCode.length - 1;i>ItemPosition;i--) {
              if (!BBCode[i][2]) {
                BBCode[i][2] = true;
                rng.text = rng.text + BBCode[i][1];
              }
            }
            BBCode[ItemPosition][2] = true;
            rng.text = strCloseTag + rng.text;
          }
          else {
            if (strCloseTag ==  '') {
              arrayPush(BBCode, new Array(strTag, strCloseTag, true));
            }
            else {
              arrayPush(BBCode, new Array(strTag, strCloseTag, false));
            }
            if (strParam) {
              strTag = strTag.replace(']', strParam + ']');
            }

            if (!rng.text) {
              rng.text = strTag;
            }
            else {
              rng.text = strTag + rng.text;
            }
            if (strText) {
              rng.text += strText;
            }

            if (isClose) {
              BBCode[BBCode.length-1][2] = true;
              rng.text += strCloseTag;
            }
          }
        }
      }
    }
  }
  // MOZZY
  else if (obText.selectionEnd) {
    var ss = obText.selectionStart;
    var st = obText.scrollTop;
    var es = obText.selectionEnd;

    if (es <= 2) {
      es = obText.textLength;
    }

    var start  = (obText.value).substring(0, ss);
    var middle = (obText.value).substring(ss, es);
    var end    = (obText.value).substring(es, obText.textLength);

    //-----------------------------------
    // text range?
    //-----------------------------------

    if (obText.selectionEnd - obText.selectionStart > 0) {
      arrayPush(BBCode, new Array(strTag, strCloseTag, true));
      if (strParam) {
        strTag = strTag.replace(']', strParam + ']');
      }

      if (strText) {
        middle += strText;
      }
      middle = strTag + middle + strCloseTag;
    }
    else {
      for (i=0; i<BBCode.length;i++) {
        if ((BBCode[i][0] == strTag) && (!BBCode[i][2])) {
          ItemPosition = i;
          IsInsert = true;
        }
      }

      if (IsInsert) {
        for (i=BBCode.length - 1;i>ItemPosition;i--) {
          if (!BBCode[i][2]) {
            BBCode[i][2] = true;
            middle += BBCode[i][1];
          }
        }
        BBCode[ItemPosition][2] = true;
        middle += strCloseTag;
      }
      else {
        if (strCloseTag ==  '') {
          arrayPush(BBCode, new Array(strTag, strCloseTag, true));
        }
        else {
          arrayPush(BBCode, new Array(strTag, strCloseTag, false));
        }
        if (strParam) {
          strTag = strTag.replace(']', strParam + ']');
        }

        middle += strTag;

        if (strText) {
          middle += strText;
        }

        if (isClose) {
          BBCode[BBCode.length-1][2] = true;
          middle += strCloseTag;
        }
      }
    }
    obText.value = start + middle + end;

    var cpos = ss + (middle.length);

    obText.selectionStart = cpos;
    obText.selectionEnd   = cpos;
    obText.scrollTop      = st;
  }
  else {
    for (i=0; i<BBCode.length;i++) {
      if ((BBCode[i][0] == strTag) && (!BBCode[i][2])) {
        ItemPosition = i;
        IsInsert = true;
      }
    }

    if (IsInsert) {
      for (i=BBCode.length - 1;i>ItemPosition;i--) {
        if (!BBCode[i][2]) {
          BBCode[i][2] = true;
          obText.value += BBCode[i][1];
        }
      }
      alert(strCloseTag);
      BBCode[ItemPosition][2] = true;
      obText.value += strCloseTag;
    }
    else {
      if (strCloseTag ==  '') {
        arrayPush(BBCode, new Array(strTag, strCloseTag, true));
      }
      else {
        arrayPush(BBCode, new Array(strTag, strCloseTag, false));
      }
      if (strParam) {
        strTag = strTag.replace(']', strParam + ']');
      }

      obText.value += strTag;

      if (strText) {
        obText.value += strText;
      }

      if (isClose) {
        BBCode[BBCode.length-1][2] = true;
        obText.value += strCloseTag;
      }
    }
  }
  obText.focus();
}