function $(id)
{
	return document.getElementById(id);
}

function openPanel(panel) {
	var panel = window.open('panel.php?name=' + panel + '&style=' + style + '&lang=' + lang, '', 'width=640,height=480,scrollbars=yes');
}

function getData()
{
	var data = window.open('getdata.php?style=' + style + '&lang=' + lang,
                            '',
                            'width=640, height=480, location=no, menubar=no, scrollbars=yes, status=no, toolbar=no, resizable=no');
}

function getDataSmall()
{
	if (window.opera && 0)
		getData();
	else
	{
		$('scantext').value = '';
		$('scan_div').style.display = '';
		$('scantext').focus();
	}
}

function sendScan()
{
	if (window.opera)
	{
		$('iframe_scantext').value = $('scantext').value;
		$('iframe_scanform').submit();
	}
	else
	{
		var curscript = window.document.createElement('script');
		curscript.type = 'text/javascript';
		//curscript.src = 'getdata.php?js_only=1&lang=' + lang + '&text=' + escape($('scantext').value);
		curscript.src = 'getdata.php?js_only=1&lang=' + lang + '&text=' + $('scantext').value;
		window.document.getElementsByTagName('head')[0].appendChild(curscript);
	}
	$('scan_div').style.display = 'none';
}

function scantext_handler(e)
{
	var keycode = (window.event) ? event.keyCode : e.keyCode;
	var escapekey = (window.event) ? 27 : e.DOM_VK_ESCAPE;

	if (keycode == escapekey)
		$('scan_div').style.display = 'none';
	else if (keycode == 13)
		$('scanform').submit();
}
function form_handler(e)
{
	var keycode = (window.event) ? event.keyCode : e.keyCode;

	if (keycode == 13)
	{
		sendForm('submitf');
		document.onclick = getAway;
	}
}

/// KB vergrößern
function maxKB()
{
	var kb_div = document.getElementById('kb_div');

	if (kb_div.style.display == 'none') {
		kb_div.style.display = '';
		document.getElementById('style').style.display = 'none';
	} else {
		kb_div.style.display = 'none';
		document.getElementById('style').style.display = '';
	}
}

function setCookie(name, value) {
  var curCookie = name + "=" + escape(value) +
      '; expires=Sunday, 17-Jan-2038 00:00:00 GMT';
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function saveState(id)
{
	cur_element = window.document.getElementById(id);

	if (cur_element.style.display == 'none')
		setCookie(id, '0');
	else
		setCookie(id, '1');
}

function loadState(id)
{
	if (getCookie(id) == '0')
		hideBox(id);
	else
		showBox(id);
}

function loadOldState(id)
{
	if (window.document.getElementById(id + '_show').value == '0')
		hideBox(id);
	else
		showBox(id);
}

function saveValue(name)
{
	cur_element = window.document.getElementsByName(name)[0];

	setCookie(name, cur_element.value);
}

function loadValue(name)
{
	cur_element = window.document.getElementsByName(name)[0];

	if (getCookie(name) != null)
		cur_element.value = getCookie(name);
}

function saveCValue(name)
{
	cur_element = window.document.getElementsByName(name)[0];

	setCookie(name, cur_element.checked);
}

function loadCValue(name)
{
	cur_element = window.document.getElementsByName(name)[0];

	if (getCookie(name) != null)
		cur_element.checked = (getCookie(name) == 'true') ? true : false;
}

function saveSettings()
{
	setCookie('data_saved', '1');

	saveState('cases');
	saveState('info');
//	saveState('menu');
	saveState('multi');
	saveState('results');
	saveState('weights');

	saveValue('simcount');
	saveValue('num[0]');
	saveValue('num[1]');

	saveValue('wmet');
	saveValue('wkris');
	saveValue('wdeut');

	saveValue('style');
	saveValue('template');
	saveValue('lang');

	saveCValue('def_tf');
	saveCValue('rf_enabled');
	saveCValue('auto_complete');
}

function loadOldStates()
{
	loadOldState('cases');
//	loadOldState('info');
//	loadOldState('menu');
//	loadOldState('multi');
	loadOldState('results');
//	loadOldState('weights');
}

function loadStates()
{
	if (getCookie('data_saved') != '1')
		return;

	loadState('cases');
//	loadState('info');
//	loadState('menu');
//	loadState('multi');
	loadState('results');
//	loadState('weights');
}

function loadSettings()
{
	if (getCookie('data_saved') != '1')
		return;

	loadStates();

	loadValue('simcount');
	loadValue('num[0]');
	loadValue('num[1]');

	loadValue('wmet');
	loadValue('wkris');
	loadValue('wdeut');

	if (getCookie('style'))
	{
		loadValue('style');
		setStyle(getCookie('style'));
	}
	loadValue('template');
	loadValue('lang');

	loadCValue('def_tf');
	loadCValue('rf_enabled');
	loadCValue('auto_complete');
}

function setStyle(newstyle)
{
	style = newstyle;
	document.getElementById('dragostyle').href = 'styles/' + newstyle + '.css';
}

function setAutocomplete(el)
{
	if (el.checked)
		document.form.setAttribute('autocomplete', 'on');
	else
		document.form.setAttribute('autocomplete', 'off');
}

function showImage(id)
{
	cur_element = window.document.getElementById(id);
	cur_element.src = 'img/min.gif';
}

function hideImage(id)
{
	cur_element = window.document.getElementById(id);
	cur_element.src = 'img/max.gif';
}

function showHideImage(id)
{
	cur_element = window.document.getElementById(id);
	
	cur_element.src = (cur_element.src.indexOf('img/min.gif') > 0) ? 'img/max.gif' : 'img/min.gif';
}

function showHide(id)
{
	cur_element = window.document.getElementById(id);
	if (cur_element.style.display == 'none')
		showBox(id);
	else
		hideBox(id);
}

function showObjID(id)
{
	showBox(id);
}

function hideObjID(id)
{
	hideBox(id);
}

function showBox(id)
{
	cur_element = window.document.getElementById(id);

	cur_element.style.display = '';
	//window.document.getElementById(id + '_show').value = '1'
	showImage('img_' + id);
}

function hideBox(id)
{
	cur_element = window.document.getElementById(id);
	
	cur_element.style.display = 'none';
	//window.document.getElementById(id + '_show').value = '0'
	hideImage('img_' + id);
}

function imgEfx(img,px,fr){
fr++;
if (fr <= 7){
	px = px + Math.round(fr/3);
	pxl = Math.round(px/1,5)
}else{
	px = px - 3;
	if (px < 0)px = 0;
	pxl = px;
}
try{
	
	window.document.getElementById('img' + img).style.marginTop = px + 'px';
	window.document.getElementById('img' + img).style.marginLeft = pxl  + 'px';
}catch(e){
		px = 0;
}
if (px > 0)setTimeout('imgEfx('+ img + ','+ px +','+ fr +')',5);	
return;	
}

function goAway()
{
	return false;
}

function sendForm(type)
{
	document.getElementsByName(type)[0].value = '1';

	if (type == 'submitf')
	{
		/*var cur_link = document.getElementById('simulate_fight');
		cur_link.href = "javascript:window.alert('Please be patient!');window.close();";
		cur_link.target = '_blank';
		cur_link.className = 'disabled';
		document.onclick = goAway;*/

		$('loading').style.display = '';
		$('loading').focus();
	}

	document.form.submit();
}

function setField(name, value) {
	window.opener.document.getElementsByName(name)[0].value = value;
}

function createURL()
{
	var s = 'http://' + location.host + location.pathname + '?';
	var removed = ' submitf reset apply page noogametools ';

	var els = document.form.elements;
	for (var i = 0; i < els.length; i ++)
	{
		el = els[i];
		if (removed.indexOf(' ' + el.name + ' ') == -1 && el.value)
			if (el.tagName == 'INPUT' && el.type == 'checkbox')
				if (el.checked)
					s += '&' + el.name + '=on';
				else
					s += '&' + el.name + '=off';
			else
				s += '&' + el.name + '=' + el.value;
	}

	prompt('This is the link to this page. Copy it by pressing Ctrl+C.', s);
}


