<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="year"/>
<xsl:param name="month"/>
<xsl:param name="firstday"/>
<xsl:param name="thisday"/>
<xsl:param name="previousmonth">0</xsl:param>
<xsl:param name="msg"/>
<xsl:param name="yearstr" select="'312931303130313130313031312831303130313130313031312831303130313130313031312831303130313130313031'"/>
<xsl:param name="hasdate"/>
<xsl:param name="dateday"/>
<xsl:param name="datemonth"/>
<xsl:param name="dateyear"/>
<xsl:param name="datefirstday"/>
<xsl:param name="firstyear">1999</xsl:param>
<xsl:param name="lastyear">2004</xsl:param>



<xsl:template match="/">
<TABLE class="containor"  border="0" cellspacing="1" cellpadding="0">
	<TR>
	<TD>
	Sun</TD>
	<TD>
	Mon</TD>
	<TD>
	Tue</TD>
	<TD>
	Wed</TD>
	<TD>
	Thu</TD>
	<TD>
	Fri</TD>
	<TD>
	Sat</TD>
	</TR>
			<xsl:call-template name="row-print"/>
	<TR>
		<TD class="msg" >
			<xsl:attribute name="colspan">7</xsl:attribute>
			<xsl:value-of select="$msg"/>
		</TD>
	</TR>
	</TABLE>
</xsl:template>



<xsl:variable name="is_leapyear"  select="$year mod 4" />

<xsl:variable name="days-in-month" 
	select="substring($yearstr,($is_leapyear * 24) + ($month + 1) * 2 - 1,2)"/>
<xsl:variable name="days-in-previousmonth" 
	select="substring($yearstr,($is_leapyear * 24) + ($previousmonth + 1) * 2 - 1,2)"/>

<xsl:variable name="previous-month-start" select="$days-in-month - $firstday + 1" />

<xsl:template name="format-date">
	<xsl:param name="iday"/>
	<xsl:variable name="days_of_week" select="'SunMonTueWedThuFriSat'"  />
	<xsl:variable name="months_in_year" select="'JanFebMarAprMayJunJulAugSepOctNovDec'"  />
	<xsl:variable name="days_to_col" select="(($iday + $datefirstday - 2) mod 7)"  />
	<xsl:value-of select="substring($days_of_week, $days_to_col * 3 + 1,3)"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="$iday"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="substring($months_in_year, $datemonth * 3 + 1,3)"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="$dateyear"/>
</xsl:template>

<xsl:template name="col-print">
	<xsl:param name="col" select="0"/>
	<xsl:param name="row"/>
	<xsl:variable name="cell" select="($row * 7) + $col + 1" />
	<xsl:if test="$col &lt; 7">
		<TD>			
			<xsl:choose>
				<xsl:when test="$cell &lt; $firstday">
					
							<xsl:choose>
								<xsl:when test="$month = 0 and $firstyear = $year">
								</xsl:when>
								<xsl:when test="$month = 0">
									<xsl:attribute name="class">pMonth</xsl:attribute>
									<xsl:attribute name="onclick">javascript:rtnDate('<xsl:value-of select="$cell + $days-in-previousmonth - $firstday + 1"/>','12','<xsl:value-of select="$year - 1"/>');</xsl:attribute>					
								</xsl:when>
								<xsl:otherwise>
									<xsl:attribute name="class">pMonth</xsl:attribute>
									<xsl:attribute name="onclick">javascript:rtnDate('<xsl:value-of select="$cell + $days-in-previousmonth - $firstday + 1"/>','<xsl:value-of select="$previousmonth + 1"/>','<xsl:value-of select="$year"/>');</xsl:attribute>					
								</xsl:otherwise>						
							</xsl:choose>
							
						
					<xsl:value-of select="$cell + $days-in-previousmonth - $firstday + 1"/>
				</xsl:when>
				
				
				
				<xsl:when test="($cell&gt; $firstday - 1) and ($cell - $firstday &lt; $days-in-month)">
					<xsl:attribute name="class">tMonth</xsl:attribute>
					<xsl:attribute name="onclick">javascript:rtnDate('<xsl:value-of select="$cell - $firstday + 1"/>','<xsl:value-of select="$month + 1"/>','<xsl:value-of select="$year"/>');</xsl:attribute>
					<xsl:if test="($cell - $firstday + 1 = $thisday) and ($thisday != 0)">
						<xsl:attribute name="class">tDay</xsl:attribute>					
					</xsl:if>
					<xsl:value-of select="$cell - $firstday + 1"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="$month = 11 and $lastyear = $year">
						</xsl:when>
						<xsl:when test="$month = 11">
							<xsl:attribute name="class">nMonth</xsl:attribute>
							<xsl:attribute name="onclick">rtnDate('<xsl:value-of select="$cell - $firstday - $days-in-month + 1"/>','1','<xsl:value-of select="$year + 1"/>');</xsl:attribute>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="class">nMonth</xsl:attribute>
							<xsl:attribute name="onclick">javascript:rtnDate('<xsl:value-of select="$cell - $firstday - $days-in-month + 1"/>','<xsl:value-of select="$month + 2"/>','<xsl:value-of select="$year"/>');</xsl:attribute>
						</xsl:otherwise>						
					</xsl:choose>
					<xsl:value-of select="$cell - $firstday - $days-in-month + 1"/>
				</xsl:otherwise>
				
			</xsl:choose>
		</TD>

	
		<xsl:call-template name="col-print">
			<xsl:with-param name="col" select="$col + 1"/>
			<xsl:with-param name="row" select="$row"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<xsl:template name="row-print">
	<xsl:param name="row" select="0"/>
	<xsl:if test="$row &lt; 6">
		<TR>
		<xsl:call-template name="col-print">
			<xsl:with-param name="row" select="$row"/>
		</xsl:call-template>

		</TR>
		<xsl:call-template name="row-print">
			<xsl:with-param name="row" select="$row + 1"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>



<xsl:template name="cell-print">
	<xsl:param name="day" select="2"/>
		<TD>
			<xsl:value-of select="$day"/>
		</TD>
		<xsl:if test="$day &lt; 31">
			<xsl:call-template name="cell-print">
				<xsl:with-param name="day" select="$day + 1"/>
			</xsl:call-template>
		</xsl:if>

</xsl:template>



</xsl:stylesheet>