
function InitGames()
{
leftWindow.igames0.value=GMS(m0, n0, 0, b0, 'gamesg0.htm');
leftWindow.igames1.value=GMS(m1, n1, 1, b1, 'gamesg1.htm');
leftWindow.igames2.value=GMS(m2, n2, 2, b2, 'gamesg2.htm');
leftWindow.igames3.value=GMS(m3, n3, 3, b3, 'gamesg3.htm');
leftWindow.igames4.value=GMS(m4, n4, 4, b4, 'gamesg4.htm');
leftWindow.igames5.value=GMS(m5, n5, 5, b5, 'gamesg5.htm');
leftWindow.igames6.value=GMS(m6, n6, 6, b6, 'gamesg6.htm');
leftWindow.igames7.value=GMS(m7, n7, 7, b7, 'gamesg7.htm');
leftWindow.igames8.value=GMS(m8, n8, 8, b8, 'gamesg8.htm');
leftWindow.igames9.value=GMS(m9, n9, 9, b9, 'gamesg9.htm');
leftWindow.igames10.value=GMS(m10, n10, 10, b10, 'gamesg10.htm');
leftWindow.igames11.value=GMS(m11, n11, 11, b11, 'gamesg11.htm');
leftWindow.igames12.value=GMS(m12, n12, 12, b12, 'gamesg12.htm');
leftWindow.igames13.value=GMS(m13, n13, 13, b13, 'gamesg13.htm');
leftWindow.igames14.value=GMS(m14, n14, 14, b14, 'gamesg14.htm');
leftWindow.igames15.value=GMS(m15, n15, 15, b15, 'gamesg15.htm');
leftWindow.igames16.value=GMS(m16, n16, 16, b16, 'gamesg16.htm');
leftWindow.igames17.value=GMS(m17, n17, 17, b17, 'gamesg17.htm');
leftWindow.igames18.value=GMS(m18, n18, 18, b18, 'gamesg18.htm');
leftWindow.igames19.value=GMS(m19, n19, 19, b19, 'gamesg19.htm');
leftWindow.igames20.value=GMS(m20, n20, 20, b20, 'gamesg20.htm');
leftWindow.igames21.value=GMS(m21, n21, 21, b21, 'gamesg21.htm');
leftWindow.igames22.value=GMS(m22, n22, 22, b22, 'gamesg22.htm');
leftWindow.igames23.value=GMS(m23, n23, 23, b23, 'gamesg23.htm');
leftWindow.igames24.value=GMS(m24, n24, 24, b24, 'gamesg24.htm');
leftWindow.igames25.value=GMS(m25, n25, 25, b25, 'gamesg25.htm');
leftWindow.igames26.value=GMS(m26, n26, 26, b26, 'gamesg26.htm');
leftWindow.igames27.value=GMS(m27, n27, 27, b27, 'gamesg27.htm');
leftWindow.igames28.value=GMS(m28, n28, 28, b28, 'gamesg28.htm');
leftWindow.igames29.value=GMS(m29, n29, 29, b29, 'gamesg29.htm');
leftWindow.igames30.value=GMS(m30, n30, 30, b30, 'gamesg30.htm');
leftWindow.igames31.value=GMS(m31, n31, 31, b31, 'gamesg31.htm');
leftWindow.igames32.value=GMS(m32, n32, 32, b32, 'gamesg32.htm');
leftWindow.igames33.value=GMS(m33, n33, 33, b33, 'gamesg33.htm');
leftWindow.igames34.value=GMS(m34, n34, 34, b34, 'gamesg34.htm');
leftWindow.igames35.value=GMS(m35, n35, 35, b35, 'gamesg35.htm');
leftWindow.igames36.value=GMS(m36, n36, 36, b36, 'gamesg36.htm');
leftWindow.igames37.value=GMS(m37, n37, 37, b37, 'gamesg37.htm');
leftWindow.igames38.value=GMS(m38, n38, 38, b38, 'gamesg38.htm');
leftWindow.igames39.value=GMS(m39, n39, 39, b39, 'gamesg39.htm');
leftWindow.igames40.value=GMS(m40, n40, 40, b40, 'gamesg40.htm');
leftWindow.igames41.value=GMS(m41, n41, 41, b41, 'gamesg41.htm');
leftWindow.igames42.value=GMS(m42, n42, 42, b42, 'gamesg42.htm');
leftWindow.igames43.value=GMS(m43, n43, 43, b43, 'gamesg43.htm');
leftWindow.igames44.value=GMS(m44, n44, 44, b44, 'gamesg44.htm');
leftWindow.igames45.value=GMS(m45, n45, 45, b45, 'gamesg45.htm');
leftWindow.igames46.value=GMS(m46, n46, 46, b46, 'gamesg46.htm');
leftWindow.igames47.value=GMS(m47, n47, 47, b47, 'gamesg47.htm');
leftWindow.igames48.value=GMS(m48, n48, 48, b48, 'gamesg48.htm');
leftWindow.igames49.value=GMS(m49, n49, 49, b49, 'gamesg49.htm');
leftWindow.igames50.value=GMS(m50, n50, 50, b50, 'gamesg50.htm');
leftWindow.igames51.value=GMS(m51, n51, 51, b51, 'gamesg51.htm');
leftWindow.igames52.value=GMS(m52, n52, 52, b52, 'gamesg52.htm');
leftWindow.igames53.value=GMS(m53, n53, 53, b53, 'gamesg53.htm');
leftWindow.igames54.value=GMS(m54, n54, 54, b54, 'gamesg54.htm');
leftWindow.igames55.value=GMS(m55, n55, 55, b55, 'gamesg55.htm');
leftWindow.igames56.value=GMS(m56, n56, 56, b56, 'gamesg56.htm');
leftWindow.igames57.value=GMS(m57, n57, 57, b57, 'gamesg57.htm');
leftWindow.igames58.value=GMS(m58, n58, 58, b58, 'gamesg58.htm');
leftWindow.igames59.value=GMS(m59, n59, 59, b59, 'gamesg59.htm');
leftWindow.igames60.value=GMS(m60, n60, 60, b60, 'gamesg60.htm');
leftWindow.igames61.value=GMS(m61, n61, 61, b61, 'gamesg61.htm');
leftWindow.igames62.value=GMS(m62, n62, 62, b62, 'gamesg62.htm');
leftWindow.igames63.value=GMS(m63, n63, 63, b63, 'gamesg63.htm');
leftWindow.igames64.value=GMS(m64, n64, 64, b64, 'gamesg64.htm');
leftWindow.igames65.value=GMS(m65, n65, 65, b65, 'gamesg65.htm');
leftWindow.igames66.value=GMS(m66, n66, 66, b66, 'gamesg66.htm');
leftWindow.igames67.value=GMS(m67, n67, 67, b67, 'gamesg67.htm');
leftWindow.igames68.value=GMS(m68, n68, 68, b68, 'gamesg68.htm');
}
// Copyright 98,99,00,01 by ChessBase GmbH, Germany
function Init( path )
{ gifs = new Array(6);
	for ( var i=0; i< 26; i++)
		gifs[i]= new Image();
	gifs[0].src = path + "b.gif";
	gifs[1].src = path + "bbb.gif";
	gifs[2].src = path + "bbw.gif";
	gifs[3].src = path + "bkb.gif";
	gifs[4].src = path + "bkw.gif";
	gifs[5].src = path + "bqb.gif";
	gifs[6].src = path + "bqw.gif";
	gifs[7].src = path + "brb.gif";
	gifs[8].src = path + "brw.gif";
	gifs[9].src = path + "bnb.gif";
	gifs[10].src = path + "bnw.gif";
	gifs[11].src = path + "wbb.gif";
	gifs[12].src = path + "wbw.gif";
	gifs[13].src = path + "wrb.gif";
	gifs[14].src = path + "wrw.gif";
	gifs[15].src = path + "wqb.gif";
	gifs[16].src = path + "wqw.gif";
	gifs[17].src = path + "wkb.gif";
	gifs[18].src = path + "wkw.gif";
	gifs[19].src = path + "wnb.gif";
	gifs[20].src = path + "wnw.gif";
	gifs[21].src = path + "wpb.gif";
	gifs[22].src = path + "wpw.gif";
	gifs[23].src = path + "bpb.gif";
	gifs[24].src = path + "wpb.gif";
	gifs[25].src = path + "b.gif";
}
function SyncPicture( base, basename )
{	if ( base > -1 ) return base;
	for ( i=0; i < leftDocument.images.length; i++ )
	{	if ( leftDocument.images[i].name != "" )
			if ( leftDocument.images[i].name == basename )
				return i;
			else if ( leftDocument.images[i].name.substring(0,3) == basename.substring(0,3) )
				i+= 63;
	}
	return -1;
}
function MF_5( moves, names, nm, base, basename )
{	base = SyncPicture( base, basename );
	for ( i=0; nMoves[nm] < moves[nLevels[nm]].length / 2 && i < 10; i++ )
		MF( moves, names, nm, base, false );
	return base;
}
function MB_5( moves, names, nm, base, basename )
{	base = SyncPicture( base, basename );
	for ( i=0; ( nMoves[nm] > 0 || nLevels[nm] > 0 ) && i < 10; i++ )
		MB( moves, names, nm, base );
	return base;
}
function GoStart( moves, names, nm, base, basename )
{	base = SyncPicture( base, basename );
	while ( nMoves[nm] > 0 || nLevels[nm] > 0 )
		MB( moves, names, nm, base );
	return base;
}
function GoEnd( moves, names, nm, base, basename )
{	base = SyncPicture( base, basename );
	while ( nMoves[nm] < moves[nLevels[nm]].length / 2 )
		MF( moves, names, nm, base, false );
	return base;
}
function MF( moves, names, nm, base, basename, bCheckV )
{	base = SyncPicture( base, basename );
	if ( bCheckV )
	{	var b = false;
		for ( i=0; i < moves.length; i++ )
			if ( i != nLevels[nm] && moves[i].root == nLevels[nm] && moves[i].move == nMoves[nm] )
				b = true;
		if ( b )
		{	GenerateVarWindow(moves, names, nm, base, basename);
			return base;
		}
	}
	if ( nMoves[nm] < moves[nLevels[nm]].length / 2 )
	{	var n = nMoves[nm]*2;
		var from = moves[nLevels[nm]][ n ] & 0x3f;
		var to = moves[nLevels[nm]][ n+1 ] & 0x3f;
		names[ nNameCount[nm]   ] = leftDocument.images[ base + from ].src;
		names[ nNameCount[nm]+1 ] = leftDocument.images[ base + to ].src;
		var len = names[nNameCount[nm]].length;
		var nn = (Math.floor(( to / 8 )) + ( to % 8 ));
		var dest = ( nn % 2 ) == 1 ? "b" : "w";
		var piece = names[nNameCount[nm]].substring(len-6,len-5);
		if (( moves[nLevels[nm]][ n ] & 0x380 ) == 0x80 )
			piece = "q";
		else if (( moves[nLevels[nm]][ n ] & 0x380 ) == 0x100 )
			piece = "n";
		else if (( moves[nLevels[nm]][ n ] & 0x380 ) == 0x180 )
			piece = "b";
		else if (( moves[nLevels[nm]][ n ] & 0x380 ) == 0x200 )
			piece = "r";
		leftDocument.images[ base + to ].src = names[nNameCount[nm]].substring(0,len-6) + piece + dest + names[nNameCount[nm]].substring(len-4,len);
		nn = (Math.floor(( from / 8 )) + ( from % 8 ));
		if (( nn % 2 ) == 1 )
			leftDocument.images[ base + from ].src = EmptyBlackPath;
		else
			leftDocument.images[ base + from ].src = EmptyWhitePath;
		nMoves[nm]++;
		nNameCount[nm] = nNameCount[nm] + 2;
		if ( nMoves[nm] < moves[nLevels[nm]].length / 2 && ( moves[nLevels[nm]][ nMoves[nm]*2 ] & 0x40 ) == 0x40 )
			MF( moves, names, nm, base, bCheckV );
	}
	return base
}
function MB( moves, names, nm, base, basename )
{	base = SyncPicture( base, basename );
	if ( nMoves[nm] > 0 )
	{	nMoves[nm]--;
		nNameCount[nm] -= 2;
		var from = moves[nLevels[nm]][ nMoves[nm]*2 ] & 0x3f;
		var to = moves[nLevels[nm]][ nMoves[nm]*2+1 ] & 0x3f;
		leftDocument.images[ base + from ].src = names[ nNameCount[nm] ];
		leftDocument.images[ base + to ].src = names[ nNameCount[nm]+1 ];
		if (( moves[nLevels[nm]][ nMoves[nm]*2 ] & 0x40 ) == 0x40 )
			MB( moves, names, nm, base );
	}
	if ( nMoves[nm] == 0 )
		if ( nLevels[nm] > 0 )
		{	nMoves[nm] = moves[nLevels[nm]].move;
			nLevels[nm] = moves[nLevels[nm]].root;
			if (( moves[nLevels[nm]][nMoves[nm]*2] & 0x40 ) == 0x40 )
				nMoves[nm]--;
		}
	return base;
}
function GMS2(  moves, names, nm, base, nMoveNumber, nLevel, basename )
{	if ( nMoveNumber > 0 )
	{	var n = (nMoveNumber-1)*2;
		var off=1;
		for ( i=0; i <= n; i += 2 )
			if (( moves[nLevel][i] & 0x40 ) == 0x40 )
				off = off+1;
		var from = moves[nLevel][ n ] & 0x3f;
		var to = moves[nLevel][ n+1 ] & 0x3f;
		var len = leftDocument.images[ base + to ].src.length;
		var piece = leftDocument.images[ base + to ].src.substring(len-6,len-5);
		if ( piece == "q" )
			piece = pieces.substring(1,2);
		else if ( piece == "r" )
			piece = pieces.substring(4,5);
		else if ( piece == "b" )
			piece = pieces.substring(3,4);
		else if ( piece == "n" )
			piece = pieces.substring(2,3);
		else if ( piece ==  "k" )
			piece = pieces.substring(0,1);
		else piece = pieces.substring(5,6);
		var piece2 = "";
		if (( moves[nLevel][ n ] & 0x380 ) == 0x80 )
			piece2 = pieces.substring(1,2);
		else if (( moves[nLevel][ n ] & 0x380 ) == 0x100 )
			piece2 = pieces.substring(2,3);
		else if (( moves[nLevel][ n ] & 0x380 ) == 0x180 )
			piece2 = pieces.substring(3,4);
		else if (( moves[nLevel][ n ] & 0x380 ) == 0x200 )
			piece2 = pieces.substring(4,5);
		var lines = "abcdefgh";
		var rows = "87654321";
		var fromLine = from%8;
		var fromRow = Math.floor(from/8);
		var toLine = to%8;
		var toRow = Math.floor(to/8);
		var moveNumber = gmn( moves, nLevel ) + nMoveNumber-off;
		var result = (Math.floor((moveNumber)/2)+1).toString() + ". ";
		if ( leftDocument.images[ base + to ].src.substring(len-7,len-6) == "b" )
			result = result + "... ";
		if ( n >= 2 && (( moves[nLevel][n] & 0x40 ) == 0x40 ))
		{	if (( moves[nLevel][n-1] == 62 ) || ( moves[nLevel][n-1] == 6 ))
				result = result + "0-0"
			else if (( moves[nLevel][n-1] == 2 + 7 * 8 ) || ( moves[nLevel][n-1] == 2 + 0 * 8 ))
				result = result + "0-0-0"
			else
			{	var sep = "x";
				from = moves[nLevel][ n-2 ] & 0x3f;
				to = moves[nLevel][ n-1 ] & 0x3f;
				fromLine = from%8;
				fromRow = Math.floor(from/8);
				toLine = to%8;
				toRow = Math.floor(to/8);
				result = result	+ lines.substring( fromLine, fromLine+1 ) + rows.substring( fromRow, fromRow+1 )
										+ sep
										+ lines.substring( toLine, toLine+1 ) + rows.substring( toRow, toRow+1 )
										+ piece2 + " ep";
			}
		}
		else
		{	var len = names[ nNameCount[nm]-1 ].length;
			var substr = names[ nNameCount[nm]-1 ].substring( len-6, len );
			var sep = (( substr == "/w.gif" ) || ( substr == "\\w.gif" ) ||
						  ( substr == "/b.gif" ) || ( substr == "\\b.gif" )) ? "-" : "x";
			if ( piece2 != "" ) piece = "";
			result = result + piece.toUpperCase() + lines.substring( fromLine, fromLine+1 ) + rows.substring( fromRow, fromRow+1 )
				+ sep
				+ lines.substring( toLine, toLine+1 ) + rows.substring( toRow, toRow+1 )
				+ piece2;
		}
	}
	else
		result='';
	if (leftDocument.all)
	{	if ( bFrames )	name = 'i' + baseName + '0';
		else name = 'i' + baseName + nm;
		if ( result == '' )
			leftDocument.all.tags( "DIV" )[name].innerHTML = start_pos;
		else
			leftDocument.all.tags( "DIV" )[name].innerHTML = pos_after + result;
	}
	else if ( leftDocument.layers )
	{	var inx = nm;
		if ( bFrames ) inx = 0;
		leftDocument.layers[inx].document.layers[0].document.clear();
		if ( result == '' )
			var gesamt = "<center>" + start_pos + "</center>";
		else
			var gesamt = "<center>" + pos_after + " " + result + "</center>";
		leftDocument.layers[inx].document.layers[0].document.write(gesamt);
		leftDocument.layers[inx].document.layers[0].document.close();
	}
	else if ( leftDocument.getElementById )
	{	if ( bFrames ) name = 'i' + baseName + '0';
		else name = 'i' + baseName + nm;
		if ( result == '' )
			leftDocument.getElementById(name).innerHTML = start_pos;
		else
			leftDocument.getElementById(name).innerHTML = pos_after + result;
	}
	return result;
}
function GMS( moves, names, nm, base, basename )
{	base = SyncPicture( base, basename );
	var nLevel = nLevels[nm];
	var nMoveNumber = nMoves[nm];
	if ( nMoveNumber >= 0 && nMoveNumber <= moves[nLevel].length / 2 )
	{	if ( leftDocument.all )
		{	if ( nOld[nm] != -1 )
				rightDocument.anchors[nOld[nm]].style.background=BackColor;
			if ( nMoveNumber > 0 )
			{	nOld[nm] = moves[nLevel].base[nMoveNumber-1];
				rightDocument.anchors[nOld[nm]].style.background="gray";
				with ( rightDocument )
					with ( all.tags("a")[nOld[nm]] )
					{	if ( parent.frames[1] && ( offsetTop > body.scrollTop + offsetParent.offsetHeight - offsetHeight || offsetTop < body.scrollTop ))
							rightWindow.scrollTo( 0, offsetTop - ( offsetParent.offsetHeight / 2 ) );
					}
			}
			else
				nOld[nm] = -1;
		}
		return GMS2( moves, names, nm, base, nMoveNumber, nLevel, basename );
	}
	else
		return "??";
}
function gm( moves, names, nm, base, basename, n, m )
{	base = SyncPicture( base, basename );
	GoStart(moves,names,nm,base,basename );
	gm_sub( moves,names,nm,base,basename,n,m);
	GMS( moves, names, nm, base, basename );
}
function gmn( moves, m )
{	if ( m > 0 )
	{	var off=0;
		var n2 = moves[m].move;
		var m2 = moves[m].root;
		for ( i=0; i <= n2*2; i += 2 )
			if (( moves[m2][i] & 0x40 ) == 0x40 )
				off = off+1;
		return gmn( moves, m2 ) + ( n2 - off );
	}
	return 0;
}
function gm_sub( moves, names, nm, base, basename, n, m )
{	if ( m > 0 )
	{	var off=0;
		var n2 = moves[m].move;
		var m2 = moves[m].root;
		for ( i=0; i <= n2*2; i += 2 )
			if (( moves[m2][i] & 0x40 ) == 0x40 )
				off = off+1;
		gm_sub( moves,names,nm,base, basename, n2-off, m2  );
	}
	nLevels[nm]=m;
	nMoves[nm]=0;
	for ( i=0; i < n; i++ )
		MF(moves,names,nm,base,basename, false);
}
