/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * It's difficult (impossible?) to style input buttons
 * using e.g. sliding door principle. Therefore, we use
 * anchors to submit forms. In order to submit forms, javascript
 * is needed. This function submits the from where some anchor is child of.
 * By using this method, an id like id="form1" is not needed.
 * 
 * Example use:
 * <a class="button" href="#" onclick="submitForm(this);return false;"><span>Verstuur</span></a>
 * 
 * To prevent name collisions, the prefix dp_ is added
 * 
 * */

function dp_submit_form(elem){
  while (elem.parentNode && elem.parentNode.tagName != "FORM"){
    elem = elem.parentNode;
  }
  var oForm = elem.parentNode;
  oForm.submit();
}


function ubbInsertTag(sElement, sTag, eTag) {

    var input = document.getElementById(sElement);
  input.focus();
  /*  Internet Explorer */
  if(typeof document.selection != 'undefined') {
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = sTag + insText + eTag;
    /* Edit cursor position */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', sTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* Gecko based Browser ex. mozilla/firefox */
  else if(typeof input.selectionStart != 'undefined')
  {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + sTag + insText + eTag + input.value.substr(end);
    var pos;
    if (insText.length == 0) {
      pos = start + sTag.length;
    } else {
      pos = start + sTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* Other browsers */
  else
  {
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Invoegen op positie (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Type in te voegen tekst:");
    input.value = input.value.substr(0, pos) + sTag + insText + eTag + input.value.substr(pos);
  }
}
