function setImage(imageName, maxDim) {
  document['thumb_filename'].src = image_lib_domain + imageName;
  document.cms_form.image_filename.value = replace(imageName, 'thumbs/', '');
  document.cms_form.image_max_dim.value = maxDim;
  document.getElementById("imgSpan").style.display = "block";
  document.getElementById("addImgSpan").style.display = "none";
}
function removeImage() {
  document['thumb_filename'].src = '';
  document.cms_form.image_filename.value = '';
  document.cms_form.image_max_dim.value = '0';
  document.cms_form.image_caption.value = '';
  document.getElementById("imgSpan").style.display = "none";
  document.getElementById("addImgSpan").style.display = "inline";
}
function storeCaret (textEl) {
  if (textEl.createTextRange)
    textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
  if (textEl.createTextRange && textEl.caretPos) {
    var caretPos = textEl.caretPos;
    caretPos.text =
      caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
        text + ' ' : text;
  }
  else
    textEl.value = textEl.value + ' ' + text;
}
function surroundAtCaret (textEl, tag1, tag2) {
  if (textEl.createTextRange && textEl.caretPos) {
    var caretPos = textEl.caretPos;
    caretPos.text = tag1 + caretPos.text + tag2;
  }
  else
    textEl.value = textEl.value + ' ' + tag1 + tag2;
}
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
function bulletsAtCaret (textEl) {
  if (textEl.createTextRange && textEl.caretPos) {
    var caretPos = textEl.caretPos;
    caretPos.text = '<ul>\n<li>' + replace(caretPos.text, '\r\n', '</li>\r\n<li>') + '</li>\r\n</ul>';
  }
  else
    textEl.value = textEl.value + ' ' + '<ul>\n<li></li>\n</ul>';;
}

