<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0" >
<xsl:key name = "piece-to-square"  match ="squares/square" use = "piece/@id" />


<xsl:param name="service">1</xsl:param>
<xsl:param name="gPossibleMovesMode">1</xsl:param>
<xsl:param name="gCoveringOwn">0</xsl:param>
<xsl:param name="gShowCommentary">0</xsl:param>



<xsl:param name="activePiece">bKe</xsl:param>
<xsl:param name="destinationPiece">0</xsl:param>
<xsl:param name="squareClick">0</xsl:param>
<xsl:param name="PawnToThis">0</xsl:param>


<xsl:variable name = "checkitms" select = "msxsl:node-set($check-list2)//itm"/>
<xsl:variable name = "moves" select = "//moves/move" />
<xsl:variable name = "squares" select = "//squares/square" />
<xsl:variable name = "from-here" select = "//from-here" />


<xsl:template match="/">

	<xsl:choose>
		<xsl:when test="$service=0"><!-- First Parse -->
			
			<xsl:choose>
				<xsl:when test="(not($squares) and not($moves) and not($from-here)) or (board/@runthrough)">
					<board>				
		   				<moves/>			   				
		   				<xsl:variable name = "board">
		  					<xsl:call-template name = "starting-board" />
		   				</xsl:variable>	
		   					   				
		   				<xsl:copy-of select = "$board"/>
		   				
					 	<validation>
						<xsl:attribute  name = "go" >w</xsl:attribute>
					 		<svg-instruction>
					 			<xsl:variable name = "pieces" select = "msxsl:node-set($board)//square[piece]" />
					 			<xsl:apply-templates select = "$pieces" mode="svg-instruction"/>				 			
					 		</svg-instruction>
					 	</validation>
					</board>				
				</xsl:when>
			  
				<xsl:otherwise>
				 
				</xsl:otherwise>
			</xsl:choose>
		
		</xsl:when>
	  
	  
		<xsl:when test="$service=1"><!-- Select Piece -->
			
			<xsl:variable name = "go" select = "substring($activePiece,1,1)" />
			<xsl:variable name = "legal-moves">
			 	<xsl:call-template name = "legal-moves" >
					<xsl:with-param name="board" select="$squares" />
					<xsl:with-param name="go" select="$go"/>
					<xsl:with-param name="PieceID" select="$activePiece"/>
					<xsl:with-param name="PossibleMovesMode" select="$gPossibleMovesMode"/>
					<xsl:with-param name="CoveringOwn" select="$gCoveringOwn"/>
					<xsl:with-param name="KingRookMoves" select="$moves[starts-with(@ref,concat($go,'K')) or starts-with(@ref,concat($go,'R'))]" />
			 	</xsl:call-template>
			</xsl:variable>
			
			
			<board>				
				<xsl:copy-of select = "//moves"/>
				<xsl:copy-of select = "//squares"/>
   				
			 	<validation>
				<xsl:attribute  name = "go" ><xsl:value-of select="//validation/@go" /></xsl:attribute>
				<xsl:attribute  name = "ap" ><xsl:value-of select="$activePiece" /></xsl:attribute>
			 		<svg-covering>	
			 			<xsl:apply-templates select = "//piece-legal-moves/covering" mode="covering-back"/>				 			
			 			<xsl:apply-templates select = "msxsl:node-set($legal-moves)/covering" mode="covering"/>				 			
			 		</svg-covering>
			 		<piece-legal-moves>
						<xsl:copy-of select = "msxsl:node-set($legal-moves)/covering"/>
			 		</piece-legal-moves>
			 	</validation>
			 	
			</board>				
		
		</xsl:when>
	  
	  
		<xsl:otherwise><!-- make move -->
		
			<xsl:variable name = "DestinationSquare">	   					
				<xsl:choose>
					<xsl:when test="$squareClick != -1"><xsl:value-of select="$squareClick" /></xsl:when>	   					  
					<xsl:otherwise>
						<xsl:variable name = "desSq" select = "key('piece-to-square',$destinationPiece)" />
						<xsl:value-of select="$desSq/@id" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>			

			<xsl:variable name = "destinationSquareRow" select = "floor($DestinationSquare div 8)" />
				<xsl:choose>
					<xsl:when test="substring($activePiece,2,1)='P'and $PawnToThis=0  and ($destinationSquareRow = 7 or $destinationSquareRow = 0)">
			 		 		
						<xsl:variable name = "allowed-moves" select = "//piece-legal-moves" />
						<xsl:variable name = "this-moved-allowed" select = "$allowed-moves/*[not(@ref) and @pid=$activePiece and @id=$DestinationSquare]" />					
						<board>
						<xsl:choose>
							<xsl:when test="not($this-moved-allowed)">
								 	<validation>
									<xsl:attribute  name = "go" ><xsl:value-of select="substring($activePiece,1,1)" /></xsl:attribute>
									<xsl:attribute  name = "errcode" >illegal move</xsl:attribute>
								 		<svg-instruction>
								 			<xsl:apply-templates select = "//piece-legal-moves/covering" mode="covering-back"/>				 			
								 		
								 		</svg-instruction>	
								 	</validation>
							</xsl:when>	
							<xsl:otherwise>
							 <validation dsq="{$DestinationSquare}">
									<xsl:attribute  name = "pawnExchange" ><xsl:value-of select="substring($activePiece,1,1)" /></xsl:attribute>
									
							 </validation>
							</xsl:otherwise>
						</xsl:choose>
							
							
							
							
											  
							<moves>
					 			<xsl:copy-of select = "$moves"/>
							</moves>
							<squares>
					 			<xsl:copy-of select = "$squares"/>
							</squares>		
								<xsl:copy-of select = "//piece-legal-moves"/> 
					</board>				
					
					</xsl:when>
				  
					<xsl:otherwise>
						<xsl:variable name = "allowed-moves" select = "//piece-legal-moves" />
						<xsl:variable name = "this-moved-allowed" select = "$allowed-moves/*[not(@ref) and @pid=$activePiece and @id=$DestinationSquare]" />					
						<xsl:choose>
							<xsl:when test="not($this-moved-allowed)">
								<board>				
									<xsl:copy-of select = "//moves"/>
									<xsl:copy-of select = "//squares"/>
								 	<validation>
									<xsl:attribute  name = "go" ><xsl:value-of select="substring($activePiece,1,1)" /></xsl:attribute>
									<xsl:attribute  name = "errcode" >illegal move</xsl:attribute>
								 		<svg-instruction>
								 			<xsl:apply-templates select = "//piece-legal-moves/covering" mode="covering-back"/>				 			
								 		
								 		</svg-instruction>	
								 	</validation>
								 	
								</board>			
							</xsl:when>					  
							<xsl:otherwise>
								<xsl:variable name = "nextplayer">			
									<xsl:choose>
										<xsl:when test="substring($activePiece,1,1) = 'w'">b</xsl:when>			  
										<xsl:otherwise>w</xsl:otherwise>
									</xsl:choose>
								</xsl:variable>
								
								<xsl:variable name = "new-board">			
									<xsl:call-template name = "new-board" >
										<xsl:with-param name="move" select="$this-moved-allowed"/>
										<xsl:with-param name="board" select="$squares" />
										<xsl:with-param name="PawnChange" select="$PawnToThis" />
									</xsl:call-template>
								</xsl:variable>
								
								
								<board>	
									<squares>
										<xsl:copy-of select = "$new-board"/>
									</squares>
									<moves>			
										<xsl:copy-of select = "$moves"/>
										<move>
											<xsl:attribute  name = "to" ><xsl:value-of select="$this-moved-allowed/@id" /></xsl:attribute>
											<xsl:attribute  name = "ref" ><xsl:value-of select="$this-moved-allowed/@pid" /></xsl:attribute>
											<xsl:attribute  name = "m" ><xsl:value-of select="count($moves) + 1" /></xsl:attribute>											
											<xsl:if test = "$PawnToThis != 0">
												<xsl:attribute  name = "pe" ><xsl:value-of select="$PawnToThis" /></xsl:attribute>											
											</xsl:if>
											
											
											<xsl:if test = "$gShowCommentary = 1">
												<xsl:variable name = "this-commentary">
													<xsl:call-template name = "move-to-commentary" >
														<xsl:with-param name="board" select="$new-board"/>
														<xsl:with-param name="go" select="substring($activePiece,1,1)" />
														<xsl:with-param name="nextplayer" select="$nextplayer" />
														<xsl:with-param name="move" select="$this-moved-allowed" />
														<xsl:with-param name="PawnChange" select="$PawnToThis" />
														<xsl:with-param name="KingRookMoves" select="$moves" />
													</xsl:call-template>
												</xsl:variable>
												
												<commentary><xsl:value-of select="$this-commentary" /></commentary>
											</xsl:if>

											
											
										</move>
									</moves>			
								 	<validation>
									<xsl:attribute  name = "go" ><xsl:value-of select="$nextplayer" /></xsl:attribute>
								 		<svg-instruction>
								 			<xsl:variable name = "pieces" select = "//piece-legal-moves/covering" />
								 			<xsl:apply-templates select = "$pieces" mode="covering-back"/>				 			
								 			<xsl:apply-templates select = "$this-moved-allowed" mode="move">				 			
												<xsl:with-param name="PawnChange" select="$PawnToThis" />
								 			</xsl:apply-templates>
								 			
								 		</svg-instruction>	
								 	</validation>
								</board>			
							</xsl:otherwise>
						</xsl:choose>
			
				</xsl:otherwise>
			</xsl:choose>
			
		</xsl:otherwise>
	</xsl:choose>


</xsl:template>

<xsl:template name="move-to-commentary">
<xsl:param name="board" />
<xsl:param name="go" />
<xsl:param name="nextplayer" />
<xsl:param name="move" />
<xsl:param name="PawnChange" />
<xsl:param name="KingRookMoves" />

		
		
		<xsl:variable name = "commentary">
			
			<xsl:variable name = "player-covering-this">
				<xsl:variable name = "pieces" select = "msxsl:node-set($board)//piece[starts-with(@id,$go)]" />
				<xsl:apply-templates select = "$pieces" >
					<xsl:with-param name="board" select="msxsl:node-set($board)/*" />
					<xsl:with-param name="canCastle" select="$nextplayer"/>
				</xsl:apply-templates>
			</xsl:variable>
	
			<xsl:variable name = "check-or-mate-or-not">
					<xsl:variable name = "covering-king" select="msxsl:node-set($player-covering-this)/covering[@capture= concat($nextplayer,'Ke')]" />
					
					<xsl:if test = "$covering-king">			
						<xsl:variable name = "legal-moves">
						 	<xsl:call-template name = "legal-moves" >
								<xsl:with-param name="board" select="msxsl:node-set($board)/*" />
								<xsl:with-param name="go" select="$nextplayer"/>
								<xsl:with-param name="PieceID" select="$go"/>
								<xsl:with-param name="PossibleMovesMode">0</xsl:with-param>
								<xsl:with-param name="CoveringOwn">0</xsl:with-param>
								<xsl:with-param name="KingRookMoves" select="$KingRookMoves[@fred]" />
						 	</xsl:call-template>
						</xsl:variable>
					
						<xsl:choose>
							<xsl:when test="not(msxsl:node-set($legal-moves)/covering[not(@ref)])">
							++
							</xsl:when>
						  
							<xsl:otherwise>
							+
							</xsl:otherwise>
						</xsl:choose>			
					</xsl:if>
			</xsl:variable>
	
	
			<xsl:variable name = "PieceMoved" select = "substring($move/@pid,2,1)" />	
			<xsl:variable name = "columnAlphafrom" select = "substring('abcdefgh',($move/@sqfrom mod 8) + 1,1)" />
			<xsl:variable name = "columnAlphato" select = "substring('abcdefgh',($move/@id mod 8) + 1,1)" />
			<xsl:variable name = "Rowto" select = "floor($move/@id div 8) + 1" />
				
			<xsl:choose>
				<xsl:when test="$PieceMoved = 'P'">				
					<xsl:choose>
						<xsl:when test="$move/@capture">
							<xsl:value-of select="$columnAlphafrom" />x<xsl:value-of select="$columnAlphato" /><xsl:value-of select="$Rowto" />
						</xsl:when>
					  
						<xsl:otherwise>
							<xsl:value-of select="$columnAlphato" /><xsl:value-of select="$Rowto" />
						</xsl:otherwise>
					</xsl:choose>
					
					<xsl:if test = "($go ='w' and $Rowto = 8) or ($go ='b' and $Rowto = b)">
					=<xsl:value-of select="substring($PawnToThis,2,1)" />
					</xsl:if>
					
				</xsl:when>
			  
				<xsl:otherwise>
					<xsl:variable name = "this-go-legal-moves">
					 	<xsl:call-template name = "legal-moves" >
							<xsl:with-param name="board" select="msxsl:node-set($board)/*" />
							<xsl:with-param name="go" select="$go"/>
							<xsl:with-param name="PieceID" select="$PieceMoved"/>
							<xsl:with-param name="PossibleMovesMode">2</xsl:with-param>
							<xsl:with-param name="CoveringOwn">0</xsl:with-param>
							<xsl:with-param name="KingRookMoves" select="$KingRookMoves[@fred]" />
					 	</xsl:call-template>
					</xsl:variable>
					<xsl:variable name = "couldhaves" select="msxsl:node-set($player-covering-this)/covering[substring(@pid,2,1) = $PieceMoved and @id=$move/@id ]" />
					
					<xsl:choose>
						<xsl:when test="$move/@castle">
							
							<xsl:choose>
								<xsl:when test="$move/@sqfrom &gt; $move/@id">
								0-0-0
								</xsl:when>
							  
								<xsl:otherwise>
								0-0
								</xsl:otherwise>
							</xsl:choose>
						</xsl:when>

						<xsl:when test="$move/@capture">
							<xsl:value-of select="$PieceMoved" /><xsl:if test = "count($couldhaves) != 0"><xsl:value-of select="$columnAlphafrom" /></xsl:if>x<xsl:value-of select="$columnAlphato" /><xsl:value-of select="$Rowto" />
						</xsl:when>
					  
						<xsl:otherwise>
							<xsl:value-of select="$PieceMoved" /><xsl:if test = "count($couldhaves) != 0"><xsl:value-of select="$columnAlphafrom" /></xsl:if><xsl:value-of select="$columnAlphato" /><xsl:value-of select="$Rowto" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="normalize-space($check-or-mate-or-not)" />
		</xsl:variable>
		
		
		<!-- if not zero need to check if check mate ie all moves for next player  -->
		<xsl:value-of select="normalize-space($commentary)" />


</xsl:template>



<xsl:template name="legal-moves">
<xsl:param name="board" />
<xsl:param name="go" />
<xsl:param name="PieceID" />
<xsl:param name="PossibleMovesMode" />
<xsl:param name="CoveringOwn" />
<xsl:param name="KingRookMoves" />

	<xsl:variable name = "nextplayer">			
		<xsl:choose>
			<xsl:when test="$go = 'w'">b</xsl:when>			  
			<xsl:otherwise>w</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<!-- Am I in Check or pass through NEED TO CHECK IF CONCERNED PIECES MOVED-->
	<xsl:variable name = "CanICastle">
	
		<!-- has king or target rook moved -->
		<!--
		
		-->
		
		<castle-test>
			
			<xsl:choose>
				<xsl:when test="$go = 'w' and not($KingRookMoves[@ref = 'wKe'])">
				
					
					<xsl:variable name = "opp-covering-this">
						<xsl:apply-templates select = "$board/piece[starts-with(@id,$nextplayer)]" >
							<xsl:with-param name="board" select="$board" />
							<xsl:with-param name="canCastle" select="$nextplayer"/>
						</xsl:apply-templates>
					</xsl:variable>	
					
					
					<xsl:choose>
						<xsl:when test="msxsl:node-set($opp-covering-this)/covering[@capture= concat($go,'Ke')]">
						 	<cannotcastle/>
						</xsl:when>					  
						<xsl:otherwise>
						 	
						 	<xsl:if test = "not($KingRookMoves[@ref = 'wRa']) and not($board[@id=1 and piece]) and not($board[@id=2 and piece])  and not($board[@id=3 and piece])">
								<xsl:if test = "not(msxsl:node-set($opp-covering-this)/covering[@id= 3]) and not(msxsl:node-set($opp-covering-this)/covering[@id= 2])">
									<castle step="-2"/>
								</xsl:if>
						 	</xsl:if>
						 	
						 	
						 	<xsl:if test = "not($KingRookMoves[@ref = 'wRh']) and not($board[@id=5 and piece]) and not($board[@id=6 and piece])">
								<xsl:if test = "not(msxsl:node-set($opp-covering-this)/covering[@id= 5]) and not(msxsl:node-set($opp-covering-this)/covering[@id= 6])">
									<castle step="2"/>
								</xsl:if>
						 	</xsl:if>
							</xsl:otherwise>
					</xsl:choose>														
				
				</xsl:when>
				
				
				<xsl:when test="$go = 'b' and not($KingRookMoves[@ref = 'bKe'])">

					<xsl:variable name = "opp-covering-this">
						<xsl:apply-templates select = "$board/piece[starts-with(@id,$nextplayer)]" >
							<xsl:with-param name="board" select="$board" />
							<xsl:with-param name="canCastle" select="$nextplayer"/>
						</xsl:apply-templates>
					</xsl:variable>	
					
					
					<xsl:choose>
						<xsl:when test="msxsl:node-set($opp-covering-this)/covering[@capture= concat($go,'Ke')]">
						 	<cannotcastle/>
						</xsl:when>
					  
						<xsl:otherwise>
						 	
						 	<xsl:if test = "not($KingRookMoves[@ref = 'bRa']) and not($board[@id=57 and piece]) and not($board[@id=58 and piece]) and not($board[@id=59 and piece])">
								<xsl:if test = "not(msxsl:node-set($opp-covering-this)/covering[@id= 59]) and not(msxsl:node-set($opp-covering-this)/covering[@id= 58])">
									<castle step="-2"/>
								</xsl:if>

						 	</xsl:if>
						 	<xsl:if test = "not($KingRookMoves[@ref != 'bRh']) and not($board[@id=61 and piece]) and not($board[@id=62 and piece])">
								<xsl:if test = "not(msxsl:node-set($opp-covering-this)/covering[@id= 61]) and not(msxsl:node-set($opp-covering-this)/covering[@id= 62])">
									<castle step="2"/>
								</xsl:if>
						 	</xsl:if>
						</xsl:otherwise>
					</xsl:choose>														
				</xsl:when>
			  
				<xsl:otherwise>
					<cannotcastle/>
				</xsl:otherwise>
			</xsl:choose>
		</castle-test>
		
	</xsl:variable>

	<xsl:variable name = "player-covering-this">
		<xsl:variable name = "pieces" select = "$board/piece[starts-with(@id,$go)]" />
		<xsl:apply-templates select = "$pieces" >
			<xsl:with-param name="board" select="$board" />
			<xsl:with-param name="canCastle" select="msxsl:node-set($CanICastle)//castle-test"/>
		</xsl:apply-templates>
	</xsl:variable>
	

	<xsl:variable name = "possible-moves">
		<xsl:choose>
			<xsl:when test="$PossibleMovesMode = 0">				
				<xsl:choose>
					<xsl:when test="$CoveringOwn = 1">
						<xsl:copy-of select = "msxsl:node-set($player-covering-this)/covering"/>
					</xsl:when>
				  
					<xsl:otherwise>
						<xsl:copy-of select = "msxsl:node-set($player-covering-this)/covering[not(@ref)]"/>
					</xsl:otherwise>
				</xsl:choose>			
			</xsl:when>
			<xsl:when test="$PossibleMovesMode = 2">				
				<xsl:choose>
					<xsl:when test="$CoveringOwn = 1">
						<xsl:copy-of select = "msxsl:node-set($player-covering-this)/covering[starts-with(@pid,concat($go,$PieceID))]"/>
					</xsl:when>
				  
					<xsl:otherwise>
						<xsl:copy-of select = "msxsl:node-set($player-covering-this)/covering[not(@ref) and starts-with(@pid,concat($go,$PieceID))]"/>
					</xsl:otherwise>
				</xsl:choose>			
			</xsl:when>
		  
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="$CoveringOwn = 1">
				<xsl:copy-of select = "msxsl:node-set($player-covering-this)/covering[@pid=$PieceID]"/>
					</xsl:when>
				  
					<xsl:otherwise>
				<xsl:copy-of select = "msxsl:node-set($player-covering-this)/covering[not(@ref) and @pid=$PieceID]"/>
					</xsl:otherwise>
				</xsl:choose>			
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>	
	
	
	
	
	<xsl:for-each select = "msxsl:node-set($possible-moves)/*">
		<xsl:variable name = "this-move" select = "." />

		<xsl:call-template name = "board-after-move" >
			<xsl:with-param name="move" select="$this-move"/>
			<xsl:with-param name="board" select="$board" />
			<xsl:with-param name="go" select="$go" />
			<xsl:with-param name="nextplayer" select="$nextplayer" />
		</xsl:call-template>
		
		
	</xsl:for-each>
	
	
</xsl:template>














<xsl:template name="board-after-move">
<xsl:param name="move" />
<xsl:param name="board" />
<xsl:param name="go" />
<xsl:param name="nextplayer" />

<xsl:choose>
	<xsl:when test="$move/@ref">
			<xsl:copy-of select = "$move"/>
	</xsl:when>
  
	<xsl:otherwise>
		<xsl:variable name = "new-board">		
			<xsl:call-template name = "new-board" >
				<xsl:with-param name="move" select="$move"/>
				<xsl:with-param name="board" select="$board" />
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name = "opposition-covering-this">
			<xsl:variable name = "pieces" select = "msxsl:node-set($new-board)//piece[starts-with(@id,$nextplayer)]" />
			<xsl:apply-templates select = "$pieces" >
				<xsl:with-param name="board" select="msxsl:node-set($new-board)/square" />
			</xsl:apply-templates>
		</xsl:variable>
		
		<xsl:if test = "not(msxsl:node-set($opposition-covering-this)/*[@capture= concat($go,'Ke')])">
			<xsl:copy-of select = "$move"/>
		</xsl:if>
	</xsl:otherwise>
</xsl:choose>
	
</xsl:template>


<xsl:template name="new-board">
<xsl:param name="move" />
<xsl:param name="board" />
<xsl:param name="PawnChange">0</xsl:param>

	<xsl:variable name = "SquareFromID" select = "$move/@sqfrom" />
	<xsl:variable name = "SquareToID" select = "$move/@id" />
	<xsl:variable name = "PieceID" select = "$move/@pid" />
	<xsl:variable name = "CapturedPieceID" select = "$move/@capture" />

	<xsl:choose>
		<xsl:when test="$move/@castle or $move/@epossion">				
			<xsl:choose>
				<xsl:when test="$move/@castle">
					<xsl:copy-of select = "$board[@id != $SquareFromID and @id != $SquareToID and @id != $move/@castle and @id != $move/@castlefrom]"/>
					<square id="{$move/@castlefrom}"></square>									 
					<square id="{$move/@castle}">
						<piece id="{$move/@castleref}"/>
					</square>
				</xsl:when>
			  
				<xsl:otherwise>
					<xsl:copy-of select = "$board[@id != $SquareFromID and @id != $SquareToID and @id != $move/@epossion]"/>
					<square id="{$move/@epossion}"></square>									 
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>		  
		<xsl:otherwise>
			<xsl:copy-of select = "$board[@id != $SquareFromID and @id != $SquareToID]"/>			 
		</xsl:otherwise>
	</xsl:choose>
	
	<square id="{$SquareFromID}"></square>
	<square id="{$SquareToID}">
		
		<xsl:choose>
			<xsl:when test="$PawnChange = 0">
				<piece id="{$PieceID}"/>
			</xsl:when>
		  
			<xsl:otherwise>
				<xsl:variable name = "newPiece">
					<xsl:call-template name = "change-pawn-to-this" >
						<xsl:with-param name="these-moves" select="$moves"/>
						<xsl:with-param name="to-this-type" select="$PawnChange" />
					</xsl:call-template>
				</xsl:variable>
				<piece id="{$newPiece}"/>
			 
			</xsl:otherwise>
		</xsl:choose>
	</square>
	
</xsl:template>


<xsl:template name="change-pawn-to-this">
<xsl:param name="these-moves" />
<xsl:param name="to-this-type" />
<xsl:choose>
	<xsl:when test="$to-this-type = 0">0</xsl:when>
	<xsl:otherwise>		
		<xsl:variable name = "swapped" select = "$these-moves[@pe =$to-this-type]" />
		<xsl:value-of select="concat($to-this-type,count($swapped)+ 1)" />
	</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="piece">
<xsl:param name="board" />
<xsl:param name="canCastle" />

	<xsl:variable name = "pieceID" select = "@id" />
	<xsl:variable name = "SquarePieceOn" select = "$board[piece/@id = $pieceID]" />
	<xsl:variable name = "SquarePieceOnID" select = "$SquarePieceOn/@id" />

	<xsl:variable name = "pieceType" select = "substring($pieceID,2,1)" />
	
	<xsl:variable name = "pieceColor" select = "substring($pieceID,1,1)" />
		
	<xsl:variable name = "validation">
		<xsl:choose>
			<xsl:when test="$pieceType = 'P'">				
				<xsl:choose>
					<xsl:when test="$pieceColor = 'w'">
			 			<xsl:copy-of select = "$checkitms[@ref = 'wP']"/>
					</xsl:when>				  
					<xsl:otherwise>
			 			<xsl:copy-of select = "$checkitms[@ref = 'bP']"/>
					</xsl:otherwise>
				</xsl:choose>			
			</xsl:when>		  
			<xsl:otherwise>			 	
			 	<xsl:copy-of select = "$checkitms[contains(@ref,$pieceType)]"/>
			</xsl:otherwise>
		</xsl:choose>		
	</xsl:variable>

		<xsl:apply-templates select = "msxsl:node-set($validation)//m" >
			<xsl:with-param name="board" select="$board" />
			<xsl:with-param name="pieceID" select="$pieceID" />			
			<xsl:with-param name="SquarePieceOnID" select="$SquarePieceOnID" />
			<xsl:with-param name="pieceType" select="$pieceType" />
			<xsl:with-param name="pieceColor" select="$pieceColor" />
			<xsl:with-param name="canCastle" select="$canCastle" />
		</xsl:apply-templates>
</xsl:template>

<xsl:template match="m">
<xsl:param name="board" />
<xsl:param name="pieceID" />
<xsl:param name="SquarePieceOnID" />
<xsl:param name="pieceType" />
<xsl:param name="pieceColor" />
<xsl:param name="canCastle" />

	<xsl:choose>
		<xsl:when test="$pieceType != 'P'">
			<xsl:call-template name = "non-Pawn-moves" >
				<xsl:with-param name="validation" select="." />
				<xsl:with-param name="board" select="$board" />
				<xsl:with-param name="pieceID" select="$pieceID" />
				<xsl:with-param name="SquarePieceOnID" select="$SquarePieceOnID" />
				<xsl:with-param name="pieceType" select="$pieceType" />
				<xsl:with-param name="pieceColor" select="$pieceColor" />
				<xsl:with-param name="StartSq" select="$SquarePieceOnID" />
			<xsl:with-param name="canCastle" select="$canCastle" />
			</xsl:call-template>
		</xsl:when>
	  
		<xsl:otherwise>
			<xsl:call-template name = "Pawn-Moves" >
				<xsl:with-param name="validation" select="." />
				<xsl:with-param name="board" select="$board" />
				<xsl:with-param name="pieceID" select="$pieceID" />
				<xsl:with-param name="SquarePieceOnID" select="$SquarePieceOnID" />
				<xsl:with-param name="pieceType" select="$pieceType" />
				<xsl:with-param name="pieceColor" select="$pieceColor" />
				<xsl:with-param name="StartSq" select="$SquarePieceOnID" />
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>




<xsl:template name="non-Pawn-moves">
<xsl:param name="validation" />
<xsl:param name="board" />
<xsl:param name="pieceID" />
<xsl:param name="SquarePieceOnID" />
<xsl:param name="pieceType" />
<xsl:param name="pieceColor" />
<xsl:param name="StartSq" />
<xsl:param name="canCastle" />

<xsl:variable name = "SquareRow" select = "floor($SquarePieceOnID div 8)" />
<xsl:variable name = "SquareCol" select = "$SquarePieceOnID mod 8" />
<xsl:variable name = "targetSquareID" select = "$validation/@step + $SquarePieceOnID" />

<xsl:if test = "$targetSquareID &gt; -1 and $targetSquareID &lt; 64 and contains($validation/@rows,$SquareRow) and contains($validation/@cols,$SquareCol)">

	<xsl:variable name = "targetSquare" select = "msxsl:node-set($board)[@id=$targetSquareID]" />
	<xsl:choose>
		<xsl:when test="$targetSquare/piece and not($validation/@mode)">
			
			<xsl:choose>
				<xsl:when test="substring($targetSquare/piece/@id,1,1) != $pieceColor">
					<covering>
						<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
						<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
						<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>
						<xsl:attribute  name = "capture" ><xsl:value-of select="$targetSquare/piece/@id" /></xsl:attribute>
					</covering>				
				</xsl:when>
				<xsl:when test="substring($targetSquare/piece/@id,1,1) = $pieceColor">
					<covering>
						<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
						<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
						<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>
						<xsl:attribute  name = "ref" ><xsl:value-of select="$targetSquare/piece/@id" /></xsl:attribute>
					</covering>				
				</xsl:when>
			</xsl:choose>
		</xsl:when>
	  
		<xsl:otherwise>		
			
			<xsl:choose>
				<xsl:when test="$pieceType = 'K'">
					
					<xsl:choose>
						<xsl:when test="$validation/@mode">
							<xsl:if test = "msxsl:node-set($canCastle)//castle/@step = $validation/@step">
								<covering>
									<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
									<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
									<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>									
									<xsl:choose>
										<xsl:when test="$targetSquareID = 2">
											<xsl:attribute  name = "castle" >3</xsl:attribute>				
											<xsl:attribute  name = "castlefrom" >0</xsl:attribute>				
											<xsl:attribute  name = "castleref" >wRa</xsl:attribute>				
										</xsl:when>
										<xsl:when test="$targetSquareID = 6">
											<xsl:attribute  name = "castle" >5</xsl:attribute>				
											<xsl:attribute  name = "castlefrom" >7</xsl:attribute>				
											<xsl:attribute  name = "castleref" >wRh</xsl:attribute>				
										</xsl:when>
										<xsl:when test="$targetSquareID = 62">
											<xsl:attribute  name = "castle" >61</xsl:attribute>				
											<xsl:attribute  name = "castlefrom" >63</xsl:attribute>				
											<xsl:attribute  name = "castleref" >bRh</xsl:attribute>				
										</xsl:when>
										<xsl:when test="$targetSquareID = 58">
											<xsl:attribute  name = "castle" >59</xsl:attribute>				
											<xsl:attribute  name = "castlefrom" >56</xsl:attribute>				
											<xsl:attribute  name = "castleref" >bRa</xsl:attribute>				
										</xsl:when>									  
									</xsl:choose>				
								</covering>
							</xsl:if>
						</xsl:when>				
						<xsl:otherwise>
							<covering>
								<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
								<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
								<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>
							</covering>
						</xsl:otherwise>
					</xsl:choose>
				
				</xsl:when>
			  
				<xsl:otherwise>
					<covering>
						<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
						<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
						<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>				
					</covering>
					<xsl:if test = "$validation/@cycle=1">
						<xsl:call-template name = "non-Pawn-moves" >
							<xsl:with-param name="validation" select="$validation" />
							<xsl:with-param name="board" select="$board" />
							<xsl:with-param name="pieceID" select="$pieceID" />
							<xsl:with-param name="SquarePieceOnID" select="$targetSquareID" />
							<xsl:with-param name="pieceType" select="$pieceType" />
							<xsl:with-param name="pieceColor" select="$pieceColor" />
							<xsl:with-param name="StartSq" select="$StartSq" />
						</xsl:call-template>
					</xsl:if>		 
				 
				</xsl:otherwise>
			</xsl:choose>	
	
	
		</xsl:otherwise>
	</xsl:choose>
</xsl:if>
</xsl:template>


<xsl:template name="Pawn-Moves">
<xsl:param name="validation" />
<xsl:param name="board" />
<xsl:param name="pieceID" />
<xsl:param name="SquarePieceOnID" />
<xsl:param name="pieceType" />
<xsl:param name="pieceColor" />
<xsl:param name="StartSq" />

<xsl:variable name = "SquareRow" select = "floor($SquarePieceOnID div 8)" />
<xsl:variable name = "SquareCol" select = "$SquarePieceOnID mod 8" />
<xsl:variable name = "targetSquareID" select = "$validation/@step + $SquarePieceOnID" />

<xsl:if test = "$targetSquareID &gt; -1 and $targetSquareID &lt; 64 and contains($validation/@rows,$SquareRow) and contains($validation/@cols,$SquareCol)">

	<xsl:variable name = "targetSquare" select = "msxsl:node-set($board)[@id=$targetSquareID]" />
			
	<xsl:choose>
		<xsl:when test="not($targetSquare/piece) and @empty=1">
			<xsl:choose>
				<xsl:when test="$validation/@step=16 or $validation/@step=-16">
					<xsl:variable name = "firstsquare" select = "msxsl:node-set($board)[@id=number($SquarePieceOnID + ($validation/@step) div 2)]" />
					<xsl:if test = "not($firstsquare/piece)">
						<covering>
						<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
						<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
							<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>	
						</covering>
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					<covering>
						<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
						<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
						<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>				
					</covering>
				</xsl:otherwise>									  
			</xsl:choose>
		
		</xsl:when>
		<xsl:when test="not($targetSquare/piece) and @empty=0">	
		<!-- en-poissant -->		
			<xsl:variable name = "last-move" select = "$moves[position() = last()]" />
			<xsl:variable name = "last-move-Piece" select = "substring($last-move/@ref,2,1)" />
			
			<xsl:if test = "$last-move-Piece = 'P'">
				<xsl:variable name = "moves-made-by-last-piece" select = "$moves[@ref=$last-move/@ref]" />
				<xsl:if test = "count($moves-made-by-last-piece) = 1">					
					<xsl:choose>
						<xsl:when test="$pieceColor = 'w' and $last-move/@to = $targetSquareID - 8">
							<covering>
								<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
								<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
								<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>				
								<xsl:attribute  name = "capture" ><xsl:value-of select="$last-move/@ref" /></xsl:attribute>
								<xsl:attribute  name = "epossion" ><xsl:value-of select="$last-move/@to" /></xsl:attribute>
							</covering>
						</xsl:when>
					   <!-- and $last-move/@to = $targetSquareID - 8 -->
						<xsl:when test="$pieceColor = 'b' and $last-move/@to = $targetSquareID + 8">
							<covering>
								<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
								<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
								<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>				
								<xsl:attribute  name = "capture" ><xsl:value-of select="$last-move/@ref" /></xsl:attribute>
								<xsl:attribute  name = "epossion" ><xsl:value-of select="$last-move/@to" /></xsl:attribute>
							</covering>
						</xsl:when>
					</xsl:choose>
		
				</xsl:if>
			</xsl:if>
		
		</xsl:when>
		
		<xsl:when test="$targetSquare/piece and @empty=0">			
			
			<xsl:choose>
				<xsl:when test="substring($targetSquare/piece/@id,1,1) != $pieceColor">
					<covering>
						<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
						<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
						<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>				
						<xsl:attribute  name = "capture" ><xsl:value-of select="$targetSquare/piece/@id" /></xsl:attribute>
					</covering>
				</xsl:when>
			  
				<xsl:otherwise>
					<covering>
						<xsl:attribute  name = "pid" ><xsl:value-of select="$pieceID" /></xsl:attribute>
						<xsl:attribute  name = "sqfrom" ><xsl:value-of select="$StartSq" /></xsl:attribute>
						<xsl:attribute  name = "id" ><xsl:value-of select="$targetSquareID" /></xsl:attribute>				
						<xsl:attribute  name = "ref" ><xsl:value-of select="$targetSquare/piece/@id" /></xsl:attribute>
					</covering>
				</xsl:otherwise>
			</xsl:choose>
			
		</xsl:when>
	  
	</xsl:choose>
			
</xsl:if>

</xsl:template>



<xsl:template match="covering" mode="move">
<xsl:param name="PawnChange">0</xsl:param>
	
	<xsl:variable name = "SquareFromID" select = "@sqfrom" />
	<xsl:variable name = "SquareToID" select = "@id" />
	<xsl:variable name = "PieceID" select = "@pid" />
	<xsl:variable name = "CapturedPieceID" select = "@capture" />
	
	<xsl:call-template name = "svg-move-piece" >
		<xsl:with-param name="PieceID" select="$PieceID"/>
		<xsl:with-param name="SquareToID" select="$SquareToID" />
	</xsl:call-template>
	

	<xsl:if test = "$PawnChange!=0">
		<xsl:call-template name = "svg-capture-piece" >
			<xsl:with-param name="PieceID" select="$PieceID"/>
		</xsl:call-template>
		
		<xsl:variable name = "newPiece">
			<xsl:call-template name = "change-pawn-to-this" >
				<xsl:with-param name="these-moves" select="$moves"/>
				<xsl:with-param name="to-this-type" select="$PawnChange" />
			</xsl:call-template>
		</xsl:variable>
	
		<xsl:call-template name = "svg-move-piece" >
			<xsl:with-param name="PieceID" select="$newPiece"/>
			<xsl:with-param name="SquareToID" select="$SquareToID" />
		</xsl:call-template>
	
	</xsl:if>	
	
	<xsl:if test = "$CapturedPieceID">
		<xsl:call-template name = "svg-capture-piece" >
			<xsl:with-param name="PieceID" select="$CapturedPieceID"/>
		</xsl:call-template>
	</xsl:if>
	
	<xsl:if test = "@castle">
		<xsl:call-template name = "svg-move-piece" >
			<xsl:with-param name="PieceID" select="@castleref"/>
			<xsl:with-param name="SquareToID" select="@castle" />
		</xsl:call-template>		
	</xsl:if>	
</xsl:template>


<xsl:template name="svg-capture-piece">
<xsl:param name="PieceID" />
	<ins>
		<xsl:attribute  name = "id" ><xsl:value-of select="$PieceID" /></xsl:attribute>
		<xsl:attribute  name = "x" >0</xsl:attribute>
		<xsl:attribute  name = "y" >0</xsl:attribute>
		<xsl:attribute  name = "visibility" >hidden</xsl:attribute>
	</ins>
</xsl:template>

<xsl:template name="svg-move-piece">
<xsl:param name="PieceID" />
<xsl:param name="SquareToID" />
	<xsl:variable name = "row" select = "78 + (($SquareToID) mod 8) * 50" />
	<xsl:variable name = "col" select = "444 - (floor((($SquareToID) div 8)) * 50)" />
	<ins>
		<xsl:attribute  name = "id" ><xsl:value-of select="$PieceID" /></xsl:attribute>
		<xsl:attribute  name = "x" ><xsl:value-of select="$row" /></xsl:attribute>
		<xsl:attribute  name = "y" ><xsl:value-of select="$col" /></xsl:attribute>
		<xsl:attribute  name = "visibility" >visible</xsl:attribute>
	</ins>
</xsl:template>

<xsl:template match="covering" mode="covering-back">
<ins>
	<xsl:attribute  name = "id" >sq_<xsl:value-of select="@id" /></xsl:attribute>
	<xsl:choose>
		<xsl:when test="(floor(@id div 8) + @id) mod 2 = 0">
			<xsl:attribute  name = "fill" >#aaa</xsl:attribute>
		</xsl:when>
	  
		<xsl:otherwise>
			<xsl:attribute  name = "fill" >#ccc</xsl:attribute>
		</xsl:otherwise>
	</xsl:choose>
</ins>
</xsl:template>


<xsl:template match="covering" mode="covering">
<ins>
	<xsl:attribute  name = "id" >sq_<xsl:value-of select="@id" /></xsl:attribute>
	<xsl:choose>
		<xsl:when test="@capture">
			<xsl:attribute  name = "fill" >#00dd00</xsl:attribute>
		</xsl:when>
		<xsl:when test="@ref">
			<xsl:attribute  name = "fill" >#dd0000</xsl:attribute>
		</xsl:when>
	  
		<xsl:otherwise>
			<xsl:attribute  name = "fill" >#0000dd</xsl:attribute>
		</xsl:otherwise>
	</xsl:choose>
</ins>
</xsl:template>

<xsl:template match="square" mode="svg-instruction">
<xsl:variable name = "row" select = "78 + ((@id) mod 8) * 50" />
<xsl:variable name = "col" select = "444 - (floor(((@id) div 8)) * 50)" />
<ins>
	<xsl:attribute  name = "id" ><xsl:value-of select="piece/@id" /></xsl:attribute>
	<xsl:attribute  name = "x" ><xsl:value-of select="$row" /></xsl:attribute>
	<xsl:attribute  name = "y" ><xsl:value-of select="$col" /></xsl:attribute>
	<xsl:attribute  name = "visibility" >visible</xsl:attribute>
</ins>
</xsl:template>



<!--  STARTING BOARD  -->
<xsl:template name="starting-board">
<squares>
	<xsl:call-template name = "starting-square" >
		<xsl:with-param name="square-number">0</xsl:with-param>
	</xsl:call-template>
</squares>
</xsl:template>


<xsl:template name="starting-square">
<xsl:param name="square-number" />	
	<xsl:choose>
		<xsl:when test="$square-number &lt; 64">
			
			<xsl:variable name = "columnNo" select = "$square-number mod 8" />
			<xsl:variable name = "columnAlpha" select = "substring('abcdefgh',$columnNo + 1,1)" />
			<xsl:choose>
				<!-- white-->
				<xsl:when test="($square-number &gt; 7) and ($square-number &lt; 16) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="w">
							<xsl:attribute  name = "id" >wP<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="($square-number = 0) or ($square-number = 7) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="w" >
							<xsl:attribute  name = "id" >wR<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="($square-number = 1) or ($square-number = 6) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="w" >
							<xsl:attribute  name = "id" >wN<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="($square-number = 2) or ($square-number = 5) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="w" >
							<xsl:attribute  name = "id" >wB<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="$square-number = 3">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="w">
							<xsl:attribute  name = "id" >wQd</xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="$square-number = 4">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="w">
							<xsl:attribute  name = "id" >wK<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<!-- black-->
				<xsl:when test="($square-number &gt; 47) and ($square-number &lt; 56) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="b">
							<xsl:attribute  name = "id" >bP<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="($square-number = 56) or ($square-number = 63) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="b">
							<xsl:attribute  name = "id" >bR<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="($square-number = 57) or ($square-number = 62) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="b">
							<xsl:attribute  name = "id" >bN<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
								
				<xsl:when test="($square-number = 58) or ($square-number = 61) ">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="b">
							<xsl:attribute  name = "id" >bB<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="$square-number = 59">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="b">
							<xsl:attribute  name = "id" >bQ<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>
				<xsl:when test="$square-number = 60">
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
						<piece player="b" >
							<xsl:attribute  name = "id" >bK<xsl:value-of select="$columnAlpha" /></xsl:attribute>
						</piece>
					</square>
				</xsl:when>			  
				<xsl:otherwise>
					<square>						
						<xsl:attribute  name = "id" ><xsl:value-of select="$square-number" /></xsl:attribute>
					</square>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:call-template name = "starting-square" >
				<xsl:with-param name="square-number" select="$square-number + 1" />
			</xsl:call-template>		
		</xsl:when>
	</xsl:choose>
</xsl:template>

<xsl:variable name = "check-list2">
<pieces>
	<itm ref="wP">
		<m step="7" cycle="0" rows="123456" cols="1234567" empty="0"/>
		<m step="9" cycle="0" rows="123456" cols="0123456" empty="0"/>
		<m step="8" cycle="0" rows="123456" cols="01234567" empty="1"/>
		<m step="16" cycle="0" rows="1" cols="01234567" empty="1"/>
	</itm>
	<itm ref="bP">
		<m step="-7" cycle="0" rows="123456" cols="0123456" empty="0"/>
		<m step="-9" cycle="0" rows="123456" cols="1234567" empty="0"/>
		<m step="-8" cycle="0" rows="123456" cols="01234567" empty="1"/>
		<m step="-16" cycle="0" rows="6" cols="01234567" empty="1"/>
	</itm>
	<itm ref="N">
		<m step="17" cycle="0" rows="012345" cols="0123456"/>
		<m step="10" cycle="0" rows="0123456" cols="012345"/>
		<m step="-6" cycle="0" rows="1234567" cols="012345"/>
		<m step="-15" cycle="0" rows="234567" cols="0123456"/>

		<m step="-17" cycle="0" rows="234567" cols="1234567"/>		
		<m step="-10" cycle="0" rows="1234567" cols="234567"/>
		<m step="6" cycle="0" rows="0123456" cols="234567"/>		
		<m step="15" cycle="0" rows="012345" cols="1234567"/>
	</itm>
	<itm ref="BQK">
		<m step="7" cycle="1" rows="0123456" cols="1234567"/>
		<m step="9" cycle="1" rows="0123456" cols="0123456"/>
		<m step="-7" cycle="1" rows="1234567" cols="0123456"/>
		<m step="-9" cycle="1" rows="1234567" cols="1234567"/>
	</itm>
	<itm ref="RQK">
		<m step="8" cycle="1" rows="0123456" cols="01234567"/>
		<m step="1" cycle="1" rows="01234567" cols="0123456"/>
		<m step="-8" cycle="1" rows="1234567" cols="01234567"/>
		<m step="-1" cycle="1" rows="01234567" cols="1234567"/>
	</itm>
	<itm ref="K">
		<m step="2" cycle="0" rows="07" cols="4" mode="castle"/>
		<m step="-2" cycle="0" rows="07" cols="4" mode="castle"/>
	</itm>

</pieces>
</xsl:variable>

</xsl:transform>