<?xml version="1.0" encoding="utf-8" ?>

<!-- ************************************************************************* -->
<!-- * This XSL-stylesheet contains templates for elements and scripts       * -->
<!-- * used by the form in system-edit.xsl and the spesific documenttype.    * -->
<!-- * Created 29. march 2004 by Even Halvorsen (even.halvorsen@usit.uio.no) * -->
<!-- ************************************************************************* -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


  <!-- *************************************************************** -->
  <!-- * insert-formscripts - inserts javascripts                    * -->
  <!-- *************************************************************** -->

  <xsl:template name="insert-formscripts">
    <xsl:comment>
      <![CDATA[ 
        function doconfirm(jcont,edaction) {
        document.form.editingAction.value=edaction;
        document.form.cont.value=jcont;
        document.form.submit();
        }
        
        function setBottomFrame() {
        if (top != self)
        parent.setBottomFrame();
        }
        
        function openBrowseWindow(url,fieldid) {
        open(url+'&id='+fieldid,'browsewindow','width=650,height=450,resizable=yes,right=0,top=0,screenX=0,screenY=0,scrollbars=yes');
        }
        
        function newSubElementAt(elementname, path) {
        document.form.editingAction.value='newSubElementAt';
        document.form.elementname.value = elementname;
        document.form.path.value = path;
        document.form.submit();
        }
    
        function deleteSubElementAt(path) {
        document.form.editingAction.value='deleteSubElementAt';
        document.form.path.value = path;
        document.form.submit();
        }
        
        // hører til sakskart og bør muligens flyttes ut av system-edit
        function formInsert(form, formelement, string) {
        document.forms[form][formelement].value = document.forms[form][formelement].value + string;
        }

      ]]> 
    </xsl:comment>
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-inputfield - creates a inputfield                    * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-inputfield">
    <xsl:param name="size" select="'55'" />
    <xsl:param name="value" select="." />
    
    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>
    <!-- hva med label? -->
    <!-- label for="{$path}"><xsl:value-of select="name()" />:</label -->
    <input type="text" size="{$size}" id="{$path}" name="{$path}" value="{$value}"/>
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-inputfield - creates a inputfield                    * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-hiddenfield">
    <xsl:param name="value" select="." />
    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>
    <input type="hidden" id="{$path}" name="{$path}" value="{$value}"/>
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-textarea - creates a texarea                         * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-textarea">
    <xsl:param name="rows" select="'10'" />
    <xsl:param name="cols" select="'55'" />
     <xsl:param name="value" select="." />
     
     <xsl:variable name="path">
       <xsl:call-template name="numericpath" />
     </xsl:variable>
     <!-- hva med label? -->
     <!-- label for="{$path}"><xsl:value-of select="name()" />:</label -->
     <textarea id="{$path}" name="{$path}" rows="{$rows}" cols="{$cols}"><xsl:value-of select="."/></textarea>
   </xsl:template>



  <!-- *************************************************************** -->
  <!-- * create-dropdown - creates a dropdown menu, with             * -->
  <!-- * values in the name-value list.                              * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-dropdown">
    <xsl:param name="name-value-list" /><!-- comma separated name=value list -->
    <xsl:param name="selected" select="."/>

    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>

    <select type="select" name="{$path}">
      <xsl:call-template name="create-dropdown-stringvalues">
        <xsl:with-param name="name-value-list" select="$name-value-list" />
        <xsl:with-param name="selected" select="$selected" />
      </xsl:call-template>
    </select>
  </xsl:template>

  <!-- help template -->
  <xsl:template name="create-dropdown-stringvalues">
    <xsl:param name="name-value-list" /><!-- comma separated name=value list -->
    <xsl:param name="selected" />

    <xsl:if test="not(normalize-space($name-value-list)='')">
      <xsl:variable name="name" select="substring-before($name-value-list, '=')" />
      <xsl:value-of select="$name" />
      <xsl:variable name="value">
        <xsl:choose>
          <xsl:when test="contains(substring-after($name-value-list, '='), ',')"> <!-- if comma in list -->
            <xsl:value-of select="substring-before(substring-after($name-value-list, '='), ',')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring-after($name-value-list, '=')" /> 
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <!-- write value -->
      <xsl:choose>
        <xsl:when test="$value=$selected">
          <option value="{$value}" selected="true"><xsl:value-of select="$name" /></option>
        </xsl:when>
        <xsl:otherwise>
          <option value="{$value}"><xsl:value-of select="$name" /></option>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:variable name="new-string">
        <!-- if more values -->       
        <xsl:choose>
          <!-- if comma in list -->
          <xsl:when test="contains(substring-after($name-value-list, $value), ',')">
            <!-- strip the comma -->
            <xsl:value-of select="substring-after(substring-after($name-value-list, $value), ',')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring-after(substring-after($name-value-list, '='), $value) " /> 
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      
      <!-- create more values -->
      <xsl:call-template name="create-dropdown-stringvalues">
        <xsl:with-param name="name-value-list" select="$new-string" /> 
        <xsl:with-param name="selected" select="$selected" />
      </xsl:call-template>
     </xsl:if>
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-dropdown-integer - creates a dropdown menu, with     * -->
  <!-- * values between the "from" and "to" parameter.               * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-dropdown-integer">
    <xsl:param name="from" />
    <xsl:param name="to" />
    <xsl:param name="selected" select="."/>
    <xsl:param name="step" select="'1'" />

    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>

    <select type="select" name="{$path}">
      <xsl:call-template name="create-dropdown-integervalues">
        <xsl:with-param name="from" select="$from" />
        <xsl:with-param name="to" select="$to" />
        <xsl:with-param name="default" select="$selected" />
        <xsl:with-param name="step" select="$step" />
      </xsl:call-template>
    </select>
  </xsl:template>

  <!-- help template -->
  <xsl:template name="create-dropdown-integervalues">
    <xsl:param name="from" />
    <xsl:param name="to" />
    <xsl:param name="selected" select="."/>
    <xsl:param name="step" select="'1'" />

    <xsl:if test="$from &lt;= $to">
      <xsl:variable name="value">
        <xsl:choose>
          <xsl:when test="$from &lt;= '9'"><xsl:value-of select="concat(0,$from)" /></xsl:when>
          <xsl:otherwise><xsl:value-of select="$from" /></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      
      <xsl:choose>
        <xsl:when test="$from=$selected">
          <option value="{$value}" selected="true"><xsl:value-of select="$value" /></option>
        </xsl:when>
        <xsl:otherwise>
          <option value="{$value}"><xsl:value-of select="$value" /></option>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:call-template name="create-dropdown-integervalues">
        <xsl:with-param name="from" select="$from + $step" />
        <xsl:with-param name="to" select="$to" />
        <xsl:with-param name="selected" select="$selected" />
        <xsl:with-param name="step" select="$step" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-radiobutton - creates a radiobutton, the button is   * -->
  <!-- * checked if vaule is equal the element content.              * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-radiobutton">
    <xsl:param name="value" />
    <xsl:param name="label" select="$value" />

    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>

    <!-- write radiobutton -->
    <xsl:choose>
      <xsl:when test=". = $value">
        <input id="{$value}" type="radio" name="{$path}" value="{$value}" checked="true"/>              
      </xsl:when>
      <xsl:otherwise>
        <input id="{$value}" type="radio" name="{$path}" value="{$value}" />                   
      </xsl:otherwise>
    </xsl:choose>
    &#160;<label for="{$value}">&#160;<xsl:value-of select="$label" /></label>
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-button - creates a button.                          * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-button">
    <xsl:param name="elementname" />
    <xsl:param name="buttonname" />
    <xsl:param name="position" select="'end'"/> <!-- position where element will be placed; values = (start | end) -->
    <xsl:variable name="path">
      <xsl:call-template name="numericpath"/> <!-- position where this template is called.  -->
      <xsl:choose> 
        <xsl:when test="$position='start'">
          <xsl:value-of select="'.0'" /> <!-- start -->        
        </xsl:when>
        <xsl:otherwise> 
          <xsl:value-of select="'.'" />
          <xsl:value-of select="count(child::*)" /> <!-- end -->     
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <input type="button" class="newbutton" onclick="newSubElementAt('{$elementname}','{$path}')" value="{$buttonname}" />
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-browsebutton - creates a button.                     * -->
  <!-- *************************************************************** -->
   <xsl:template name="insert-browsebutton">
     <xsl:param name="buttontext" select="'Se dokumenter ...'" />
     <xsl:variable name="path">
       <xsl:call-template name="numericpath" />
     </xsl:variable>
     <input type="button" value="Se dokumenter ..." onclick="openBrowseWindow('{$BROWSEURL}','{$path}')" />
   </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-delete-link - delete a element.                      * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-delete-link">
    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>
    <a target="_parent" href="javascript: deleteSubElementAt('{$path}')">slett</a>
  </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-edit-link - edit a element.                            * -->
  <!-- *************************************************************** -->
  <!-- gir redigeringslenke - må kalles inne i elementer som skal redigeres (mode="edit") -->
  <xsl:template name="insert-edit-link">
    <!-- vil ikke ha redigerlenke i andre modus en default --> 
    <xsl:if test="not(//processing-instruction('mode')='delete') and 
                  not(//processing-instruction('mode')='move') and
                  not(//processing-instruction('mode')='edit') and
                  not(//processing-instruction('mode')='newElement') and
                  not(//processing-instruction('mode')='newElementAt')">
      <xsl:variable name="path">
        <xsl:call-template name="numericpath"/>
      </xsl:variable>
      <xsl:variable name="editurl"> 
        <xsl:value-of select="concat(concat(concat($BASEURL,'&amp;action=edit&amp;editingAction=edit&amp;path='),$path),'#jump')"/>
      </xsl:variable>
      (<a href="{$editurl}" target="_parent">rediger</a>)
    </xsl:if>
  </xsl:template>





  <!-- *************************************************************** -->
  <!-- * Internal help-templates              .                      * -->
  <!-- *************************************************************** -->

  <xsl:template name="numericpath">
    <xsl:for-each select="ancestor::*">
      <xsl:sort select="position()" order="ascending" data-type="number"/>
      <xsl:value-of select="count(preceding-sibling::*) + 1"/>
      <xsl:text>.</xsl:text>
    </xsl:for-each>
    <xsl:value-of select="count(preceding-sibling::*) + 1"/>
  </xsl:template>

  <!-- brukes disse ? -->
  <xsl:template name="tracepath">
    <xsl:text>/</xsl:text>
    <xsl:for-each select="ancestor::*">
      <xsl:sort select="position()" order="descending" data-type="number"/>
      <xsl:value-of select="name()"/>
      <xsl:text>/</xsl:text>
    </xsl:for-each>
    <xsl:value-of select="name()"/>
  </xsl:template>

  <xsl:template name="filepath">
    <xsl:for-each select="ancestor-or-self::*">
      <xsl:if test="parent::*">
        <xsl:text>/</xsl:text>
        <xsl:value-of select="@name"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>



</xsl:stylesheet>
