<?xml version="1.0" encoding="UTF-8"?>
<!--

Stylesheet for text formatting templates
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:urn="urn:hl7-org:v3" xmlns:ext="urn:hl7-EE-DL-Ext:v1">
	<!--

Template for formatting date
	-->
	<xsl:template name="dateFormat">
		<xsl:param name="date"/>
		<xsl:param name="format"/>
		<xsl:if test="$date">
			<xsl:value-of select="substring($date,7,2)"/><xsl:if test="string-length($date)>6">.</xsl:if><xsl:value-of select="substring($date,5,2)"/>.<xsl:value-of select="substring($date,1,4)"/>
			<xsl:if test="($format='time' or $format='millis') and string-length($date) >= 12">
				<xsl:text> </xsl:text>
				<xsl:value-of select="substring($date,9,2)"/>:<xsl:value-of select="substring($date,11,2)"/>
				<xsl:if test="string-length($date) >= 14">:<xsl:value-of select="substring($date,13,2)"/></xsl:if>
			</xsl:if>
			<xsl:if test="string-length($date) >14">
				<xsl:value-of select="substring($date,15)"/>
			</xsl:if>
			<xsl:if test="$format='break'">
				<br/>
				<xsl:value-of select="substring($date,9,2)"/>:<xsl:value-of select="substring($date,11,2)"/>:<xsl:value-of select="substring($date,13,2)"/>
			</xsl:if>
		</xsl:if>
	</xsl:template>

	<!--

	Template for mapping date units to locale
	-->
	<xsl:template name="dateUnit">
		<xsl:param name="unit"/>
		<xsl:param name="mode"/>
		<xsl:param name="one"/>
		<xsl:choose>
			<xsl:when test="$mode='per'">
				<xsl:choose>
					<xsl:when test="$unit='a'">aastas</xsl:when>
					<xsl:when test="$unit='y'">aastas</xsl:when>
					<xsl:when test="$unit='k'">kuus</xsl:when>
					<xsl:when test="$unit='mo'">kuus</xsl:when>
					<xsl:when test="$unit='p'">päevas</xsl:when>
					<xsl:when test="$unit='d'">päevas</xsl:when>
					<xsl:when test="$unit='n'">nädalas</xsl:when>
					<xsl:when test="$unit='wk'">nädalas</xsl:when>
					<xsl:when test="$unit!='a' and $unit!='k' and $unit!='p' and $unit!='y' and $unit!='mo' and $unit!='d' and $unit!='wk'"><xsl:value-of select="$unit"/></xsl:when>
				</xsl:choose>
			</xsl:when>	
			<xsl:when test="$one='1'">
				<xsl:choose>
					<xsl:when test="$unit='a'">aasta</xsl:when>
					<xsl:when test="$unit='y'">aasta</xsl:when>
					<xsl:when test="$unit='k'">kuu</xsl:when>
					<xsl:when test="$unit='mo'">kuu</xsl:when>
					<xsl:when test="$unit='p'">päev</xsl:when>
					<xsl:when test="$unit='d'">päev</xsl:when>
					<xsl:when test="$unit='n'">nädal</xsl:when>
					<xsl:when test="$unit='wk'">nädal</xsl:when>
					<xsl:when test="$unit!='a' and $unit!='k' and $unit!='p' and $unit!='n' and $unit!='y' and $unit!='mo' and $unit!='d' and $unit!='wk'"><xsl:value-of select="$unit"/></xsl:when>
				</xsl:choose>
			</xsl:when>	
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="$unit='a'">aastat</xsl:when>
					<xsl:when test="$unit='y'">aastat</xsl:when>
					<xsl:when test="$unit='k'">kuud</xsl:when>
					<xsl:when test="$unit='mo'">kuud</xsl:when>
					<xsl:when test="$unit='p'">päeva</xsl:when>
					<xsl:when test="$unit='d'">päeva</xsl:when>
					<xsl:when test="$unit='n'">nädalat</xsl:when>
					<xsl:when test="$unit='wk'">nädalat</xsl:when>
					<xsl:when test="$unit!='a' and $unit!='k' and $unit!='p' and $unit!='n' and $unit!='y' and $unit!='mo' and $unit!='d' and $unit!='wk'"><xsl:value-of select="$unit"/></xsl:when>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!--

	Template for person name
	-->
	<xsl:template name="name" match="urn:name">
		<xsl:choose>
			<xsl:when test="urn:given or urn:family">
				<xsl:value-of select="urn:given"/>
					<xsl:if test="urn:given/text()">
						<xsl:text> </xsl:text>
					</xsl:if>
				<xsl:value-of select="urn:family"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

<!--

	Template for extPerson name
	-->
	<xsl:template name="extName" match="ext:name">
		<xsl:choose>
			<xsl:when test="ext:given or ext:family">
				<xsl:value-of select="ext:given"/>
					<xsl:if test="ext:given/text()">
						<xsl:text> </xsl:text>
					</xsl:if>
				<xsl:value-of select="ext:family"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!--

	Template for displaying urn:code - @displayName for urn:value
	-->
	<xsl:template name="value.formatting" match="urn:value" mode="formatting">
		<xsl:value-of select="@code"/>
		<xsl:text> - </xsl:text>
		<xsl:value-of select="@displayName"/>
	</xsl:template>

	<!--Template for displaying ext:code - @displayName for ext:value
	-->
	<xsl:template name="extvalue.formatting" match="ext:value" mode="formatting">
		<xsl:value-of select="@code"/>
		<xsl:text> - </xsl:text>
		<xsl:value-of select="@displayName"/>
	</xsl:template>


	<!--

	Template for displaying urn:extension - @assigningAuthorityName for urn:templateId
	-->
	<xsl:template name="templateId.formatting" match="urn:templateId" mode="formatting">
		<xsl:value-of select="@extension"/>
		<xsl:text> - </xsl:text>
		<xsl:value-of select="@assigningAuthorityName"/>
	</xsl:template>
	<!--

	Template for displaying urn:code - @displayName pairs for urn:code
	optionally codeSystemName will be shown if param $showCodeSystemName is true
	-->
	<xsl:template name="code.formatting" match="urn:code" mode="formatting">
		<xsl:param name="showCodeSystemName"/>
		<xsl:if test="$showCodeSystemName">
			<xsl:value-of select="@codeSystemName"/>
			<xsl:text> </xsl:text>
		</xsl:if>
		<xsl:value-of select="@code"/>
		<xsl:if test="@code">
			<xsl:text> - </xsl:text>
		</xsl:if>
		<xsl:value-of select="@displayName"/>
	</xsl:template>
	
	<!--
	Template for displaying urn:translation 
	optionally codeSystemName will be shown if param $showCodeSystemName is true
	-->
	<xsl:template name="translation.formatting" match="urn:translation" mode="formatting">
		<xsl:param name="showCodeSystemName"/>
		<xsl:value-of select="@code"/>
		<xsl:if test="@code">
			<xsl:text> - </xsl:text>
		</xsl:if>
		<xsl:value-of select="@displayName"/>
		<xsl:if test="$showCodeSystemName">
			<xsl:text> (</xsl:text>
			<xsl:value-of select="@codeSystemName"/>
			<xsl:text>)</xsl:text>
		</xsl:if>
	</xsl:template>


	<!--
	Template for displaying urn:value as diagnose 
	-->
	<xsl:template name="diagnose.formatting" match="urn:value" mode="diagnoses">
	<table>
		<tr>
			<td class="rowTitle">Kood:</td>
			<td class="rowValue">
				<xsl:value-of select="@code"/>
				<xsl:text> - </xsl:text>
				<xsl:value-of select="@displayName"/>
			</td>
		</tr>	
		<tr>
			<td class="rowTitle">Arsti sõnaline diagnoos:</td>
			<td class="rowValue">
				<xsl:value-of select="urn:originalText"/>
			</td>	
		</tr>	
		<xsl:if test="urn:qualifier/urn:value/@displayName">
		<tr>
			<td class="rowTitle">Statistiline liik:</td>
			<td class="rowValue">
				<xsl:value-of select="urn:qualifier/urn:value/@displayName"/>
			</td>
		</tr>		
		</xsl:if>	
		<xsl:if test="../urn:interpretationCode/@displayName">
		<tr>
			<td class="rowTitle">Liik:</td>
			<td class="rowValue">
				<xsl:value-of select="../urn:interpretationCode/@displayName"/>
			</td>	
		</tr>	
		</xsl:if>	
	</table>
	</xsl:template>


<!--
	Template for address info
	-->
	<xsl:template name="addr" match="urn:addr">
		<xsl:if test="urn:streetAddressLine/text()">
			<xsl:value-of select="urn:streetAddressLine"/>
			<xsl:if test="urn:city or urn:county or urn:postalCode or urn:direction or urn:country">
				<xsl:text>, </xsl:text>
			</xsl:if>	
		</xsl:if>
		<xsl:if test="urn:streetName/text()">
			<xsl:value-of select="urn:streetName"/>
			<xsl:if test="urn:houseNumber">
			<xsl:text> </xsl:text>
			</xsl:if>
			<xsl:value-of select="urn:houseNumber"/>	
			<xsl:choose>
				<xsl:when test="urn:additionalLocator">
					<xsl:text>-</xsl:text>
				</xsl:when>
				<xsl:otherwise>				
					<xsl:text>, </xsl:text>
				</xsl:otherwise>
			</xsl:choose>	
		</xsl:if>
		<xsl:if test="urn:additionalLocator/text()">
			<xsl:value-of select="urn:additionalLocator"/>
			<xsl:if test="urn:city or urn:county or urn:postalCode or urn:direction or urn:country or urn:censusTract">
				<xsl:text>, </xsl:text>
			</xsl:if>	
		</xsl:if>
		<xsl:if test="urn:censusTract/text()">
			<xsl:value-of select="urn:censusTract"/>
				<xsl:if test="urn:city or urn:county or urn:postalCode or urn:direction or urn:country">
				<xsl:text>, </xsl:text>
			</xsl:if>
		</xsl:if>
		<xsl:if test="urn:city/text()">
			<xsl:value-of select="urn:city"/>
			<xsl:if test="urn:county or urn:postalCode or urn:direction or urn:country">
				<xsl:text>, </xsl:text>
			</xsl:if>
		</xsl:if>
		<xsl:if test="urn:postalCode/text()">
		<xsl:value-of select="urn:postalCode"/>
			<xsl:choose>
				<xsl:when test="urn:county">
					<xsl:text> </xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>, </xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
		<xsl:if test="urn:county">
			<xsl:value-of select="urn:county"/>
				<xsl:if test="urn:direction or urn:country">
					<xsl:text>, </xsl:text>
				</xsl:if>	
		</xsl:if>
		<xsl:if test="urn:direction/text()">
			<xsl:text>  </xsl:text>
			<span class="rowTitle">GPS koordinaadid:</span>
			<xsl:text>  </xsl:text>
			<xsl:value-of select="urn:direction/text()"/>
				<xsl:if test="urn:country">
					<xsl:text>, </xsl:text>
				</xsl:if>
		</xsl:if>
		<xsl:if test="urn:country">
			<xsl:choose>
				<xsl:when test="urn:country='EST'">
					<xsl:text>Eesti</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="urn:country"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	
	<!--
	Template for address info
	-->
	<xsl:template name="extaddr" match="ext:addr">
		<xsl:if test="ext:streetAddressLine/text()">
			<xsl:value-of select="ext:streetAddressLine"/>
			<xsl:if test="ext:city or ext:county or ext:postalCode or ext:direction or ext:country">
				<xsl:text>, </xsl:text>
			</xsl:if>	
		</xsl:if>
		<xsl:if test="ext:streetName/text()">
			<xsl:value-of select="ext:streetName"/>
			<xsl:if test="ext:houseNumber">
			<xsl:text> </xsl:text>
			</xsl:if>
			<xsl:value-of select="ext:houseNumber"/>	
			<xsl:choose>
				<xsl:when test="ext:additionalLocator">
					<xsl:text>-</xsl:text>
				</xsl:when>
				<xsl:otherwise>				
					<xsl:text>, </xsl:text>
				</xsl:otherwise>
			</xsl:choose>	
		</xsl:if>
		<xsl:if test="ext:additionalLocator/text()">
			<xsl:value-of select="ext:additionalLocator"/>
			<xsl:if test="ext:city or ext:county or ext:postalCode or ext:direction or ext:country or ext:censusTrac">
				<xsl:text>, </xsl:text>
			</xsl:if>	
		</xsl:if>
		<xsl:if test="ext:censusTract/text()">
			<xsl:value-of select="ext:censusTract"/>
			<xsl:if test="ext:city or ext:county or ext:postalCode or ext:direction or ext:country">
				<xsl:text>, </xsl:text>
			</xsl:if>
		</xsl:if>
		<xsl:if test="ext:city/text()">
			<xsl:value-of select="ext:city"/>
			<xsl:if test="ext:county or ext:postalCode or ext:direction or ext:country">
				<xsl:text>, </xsl:text>
			</xsl:if>
		</xsl:if>
		<xsl:if test="ext:postalCode/text()">
		<xsl:value-of select="ext:postalCode"/>
			<xsl:choose>
				<xsl:when test="ext:county">
					<xsl:text> </xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>, </xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
		<xsl:if test="ext:county">
			<xsl:value-of select="ext:county"/>
				<xsl:if test="ext:direction or ext:country">
					<xsl:text>, </xsl:text>
				</xsl:if>	
		</xsl:if>
		<xsl:if test="ext:direction/text()">
			<xsl:text>  </xsl:text>
			<span class="rowTitle">GPS koordinaadid:</span>
			<xsl:text>  </xsl:text>
			<xsl:value-of select="ext:direction/text()"/>
				<xsl:if test="ext:country">
					<xsl:text>, </xsl:text>
				</xsl:if>
		</xsl:if>
		<xsl:if test="ext:country">
			<xsl:choose>
				<xsl:when test="ext:country='EST'">
					<xsl:text>Eesti</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="ext:country"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	
	<!--

	Template for telecomInfo company info
	-->
	<xsl:template name="telecomFormat">
		<table>
			<tr>
				<xsl:if test="urn:telecom[substring(@value,1,4)='tel:']">
					<td class="rowTitle">Telefon</td>
					<td class="rowValue">
						<table>
							<xsl:for-each select="urn:telecom[substring(@value,1,4)='tel:']">
								<tr>
									<td>
										<xsl:value-of select="substring(@value, 5)"/>
									</td>
								</tr>
							</xsl:for-each>
						</table>
					</td>
				</xsl:if>
				<xsl:if test="urn:telecom[substring(@value,1,7)='mailto:']">
					<td class="rowTitle">E-post</td>
					<td class="rowValue">
						<table>
							<xsl:for-each select="urn:telecom[substring(@value,1,7)='mailto:']">
								<tr>
									<td>
										<xsl:value-of select="substring(@value, 8)"/>
									</td>
								</tr>
							</xsl:for-each>
						</table>
					</td>
				</xsl:if>
				<xsl:if test="urn:telecom[substring(@value,1,4)='fax:']">
					<td class="rowTitle">Faks</td>
					<td class="rowValue">
						<table>
							<xsl:for-each select="urn:telecom[substring(@value,1,4)='fax:']">
								<tr>
									<td>
										<xsl:value-of select="substring(@value, 5)"/>
									</td>
								</tr>
							</xsl:for-each>
						</table>
					</td>
				</xsl:if>
			</tr>
		</table>
	</xsl:template>	

<!--

	Template for telecomInfo company info
	-->
	<xsl:template name="telecomCellFormat">
		<table>
			<tr>
				<xsl:if test="urn:telecom[substring(@value,1,4)='tel:']">
					<td class="rowTitle">Telefon</td>
					<td class="rowValue">
						<table>
							<xsl:for-each select="urn:telecom[substring(@value,1,4)='tel:']">
								<tr>
									<td>
										<xsl:value-of select="substring(@value, 5)"/>
									</td>
								</tr>
							</xsl:for-each>
						</table>
					</td>
				</xsl:if>
			</tr>
			<tr>
				<xsl:if test="urn:telecom[substring(@value,1,7)='mailto:']">
					<td class="rowTitle">E-post</td>
					<td class="rowValue">
						<table>
							<xsl:for-each select="urn:telecom[substring(@value,1,7)='mailto:']">
								<tr>
									<td>
										<xsl:value-of select="substring(@value, 8)"/>
									</td>
								</tr>
							</xsl:for-each>
						</table>
					</td>
				</xsl:if>
			</tr>	
			<tr>
				<xsl:if test="urn:telecom[substring(@value,1,4)='fax:']">
					<td class="rowTitle">Faks</td>
					<td class="rowValue">
						<table>
							<xsl:for-each select="urn:telecom[substring(@value,1,4)='fax:']">
								<tr>
									<td>
										<xsl:value-of select="substring(@value, 5)"/>
									</td>
								</tr>
							</xsl:for-each>
						</table>
					</td>
				</xsl:if>
			</tr>
		</table>
	</xsl:template>		
	
</xsl:stylesheet>

