<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:trend="http://trendmicro.com/smex">
	<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

	<xsl:decimal-format name="us" 		decimal-separator='.' grouping-separator=',' />
	<xsl:decimal-format name="european" decimal-separator=',' grouping-separator='.' />

	<xsl:variable name="dec-format"		select="document('localization.xml')/strings/group[@name='report']/decimal-format"/>
	<xsl:variable name="date-format"	select="document('localization.xml')/strings/group[@name='report']/date-format"/>
	<xsl:variable name="time-format"	select="document('localization.xml')/strings/group[@name='report']/time-format"/>
	<xsl:variable name="hour-format"	select="document('localization.xml')/strings/group[@name='report']/hour-format"/>

<!--|| TEMPLATE : generic number formation -->
	<xsl:template name="format_number">
		<xsl:param name="num"/>
		<xsl:variable name="num_">
			<xsl:choose>
				<xsl:when test="not(boolean(number($num)))">0</xsl:when>
				<xsl:otherwise><xsl:value-of select="$num"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="format-number($num_, '###,##0.####', $dec-format)"/>
	</xsl:template>


<!--|| TEMPLATE : integer number formation -->
	<xsl:template name="format_int_number">
		<xsl:param name="num"/>
		<xsl:variable name="num_">
			<xsl:choose>
				<xsl:when test="not(boolean(number($num)))">0</xsl:when>
				<xsl:otherwise><xsl:value-of select="$num"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="format-number($num_, '###,##0', $dec-format)"/>
	</xsl:template>


<!--|| TEMPLATE : float number formation -->
	<xsl:template name="format_float_number">
		<xsl:param name="num"/>
		<xsl:variable name="num_">
			<xsl:choose>
				<xsl:when test="not(boolean(number($num)))">0</xsl:when>
				<xsl:otherwise><xsl:value-of select="$num"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="format-number($num_, '###,##0.####', $dec-format)"/>
	</xsl:template>


<!--|| TEMPLATE : make percentage -->
	<xsl:template name="format_percentage">
		<xsl:param name="numerator"/>
		<xsl:param name="denominator"/>
		<xsl:variable name="output-number">
			<xsl:choose>
				<xsl:when test="not(boolean(number($numerator))) or ($numerator = 0)">0</xsl:when>
				<xsl:when test="not(boolean(number($denominator)))">0</xsl:when>
				<xsl:otherwise><xsl:value-of select="number($numerator) div number($denominator)"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="format-number($output-number, '###,##0.##%', $dec-format)"/>
	</xsl:template>


<!--|| DATETIME SCRIPT FUNCTION -->
	<msxsl:script language="JScript" implements-prefix="trend"><![CDATA[
		function GetLocaleDateTimeString(datetime_string, part)
		{
			var oDate = new Date(datetime_string);
			if (part == 'to_datetime')
				return oDate.toLocaleString();
			else if (part == 'to_date')
			{
				var sLocaleDateString = "";
				try
				{
					sLocaleDateString = oDate.toLocaleDateString();
				}
				catch (e)
				{
				}
				return sLocaleDateString;
			}
			else if (part == 'to_time')
			{
				var sLocaleTimeString = "";
				try
				{
					sLocaleTimeString = oDate.toLocaleTimeString();
				}
				catch (e)
				{
				}
				return sLocaleTimeString;
			}
			else
				return "";
		}
	//]]>
	</msxsl:script>

<!--|| TEMPLATE: Datetime formation -->
	<xsl:template name="format_datetime_string" xmlns:ms="urn:schemas-microsoft-com:xslt">
		<xsl:param name="datetime_string"/>
		<xsl:variable name="datetime_string_normalize" select="translate($datetime_string, '-', '/')"/>
		<xsl:value-of select="trend:GetLocaleDateTimeString($datetime_string_normalize, 'to_datetime')"/>
	</xsl:template>

	<xsl:template name="format_datehour_string" xmlns:ms="urn:schemas-microsoft-com:xslt">
		<xsl:param name="datetime_string"/>
		<xsl:variable name="xsd_datetime_string" select="translate($datetime_string, ' ', 'T')"/>
		<xsl:call-template name="format_date_string">
			<xsl:with-param name="datetime_string" select="$datetime_string"/>
		</xsl:call-template>
		<xsl:text> </xsl:text>
		<xsl:value-of select="ms:format-time($xsd_datetime_string, $hour-format)"/>:00
	</xsl:template>

	<xsl:template name="format_date_string" xmlns:ms="urn:schemas-microsoft-com:xslt">
		<xsl:param name="datetime_string"/>
		<xsl:variable name="datetime_string_normalize" select="translate($datetime_string, '-', '/')"/>
		<xsl:variable name="datetime_string_localized" select="trend:GetLocaleDateTimeString($datetime_string_normalize, 'to_date')"/>
		<xsl:choose>
			<xsl:when test="$datetime_string_localized=''">
				<xsl:variable name="xsd_datetime_string" select="translate($datetime_string, ' ', 'T')"/>
				<xsl:value-of select="ms:format-date($xsd_datetime_string, $date-format)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$datetime_string_localized"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="format_time_string" xmlns:ms="urn:schemas-microsoft-com:xslt">
		<xsl:param name="datetime_string"/>
		<xsl:variable name="datetime_string_normalize" select="translate($datetime_string, '-', '/')"/>
		<xsl:variable name="datetime_string_localized" select="trend:GetLocaleDateTimeString($datetime_string_normalize, 'to_time')"/>
		<xsl:choose>
			<xsl:when test="$datetime_string_localized=''">
				<xsl:variable name="xsd_datetime_string" select="translate($datetime_string, ' ', 'T')"/>
				<xsl:value-of select="ms:format-time($xsd_datetime_string, $time-format)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$datetime_string_localized"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
