function showSelectGallery() {
 pop_out('nl-select_gallery.html', 500, 500);
}
function showSelectPicture() {
 pop_out('nl-select_picture.html', 500, 900);
}
function showSelectFile() {
 pop_out('nl-select_files.html', 500, 900);
}

function getCaretPosition (el) {
 var CaretPos = 0
 
 if (el.type != 'text' && el.type != 'textarea') {
  return;
 }
 
 el.focus();
 if (el.selectionStart) {
  CaretPos = el.selectionStart;
 } else if (document.selection) { // IE
  var selectionRange = document.selection.createRange();
  selectionRange.moveStart ('word', -el.value.length);
  CaretPos = selectionRange.text.length - 100; /* The number representes some startvalue of some kind, found by trial-and-error */
 } else {
  CaretPos = el.value.length;
 }
 return CaretPos;
}
function setCaretPosition(el, pos) {
 //http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
 if(el.setSelectionRange) {
  el.focus();
  el.setSelectionRange(pos,pos);
 } else if (el.createTextRange) {
  var range = el.createTextRange();
  range.collapse(true);
  range.moveEnd('character', pos);
  range.moveStart('character', pos);
  range.select();
 }
 el.focus();
}
function fillIn(string) {
 var caretPos = getCaretPosition(document.getElementById('body'));
 var text = document.getElementById('body').value;
 var start = text.substring(0, caretPos);
 var end = text.substring(caretPos);
 
 document.getElementById('body').value = start + string + end;
 setCaretPosition(document.getElementById('body'), (start.length + string.length));
}
function pop_out(url, height, width) {
 if (typeof height == "undefined") {
  height = 300;
 }
 if (typeof width == "undefined") {
  width = 400;
 }

 var left = (screen.width - width)/2;
 var top = (screen.height - height)/2;
 popper = window.open(url,'popper','resizable=yes, scrollbars=yes, height='+height+', width='+width+', left='+left+', top='+top);
 if (window.focus) {popper.focus()}
 return true;
}
function ac_niceDisable(element) {
 element.disabled = true;
 element.style.backgroundColor = '#AAAAAA';
}
function ac_niceEnable(element) {
 element.disabled = false;
 element.style.backgroundColor = '#EAEAEA';
}
function selectedPageType() {
 if (document.getElementById('type')) {
  if (document.getElementById('type').value == 'static') {
   ac_niceEnable(document.getElementById('title'));
   ac_niceEnable(document.getElementById('htmltitle'));
   ac_niceEnable(document.getElementById('position'));
   ac_niceEnable(document.getElementById('uri'));
   ac_niceEnable(document.getElementById('childOf'));
   ac_niceEnable(document.getElementById('type'));
   ac_niceDisable(document.getElementById('linkedTo'));
   ac_niceEnable(document.getElementById('visibility'));
  } else if (document.getElementById('type').value == 'devider') {
   ac_niceEnable(document.getElementById('title'));
   ac_niceDisable(document.getElementById('htmltitle'));
   ac_niceEnable(document.getElementById('position'));
   ac_niceDisable(document.getElementById('uri'));
   ac_niceEnable(document.getElementById('childOf'));
   ac_niceEnable(document.getElementById('type'));
   ac_niceDisable(document.getElementById('linkedTo'));
   ac_niceEnable(document.getElementById('visibility'));
  } else if (document.getElementById('type').value == 'linked') {
   ac_niceEnable(document.getElementById('title'));
   ac_niceEnable(document.getElementById('htmltitle'));
   ac_niceEnable(document.getElementById('position'));
   ac_niceDisable(document.getElementById('uri'));
   ac_niceEnable(document.getElementById('childOf'));
   ac_niceEnable(document.getElementById('type'));
   ac_niceEnable(document.getElementById('linkedTo'));
   ac_niceEnable(document.getElementById('visibility'));
  } else if (document.getElementById('type').value == 'news') {
   ac_niceEnable(document.getElementById('title'));
   ac_niceEnable(document.getElementById('htmltitle'));
   ac_niceEnable(document.getElementById('position'));
   ac_niceEnable(document.getElementById('uri'));
   ac_niceEnable(document.getElementById('childOf'));
   ac_niceEnable(document.getElementById('type'));
   ac_niceDisable(document.getElementById('linkedTo'));
   ac_niceEnable(document.getElementById('visibility'));
  }
 }
}
function changedAutoSelectURI(element, setFocusOn) {
 if (element.checked == false) {
  document.getElementById(setFocusOn).disabled = false;
  document.getElementById(setFocusOn).focus();
  document.getElementById(setFocusOn).style.backgroundColor = '#EAEAEA';
 } else {
  document.getElementById(setFocusOn).disabled = true;
  document.getElementById(setFocusOn).style.backgroundColor = '#AAA';
 }
}
function focusUsername() {
 if (document.getElementById('naam')) {
  if (document.activeElement.id != 'wachtwoord') {
   document.getElementById('naam').focus();
  }
 }
}
function focusSearchfield(){
 if (document.getElementById('q')) {
  document.getElementById('q').focus();
 }
}
function focusTitlefield(){
 if (document.getElementById('title')) {
  document.getElementById('title').focus();
 }
}
