<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:SQ="urn:SQ-namespace"
	xmlns:Locale="urn:Locale-namespace">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:include href="DefaultPrint-r102.xsl"/>
	<xsl:variable name="NativePressureUnits">psi</xsl:variable>
	<xsl:variable name="TheSchema" select="document('AcquityTQ-Report-r100.xsd')"/>
	<xsl:variable name="LanguageSubfolder" select="Locale:LanguageSubfolder()"/>
	<xsl:variable name="ResourceFile" select="concat($LanguageSubfolder,'\\AcquityTQ-Resource-r100.xml')"/>
	<xsl:variable name="TheResources" select="document(string($ResourceFile))"></xsl:variable>
	
	<xsl:template match="/*">
		<xsl:element name="AcquityTQReport">
		
			<!-- Header info -->
			<xsl:element name="FieldList">
				<xsl:element name="Title"/>
				<xsl:apply-templates select="IcsVersion"/>
				<xsl:apply-templates select="SerialNumber"/>
				<xsl:apply-templates select="TuneName"/>
		        <xsl:apply-templates select="Reservoir"/>
				<xsl:apply-templates select="Configuration/InterScanDelay"/>
				<xsl:apply-templates select="Configuration/PolarityModeSwitchISD"/>
				<xsl:apply-templates select="Configuration/InterChannelDelay"/>
			</xsl:element>

			<xsl:apply-templates select="Messages"/>

			<!-- Tune Settings -->
			<xsl:apply-templates select="ESPositive"/>
			<xsl:apply-templates select="ESNegative"/>
			<xsl:apply-templates select="APcIPositive"/>
			<xsl:apply-templates select="APcINegative"/>
			<xsl:apply-templates select="APPIPositive"/>
			<xsl:apply-templates select="APPINegative"/>
			<xsl:apply-templates select="DualESCIPositive"/>
			<xsl:apply-templates select="DualESCINegative"/>
			<xsl:apply-templates select="DualAPPIPositive"/>
			<xsl:apply-templates select="DualAPPINegative"/>

			<!-- Calibration -->
			<xsl:element name="FieldList">
				<xsl:element name="Title">
					<xsl:value-of  select="$TheResources//Type[@name='tCalibration']/@prompt"/>
				</xsl:element>
				<xsl:apply-templates select="CalibrationName"/>
				<xsl:apply-templates select="CalibrationTime"/>
			</xsl:element>
			<xsl:apply-templates select="Static"/>
			<xsl:apply-templates select="Dynamic"/>
			<xsl:apply-templates select="Fast"/>
			<xsl:apply-templates select="Quad2Static"/>
			<xsl:apply-templates select="Quad2Dynamic"/>
			<xsl:apply-templates select="Quad2Fast"/>
			
		</xsl:element>
	</xsl:template>
	
  <xsl:template match="IcsVersion|SerialNumber|TuneName|CalibrationName|CalibrationTime|Reservoir">
      <xsl:call-template name="Emit"/>
	</xsl:template>
	
	<xsl:template match="Configuration/InterScanDelay|Configuration/PolarityModeSwitchISD|Configuration/InterChannelDelay">
		<xsl:call-template name="Emit"/>
	</xsl:template>
	
	<xsl:template match="Messages">
		<xsl:if test="count(child::*) > 0">
			<xsl:element name="FieldList">
				<xsl:element name="Title">Messages</xsl:element>
				<xsl:apply-templates/>
			</xsl:element>
		</xsl:if>
	</xsl:template>

	<xsl:template match="Message">
		<xsl:element name="Field">
			<xsl:attribute name="name">
				<xsl:value-of select="position()"/>
				<xsl:text>. </xsl:text>
			</xsl:attribute>
			<xsl:attribute name="type">String</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

  <xsl:template match="ESPositive|ESNegative|APcIPositive|APcINegative|APPIPositive|APPINegative|DualESCIPositive|DualESCINegative|DualAPPIPositive|DualAPPINegative">

    <xsl:element name="FieldList">

      <xsl:variable name="TuneSection">
        <xsl:value-of select="name()"/>
      </xsl:variable>

      <xsl:variable name="IonMode">
        <xsl:choose>
          <xsl:when test="$TuneSection='ESPositive' or $TuneSection='ESNegative'">ES</xsl:when>
          <xsl:when test="$TuneSection='APcIPositive' or $TuneSection='APcINegative'">APCI</xsl:when>
          <xsl:when test="$TuneSection='APPIPositive' or $TuneSection='APPINegative'">APPI</xsl:when>
          <xsl:when test="$TuneSection='DualESCIPositive' or $TuneSection='DualESCINegative'">DualESCI</xsl:when>
          <xsl:when test="$TuneSection='DualAPPIPositive' or $TuneSection='DualAPPINegative'">DualAPPI</xsl:when>
        </xsl:choose>
      </xsl:variable>
      
      <xsl:element name="Title">
          <xsl:value-of select="$TheResources//Type[@name='tTuneSettings']/Prompt[@id=$TuneSection]/@value"/>
      </xsl:element>

      <xsl:apply-templates select="ConeVoltage"/>
      <xsl:if test="$IonMode = 'DualESCI'">
        <xsl:apply-templates select="ConeVoltageES"/>
      </xsl:if>
      <xsl:if test="$IonMode = 'DualAPPI'">
        <xsl:apply-templates select="ConeVoltageAPPI"/>
      </xsl:if>
      <xsl:if test="$IonMode = 'DualESCI' or $IonMode = 'DualAPPI'">
        <xsl:apply-templates select="ConeVoltageAPCI"/>
      </xsl:if>
      <xsl:if test="not ( $IonMode='APcI' )">
        <xsl:apply-templates select="CapillaryVoltage"/>
      </xsl:if>
      <xsl:apply-templates select="ExtractorVoltage"/>
      <xsl:if test="not ( $IonMode = 'ES' or $IonMode = 'APPI')">
        <xsl:apply-templates select="CoronaCurrent"/>
        <xsl:apply-templates select="CoronaVoltage"/>
      </xsl:if>
      <xsl:apply-templates select="RFLens"/>
      <xsl:apply-templates select="SourceTemperature"/>
      <xsl:if test="not ( $IonMode = 'ES' or $IonMode = 'DualESCI')">
        <xsl:apply-templates select="ProbeTemperature"/>
      </xsl:if>
      <xsl:apply-templates select="ConeGasFlow"/>
      <xsl:if test="not ( $IonMode = 'APcI' or $IonMode = 'APPI')">
        <xsl:apply-templates select="DesolvationTemperature"/>
      </xsl:if>
      <xsl:apply-templates select="DesolvationGasFlow"/>
      <xsl:apply-templates select="LM1Resolution"/>
      <xsl:apply-templates select="HM1Resolution"/>
      <xsl:apply-templates select="IonEnergy1"/>
      <xsl:apply-templates select="MSModeEntrance"/>
      <xsl:apply-templates select="MSModeCollisionEnergy"/>
      <xsl:apply-templates select="MSModeExit"/>
      <xsl:apply-templates select="LM2Resolution"/>
      <xsl:apply-templates select="HM2Resolution"/>
      <xsl:apply-templates select="IonEnergy2"/>
      <xsl:apply-templates select="MSMSModeEntrance"/>
      <xsl:apply-templates select="MSMSModeCollisionEnergy"/>
      <xsl:apply-templates select="MSMSModeExit"/>
      <xsl:apply-templates select="CollisionGasFlow"/>
      <xsl:apply-templates select="Gain"/>

    </xsl:element>
  
  </xsl:template>

	<xsl:template match="Static|Dynamic|Quad2Static|Quad2Dynamic">
		<xsl:variable name="FieldName">
			<xsl:value-of select="name()"/>
		</xsl:variable>
		<xsl:element name="FieldList">
			<xsl:element name="Title">
				<xsl:value-of  select="$TheResources//Type[@name='tCalibrationSettings']/Prompt[@id=$FieldName]/@value"/>
			</xsl:element>
			
			<xsl:call-template name="Emit-Field-Value-Prompt">
				<xsl:with-param name="InputVar" select="MassRange" />
				<xsl:with-param name="InputType">String</xsl:with-param>
				<xsl:with-param name="InputPrecision">0</xsl:with-param>
				<xsl:with-param name="InputValue">
					<xsl:value-of select="concat(LowMass,' - ',HighMass)"/>
				</xsl:with-param>
				<xsl:with-param name="InputPrompt">
					<xsl:value-of  select="$TheResources//Type[@name='tMass']/@prompt"/>
				</xsl:with-param>
				<xsl:with-param name="InputUnits">
					<xsl:value-of  select="$TheResources//Type[@name='tMass']/@units"/>
				</xsl:with-param>
			</xsl:call-template>
			
			<xsl:apply-templates select="HMResolution"/>
			<xsl:apply-templates select="LMResolution"/>
			<xsl:apply-templates select="IonEnergy"/>
			
			<xsl:if test="$FieldName='Dynamic' or $FieldName='Quad2Dynamic'">
				<xsl:apply-templates select="InterScanDelay"/>
			</xsl:if>
			
		</xsl:element>
	</xsl:template>

	<xsl:template match="Fast|Quad2Fast">
		<xsl:variable name="FieldName">
			<xsl:value-of select="name()"/>
		</xsl:variable>
		<xsl:element name="FieldList">
			<xsl:element name="Title">
				<xsl:value-of  select="$TheResources//Type[@name='tScanSpeedCompSettings']/Prompt[@id=$FieldName]/@value"/>
			</xsl:element>
			
			<xsl:call-template name="Emit-Field-Value-Prompt">
				<xsl:with-param name="InputVar" select="ScanRange" />
				<xsl:with-param name="InputType">String</xsl:with-param>
				<xsl:with-param name="InputPrecision">0</xsl:with-param>
				<xsl:with-param name="InputValue">
					<xsl:value-of select="concat(LowScan,' - ',HighScan)"/>
				</xsl:with-param>
				<xsl:with-param name="InputPrompt">
					<xsl:value-of  select="$TheResources//Type[@name='tScanRate']/@prompt"/>
				</xsl:with-param>
				<xsl:with-param name="InputUnits">
					<xsl:value-of  select="$TheResources//Type[@name='tScanRate']/@units"/>
				</xsl:with-param>
			</xsl:call-template>
			
			<xsl:apply-templates select="HMResolution"/>
			<xsl:apply-templates select="LMResolution"/>
			<xsl:apply-templates select="IonEnergy"/>
			<xsl:apply-templates select="InterScanDelay"/>
			
		</xsl:element>
	</xsl:template>

	<msxsl:script language="VBScript" implements-prefix="SQ">
	
	<!-- A C#-like formatter, which replaces {0} with Sub0 and {1} with Sub1 -->
	Function FormatString(s,Sub0,Sub1)
	
		On Error Resume Next
		
		FormatString = s
		index0 = InStr( s,"{0}" )
	
		If index0 >= 1 Then
		
			FormatString = Mid(s, 1, index0-1)  + Sub0 + Mid( s, index0 + 3)
			index1 = InStr( FormatString, "{1}")
			
			If index1 >= 1 Then
				FormatString = Mid(FormatString, 1, index1-1) + Sub1 + Mid(FormatString, index1+3)
			End If
		
		End If
		
		On Error Goto 0 ' (Re-enable error trapping)
			
	End Function
	
	</msxsl:script>
	
</xsl:stylesheet>
