function aftellen(secs, spanid, disp, reload) {
	span = document.getElementById(spanid);
	
	if (disp == true) {
		hours = parseInt(secs / 3600);
		mins = parseInt((secs-(hours*3600)) / 60);
		secs = (secs % 60);
	} else {
		mins = 0;
		hours = 0;
	}
	doortellen(hours, mins, secs, span, disp, reload);
}

function borg(borg, af, spanid) {
	bspan = document.getElementById(spanid);
	borgaf(borg, af, bspan);
}

function borgaf(borg, af, bspan) {
	stop = false;
	borg = borg-af;
	secs = borg/af;
	if (secs == 0) {
		stop = true;
	}
	bspan.innerHTML = number_format(borg, 0, ',', '.');
	if (stop == false) {
		setTimeout(function(){borgaf(borg, af, bspan)}, 1000);
	}
}

function doortellen(hours, mins, secs, span, disp, reload) {
	stop = false;
	secs--;
	
	if(disp == false){
		if(secs <= 0){
			stop = true;
		}
	} else {
		if(secs == 0 && mins == 0 && hours == 0) {
			stop = true;
		}
	}
	
	if(disp == true) {
		if(secs < 0) {
			mins--;
			secs = 59;
		}
		if(mins < 0) {
			hours--;
			mins = 59;
		}
	}
	if(hours < 10) { hoursextra = '0';} else{hoursextra = '';} if(mins < 10) { minsextra = '0';} else{minsextra = '';}
 if(secs < 10) { secsextra = '0';} else{secsextra = '';}	if(disp == true) {
		span.innerHTML = hoursextra + hours + ':' +minsextra + mins + (mins!=1?':':':') + secsextra + secs + (secs!=1?' ':' ');
	} else {
		span.innerHTML = secs + (secs!=1?' ':' ');
	}

	if(stop == false) {
		setTimeout(function(){doortellen(hours, mins, secs, span, disp, reload)}, 1000);
	}
	
	if(stop == true && reload == true) {
	   window.location.reload(true);
	}
	
}


function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}
