<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
		version="1.0" xmlns:xdif="http://www.wa5znu.org/2006/04/xdif/"
		xmlns:exslt="http://exslt.org/common" xmlns:math="http://exslt.org/math" 
		xmlns:eqp="http://elecraft.com/Awards/eqp_2006.htm"
		extension-element-prefixes="exslt math">

  <xsl:output method="text" />

  <!-- Rules table for this contest -->
  <xsl:variable name="rules" select="exslt:node-set(document('')/xsl:stylesheet/eqp:rules)" />

  <!-- definition expansion -->
  <!-- TODO: Need to do definition expansion -->

  <!-- DUPS -->
  <!-- Multiple Contacts with Same Station:
       Each station may be worked on each mode and each band for QSO credit. The three modes are CW, SSB, and Digital.
       Work the same station again on the same band and mode IF Elecraft rig changes. Both parties should count the duplicate QSO, but ONLY under these conditions!
       TODO: Won't eliminate dup where the SN is present in one QSO and absent in another.
  -->
  <xsl:key
      name="qsosByCallBandModeSN" match="xdif:qso[xdif:om/xdif:control/xdif:callsign and xdif:band and xdif:mode and xdif:rx and xdif:tx]"
      use="concat(xdif:om/xdif:control/xdif:callsign, '-', xdif:band, '-', xdif:mode, '-' xdif:rx/eqn:serialNumber, '-', xdif:tx/eqn:serialNumber)" />
  <xsl:variable
      name="nodupslist"
      select="/xdif:log/xdif:qsos/xdif:qso[generate-id(.) = generate-id(key('qsosByCallBandModeSN', concat(xdif:om/xdif:control/xdif:callsign, '-', xdif:band, '-', xdif:mode, '-' xdif:rx/eqn:serialNumber, '-', xdif:tx/eqn:serialNumber)))]" />
 
  <!-- States and Provinces count only in the US and Canada.  No need to filter duplicate QSOs -->
  <xsl:key 
   name="statesOrProvinces" 
   match="xdif:qso/xdif:om/xdif:location[xdif:state and (xdif:dxcc = 291 or xdif:dxcc = 1)]" use="xdif:state" />
  <xsl:variable 
   name="stateProvinceList" 
   select="exslt:node-set(/xdif:log/xdif:qsos/xdif:qso/xdif:om/xdif:location/xdif:state[generate-id(..)=generate-id(key('statesOrProvinces', .))])" />

  <!-- DXCC (Other than US or Canada) No need to filter duplicate QSOs -->
  <xsl:key 
   name="dxccs" 
   match="xdif:qso/xdif:om/xdif:location[xdif:dxcc != /xdif:log/xdif:my/xdif:location/xdif:dxcc]" use="xdif:dxcc" />
  <xsl:variable
   name="dxcclist" 
   select="exslt:node-set(/xdif:log/xdif:qsos/xdif:qso/xdif:om/xdif:location/xdif:dxcc[generate-id(..)=generate-id(key('dxccs', .))])" />

  <!-- SPC -->
  <xsl:variable name="statecount" select="count($stateProvinceList)" />
  <xsl:variable name="dxcccount" select="count($dxcclist)" />
  <xsl:variable name="spcs" select="$statecount + $dxcccount" />

  <!-- Power bonus is based on maximum power -->
  <xsl:variable name="maxpower" select="math:max(/xdif:log/xdif:qsos/xdif:qso/xdif:my/xdif:equipment/xdif:power)" />
  
  <!-- Highest power used at any time on any mode determines multiplier, so we use minimum multiplier.
       Dups count towards highest power used.-->
  <xsl:variable name="powerMultiplierList">
    <xsl:for-each select="/xdif:log/xdif:qsos/xdif:qso">
      <xsl:variable name="power" select="xdif:my/xdif:equipment/xdif:power" />
      <xsl:variable name="mode" select="xdif:mode" />
      <multiplier>
	<xsl:value-of select="$rules/ep:multipliers/eqp:choice[xdif:mode=$mode]/power[(@maxExclusive and $power &lt;= @maxExclusive) or (@maxInclusive and $power &lt; @maxInclusive)]/multiplier" />
      </multiplier>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="powermuliplier" select="min(exsl:node-set($powerMultiplierList)/multiplier)" />

  <!-- QSO point is 5 for s/n, 4 for other contintent, 2 otherwise. Eliminate dups.-->
  <xsl:variable name="qsoPointsList">
    <xsl:for-each select="common:node-set($qsos)">
      <xsl:choose>
	<xsl:when test="xdif:rx/eqn:serialNumber"><points>5</points></xsl:when>
	<xsl:when test="not(xdif:rx/eqn:serialNumber) and (xdif:om/xdif:location/xdif:continent != xdif:my/xdif:location/xdif:continent)"><point>4</point></xsl:when>
	<xsl:otherwise><points>2</points></xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="qsopoints" select="sum(exsl:node-set($qsoPointsList)/points)" />


  <!-- This template copies the input document, applying the exceptions below to the entire document -->
  <xsl:template match="@*|node()" >
    <xsl:apply-templates select="*"/>
  </xsl:template>

  <xsl:template match="xdif:qsos" >
    SPCS:
    <xsl:value-of select="$spcs" />
    <xsl:text>&#xa;</xsl:text>
    POINTS:
    <!-- score = QSO points X S/P/C's X Power Mult-->
    <xsl:value-of select="(qsopoints * $spcs * $powermultiplier" />
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <!-- Rules for the EQP QSO Party -->
  <rules xmlns="http://elecraft.com/Awards/eqp_2006.htm">
    <multipliers>
      <!-- rewrite to use xpath? -->
      <choice>
	<xdif:mode emission="J3E">SSB</xdif:mode>
	<power maxExclusive="0.500"><multiplier>15</multiplier></power>
	<power maxExclusive="2"><multiplier>10</multiplier></power>
	<power maxExclusive="10"><multiplier>7</multiplier></power>
	<power maxInclusive="100"><multiplier>1</multiplier></power>
	<power maxInclusive="1500"><multiplier>0</multiplier></power>
      </choice>
      <choice>
	<power maxExclusive="0.250"><multiplier>15</multiplier></power>
	<power maxExclusive="1"><multiplier>10</multiplier></power>
	<power maxExclusive="5"><multiplier>7</multiplier></power>
	<power maxInclusive="100"><multiplier>1</multiplier></power>
	<power maxInclusive="1500"><multiplier>0</multiplier></power>
      </choice>
    </multipliers>
    <bonuses />
    <defaults />
  </rules>
  
</xsl:stylesheet>



