﻿<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

	<!-- external references -->
	<xsl:variable name="field_name_list"	    		select="document('field_list.xml')//field_list/field"/>
	<xsl:variable name="action_description_list"		select="document('action_descriptions.xml')//action_description/action"/>
	<xsl:variable name="virus_encyclopedia_link"		select="document('localization.xml')/strings/group[@name='log']/string[@name='virus_encyclopedia_link']"/>
	<xsl:variable name="virus_name_over_restriction"	select="document('localization.xml')/strings/group[@name='log']/string[@name='virus_name_over_restriction']"/>
	<xsl:variable name="virus_name_password_protected"	select="document('localization.xml')/strings/group[@name='log']/string[@name='virus_name_password_protected']"/>
	<xsl:variable name="log_print"      select="document('pr_status.xml')/pr/log_print"/>
	<xsl:variable name="event_type_list"   select="document('event_tracking.xml')//event_tracking/event_type"/>
    <xsl:variable name="source_type_list"  select="document('event_tracking.xml')//event_tracking/source_type"/>
	<!-- internal settings -->
	<xsl:variable name="virus_name_over_restriction_internal"	>over restriction</xsl:variable>
	<xsl:variable name="virus_name_password_protected_internal"	>protected file</xsl:variable>


	<!-- ||TEMPLATE: Main iteration -->
	<xsl:template match="/">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<!-- header fields -->
		<tr>
			<xsl:for-each select="//record[1]/@*">
				<xsl:variable name="current_field_tag" select="name(.)"></xsl:variable>
				<xsl:variable name="current_field_name"	select="$field_name_list[@name=$current_field_tag]"></xsl:variable>
                
                <!-- Delete unwanted column for PP 1.8 by Joy Zhou -->  
          		<xsl:choose>
                <xsl:when test="$current_field_tag='storage_resend_count'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_delivery_time'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_destination'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_subject'"></xsl:when>
                <!-- Add Number column for PP 1.8 by Joy Zhou -->  
                <xsl:when test="$current_field_tag = 'storage_entry_id'">
                <td>
                <xsl:attribute name="nowrap">
                	nowrap
              	</xsl:attribute>
              	<xsl:attribute name="class">
                	grey2<xsl:if test="position()=last()">-end</xsl:if>
              	</xsl:attribute>
            	<xsl:value-of select="$field_name_list[@name='storage_entry_id']"/>
                </td>
		</xsl:when>
                
                <!-- Show Date&Time column for PP 1.8 by Joy Zhou -->  
                <xsl:when test="$current_field_tag = 'activity_time'">
                <td>
                <xsl:attribute name="nowrap">
                	nowrap
              	</xsl:attribute>
              	<xsl:attribute name="class">
                	grey2<xsl:if test="position()=last()">-end</xsl:if>
              	</xsl:attribute>
                <xsl:value-of select="$field_name_list[@name='storage_entry_id']"/>
                </td>
                <td>
                <xsl:attribute name="nowrap">
                	nowrap
              	</xsl:attribute>
              	<xsl:attribute name="class">
                	grey2<xsl:if test="position()=last()">-end</xsl:if>
              	</xsl:attribute>
                <xsl:value-of select="$field_name_list[@name='activity_time']"/>
                </td>
		</xsl:when>
                
                <xsl:otherwise>
         		<td>
				<xsl:attribute name="nowrap">
					nowrap
				</xsl:attribute>
				<xsl:attribute name="class">
					grey2<xsl:if test="position()=last()">-end</xsl:if>
				</xsl:attribute>
				<xsl:attribute name="id">
				<xsl:value-of select="$current_field_tag"/>
				</xsl:attribute>
				<xsl:if test="$log_print != '1'">
					<xsl:if test="$current_field_tag != 'activity_description'">
					<a>
					<xsl:attribute name="href">
						javascript: jsToggleSort("<xsl:value-of select="$current_field_tag"/>")
					</xsl:attribute>
					<xsl:choose>
					<xsl:when test="$current_field_name"><xsl:value-of select="$current_field_name"/></xsl:when>
					<xsl:otherwise><font color="red"><xsl:value-of select="$current_field_tag"/></font></xsl:otherwise>	
					</xsl:choose>
					</a>
					</xsl:if>
					<xsl:if test="$current_field_tag = 'activity_description'">
					<xsl:choose>
					<xsl:when test="$current_field_name"><xsl:value-of select="$current_field_name"/></xsl:when>
					<xsl:otherwise><font color="red"><xsl:value-of select="$current_field_tag"/></font></xsl:otherwise>	
					</xsl:choose>
					</xsl:if>
				</xsl:if>
				<xsl:if test="$log_print = '1'">
					<xsl:choose>
					<xsl:when test="$current_field_name"><xsl:value-of select="$current_field_name"/></xsl:when>
					<xsl:otherwise><font color="red"><xsl:value-of select="$current_field_tag"/></font></xsl:otherwise>	
					</xsl:choose>
				</xsl:if>
				</td>
			 
            
           
            </xsl:otherwise>
        	</xsl:choose>
              
		</xsl:for-each>	
		</tr>
	<!--/ heder fields -->
    
	<!-- content fields -->
		<xsl:for-each select="//record">
			<xsl:variable name="toggle"	select="boolean(position() mod 2)"/>
            <xsl:variable name="row_num" select="position()"/>
			<tr>
			<xsl:for-each select="@*">
		  	<xsl:variable name="current_field_tag" select="name(.)"></xsl:variable>
				<xsl:variable name="islast" select="boolean(position() = last())"/>
				<xsl:variable name="current_value" select="."></xsl:variable>
				<xsl:variable name="col_style">
					<xsl:choose>
						<xsl:when test="$toggle">data1</xsl:when>
						<xsl:otherwise>data2</xsl:otherwise>
					</xsl:choose>
					<xsl:if test="position()!=last()">b</xsl:if>
				</xsl:variable>
                
                <xsl:choose>
                <xsl:when test="$current_field_tag='storage_resend_count'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_delivery_time'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_destination'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_subject'"></xsl:when>
                <xsl:when test="$current_field_tag = 'activity_time'">
					<td>
                    <xsl:attribute name="class"><xsl:value-of select="$col_style"/></xsl:attribute>
                    <!--<center>-->
				 	<xsl:value-of select="$row_num"/> 
                    <!--</center>-->
					</td>
                    <td>
				    <xsl:attribute name="class"><xsl:value-of select="$col_style"/></xsl:attribute>
					<xsl:apply-templates select="."/>
					</td>
				</xsl:when>
                <xsl:when test="$current_field_tag = 'storage_entry_id'">
					<td>
                    <xsl:attribute name="class"><xsl:value-of select="$col_style"/></xsl:attribute>
                    <!--<center>-->
				 	<xsl:value-of select="$row_num"/> 
                    <!--</center>-->
					</td>
                </xsl:when>
                <xsl:otherwise>
				 	<td>
					<xsl:attribute name="class"><xsl:value-of select="$col_style"/></xsl:attribute>
					<xsl:apply-templates select="."/>
					</td>
			 	</xsl:otherwise>
        		</xsl:choose>

			</xsl:for-each>
			</tr>
		</xsl:for-each>
	<!--/ content fields-->
	</table>
	</xsl:template>	


	<!-- ||TEMPLATE: ordinary fields -->
	<xsl:template match="record/@*">
		<xsl:call-template name="escape_special">
			<xsl:with-param name="value" select="."/>
		</xsl:call-template>
		<xsl:if test="normalize-space(.)=''"><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text></xsl:if>
	</xsl:template>


	<!-- ||TEMPLATE: action field -->
	<xsl:template match="record/@*[name(.)='filter_action']">
		<xsl:value-of select="$action_description_list[@id=current()/.]"/>
	</xsl:template>


	<xsl:template name="escape_special">
		<xsl:param name="value"/>
		<xsl:choose>
			<xsl:when test="contains($value, ' ')"><xsl:call-template name="escape_quot"><xsl:with-param name="substr" select="$value"/></xsl:call-template></xsl:when>
			<xsl:otherwise><xsl:value-of select="$value"/></xsl:otherwise>
		</xsl:choose>	
	</xsl:template>
	
	<xsl:template name="escape_quot">
		<xsl:param name="substr"/>
		<xsl:choose>
			<xsl:when test="contains($substr, ' ')">
				<xsl:variable name="str_before_quot" select="substring-before($substr, ' ')"/>
				<xsl:variable name="str_after_quot" select="substring-after($substr, ' ')"/>
				<xsl:value-of select="$str_before_quot"/><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text><xsl:text/>	
				<xsl:call-template name="escape_quot">
					<xsl:with-param name="substr" select="$str_after_quot"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$substr"/>
			</xsl:otherwise>	
		</xsl:choose>
	</xsl:template>

	<!-- ||TEMPLATE: virus description -->
	<xsl:template match="record/@*[name(.)='filter_rule_av']">
		<xsl:choose>
			<xsl:when test="starts-with(., $virus_name_over_restriction)"><xsl:value-of select="."/></xsl:when>
			<xsl:when test=".=$virus_name_password_protected"><xsl:value-of select="."/></xsl:when>
			<xsl:otherwise>
				<a target="_new">
					<xsl:attribute name="href">
						<xsl:value-of select="$virus_encyclopedia_link"/>
						<xsl:value-of select="."/>
					</xsl:attribute>
					<xsl:value-of select="."/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
   <!-- ||TEMPLATE: Event Tracking : Event Type field -->
  <xsl:template match="record/@*[name(.)='EventType']">
    <xsl:value-of select="$event_type_list[@id=current()/.]"/>
  </xsl:template>

  <!-- ||TEMPLATE: Event Tracking : Source Type field -->
  <xsl:template match="record/@*[name(.)='SourceType']">
    <xsl:value-of select="$source_type_list[@id=current()/.]"/>
  </xsl:template> 

</xsl:stylesheet>
