<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:exslt="http://exslt.org/common"
 xmlns:set="http://exslt.org/sets"
 xmlns:string="http://exslt.org/strings"
 xmlns:math="http://exslt.org/math"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:dyn="http://exslt.org/dynamic"
 xmlns:xnl="urn:oasis:names:tc:ciq:xsdschema:xNL:2.0"
 xmlns:cil="urn:oasis:names:tc:ciq:xsdschema:xCIL:2.0"
 xmlns:vol="http://pcvs.org/2004/05/volunteerML"
 xmlns:stf="http://pcvs.org/2004/05/staffML">
  <xsl:import href="print_name.xslt"/>
  <xsl:import href="convert_date.xslt"/>
  <xsl:import href="standard_deviation.xslt"/>

  <xsl:output method="xml"
              standalone="no"
              indent="yes"
              doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
              doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
  <xsl:param name="type" />
  <xsl:param name="class" />
  <xsl:param name="author" />
  <xsl:param name="fullfile" />
  
  <xsl:variable name="population_set">
    <xsl:choose>
      <xsl:when test="$class = 'active'">
        <xsl:choose>
          <xsl:when test="count(//vol:volunteer[not(@status)]) &gt; 0">
            <xsl:text>//vol:volunteer[not(@status)]</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>//vol:volunteer</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$class != ''">
        <xsl:text>//vol:volunteer[@status = '</xsl:text>
        <xsl:value-of select="$class"/>
        <xsl:text>']</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>//vol:volunteer</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="population" select="dyn:evaluate($population_set)"/>

  <xsl:template match="vol:volunteers">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Volunteers for <xsl:value-of select="@country"/></title>
        <!-- <base href="http://mr.pcvs.org/"/> -->
        <link rel="stylesheet" type="text/css" href="styles/page.css"/>
        <link rel="stylesheet" type="text/css" href="styles/table.css"/>
        <link rel="stylesheet" type="text/css" href="styles/vols_list.css"/>
        <link rel="alternate stylesheet" type="text/css"
              title="Full Info" href="styles/vols_full.css"/>
        <link rel="alternate stylesheet" type="text/css"
              title="Phone List" href="styles/vols_phone.css"/>
        <link rel="stylesheet" href="table_arrow.css" type="text/css" />
        <script type="text/javascript"
                src="http://odin.himinbi.org/table_sort/table_sort.js">
        </script>
      </head>
      <body onload="if(allowSorting) {{ allowSorting('volunteers'); }}">
        <h1>Peace Corps Volunteers for <xsl:value-of select="@country"/>:</h1>
        <hr/>
        <xsl:if test="$type != 'secret'">
          <p>This is only a list of partial information because it is
          against Peace Corps regulations to release site assignments
          to the public. <xsl:if test="boolean($fullfile)">If you are
          a volunteer and would like to see a list with site
          assignments, email addresses and phone numbers you may visit
          <a href="{$fullfile}"> <xsl:value-of
          select="$fullfile"/></a>. <xsl:if test="boolean($author)">If
          you need the password <a href="mailto:{$author}">email
          me</a>.</xsl:if></xsl:if></p>
        </xsl:if>
        <table id="volunteers">
          <col id="name" />
          <xsl:choose>
            <xsl:when test="$type = 'private' or $type = 'secret'">
              <col id="photo" />
              <col id="email" />
              <col id="phone" />
              <col id="ext" />
              <xsl:if test="$type = 'secret'">
                <col id="site" />
              </xsl:if>
              <col id="birthday" />
            </xsl:when>
            <xsl:otherwise>
              <col id="sector" />
              <col id="home" />
              <col id="school" />
              <col id="links" />
            </xsl:otherwise>
          </xsl:choose>
          <thead>
            <tr>
              <th class="name">Name</th>
               <xsl:choose>
                <xsl:when test="$type = 'private' or $type = 'secret'">
                  <th class="photo"></th>
                  <th class="email">Email</th>
                  <th class="phone">Phone Number</th> 
                  <th class="ext">Bureau Extension</th> 
                  <xsl:if test="$type = 'secret'">
                    <th class="site">Site</th>
                  </xsl:if>
                  <th class="site">Birthday</th>
                </xsl:when>
                <xsl:otherwise>
                  <th class="sector">Sector</th>
                  <th class="school">Links</th>
                  <th class="home">Hometown</th>
                  <th class="school">Education</th>
                </xsl:otherwise>
              </xsl:choose>
            </tr>
          </thead>
          <tbody>
            <xsl:choose>
              <xsl:when test="$type != 'private' and $type != 'secret'">
                <xsl:apply-templates select="$population">
                  <xsl:sort select="xnl:PersonName/xnl:FirstName"/>
                </xsl:apply-templates>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="$population">
                  <xsl:sort select="concat(xnl:PersonName/xnl:KnownAs, xnl:PersonName/xnl:FirstName)"/>
                </xsl:apply-templates>
              </xsl:otherwise>
            </xsl:choose>
          </tbody>
        </table>
        <hr />
        <xsl:call-template name="statstable"/>
        <xsl:if test="function-available('exslt:node-set')">
          <xsl:variable name="ages">
            <!-- This takes two iterations to get a sort in because sorting on
               -  that dates can cause errors 1972-5-5 > 1972-12-01
              -->
            <xsl:variable name="agexml">
              <ages>
                <xsl:for-each select="$population//cil:BirthDate/cil:Date[text() != '']">
                  <age>
                    <xsl:call-template name="age">
                      <xsl:with-param name="date" select="."/>
                    </xsl:call-template>
                  </age>
                </xsl:for-each>
              </ages>
            </xsl:variable>
            <ages>
              <xsl:for-each select="exslt:node-set($agexml)//*[text()]">
                <xsl:sort/>
                <age><xsl:value-of select="self::node()"/></age>
              </xsl:for-each>
            </ages>
          </xsl:variable>
          <xsl:variable name="ageset" select="exslt:node-set($ages)//*[text()]"/>
          <xsl:variable name="numages" select="count($ageset)"/>
          <xsl:if test="$numages &gt; 0">
            <h2>
              <xsl:text>Average age: </xsl:text>
              
              <xsl:variable name="avgage">
                <xsl:value-of select="sum($ageset) div $numages"/>
              </xsl:variable>
              <xsl:value-of select="round($avgage * 10) div 10"/>
              <!-- <xsl:value-of select="math:std-deviation(exslt:node-set($ages))"/> -->
              <xsl:variable name="deviation">
                <xsl:call-template name="std-deviation">
                  <xsl:with-param name="set" select="$ageset"/>
                </xsl:call-template>
              </xsl:variable>
              <xsl:text> (&#x03c3;</xsl:text>
              <sub><xsl:value-of select="$numages"/></sub>
              <xsl:text> = </xsl:text>
              <xsl:value-of select="round($deviation * 100) div 100"/>
              <xsl:text>)</xsl:text>
            </h2>
            <table>
              <thead>
                <tr>
                  <th>Age:</th>
                  <xsl:for-each select="set:distinct($ageset)">
                    <th><xsl:apply-templates/></th>
                  </xsl:for-each>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <th>Count:</th>
                  <xsl:for-each select="set:distinct($ageset)">
                    <xsl:variable name="value" select="."/>
                    <xsl:variable name="numitem" select="count($ageset[self::node() = $value])"/>
                    <td>
                      <xsl:value-of select="$numitem"/>
                      <xsl:text> (</xsl:text>
                      <xsl:value-of select="round($numitem div $numages * 100)"/>
                      <xsl:text>%)</xsl:text>
                    </td>
                  </xsl:for-each>
                </tr>
              </tbody>
            </table>
          </xsl:if>
        </xsl:if>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="vol:volunteer">
    <xsl:element name="tr">
      <xsl:variable name="name">
        <xsl:apply-templates select="xnl:PersonName"/>
      </xsl:variable>
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="@status = 'is' or @status = 'ms'">is</xsl:when>
          <xsl:when test="not(@status)">active</xsl:when>
          <xsl:otherwise><xsl:value-of select="@status"/></xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <td class="name">
        <!--<a name="{translate($name, ' ABCDEFGHIJKLMNOPQRSTUVWXYZ', '_abcdefghijklmnopqrstuvwxyz')}"/>-->
        <xsl:choose>
          <xsl:when test="$type = 'private' or $type = 'secret'">
            <xsl:apply-templates select="xnl:PersonName" mode="familiar"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$name"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
      <xsl:choose>
        <xsl:when test="$type = 'private' or $type = 'secret'">
          <td class="photo"><xsl:apply-templates select="stf:photo"/></td>
          <td class="email"><xsl:apply-templates select="cil:EmailAddresses/cil:EmailAddress"/></td>
          <td class="phone"><xsl:apply-templates select="cil:ContactNumbers/cil:ContactNumber"/></td>
          <td class="ext"><xsl:apply-templates select="cil:ContactNumbers/cil:ContactNumber/cil:Extension"/></td>
          <xsl:if test="$type = 'secret'">
            <td class="site"><xsl:apply-templates select="vol:site"/></td>
          </xsl:if>
          <td class="birthday">
            <xsl:variable name="birthday">
              <xsl:value-of select="cil:BirthDate/cil:Date"/>
            </xsl:variable>
            <xsl:if test="$birthday != ''">
              <xsl:call-template name="padded_date">
                <xsl:with-param name="date"><xsl:value-of select="$birthday"/></xsl:with-param>
              </xsl:call-template>
              <xsl:variable name="age">
                <xsl:call-template name="age">
                  <xsl:with-param name="date"><xsl:value-of select="$birthday"/></xsl:with-param>
                </xsl:call-template>
              </xsl:variable>
              <xsl:if test="$age != ''">
                <xsl:text> (</xsl:text>
                <xsl:value-of select="$age"/>
                <xsl:text>)</xsl:text>
              </xsl:if>
            </xsl:if>
          </td>
        </xsl:when>
        <xsl:otherwise>
          <td class="sector"><xsl:apply-templates select="@sector"/></td>
          <td class="links">
            <xsl:apply-templates select="vol:url"/>
          </td>
          <td class="home"><xsl:apply-templates select="vol:home"/></td>
          <td class="school"><xsl:apply-templates select="vol:education"/></td>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:template>

  <xsl:template name="EmailAddress">
    <xsl:param name="output"/>
    <xsl:apply-templates select="../../xnl:PersonName" mode="familiar"/>
    <xsl:text> &lt;</xsl:text>        
    <xsl:apply-templates/>
    <xsl:text>&gt;</xsl:text>
  </xsl:template>

  <xsl:template match="cil:EmailAddress">
    <xsl:if test="count(text()) &gt; 0">
      <xsl:variable name="address">
        <xsl:if test="function-available('string:encode-uri')">
          <xsl:call-template name="EmailAddress"/>
        </xsl:if>
      </xsl:variable>
      <xsl:variable name="encoded_address">
        <xsl:text>mailto:</xsl:text>
        <xsl:choose>
          <xsl:when test="function-available('string:encode-uri')">
            <xsl:value-of select="string:encode-uri($address, false())"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <a href="{$encoded_address}"><xsl:apply-templates/></a>
    </xsl:if>
  </xsl:template>

  <xsl:template match="vol:url">
    <a href="{@href}">
      <xsl:value-of select="concat
                    (translate(substring(@type, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
                    substring(@type, 2))"/>
    </a>
  </xsl:template>

  <xsl:template match="stf:photo">
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:apply-templates/></xsl:attribute>
      <img src="http://will.mr.pcvs.org/camera_icon.png" alt="photo"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="cil:ContactNumber">
    <xsl:variable name="number" select="cil:Number"/>
    <xsl:if test="$number != ''">
      <div>
        <xsl:if test="count(cil:Type) != 0">
          <xsl:apply-templates select="cil:Type"/>
          <xsl:text>: </xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$number"/>
      </div>
    </xsl:if>
  </xsl:template>

  <xsl:template match="cil:ContactNumber/cil:Extension">
    <div><xsl:value-of select="."/></div>
  </xsl:template>

  <xsl:template match="vol:education">
    <xsl:apply-templates select="vol:award/vol:degree"/>
  </xsl:template>

  <xsl:template match="vol:degree">
    <div>
      <xsl:value-of select="@rank"/>
      <xsl:text> in </xsl:text>
      <xsl:value-of select="@subject"/>
    </div>
  </xsl:template>

  <xsl:template name="statstable">
    <table>
      <thead>
        <tr>
          <th>Sector</th>
          <xsl:if test="not($class)">
            <th><div>Number <a href="volunteers.html">Active</a></div><div>(% of total)</div></th>
            <th><div>Number <a href="resigned.html">ET</a></div><div>(% of sector / % of et's)</div></th>
            <th><div>Number <a href="interrupted_service.html">IS</a>/<a href="medically_separated.html">MS</a></div><div>(% of sector / % of total is/ms's)</div></th>
            <th><div>Number <a href="honor_roll.html">COS</a></div><div>(% of sector / % of cos's)</div></th>
          </xsl:if>
          <th>Total</th>
        </tr>
      </thead>
      <tbody>
        <xsl:if test="function-available('set:distinct')">
          <xsl:for-each select="set:distinct($population//@sector)">
            <xsl:sort/>
            <xsl:call-template name="statsrow">
              <xsl:with-param name="sector" select="."/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:if>
        <tr>
          <td>Total:</td>
          <xsl:if test="not($class)">
            <xsl:call-template name="totalcell">
              <xsl:with-param name="subset_size"
                              select="count($population[not(@status)]|$population[@status = 'stage'])"/>
            </xsl:call-template>
            <xsl:call-template name="totalcell">
              <xsl:with-param name="subset_size"
                              select="count($population[@status='et'])"/>
            </xsl:call-template>
            <xsl:call-template name="totalcell">
              <xsl:with-param name="subset_size"
                              select="count($population[@status='is']|$population[@status='ms'])"/>
            </xsl:call-template>
            <xsl:call-template name="totalcell">
              <xsl:with-param name="subset_size"
                              select="count($population[@status='cos'])"/>
            </xsl:call-template>
          </xsl:if>
          <td><xsl:value-of select="count($population)"/></td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
    
  <xsl:template name="statsrow">
    <xsl:param name="sector"/>
    <xsl:if test="function-available('string:tokenize')">
      <xsl:variable name="active"
                    select="count($population[@sector=$sector][not(@status)]) +
                    count($population[@sector=$sector][@status='stage'])"/>
      <xsl:variable name="active_total"
                    select="count($population[not(@status)]|$population[@status = 'stage'])"/>
      
      <tr>
        <td><xsl:value-of select="$sector"/></td>
        <xsl:if test="not($class)">
          <td>
            <xsl:value-of select="$active"/>
            <xsl:text> (</xsl:text>
            <xsl:choose>
              <xsl:when test="$active > 0">
                <xsl:value-of select="ceiling($active div $active_total * 100)"/>
              </xsl:when>
            <xsl:otherwise>
              <xsl:text>0</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
            <xsl:text>%)</xsl:text>
          </td>
          
          <xsl:for-each select="string:tokenize('et is cos')">
            <xsl:call-template name="statscell">
              <xsl:with-param name="sector" select="$sector"/>
              <xsl:with-param name="status" select="."/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:if>

        <xsl:call-template name="totalcell">
          <xsl:with-param name="subset_size" select="count($population[@sector=$sector])"/>
        </xsl:call-template>
      </tr>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="statscell">
    <xsl:param name="sector"/>
    <xsl:param name="status"/>
    <xsl:variable name="stat_and_sec"
                  select="count($population[@sector=$sector][@status=$status])"/>
    <xsl:variable name="stat"
                  select="count($population[@status=$status])"/>
    <xsl:variable name="sec"
                  select="count($population[@sector=$sector])"/>
    <td>
      <xsl:value-of select="$stat_and_sec"/>
      <xsl:text> (</xsl:text>
      <xsl:choose>
        <xsl:when test="$sec > 0 and $stat_and_sec > 0">
          <!-- This would be agfo et's / total agfo's -->
          <xsl:value-of select="ceiling($stat_and_sec div $sec * 100)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>0</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>% / </xsl:text>
      <xsl:choose>
        <xsl:when test="$sec > 0 and $stat_and_sec > 0">
          <!-- This would be agfo et's / total et's -->
          <xsl:value-of select="ceiling($stat_and_sec div $stat * 100)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>0</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>%)</xsl:text>
    </td>
  </xsl:template>

  <xsl:template name="totalcell">
    <xsl:param name="subset_size"/>
    <xsl:variable name="total">
      <xsl:value-of select="count($population)"/>
    </xsl:variable>
    <td>
      <xsl:value-of select="$subset_size"/>
      <xsl:text> (</xsl:text>
      <xsl:choose>
        <xsl:when test="$subset_size > 0 and $total > 0">
          <xsl:value-of select="ceiling($subset_size div $total * 100)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>0</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>%)</xsl:text>
    </td>
  </xsl:template>
</xsl:stylesheet>
