var IE = document.all?true:false

if ( !IE ) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMPos;

var x = 0;
var y = 0;

function checkPass() {
	setTimeout('rlyCheckPass()',30);
	setTimeout('rlyCheckNick()',30);
}

function rlyCheckPass() {
	a=$('regf1');
	b=$('regf2');
	
	if ( a.value == '' ) return 0;
	
	if ( a.value != b.value ) {
		$('reg2').style.color='darkred';
		$('reg2').innerHTML='Passw&ouml;rter stimmen nicht &uuml;berein';
	}
	else {
		$('reg2').style.color='darkgreen';
		$('reg2').innerHTML='Passw&ouml;rter stimmen &uuml;berein';
	}
}

function rlyCheckNick() {
	nick=$('regf0').value;
	if ( nick.search("'") != -1 || nick.search('"') != -1 ) {
		$('reg1').style.color='darkred';
		$('reg1').innerHTML='Name darf folgende Zeichen nicht enthalten: \' &quot; ';
		window.nickBlurCheckd=0;
	}
	else if ( ! window.nickBlurCheckd ) {
		$('reg1').style.color='darkgrey';
		$('reg1').innerHTML='Warten...';
	}
}

function checkNickBlur() {
	new Ajax.Request('/ajax/checkNickExists.php?nick='+$('regf0').value, {
		onSuccess: function(transport) {
			if ( transport.responseText == 'ok' ) {
				$('reg1').style.color='darkgreen';
				$('reg1').innerHTML='Name ist noch verf&uuml;gbar';
			}
			else {
				$('reg1').style.color='darkred';
				$('reg1').innerHTML='Entschuldigung, dieser Name ist schon vergeben';
			}
		}	
	});
	window.nickBlurCheckd=1;
}

function quickanswer_show() {
	obj=$('answer');
	obj.style.display='block';
	obj.style.width='590px';
	obj.style.height='0px';
	
	for ( i=0; i<50; i++ ) {
		setTimeout('$(\''+obj.id+'\').style.height=\''+i*4+'px\'',i*5);
	}
	
	setTimeout('$(\'sendbutton\').style.display=\'block\'',320);
	setTimeout('$(\'answer\').focus()',250);
	
	$('answer_link').style.display='none';
}

function incrRess(rn,by) {
	
	duration=$('r'+rn).every;
	setTo=Number(Number($('r'+rn).innerHTML)+Number(by));
	
	if ( setTo > Number($('kapazitaet').innerHTML) ) return 0;
	
	setTimeout('$(\'r'+rn+'\').innerHTML='+setTo,duration);
	
	if ( duration > 2500 ) {
		setTimeout('$(\'r'+rn+'\').style.color=\'#eeeeee\'',Number(duration-100));
		setTimeout('$(\'r'+rn+'\').style.color=\'#aaaaaa\'',Number(duration-60));
		setTimeout('$(\'r'+rn+'\').style.color=\'#777777\'',Number(duration-20));
		setTimeout('$(\'r'+rn+'\').style.color=\'#888888\'',Number(duration-20));
		setTimeout('$(\'r'+rn+'\').style.color=\'#999999\'',Number(duration-60));
		setTimeout('$(\'r'+rn+'\').style.color=\'#AAAAAA\'',Number(duration+100));
		setTimeout('$(\'r'+rn+'\').style.color=\'#BBBBBB\'',Number(duration+140));
		setTimeout('$(\'r'+rn+'\').style.color=\'#cccccc\'',Number(duration+180));
		setTimeout('$(\'r'+rn+'\').style.color=\'#DDDDDD\'',Number(duration+220));
		setTimeout('$(\'r'+rn+'\').style.color=\'#EEEEEE\'',Number(duration+260));
		setTimeout('$(\'r'+rn+'\').style.color=\'#FFFFFF\'',Number(duration+300));
	}
	
	setTimeout('incrRess(\''+rn+'\','+by+')',duration+1);
}

function startRess(rn) {
	val=Number($('r'+rn).innerHTML);
	prod=Number($('p'+rn).innerHTML);
	if ( prod == 0 ) return 0;
	everyNsecs=Number(3600/prod*1000);
	if ( prod < 0 ) {
		everyNsecs=everyNsecs * (-1);
		$('r'+rn).every=everyNsecs;
		incrRess(rn,-1);
	}
	else {
		$('r'+rn).every=everyNsecs;
		incrRess(rn,1);
	}
}

function islFadeStep(oid,step) {
	obj=$(oid);
	
	if ( step == 0 ) {
		obj.style.color='#444444';
	}
	if ( step == 1 ) {
		obj.style.color='#555555';
	}
	if ( step == 2 )  {
		obj.style.color='#666666';
	}
	if ( step == 3 ) {
		obj.style.color='#777777';
	}
	if ( step == 4 ) {
		obj.style.color='#888888';
	}
}


function islFade(obj) {
	obj=$(obj);
	
	n=40;
	
	obj.t=Array();
	obj.t[0]=setTimeout('islFadeStep("'+obj.id+'",0)',n*0);
	obj.t[1]=setTimeout('islFadeStep("'+obj.id+'",1)',n*1);
	obj.t[2]=setTimeout('islFadeStep("'+obj.id+'",2)',n*2);
	obj.t[3]=setTimeout('islFadeStep("'+obj.id+'",3)',n*3);
	obj.t[4]=setTimeout('islFadeStep("'+obj.id+'",4)',n*4);
}

function islFadeout(obj) {
	obj=$(obj);
	
	clearTimeout(obj.t[0]);
	clearTimeout(obj.t[1]);
	clearTimeout(obj.t[2]);
	clearTimeout(obj.t[3]);
	clearTimeout(obj.t[4]);
	
	setTimeout('islFadeStep("'+obj.id+'",4)',n*0);
	setTimeout('islFadeStep("'+obj.id+'",3)',n*1);
	setTimeout('islFadeStep("'+obj.id+'",2)',n*2);
	setTimeout('islFadeStep("'+obj.id+'",1)',n*3);
	setTimeout('islFadeStep("'+obj.id+'",0)',n*4);
	
	setTimeout('$("'+obj.id+'").style.borderBottom="'+obj.prevBorder+'"',n*5);
	setTimeout('$("'+obj.id+'").style.color="'+obj.prevCol+'"',n*5);
}


function liFade(obj) {
	obj=$(obj);
	
	n=40;
	
	obj.t=Array();
	obj.t[0]=setTimeout('liFadeStep("'+obj.id+'",0)',n*0);
	obj.t[1]=setTimeout('liFadeStep("'+obj.id+'",1)',n*1);
	obj.t[2]=setTimeout('liFadeStep("'+obj.id+'",2)',n*2);
	obj.t[3]=setTimeout('liFadeStep("'+obj.id+'",3)',n*3);
	obj.t[4]=setTimeout('liFadeStep("'+obj.id+'",4)',n*4);
}

function liFadeout(obj) {
	obj=$(obj);
	
	clearTimeout(obj.t[0]);
	clearTimeout(obj.t[1]);
	clearTimeout(obj.t[2]);
	clearTimeout(obj.t[3]);
	clearTimeout(obj.t[4]);
	
	setTimeout('liFadeStep("'+obj.id+'",4)',n*0);
	setTimeout('liFadeStep("'+obj.id+'",3)',n*1);
	setTimeout('liFadeStep("'+obj.id+'",2)',n*2);
	setTimeout('liFadeStep("'+obj.id+'",1)',n*3);
	setTimeout('liFadeStep("'+obj.id+'",0)',n*4);
	
	setTimeout('$("'+obj.id+'").style.borderBottom="'+obj.prevBorder+'"',n*5);
	setTimeout('$("'+obj.id+'").style.color="'+obj.prevCol+'"',n*5);
	
	if ( typeof(liFadeInit) == 'function' ) {
		setTimeout('liFadeInit("'+obj.id+'")',n*5+1);
	}
}

window.storeH=Array();
function setmap(x,y) {
	if ( IE ) window.location='/map/'+x+','+y;
	try {
		$('linkspan').innerHTML='Laden...';
		if ( x != 'home' ) url='/ajax/fastCreateMap.php?X='+x+'&Y='+y;
		else url='/ajax/fastCreateMap.php';
		new Ajax.Request(url, {
			onSuccess: function(transport) {
				a=transport.responseText.split('[====================');
				b=a[1];
				b=b.split('====================]');
				b=b[0];
				eval(b);
				$('mapContents').innerHTML=a[0];
				
				if ( window.flagv ) showflags();
			}
		});
		
		$('gmp').href='/gmap/'+x+','+y;
		$('smp').href='/map/'+x+','+y;
		
		$('linkspan').innerHTML='Link zu dieser Ansicht: <a href="/map/'+x+','+y+'">/map/'+x+','+y+'</a>';
	}
	catch ( err ) {
		alert(err);
		window.location='/map/'+x+','+y;
	}
}

function bc_next(i) {
	n=i+1;
	if ( ! $('bau_'+n) ) return 0;
	$('bau_'+i).style.display='none';
	$('bau_'+n).style.display='inline-table';
}

function bc_prev(i) {
	n=i-1;
	if ( ! $('bau_'+n) ) return 0;
	$('bau_'+i).style.display='none';
	$('bau_'+n).style.display='inline-table';
}

function showTip(id) {
	if ( id == 0 ) return 0;
	new Ajax.Request('/ajax/tip.php?id='+id, {
		onSuccess: function(transport) {
			$('tipc').innerHTML=transport.responseText;
			if ( $('tipc').innerHTML == 'NONE' ) {
				showTip(1);
			}
			else window.currentTip=id;
			$('tnum').innerHTML='(Tip '+window.currentTip+' von '+window.maxTip+')';
		}
	});
}

function checkNames(l) {
	if ( ! l ) return 0;
	new Ajax.Request('/ajax/checknames.php?namelist='+l, {
		onSuccess: function(transport) {
			$('namecheck').innerHTML=transport.responseText;
		}
	});
}

function expandShadbox(obj) {
	if ( ! obj ) obj=window.obj;
	th=window.storeH[obj.id];
	obj.style.margin=3+'px';
// 	alert(obj.style.margin);
	if ( obj.clientHeight >= th ) {
		obj.style.height='';
		return 0;
	}
	h=obj.clientHeight;
	window.obj=obj;
	nh=Number(h)+12;
	if ( nh >= th ) nh=th;
// 	alert("new:"+nh+"old:"+th);
// 	alert("height: "+h+" new height:"+nh);
	obj.style.height=nh+'px';
// 	alert(obj.style.height);
	setTimeout("expandShadbox(0)",15);
}

function collapseShadbox(obj) {
	if ( obj.clientHeight <= 0 && obj ) {
		expandShadbox(obj);
		return 0;
	}
	
	if ( ! window.storeH[obj.id] ) window.storeH[obj.id]=obj.clientHeight;
	if ( ! obj ) obj=window.obj;
	obj.style.margin=0+'px';
// 	alert(obj.style.margin);
	if ( obj.clientHeight <= 0 ) {
		return 0;
	}
	h=obj.clientHeight;
	window.obj=obj;
	nh=Number(h)-12;
	if ( nh < 0 ) nh=0;
// 	alert("height: "+h+" new height:"+nh);
	obj.style.height=nh+'px';
// 	alert(obj.style.height);
	setTimeout("collapseShadbox(0)",15);
}

function extractCookieVal(key) {
	c=document.cookie;
	all=document.cookie.split(';');
	la=all.length;
	for ( i=0; i<la; i++ ) {
		all[i]=all[i].replace(/^ /,'');
		cur=all[i].split('=');
		if ( cur[0] == key ) return cur[1];
	}
}

function hide(el) {
	$(el).style.display='none';
}

function contextMenu(config,menutitle) {
	this.confignum=config;
	if ( ! window.contextConfigs ) return 0;
	this.config=window.contextConfigs[config];
	if ( ! this.config ) this.config='Failed to load configuration.:#{}';
	this.config=this.config.split('{}');
	names=new Array();
	hrefs=new Array();
	this.config.each(function(r) {
		if ( ! r ) return 0;
		spl=r.split(':');
		names.push(spl[0]);
		hrefs.push(spl[1]);
	});
	
	linklist=Array();
	l=names.length;
	content='';
	subtr=0;
	for ( var i=0; i<l; i++ ) {
		if ( names[i] && hrefs[i] ) {
			j=Number(i)-subtr;
			if ( names[i].search(/\[sect\]/) != '-1' ) {
				content=content+'<div class="cmenuSubTitle">'+hrefs[i]+'</div>';
				continue;
			}
			set="document.cookie='pcle_"+this.confignum+'='+j+"';";
			set+="setTimeout(&quot;$('contMenu').style.display='none'&quot;,50);";
			content=content+'<a class="cmenu" id="cm_'+i+'" onclick="this.style.backgroundColor=&quot;#FF4400&quot;; '+set+'" onmouseover="fadein(this)" onmouseout="fadeout(this)" onclick="'+set+'" href="'+hrefs[i]+'">'+names[i]+'</a>';
		}
		else subtr=Number(subtr)+1;
	}
	
	this.content=content;
	this.menutitle=menutitle;
}

function fadein(obj) {
	n=40;
	
	obj.t=Array();
	obj.t[0]=setTimeout('fistep("'+obj.id+'",0)',n*0);
	obj.t[1]=setTimeout('fistep("'+obj.id+'",1)',n*1);
	obj.t[2]=setTimeout('fistep("'+obj.id+'",2)',n*2);
	obj.t[3]=setTimeout('fistep("'+obj.id+'",3)',n*3);
	obj.t[4]=setTimeout('fistep("'+obj.id+'",4)',n*4);
}

function fistep(oid,step) {
	obj=$(oid);
	if ( step == 0 ) {
		 obj.style.backgroundColor='#5E5042';
		 obj.style.color='#AAAAAA';
	}
	if ( step == 1 ) {
		 obj.style.backgroundColor='#78653C';
		 obj.style.color='#888888';
	}
	if ( step == 2 )  {
		 obj.style.backgroundColor='#A58734';
		 obj.style.color='#555555';
	}
	if ( step == 3 ) {
		 obj.style.backgroundColor='#D58920';
		 obj.style.color='#222222';
	}
	if ( step == 4 ) {
		 obj.style.backgroundColor='#FFBB00';
		 obj.style.color='#000000';
	}
}

function fadeout(obj) {
	clearTimeout(obj.t[0]);
	clearTimeout(obj.t[1]);
	clearTimeout(obj.t[2]);
	clearTimeout(obj.t[3]);
	clearTimeout(obj.t[4]);
	
	setTimeout('fistep("'+obj.id+'",4)',n*0);
	setTimeout('fistep("'+obj.id+'",3)',n*1);
	setTimeout('fistep("'+obj.id+'",2)',n*2);
	setTimeout('fistep("'+obj.id+'",1)',n*3);
	setTimeout('fistep("'+obj.id+'",0)',n*4);
	
	setTimeout('$("'+obj.id+'").style.backgroundColor="#3F3F3F"',n*5);
	setTimeout('$("'+obj.id+'").style.color="white"',n*5);
}

contextMenu.prototype.show=function() {
	
	if ( ! window.contextConfigs ) return 0;
	
	if ( ! window.x ) window.x=extractCookieVal('px');
	if ( ! window.y ) window.y=extractCookieVal('py');
	
	$('contMenu').style.left=window.x-120+"px";
	num=extractCookieVal('pcle_'+this.confignum);
	if ( ! num ) num=0;
	$('contMenu').style.top=window.y-40-num*20+"px";
	$('contMenuItems').innerHTML='<div class="cmenuTitle">'+this.menutitle+'</div>';
	$('contMenuItems').innerHTML+=this.content;
	$('contMenu').style.display='block';
	window.cme=1;
}

function cvChange(id,x,y) {
	window.contextConfigs[998]='Karte zentrieren:/map/'+x+','+y+'{}Truppen schicken:/gid/4/'+x+','+y+'{}Rohstoffe schicken [Land]:/gid/39/'+x+','+y+'{}Rohstoffe schicken [Meer]:/gid/7/'+x+','+y+'';
}

function cvShow() {
	goto=new contextMenu(998,'Aktionen');
	goto.show();
}

function checkContextMenu(event) {
	if ( window.disableCMenu == true || ! window.contextConfigs || window.cMenDisabled ) return 0;
// 	alert(window.cMenDisabled);
	la=document.forms.length;
	for ( i=0; i<la; i++ ) {
		lb=document.forms[i].length;
		for ( j=0; j<lb; j++ ) {
			t=document.forms[i].elements[j];
			if ( t.hasFocus ) return 0;
		}
	}
	
	if ( ! event ) event=window.event;
	if ( event.keyCode ) kc=event.keyCode;
	if ( event.which ) kc=event.which;
	
	if ( window.keyCheckerMap ) {
		if ( kc == 48 ) {
			if ( window.flagv ) hideflags();
			else showflags();
		}
		if ( kc == 55 ) setmap('home',false);
		if ( kc == 56 ) {
			$('i_2').src='/img/map/active_2.png';
			setmap(window.sx,Number(window.sy)+1);
		}
		if ( kc == 50 ) {
			$('i_4').src='/img/map/active_4.png';
			setmap(window.sx,Number(window.sy)-1);
		}
		if ( kc == 52 ) {
			$('i_3').src='/img/map/active_3.png';
			setmap(Number(window.sx)-1,Number(window.sy));
		}
		if ( kc == 54 ) {
			$('i_1').src='/img/map/active_1.png';
			setmap(Number(window.sx)+1,Number(window.sy));
		}
	}
	
	if ( kc == 103 ) { // "g"
		goto=new contextMenu(0,'Navigation');
		goto.show();
	}
	
	if ( kc == 82 ) { // "R"
		goto=new contextMenu(1,'Rohstoffe senden: Land');
		goto.show();
	}
	if ( kc == 69 ) { // "E"
		goto=new contextMenu(2,'Rohstoffe senden: Meer');
		goto.show();
	}
	
	if ( kc == 101 ) { // "r"
		goto=new contextMenu(3,'Handels-Verlauf: Meer');
		goto.show();
	}
	
	if ( kc == 114 ) { // "e"
		goto=new contextMenu(4,'Handels-Verlauf: Land');
		goto.show();
	}
	
	if ( kc == 97 ) { // "a"
		goto=new contextMenu(5,'Letzte Angriffsziele');
		goto.show();
	}
	
	if ( kc == 102 ) { // "f"
		goto=new contextMenu(7,'Rohstofflager voll...');
		goto.show();
	}
	
	if ( kc == 99 ) { // "c"
		goto=new contextMenu(8,'Berichte und Nachrichten');
		goto.show();
	}
	
	if ( kc == 121 ) { // "y"
		goto=new contextMenu(999,'Benutzerdefiniertes Menu');
		goto.show();
	}
	
	t=$('loginform');
	if ( kc == 113 && ! t ) { // "q"
		goto=new contextMenu(6,'Feorar verlassen?');
		goto.show();
	}
	else if ( kc == 113 ) $('loginform').submit();
}

document.onkeypress=checkContextMenu;

function fillFields() {
	try {
	user=$('luser');
	pass=$('lpass');
	if ( ! user.value && ! pass.value ) {
		setTimeout('$(\'luser\').value=\'Benutzername\'',600);
		setTimeout('$(\'lpass\').value=\'Passwort\'',600);
	}
	}
	catch (err) { }
}

function getMPos(ev) {
	document.mouseMoved=1;
	if (IE) { // grab the x-y pos.s if browser is IE
		x = event.clientX + document.body.scrollLeft
		y = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
			x = ev.pageX
			y = ev.pageY
	}  
	if ( x < 0 ) { x = 0 }
	if ( y < 0 ) { y = 0 }  
  
	window.x=x;
	window.y=y;
	
	x+=10;
	y+=10;
  
	$('ddiv').style.left=x+"px";
	$('ddiv').style.top=y+"px";
	return true
}

function sdiv(c) {
	$('ddiv').innerHTML=c;
	$('ddiv').style.display='block';
}

function hdiv() {
	$('ddiv').style.display='none';
	$('ddiv').innerHTML='';
}

function clock() {

	if ( ! $('sc_s') ) return 0;
	
	setTimeout('clock()',1000);
	
	s=$('sc_s');
	m=$('sc_m');
	h=$('sc_h');
	
	secs=Number(s.innerHTML);
	mins=Number(m.innerHTML);
	hours=Number(h.innerHTML);
	
	secs=secs+1;
	if ( secs >= 60 ) {
		secs="0";
		mins=mins+1;
	}
	if ( mins >= 60 ) {
		mins="0";
		hours=hours+1;
	}
	
	if ( hours >= 24 ) {
		secs="0";
		mins="0";
		hours="0";
	}
	
	if ( secs < 10 ) secs="0"+secs;
	if ( mins < 10 ) mins="0"+mins;
	
	s.innerHTML=secs;
	m.innerHTML=mins;
	h.innerHTML=hours;
}

function addTroops() {
	ges=Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	window.garr[1].each ( function(r) {
		box=$('g_'+r);
		if ( box ) {
			if ( ! box.checked ) return 1;
		}
		window.ttp.each(function(tt) {
			jsg=$('jsa_'+r+'_'+tt);
			if ( jsg ) {
				if ( ! ges[tt] ) ges[tt]=0;
				ges[tt]=ges[tt] + parseInt(jsg.value);
			}
		});
	});
	i=0;
	tges=0;
	ges.each(function(c) {
		m:
		cid=window.ttp[i];
		if ( ! cid ) return 1;
		i++;
		a=$('gf_'+cid);
		if ( a ) {
			a.value=c;
			tges+=c;
		}
	});
	if ( tges > 0 ) $('groups').innerHTML='<br><img src="/img/y.png"> Truppenwahl ok';
	else $('groups').innerHTML='<br><img src="/img/n.png"> Keine Truppen gew&auml;hlt';
	
	getMsAndDispTime();
}

function rmpc() {
	amount=$('am').value;
	cost=window.cper;
	tc=Number(cost*amount);
	$('cost').innerHTML='('+tc+' Rohstoffe)';
}

function getMsAndDispTime() {
	mms=0;
	window.garr[1].each ( function(r) {
		box=$('g_'+r);
		if ( box ) {
			if ( ! box.checked ) return 1;
		}
		cms=$('ms_'+r);
		if ( cms ) {
			if ( cms.value > mms ) mms=cms.value;
		}
	});
		
	xa=$('hx').value;
	ya=$('hy').value;
	xb=$('x').value;
	yb=$('y').value;
	
	r=Math.random();
	
	new Ajax.Request('/ajax/getTime.php?ms='+mms+'&x1='+xa+'&y1='+ya+'&x2='+xb+'&y2='+yb+'&id='+r, {
		onSuccess: function(transport) {
			$('ltime').innerHTML=transport.responseText;
			countUp('bcdt'+r);
		}
	});
	
// 	$('ltime').innerHTML="Ankunft um <b>"+h+":"+m+":"+s+"</b> Uhr";
}

function betreffaendern() {
          if(document.getElementById('betreff').value == '(kein Betreff)')
          {
            document.getElementById('betreff').value = '';
          }
	}
	
function toggleVisibility(eid) {
	o=document.getElementById(eid);
// 	collapseShadbox(o);
	if ( o.style.display == 'none' ) o.style.display='block';
	else o.style.display='none';
	saveSidebarCollapseStatus();
}

function toggleArrowSource(id) {
	c=document.getElementById(id);
	if ( c.src.search('a-down.gif') != -1 ) c.src='/img/a-right.gif';
	else c.src='/img/a-down.gif';
}

function glogin() {
	$('luser').value='Gast';
	$('lpass').value='guest';
	document.loginform.submit();
}

function sbar(dur,w,eid,rtf) {
	h=Number($(rtf+"h").value)*3600;
	s=Number($(rtf+"s").value)-1;
	m=Number($(rtf+"m").value)*60;
	
	secl=h+m+s;

	p=secl/dur;
	if ( secl == 0 ) p=0;
	cw=w-p*w;
// 	alert(h+","+m+","+s);
	
// 	alert(m+","+s);
	
	$(eid).style.width=Math.round(cw)+"px";
	
// 	alert(Math.round(cw)+"px, "+p);
	
	setTimeout("sbar('"+dur+"','"+w+"','"+eid+"','"+rtf+"')",1000);
}

function initializeSidebarCollapseStatus() {
	c = document.cookie.split('scs=');
	if ( ! c[1] ) return 0;
//	alert('1::'+c[1]);
	c = c[1];
	c = c.split('||');
	c = c[1];
	c = c.split('*');
	
	var boxes=new Array('allgemein','comm','trp','sys','other','isl');
	var arrows=new Array('a','b','c','d','f','e');
	
	i=0;
	c.each (function(s) {
		if ( ! boxes[i] ) return 0;
//		alert('['+i+']'+boxes[i]+' : '+s);
		obj=document.getElementById(boxes[i]);
		if ( ! obj ) return 1;
		else obj=obj.style;
		obj.display=s;
		if ( s == 'none' ) toggleArrowSource(arrows[i]);
		i+=1;
	});	
}

function utf8_decode ( str_data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var tmp_arr = [], i = ac = c = c1 = c2 = 0;
 
    while ( i < str_data.length ) {
        c = str_data.charCodeAt(i);
        if (c < 128) {
            tmp_arr[ac++] = String.fromCharCode(c); 
            i++;
        } else if ((c > 191) && (c < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    
    return tmp_arr.join('');
}

function saveSidebarCollapseStatus() {
	var a = new Date();
	a = new Date(a.getTime() +1000*60*60*24*365);
	
	trpStyle=document.getElementById('trp');
	if ( ! trpStyle ) trpStyle='block';
	else trpStyle=trpStyle.style.display;
	
	sysStyle=document.getElementById('trp');
	if ( ! sysStyle ) sysStyle='block';
	else sysStyle=sysStyle.style.display;
	
	document.cookie = 'scs=||'+document.getElementById('allgemein').style.display+'*'+document.getElementById('comm').style.display+'*'+trpStyle+'*'+sysStyle+'*'+document.getElementById('other').style.display+'*'+document.getElementById('isl').style.display+'||;'+'expires='+a.toGMTString()+';';
	
//	alert(document.cookie);
}

function open_help(id) {
	$('helptext').innerHTML='Lade Informationen vom Server, bitte warten...';
	$('outer_helpbox').style.display='block';
	new Ajax.Request('/ajax/help.php?id='+id, {
		onSuccess: function(transport) {
			$('helptext').innerHTML=utf8_decode(transport.responseText);
		}
	});
}

function open_url(url) {
	$('helptext').innerHTML='Lade Informationen vom Server, bitte warten...';
	$('outer_helpbox').style.display='block';
	new Ajax.Request(url, {
		onSuccess: function(transport) {
			$('helptext').innerHTML=utf8_decode(transport.responseText);
		}
	});
}

function close_help() {
	$('outer_helpbox').style.display='none';
}


function countUp(n) {
	
// 	if ( ! $(n) ) return 0;
	
	stunden=Number(document.getElementById(n+"h").value);
	minuten=Number(document.getElementById(n+"m").value);
	sekunden=Number(document.getElementById(n+"s").value);
	
	sekunden=sekunden+1;
	
	if ( sekunden > 59 ) {
		sekunden=00;
		minuten=minuten+1;
	}
	
	if ( minuten > 59 ) {
		minuten=00;
		stunden=stunden+1;
	}
	
	if ( sekunden < 10 ) {
		sekunden="0"+sekunden;
	}
	
	if ( minuten < 10 ) {
		minuten="0"+minuten;
	}
	
	if ( stunden < 10 ) {
		stunden="0"+stunden;
	}
	
	document.getElementById(n+"h").value=stunden;
	document.getElementById(n+"m").value=minuten;
	document.getElementById(n+"s").value=sekunden;
	
	document.getElementById(n).innerHTML=stunden+":"+minuten+":"+sekunden;
	
	setTimeout("countUp('"+n+"')",1000);
}

function timer(n,w) {
	
	$(n).style.padding='2px';
	$(n).style.paddingLeft='3px';
	$(n).style.paddingRight='3px';
	
	if ( ! w ) w='i';
	
	stunden=Number(document.getElementById(n+"h").value);
	minuten=Number(document.getElementById(n+"m").value);
	sekunden=Number(document.getElementById(n+"s").value);
	
	sekunden=sekunden-1;
	
	skip="false";
	
	if ( stunden <= 0 && minuten <= 0 && sekunden <= 0 ) {
		if ( w == 'i' ) document.getElementById(n).value='00:00:00';
		else document.getElementById(n).innerHTML='00:00:00';
		if ( ! window.donreload ) setTimeout("location.reload()",1700);
		if ( w == 'i' ) setTimeout("document.getElementById('"+n+"').value='00:00:00'",1700);
		else setTimeout("document.getElementById('"+n+"').innerHTML='00:00:00'",1700);
		return 1;
		skip="true";
	}
	
	if ( skip != "true" ) {
	
		if ( sekunden < 0 ) {
			sekunden=59;
			minuten=minuten-1;
		}
		
		if ( minuten < 0 ) {
			minuten=59;
			stunden=stunden-1;
		}
		
		if ( sekunden < 10 ) {
			sekunden="0"+sekunden;
		}
		
		if ( minuten < 10 ) {
			minuten="0"+minuten;
		}
		
		if ( stunden < 10 ) {
			stunden="0"+stunden;
		}
		
		if ( sekunden == 10 && minuten == 0 && stunden == 0 ) {
			if ( n.search('btimer') == -1 ) {
				for ( i=0; i<255; i++ ) {
					v=255-i;
					if ( v < 0 ) v=0;
					setTimeout('$(\''+n+'\').style.backgroundColor=\'rgb('+v+','+v+','+v+')\'',39*i);
					
					v=i;
					g=Math.round(v/2);
					
					if ( v > 255 ) v=255;
					setTimeout('$(\''+n+'\').style.color=\'rgb('+v+','+g+','+0+')\'',39*i);
				}
			}
		}
		
		document.getElementById(n+"h").value=stunden;
		document.getElementById(n+"m").value=minuten;
		document.getElementById(n+"s").value=sekunden;
		
		if ( w == 'i' ) document.getElementById(n).value=stunden+":"+minuten+":"+sekunden;
		else document.getElementById(n).innerHTML=stunden+":"+minuten+":"+sekunden;
			
	}


	
	setTimeout("timer('"+n+"','"+w+"')",1000);
}

function checkDCoordinates() {
	if ( ! $('x').value || ! $('y').value ) {
		document.getElementById('imsg').innerHTML='<img src="/img/n.png"> Koordinaten unvollst&auml;ndig...';
		document.getElementById('coord').innerHTML='<img src="/img/n.png"> Koordinaten unvollst&auml;ndig...';
		return 0;
	}
	setTimeout('checkCoordinates()',500);
}

function checkCoordinates() {
	
	new Ajax.Request('/ajax/checkCoord.php?x='+$('x').value+'&y='+$('y').value, {
		onSuccess: function(transport) {
			document.getElementById('imsg').innerHTML=transport.responseText;
			document.getElementById('coord').innerHTML=transport.responseText;
		}
	});
	
}

function checkTradeCoords(way) {
	new Ajax.Request('/ajax/checkCoord.php?ctime=1&way='+way+'&x='+$('x').value+'&y='+$('y').value, {
		onSuccess: function(transport) {
			document.getElementById('imsg').innerHTML=transport.responseText;
		}
	});
}

function checkDTradeCoordinates(way) {
	if ( ! $('x').value || ! $('y').value ) {
		document.getElementById('imsg').innerHTML='<img src="/img/n.png"> Koordinaten unvollst&auml;ndig...';
		return 0;
	}
	$('imsg').innerHTML='Laden...<br>';
	setTimeout('checkTradeCoords("'+way+'")',500);
}

function checkTS() {
	if ( ! $('off').checked && ! $('deff').checked && ! $('merge').checked ) document.getElementById('type').innerHTML='<br><img src="/img/n.png"> Kein Truppenbewegungs-Typ gew&auml;hlt';
	else if ( $('off').checked ) $('type').innerHTML='<br><img src="/img/y.png"> Gew&auml;hlte Siedlung wird angegriffen';
	else if ( $('deff').checked ) $('type').innerHTML='<br><img src="/img/y.png"> Gew&auml;hlte Siedlung wird unterst&uuml;tzt';
	else if ( $('merge').checked ) $('type').innerHTML='<br><img src="/img/y.png"> Truppen werden in die gew&auml;hlte Siedlung verschoben';

	setTimeout('checkTS()',300);
}

function getGroups() {
	groups=Array(0);
	str='';
// 	alert($('trselect').length);
	for ( var i=0; i < $('trselect').length; ++i ) {
		if ( $('trselect').elements[i].type == "checkbox" ) {
			e=$('trselect').elements[i];
			if ( e.value == 'on' ) {
				groups.push(e.value);
				str=str+(e.name);
			}
		}
	}

	setTimeout('getGroups()',2000);
}

function enableSend() {
	a=false;
	b=false;
	c=false;
	
	if ( $('coord').innerHTML.search('y.png') > 0 ) a=true;
	if ( $('type').innerHTML.search('y.png') > 0 ) b=true;
	if ( $('groups').innerHTML.search('y.png') > 0 ) c=true;
	
	if ( a && b && c ) {
		$('send').style.display='inline';
	}
	else $('send').style.display='none';
	setTimeout('enableSend()',300);
}

function showGroup(key,me) {
	document.getElementById('g_'+key).style.display='inline';
	document.getElementById('gd_'+key).style.display='none';
	me.style.cursor='default';
	if ( window.cedgid && window.cedgid != key ){
		if ( window.cgch == true ) {
			sendRequest(window.cedgid);
			window.cgch=false;
		}
		else hideGroup(window.cedgid,me);
	}
	window.cedgid=key;
}

function hideGroup(key,me) {
	document.getElementById('g_'+key).style.display='none';
	document.getElementById('gd_'+key).style.display='inline';
	me.style.cursor='pointer';
}

function setState(obj,state) {
	if ( window.stopSpin != 1 ) {
		obj=$(obj);
		obj.value=state;
	}
}

function enableSpin(obj) {
	window.stopSpin=0;
	spin(obj);
}

function spin(obj) {
	obj=$(obj);
	obj.style.fontFamily='monospace';
	obj.style.width='20px';
	obj.style.backgroundImage='none';
	obj.value='/';
	obj.blur();
	setTimeout("setState('"+obj.id+"','-')",50);
	setTimeout("setState('"+obj.id+"','\')",100);
	setTimeout("setState('"+obj.id+"','|')",150);
	setTimeout("if ( ! window.stopSpin ) spin('"+obj.id+"')",200);
}

function disableSpin(obj) {
	window.stopSpin=1;
	obj.backgroundImage='url(/img/save.png)';
	obj.value="";
}

function sendRequest(gid) {
	enableSpin($('save_'+gid));
	try {
		fields=Array();
		objects=document.getElementsByName('of_'+gid);
		
		for ( var i=0; i<objects.length; i++ ) {
			fields.push(objects[i]);
		}
		
		uid=Array();
		
		string='/ajax/updGrp.php?gid='+gid;
		
		fields.each(function(c) {
	// 		alert(c.value);
	// 		if ( ! c.value ) return 0;
			s=c.id.replace('of_','');
			arr=s.split('_');
			
			if ( arr[0] ) string=string+'&ct_'+arr[0]+'='+c.value;
			
			uid[arr[0]]=c.value;
		});
		
// 		alert(string);
		
		new Ajax.Request(string, {
			onSuccess: function(transport) {
				if ( ! transport.responseText.search('<!-- success -->') ) {
					$('outline_'+gid).innerHTML=transport.responseText;
					document.getElementById('save_'+gid).type='button';
					disableSpin($('save_'+gid));
				}
				else {
					disableSpin($('save_'+gid));
					$('outer_helpbox').style.display='block';
					$('helptext').innerHTML=transport.responseText;
				}
			}
		});
		
	}
	catch (err) {
		document.getElementById('submitForm_'+gid).submit();
	}
}

function updF(id,me) {
	t=document.getElementById(id);
	s=document.getElementById(me);
	
	if ( ! window.maingrpCache ) window.maingrpCache=Array();
	
	if ( ! window.maingrpCache[id] ) {
		 window.maingrpCache[id]=t.value;
	}
	
	if ( ! window.subgrpCache ) window.subgrpCache=Array();
	
	if ( ! window.subgrpCache[id] ) {
		window.subgrpCache[id]=s.value;
	}
	
	max=Number(window.maingrpCache[id]) + Number(window.subgrpCache[id])+1;
	nval=Number(window.maingrpCache[id]) + (Number(window.subgrpCache[id])-Number(s.value));
	if ( nval >= 0 ) t.value=nval;
	else s.value=Number(window.maingrpCache[id]) + Number(window.subgrpCache[id]);
	
	window.cUpdMax=max;
}

// Eingabefelder

function start(id,step,min,max) {
	window.objCache=Array();
	window.cUpdElement=id;
	window.cUpdStep=step;
	if ( min ) window.cUpdMin=min;
	if ( max ) window.cUpdMax=max;
	document.getElementById(id).className='ddfield_ac';
	window.restoreOnmousemove=document.onmousemove;
	document.onmousemove=gPos;
}

function stop() {
// 	alert("stop");
	if ( window.restoreOnmousemove ) document.onmousemove=window.restoreOnmousemove;
	else if ( window.cUpdElement ) document.onmousemove='';
	window.prevx='';
	
	if ( window.cUpdElement ) {
		a=document.getElementById(window.cUpdElement);
		document.getElementById(window.cUpdElement).className='ddfield';
	}
	
	if ( window.maingrpCache ) window.maingrpCache=Array();
	if ( window.subgrpCache ) window.subgrpCache=Array();
	window.cUpdElement='';
}

function gPos(e) {
	a=document.getElementById(window.cUpdElement);
	
	if ( a ) {
		a.blur();
		a.focus();
		a.blur();
	}
	
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
	}
	
	if ( window.prevx ) {
		val=Number(a.value);
		change=( posx - window.prevx ) * window.cUpdStep;
		if ( e.shiftKey == 1 ) change=change / 10;
		if ( e.ctrlKey == 1 ) change=change * 10;
		newv=val + change;
		if ( ! window.objCache[window.cUpdElement] ) window.objCache[window.cUpdElement]=val;
		window.objCache[window.cUpdElement]=window.objCache[window.cUpdElement]+change;
		
		rnewv=Math.round(window.objCache[window.cUpdElement]);
		
		window.objCache[window.cUpdElement]=Math.round(window.objCache[window.cUpdElement]*10)/10;
		
		if ( window.objCache[window.cUpdElement] == rnewv || window.objCache[window.cUpdElement] +1 < val || window.objCache[window.cUpdElement] -1 > val ) a.value=rnewv;
		
		if ( window.cUpdMax ) {
			if ( window.cUpdMax < a.value ) a.value=window.cUpdMax - 1;
		}
		if ( window.cUpdMin ) {
			if ( window.cUpdMin - 1 > a.value ) a.value=window.cUpdMin - 1;
		}
		
		if ( window.cUpdMax && window.cUpdMin && a.style.width ) {
			v=a.value;
			w=Number(a.style.width.replace('px',''));
			
			r=(v - window.cUpdMin) / (window.cUpdMax - 1 - window.cUpdMin);
			pwidth=r*w;
			
			set=Math.round(-800 + pwidth);
			
			a.style.backgroundPosition=set +'px';
		}
		
// 		document.getElementById('a').value=rnewv;
// 		document.getElementById('b').value=window.objCache[window.cUpdElement];
	}
	window.prevx=posx;
	
	if ( a ) {
		a.blur();
		a.focus();
		a.blur();
	}
}

// Link-Effekte

function setupFieldsForCMenu() {
	la=$('body').getElementsByTagName("*");
	for ( i=0; i < la.length; i++ ) {
		if ( la[i].tagName == 'TEXTAREA' || la[i].tagName == 'INPUT' ) {
			
			t=la[i];
			
			if ( t.onblur || t.onfocus ) continue;
			
			t.hasFocus=false;
		
			t.onfocus=function(){ this.hasFocus=true; };
			t.onblur =function(){ this.hasFocus=false; };
		}
	}
}

function setupEffects() {
	
	if ( ! window.specialScript ) {
	
		la=$('body').getElementsByTagName("*");
		for ( i=0; i < la.length; i++ ) {
			
			if ( la[i].tagName == 'A' && ! la[i].onmouseover && ! la[i].onmouseout ) {
				
				if ( la[i].parentNode.parentNode.id == 'ress_links' || la[i].parentNode.className == 'pagenav' || ( la[i].innerHTML.search('<img') != -1 && la[i].innerHTML.search('>') == Number(la[i].innerHTML.length)-1 ) ) continue;
				
				if ( ! la[i].id ) la[i].id='a_item_'+i;
				
				if ( la[i].parentNode.parentNode.parentNode.id == 'isl' ) {
					la[i].onmouseover=function(event) { islFade(this.id); };
					la[i].onmouseout=function(event) { islFadeout(this.id); };
				}
				else if ( ! la[i].className && ! la[i].style.color ) {
					la[i].onmouseover=function(event) { liFade(this.id); };
					la[i].onmouseout=function(event) { liFadeout(this.id); };
					
					try {
						liFadeInit(la[i]);
					}
					catch(err) { }
				}
				
				if ( ! la[i].prevCol ) la[i].prevCol=la[i].style.color;
				if ( ! la[i].prevBorder ) la[i].prevBorder=la[i].style.borderBottom;
				la[i].pleft=la[i].style.paddingLeft.replace('px','');
			}
		}
		
	}
	
}

function login_user_focus() {
	obj=$('luser');
	if ( obj.value == 'Benutzername' ) obj.value='';
}

function login_user_blur() {
	obj=$('luser');
	if ( obj.value == '' ) obj.value='Benutzername';
}

function login_pass_focus() {
	obj=$('lpass');
	if ( obj.value == 'Passwort' ) obj.value='';
}

function login_pass_blur() {
	obj=$('lpass');
	if ( obj.value == '' ) obj.value='Passwort';
}



