<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.0" />

<!--
  Overall content area.
-->
<xsl:template match="/">
  <!-- Layout table to arrange content sections -->
  <table style="height:100%;">
    <tr>
      <!-- Tabs on left -->
      <td style="vertical-align:top;">
        <xsl:call-template name="createTabSection" />
      </td>
      <!-- Separator between tabs and content -->
      <td style="width:20px;"></td>
      <!-- Content on right -->
      <td style="vertical-align:top; padding-top:5px;">
        <xsl:call-template name="createContentSection" />
      </td>
      <!-- Right-hand margin -->
      <td class="rightMargin"></td>
    </tr>
  </table>
</xsl:template>

<!-- 
  createTabSection: Create tabs section on left.
-->
<xsl:template name="createTabSection">
  <!-- Layout table to arrange tabs -->
  <table cellpadding="0" id="tabTable"
         style="border-collapse:collapse; height:100%;">
    <!-- Create area around tab column header (for borders) -->
    <tr>
      <td style="height:65px;" class="tabMarginTop">
      </td>
    </tr>
    <!-- Call function to create each tab -->
    <xsl:for-each select="learningGuide/tabs/tab">
      <xsl:call-template name="createTab">
        <xsl:with-param name="tabID">
          <xsl:value-of select="concat('tab_',id)" />
        </xsl:with-param>
        <xsl:with-param name="title">
          <xsl:value-of select="title" />
        </xsl:with-param>
        <xsl:with-param name="icon">
          <xsl:value-of select="icon" />
        </xsl:with-param>
        <xsl:with-param name="tabOrder">
          <xsl:value-of select="1000*position()" />
        </xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>
    <!-- Create area below tabs (for borders) -->
    <tr>
      <td style="height:100%; vertical-align:bottom;" class="tabMarginBottom">
	<!-- Add ProAdvisor link -->
	<div style="padding-left:0px; padding-bottom:12px;">
          <table>
            <tr>
  	      <td style="vertical-align:top; padding-top:5px;">
                <img src="..\Help\images\bolt.gif"/>
              </td>
  	      <td style="padding-left:2px; width:95px;">
                <a href="javascript:;" class="tabLink" onClick="InvokeLink('http://usequickbooks.com/paonboarding1');">
                  Find a QuickBooks expert in your area
                </a>
              </td>
	      <td>
                <img src="pro_advisor.gif" />
              </td>
            </tr>
          </table>
        </div>
      </td>
    </tr>
  </table>
</xsl:template>

<!-- 
  createTab: Create a single tab.
    tabID    = unique id for tab, used by javascript to modify tab properties (normal, hover, selected)
    title    = text title displayed within tab
    icon     = link to image icon for tab
    tabOrder = tab order of tab (tabs are multiples of 1000, links for tab content are incremented from this)

    The image and title cells for the tab are given ID's derived from the tabID.
    This allows attibutes to be changed for the image and title when the main
    tab attributes are changed. 
-->
<xsl:template name="createTab">
    <xsl:param name="tabID" />
    <xsl:param name="title" />
    <xsl:param name="icon" />
    <xsl:param name="tabOrder" />
  <tr>
    <td class="tabNormal" id="{$tabID}"
        onmouseover="TabMouseOver(this);" 
        onmouseout="TabMouseOut(this);"
        onclick="TabClick(this);"
        onKeyPress="TabKeyPress(this);">
      <!-- This anchor tag is for the sole purpose of adding each tab to the keyboard navigation tab order -->
      <a href="javascript:;" style="width:100%; cursor:pointer;" name="tabLink">
        <xsl:attribute name="tabindex">
          <xsl:value-of select="$tabOrder" />
        </xsl:attribute>
        <!-- Layout table to arrange image and text within tab -->
        <table style="width:160px;">
          <tr>
            <!-- Image cell -->
            <td class="tabImageNormal">
              <xsl:attribute name="id">
                <xsl:value-of select="concat($tabID,'_image')" />
              </xsl:attribute>
              <img src="{$icon}" />
            </td>
            <!-- Text cell -->
            <td class="tabTextNormal">
              <xsl:attribute name="id">
                <xsl:value-of select="concat($tabID,'_text')" />
              </xsl:attribute>
              <!-- Call formatLineBreaks to replace line-feeds with <br/> -->
              <xsl:call-template name="formatLineBreaks">
                <xsl:with-param name="text">
                  <xsl:value-of select="$title" />
                </xsl:with-param>
              </xsl:call-template>
            </td>
          </tr>
        </table>
      </a>
    </td>
  </tr>
</xsl:template>

<!--
  createContentSection: Create content section on right.
-->
<xsl:template name="createContentSection">
  <!-- For each tab, call function to create associated content -->
  <xsl:for-each select="learningGuide/tabs/tab">
    <xsl:call-template name="createContent">
      <!-- Use the same unique id generated above for the tab -->
      <xsl:with-param name="tabID">
        <xsl:value-of select="concat('tab_',id)" />
      </xsl:with-param>
      <xsl:with-param name="tabOrder">
        <xsl:value-of select="1000*position()" />
      </xsl:with-param>
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

<!-- 
  createContent: Create content for given tab.
    tabID = unique id for the associated tab, used as basis for content id
    tabOrder = tab order of tab (tabs are multiples of 1000, links for tab content are incremented from this)
-->
<xsl:template name="createContent">
    <xsl:param name="tabID" />
    <xsl:param name="tabOrder" />
  <!-- The content for each tab is initially hidden. It is shown when the tab is clicked. -->
  <span style="display:none;" class="tabPadding">
    <!-- Assign an id based on the tab id so that the content can be found when the tab is clicked. -->
    <xsl:attribute name="id">
      <xsl:value-of select="concat($tabID,'_content')" />
    </xsl:attribute>

    <!-- Display the title if it is defined for this tab. -->
    <xsl:call-template name="displayIfDefined">
      <xsl:with-param name="text">
        <xsl:choose>
          <xsl:when test="pagetitle">
            <xsl:value-of select="pagetitle" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="title" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="class">
        <xsl:value-of select="'tabTitle'" />
      </xsl:with-param>
      <xsl:with-param name="format">
        <xsl:value-of select="'no'" />
      </xsl:with-param>
    </xsl:call-template>

    <!-- Display the subtitle if it is defined for this tab. -->
    <xsl:call-template name="displayIfDefined">
      <xsl:with-param name="text">
        <xsl:value-of select="subtitle" />
      </xsl:with-param>
      <xsl:with-param name="class">
        <xsl:value-of select="'tabSubTitle'" />
      </xsl:with-param>
      <xsl:with-param name="format">
        <xsl:value-of select="'no'" />
      </xsl:with-param>
    </xsl:call-template>

    <!-- Display each section defined for this content. -->
    <!-- Each section is one table, and all content lives within rows of that table. -->
    <!-- Only the "item" lines actually have individual cells, so the other rows use -->
    <!-- a colspan to span these cells. -->
    <table width="100%">
      <xsl:for-each select="section">
      <!-- If there are no items in section, and attribute is set -->
      <!-- to hide such a section, skip the section processing.   -->
      <xsl:if test="not(@hideIfNoItems) or @hideIfNoItems='N' or count(item) > 0">
        <xsl:call-template name="displaySection">
          <xsl:with-param name="sectionTabOrder">
            <xsl:value-of select="$tabOrder + 100*position()" />
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>
      </xsl:for-each>
    </table>
  </span>
</xsl:template>

<!-- 
  displaySection: Display all content for a section.
    sectionTabOrder = base tab order for this section
     Tab order is calculated as follows: 1000*[tab#] + 100*[section#] + [link#]
-->
<xsl:template name="displaySection">
    <xsl:param name="sectionTabOrder" />
  <!-- Separate sections with a blank line. -->
  <tr>
    <td colspan="6">
      <xsl:choose>
        <!-- If no spacing is specified for the section, add the standard amount -->
        <xsl:when test="not(@spacing)">
          <xsl:attribute name="class">
            <xsl:value-of select="'sectionSpacing'" />
          </xsl:attribute>
        </xsl:when>
        <!-- If spacing is specified for the section, use that -->
        <xsl:otherwise>
          <xsl:variable name="styleSpacing">
            <xsl:value-of select="concat('height:',@spacing,'px;')" />
          </xsl:variable>
          <xsl:attribute name="style">
            <xsl:value-of select="$styleSpacing" />
          </xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
    </td>
  </tr>

  <!-- Go through section elements and create corresponding content -->
  <xsl:for-each select="*">
    <xsl:choose>

      <!-- Display the section title -->
      <xsl:when test="name() = 'title'">
        <tr><td colspan="6">
          <span>
            <!-- Apply title formatting style -->
            <xsl:attribute name="class">
              <xsl:value-of select="'sectionTitle'" />
            </xsl:attribute>
            <!-- If indentation attribute is set, apply it -->
            <xsl:if test="@indent">
              <xsl:attribute name="style">
                <xsl:value-of select="concat('padding-left:',@indent,'px')" />
              </xsl:attribute>
            </xsl:if>
            <!-- Add title text -->
            <xsl:value-of select="." />
          </span>
        </td></tr>
      </xsl:when>

      <!-- Display the section text -->
      <xsl:when test="name() = 'text'">
        <tr><td colspan="6">
          <!-- If indentation attribute is set, apply it -->
          <xsl:if test="@indent">
            <xsl:attribute name="style">
              <xsl:value-of select="concat('padding-left:',@indent,'px')" />
            </xsl:attribute>
          </xsl:if>
          <span>
            <!-- Apply text formatting style -->
            <xsl:attribute name="class">
              <xsl:value-of select="'sectionText'" />
            </xsl:attribute>
            <!-- Call formatLineBreaks to replace line-feeds with <br/> -->
            <xsl:call-template name="formatLineBreaks">
              <xsl:with-param name="text">
                <xsl:value-of select="." />
              </xsl:with-param>
            </xsl:call-template>
          </span>
        </td></tr>
      </xsl:when>

      <!-- Display formatted text -->
      <xsl:when test="name() = 'formattext'">
        <tr><td colspan="6">
          <!-- If indentation attribute is set, apply it -->
          <xsl:if test="@indent">
            <xsl:attribute name="style">
              <xsl:value-of select="concat('padding-left:',@indent,'px')" />
            </xsl:attribute>
          </xsl:if>
          <!-- create the formatted text -->
          <xsl:call-template name="displayFormatText">
            <xsl:with-param name="className">
              <xsl:value-of select="'sectionText'" />
            </xsl:with-param>
          </xsl:call-template>
        </td></tr>
      </xsl:when>

      <!-- Display the section items -->
      <xsl:when test="name() = 'item'">
        <xsl:call-template name="displaySectionItem">
          <xsl:with-param name="tabOrder">
            <xsl:value-of select="position() + $sectionTabOrder" />
          </xsl:with-param>
        </xsl:call-template>
      </xsl:when>

      <!-- Display any text with links -->
      <xsl:when test="name() = 'textwithlinks'">
        <tr><td colspan="6">
          <!-- If indentation attribute is set, apply it -->
          <xsl:if test="@indent">
            <xsl:attribute name="style">
              <xsl:value-of select="concat('padding-left:',@indent,'px')" />
            </xsl:attribute>
          </xsl:if>
          <!-- create the text with links -->
          <xsl:call-template name="displayTextWithLinks">
            <xsl:with-param name="tabOrder">
              <xsl:value-of select="$sectionTabOrder + position()" />
            </xsl:with-param>
            <xsl:with-param name="textClass">
              <xsl:value-of select="'sectionText'" />
            </xsl:with-param>
          </xsl:call-template>
        </td></tr>
      </xsl:when>

      <!-- Add a blank line -->
      <xsl:when test="name() = 'blankline'">
        <xsl:variable name="lineheight">
          <xsl:choose>
            <xsl:when test="@height">
              <xsl:value-of select="concat(@height,'px')" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="'10px'" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <tr><td colspan="6" style="height:{$lineheight};"></td></tr>
      </xsl:when>

      <!-- Draw a separator line -->
      <xsl:when test="name() = 'drawline'">
        <tr><td colspan="6" class="sectionLine"></td></tr>
        <!-- Also, add a little extra padding between the header and the first link -->
        <tr><td colspan="6" class="firstLineSpacing"></td></tr>
      </xsl:when>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>


<!--
 displaySectionItem: Display a section link item.
   tabOrder = tab order for link
     Tab order is calculated as follows: 1000*[tab#] + 100*[section#] + [link#]
-->
<xsl:template name="displaySectionItem">
    <xsl:param name="tabOrder" />
  <!-- Create the row with a "link" attribute set to the name of the file to run. This attribute -->
  <!-- will be used to set checkmark bullets on rows for files that have been viewed.            -->
  <tr>
    <xsl:choose>
      <xsl:when test="file">
        <xsl:attribute name="tutorial">
          <xsl:value-of select="file" />
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="textwithlinks/link/file">
        <xsl:attribute name="tutorial">
          <xsl:value-of select="textwithlinks/link/file" />
        </xsl:attribute>
      </xsl:when>
    </xsl:choose>
    <!-- Cell for indentation, if required -->
    <td class="linkNoIndent">
      <xsl:if test="string-length(../title) > 0 or string-length(../text) > 0">
        <xsl:attribute name="class">
          <xsl:value-of select="'linkIndent'" />
        </xsl:attribute>
      </xsl:if>
    </td>
    <!-- Cell for bullet/checkmark image -->
    <td class="linkBullet">
      <img src="bullet_round.gif"/>
    </td>
    <!-- Cell for link text -->
    <xsl:choose>
      <!-- If a file link is defined, create a link -->
      <xsl:when test="file">
        <td class="linkText">
          <xsl:call-template name="displayLink">
            <xsl:with-param name="tabOrder">
              <xsl:value-of select="$tabOrder" />
            </xsl:with-param>
            <xsl:with-param name="clickMethod">
              <xsl:value-of select="'TutorialLinkClick'" />
            </xsl:with-param>
            <xsl:with-param name="keyMethod">
              <xsl:value-of select="'TutorialLinkKeyPress'" />
            </xsl:with-param>
            <xsl:with-param name="linkClass">
              <xsl:value-of select="'itemLink1'" />
            </xsl:with-param>
          </xsl:call-template>
        </td>
      </xsl:when>
      <!-- If a help id or string is defined or a qbcommand is defined, create a link -->
      <xsl:when test="helpid or helpstring or qbcommand">
        <td class="linkText">
          <xsl:call-template name="displayLink">
            <xsl:with-param name="tabOrder">
              <xsl:value-of select="$tabOrder" />
            </xsl:with-param>
            <xsl:with-param name="clickMethod">
              <xsl:value-of select="'EmbeddedLinkClick'" />
            </xsl:with-param>
            <xsl:with-param name="keyMethod">
              <xsl:value-of select="'EmbeddedLinkKeyPress'" />
            </xsl:with-param>
            <xsl:with-param name="linkClass">
              <xsl:value-of select="'itemLink1'" />
            </xsl:with-param>
          </xsl:call-template>
        </td>
      </xsl:when>
      <!-- If text+links is defined, create that -->
      <xsl:when test="textwithlinks">
        <td class="linkText">
          <xsl:for-each select="textwithlinks">
            <xsl:call-template name="displayTextWithLinks">
              <xsl:with-param name="tabOrder">
                <xsl:value-of select="$tabOrder" />
              </xsl:with-param>
              <xsl:with-param name="textClass">
                <xsl:value-of select="'itemText'" />
              </xsl:with-param>
            </xsl:call-template>
          </xsl:for-each>
        </td>
      </xsl:when>
      <!-- If none of the link types are defined, just display plain text -->
      <xsl:otherwise>
        <td class="noLinkText">
          <xsl:call-template name="displayTextOrFormatText" />
        </td>
      </xsl:otherwise>
    </xsl:choose>
    <td style="width:10%;"></td>
    <td class="linkTime">
      <xsl:value-of select="time" />
    </td>
    <td style="width:100%;"></td>
  </tr>
</xsl:template> 

<!-- 
  Display section content consisting of links mixed with text
   tabOrder = tab order for link
     Tab order is calculated as follows: 1000*[tab#] + 100*[section#] + [link#]
-->
<xsl:template name="displayTextWithLinks">
    <xsl:param name="tabOrder" />
    <xsl:param name="textClass" />
    <xsl:param name="linkClass" />
  <!-- Set class for text. Link class will be set on each link -->
  <span>
    <!-- Set style -->
    <xsl:attribute name="class">
      <xsl:value-of select="$textClass" />
    </xsl:attribute>
    <!-- Go through each node . . . -->
    <xsl:for-each select="*">
      <xsl:choose>

        <!-- For text nodes, display text as-is -->
        <xsl:when test="name() = 'text'">
          <xsl:value-of select="text()" />
        </xsl:when>

        <!-- For formattext nodes, construct the text -->
        <xsl:when test="name() = 'formattext'">
          <xsl:call-template name="displayFormatText">
            <xsl:with-param name="className">
              <xsl:value-of select="$textClass" />
            </xsl:with-param>
          </xsl:call-template>
        </xsl:when>

        <!-- Display line break -->
        <xsl:when test="name() = 'linebreak'">
          <br/>
        </xsl:when>

        <!-- Add line padding -->
        <xsl:when test="name() = 'linepadding'">
          <xsl:variable name="width">
            <xsl:choose>
              <xsl:when test="@width">
                <xsl:value-of select="concat(@width,'px')" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="'10px'" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <span style="margin-left:{$width};" />
        </xsl:when>

        <!-- Display links -->
        <xsl:when test="name() = 'link'">
          <xsl:call-template name="displayLink">
            <xsl:with-param name="tabOrder">
              <xsl:value-of select="$tabOrder" />
            </xsl:with-param>
            <xsl:with-param name="clickMethod">
              <xsl:value-of select="'EmbeddedLinkClick'" />
            </xsl:with-param>
            <xsl:with-param name="keyMethod">
              <xsl:value-of select="'EmbeddedLinkKeyPress'" />
            </xsl:with-param>
            <xsl:with-param name="linkClass">
              <xsl:choose>
                <!-- If the link if preceeded by other stuff, add some padding -->
                <xsl:when test="position() > 1">
                  <xsl:value-of select="'anchorPadding itemLink2'" />
                </xsl:when>
                <!-- If the link is at the beginning, don't add any padding -->
                <xsl:otherwise>
                  <xsl:value-of select="'itemLink2'" />
                </xsl:otherwise>
              </xsl:choose>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:when>

      </xsl:choose>
    </xsl:for-each>
    <br/>
  </span>
</xsl:template>

<!--
  displayLink: Display a link to a tutorial or help item
   tabOrder = tab order for link
     Tab order is calculated as follows: 1000*[tab#] + 100*[section#] + [link#]
   clickMethod = JavaScript method to call when link is clicked
   keyMethod = JavaScript method to call for keypress on link
     Handling is a little different between a standalone tutorial link and an embedded link, 
     so we call different methods.
-->
<xsl:template name="displayLink">
    <xsl:param name="tabOrder" />
    <xsl:param name="clickMethod" />
    <xsl:param name="keyMethod" />
    <xsl:param name="linkClass" />

  <!--  In order to properly handle both clicks and key presses on the link, we need   -->
  <!--  an anchor and a span tag. These will need slightly different callback methods. -->
  <xsl:variable name="onKeyMethod">
    <xsl:value-of select="concat('javascript:',$keyMethod,'(this);')" />
  </xsl:variable>
  <xsl:variable name="onClickMethod">
    <xsl:value-of select="concat('javascript:',$clickMethod,'(this.parentElement);')" />
  </xsl:variable>

  <xsl:if test="@external">
    <img src="..\Help\images\bolt.gif"/>
  </xsl:if>

  <!-- Event handling for an anchor tag is odd, so use a span element within the anchor -->
  <!-- The key press goes to the anchor tag fine, but we don't get it on the span.      -->
  <!-- A click handler on the anchor tag has odd behavior, so put this on the span.     -->
  <a href="javascript:;" class="sectionLink" onKeyPress="{$onKeyMethod}">

    <!-- Set tab order. -->
    <xsl:attribute name="tabindex">
      <xsl:value-of select="$tabOrder" /> 
    </xsl:attribute>

    <!-- The link to invoke is added as an attribute. The handler will grab this. -->
    <xsl:choose>
      <xsl:when test="file">
        <xsl:attribute name="link">
          <xsl:value-of select="file" />
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="helpid">
        <xsl:attribute name="helpid">
          <xsl:value-of select="helpid" />
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="helpstring">
        <xsl:attribute name="helpstring">
          <xsl:value-of select="helpstring" />
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="qbcommand">
        <xsl:attribute name="qbcommand">
          <xsl:value-of select="qbcommand" />
        </xsl:attribute>
      </xsl:when>
    </xsl:choose>

    <span class="{$linkClass}" onClick="{$onClickMethod}">
      <xsl:call-template name="displayTextOrFormatText" />
    </span>
  </a>
</xsl:template>

<!--
  displayTextOrFormatText: Display a simple text element or a formatted text element, depending on which is present
-->
<xsl:template name="displayTextOrFormatText">
  <xsl:choose>
    <xsl:when test="text">
      <xsl:value-of select="text" />
    </xsl:when>
    <xsl:when test="formattext">
      <xsl:for-each select="formattext">
        <xsl:call-template name="displayFormatText" />
      </xsl:for-each>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<!--
  displayFormatText: Display text that is comprised with multiple tags, each possibly with a different attribute.
-->
<xsl:template name="displayFormatText">
    <xsl:param name="className" />
  <xsl:for-each select="*">
    <xsl:choose>
  
      <xsl:when test="name() = 'linebreak'">
        <br/>
      </xsl:when>

      <xsl:when test="name() = 'text'">
        <span>
          <xsl:if test="$className">
            <xsl:attribute name="class">
              <xsl:value-of select="$className" />
            </xsl:attribute>
          </xsl:if>

          <xsl:variable name="attrBold">
            <xsl:choose>
              <xsl:when test="@bold">
                <xsl:value-of select="'font-weight:bold;'" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="''" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="attrItalics">
            <xsl:choose>
              <xsl:when test="@italics">
                <xsl:value-of select="'font-style:italic;'" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="''" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="attrUnderline">
            <xsl:choose>
              <xsl:when test="@underline">
                <xsl:value-of select="'text-decoration:underline;'" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="''" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:variable name="attrHighlight">
            <xsl:choose>
              <xsl:when test="@highlight">
                <xsl:value-of select="'color:#E68833;'" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="''" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>

          <xsl:attribute name="style">
            <xsl:value-of select="concat($attrBold,$attrItalics,$attrUnderline,$attrHighlight)" />
          </xsl:attribute>

          <xsl:value-of select="." />

        </span>
      </xsl:when>

    </xsl:choose>
  </xsl:for-each>
</xsl:template>

<!-- 
  displayIfDefined: Display text if it's defined.
    text   = text to display
    class  = CSS class to assign to text (defined in parachute.css)
    format = "yes" (default) to convert line-feeds to <br/>, "no" to not do this conversion
-->
<xsl:template name="displayIfDefined">
    <xsl:param name="text" />
    <xsl:param name="class" />
    <xsl:param name="format" select="'yes'" />
  <xsl:if test="$text and string-length($text) > 0">
    <span>
      <xsl:attribute name="class">
        <xsl:value-of select="$class" />
      </xsl:attribute>
      <xsl:choose>
        <xsl:when test="$format = 'yes'">
          <xsl:call-template name="formatLineBreaks">
            <xsl:with-param name="text">
              <xsl:value-of select="$text" />
            </xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$text" />
        </xsl:otherwise>
      </xsl:choose>
      <br/>
    </span>
  </xsl:if>
</xsl:template>

<!--
  formatLineBreaks: Replace line-feeds with <br/>
    text = text to format
-->
<xsl:template name="formatLineBreaks">
    <xsl:param name="text" select="''" />
  <xsl:choose>
    <!-- If text contains a linefeed, do linefeed processing -->
    <xsl:when test="contains($text,'&#x0A;')">
      <xsl:variable name="BeforeLineFeed">
        <xsl:value-of select="substring-before($text,'&#x0A;')" />
      </xsl:variable>
      <xsl:variable name="AfterLineFeed">
        <xsl:call-template name="removeWhiteSpace">
          <xsl:with-param name="text">
            <xsl:value-of select="substring-after($text,'&#x0A;')" />
          </xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$BeforeLineFeed" />
      <xsl:if test="string-length($BeforeLineFeed) > 0 and string-length($AfterLineFeed) > 0">
        <br/>
      </xsl:if>
      <xsl:call-template name="formatLineBreaks">
        <xsl:with-param name="text">
          <xsl:value-of select="$AfterLineFeed" />
        </xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <!-- If text does NOT contain a linefeed, output text as-is -->
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!--
  removeWhiteSpace: Trim leading whitespace from text.
    text = text to format
-->
<xsl:template name="removeWhiteSpace">
    <xsl:param name="text" select="''" />
  <xsl:variable name="testChar">
    <xsl:value-of select="substring($text,1,1)" />
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$testChar = '&#x0A;'">
      <xsl:call-template name="removeWhiteSpace">
        <xsl:with-param name="text">
          <xsl:value-of select="substring($text,2)" />
        </xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$testChar = ' '">
      <xsl:call-template name="removeWhiteSpace">
        <xsl:with-param name="text">
          <xsl:value-of select="substring($text,2)" />
        </xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet> 
