var url = window.location.href,
postcode = '',
lokaalpin='3073PP',
H,W, // inner hoogte en breedte
wednr = 0,
rows,
screenfactor = 1,
clid = '0',
partij,
k,
setcount = [0,0],
p = ['',''], // plaats in team
tmc = [0,0,0,0], // te maken driebanden divisie en topteam
gifW = [29,44,61,103,90,114,94,30,51,51,46,87,28,48,30,65,88,62,82,87,82,88,89,64,88,89,33,33,87,87,87,86,127,83,91,91,91,68,65,90,91,48,54,88,62,118,89,90,82,90,88,85,76,90,86,134,79,78,65,46,65,46,79,91,55,83,85,81,85,84,47,85,86,44,45,78,44,126,86,84,85,85,59,77,50,86,72,110,71,74,58,61,44,61,86,123,87,123,28,73,57,91,89,89,55,168,85,32,113,123,65,123,123,28,28,57,57,57,82,164,55,128,77,32,125,123,58,78,29,44,85,88,90,78,44,79,55,129,53,61,87,48,129,91,57,87,52,53,55,77,94,55,55,40,54,61,102,106,113,86,83,83,83,83,83,83,117,91,68,68,68,68,48,48,48,48,92,89,90,90,90,90,90,87,90,90,90,90,90,78,82,90,83,83,83,83,83,83,124,81,84,84,84,84,44,44,44,44,84,86,84,84,84,84,84,87,84,86,86,86,86,74,85,74], // gif width
SB=[],SBsave=[],huisnamen=[],idle=[],teamnr=[],code=[],l=[],B=[],clock=[],postcodes=[];

B.local=false;B.OnlyLive=false;B.isTel=false;B.NoInput=false;B.TestMode=false;B.Beker=false;B.offline=false;B.SetMode=false;B.mobile=false;
SB.t0='T H U I S';SB.t1='G A S T E N';SB.s0='';SB.s1='';SB.c0='';SB.c1='';SB.m0='';SB.m1='';SB.h0='';SB.h1='';SB.b0='';SB.b1='';SB.x0='';SB.x1='';
SB.tafelnr='';SB.ip='';SB.BarTimer=-1;SB.decimals=3;SB.ImgBase=0;SB.buf='';SB.shrink=0;SB.focus=0;SB.prefix='';SB.keylog=''; 
SBsave.t0='';SBsave.t1='';SBsave.s0='';SBsave.s1='';SBsave.c0='';SBsave.c1='';SBsave.m0='';SBsave.m1='';SBsave.h0='';SBsave.h1='';SBsave.b0='';SBsave.b1='';SBsave.x0='';SBsave.x1='';
l[0] = []; // tellijst boven
l[1] = []; // tellijst onder 
idle.Timer=-1;idle.Type='R';idle.Time = 360000;idle.TimeSave=0;idle.Running = false;idle.Images=[];idle.Idx=0;idle.Enabled=true;
code.team0 = '0';code.team1 = '0';code.bond0 = '0';code.bond1 = '0';code.pinodd0=lokaalpin;code.pinodd1=lokaalpin;code.pineven0=lokaalpin;code.pineven1=lokaalpin; 
clock.Timer=-1;clock.Time=0;clock.Secs=0;clock.Stopped=false;
SB.versie = "11.02.14";

function setCookie(c_name,value,expiredays) {

	var exdate=new window.Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +window.escape(value)+((expiredays===null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name) {

	var c_start,c_end,cookie,arr;
	
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) {
				c_end=document.cookie.length;
			}
			cookie = window.unescape(document.cookie.substring(c_start,c_end));
			if(cookie.indexOf('#') >=0) {
				arr = cookie.split('#');
				SB.tafelnr = arr[0];
				SB.ip = arr[1];
			}
		}
	}
	return "";
}
function hide(id,hideme) {

	id.style.visibility = (hideme) ? 'hidden' : 'visible';	
}
function GetXmlHttpObject() {

	var i = (window.XMLHttpRequest) ? new window.XMLHttpRequest() : new window.ActiveXObject("Microsoft.XMLHTTP");return i;

} 

function Ajax(page, sync) {

	var xmlhttp =  GetXmlHttpObject(),arr=[],arr2=[],L,i;
	if(page.indexOf('?') > 0) {
		xmlhttp.open('GET', page+'&rnd=' + new window.Date().getTime(), sync);		
	}
	else {
		xmlhttp.open('GET', page+'?rnd=' + new window.Date().getTime(), sync);		
	}
	xmlhttp.send(null);
	if(page.indexOf('sblicentie') >=0) {
		if(xmlhttp.responseText.length > 0) {					
			arr = xmlhttp.responseText.split(';');  
			SB.ip = arr[0]; 
			postcode = (arr.length > 1) ? arr[1] : '';
			if(arr.length>2) {
				idle.Type = arr[2];
				screenfactor = arr[3];
				SB.tafelnr=arr[4];
				setCookie('SB',SB.tafelnr+'#'+SB.ip,365);
			}
			else {
				if(H == window.screen.height) { // full screen
					//window.alert('vraag tafel');
					// vraag tafelnummer
				}
			}				
		}	
	}
	else if(page.indexOf('sbhuisnamen') >=0) {
		if(xmlhttp.responseText.length > 0) {					
			arr = xmlhttp.responseText.split('@');  
			L = arr.length;
			for(i=0;i<L;i++) {
				arr2=arr[i].split('#');
				huisnamen[arr2[0]]=arr2[1];
			}
		}	
		Ajax('sbpostcodes.php',false);
	}
	else if(page.indexOf('sbpostcodes') >=0) {
		if(xmlhttp.responseText.length > 0) {
			arr = xmlhttp.responseText.split('@');  
			L = arr.length;
			for(i=0;i<L;i++) {
				postcodes[i]=arr[i];
			}
			Ajax('sbpincodes.php?postcode='+postcode,false);
		}
	}
	else if(page.indexOf('sbpincodes') >=0) {
		if(xmlhttp.responseText.length > 0) {
			lokaalpin = xmlhttp.responseText;
			code.pinodd0 = lokaalpin;
			code.pinodd1= lokaalpin;
			code.pineven0 = lokaalpin;
			code.pineven1= lokaalpin;
			//dg('foto1').src = 'http://www.biljartdivisies.eu/spelersfotos/'+lokaalpin+'.png';
		}
	}
	else if(page.indexOf('sbnieuwip') >=0) {
		if(xmlhttp.responseText.length > 0) {
			if(xmlhttp.responseText.indexOf('.') >= 0) { 
				window.location.replace(url);
			}
			else {
				ShowBox(xmlhttp.responseText,5000);
			}
		}
	}
}

function demo() {

	if(postcode.length>1) {
		return false;
	}
	else {
		return true;
	}
}

function postcodekort() {

	return postcode.substr(0,4)+postcode.substr(5,2);
}

function dg(id) {

	return document.getElementById(id);
}
function isInteger(value) {

	var isOk = '0123456789.',i;
	for(i=0;i<=isOk.length;i++) {
		if(value == isOk.substr(i,1)) {
			return true;
		}
	}
	return false;
}

function HideBox() {
	
	SB.Bartimer = window.clearTimeout(SB.Bartimer);
	dg('TextBox').innerHTML = '';
	B.NoInput = false;
	dg('OuterBox').style.display='none';
	dg('fade').style.display='none';
	idleSet(true);

}
function LoadBalls() {

	var i=0,ii,i1=0,i2=1;
	if(B.offline) {
		i = 3;
	}
	else if(wednr !== 0) {
		i = 1;
	}
	else if(B.SetMode) {
		i = 4;
		ii = setcount[0]+setcount[1];
		if(ii%2) {
			i1 = 1;
			i2 = 0;
		}
		else {
			i1 = 0;
			i2 = 1;
		}
	}
	if(B.local) {
		dg('bal'+i1).src = "data:image/gif;base64," + window.localStorage.getItem('balwit' +i+ ".gif");
		dg('bal'+i2).src = "data:image/gif;base64," + window.localStorage.getItem('balgeel' +i+ ".gif");
	}
	else {
		dg('bal'+i1).src = 'images/balwit'+i+'.gif';
		dg('bal'+i2).src = 'images/balgeel'+i+'.gif';
	}
	
	if(B.OnlyLive) {
		dg('bal0').src = 'images/balwit.png';
		dg('bal1').src = 'images/balgeel.png';
	}
}
function font(id, DoClear, recalled, forced) {

	var id0 = id.substr(0,1),id1 = id.substr(1,1),i,dge,factorW,txt2,max,txt,path,path2,width,L2,left,keyname,keyname2,L;
	if(recalled || SB[id] != SBsave[id] || id0 == 'x') {
		txt=SB[id];
		L=txt.length;
		left = (id0 == 'x') ? 0 : SB.shrink*2;
		path='SBfont/';
		switch(id) {
			case 't0':case 's0':case 'c0':	
				path += 'BlackOnRed/';
				keyname = 'BlackOnRed';
				break;
			case 'b0':case 'm0':case 'h0':
				path2 = path + 'YellowOnRed/';
				keyname2 = 'YellowOnRed';
				path += 'WhiteOnRed/';
				keyname = 'WhiteOnRed';
				break;
			case 't1':case 's1':case 'c1':
				path += 'BlackOnBlue/';
				keyname = 'BlackOnBlue';				
				break;
			case 'm1':case 'h1':case 'b1':
				path2 = path + 'YellowOnBlue/';
				keyname2 = 'YellowOnBlue';				
				path += 'WhiteOnBlue/';
				keyname = 'WhiteOnBlue';				
				break;
			case 'x0':case 'x1':
				path += 'BlackOnWhite/';
				keyname = 'BlackOnWhite';	
				break;
			default:
				break;				
		}
		
		switch(id0) {
			case 't':case 's':
				factorW = SB.ImgBase * screenfactor * 1.1;
				txt = txt.replace("`","'");
				max=25;
				break;
			case 'h':case 'b':
				factorW = SB.ImgBase * 0.85 * screenfactor;
				max=3;
				break;
			case 'm':
				factorW = SB.ImgBase * 0.85 * screenfactor;
				max=10;
				break;
			case 'c':
				factorW = SB.ImgBase * 1.1 * screenfactor;
				max=3;
				break;
			case 'x':
				factorW = SB.ImgBase * 1.2 * screenfactor;
				max = 7;					
				break;
			default:
				break;
		}

//CAR EN BRT RECHTS UITLIJNEN
		if(id0 == 'c' || id0 == 'b') {
			for (i=0;i<L;i++) {
				left += window.parseInt(gifW[txt.charCodeAt(i)-32] * factorW,10);
			}
			left = W - left - 5;
		}
		
//HS IN HET MIDDEN		
		else if(id0 == 'h') {
			left=0;
			for (i=0;i<L;i++) {
				left += window.parseInt(gifW[txt.charCodeAt(i)-32] * factorW,10);
			}
			left = window.parseInt((W-(SB.shrink*2))/2,10) - window.parseInt(left/2,10) ;	
		}

		if(id0 == 'm' && B.SetMode) {
			txt = setcount[id1].toString() + '  ' + txt;
			txt = txt.substr(0,10);
			L=txt.length;
		}
//SCHOONPOETSEN SPELER TEAM
		if (id0 == 's' ||  id0 == 't') {
			txt2='';
			for(i=0;i<max;i++) {
				txt2 += ' ';
			}
			L=txt2.length;
			for (i=0;i<L;i++) {	
				dg(id+i).src = 'images/empty.gif';		
			}
		}
		else {
			for(i=L;i<max;i++) {
				txt += ' ';
			}
		}
		L=txt.length;
		while(1) {
			for (i=0;i<L;i++) {	
				dge=dg(id+i);		
				if ((id0 == 's' ||  id0 == 't') && screenfactor != 1) {				
					dge.style.left=-50;
				}
				if ((id0 == 's' ||  id0 == 't') && isInteger(txt.substr(i,1))) {
					if(B.local) {
						dge.src = "data:image/png;base64," + window.localStorage.getItem(keyname + txt.charCodeAt(i) + "n.png");
					}
					else {
						dge.src = path + txt.charCodeAt(i) + "n.png";
					}
				}
				else {
					if(B.local) {
						dge.src = (B.SetMode && i===0 && id0 == 'm') ? "data:image/png;base64," + window.localStorage.getItem(keyname2 + txt.charCodeAt(i) + ".png") : "data:image/png;base64," + window.localStorage.getItem(keyname + txt.charCodeAt(i) + ".png");
					}
					else {
						dge.src = (B.SetMode && i===0 && id0 == 'm') ? path2 + txt.charCodeAt(i) + ".png" : path + txt.charCodeAt(i) + ".png";
					}
				}
				if(id0 == 'c' || id0 == 'b' || id0 == 'h') {
					dge.style.left=left.toString();							
				}

				dge.width=window.parseInt(gifW[txt.charCodeAt(i)-32] * factorW,10);	
				

				if(id0 == 'x') {
					hide(dge, false); 
				}
				else {
					if(id0 == 'c' || id0 == 'b' || id0 == 'h') {
						left += window.parseInt(gifW[txt.charCodeAt(i)-32] * factorW,10);
					}
				}
			}
			if((id0 == 't' || id0 == 's') && (SB.b0.toString() == '0' || forced)) {		
				width = H*0.36; //start breedte is foto
				for(i=0;i<L;i++) { //team of speler breedte
					width += dg(id + i).width;
				}
				if(id0 == 't') { // car breedte bijtellen
					L2=SB['c'+id1].length;
					for(i=0;i<L2;i++) {
						width += dg('c'+id1+i).width;
					}
				}
				width += H/100;
				if(width > W-(SB.shrink*2)) {
					factorW -= 0.01;				
				}
				else {
					break;
				}
			} 
			else {
				break;
			}
		}
	}
	if(SB['c'+id1].length != SBsave['c'+id1].length) {
		SBsave[id] = SB[id]; 
		font('t'+id1,DoClear,true,true);
	}
	else {
		SBsave[id] = SB[id];
	}

	if(DoClear) { // Einde proces callasp
		LoadBalls();
		HideBox();
	}
	
} 


function S(id,val,DoClear) {

	SB[id] = val;
	if(arguments.length == 3) { // de laatste uit sb.asp
		font(id, DoClear);
	}
	else {
		font(id);
	}
}

function ScreenSize(DoDiv) {

	var i;
	//if(B.mobile) {
		//W = window.screen.width;
		//H = window.screen.height-20;
	//}
	//else {
		if(window.navigator.appName == "Microsoft Internet Explorer") {
			H = document.body.offsetHeight;
			W = document.body.offsetWidth;	
		}
		else {
			H = window.innerHeight;
			W = window.innerWidth;	
			if(DoDiv) {
				for(i=0;i<2;i++) {
					dg('divteam'+i).style.top = 0;
					dg('divteam'+i).style.left = H*0.27;
					dg('divspeler'+i).style.left = H*0.27;
				}
			}
		}
	//}

}
	
function isNumeric(value) {

	if (value === null || !value.toString().match(/^[-]?\d*\.?\d*$/)) { 
		return false;
	}
	else {
		return true;
	}
}
function tm(id) { 

	var temaken='',i;
	if(SB['s'+id] === '') {
		return 0;
	}
	else {
		for(i=0;isNumeric(SB['s'+id].substr(i,1));i++) {
			temaken += SB['s'+id].substr(i,1);
		}
		if(temaken==='') {
			return 0;
		}
		else {
			return window.parseInt(temaken,10);
		}		
	}
}
function CallAsp(StrPass) { 

    dg('asp').contentWindow.location.replace('sb.asp?action=' + StrPass);      
}
function idleFrame() {

//	if((idle.Type == 'R' && idleDivPoule.length > 0) || (idle.Type == 'I' && idle.Images.length > 0)) {
	if(idle.Enabled) {
		window.clearTimeout(idle);	
		if (idle.Type == 'R') { 
			idle.Running = true;
			dg('asp').contentWindow.location.replace('sbidle.asp?parent=ja&plusmin=ja&cellspacing=4&interval=20000&postcode=' + postcode);
		}
		else if (idle.Type == 'I') { 
			idle.Timer = window.setTimeout(function() {
				idleFrame(); 
			}, 15000);			
		
			idle.Running = true;
			idle.Idx++;
			if (idle.Idx == idle.Images.length) {
				idle.Idx = 1;
			}
			dg('asp').contentWindow.location.replace('sbimages.asp?width=' +W + '&height=' +H+ '&image=' + idle.Images[0] + idle.Images[idle.Idx]);
			hide(dg('hidden'),false);			
		}
		else if (idle.Type == 'f') { 
			idle.Running = true;
			dg('asp').contentWindow.location.replace('runflash.asp?width=' +window.parseInt(W,10) + '&height=' +window.parseInt(H,10) + '&postcode=' + postcodekort() + '&tafelnr=' + SB.tafelnr);	
			hide(dg('hidden'),false);
		}
		else if (idle.Type == 'F') { 
			idle.Running = true;
			dg('asp').contentWindow.location.replace('runflash.asp?width=' +window.parseInt(W,10) + '&height=' +window.parseInt(H,10) + '&postcode=' + postcodekort() + '&tafelnr=0');	
			hide(dg('hidden'),false);
		}		
	}
	else {
		idle.Timer = window.clearTimeout(idle.Timer);
	}
}

function idleSet(ClearTimer) {
	
	var i;
	document.body.focus();
	idle.Running = false;	
	if (ClearTimer) {
		idle.Timer = window.clearTimeout(idle.Timer);
	}
	if (tm(0) === 0 && tm(1) === 0) {
		idle.Time = 360000;
	} 
	else {
		i = (tm(0) > tm(1)) ? 0 : 1;
		idle.Time = 360001;
	}
	idle.Time = (idle.Time == 360000) ? 360000 : (tm(i) / 5) * 1000 * 60;
	idle.Time = (idle.Time < 360000) ? 360000 : idle.Time;
	idle.Timer = window.setTimeout(function() {
       idleFrame();
	}, idle.Time);	
	if (idle.Time != idle.TimeSave && ClearTimer) {
		CallAsp('IDLETIME&idletime=' + idle.Time + '&tafelnr=' + SB.tafelnr + '&postcode=' + postcode);
		idle.TimeSave = idle.Time;
	}	
}

function ClockCountDown() {

	clock.Timer = window.clearTimeout(clock.Timer);

	S('h'+(SB.b0-SB.b1),clock.Time.toString());

	if(clock.Time === 0) {
		dg('audio').contentWindow.location.replace('sbaudio.htm');   	 	
		ShowBox('TIJD VERSTREKEN<br>druk op een toets',100000);
	}		
	else {
		clock.Timer = window.setTimeout(function() {
		   ClockCountDown(clock.Time-=1);
		}, 1000);	
	}
}

function ShowBox(text, secs) {

	var t, i;
	i = (text.split('>').length);
	dg('OuterBox').style.height = i*10 + '%';
	dg('OuterBox').style.top = (100 - (i*10)) / 2 + '%';	
	HideBox();
	dg('TextBox').innerHTML = text;
	if(arguments.length == 1) {
		B.NoInput = true;
		t=5000;
	}
	else {
		B.NoInput = false;
		t=secs;
	}

	SB.Bartimer = window.setTimeout(function() {
		HideBox();
	}, t);			
	dg('OuterBox').style.display='inline-block';
	dg('fade').style.display='block';
}
function ResizeTelTable(lr) {

	var w = window.parseInt(W * 0.15,10),h = window.parseInt(H / (rows+1),10),tdtel = document.getElementsByTagName('td'),L = tdtel.length,i,kopH;
	dg('teltable'+lr).style.width=w;
	for(i=0;i<L;i++) {
		tdtel[i].style.height=h;
	} 
	kopH = dg('b'+lr+'0').offsetHeight + (H - dg('teltable'+lr).offsetHeight);
	if(kopH > 0) {
		dg('b'+lr+'0').style.height = kopH;
		dg('bL0').style.height = kopH;
	}
	dg('teltable'+lr).style.fontSize = (h / 3) * 2;
}
function tel(lr) {

	var telVan=0,telMin=0,ii,tot=0,i = (lr == 'L') ? 0 : 1,L = l[i].length,n=0;
	
	telVan = L - rows - telMin;
	if(telVan < 0) { 
		telVan = 0;
	}
	for(ii=0;ii<telVan;ii++) {
		tot+= window.parseInt(l[i][ii],10);
	}

	for(ii=telVan;ii<L;ii++) {
		n++;
		tot+=window.parseInt(l[i][ii],10);
		dg('b'+lr+n).innerHTML	= ii+1;
		if(l[i][ii]===0) { 
			dg('s'+lr+n).innerHTML = '---';
			dg('t'+lr+n).innerHTML = '---';
		}	
		else {
			dg('s'+lr+n).innerHTML = l[i][ii];
			dg('t'+lr+n).innerHTML = tot;
		}
	}
	if(ii<rows) {
		for(i=ii+1;i<=rows;i++) {
			dg('b'+lr+i).innerHTML	= '&nbsp;';
			dg('s'+lr+i).innerHTML	= '&nbsp;';
			dg('t'+lr+i).innerHTML	= '&nbsp;';			
		}
	}
}
function Show(n) {
	
	if(clock.Secs !== 0) {
		clock.Timer = window.clearTimeout(clock.Timer);
		clock.Time = 0;
	}
	
	var i = l[0].length - l[1].length,ii,i2,car,brt,hs,moy,d,i3,i4,nu = new window.Date(),oddeven; 
	if(n>=0) {
		l[i].push(n);
		i2=(i-1)*-1;
	}
	else if(n==-1) {
		i=(i-1)*-1;
		l[i].pop();
		i2=i;
	}
	else {
		i2=i;
	}
	brt=l[i].length;
	for(i3=0;i3<2;i3++) {
		hide(dg('bal'+i3),(i2 == i3)?false:true);

		oddeven = (brt%2) ? 'odd' : 'even';
		if(code['pin'+oddeven+i3] != '0') {
			if(brt<2) {
				dg('foto'+i3).src = (i2==i3) ? 'http://www.biljartdivisies.eu/spelersfotos/' + code['bond'+i2] + '.jpg?'+nu : 'http://www.biljartdivisies.eu/spelersfotos/' + code['pin'+oddeven+i3] + '.jpg?'+nu;
			}
			else {
				dg('foto'+i3).src = (i2==i3) ? 'http://www.biljartdivisies.eu/spelersfotos/' + code['bond'+i2] + '.jpg' : 'http://www.biljartdivisies.eu/spelersfotos/' + code['pin'+oddeven+i3] + '.jpg';
			}
		}
	}

	car=0;
	for(ii=0;ii<brt;car+=i4) {
		i4=window.parseInt(l[i][ii],10);
		ii++;
	}
	S('c'+i,car.toString());
	S('b'+i,brt.toString());


	moy = (brt > 0)  ? car / brt : 0;
	if(moy < 10) {
		d = SB.decimals;
	}
	else if(moy < 100) {
		d = 2;
	}
	else {
		d = 1;
	}
	moy = (((brt>0) ? car / brt : 0).toFixed(4 + (d - 3)));
	
	moy = moy.substr(0, moy.length-1);
	S('m'+i,moy.toString());
	if (B.isTel && brt > 0 && !B.OnlyLive) {
		if (i === 0) { 
			tel('L');
		}
		else {
			tel('R');
		}
	}
	hs = (brt===0) ? 0 : window.Math.max.apply({},l[i]);
	S('h'+i,hs.toString());			
	if(clock.Secs !== 0) {
		brt=l[(i-1)*-1].length;
		hs = (brt===0) ? 0 : window.Math.max.apply({},l[(i-1)*-1]);
		S('h'+((i-1)*-1),hs.toString());			
	}
	if(!B.OnlyLive) {
		Ajax('empty/empty.htm', true);
	}
	
	if(!B.OnlyLive+demo()) {
		dg('asp').contentWindow.location.replace('sbupdlive.asp');
	}
}


function CalcScreen(DoShow, forced) {

	var z;
	ScreenSize(false);	

	SB.shrink = (B.isTel) ? window.parseInt(W * 0.15,10) : 0;
	SB.ImgBase = window.parseInt((H / 5) * 0.5,10) / 100;
	ScreenSize(true);
	if(screenfactor != 1) { 
		dg('bal0').width=H*0.50*0.28*0.9*screenfactor;
		dg('bal1').width=H*0.50*0.28*0.9*screenfactor;
	}
	if (!B.OnlyLive) {
		dg('TextBox').style.fontSize = window.parseInt(H / 13,10) + 'px';
		if(demo()) {
			dg('TextDemo').style.fontSize = window.parseInt(H / 13,10) + 'px';
		}
	}
	dg('Red').style.left=SB.shrink;
	dg('Blue').style.left=SB.shrink;
	dg('Red').style.width=W - (SB.shrink*2);
	dg('Blue').style.width=W - (SB.shrink*2);
	for(z=0;z<2;z++) {
		dg('foto'+z).width = H * 0.27 * screenfactor;
		dg('foto'+z).height = H * 0.36;
		font('t'+z,false,true,forced);
		font('s'+z,false,true,forced);
		font('c'+z,false,true,forced);
		font('m'+z,false,true);
		font('h'+z,false,true);
		font('b'+z,false,true);
	}
	ResizeTelTable('L');ResizeTelTable('R');

	
	if (DoShow) {
		if(!B.OnlyLive) {
			Show(-2); 
		}
	}

	window.status = W + 'x' + H;	
}

function browser() {

	var nAgt = window.navigator.userAgent,
	majorVersion,
	browserName  = window.navigator.appName,
	fullVersion  = ''+window.parseFloat(window.navigator.appVersion),
	nameOffset,verOffset,ix;

	// In MSIE, the true version is after "MSIE" in userAgent
	if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
		browserName = "Explorer";
		fullVersion = nAgt.substring(verOffset+5);
	}
	// In Opera, the true version is after "Opera" 
	else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
		browserName = "Opera";
		fullVersion = nAgt.substring(verOffset+6);
	}
	// In Chrome, the true version is after "Chrome" 
	else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
		browserName = "Chrome";
		fullVersion = nAgt.substring(verOffset+7);
	}
	// In Safari, the true version is after "Safari" 
	else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
		browserName = "Safari";
		fullVersion = nAgt.substring(verOffset+7);
	}
	// In Firefox, the true version is after "Firefox" 
	else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
		browserName = "Firefox";
		fullVersion = nAgt.substring(verOffset+8);
	}
	// In most other browsers, "name/version" is at the end of userAgent 
	else if ((nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/'))) {
		browserName = nAgt.substring(nameOffset,verOffset);
		fullVersion = nAgt.substring(verOffset+1);
		if (browserName.toLowerCase()==browserName.toUpperCase()) {
			browserName = window.navigator.appName;
		}
	}
	// trim the fullVersion string at semicolon/space if present
	if ((ix=fullVersion.indexOf(";"))!=-1) {
		fullVersion=fullVersion.substring(0,ix);
	}
	if ((ix=fullVersion.indexOf(" "))!=-1) {
		fullVersion=fullVersion.substring(0,ix);
	}

	majorVersion = window.parseInt(''+fullVersion,10);
	if (window.isNaN(majorVersion)) {
		fullVersion  = ''+window.parseFloat(window.navigator.appVersion); 
	}
	return 'Browser: ' + browserName +  ' ' + fullVersion;
}

function info() {

	var txt;
	if(demo()) {
		txt = 'GEEN LICENTIE<br>Demoversie: ' + SB.versie +  ' max. 20 beurten';			
	}
	else {
		txt = 'Software versie: ' + SB.versie;
	}
	txt += '<br>';
	txt += browser();
	txt += '<br>';
	txt += 'Venstergrootte: ' + W + ' x ' + H;
	txt += '<br>';
	txt += 'IP-adres: ' + SB.ip;
	if(!demo()) {
		txt += '<br>';
		txt += 'Tafelnummer: ' + SB.tafelnr;
	}
	
	return txt;
}

function nomouse() {

	//var KeyBuffer,i,L;
	//KeyBuffer='18*5*1E0EEEEEE5E4E';
	if (!demo() && idle.Running) {
		idleSet(true);
	}
	else {
		CalcScreen(true);
		//L=KeyBuffer.length;
		//for(i=0;i<L;i++) {
			//KeyInput(KeyBuffer.substr(i,1));
		//}
		//dg('body').style.cursor = 'help';
		document.body.focus();
		if(clock.Secs === 0) {
			ShowBox("Geen muis gebruiken a.u.b.",5000);
		}
		else {
			clock.Time = clock.Secs;
			ClockCountDown();
		}
	}
}

function AllZero() {

	return (l[0].length === 0 && l[1].length === 0) ? true : false;
}

function setSetCounts(i, recalc) {

	var ii,n;
	setcount = [0,0];

	tmc = [i,i,i,i];
	if(i === 0) {
		if(B.SetMode) {
			for(ii=0;ii<=1;ii++) {
			
				n = SB['s'+ii].indexOf(' ',0);
				if(n>0) {
					if(isNumeric(SB['s'+ii].substr(0,n-1))) {
						 S('s'+ii,SB['s'+ii].substr(0,n) + ' ' + SB['s'+ii].substr(n+1));
					}
				}		
			}
		}
	}
	else {
		for(ii=0;ii<=1;ii++) {
			S('s'+ii, i + ' ' + SB['s'+ii]);
		}
	}	
	if(recalc) {
		CalcScreen(true,false);
	}
	
}

function codeToZero(focus) {

	if(focus === 0 || focus == 1){
		code['team'+focus] = '0';
		code['bond'+focus] = '0';
		code['pinodd'+focus] = lokaalpin;
		code['pineven'+focus] = lokaalpin;
	}
	else {
		code.team0 = '0';
		code.team1 = '0';
		code.bond0 = '0';
		code.bond1 = '0';
		code.pinodd0=lokaalpin;
		code.pinodd1=lokaalpin;
		code.pineven0=lokaalpin;
		code.pineven1=lokaalpin;		
	}

}

function postcodeLive(lokaal) {
	window.location.replace('sl.htm#postcode' + lokaal);
}

function ShowBuf() {

	var id = (l[0].length - l[1].length);
	S('x'+id,SB.buf);
}

function HideBuf() {

	var txt = ' ',id,i;
    SB.buf = '';       
	for(id=0;id<2;id++) {
		SB['x'+id] = '';		
		for(i=0;i<9;i++) {			
			dg('x'+id.toString()+i.toString()).scr = 'SBfonts/BlackOnWhite/' + txt.charCodeAt(i) + ".gif";
			hide(dg('x'+id.toString()+i.toString()),true);
		}
	}
}

function Swap(id) {

    var i = SB[id+'0'];
	S(id+'0', SB[id+'1']);
	S(id+'1', i);
	
}
function draaien() {

	var img = new window.Image(),i=setcount[0];setcount[0]=setcount[1];setcount[1]=i;
	i=dg('bal0').alt;dg('bal0').alt=dg('bal1').alt;dg('bal1').alt=i;						
	Swap('t');Swap('s');              
	i=code.pinodd0;code.pinodd0=code.pinodd1;code.pinodd1=i;
	i=code.pineven0;code.pineven0=code.pineven1;code.pineven1=i;
	i=code.bond0;code.bond0=code.bond1;code.bond1=i;
	i=code.team0;code.team0=code.team1;code.team1=i;
	i=SB.h0;SB.h0=SB.h1;SB.h1=i;
	i=p[0];p[0]=p[1];p[1]=i;								
	img.src=dg('foto0').src;dg('foto0').src=dg('foto1').src;dg('foto1').src=img.src;
	CalcScreen(true);
	
}
	
function setZeroAll(DoShow) {

	var i,historie = false;

	if(B.SetMode) {
		if(window.parseInt(SB.c0,10) > window.parseInt(SB.c1,10)) {
			setcount[0]++;	
		}
		else {
			setcount[1]++;	
		}	
		LoadBalls();
	}
	
	for(i=0;i<2;i++) {
		l[i] = [];
		S('c'+i,'0');
		S('b'+i,'0');
		S('h'+i,'0');
		S('m'+i,(0).toFixed(SB.decimals));
	}
	tel('L');tel('R');
	if(B.SetMode) {
		draaien();
	}
	if (SB.t0 != 'T H U I S' && SB.t1 != 'G A S T E N' && DoShow && !demo() && wednr === 0 && SB.buf != '00000') {
		historie = true;
		ShowBox('Historie bijwerken');
		CallAsp('NAARHISTORIE&postcode=' + postcode + '&tafelnr=' + SB.tafelnr);		
	}				
	
	if (DoShow && !historie) {
		Show(-2);
	}
}

if(url.indexOf('mobile',0) > 0) {
	B.mobile = true;
}
// DIT WEG als alle nieuwe licentie doorlopen hebben zie tabel SBlicentie COOKIE = Y
clid  = url.substr(40);

if(clid.length === 0) {
	clid='0';
	SB.tafelnr = (getCookie('SBtafel'));
	if(SB.tafelnr === '') {
		if(SB.tafelnr === '') {	
			SB.tafelnr=0;
		}
	}
}
else if(clid.length == 7) {
	SB.tafelnr = clid.substr(6,1); 
}	
else if(clid.length == 9) {
	SB.tafelnr = clid.substr(7,1);
}	
// TOT HIER WEG
// ip-adres in database controleren is licentie 
getCookie('SB');
if (url.indexOf('onlylive') <= 0) { 	
	Ajax('sblicentie.php?ip='+SB.ip+'&tafelnr='+SB.tafelnr+'&SB.versie='+SB.versie+ '&resolutie=' + window.screen.width + 'x' + window.screen.height,false); 
}
