<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="PAGED-LIST/PAGER">
<script language="javascript">
function pagedListGotoPage(tab, pageNum) {
	var formName = '<xsl:value-of select="../@FORM-NAME"/>';
	var form = document.forms[formName];
	form.tab.value = tab;
	form.pageNum.value = pageNum;
	form.submit();
}
function pagedListSetPageSize(tab, pageSize) {
	var formName = '<xsl:value-of select="../@FORM-NAME"/>';
	var form = document.forms[formName];
	form.tab.value = tab;
	form.pageNum.value = 1;
	form.pageSize.value = pageSize;
	form.submit();
}
</script>
<xsl:call-template name="LIST-PAGER-LINE">
	<xsl:with-param name="totalRecords"><xsl:value-of select="TOTAL-RECORDS"/></xsl:with-param>
	<xsl:with-param name="currentPageNum"><xsl:value-of select="CURRENT-PAGE"/></xsl:with-param>
	<xsl:with-param name="recordsPerPage"><xsl:value-of select="RECORDS-PER-PAGE"/></xsl:with-param>
	<xsl:with-param name="pageLimit"><xsl:value-of select="PAGE-LIMIT"/></xsl:with-param>
	
	<xsl:with-param name="tab"><xsl:value-of select="ancestor::TAB-PANEL-CARD/@ID"/></xsl:with-param>

	<xsl:with-param name="msgTotal"><xsl:value-of select="MESSAGES/TOTAL"/></xsl:with-param>

	<xsl:with-param name="msgFirstPage"><xsl:value-of select="MESSAGES/FIRST-PAGE"/></xsl:with-param>
	<xsl:with-param name="msgLastPage"><xsl:value-of select="MESSAGES/LAST-PAGE"/></xsl:with-param>

	<xsl:with-param name="msgPageOf"><xsl:value-of select="MESSAGES/PAGE-OF"/></xsl:with-param>
	<xsl:with-param name="msgRecordsPerPage"><xsl:value-of select="MESSAGES/RECORDS-PER-PAGE"/></xsl:with-param>
</xsl:call-template>
</xsl:template>

<!--
	Draw top\bottom line for list objects (with pager total records etc)
	totalRecords - common records in ALL list
	currentPageNum - number of selected page 
	recordsPerPage - 10, 25, 100
-->
<xsl:template name="LIST-PAGER-LINE">
<xsl:param name="totalRecords"/>
<xsl:param name="currentPageNum"/>
<xsl:param name="recordsPerPage"/>
<xsl:param name="pageLimit"/>
<xsl:param name="tab"/>

<xsl:param name="msgFirstPage"/>
<xsl:param name="msgLastPage"/>
<xsl:param name="msgPageOf"/>
<xsl:param name="msgTotal"/>
<xsl:param name="msgRecordsPerPage"/>

<xsl:param name="countPages" select="round($totalRecords div $recordsPerPage + 0.4999)"/>

<table width="100%" cellspacing="0" cellpadding="5" border="0" style="border-collapse:collapse;">
<tr>
	<td align="left" valign="middle" style="width:33%;white-space:nowrap;">
		<xsl:value-of select="$totalRecords"/> &#160; <xsl:value-of select="$msgTotal"/>
	</td>
	<td align="center" valign="middle" style="width:33%;white-space:nowrap;">
		<xsl:if test="$countPages &gt; 1">
			<xsl:call-template name="PAGER">
				<xsl:with-param name="currentPageNum"><xsl:value-of select="$currentPageNum"/></xsl:with-param>
				<xsl:with-param name="countPages"><xsl:value-of select="$countPages"/></xsl:with-param>
				<xsl:with-param name="pageLimit"><xsl:value-of select="$pageLimit"/></xsl:with-param>
				<xsl:with-param name="tab"><xsl:value-of select="$tab"/></xsl:with-param>

				<xsl:with-param name="msgFirstPage"><xsl:value-of select="$msgFirstPage"/></xsl:with-param>
				<xsl:with-param name="msgLastPage"><xsl:value-of select="$msgLastPage"/></xsl:with-param>
				<xsl:with-param name="msgPageOf"><xsl:value-of select="$msgPageOf"/></xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</td>
	<td align="right" valign="middle" style="width:33%;white-space:nowrap;">
		<xsl:value-of select="$msgRecordsPerPage"/>
		&#160;
		<xsl:call-template name="PER-PAGE-ITEM">
			<xsl:with-param name="itemValue">5</xsl:with-param>
			<xsl:with-param name="currentValue"><xsl:value-of select="$recordsPerPage"/></xsl:with-param>
			<xsl:with-param name="tab"><xsl:value-of select="$tab"/></xsl:with-param>
		</xsl:call-template>
		&#160;
		<xsl:call-template name="PER-PAGE-ITEM">
			<xsl:with-param name="itemValue">10</xsl:with-param>
			<xsl:with-param name="currentValue"><xsl:value-of select="$recordsPerPage"/></xsl:with-param>
			<xsl:with-param name="tab"><xsl:value-of select="$tab"/></xsl:with-param>
		</xsl:call-template>
		&#160;
		<xsl:call-template name="PER-PAGE-ITEM">
			<xsl:with-param name="itemValue">25</xsl:with-param>
			<xsl:with-param name="currentValue"><xsl:value-of select="$recordsPerPage"/></xsl:with-param>
			<xsl:with-param name="tab"><xsl:value-of select="$tab"/></xsl:with-param>
		</xsl:call-template>
		&#160;
		<xsl:call-template name="PER-PAGE-ITEM">
			<xsl:with-param name="itemValue">100</xsl:with-param>
			<xsl:with-param name="currentValue"><xsl:value-of select="$recordsPerPage"/></xsl:with-param>
			<xsl:with-param name="tab"><xsl:value-of select="$tab"/></xsl:with-param>
		</xsl:call-template>
	</td>
</tr>
</table>
</xsl:template>


<xsl:template name="PER-PAGE-ITEM">
	<xsl:param name="itemValue"/>
	<xsl:param name="currentValue"/>
	<xsl:param name="tab"/>
	<xsl:param name="itemValue"/>
	<xsl:choose>
		<xsl:when test="$itemValue = $currentValue">
			<xsl:value-of select="$itemValue"/>
		</xsl:when>
		<xsl:otherwise>
			<a href="javascript:pagedListSetPageSize('{$tab}',{$itemValue});" class="sb-pager-link"><xsl:value-of select="$itemValue"/></a>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<!--
	Draw pager
	currentPageNum - number of selected page 
	countPages - count pages
	pageLimit - how much itesm of pages we should to diplay
-->
<xsl:template name="PAGER">
<xsl:param name="currentPageNum"/>
<xsl:param name="countPages"/>
<xsl:param name="pageLimit"/>
<xsl:param name="tab"/>

<xsl:param name="msgFirstPage"/>
<xsl:param name="msgLastPage"/>
<xsl:param name="msgPageOf"/>

<xsl:if test="$countPages &gt; 0">
<table cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
<tr>
	<xsl:variable name="v1">
		<xsl:choose>
			<xsl:when test="round($currentPageNum - $pageLimit div 2) &gt; 1">
				<xsl:value-of select="round($currentPageNum - $pageLimit div 2)"/>
			</xsl:when>
			<xsl:otherwise>
				1
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="v2">
		<xsl:choose>
			<xsl:when test="$v1 + $pageLimit &gt; $countPages">
				<xsl:value-of select="$countPages - $pageLimit + 1"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$v1"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="startWith">
		<xsl:choose>
			<xsl:when test="$v2 &gt; 0">
				<xsl:value-of select="$v2"/>
			</xsl:when>
			<xsl:otherwise>
				1
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<td style="white-space:nowrap;padding-left:3px;padding-right:13px;">
		<!-- Strreplace made by call template not so good but  it works -->
		<xsl:variable name="tmp">
			<xsl:call-template name="SEARCH-AND-REPLACE">
				<xsl:with-param name="string"><xsl:value-of select="$msgPageOf"/></xsl:with-param>
				<xsl:with-param name="searchFor">%0</xsl:with-param>
				<xsl:with-param name="replaceWith"><xsl:value-of select="$currentPageNum"/></xsl:with-param>
			</xsl:call-template>
		</xsl:variable>
		<xsl:call-template name="SEARCH-AND-REPLACE">
			<xsl:with-param name="string"><xsl:value-of select="$tmp"/></xsl:with-param>
			<xsl:with-param name="searchFor">%1</xsl:with-param>
			<xsl:with-param name="replaceWith"><xsl:value-of select="$countPages"/></xsl:with-param>
		</xsl:call-template>
	</td>

	<xsl:if test="$currentPageNum != 1">
		<td style="white-space:nowrap;padding-left:3px;padding-right:3px;">
			<a href="javascript:pagedListGotoPage('{$tab}',1);" class="sb-pager-link"><xsl:value-of select="$msgFirstPage"/></a>
		</td>
	</xsl:if>

		<xsl:call-template name="PAGER-ITEM">
			<xsl:with-param name="currentPageNum"><xsl:value-of select="$currentPageNum"/></xsl:with-param>
			<xsl:with-param name="pageNum"><xsl:value-of select="$startWith + 0"/></xsl:with-param>
			<xsl:with-param name="pageCounter">0</xsl:with-param>
			<xsl:with-param name="pageLimit">
				<xsl:choose>
					<xsl:when test="$pageLimit &lt; $countPages"><xsl:value-of select="$pageLimit"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="$countPages"/></xsl:otherwise>
				</xsl:choose>
			</xsl:with-param>
			<xsl:with-param name="tab"><xsl:value-of select="$tab"/></xsl:with-param>
		</xsl:call-template>

	<xsl:if test="$currentPageNum != $countPages">
		<td style="white-space:nowrap;padding-left:3px;padding-right:3px;">
			<a href="javascript:pagedListGotoPage('{$tab}',{$countPages});" class="sb-pager-link"><xsl:value-of select="$msgLastPage"/></a>
		</td>
	</xsl:if>
</tr>
</table>
</xsl:if>
</xsl:template>


<!--
	Recursive draw pager item
-->
<xsl:template name="PAGER-ITEM">
<xsl:param name="currentPageNum"/>
<xsl:param name="pageNum"/>
<xsl:param name="pageCounter"/>
<xsl:param name="pageLimit"/>
<xsl:param name="tab"/>

<xsl:if test="$pageCounter &lt; $pageLimit">
	<td style="white-space:nowrap;padding-left:3px;padding-right:3px;">
			<xsl:choose>
				<xsl:when test="$pageNum + 0 = $currentPageNum">
					<a disabled="true" class="sb-pager-current">
						<xsl:value-of select="$pageNum"/>
					</a>
					</xsl:when>
				<xsl:otherwise>
					<a href="javascript:pagedListGotoPage('{$tab}',{$pageNum + 0});" class="sb-pager-link">
						<xsl:value-of select="$pageNum"/>
					</a>
					</xsl:otherwise>
			</xsl:choose>
	</td>
	<xsl:call-template name="PAGER-ITEM">
		<xsl:with-param name="currentPageNum"><xsl:value-of select="$currentPageNum"/></xsl:with-param>
		<xsl:with-param name="pageNum"><xsl:value-of select="$pageNum + 1"/></xsl:with-param>
		<xsl:with-param name="pageCounter"><xsl:value-of select="$pageCounter + 1"/></xsl:with-param>
		<xsl:with-param name="pageLimit"><xsl:value-of select="$pageLimit"/></xsl:with-param>
		<xsl:with-param name="tab"><xsl:value-of select="$tab"/></xsl:with-param>
	</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template name="SEARCH-AND-REPLACE">
	<xsl:param name="string"/>
	<xsl:param name="searchFor"/>
	<xsl:param name="replaceWith"/>
	<xsl:choose>
		<xsl:when test="$searchFor">
			<xsl:choose>
				<xsl:when test="contains($string,$searchFor)">
					<xsl:value-of select="substring-before($string,$searchFor)"/>
					<xsl:value-of select="$replaceWith"/>
					<xsl:call-template name="SEARCH-AND-REPLACE">
						<xsl:with-param
							name="string"
							select="substring-after($string,$searchFor)"/>
						<xsl:with-param name="searchFor" select="$searchFor"/>
						<xsl:with-param name="replaceWith" select="$replaceWith"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$string"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$string"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>