function captcha () {
  document.write('<span class="captcha_reload">Klicke <a href="javascript: void(0);" onclick="reload_captcha();">hier</a>, um das Sicherheits-Bild neu zu laden, falls du die Buchstaben nicht lesen kannst und <a href="javascript: void(0);" onclick="redgreen_captcha();">hier</a>, falls du an einer Rot-Grün-Sehschwäche leidest.</span>');
}
function change_box (num, ref) {
  ids = new Array('right_box_buddylist', 'right_box_map', 'right_box_blog', 'right_box_favorites', 'right_box_group');
  for (i = 0; i <= 4; i++) {
    if (document.getElementById(ids[i])) {
      if (i + 1 == num) {
        document.getElementById(ids[i]).style.display = 'block';
      } else {
        document.getElementById(ids[i]).style.display = 'none';
      }
    }
  }
  for (i = 0; i <= document.getElementById('right_menu_switch').getElementsByTagName('a').length - 1; i++) {
    document.getElementById('right_menu_switch').getElementsByTagName('a')[i].className = '';
  }
  ref.className = 'hover';
  document.cookie = 'box='+ num + '; path=/;';
  return false;
}
function map (i, t) {
  if (t == 1) {
    t = 'map_bottom_left';
  } else if (t == 2) {
    t = 'map_box_doc';
  }
  if (document.getElementById(t).getElementsByTagName('table')[0].getElementsByTagName('thead')[0].getElementsByTagName('th')[i].className == 'hover') {
    document.getElementById(t).getElementsByTagName('table')[0].getElementsByTagName('thead')[0].getElementsByTagName('th')[i].className = '';
  } else {
    document.getElementById(t).getElementsByTagName('table')[0].getElementsByTagName('thead')[0].getElementsByTagName('th')[i].className = 'hover';
  }
}
function reload_captcha () {
  var random = Math.round(Math.random()/(1/99999));
  random = random + 100000;
  captcha = new Image()
  captcha.src = 'http://neagora.de/captcha/' + random + '.jpg';
  document.getElementById('captcha_img').src = captcha.src;
}
function redgreen_captcha () {
  var random = Math.round(Math.random()/(1/9999999));
  random = random + 10000000;
  captcha = new Image()
  captcha.src = 'http://neagora.de/captcha/' + random + '.jpg';
  document.getElementById('captcha_img').src = captcha.src;
}
function set_maximum (id, select, check_id) {
  select = select.replace('.', '');
  select = select.replace('.', '');
  if (document.getElementById(id).tagName.toLowerCase() == 'input') {
    document.getElementById(id).value = select;
  } else {
    document.getElementById(id).selectedIndex = document.getElementById(id).getElementsByTagName('option').length - 1;
  }
  if (check_id != '') {
    document.getElementById(check_id).checked = 'checked';
  }  
}
function maximum_button (id, select, check_id) {
  if (select < 0) {
    select = 0;
  }
  document.write('<span onclick="set_maximum(\'' + id + '\', \'' + select + '\', \'' + check_id + '\');">(Maximum: ' + select + ')</span>');
}
function generate_css_classes () {
  document.getElementsByTagName('body')[0].className = 'javascript_enabled';
}
function select_all_checkboxes () {
  for (i = 0; i < document.getElementsByTagName('input').length; i++) {
    if (document.getElementsByTagName('input')[i].type == "checkbox" && document.getElementsByTagName('input')[i].id != 'check_all') {
      document.getElementsByTagName('input')[i].checked = !document.getElementsByTagName('input')[i].checked;
    }    
  }
}
function select_all_checkboxes_checkbox () {
  document.write('<td><input type="checkbox" class="checkbox" id="check_all" onclick="select_all_checkboxes();" /></td><td><label for="check_all">Alle auswählen</label></td>');
}
function toggle_right_menu (id1, id2) {
  fixInternetExplorerRedrawIssues('vote_button');
  if (document.getElementById(id1).className.length > 0) {
    document.getElementById(id1).className = '';
    document.cookie = id2 + '=open; path=/;';
    document.getElementById(id2).getElementsByTagName('span')[0].className = 'javascript';
  } else {
    document.getElementById(id1).className = 'noheight';
    document.cookie = id2 + '=closed; path=/;';
    document.getElementById(id2).getElementsByTagName('span')[0].className = 'javascript_hover';
  }
}
function on_load_function () {
  generate_css_classes();
  var items = new Array('resources', 'resources_list', 'character', 'character_information', 'inventory_set_box', 'inventory_set_items', 'premium_box', 'premium_box_items', 'tip_box', 'tip_content');
  for (var i = 0; i <= items.length; i += 2) {
    if (document.getElementById(items[i])) {
      if (document.getElementById(items[i]).getElementsByTagName('span')[0].className.length == 0) {
        document.getElementById(items[i]).getElementsByTagName('span')[0].className = 'javascript';
      }
    }
  }
  if (document.getElementById('resources')) {
    document.getElementById('resources').onclick = function () { 
      toggle_right_menu ('resources_list', 'resources');
    }
  }
  if (document.getElementById('character')) {
    document.getElementById('character').onclick = function () { 
      toggle_right_menu ('character_information', 'character');
    }
  }
  if (document.getElementById('premium_box')) {
    document.getElementById('premium_box').onclick = function (e) { 
      if (!e) {  
        var e = window.event;
      }
      var target = e.target || e.srcElement;
      if (target.tagName != 'A') {
        toggle_right_menu ('premium_box_items', 'premium_box');
      }
    }
  }
  if (document.getElementById('inventory_set_box')) {
    document.getElementById('inventory_set_box').onclick = function (e) { 
      if (!e) {  
        var e = window.event;
      }
      var target = e.target || e.srcElement;
      if (target.tagName != 'A') {
        toggle_right_menu ('inventory_set_items', 'inventory_set_box');
      }
    }
  }
  if (document.getElementById('tip_box')) {
    document.getElementById('tip_box').onclick = function (e) { 
      if (!e) {  
        var e = window.event;
      }
      var target = e.target || e.srcElement;
      if (target.tagName != 'A') {
        toggle_right_menu ('tip_content', 'tip_box');
      }
    }
  }
  var currentItem = document.getElementsByTagName('input');
  if (document.URL.substr(document.URL.length - 11, 9) == "ressource") {
    var explicitFocus = document.URL.substr(document.URL.length - 1, 1) * 1; 
  }
  if (currentItem[explicitFocus] != null && (currentItem[explicitFocus].type == "text" || currentItem[explicitFocus].type == "password")) {
    currentItem[explicitFocus].focus();
  } else {
    var onlyControls = true;
    for (i = 0; i < currentItem.length; i++) {
      if (currentItem[i].type == "text" || currentItem[i].type == "password") {
        currentItem[0].focus();
        break;
      }
    }
  }
}
function set_text_width(text1, text2, idwidth, idtext, width) {
  if (document.getElementById(idwidth).offsetWidth < width) {
    document.getElementById(idtext).innerHTML = text1;
  } else {
    document.getElementById(idtext).innerHTML = text2;
  }
}
var clickTimeout;
function set_long_click_event(active, delay, url) {
  if (active == 1) {
    clickTimeout = window.setTimeout('call_url(\'' + url + '\')', delay);
  }
  return false;
}
function unset_long_click_event() {
  if (clickTimeout != undefined) {
    window.clearTimeout(clickTimeout);
  }
}
function call_url(url) {
  window.location.href = url;
}
function fixInternetExplorerRedrawIssues(id) {
  if (navigator.appName == 'Microsoft Internet Explorer') {
    document.getElementById(id).style.display = 'none';
    document.getElementById(id).style.display = 'block';
  }
}
/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/
function ajax_request(method, url, data, cb, apply_para) {
  method = method.toLowerCase();
  var req;
  req = new XMLHttpRequest();
  req.open(method, url + (data && method == 'get' ? '?' + data : ''), true);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  if (method == 'post') {
    req.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
    req.setRequestHeader("Content-Length", data.length);
  }
  req.onreadystatechange = function() {
    if (req.readyState == 4 && req.status == 200) {
      if (cb) {
        cb.apply(null, [req].concat(apply_para));
      }
    }
  }
  req.send(data);
}
function ajax_request_handler(xhr, type, id) {
  if (type == 'comment_delete') {
    fadeOut('comment_' + id);
  }
}
function fadeOut(id) {
  for (i = 1; i <= 5; i++) {
    if (i <= 4) {
      setTimeout('setHeight(\'' + id + '\', ' + (document.getElementById(id).offsetHeight / (i + 1)) + ')', i * 100);
    } else {
      setTimeout('Hide(\'' + id + '\')', i * 100);
    }  
  }
}
function setHeight(id, height) {
  document.getElementById(id).style.height = height + 'px';
}
function Hide(id) {
  document.getElementById(id).style.display = 'none';
}
var isCtrl = false; 
document.onkeyup = function (e) { 
  if (e.which == 17) {
    isCtrl = false;
  } 
}
document.onkeydown = function (e) {
  if (e.which == 17) { 
    isCtrl = true;
  }
}
var colorList = new Array(
'#000000', '#330000', '#660000', '#000000', '#003300', '#006600',
'#990000', '#FF0000', '#FA7D4C', '#3D4C17', '#436600', '#648A00',
'#FF7700', '#FF9600', '#FFFFFF', '#96CE00', '#D9FF74', '#FFFFFF',
'#000000', '#002F3E', '#006666', '#000000', '#000033', '#000099',
'#00507A', '#008AD3', '#00C0FF', '#0000CC', '#3333FF', '#8F8FFF',
'#00FFEA', '#00FFEA', '#FFFFFF', '#B400FF', '#E3A1FF', '#FFFFFF');
var lastColorBBCodeClick = new Date(0);
function bbcodes (codes, target, bold, color, image, url, sizable, quote) {
  label = document.getElementById(codes);
  tmp = "";
  tmp += '<div id="bbcode">';
  tmp += '<ul>';
  if (bold == 1) {
    tmp += '<li class="bold" onmousedown="setIeRange();" onclick="bbcode_bold(\'' + target + '\');" title="Text dick darstellen">Text dick darstellen</li>';
  }
  if (color == 1) {
    tmp += '<li id="bbcode_color" class="color" onmousedown="setIeRange();" onclick="bbcode_color(\'' + target + '\', \'\');" title="Text farbig darstellen">Text farbig darstellen<br />';
    tmp += '<div>';
    tmp += '<table cellpadding="0" cellspacing="0">';
    tmp += '<tbody>';
    tmp += '<tr>';
    for (i = 0; i < colorList.length; i++) {
      if (i > 0 && i % 6 == 0) {
        tmp += '</tr><tr>';
      }
      
      tmp += '<td style="background-color: ' + colorList[i] + ';" title="' + colorList[i] + '" onclick="bbcode_color(\'' + target + '\', \'' + colorList[i] + '\');"><span>' + colorList[i] + '</span></td>';
      
    }
    tmp += '</tr>';
    tmp += '</tbody>';
    tmp += '</table>';
    tmp += '</div>';
    tmp += '</li>';
  }
  if (color == 1) {
    tmp += '<li class="img" onmousedown="setIeRange();" onclick="bbcode_image(\'' + target + '\');" title="Bild hinzufügen">Bild hinzufügen</li>';
  }
  if (url == 1) {
    tmp += '<li class="url" onmousedown="setIeRange();" onclick="bbcode_url(\'' + target + '\');" title="Link hinzufügen">Link hinzufügen</li>';
  }
  if (quote == 1) {
    tmp += '<li class="quote" onmousedown="setIeRange();" onclick="bbcode_quote(\'' + target + '\');" title="Zitatbox hinzufügen">Zitatbox hinzufügen</li>';
  }
  if (sizable == 1) {
    tmp += '<li class="smaller" onclick="bbcode_sizeable(\'' + target + '\', -50);" title="Textfeld verkleinern">Textfeld verkleinern</span>';
    tmp += '<li class="bigger" onclick="bbcode_sizeable(\'' + target + '\', 50);" title="Textfeld vergrößern">Textfeld vergrößern</span>';
  }
  tmp += '</ul>';
  tmp += '</div>';
  label.innerHTML += tmp;
}

function setIeRange() {
  ieSelection = document.selection.createRange();
}
function setCaretPosition(element, pos) {
	if(element.setSelectionRange) {
		element.selectionStart = pos;
		element.selectionEnd = pos;
  } else if (element.createTextRange) {
    var range = element.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
  }
}
var ieSelection = "";
function get_selection(id) {
  textarea = document.getElementById(id);
  if (document.selection) {
    selection = ieSelection.duplicate();
    selection.moveToElementText(textarea);
    selection.setEndPoint('EndToEnd', ieSelection);
    textarea.selectionStart = selection.text.length - ieSelection.text.length;
    textarea.selectionEnd = textarea.selectionStart + ieSelection.text.length;
  }
  return textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
}
function set_selection(id, text) {
  textarea = document.getElementById(id);
  oldlength = textarea.value.length;
  select = textarea.selectionStart;
  buffer = textarea.value.substring(0, textarea.selectionStart);
  buffer += text;
  buffer += textarea.value.substring(textarea.selectionEnd, textarea.value.length)
  textarea.value = buffer;
  setCaretPosition(textarea, select + text.length);
}
function bbcode_bold (id) {
  text = get_selection(id);
  if (text != '' && text != null) {
    set_selection(id, '[b]' + text + '[/b]');
  } else {
    text = prompt('Bitte gib den Text ein, den du dick darstellen möchtest.', '');
    if (text != '' && text != null) {
      document.getElementById(id).value += '[b]' + text + '[/b]';
    }
  }
}
function bbcode_color (id, color) {  
  currentTime = new Date();
  if (currentTime - 1000 > lastColorBBCodeClick) {
	
	  if (color == '') {
	    color = prompt('Bitte gib den Hex-Code der Farbe ein, die du benutzen möchtest, zum Beispiel: "#FF0000".', '');
	  }
	  if (color != '' && color != null) {
	    text = get_selection(id);
	    if (text != '' && text != null) {
	      set_selection(id, '[color=' + color + ']' + text + '[/color]');
	    } else {
	      text = prompt('Bitte gib den Text ein, den du farbig darstellen möchtest.', '');
	      if (text != '' && text != null) {
	        document.getElementById(id).value += '[color=' + color + ']' + text + '[/color]';
	      }
	    }
	  }
	  lastColorBBCodeClick = new Date();
  }
}
function bbcode_image (id) {
  url = get_selection(id);
  if (url != '' && url != null) {
    set_selection(id, '[img]' + url + '[/img]');
  } else {
	  url = prompt('Bitte gib Pfad zu dem Bild ein, dass du einbinden möchtest. Das Bild darf maximal 400 Pixel breit sein. Wird diese Maximalbreite überschritten, solltest du das Bild lieber direkt verlinken.', '');
	  if (url != '' && url != null) {
	    document.getElementById(id).value += '[img]' + url + '[/img]';
	  }
  }
}
function bbcode_url (id) {
  Url = prompt('Bitte gib den Link ein, den du hinzfügen möchtest.', '');
  if (Url != '' && Url != null) {
    text = get_selection(id);
    if (text != '' && text != null) {
      set_selection(id, '[url=' + Url + ']' + text + '[/url]');
    } else {
      text = prompt('Bitte gib optional einen Alternativtext für deinen Link ein oder klicke einfach auf "Ok".', '');
	    if (text != '' && text != null) {
	      document.getElementById(id).value += '[url=' + Url + ']' + text + '[/url]';
	    } else {
	      document.getElementById(id).value += '[url]' + Url + '[/url]';
	    }
	  }
  }
}
function bbcode_sizeable (id, size) {
  element = document.getElementById(id);
  height = parseInt(element.offsetHeight);
  if (size > 0 || (size < 0 && height > 200)) {
    element.style.height = (height + size) + 'px';
    parent = element.parentNode;
    if (parent.className == 'wrapper') {
      parent.style.height = (height + size) + 'px';
    }
  }
}
function bbcode_quote (id) {
  title = prompt('Bitte gib den Titel der Zitatbox ein. Zum Beispiel "Zitat von Blauesocke".', '');
  if (title != '' && title != null) {
    text = get_selection(id);
    if (text != '' && text != null) {
      set_selection(id, '[quote=' + title + ']' + text + '[/quote]');
    } else {
	    text = prompt('Bitte gib den Inhalt der Zitatbox ein.', '');
	    if (text != '' && text != null) {
	      document.getElementById(id).value += '[quote=' + title + ']' + text + '[/quote]';
	    }
    }
  }
}
function resize_player()
{
  player = document.getElementById('player');
  player_container = document.getElementById('playercontainer');
  if (player_container) {
     player_width = player_container.offsetWidth;

     video_width  = player_width - 30;
     video_height  = video_width * 0.75;
     player_height = video_height + 60;
     player.stageSize(player_width, player_height);
     player_container.style.height = player_height + 'px';
   }
}
function try_to_resize_player() {
   try {
     resize_player();
     window.setTimeout('resize_player();', 300);
   }
   catch (ex) {
     window.setTimeout('try_to_resize_player();', 100);
   }  
}
function twoDigits(digit) {
	if(digit < 10)
	{
		return '0' + digit;
	}
	else
	{
		return digit;
	}
}
