
function switchformular() {
  if (document.getElementsByName("spielisturnier[]")[0].checked) {
    document.getElementById("form_turnier").style.display = "block";
    document.getElementById("form_spiel").style.display = "none";
  } else {
    document.getElementById("form_turnier").style.display = "none";
    document.getElementById("form_spiel").style.display = "block";
  }
}

function submitformular(btn) {
  document.getElementsByName(btn)[0].click();
}
  
function submitformular2(btn) {
  document.getElementsByName(btn)[1].click();
}

function submitformular3(btn) {
  document.getElementsByName(btn)[2].click();
}

function insertInto(textarea, text) {
  
  var input = document.getElementsByName(textarea)[0];
  input.focus();
  if (typeof document.selection != 'undefined') {
    var range = document.selection.createRange();
    range.text = text;
  } else if ((typeof input.selectionStart != 'undefined') && (typeof input.scrollTop != 'undefined')) {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var scroll = input.scrollTop;
    input.value = input.value.substr(0, start) + text + input.value.substr(end);
    var pos = start + text.length;
    input.selectionStart = pos;
    input.selectionEnd = pos;
    input.scrollTop = scroll;
  } else if (typeof input.selectionStart != 'undefined') {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    input.value = input.value.substr(0, start) + text + input.value.substr(end);
    var pos = start + text.length;
    input.selectionStart = pos;
    input.selectionEnd = pos;
  } else {
    input.value = input.value + text;
  }
}

function markSelection(textarea, left, text, right) {
  
  var input = document.getElementsByName(textarea)[0];
  input.focus();
  if (typeof document.selection != 'undefined') {
    var range = document.selection.createRange();
    if (range.text.length > 0) text = range.text;
    text = left + text + right;
    range.text = text;
    range = document.selection.createRange();
    range.select();
  } else if ((typeof input.selectionStart != 'undefined') && (typeof input.scrollTop != 'undefined')) {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var scroll = input.scrollTop;
    var sel = input.value.substr(start, end - start);
    if (sel.length > 0) text = sel;
    text = left + text + right;
    input.value = input.value.substr(0, start) + text + input.value.substr(end);
    var pos = start + text.length;
    input.selectionStart = pos;
    input.selectionEnd = pos;
    input.scrollTop = scroll;
  } else if (typeof input.selectionStart != 'undefined') {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var sel = input.value.substr(start, end - start);
    if (sel.length > 0) text = sel;
    text = left + text + right;
    input.value = input.value.substr(0, start) + text + input.value.substr(end);
    var pos = start + text.length;
    input.selectionStart = pos;
    input.selectionEnd = pos;
  } else {
    input.value = input.value + left + text + right;
  }
}

function fillColor(obj) {
  obj.className += " user_selected";
}

function restoreColor(obj) {
  obj.className = obj.className.replace(/ user_selected/, "");
}

function selectRadio(name, index) {
  var obj = document.getElementsByName(name)[index];
  obj.checked = "checked";
}

function setText(id, text) {
  var obj = document.getElementById(id);
  obj.firstChild.data = text;
}

function setLink(url) {
  window.location.href = url;
}

function showTeamText(obj) {
  var option = obj.value.split("_");
  for (child = obj.parentNode.parentNode.firstChild; child != null; child = child.nextSibling) {
    if (child.nodeName != "INPUT") continue;
    if (child.name == "function") {
      child.value = option[3].base64decode();
    } else if (child.name == "trikot") {
      child.value = option[1];
    }
  }
}

function showTeamFoto(obj) {
  var option = obj.value.split("_");
  for (child = obj.parentNode.parentNode.firstChild; child != null; child = child.nextSibling) {
    if (child.nodeName != "IMG") continue;
    child.src = "users/" + option[2];
    child.style.width = "";
    child.style.height = "105px";
    if (options[2].indexOf(".") >= 0) {
      child.style.display = "block";
    } else {
      child.style.display = "none";
    }
  }
}

String.prototype.base64decode = function ()
{
  if (!window.base64decodeArray) {
    var code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    base64decodeArray = new Array();
    for (var i = 0; i < 64; ++i) base64decodeArray[code.charAt(i)] = i;
    base64decodeArray['='] = 0;
  }
  var str = this.replace(/\n|\r/g, "");
  var res = "";
  var i = 0;
  while (i < str.length) {
    var c = base64decodeArray[str.charAt(i)] << 18;
    c |= base64decodeArray[str.charAt(i + 1)] << 12;
    c |= base64decodeArray[str.charAt(i + 2)] << 6;
    c |= base64decodeArray[str.charAt(i + 3)];
    i += 4;
    res += String.fromCharCode(c >> 16) + String.fromCharCode((c >> 8) & 0xff) + String.fromCharCode(c & 0xff);
  }
  if (str.charCodeAt(i - 1) != 0x3d) return res.fromUtf8Bytes();
  if (str.charCodeAt(i - 2) != 0x3d) return res.slice(0, res.length - 1).fromUtf8Bytes();
  return res.slice(0, res.length - 2).fromUtf8Bytes();
}

String.prototype.fromUtf8Bytes = function ()
{
  var res = "";
  var i = 0;
  while (i < this.length) {
    var c = this.charCodeAt(i++);
    if (c < 128) {
      res += String.fromCharCode(c);
    } else if ((c >= 192) && (c < 224)) {
      res += String.fromCharCode(((c & 31) << 6) | (this.charCodeAt(i++) & 63));
    } else {
      c = ((c & 15) << 12) | ((this.charCodeAt(i++) & 63) << 6);
      res += String.fromCharCode(c | (this.charCodeAt(i++) & 63));
    }
  }
  return res;
}



