var image_lib_domain = '';

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>';;
}

// script for use with nested_checkboxes()

_parent = new Array();
_parent[0] = new Array();
_parent[1] = new Array();

function siblingsChecked(table, checkbox_id) {
  result = false;
  if (_parent[table_id][checkbox_id]) {
    for (key in _parent[table_id]) {
      if (_parent[table_id][key] == _parent[table_id][checkbox_id]) {
        result = result || document.getElementById(table + '_' + key).checked;
      }
    }
  }
  return result;
}

function checkUp(table, checkbox_id, checked) {
  table_id = (table == "region" ? 0 : 1);
  if (_parent[table_id][checkbox_id]) {
    document.getElementById(table + '_' + _parent[table_id][checkbox_id]).checked = checked ||
      siblingsChecked(table, checkbox_id);
    checkUp(table, _parent[table_id][checkbox_id], checked);
  }
}

function checkDown(table, checkbox_id, checked) {
  table_id = (table == "region" ? 0 : 1);
  for (key in _parent[table_id]) {
    if (_parent[table_id][key] == checkbox_id) {
      document.getElementById(table + '_' + key).checked = checked;
      checkDown(table, key, checked);
    }
  }
}

function checkbranch(table, checkbox_id, checked) {
  checkUp(table, checkbox_id, checked);
  checkDown(table, checkbox_id, checked);
}



