// JavaScript ubb

var prompttext = new Array();
prompttext['b'] = 'Voeg tekst in om dikgedrukt te maken';
prompttext['i'] = 'Voeg tekst in om schuin te maken';
prompttext['s'] = 'Voeg tekst in om doorstreept te maken';
prompttext['u'] = 'Voeg tekst in om te onderstrepen';
prompttext['quote'] = 'Voeg tekst in om te quoten';
prompttext['url'] = 'Url invoeren';
prompttext['urld'] = 'Geef een omschrijving van de URL';
prompttext['mail'] = 'Typ een e-mail address';
prompttext['video'] = 'Video url (Youtube, Gametrailers, Google video)';


var target = null;
function getTarget(el) {

  target = (document.getElementById)? document.getElementById(el):0;

}

function storeCursor(el) {

  if (document.all && el.createTextRange) el.cursorPos = document.selection.createRange().duplicate();

}

function putStr(text) {

  if (target) {

    if (document.all && target.cursorPos) {

      target.cursorPos.text = text;

    } else if (typeof(target.selectionStart) != 'undefined') {

      var sStart = target.selectionStart;
      var sEnd = target.selectionEnd;
      target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd, target.value.length);
      target.selectionStart = (sStart == sEnd)? sStart + text.length:sStart;
      target.selectionEnd = sStart + text.length;

    } else {

      target.value += text;

    }

    target.focus();
    storeCursor(target);

  }
}

function applyUBB(style) {

  if (target) {

    var selectedtext = '';
    if (document.all && target.cursorPos) {
      selectedtext = target.cursorPos.text;
    } else if (typeof(target.selectionStart) != 'undefined') {
      selectedtext = target.value.substr(target.selectionStart, target.selectionEnd - target.selectionStart);
    }

    if (!selectedtext) selectedtext = prompt(prompttext[style], '');
    if (!selectedtext) { target.focus(); return; }

    if (style == 'url=') {
      var description = prompt(prompttext['urld'], '');
      if (!description) { target.focus(); return; }
      selectedtext = '[url='+selectedtext+']'+description+'[/url]';
    } else {
      selectedtext = '['+style+']'+selectedtext+'[/'+style+']';
    }

    putStr(selectedtext);

  } 
}
