<?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:date="http://exslt.org/dates-and-times"
    extension-element-prefixes="date">

<!-- (c) 2021, Trimble Inc. All rights reserved.                                               -->
<!-- Permission is hereby granted to use, copy, modify, or distribute this style sheet for any -->
<!-- purpose and without fee, provided that the above copyright notice appears in all copies   -->
<!-- and that both the copyright notice and the limited warranty and restricted rights notice  -->
<!-- below appear in all supporting documentation.                                             -->

<!-- TRIMBLE INC. PROVIDES THIS STYLE SHEET "AS IS" AND WITH ALL FAULTS.                       -->
<!-- TRIMBLE INC. SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY               -->
<!-- OR FITNESS FOR A PARTICULAR USE. TRIMBLE INC. DOES NOT WARRANT THAT THE                   -->
<!-- OPERATION OF THIS STYLE SHEET WILL BE UNINTERRUPTED OR ERROR FREE.                        -->

<xsl:output method="html" omit-xml-declaration="no" encoding="utf-8"/>

<!-- Set the numeric display details i.e. decimal point, thousands separator etc -->
<xsl:variable name="DecPt" select="string('.')"/>    <!-- Change as appropriate for US/European -->
<xsl:variable name="GroupSep" select="string(',')"/> <!-- Change as appropriate for US/European -->
<!-- Also change decimal-separator & grouping-separator in decimal-format below 
     as appropriate for US/European output -->
<xsl:decimal-format name="Standard" 
                    decimal-separator="."
                    grouping-separator=","
                    infinity="Infinity"
                    minus-sign="-"
                    NaN="?"
                    percent="%"
                    per-mille="&#2030;"
                    zero-digit="0" 
                    digit="#" 
                    pattern-separator=";" />

<xsl:variable name="DecPl0" select="string('#0')"/>
<xsl:variable name="DecPl1" select="string(concat('#0', $DecPt, '0'))"/>
<xsl:variable name="DecPl2" select="string(concat('#0', $DecPt, '00'))"/>
<xsl:variable name="DecPl3" select="string(concat('#0', $DecPt, '000'))"/>
<xsl:variable name="DecPl4" select="string(concat('#0', $DecPt, '0000'))"/>
<xsl:variable name="DecPl5" select="string(concat('#0', $DecPt, '00000'))"/>
<xsl:variable name="DecPl6" select="string(concat('#0', $DecPt, '000000'))"/>
<xsl:variable name="DecPl8" select="string(concat('#0', $DecPt, '00000000'))"/>

<xsl:variable name="Pi" select="3.14159265358979323846264"/>
<xsl:variable name="halfPi" select="$Pi div 2.0"/>
<xsl:variable name="DegreesSymbol" select="'.'"/>
<xsl:variable name="MinutesSymbol" select="''"/>
<xsl:variable name="SecondsSymbol" select="''"/>

<xsl:variable name="fileExt" select="'htm'"/>
<xsl:variable name="forcePDFOutput" select="'true'"/>  <!-- Forces conversion of resultant html file to a pdf file -->

<xsl:key name="tgtID-search" match="//JOBFile/FieldBook/TargetRecord" use="@ID"/>
<xsl:key name="antennaID-search" match="//JOBFile/FieldBook/AntennaRecord" use="@ID"/>
<xsl:key name="travID-search" match="//JOBFile/FieldBook/TraverseDefinitionRecord" use="@ID"/>

<!-- User variable definitions - Appropriate fields are displayed on the       -->
<!-- Survey Controller screen to allow the user to enter specific values       -->
<!-- which can then be used within the style sheet definition to control the   -->
<!-- output data.                                                              -->
<!--                                                                           -->
<!-- All user variables must be identified by a variable element definition    -->
<!-- named starting with 'userField' (case sensitive) followed by one or more  -->
<!-- characters uniquely identifying the user variable definition.             -->
<!--                                                                           -->
<!-- The text within the 'select' field for the user variable description      -->
<!-- references the actual user variable and uses the '|' character to         -->
<!-- separate the definition details into separate fields as follows:          -->
<!-- For all user variables the first field must be the name of the user       -->
<!-- variable itself (this is case sensitive) and the second field is the      -->
<!-- prompt that will appear on the Survey Controller screen.                  -->
<!-- The third field defines the variable type - there are four possible       -->
<!-- variable types: Double, Integer, String and StringMenu.  These variable   -->
<!-- type references are not case sensitive.                                   -->
<!-- The fields that follow the variable type change according to the type of  -->
<!-- variable as follow:                                                       -->
<!-- Double and Integer: Fourth field = optional minimum value                 -->
<!--                     Fifth field = optional maximum value                  -->
<!--   These minimum and maximum values are used by the Survey Controller for  -->
<!--   entry validation.                                                       -->
<!-- String: No further fields are needed or used.                             -->
<!-- StringMenu: Fourth field = number of menu items                           -->
<!--             Remaining fields are the actual menu items - the number of    -->
<!--             items provided must equal the specified number of menu items. -->
<!--                                                                           -->
<!-- The style sheet must also define the variable itself, named according to  -->
<!-- the definition.  The value within the 'select' field will be displayed in -->
<!-- the Survey Controller as the default value for the item.                  -->

<!-- Need to use the select attribute for userField definitions and also include the -->
<!-- contents in single quotes so that the user information can be picked up by the  -->
<!-- Survey Controller and ASCII File Generator.  This means it is not possible to   -->
<!-- have text that includes any apostrophes (single quotes) in it.                  -->
<xsl:variable name="userField1" select="'reportDescription|Rapportbeskrivning|string'"/>
<xsl:variable name="reportDescription" select="''"/>

<xsl:param name="saveAsPDF" select="'false'"/>  <!-- Parameter passed in to indicate whether the output html file is going to be converted to and saved as a pdf file -->
<xsl:param name="onWindows" select="'true'"/>   <!-- Parameter passed in to indicate whether the platform the system is running on is Windows or Android -->
<xsl:param name="dpiScale" select="1.00"/>      <!-- Parameter passed in providing the dpi scale factor in play on the platform the system is running on -->

<!-- **************************************************************** -->
<!-- Set global variables from the Environment section of JobXML file -->
<!-- **************************************************************** -->
<xsl:variable name="DistUnit"   select="/JOBFile/Environment/DisplaySettings/DistanceUnits" />
<xsl:variable name="ElevUnit"   select="/JOBFile/Environment/DisplaySettings/HeightUnits" />
<xsl:variable name="AngleUnit"  select="/JOBFile/Environment/DisplaySettings/AngleUnits" />
<xsl:variable name="CoordOrder" select="/JOBFile/Environment/DisplaySettings/CoordinateOrder" />
<xsl:variable name="TempUnit"   select="/JOBFile/Environment/DisplaySettings/TemperatureUnits" />
<xsl:variable name="PressUnit"  select="/JOBFile/Environment/DisplaySettings/PressureUnits" />

<!-- Setup conversion factor for coordinate and distance values -->
<xsl:variable name="DistConvFactor">
  <xsl:choose>
    <xsl:when test="$DistUnit='Metres'">1.0</xsl:when>
    <xsl:when test="$DistUnit='InternationalFeet'">3.280839895</xsl:when>
    <xsl:when test="$DistUnit='USSurveyFeet'">3.2808333333357</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for coordinate and distance values -->
<xsl:variable name="ElevConvFactor">
  <xsl:choose>
    <xsl:when test="$ElevUnit='Metres'">1.0</xsl:when>
    <xsl:when test="$ElevUnit='InternationalFeet'">3.280839895</xsl:when>
    <xsl:when test="$ElevUnit='USSurveyFeet'">3.2808333333357</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup conversion factor for angular values -->
<xsl:variable name="AngleConvFactor">
  <xsl:choose>
    <xsl:when test="$AngleUnit='DMSDegrees'">1.0</xsl:when>
    <xsl:when test="$AngleUnit='Gons'">1.111111111111</xsl:when>
    <xsl:when test="$AngleUnit='Mils'">17.77777777777</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Setup boolean variable for coordinate order -->
<xsl:variable name="NECoords">
  <xsl:choose>
    <xsl:when test="$CoordOrder='North-East-Elevation'">true</xsl:when>
    <xsl:when test="$CoordOrder='X-Y-Z'">true</xsl:when>
    <xsl:otherwise>false</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- Set up global variables for the north, east, elevation, delta north, delta east and -->
<!-- delta elevation prompts according to the type of coordinate labeling.               -->
<xsl:variable name="northPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">Norr</xsl:when>
    <xsl:otherwise>X</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="eastPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">Öst</xsl:when>
    <xsl:otherwise>Y</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="elevPrompt">
  <xsl:choose>
    <xsl:when test="($CoordOrder='North-East-Elevation') or ($CoordOrder='East-North-Elevation')">Höjd</xsl:when>
    <xsl:otherwise>Z</xsl:otherwise>
  </xsl:choose>
</xsl:variable>


<!-- Setup conversion factor for pressure values -->
<!-- Pressure values in JobXML file are always in millibars (hPa) -->
<xsl:variable name="PressConvFactor">
  <xsl:choose>
    <xsl:when test="$PressUnit='MilliBar'">1.0</xsl:when>
    <xsl:when test="$PressUnit='InchHg'">0.029529921</xsl:when>
    <xsl:when test="$PressUnit='mmHg'">0.75006</xsl:when>
    <xsl:otherwise>1.0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<!-- When the html file created by this style sheet is being converted to a pdf file, the font size  -->
<!-- setting can be quite critical in attempting to get a consistent pdf output on different devices -->
<!-- and operating systems. There are 3 parameters passed in by Trimble Access (saveAsPDF, onWindows -->
<!-- and dpiScale) that are used to achieve as consistent an output as possible.                     -->
<xsl:variable name="fontDefn">  <!-- Sets up the appropriate CSS text settings depending on the passed in parameters -->
  <xsl:choose>
    <xsl:when test="($onWindows = 'true') or ($saveAsPDF = 'false')">font-size: small;</xsl:when>
    <xsl:otherwise>
      <xsl:text>font-size: </xsl:text>
      <xsl:value-of select="ceiling($dpiScale * 12)"/>
      <xsl:text>px;</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="GSVersion">
  <xsl:choose>
    <xsl:when test="/JOBFile/@productVersion">
      <xsl:value-of select="/JOBFile/@productVersion"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="/JOBFile/@version div 100"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>


<!-- **************************************************************** -->
<!-- ************************** Main Loop *************************** -->
<!-- **************************************************************** -->
<xsl:template match="/" >
  <html>

  <title>Ytinspektionsrapport</title>
  
  <xsl:call-template name="DisplayLogo"/>  <!-- Displays the Trimble logo at the start of the report - contents can be substituted with appropriate company logo -->
  
  <h1><xsl:text>Ytinspektionsrapport - </xsl:text><xsl:value-of select="JOBFile/@jobName"/></h1>

  <!-- Set the font size for use in tables -->
  <style type="text/css">
    html { font-family: Arial }
    body, table, td, th
    {
      <xsl:value-of select="$fontDefn"/>
      padding: 2px;
    }

    table.border {border-style: solid; border-width: 1px; border-color: black; border-collapse: collapse;}
    table.noBorder {border-style: none; border-width: 0;}

    img.noBorder {border: unset}
    img.border
    {
      border: solid;
      border-width: 2px;
      border-color: black;
    }
  </style>

  <head/>

  <body>
    <!-- Display a table with appropriate job details -->
    <xsl:call-template name="StartTable">
      <xsl:with-param name="includeBorders">false</xsl:with-param>
    </xsl:call-template>
      <xsl:if test="$reportDescription != ''">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Beskrivning:</xsl:with-param>
          <xsl:with-param name="Val" select="$reportDescription"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Operatör:</xsl:with-param>
        <xsl:with-param name="Val" select="JOBFile/FieldBook/JobPropertiesRecord[last()]/Operator"/>
      </xsl:call-template>

      <xsl:if test="function-available('date:date') = 'true'">
        <xsl:variable name="dateTemp" select="date:date()"/>
        <xsl:variable name="date" select="substring-before($dateTemp, '+')"/>
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Datum:</xsl:with-param>
          <xsl:with-param name="Val">
            <xsl:value-of select="date:day-in-month($date)"/>
            <xsl:value-of select="' '"/>
            <xsl:value-of select="date:month-name($date)"/>
            <xsl:value-of select="' '"/>
            <xsl:value-of select="date:year($date)"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>

      <xsl:call-template name="OutputSingleElementTableLine">
        <xsl:with-param name="Hdr">Instrument:</xsl:with-param>
        <xsl:with-param name="Val">
          <xsl:for-each select="JOBFile/FieldBook/InstrumentRecord[1]">
            <xsl:variable name="instName">
              <xsl:choose>
                <xsl:when test="Type = 'TrimbleSX10'">
                  <xsl:value-of select="concat('Trimble ', Model)"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:call-template name="InstrumentName">
                    <xsl:with-param name="type" select="Type"/>
                  </xsl:call-template>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            
            <xsl:value-of select="$instName"/>
            <xsl:text>, Serienummer: </xsl:text>
            <xsl:value-of select="Serial"/>
          </xsl:for-each>
        </xsl:with-param>
      </xsl:call-template>

    <xsl:call-template name="EndTable"/>

    <!-- Insert a new page for printing purposes -->
    <!-- <table style="page-break-before: always"/> -->

    <xsl:apply-templates select="JOBFile/FieldBook"/>

  </body>
  </html>
</xsl:template>


<!-- **************************************************************** -->
<!-- ***************** FieldBook Node Processing ******************** -->
<!-- **************************************************************** -->
<xsl:template match="FieldBook">

  <xsl:apply-templates select="SurfaceInspectionRecord[Deleted = 'false']"/>

</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Separating Line Output ********************** -->
<!-- **************************************************************** -->
<xsl:template match="SurfaceInspectionRecord">

  <p style="page-break-before: always"/>  <!-- Empty line - will start new page in PDF output -->

  <!-- Present a table showing details of the surface inspection -->
  <xsl:call-template name="StartTable">
    <xsl:with-param name="caption" select="concat('Inspection: ', OutputName)"/>
    <xsl:with-param name="captionFontSize">4</xsl:with-param>
  </xsl:call-template>
  
    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Metod</xsl:with-param>
      <xsl:with-param name="Val">
        <xsl:call-template name="SurfaceInspectionMethod"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:choose>
      <xsl:when test="Method = 'HorizontalPlane'">   <!-- Defined by a single reference point or a specified elevation -->
        <!-- Always output the reference elevation (RefDistance) even if a selected point has been used -->
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Höjd</xsl:with-param>
          <xsl:with-param name="Val" select="format-number(RefDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>

        <xsl:if test="RefPoint1 != ''">
          <xsl:variable name="ptCoordStr">
            <xsl:call-template name="BuildPointCoordString">
              <xsl:with-param name="pointName" select="RefPoint1"/>
            </xsl:call-template>
          </xsl:variable>

          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Referenspunkt</xsl:with-param>
            <xsl:with-param name="Val" select="concat(RefPoint1, '&#0160;(', $ptCoordStr, ')')"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:when>

      <xsl:when test="Method = 'VerticalPlane'">   <!-- Defined by a two reference points -->
        <xsl:variable name="pt1CoordStr">
          <xsl:call-template name="BuildPointCoordString">
            <xsl:with-param name="pointName" select="RefPoint1"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Referenspunkt 1</xsl:with-param>
          <xsl:with-param name="Val" select="concat(RefPoint1, '&#0160;(', $pt1CoordStr, ')')"/>
        </xsl:call-template>

        <xsl:variable name="pt2CoordStr">
          <xsl:call-template name="BuildPointCoordString">
            <xsl:with-param name="pointName" select="RefPoint2"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Referenspunkt 2</xsl:with-param>
          <xsl:with-param name="Val" select="concat(RefPoint2, '&#0160;(', $pt2CoordStr, ')')"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:when test="Method = 'InclinedPlane'">   <!-- Defined by a three reference points -->
        <xsl:variable name="pt1CoordStr">
          <xsl:call-template name="BuildPointCoordString">
            <xsl:with-param name="pointName" select="RefPoint1"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Referenspunkt 1</xsl:with-param>
          <xsl:with-param name="Val" select="concat(RefPoint1, '&#0160;(', $pt1CoordStr, ')')"/>
        </xsl:call-template>

        <xsl:variable name="pt2CoordStr">
          <xsl:call-template name="BuildPointCoordString">
            <xsl:with-param name="pointName" select="RefPoint2"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Referenspunkt 2</xsl:with-param>
          <xsl:with-param name="Val" select="concat(RefPoint2, '&#0160;(', $pt2CoordStr, ')')"/>
        </xsl:call-template>

        <xsl:variable name="pt3CoordStr">
          <xsl:call-template name="BuildPointCoordString">
            <xsl:with-param name="pointName" select="RefPoint3"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Referenspunkt 3</xsl:with-param>
          <xsl:with-param name="Val" select="concat(RefPoint3, '&#0160;(', $pt3CoordStr, ')')"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:when test="(Method = 'Cylinder') or (Method = 'VerticalCylinder')">
        <xsl:call-template name="OutputSingleElementTableLine">
          <xsl:with-param name="Hdr">Radie</xsl:with-param>
          <xsl:with-param name="Val" select="format-number(RefDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
        </xsl:call-template>

        <xsl:if test="RefPoint1 != ''">
          <xsl:variable name="pt1CoordStr">
            <xsl:call-template name="BuildPointCoordString">
              <xsl:with-param name="pointName" select="RefPoint1"/>
            </xsl:call-template>
          </xsl:variable>

          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Referenspunkt 1</xsl:with-param>
            <xsl:with-param name="Val" select="concat(RefPoint1, '&#0160;(', $pt1CoordStr, ')')"/>
          </xsl:call-template>
        </xsl:if>

        <xsl:if test="RefPoint2 != ''">
          <xsl:variable name="pt2CoordStr">
            <xsl:call-template name="BuildPointCoordString">
              <xsl:with-param name="pointName" select="RefPoint2"/>
            </xsl:call-template>
          </xsl:variable>

          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Referenspunkt 2</xsl:with-param>
            <xsl:with-param name="Val" select="concat(RefPoint2, '&#0160;(', $pt2CoordStr, ')')"/>
          </xsl:call-template>
        </xsl:if>

        <xsl:if test="RefPoint3 != ''">
          <xsl:variable name="pt3CoordStr">
            <xsl:call-template name="BuildPointCoordString">
              <xsl:with-param name="pointName" select="RefPoint3"/>
            </xsl:call-template>
          </xsl:variable>

          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Referenspunkt 3</xsl:with-param>
            <xsl:with-param name="Val" select="concat(RefPoint3, '&#0160;(', $pt3CoordStr, ')')"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:when>

      <xsl:when test="Method = 'Scan'">
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Referensskanning</xsl:with-param>
            <xsl:with-param name="Val" select="ReferenceSurface"/>
          </xsl:call-template>
      </xsl:when>
      
      <xsl:when test="Method = 'Surface'">
          <xsl:call-template name="OutputSingleElementTableLine">
            <xsl:with-param name="Hdr">Referensyta</xsl:with-param>
            <xsl:with-param name="Val" select="ReferenceSurface"/>
          </xsl:call-template>
      </xsl:when>
    </xsl:choose>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Valda skanningar</xsl:with-param>
      <xsl:with-param name="Val" select="InputScan"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Minsta delta</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(MinDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Största delta</xsl:with-param>
      <xsl:with-param name="Val" select="format-number(MaxDistance * $DistConvFactor, $DecPl3, 'Standard')"/>
    </xsl:call-template>

    <xsl:call-template name="OutputSingleElementTableLine">
      <xsl:with-param name="Hdr">Färgskala</xsl:with-param>
      <xsl:with-param name="Val" select="ColorMap/Name"/>
    </xsl:call-template>
  <xsl:call-template name="EndTable"/>
  
  <xsl:call-template name="BlankLine"/>
  
  <xsl:variable name="inspectionRecID" select="@ID"/>  <!-- Used to locat the MediaFileRecords associated with this SurfaceInspectionRecord -->
  
  <!-- Now output the screen snap images that follow the SurfaceInspectionRecord -->
  <!-- We want the images to be inserted using a width of 600px. In order to     -->
  <!-- maintain the aspect ratio use the Width value to determine the horizontal -->
  <!-- scaling to get to 600px and apply the same ratio to the specified height. -->
  <xsl:for-each select="following-sibling::MediaFileRecord[RecordID = $inspectionRecID]">
    <xsl:variable name="ratio">
      <xsl:choose>
        <xsl:when test="(Width = 0) or (Width = '')">1.0</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="600 div Width"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="height">
      <xsl:choose>
        <xsl:when test="(Height = 0) or (Height = '')">480</xsl:when>  <!-- Defaulting to 480 to give scaled default of 800 x 640 image -->
        <xsl:otherwise>
          <xsl:value-of select="round($ratio * Height)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:if test="FileName != ''">
      <xsl:variable name="stdFileName" select="translate(FileName, '/', '\')"/>  <!-- Ensure path separating chars are backslashes -->
      <h3><xsl:text>Filnamn:</xsl:text><xsl:value-of select="substring-after($stdFileName, '\')"/></h3>
      <br/>
      <xsl:call-template name="NewLine"/>
      <xsl:variable name="filePath" select="FileName"/>
      <!-- Use a table to provide a border (defining a border in the <img> element doesn't come through to pdf file) -->
      <table class="border">
        <tr>
          <th><img src="{$filePath}" width="600" height="{$height}"/></th>
        </tr>
      </table>
      <xsl:call-template name="NewLine"/>
    </xsl:if>

    <xsl:if test="FileNameModified != ''">
      <xsl:variable name="stdFileName" select="translate(FileNameModified, '/', '\')"/>  <!-- Ensure path separating chars are backslashes -->
      <h3><xsl:text>Filnamn:</xsl:text><xsl:value-of select="substring-after($stdFileName, '\')"/></h3>
      <br/>
      <xsl:call-template name="NewLine"/>
      <xsl:variable name="filePath" select="FileNameModified"/>
      <!-- Use a table to provide a border (defining a border in the <img> element doesn't come through to pdf file) -->
      <table class="border">
        <tr>
          <th><img src="{$filePath}" width="600" height="{$height}"/></th>
        </tr>
      </table>
      <xsl:call-template name="NewLine"/>
    </xsl:if>
  </xsl:for-each>
  
  <!-- Finally output any surface inspection points associated with this inspection   -->
  <!-- The surface inspection points are output following the SurfaceInspectionRecord -->
  <!-- and its associated MediaFileRecords.                                           -->
  <xsl:variable name="nextInspectionRecPos">
    <xsl:variable name="nextInspectionRecPosns">
      <xsl:for-each select="following-sibling::*">
        <xsl:if test="name(.) = 'SurfaceInspectionRecord'">
          <xsl:element name="posn">
            <xsl:value-of select="position()"/>
          </xsl:element>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select="msxsl:node-set($nextInspectionRecPosns)/posn[1]"/>
  </xsl:variable>

  <xsl:variable name="surfInspectionPoints">
    <xsl:choose>
      <xsl:when test="$nextInspectionRecPos = ''">  <!-- There is no futher SurfaceInspectionRecord in the file -->
        <xsl:for-each select="following-sibling::PointRecord[Method = 'SurfaceInspection']">
          <xsl:copy-of select="."/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="following-sibling::*">
          <xsl:if test="(name(.) = 'PointRecord') and (Method = 'SurfaceInspection') and (position() &lt; $nextInspectionRecPos)">
            <xsl:variable name="posnVal" select="position()"/>
            <xsl:copy-of select="."/>
          </xsl:if>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <xsl:if test="$surfInspectionPoints != ''">   <!-- We have some surface inspection points -->
    <p>&#0160;</p>  <!-- Empty line -->
    <xsl:call-template name="StartTable">
      <xsl:with-param name="caption" select="concat('Inspection point deviations for: ', OutputName)"/>
      <xsl:with-param name="captionFontSize">4</xsl:with-param>
      <xsl:with-param name="includeInternalBorders">true</xsl:with-param>
    </xsl:call-template>
      <!-- Output the header line for the inspection points -->
      <xsl:call-template name="OutputTableDeviationPointLine">
        <xsl:with-param name="Col1">Punktnamn (Nr)</xsl:with-param>
        <xsl:with-param name="Col2">
          <xsl:choose>
            <xsl:when test="($CoordOrder = 'North-East-Elevation') or ($CoordOrder = 'Y-X-Z')">
              <xsl:value-of select="$northPrompt"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$eastPrompt"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Col3">
          <xsl:choose>
            <xsl:when test="($CoordOrder = 'North-East-Elevation') or ($CoordOrder = 'Y-X-Z')">
              <xsl:value-of select="$eastPrompt"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$northPrompt"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="Col4"><xsl:value-of select="$elevPrompt"/></xsl:with-param>
        <xsl:with-param name="Col5">Avvikelse</xsl:with-param>
        <xsl:with-param name="highlighted">true</xsl:with-param>
      </xsl:call-template>
      
      <xsl:variable name="colourMap" select="ColorMap"/>  <!-- Store in local variable so is accessible when iterating through inspection points -->
      
      <!-- Output the inspection point details -->
      <xsl:for-each select="msxsl:node-set($surfInspectionPoints)/PointRecord[Code != '']">
        <xsl:variable name="deviation" select="SurfaceInspection/Deviation"/>
        <xsl:call-template name="OutputTableDeviationPointLine">
          <xsl:with-param name="Col1"><xsl:value-of select="Name"/></xsl:with-param>
          <xsl:with-param name="Col2">
            <xsl:choose>
              <xsl:when test="($CoordOrder = 'North-East-Elevation') or ($CoordOrder = 'Y-X-Z')">
                <xsl:value-of select="format-number(Grid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="format-number(Grid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="Col3">
            <xsl:choose>
              <xsl:when test="($CoordOrder = 'North-East-Elevation') or ($CoordOrder = 'Y-X-Z')">
                <xsl:value-of select="format-number(Grid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="format-number(Grid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="Col4"><xsl:value-of select="format-number(Grid/Elevation * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:with-param>
          <xsl:with-param name="Col5"><xsl:value-of select="format-number(SurfaceInspection/Deviation * $DistConvFactor, $DecPl3, 'Standard')"/></xsl:with-param>
          <xsl:with-param name="devColour">
            <xsl:call-template name="ColourFromColourMap">
              <xsl:with-param name="colourMap" select="$colourMap"/>
              <xsl:with-param name="deviation" select="$deviation"/>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:for-each>
    <xsl:call-template name="EndTable"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Surface inspection method text ****************** -->
<!-- **************************************************************** -->
<xsl:template name="SurfaceInspectionMethod">

  <xsl:choose>
    <xsl:when test="Method = 'HorizontalPlane'">Skanning mot horisontellt plan</xsl:when>
    <xsl:when test="Method = 'VerticalPlane'">Skanning mot vertikalt plan</xsl:when>
    <xsl:when test="Method = 'InclinedPlane'">Skanning mot lutande plan</xsl:when>
    <xsl:when test="Method = 'Cylinder'">Skanning mot cylinder</xsl:when>
    <xsl:when test="Method = 'Surface'">Skanning mot yta</xsl:when>
    <xsl:when test="Method = 'Scan'">Skanning mot skanning</xsl:when>
    <xsl:when test="Method = 'VerticalCylinder'">Skanning mot vertikal cylinder</xsl:when>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Build point coordinate string ****************** -->
<!-- **************************************************************** -->
<xsl:template name="BuildPointCoordString">
  <xsl:param name="pointName"/>

  <xsl:for-each select="/JOBFile/Reductions/Point[Name = $pointName]">
    <xsl:choose>
      <xsl:when test="($CoordOrder = 'North-East-Elevation') or ($CoordOrder = 'Y-X-Z')">
        <xsl:value-of select="$northPrompt"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="format-number(Grid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
        <xsl:text>&#0160;&#0160;</xsl:text>
        <xsl:value-of select="$eastPrompt"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="format-number(Grid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="$eastPrompt"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="format-number(Grid/East * $DistConvFactor, $DecPl3, 'Standard')"/>
        <xsl:text>&#0160;&#0160;</xsl:text>
        <xsl:value-of select="$northPrompt"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="format-number(Grid/North * $DistConvFactor, $DecPl3, 'Standard')"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:text>&#0160;&#0160;</xsl:text>
    <xsl:value-of select="$elevPrompt"/>
    <xsl:text>: </xsl:text>
    <xsl:value-of select="format-number(Grid/Elevation * $DistConvFactor, $DecPl3, 'Standard')"/>
  </xsl:for-each>
</xsl:template>


<!-- **************************************************************** -->
<!-- **************** Return Expanded Instrument Name *************** -->
<!-- **************************************************************** -->
<xsl:template name="InstrumentName">
  <xsl:param name="type"/>

  <xsl:choose>
    <xsl:when test="$type = 'TrimbleTTS'">Trimble TTS</xsl:when>
    <xsl:when test="$type = 'Trimble3300'">Trimble 3300</xsl:when>
    <xsl:when test="$type = 'Trimble3600Elta'">Trimble 3600 Elta</xsl:when>
    <xsl:when test="$type = 'Trimble3600GDM'">Trimble 3600 GDM</xsl:when>
    <xsl:when test="$type = 'Trimble600M'">Trimble 600M</xsl:when>
    <xsl:when test="$type = 'Trimble5600'">Trimble 5600</xsl:when>
    <xsl:when test="$type = 'TrimbleM3'">Trimble M3</xsl:when>
    <xsl:when test="$type = 'TrimbleSSeries'">Trimble S Series</xsl:when>
    <xsl:when test="$type = 'TrimbleVXandSSeries'">Trimble VX/S Series</xsl:when>
    <xsl:when test="$type = 'TrimbleSX10'">Trimble SX10</xsl:when>
    <xsl:when test="$type = 'SETBasic'">Sokkia SET</xsl:when>
    <xsl:when test="$type = 'SETEnhanced'">Sokkia SET</xsl:when>
    <xsl:when test="$type = 'Geodimeter400'">Geodimeter 400</xsl:when>
    <xsl:when test="$type = 'Geodimeter500Servo'">Geodimeter 500 Servo</xsl:when>
    <xsl:when test="$type = 'Geodimeter600'">Geodimeter 600</xsl:when>
    <xsl:when test="$type = 'Geodimeter600Servo'">Geodimeter 600 Servo</xsl:when>
    <xsl:when test="$type = 'Geodimeter600Robotic'">Geodimeter 600 Robotic</xsl:when>
    <xsl:when test="$type = 'Geodimeter4000'">Geodimeter 4000</xsl:when>
    <xsl:when test="$type = 'LeicaTC300'">Leica TC300</xsl:when>
    <xsl:when test="$type = 'LeicaTC500'">Leica TC500</xsl:when>
    <xsl:when test="$type = 'LeicaTC800'">Leica TC800</xsl:when>
    <xsl:when test="$type = 'LeicaT1000_6'">Leica T1000 6</xsl:when>
    <xsl:when test="$type = 'LeicaT1000_14'">Leica T1000 14</xsl:when>
    <xsl:when test="$type = 'LeicaTC1100'">Leica TC1100</xsl:when>
    <xsl:when test="$type = 'LeicaTC1100ServoGeoCom'">Leica TC1100 Servo (GeoCom)</xsl:when>
    <xsl:when test="$type = 'LeicaTC1100RoboticGeoCom'">Leica TC1100 Robotic (GeoCom)</xsl:when>
    <xsl:when test="$type = 'LeicaTC1600'">Leica TC1600</xsl:when>
    <xsl:when test="$type = 'LeicaTC2000'">Leica TC2000</xsl:when>
    <xsl:when test="$type = 'Nikon'">Nikon</xsl:when>
    <xsl:when test="$type = 'Pentax'">Pentax</xsl:when>
    <xsl:when test="$type = 'TopconGeneric'">Topcon</xsl:when>
    <xsl:when test="$type = 'Topcon500'">Topcon 500</xsl:when>
    <xsl:when test="$type = 'ZeissElta2'">Zeiss Elta 2</xsl:when>
    <xsl:when test="$type = 'ZeissElta3'">Zeiss Elta 3</xsl:when>
    <xsl:when test="$type = 'ZeissElta4'">Zeiss Elta 4</xsl:when>
    <xsl:when test="$type = 'ZeissEltaC'">Zeiss Elta C</xsl:when>
    <xsl:when test="$type = 'ZeissRecEltaE'">Zeiss Rec Elta E</xsl:when>
    <xsl:when test="$type = 'ZeissRSeries'">Zeiss R Series</xsl:when>
    <xsl:when test="$type = 'Manual'">Manual</xsl:when>
    <xsl:when test="$type = 'Null'">None</xsl:when>
    <xsl:otherwise><xsl:value-of select="$type"/></xsl:otherwise>  <!-- Simply return the passed in type -->
  </xsl:choose>

</xsl:template>


<!-- **************************************************************** -->
<!-- *** Returns interpolated colour for deviation from colour map ** -->
<!-- **************************************************************** -->
<xsl:template name="ColourFromColourMap">
  <xsl:param name="colourMap"/>
  <xsl:param name="deviation"/>
  
  <xsl:variable name="colour">
    <xsl:for-each select="msxsl:node-set($colourMap)/ColorBands/Band">
      <xsl:choose>
        <xsl:when test="position() = 1">
          <xsl:choose>
            <xsl:when test="$deviation &gt; Distance">
              <xsl:value-of select="msxsl:node-set($colourMap)/MaxColor"/>
            </xsl:when>
            <xsl:when test="$deviation = Distance">
              <xsl:value-of select="Color"/>
            </xsl:when>
          </xsl:choose>
        </xsl:when>

        <xsl:otherwise>
          <xsl:if test="(position() = last()) and ($deviation &lt; Distance)">
            <xsl:value-of select="msxsl:node-set($colourMap)/MinColor"/>
          </xsl:if>

          <xsl:if test="(position() = last()) and ($deviation = Distance)">
            <xsl:value-of select="Color"/>
          </xsl:if>

          <xsl:if test="($deviation &gt;= Distance) and ($deviation &lt; preceding-sibling::Band[1]/Distance)">
            <xsl:choose>
              <xsl:when test="msxsl:node-set($colourMap)/Gradient = 'true'">
                <xsl:variable name="deviationRange" select="preceding-sibling::Band[1]/Distance - Distance"/>
                <xsl:variable name="proportion" select="($deviation - Distance) div $deviationRange"/>
                <xsl:call-template name="InterpolatedColourValue">
                  <xsl:with-param name="lowColour" select="Color"/>
                  <xsl:with-param name="highColour" select="preceding-sibling::Band[1]/Color"/>
                  <xsl:with-param name="proportion" select="$proportion"/>
                </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>  <!-- Not using a colour gradient so simply return the 'lower' colour -->
                <xsl:value-of select="Color"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:variable name="hexColour">
    <xsl:call-template name="PadLeftWithZeros">
      <xsl:with-param name="theString">
        <xsl:call-template name="DecimalToHex">
          <xsl:with-param name="decimalNumber" select="$colour"/>
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="stringWidth">6</xsl:with-param>
    </xsl:call-template>
  </xsl:variable>

  <!-- Swap the R,G,B components around -->
  <xsl:variable name="outputHexColour" select="concat('#', substring($hexColour, 5, 2), substring($hexColour, 3, 2), substring($hexColour, 1, 2))"/>

  <xsl:value-of select="$outputHexColour"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- *************** Interpolate the colour value ******************* -->
<!-- **************************************************************** -->
<xsl:template name="InterpolatedColourValue">
  <xsl:param name="lowColour"/>
  <xsl:param name="highColour"/>
  <xsl:param name="proportion"/>

  <xsl:variable name="hexLowColour">
    <xsl:call-template name="PadLeftWithZeros">
      <xsl:with-param name="theString">
        <xsl:call-template name="DecimalToHex">
          <xsl:with-param name="decimalNumber" select="$lowColour"/>
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="stringWidth">6</xsl:with-param>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="hexHighColour">
    <xsl:call-template name="PadLeftWithZeros">
      <xsl:with-param name="theString">
        <xsl:call-template name="DecimalToHex">
          <xsl:with-param name="decimalNumber" select="$highColour"/>
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="stringWidth">6</xsl:with-param>
    </xsl:call-template>
  </xsl:variable>

  <!-- Interpolate blue component -->
  <xsl:variable name="decimalLowBlue">
    <xsl:call-template name="HexToDecimal">
      <xsl:with-param name="hexNumber" select="substring($hexLowColour, 1, 2)"/>
    </xsl:call-template>
  </xsl:variable>
  
  <xsl:variable name="decimalHighBlue">
    <xsl:call-template name="HexToDecimal">
      <xsl:with-param name="hexNumber" select="substring($hexHighColour, 1, 2)"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="interpolatedHexBlue">
    <xsl:variable name="rawHexVal">
      <xsl:call-template name="DecimalToHex">
        <xsl:with-param name="decimalNumber" select="$decimalLowBlue + $proportion * ($decimalHighBlue - $decimalLowBlue)"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="hexVal">
      <xsl:choose>
        <xsl:when test="contains($rawHexVal, '.')">
          <xsl:value-of select="substring-before($rawHexVal, '.')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$rawHexVal"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$hexVal = ''">00</xsl:when>
      <xsl:when test="string-length($hexVal) &lt; 2"><xsl:value-of select="concat('0', $hexVal)"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="substring($hexVal, 1, 2)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <!-- Interpolate green component -->
  <xsl:variable name="decimalLowGreen">
    <xsl:call-template name="HexToDecimal">
      <xsl:with-param name="hexNumber" select="substring($hexLowColour, 3, 2)"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="decimalHighGreen">
    <xsl:call-template name="HexToDecimal">
      <xsl:with-param name="hexNumber" select="substring($hexHighColour, 3, 2)"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="interpolatedHexGreen">
    <xsl:variable name="rawHexVal">
      <xsl:call-template name="DecimalToHex">
        <xsl:with-param name="decimalNumber" select="$decimalLowGreen + $proportion * ($decimalHighGreen - $decimalLowGreen)"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="hexVal">
      <xsl:choose>
        <xsl:when test="contains($rawHexVal, '.')">
          <xsl:value-of select="substring-before($rawHexVal, '.')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$rawHexVal"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$hexVal = ''">00</xsl:when>
      <xsl:when test="string-length($hexVal) &lt; 2"><xsl:value-of select="concat('0', $hexVal)"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="substring($hexVal, 1, 2)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <!-- Interpolate red component -->
  <xsl:variable name="decimalLowRed">
    <xsl:call-template name="HexToDecimal">
      <xsl:with-param name="hexNumber" select="substring($hexLowColour, 5, 2)"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="decimalHighRed">
    <xsl:call-template name="HexToDecimal">
      <xsl:with-param name="hexNumber" select="substring($hexHighColour, 5, 2)"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="interpolatedHexRed">
    <xsl:variable name="rawHexVal">
      <xsl:call-template name="DecimalToHex">
        <xsl:with-param name="decimalNumber" select="$decimalLowRed + $proportion * ($decimalHighRed - $decimalLowRed)"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="hexVal">
      <xsl:choose>
        <xsl:when test="contains($rawHexVal, '.')">
          <xsl:value-of select="substring-before($rawHexVal, '.')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$rawHexVal"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$hexVal = ''">00</xsl:when>
      <xsl:when test="string-length($hexVal) &lt; 2"><xsl:value-of select="concat('0', $hexVal)"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="substring($hexVal, 1, 2)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <!-- Assemble the colour back into a hexadecimal value then convert to decimal -->
  <xsl:call-template name="HexToDecimal">
    <xsl:with-param name="hexNumber" select="concat($interpolatedHexBlue, $interpolatedHexGreen, $interpolatedHexRed)"/>
  </xsl:call-template>
</xsl:template>


<!-- **************************************************************** -->
<!-- *********** Pad a string to the left with spaces *************** -->
<!-- **************************************************************** -->
<xsl:template name="PadLeftWithZeros">
  <xsl:param name="stringWidth"/>
  <xsl:param name="theString"/>
  <xsl:choose>
    <xsl:when test="$stringWidth = '0'">
      <xsl:value-of select="normalize-space($theString)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="paddedStr" select="concat('00000000000000000000000000000000000000000000000000000000000000000000', $theString)"/>
      <xsl:value-of select="substring($paddedStr, string-length($paddedStr) - $stringWidth + 1)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****************** Separating Line Output ********************** -->
<!-- **************************************************************** -->
<xsl:template name="SeparatingLine">
  <xsl:param name="includeBorders" select="'true'"/>
  
  <xsl:choose>
    <xsl:when test="$includeBorders != 'true'">   <!-- Only include separating lines -->
      <hr></hr>                                   <!-- if there are no table borders -->
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="BlankLine"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********* Output Table Line for Observed Point Details ********* -->
<!-- **************************************************************** -->
<xsl:template name="OutputTableDeviationPointLine">
  <xsl:param name="Col1" select="'&#160;'"/>
  <xsl:param name="Col2" select="'&#160;'"/>
  <xsl:param name="Col3" select="'&#160;'"/>
  <xsl:param name="Col4" select="'&#160;'"/>
  <xsl:param name="Col5" select="'&#160;'"/>
  <xsl:param name="highlighted" select="'false'"/>
  <xsl:param name="devColour" select="'#FFFFFF'"/>

  <tr>
    <xsl:choose>
      <xsl:when test="$highlighted = 'true'">
        <th width="20%" align="center"><xsl:value-of select="$Col1"/></th>
        <th width="20%" align="center"><xsl:value-of select="$Col2"/></th>
        <th width="20%" align="center"><xsl:value-of select="$Col3"/></th>
        <th width="20%" align="center"><xsl:value-of select="$Col4"/></th>
        <th width="20%" align="center" colspan="2"><xsl:value-of select="$Col5"/></th>
      </xsl:when>
      <xsl:otherwise>
        <td width="20%" align="left"><xsl:value-of select="$Col1"/></td>
        <td width="20%" align="right"><xsl:value-of select="$Col2"/></td>
        <td width="20%" align="right"><xsl:value-of select="$Col3"/></td>
        <td width="20%" align="right"><xsl:value-of select="$Col4"/></td>
        <td width="5%"  align="right" style="background-color: {$devColour}">&#160;</td>  <!-- Displays cell with interpolated colour relating to the deviation value -->
        <td width="15%" align="right"><xsl:value-of select="$Col5"/></td>
      </xsl:otherwise>
    </xsl:choose>
  </tr>

</xsl:template>


<!-- **************************************************************** -->
<!-- ************** Output Single Element Table Line **************** -->
<!-- **************************************************************** -->
<xsl:template name="OutputSingleElementTableLine">
  <xsl:param name="Hdr" select="'&#160;'"/>
  <xsl:param name="Val" select="'&#160;'"/>

  <tr>
    <th width="25%" align="left"><xsl:value-of select="$Hdr"/></th>
    <td width="75%" align="left"><xsl:value-of select="$Val"/></td>
  </tr>
  
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************* Blank Line Output ************************ -->
<!-- **************************************************************** -->
<xsl:template name="BlankLine">
  <xsl:text>&#160;</xsl:text>
  <br/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************************* Start Table ************************** -->
<!-- **************************************************************** -->
<xsl:template name="StartTable">
  <xsl:param name="caption"/>
  <xsl:param name="captionFontSize" select="2"/>  <!-- 2 is the standard font size -->
  <xsl:param name="includeBorders" select="'true'"/>
  <xsl:param name="includeInternalBorders" select="'false'"/>
  
  <xsl:call-template name="NewLine"/>
  <xsl:choose>
    <xsl:when test="$includeBorders = 'true'">
      <xsl:choose>
        <xsl:when test="$includeInternalBorders = 'true'">
          <xsl:value-of disable-output-escaping="yes" select="'&lt;table class=border width=100% cellpadding=2 rules=all&gt;'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of disable-output-escaping="yes" select="'&lt;table class=border width=100% cellpadding=2 rules=cols&gt;'"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of disable-output-escaping="yes" select="'&lt;table class=noBorder width=100% cellpadding=2 rules=none&gt;'"/>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:if test="$caption != ''">
    <xsl:call-template name="NewLine"/>
    <caption align="top"><b><font size="{$captionFontSize}"><p align="left"><xsl:value-of select="$caption"/></p></font></b></caption>
    <xsl:call-template name="NewLine"/>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************************** End Table *************************** -->
<!-- **************************************************************** -->
<xsl:template name="EndTable">
  <xsl:value-of disable-output-escaping="yes" select="'&lt;/table&gt;'"/>
  <xsl:call-template name="NewLine"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ************************* Display Logo ************************* -->
<!-- **************************************************************** -->
<xsl:template name="DisplayLogo">

  <img class="noBorder" src="data:image/png;base64,
[encoded data, 6916 characters]
AAAAAElFTkSuQmCC"/>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********** Convert Hexadecimal Value To Decimal Value ********** -->
<!-- **************************************************************** -->
<xsl:template name="HexToDecimal">
  <xsl:param name="hexNumber" />
  <xsl:param name="decimalNumber" >0</xsl:param>

  <!-- If there are zero hex digits left, output -->
  <xsl:choose>
    <xsl:when test="$hexNumber">
      <xsl:call-template name="HexToDecimal">
        <xsl:with-param name="decimalNumber" select="($decimalNumber*16)+number(substring-before(substring-after('00/11/22/33/44/55/66/77/88/99/A10/B11/C12/D13/E14/F15/a10/b11/c12/d13/e14/f15/',substring($hexNumber,1,1)),'/'))" />
        <xsl:with-param name="hexNumber" select="substring($hexNumber,2)" />
      </xsl:call-template>
    </xsl:when>
    <!-- otherwise multiply, and add the next digit, and recurse -->
    <xsl:otherwise>
      <xsl:value-of select="$decimalNumber"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- **************************************************************** -->
<!-- ****** Returns Hexadecimal value of supplied decimalNumber ***** -->
<!-- **************************************************************** -->
<xsl:template name="DecimalToHex">
  <xsl:param name="decimalNumber" />

  <xsl:if test="$decimalNumber > 0">
    <xsl:call-template name="DecimalToHex">
      <xsl:with-param name="decimalNumber" select="floor($decimalNumber div 16)" />
    </xsl:call-template>

    <xsl:choose>
      <xsl:when test="$decimalNumber mod 16 &lt; 10">
        <xsl:value-of select="$decimalNumber mod 16" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$decimalNumber mod 16 = 10">A</xsl:when>
          <xsl:when test="$decimalNumber mod 16 = 11">B</xsl:when>
          <xsl:when test="$decimalNumber mod 16 = 12">C</xsl:when>
          <xsl:when test="$decimalNumber mod 16 = 13">D</xsl:when>
          <xsl:when test="$decimalNumber mod 16 = 14">E</xsl:when>
          <xsl:when test="$decimalNumber mod 16 = 15">F</xsl:when>
          <xsl:otherwise>A</xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template>


<!-- **************************************************************** -->
<!-- ********************** New Line Output ************************* -->
<!-- **************************************************************** -->
<xsl:template name="NewLine">
  <xsl:text>&#10;</xsl:text>
</xsl:template>


</xsl:stylesheet>
