/////////////////////////////////////////////////
// UNIVERSAL BULLETIN BOARD CODE INTERFACE v2.1
// By Mark Plachetta (astroboy@zip.com.au)
/////////////////////////////////////////////////

/////////////////////////////
// Basic browser detection
//
  var ie = (document.all) ? 1 : 0;
  var nn = (document.layers) ? 1 : 0;
  var n6 = (window.sidebar) ? 1 : 0;


/////////////////////////////
// Stylesheet output
//
  function writeStyle() {
    var html = '<style type="text/css"><!--\n';
       html += '.ibcButton { font-size:11px; font-family:Verdana, Arial, Helvetica, sans-serif; background-color:#cccccc;  }\n';
       html += '.ubbcButton { font-size:11px; font-family:Verdana, Arial, Helvetica, sans-serif; background-color:#ececec;}\n'; 
       html += '.ibcSelect{ font-size:11px; font-family:Verdana, Arial, Helvetica, sans-serif; background-color:#cccccc;  }\n';
       html += '.ibcButton { font-size:11px; font-family:Verdana, Arial, Helvetica, sans-serif; background-color:#ececec; }\n';
       html += ' --></style>';

    document.write(html);
  }


/////////////////////////////
// Interface output
//
  function makeInterface(images,flash,graphical) {
    var html = '';
    
    document.write(html);
  }


/////////////////////////////
// Code inserter
//
  function ubbCode(code) {
    if (form["Post"].createTextRange && form["Post"].caretPos) {
      var caretPos = form["Post"].caretPos;
      caretPos.text = code;
    } else { form["Post"].value += code; }
    form["Post"].focus();
  }


/////////////////////////////
// HTML shortcuts
//
  function makeButton(onclick,value,title,accesskey) {
    var html = '<input type="button" onclick="' + onclick;
       html += 'return false;" title="' + title;
       html += '" accesskey="' + accesskey + '" class="ubbcButton';
       html += '" value="' + value + '">';
    return html;
  }

  function makeLink(onclick,text) {
    var html = '<a href="#" onclick="' + onclick;
       html += 'return false;" onmouseover="return winStat(\'' + text;
       html += '\');" onmouseout="return winStat(\'\');">';
    return html;
  }

  function makeImage(source,width,height,alt) {
    var html = '<img src="' + ubbc_dir + source + '" width="' + width;
       html += '" height="' + height + '" border="0" alt="' + alt;
       html += '" align="absmiddle">';
    return html;
  }

  function makeOption(value,text,style) {
    var html = '<option value="' + value;
       html += ((style && ie) ? '" style="color:' + value : '');
       html += '">' + text + '</option>';
    return html;
  }


/////////////////////////////
// Misc utils
//
  function storeCaret(el) { 
    if (el.createTextRange) {
      el.caretPos = document.selection.createRange().duplicate();
    }
  }

  function getText() {
    if (ie) {
      return ((form["Post"].createTextRange && form["Post"].caretPos) ? form["Post"].caretPos.text : '');
    } else { return ''; }
  }

  function isUrl(text) {
    return ((text.indexOf('.') > 7) &&
            ((text.substring(0,7) == 'http://') ||
            (text.substring(0,6) == 'ftp://')));
  }

  function isEmail(str) {
    if (!reSupport) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); }
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(str) && r2.test(str));
  }

  function winStat(txt) {
    window.status = txt;
    return true;
  }

  function returnFocus() {
    setTimeout('form["Post"].focus()',10);
  }

  function resetList(list) {
    setTimeout('form["'+list+'"].options[0].selected = true',10);
  }

  function ubbHelp() {
    var url = ubbc_dir + '/help/index.htm';
    var options = 'height=350,width=300,scrollbars=yes';
    window.open(url,'ubbc_help',options);
  }

function ubbSmile() {
    var url = 'http://mysmilies.com';
    var options = 'scrollbars=yes';
    window.open(url);
  }

  function removeElement(array,value) {
    array = array.split(',');
    for (i = 0; i < array.length; i++) {
      if (array[i] == value) { var pos = i; break; }
    }
    for (i = pos; i < (array.length-1); i++) {
      array[i] = array[i + 1];
    }
    array.length = array.length - 1;
    return array.join(',');
  }


/////////////////////////////
// Indivdual code types
//
  var openTags = new Array('');
  var closedTags = new Array('dummy','b','i','u','s','code','quote','me','list');
  function ubbBasic(code) {
    var text = getText();
    if (text) {
      code = '[' + code + ']' + text + '[/' + code + ']';
      ubbCode(code);
    } else {
      if (openTags.join(',').indexOf(','+code) != -1) {
        var tag = '[/' + code + ']';
        openTags = removeElement(openTags.join(','),code).split(',');
        closedTags[closedTags.length] = code;
      } else {
        var tag = '[' + code + ']';
        closedTags = removeElement(closedTags.join(','),code).split(',');
        openTags[openTags.length] = code;
      } ubbCode(tag);
    }
  }

  function ubbFont(list) {
    var attrib = list.name.substring(1,list.name.length);
    var value = list.options[list.selectedIndex].value;
    if (value && attrib) {
      var code = '[' + attrib + '=' + value + ']' + getText() + '[/' + attrib + ']';
      ubbCode(code);
    }
    resetList(list.name);
  }

 function ubbAlign(align) {
    if (!align) { return; }
    code = '[align=' + align + ']' + getText() + '[/align]';
    ubbCode(code);
    resetList("talign");
  }


  function ubbList(size) {
    var text = getText();
    if (!size && !text) { ubbBasic('list'); }
    else if (!size && text && reSupport) {
      var regExp = /\n/g;
      text = text.replace(regExp,'\n[*]');
      var code = '[list]\n[*]' + text + '\n[/list]\n';
      ubbCode(code);
    } else {
      if (text) { text += '\n'; }
      var code = text + '[list]\n';
      for (i = 0; i < size; i++) { code += '[*]\n'; }
      code += '[/list]\n';
      ubbCode(code);
      resetList("quicklist");
    }
  }

  function ubbListItem() {
    var code = '[*]' + getText();
    ubbCode(code);
  }

  function ubbHref() {
    var url = 'http://'; var desc = '';
    var text = getText();
    if (text) {
      if (isUrl(text)) { url = text; }
      else { desc = text; }
    }
    url = prompt('Ââåäèòå ññûëêó:',url) || '';
    desc = prompt('Îïèñàíèå ññûëêè:',desc) || url;
    if (!isUrl(url)) { returnFocus(); return; }
    var code = '[url=' + url + ']' + desc + '[/url]';
    ubbCode(code);
  }

  function ubbEmail() {
    var email = ''; var desc = '';
    var text = getText();
    if (text) {
      if (isEmail(text)) { email = text; }
      else { desc = text; }
    }
    email = prompt('Ââåäèòå E-mail àäðåñ:',email) || '';
    desc = prompt('Ââåäèòå îïèñàíèå:',desc) || email;
    if (!isEmail(email)) { returnFocus(); return; }
    var code = '[email=' + email + ']' + desc + '[/email]';
    ubbCode(code);
  }

  function ubbImage() {
    var text = getText();
    var url = (text && isUrl) ? text : prompt("\nÂâåäèòå URL êàðòèíêè:","http://") || "";
    if (!url) { return; }
    var code = "[IMG]" + url + "[/IMG]";
    ubbCode(code);
  }



  function ubbFlash() {
    var url = 'http://'; var h = ''; var w = '';
    var text = getText();
    if (text && isUrl(text)) { url = text; text = ''; }
    url = prompt('Ââåäèòå URL Flash îáúåêòà:',url) || '';
    w = prompt('Ââåäèòå øèðèíó Flash:\nMax = '+flash_w, w) || '';
    h = prompt('Ââåäèòå âûñîòó Flash:\nMax = '+flash_h, h) || '';
    if (isNaN(w) || (w > flash_w)) { w = flash_w; }
    if (isNaN(h) || (h > flash_h)) { h = flash_h; }
    if (!isUrl(url)) { returnFocus(); return; }
    var code = ((text) ? text + ' ' : '') + '[flash=' + w + ',' + h + ']' + url + '[/flash]';
    ubbCode(code);
  }


function ubbGlow() {
    var color = ''; var write = '';
    var text = getText();
    if (text && isUrl(text)) { url = text; text = ''; }
    color = prompt('Ââåäèòå öâåò ñâå÷åíèÿ:',color) || '';
    write = prompt('Ââåäèòå òåêñò:',write) || '';
    var code = ((text) ? text + ' ' : '') + '[glow=' + color + ']' + write + '[/glow]';
    ubbCode(code);
  }

  function ubbShadow() {
    var color = ''; var write = '';
    var text = getText();
    if (text && isUrl(text)) { url = text; text = ''; }
    color = prompt('Ââåäèòå öâåò òåíè:',color) || '';
    write = prompt('Ââåäèòå òåêñò:',write) || '';
    var code = ((text) ? text + ' ' : '') + '[shadow=' + color + ']' + write + '[/shadow]';
    ubbCode(code);
  }

  function ubbSpoil() {
    var write = '';
    var text = getText();
    if (text && isUrl(text)) { url = text; text = ''; }
    write = prompt('Ââåäèòå ñêðûâàåìûé òåêñò:',write) || '';
    var code = ((text) ? text + ' ' : '') + '[spoiler]' + write + '[/spoiler]';
    ubbCode(code);
  }

  function ubbsound() {
    var text = getText();
    var url = (text && isUrl) ? text : prompt("\nÂâåäèòå URL çâóêà:","http://") || "";
    if (!url) { return; }
    var code = '[sound]' + url + '[/sound]';
    ubbCode(code);
  }

  function ubbvideo() {
    var text = getText();
    var url = (text && isUrl) ? text : prompt("\nÂâåäèòå URL âèäåî:","http://") || "";
    if (!url) { return; }
    var code = '[video]' + url + '[/video]';
    ubbCode(code);
  }

  function ubbweb() {
    var text = getText();
    var url = (text && isUrl) ? text : prompt("\nÂâåäèòå ññûëêó êîïèðóåìîãî ñàéòà:","http://") || "";
    if (!url) { return; }
    var code = '[web]' + url + '[/web]';
    ubbCode(code);
  }

/////////////////////////////
// Access Keys
//
  var keys = new Array('b','i','u','s','g','q','m','h','e','l','k');
  function checkKey() {
    if (event.ctrlKey) {
      for (i = 0; i < keys.length; i++) {
        if (String.fromCharCode(event.keyCode) == keys[i].toUpperCase()) {
          var found = keys[i]; break;
        }
      }
      if (found) {
        switch(found) {
          case 'h':ubbHref();break;
          case 'e':ubbEmail();break;
          case 'p':ubbImage();break;
          case 'f':ubbFlash();break;
          case 'l':ubbList();break;
          case 'k':ubbListItem();break;
          case 'g':ubbBasic('code');break;
          case 'q':ubbBasic('quote');break;
          case 'm':ubbBasic('me');break;
          default:ubbBasic(found);
        }
        this.cancelBubble = true;
        this.returnValue = false;
        return false;
      }
    }
  }


/////////////////////////////
// Replacing default iB JS
//
  function emoticon(theSmilie) {
    var text = getText() + ' ';
    var code = text + theSmilie + ' ';
    ubbCode(code);
  }


/////////////////////////////
// Initilization
//
  var form;
  writeStyle();
  var reSupport = 0;
  function ubbcInit(images,flash,graphical) {
    form = document.forms["REPLIER"];

    if (images) { keys[keys.length] = 'p'; }
    if (flash) { keys[keys.length] = 'f'; }
    if (graphical) { document.onkeydown = checkKey; }

    if (window.RegExp) {
      var tempStr = "a";
      var tempReg = new RegExp(tempStr);
      if (tempReg.test(tempStr)) { reSupport = 1; }
    }
  }

