// COPYRIGHT (C) 2004 SLIM WEB ARCHITECTURE, BREDA, THE NETHERLANDS
// ALL CODE IS COPYRIGHTED MATERIAL
// NOTHING MAY BE COPIED OR USED WITHOUT EXPRESS PERMISSION FROM SLIM WEB ARCHITECTURE
// WWW.SLIMWEB.NL

var eElmnt;

function bewaarKeyPos(e) {
 if (document.selection) {
  if (document.selection.createRange) {
   eElmnt.cursorPos = document.selection.createRange().duplicate();
  }
 }
 var keyCode = e? e.which : event.keyCode;
 var key = String.fromCharCode(keyCode);
 var revCode = (key).charCodeAt(0);
 if ((e || event).shiftKey && (e || event).ctrlKey) {
  translateKey(revCode);
 }
}

function bewaarPos() {
 if (document.selection) {
  if (document.selection.createRange) {
   eElmnt.cursorPos = document.selection.createRange().duplicate();
  }
 }
}

if (document.addEventListener) {
 document.addEventListener("keypress", bewaarKeyPos, true); // add listener
} else {
 document.onkeyup = bewaarKeyPos;
}

function translateKey(keycode) {
e = keycode;
switch (e) {
// I
case  73: setTag("[I]","[/I]","");break;
// B
case  66: setTag("[B]","[/B]","");break;
case  98: setTag("[B]","[/B]","");break;
// U
case  85: setTag("[U]","[/U]","");break;
// A
case  65: setTag(":)","","");break;
}
}

function setUrl() {
 url = window.prompt("Enter the URL","http://");
 if (url.length>7) {
  bareUrl = url.replace("http://","");
  naam = window.prompt("Enter a name or title for this URL", bareUrl);
  if (!naam) {
   naam = bareUrl;
  }
  setTag("[URL="+url+"]","[/URL]",naam);
 } else {
  alert("Fout");
 }
}

function setImg() {
 url = window.prompt("Enter the URL of the picture","http://");
 if (url.length>7) {
  setTag("[IMG]"+url,"[/IMG]","");
 } else {
  alert("Fout");
 }
}

function setList(listtype) {
var seltext;
 if (eElmnt) {
  if ( document.all && eElmnt.cursorPos) {
   cursorPos = eElmnt.cursorPos;
   seltext = cursorPos.text;
  }
 } else if (eElmnt.selectionStart) {
  start = eElmnt.selectionStart;
  end = eElmnt.selectionEnd;
  before = (eElmnt.value).substring(0, start);
  seltext = (eElmnt.value).substring(start, end);
  after = (eElmnt.value).substring(end, eElmnt.textLength);
 }
 if (seltext) {
  selArr = seltext.split("\n");
  for (x=0;x<selArr.length;x++) {
   selArr[x]="[*]"+selArr[x];
  }
  seltext = selArr.join("\n");
 }
 if (eElmnt) {
  if ( document.all && eElmnt.cursorPos) {
   cursorPos.text = "[LIST]\n"+seltext+"\n[/LIST]";
   eElmnt.focus();
   bewaarPos();
  }
 } else if (eElmnt.selectionStart) {
  seltext = "[LIST]\n" + seltext + "\n[/LIST]";
  eElmnt.value = before + seltext + after;
  eElmnt.focus();
  eElmnt.selectionStart = start;
  eElmnt.selectionEnd = start+seltext.length; 
  bewaarPos();
 } else {
  eElmnt.value = eElmnt.value+"[LIST]\n\n[/LIST]";
  eElmnt.focus();
  bewaarPos();
 }
}

function setTag(startTag, endTag, defStr) {
var target;
var cursorPos;
var seltext;
var sel;
var sT;
if (eElmnt) {
 if ( document.all && eElmnt.cursorPos) {
  sT = eElmnt.scrollTop;
  cursorPos = eElmnt.cursorPos;
  seltext = cursorPos.text;
  if (cursorPos.text=="" && defStr!="") {
   seltext = defStr;
  }
  cursorPos.text = seltext.charAt(seltext.length - 1) == ' ' ? (startTag + seltext.substr(0,seltext.length - 1) + endTag+" ") : (startTag + seltext + endTag);
//  cursorPos.move("textedit");
//  eElmnt.setActive();
//  eElmnt.scrollTop = sT;
eElmnt.focus();
bewaarPos();
  // eElmnt.focus();
 } else if (eElmnt.selectionStart) {
  start = eElmnt.selectionStart;
  end = eElmnt.selectionEnd;
  before = (eElmnt.value).substring(0, start);
  sel = (eElmnt.value).substring(start, end);
  if (sel =="" && defStr != "") {
   sel=defStr;
  }
  after = (eElmnt.value).substring(end, eElmnt.textLength);
  if (sel) {
   if (sel.charAt(sel.length - 1) == ' ') {
    sel = sel.substr(0,sel.length - 1);
    after=" "+after;
   }
  } else {
   sel = "";
  }
  eElmnt.value = before + startTag + sel + endTag + after;
  eElmnt.focus();
  eElmnt.selectionStart = end + (startTag.length + endTag.length);
  eElmnt.selectionEnd = eElmnt.selectionStart; 
  bewaarPos();
 } else {
  eElmnt.value = eElmnt.value+startTag+endTag;
  eElmnt.focus();
  bewaarPos();
 }
}
}

