function dezRound(num,pos){
  if(pos<0){
    pos=Math.pow(10,Math.abs(pos));
    return Math.round(num/pos)*pos;
  }
  else if(pos>0){
    pos=Math.pow(10,pos);
    return (Math.round(num*pos)/pos);
  }
  else{
    return Math.round(num);
  }
}

function Swap(span) {
    displayType = ( document.getElementById( span ).style.display == 'none' ) ? '' : 'none';
    document.getElementById( span ).style.display = displayType;
}

function swapShow(span) {
    document.getElementById( span ).style.display = '';
}

function swapHide(span) {
    document.getElementById( span ).style.display = 'none';
}

var cX = 0;
var cY = 0;
var rX = 0;
var rY = 0;

function UpdateCursorPosition(e){
  cX = e.pageX;
  cY = e.pageY;
}

function UpdateCursorPositionDocAll(e){
  cX = event.clientX;
  cY = event.clientY;
}

if(document.all){
  document.onmousemove = UpdateCursorPositionDocAll;
}
else{
  document.onmousemove = UpdateCursorPosition;
}

function AssignPosition(d){
  if(self.pageYOffset){
    rX = self.pageXOffset;
    rY = self.pageYOffset;
  }
  else if(document.documentElement && document.documentElement.scrollTop){
    rX = document.documentElement.scrollLeft;
    rY = document.documentElement.scrollTop;
  }
  else if(document.body){
    rX = document.body.scrollLeft;
    rY = document.body.scrollTop;
  }
  if(document.all){
    cX += rX; 
    cY += rY;
  }
  d.style.left = (cX+10-((document.body.clientWidth - 900) /2)) + "px";
  d.style.top  = (cY+10) + "px";
}

function hideContent(d) {
  if(d.length >= 1){
    document.getElementById(d).style.display = "none";
  }
  return;
}

function showContent(d){
  if(d.length >= 1){
    var dd = document.getElementById(d);

    AssignPosition(dd);
    dd.style.zIndex="99";
    dd.style.display = "block";
  }
  return;
}

function redirect(url) {
	window.setTimeout("window.location.href = '" + url + "';", 3000);
}

function abortConstruction(id){
  self.location.href="game.php?section=construction&action=AbortConstruct&id=" + id;
  document.getElementById("CountDown").disabled = true;
}

function abortResearch(id){
  self.location.href="game.php?section=technologies&action=Abort&id=" + id;
  document.getElementById("CountDown").disabled = true;
}

function CountDown(cid) {
	if (cid){
		counter[cid+""] --;

		if(counter[cid+""] < 0) {
			self.location.href = self.location.href;
		}
		h = Math.floor(counter[cid+""] / 3600);
		i = Math.floor(counter[cid+""] / 60 % 60);
		s = counter[cid+""] % 60;

		h = h < 10 ? '0' + h : h;
		i = i < 10 ? '0' + i : i;
		s = s < 10 ? '0' + s : s;

		if(counter[cid+""] <= 0) {
			h = '00';
			i = '00';
			s = '00';
		}
		insert = h + ':' + i + ':' + s;
		document.getElementById ("CountDown"+cid).firstChild.nodeValue = insert;
		setTimeout('CountDown('+cid+')', 1000);
	}
	else{
		counter --;

		if(counter < 0) {
			self.location.href = self.location.href;
		}
		h = Math.floor(counter / 3600);
		i = Math.floor(counter / 60 % 60);
		s = counter % 60;

		h = h < 10 ? '0' + h : h;
		i = i < 10 ? '0' + i : i;
		s = s < 10 ? '0' + s : s;

		if(counter <= 0) {
			h = '00';
			i = '00';
			s = '00';
		}
		insert = h + ':' + i + ':' + s + " - Abbrechen";
		document.getElementById("CountDown").value = insert;
		setTimeout('CountDown()', 1000);
	}

}

function CheckAll(Name) {
	var elements = document.getElementsByName(Name);
	for(i = 0; i < elements.length; i ++) {
		elements[i].checked = elements[i].checked == true ? false : true;
	}
}

function MakeProgress(Now, Base, MPID) {
	Now += 0.5;
	Percent = Now * 100 / Base;
	
	if(Percent > 0) {
		Done = Percent;
		Left = 100 - Percent;
	} else {
		Done = 100;
		Left =   0;
	}
	
	D = dezRound(Done, 3);
	L = dezRound(Left, 3);
	
	if(D > 99) {
		D = 100;
		L =   0;
	}
	
	insert = document.createTextNode(Math.floor(D));
	document.getElementById('Percent' + MPID).replaceChild(insert, document.getElementById('Percent' + MPID).firstChild);
	
	document.getElementById('Done' + MPID).style.width = D + '%';
	document.getElementById('Left' + MPID).style.width = L + '%';
	
	var Do = 'MakeProgress(' + Now + ', ' + Base + ', ' + MPID + ')';
	setTimeout(Do, 500);
}
function Check() {
	var Check = window.confirm('Wollen Sie wirklich abbrechen?');
	return (Check);
}
function CheckDelete() {
	var Check = window.confirm('Wollen Sie wirklich loeschen?');
	return (Check);
}
function CheckKick() {
	var Check = window.confirm('Wollen Sie das Mitglied wirklich ausschliessen?');
	return (Check);
}

