<?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                    * -->
  <!-- *************************************************************** -->

  <!--      var editElementDoneServiceURL = '<xsl:value-of select="$editElementDoneServiceURL"/>'; -->
  <xsl:template name="insert-formscripts">
    <xsl:comment>   
      var formActionURL = '<xsl:value-of select="$formAction"/>';
      var newElementServiceURL = '<xsl:value-of select="$newElementServiceURL"/>';
      var newElementAtServiceURL = '<xsl:value-of select="$newElementAtServiceURL"/>';
      var newSubElementAtServiceURL = '<xsl:value-of select="$newSubElementAtServiceURL"/>';
      var deleteSubElementAtServiceURL = '<xsl:value-of select="$deleteSubElementAtServiceURL"/>';
      var moveElementServiceURL = '<xsl:value-of select="$moveElementServiceURL"/>';

      <![CDATA[ 
        function doconfirm(jcont,path) {
        document.form.action= formActionURL + "#" + path;
        document.form.cont.value=jcont;
        document.form.submit();
        }

        function submitMoveAction() {
        document.form.action = moveElementServiceURL;
        document.form.submit();
        }

        function submitAction(url) {
        document.form.action = url;
        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 newElement(elementname) {
        document.form.name.value = elementname;
        document.form.action = newElementServiceURL;
        document.form.submit();
        }

        function newElementAt(elementname, path) {
        document.form.name.value = elementname;
        document.form.at.value = path;
        document.form.action = newElementAtServiceURL + '#jump-expanded';
        document.form.submit();
        }
    
        function newSubElementAt(elementname, path) {
        var jumppath;
        if (path.charAt(path.length-1)=='0') {
          jumppath = path.substring(0, path.length-2); /* Jump to parent element */
        }
        else {
          jumppath = path;
        }
        document.form.elementname.value = elementname;
        document.form.path.value = path;
        document.form.action = newSubElementAtServiceURL + '#' + jumppath;
        document.form.submit();
        }
    
        function deleteSubElementAt(path) {
        var jumppath;
        if (path.charAt(path.length-1)=='1') {
          jumppath = path.substring(0, path.length-2); /* Jump to parent element */
        }
        else {
          jumppath = path.substring(0, path.length-1) + (path.substring(path.length-1)-1); /* Jump to element before this one*/
        }
        document.form.path.value = path;
        document.form.action = deleteSubElementAtServiceURL + '#' + jumppath;
        document.form.submit();
        }
        
        // virker ikke i IE (takler ikke id-er som "1.4.2" osv.)
        function formInsert(form, formelement, string) {
        document.forms[form][formelement].value = document.forms[form][formelement].value + string;
        }

        // Kun for sakskart
        function formInsertForAgenda() {
        var elements = document.getElementsByTagName('textarea');
        elements.item(0).value = elements.item(0).value + 'Notat fra universitetsdirektøren av xx.xx.2005 følger vedlagt.';
        }

        // Kun for emneplan
        function disableBestemteSemestre() {
        document.getElementById('bestemteSemestre').style.display = "none";
        }

        // Kun for emneplan
        function enableBestemteSemestre() {
        document.getElementById('bestemteSemestre').style.display = "block";
        }
        
        // For bruk til aa vise/skjule elementer
        function changeParentElement(elementId, parentName, displayvalue) {
        var e = document.getElementById(elementId);
        while (e.nodeName.toUpperCase() != parentName.toUpperCase()) e = e.parentNode; 
        e.style.display = displayvalue;
        }
        // Kun for Tavle
        function dateCheck() {
        
          var idag = new Date();
          
          // Sjekke at nedtaksdato er etter opphengsdato
          try {
            var opphengsdato_dag = document.getElementById('opphengsdato_dag').value;
            var opphengsdato_mnd = document.getElementById('opphengsdato_mnd').value;
            var opphengsdato_aar = document.getElementById('opphengsdato_aar').value;
            var nedtaksdato_dag = document.getElementById('nedtaksdato_dag').value;
            var nedtaksdato_mnd = document.getElementById('nedtaksdato_mnd').value;
            var nedtaksdato_aar = document.getElementById('nedtaksdato_aar').value;
          
            var opphengsdato = new Date();
            var nedtaksdato = new Date();
          
            opphengsdato.setFullYear(opphengsdato_aar, opphengsdato_mnd - 1, opphengsdato_dag);
            nedtaksdato.setFullYear(nedtaksdato_aar, nedtaksdato_mnd - 1, nedtaksdato_dag);
          
            if( nedtaksdato < opphengsdato ) {
              document.getElementById('nedtaksdato_error').style.setProperty('visibility','visible',null);
              document.getElementById('nedtaksdato_error').innerHTML = "Nedtaksdato er f&oslash;r opphengsdato!";
            }
            else if( nedtaksdato < idag ) {
              document.getElementById('nedtaksdato_error').style.setProperty('visibility','visible',null);
              document.getElementById('nedtaksdato_error').innerHTML = "Nedtaksdato er f&oslash;r dagens dato!";
            }
            else {
              document.getElementById('nedtaksdato_error').style.setProperty('visibility','hidden',null);
              document.getElementById('nedtaksdato_error').innerHTML = " - ";
            }
          } catch (exception) {
            window.status = "Not 'oppetid' component";
          }
          
          // Sjekke at evenementsdato ikke er før opphengsdato eller dagens dato
          try {
            var startdato_dag = document.getElementById('startdato_dag').value;
            var startdato_mnd = document.getElementById('startdato_mnd').value;
            var startdato_aar = document.getElementById('startdato_aar').value;            
            var startdato_time = document.getElementById('startdato_time').value;
            var startdato_min = document.getElementById('startdato_min').value;
            
            var sluttdato_dag = document.getElementById('sluttdato_dag').value;
            var sluttdato_mnd = document.getElementById('sluttdato_mnd').value;
            var sluttdato_aar = document.getElementById('sluttdato_aar').value;            
            var sluttdato_time = document.getElementById('sluttdato_time').value;
            var sluttdato_min = document.getElementById('sluttdato_min').value;            
            
            var startdato = new Date();
            var sluttdato = new Date();
            startdato.setFullYear(startdato_aar, startdato_mnd - 1, startdato_dag);
            sluttdato.setFullYear(sluttdato_aar, sluttdato_mnd - 1, sluttdato_dag);
                                                
            if( startdato < idag ) {
              removeEventdateErrorMessages()
              document.getElementById('startdato_error').style.setProperty('visibility','visible',null);
              document.getElementById('startdato_error').innerHTML = "Startdato er f&oslash;r dagens dato!";
            }
            else if ( startdato > sluttdato ) {
              removeEventdateErrorMessages()
              document.getElementById('sluttdato_error').style.setProperty('visibility','visible',null);
              document.getElementById('sluttdato_error').innerHTML = "Sluttdato er f&oslash;r startdato!";
            }            
            else {
              removeEventdateErrorMessages()
            }
          } catch (exception) {
            window.status = "Not 'evenementdato' component";
            window.status = exception.message;
          }          
        } // end dateCheck()
        
        // Hjelpefunksjon for dateCheck()
        function removeEventdateErrorMessages() {
          document.getElementById('startdato_error').style.setProperty('visibility','hidden',null);
          document.getElementById('startdato_error').innerHTML = " - ";            
          document.getElementById('sluttdato_error').style.setProperty('visibility','hidden',null);
          document.getElementById('sluttdato_error').innerHTML = " - ";
        }

      ]]> 
    </xsl:comment>
  </xsl:template> 


  <!-- =============================================================== -->
  <!-- =============================================================== -->
  <!-- Templates for editing elements of type choice, sequence and     -->
  <!-- elements with attributes.                                       -->
  <!-- =============================================================== -->
  <!-- =============================================================== -->

  <!-- *************************************************************** -->
  <!-- * sub-edit - use for editing choice and sequence elements.    * -->
  <!-- * template check the choice-subelements template defined in   * -->
  <!-- * system-edit and the spesific documenttype to find out       * -->
  <!-- * if the element is of type choice or sequence                * -->
  <!-- *************************************************************** -->



  <!-- *************************************************************** -->
  <!-- * choice-edit - use for editing choice elements               * -->
  <!-- *************************************************************** -->
  <xsl:template name="choice-edit">
    <xsl:param name="depth" />
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>
    <a name="{$path}" />
    <xsl:call-template name="attribute-edit" />

    <xsl:choose>
      <!-- do not use fieldset when element has one element-depth structure -->
      <xsl:when test="$depth = '1'">
        <table border="0" cellspacing="0" cellpadding="0">
          <xsl:for-each select="./*">
            <tr>
              <td style="padding: 3px 3px 3px 0;" valign="top" width="1%"><xsl:value-of select="name()" />:</td>
              <td style="padding: 3px;" valign="top" width="99%"><xsl:apply-templates select="." mode="edit" />&#160;<xsl:call-template name="insert-delete-button" /></td>
             
            </tr>
          </xsl:for-each>
        </table>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="./*">
          <fieldset>
            <legend><xsl:value-of select="name()" /></legend>
            <table class="nothing">
              <tr>
                <td class="elementvaluecell"><xsl:apply-templates select="." mode="edit"/></td>
                <td class="deletebuttoncell"><xsl:call-template name="insert-delete-button" /></td> 
              </tr>
            </table>
          </fieldset>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:variable name="subelements">
      <xsl:call-template name="choice-subelements" />
    </xsl:variable>
    <div class="subbuttons">
      <xsl:value-of select="insertsubElementText" />&#160;
      <xsl:call-template name="insert-buttons">
        <xsl:with-param name="elementlist" select="$subelements" />
      </xsl:call-template>
    </div>
  </xsl:template>

  <!-- *************************************************************** -->
  <!-- * sequence-edit - use for editing sequence elements           * -->
  <!-- *************************************************************** -->
  <xsl:template name="sequence-edit">
    <xsl:param name="depth" />
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>
    <a name="{$path}" />
    <xsl:call-template name="attribute-edit" />

    <xsl:choose>
      <!-- do not use fieldset when element has one element-depth structure -->
      <xsl:when test="$depth = '1'">
        <table border="0" cellspacing="0" cellpadding="0">
          <xsl:for-each select="./*">
            <tr>
              <td style="padding: 3px 3px 3px 0;" valign="top" width="10%"><xsl:value-of select="name()" />:</td>
              <td style="padding: 3px;" valign="top" width="90%"><xsl:apply-templates select="." mode="edit" /></td>
            </tr>
          </xsl:for-each>
        </table>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="./*">
          <fieldset>
            <legend><xsl:value-of select="name()" /></legend>
            <table class="nothing">
              <tr>
                <td class="elementvaluecell"><xsl:apply-templates select="." mode="edit"/></td>
              </tr>
            </table>
          </fieldset>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- *************************************************************** -->
  <!-- * attribute-edit - use for editing elements with attributes   * -->
  <!-- *************************************************************** -->
  <xsl:template name="attribute-edit">
    <!-- if attributes exists -->
    <xsl:if test="@*">
      <table class="nothing">
        <xsl:for-each select="./@*">
          <tr>
            <td valign="top" class="attributelabelcell"><xsl:value-of select="name()" />:</td>
            <td class="attributevaluecell"><xsl:apply-templates select="." mode="edit"/></td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if> 
  </xsl:template>



  <!-- =============================================================== -->
  <!-- =============================================================== -->
  <!-- Templates for editing elements and attributes of type           -->
  <!-- xsd:string. Choose one of the template under to spesify how to  -->
  <!-- input data.                                                     -->
  <!-- =============================================================== -->
  <!-- =============================================================== -->

  
  <!-- *************************************************************** -->
  <!-- * insert-inputfield - creates a inputfield                    * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-inputfield">
    <xsl:param name="size" select="'35'" />
    <xsl:param name="value" select="." />
    
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </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-maxlength - creates a inputfield with a   * -->
  <!-- *                               specified max content length  * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-inputfield-maxlength">
    <xsl:param name="size" select="'35'" />
    <xsl:param name="value" select="." />
    <xsl:param name="maxlength" /> 
    
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>
    <input type="text" size="{$size}" id="{$path}" name="{$path}" value="{$value}" maxlength="{$maxlength}" />
  </xsl:template>


  <!-- *************************************************************** -->
  <!-- * insert-hiddenfield - creates a hidden field                 * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-hiddenfield">
    <xsl:param name="value" select="." />
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </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="'65'" />
    <xsl:param name="value" select="." />
    <xsl:param name="helptext" select="'yes'" />
     
    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>
    <!-- hva med label? -->
    <!-- label for="{$path}"><xsl:value-of select="name()" />:</label -->
    <xsl:choose>
      <xsl:when test="$helptext='yes'">
        <table cellspacing="0" cellpadding="0" border="0">
          <tr>
            <td width="9%">
              <textarea id="{$path}" name="{$path}" rows="{$rows}" cols="{$cols}"><xsl:value-of select="."/></textarea>
            </td>
            <td width="1%">&#160;&#160;</td>
            <td width="90%" valign="top"><xsl:call-template name="textarea-help" /></td>
          </tr>  
        </table>
      </xsl:when>
      <xsl:when test="$helptext='with-reference'">
        <table cellspacing="0" cellpadding="0" border="0">
          <tr>
            <td width="9%">
              <textarea id="{$path}" name="{$path}" rows="{$rows}" cols="{$cols}"><xsl:value-of select="."/></textarea>
            </td>
            <td width="1%">&#160;&#160;</td>
            <td width="90%" valign="top"><xsl:call-template name="textarea-help-with-reference" /></td>
          </tr>  
        </table>
      </xsl:when>
      <xsl:otherwise> <!-- ikke behov for tabell rundt når det ikke skal være hjelpetekst -->
        <textarea id="{$path}" name="{$path}" rows="{$rows}" cols="{$cols}"><xsl:value-of select="."/></textarea>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  
  <!-- *************************************************************** -->
  <!-- * insert-textarea - creates a texarea without the table       * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-textarea-tableless">
    <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>
    <textarea id="{$path}" name="{$path}" rows="{$rows}" cols="{$cols}"><xsl:value-of select="."/></textarea>
  </xsl:template>

  
  <!-- *************************************************************** -->
  <!-- * insert-text-into-formelement                                * -->
  <!-- *************************************************************** -->

  <xsl:template name="insert-text-into-formelement">
    <xsl:param name="link-text" select="."/>
    <xsl:param name="insert-text" select="."/>

    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>
    <a href="javascript:formInsert('form', '{$path}', '{$insert-text}')"><xsl:value-of select="$link-text"/></a>
  </xsl:template>
  

  <!-- *************************************************************** -->
  <!-- * create-dropdown - creates a dropdown menu, with             * -->
  <!-- * values in the name-value list.                              * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-dropdown">
    <xsl:param name="separator" select="','" /> <!-- comma is default separator for the name=value list -->
    <xsl:param name="name-value-list" /><!-- comma separated name=value list -->
    <xsl:param name="selected" select="."/> <!-- can be used to overide element value -->
    <xsl:param name="default" /> <!-- can be used to give a default value when element or $selected is empty  -->

    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>

    <select type="select" name="{$path}">
      <xsl:call-template name="create-dropdown-stringvalues">
        <xsl:with-param name="separator" select="$separator" />
        <xsl:with-param name="name-value-list" select="$name-value-list" />
        <xsl:with-param name="selected" select="$selected" />
        <xsl:with-param name="default" select="$default" />
      </xsl:call-template>
    </select>
  </xsl:template>

  <!-- help template -->
  <xsl:template name="create-dropdown-stringvalues">
    <xsl:param name="separator" /> 
    <xsl:param name="name-value-list" /><!-- $separator separated name=value list -->
    <xsl:param name="selected" />
    <xsl:param name="default" />

    <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, '='), $separator)"> <!-- if $separator in list -->
            <xsl:value-of select="substring-before(substring-after($name-value-list, '='), $separator)" />
          </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 or ($selected='' and not($default='') and $value=$default)">
          <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 $separator in list -->
          <xsl:when test="contains(substring-after($name-value-list, $value), $separator)">
            <!-- strip the $separator -->
            <xsl:value-of select="substring-after(substring-after($name-value-list, $value), $separator)" />
          </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="separator" select="$separator" /> 
        <xsl:with-param name="name-value-list" select="$new-string" /> 
        <xsl:with-param name="selected" select="$selected" />
        <xsl:with-param name="default" select="$default" />
      </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:param name="with-null-prefix" select="true()" />
    <xsl:param name="empty-value" select="false()" />

    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>

    <select type="select" name="{$path}">
      <xsl:if test="$empty-value">
        <xsl:choose>
          <xsl:when test="normalize-space($selected)=''">
            <option value="" selected="true"></option>
          </xsl:when>
          <xsl:otherwise>
            <option value=""></option>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

      <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="selected" select="$selected" />
        <xsl:with-param name="step" select="$step" />
        <xsl:with-param name="with-null-prefix" select="$with-null-prefix" />
      </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:param name="with-null-prefix" />

    <xsl:if test="$from &lt;= $to">
      <xsl:variable name="value">
        <xsl:choose>
          <xsl:when test="$from &lt;= '9' and $with-null-prefix"><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:with-param name="with-null-prefix" select="$with-null-prefix"/>
      </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:param name="default" select="false()"/>
    <xsl:param name="onclick" />

    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>

    <xsl:choose>
      <!-- write radiobutton -->
      <xsl:when test=".=$value or (.='' and $default)">
        <!-- id="{concat($path,$value)}" for at label skal fungere -->
        <input onclick="{$onclick}" id="{concat($path,$value)}" type="radio" name="{$path}" value="{$value}" checked="true"/>              
      </xsl:when>
      <xsl:otherwise>
        <input onclick="{$onclick}" id="{concat($path,$value)}" type="radio" name="{$path}" value="{$value}"/>
      </xsl:otherwise>
    </xsl:choose>
    <label for="{concat($path,$value)}"><span style="font-size: 12px;">&#160;</span><xsl:value-of select="$label" /></label>
  </xsl:template>



  <!-- =============================================================== -->
  <!-- =============================================================== -->
  <!-- Templates for inserting buttons.                                -->
  <!-- =============================================================== -->
  <!-- =============================================================== -->


  <!-- rydd opp -->

  <!-- *************************************************************** -->
  <!-- * insert-buttons - insert buttons.                         * -->
  <!-- *************************************************************** -->
    <xsl:template name="insert-buttons">
      <xsl:param name="elementlist" />
      <xsl:param name="separator" select="','" />
   
      <xsl:if test="not($elementlist='')">
        <xsl:variable name="elementname">
          <xsl:choose>
            <xsl:when test="contains($elementlist, $separator)"> <!-- if $separator in list -->
              <xsl:value-of select="substring-before($elementlist, $separator)" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$elementlist" /> 
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <xsl:call-template name="insert-button">
          <xsl:with-param name="elementname" select="normalize-space($elementname)" />
        </xsl:call-template>&#160;

        <xsl:variable name="rest" select="normalize-space(substring-after($elementlist, concat($elementname,$separator)))" />

        <xsl:call-template name="insert-buttons">
          <xsl:with-param name="elementlist" select="$rest" />
        </xsl:call-template>&#160;
      </xsl:if>
    </xsl:template>



  <!-- *************************************************************** -->
  <!-- * insert-buttons-with-buttontekst - insert buttons (not in use)* -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-buttons-with-buttontext">
    <xsl:param name="separator" select="','" /> <!-- comma is default separator for the element=button list -->
    <xsl:param name="element-button-list" /><!-- comma separated name=value list -->

    <xsl:if test="not(normalize-space($element-button-list)='')">
      <xsl:variable name="element" select="substring-before($element-button-list, '=')" />

      <xsl:variable name="button">
        <xsl:choose>
          <xsl:when test="contains(substring-after($element-button-list, '='), $separator)"> <!-- if $separator in list -->
            <xsl:value-of select="substring-before(substring-after($element-button-list, '='), $separator)" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring-after($element-button-list, '=')" /> 
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
    
      <!-- make button -->
      <xsl:call-template name="insert-button">
        <xsl:with-param name="elementname" select="$element" />
        <xsl:with-param name="buttonname" select="$button" />
      </xsl:call-template>&#160;

      <!-- create new element button list -->
      <xsl:variable name="new-string">
        <!-- if more values -->       
        <xsl:choose>
          <!-- if $separator in list -->
          <xsl:when test="contains(substring-after($element-button-list, $button), $separator)">
            <!-- strip the $separator -->
            <xsl:value-of select="substring-after(substring-after($element-button-list, $button), $separator)" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring-after(substring-after($element-button-list, '='), $button) " /> 
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      
      <!-- create more buttons -->
      <xsl:call-template name="insert-buttons-with-buttontext">
        <xsl:with-param name="separator" select="$separator" /> 
        <xsl:with-param name="element-button-list" select="$new-string" /> 
      </xsl:call-template>
     </xsl:if>
  </xsl:template>


  <!-- *************************************************************** -->
  <!-- * insert-button - creates a button.                           * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-button">
    <xsl:param name="elementname" />
    <xsl:param name="buttonname" select="$elementname"/>
    <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 for the browseapp    * -->
  <!-- *************************************************************** -->
   <xsl:template name="insert-browsebutton">
     <xsl:param name="buttontext" select="'Se dokumenter ...'" />
     <xsl:variable name="path">
       <xsl:call-template name="numericpath" />
     </xsl:variable>
     <xsl:if test="not($BROWSEURL='')">
       <input type="button" value="{$browseButtonText}" onclick="openBrowseWindow('{$BROWSEURL}','{$path}')" />
     </xsl:if>
   </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}')"><xsl:value-of select="$deleteSubelementLink" /></a>
  </xsl:template>


  <!-- *************************************************************** -->
  <!-- * insert-delete-button - delete a element.                      * -->
  <!-- *************************************************************** -->
  <xsl:template name="insert-delete-button">
    <xsl:variable name="path">
      <xsl:call-template name="numericpath" />
    </xsl:variable>
    <input type="button" class="newbutton" onclick="deleteSubElementAt('{$path}')" value="{$deleteSubelementButton}" />
  </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 + -->
    <!-- kun redigeringslenke på elementer på 1. nivå --> 
    <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') and
                  (count(ancestor::*)='1')"> <!-- er nivået under rot -->
      <xsl:variable name="path">
        <xsl:call-template name="numericpath"/>
      </xsl:variable>
      <xsl:variable name="editurl"> 
        <!-- xsl:value-of select="concat(concat(concat($editElementServiceURL,'&amp;path='),$path),'#jump')"/ -->
        <xsl:value-of select="concat($editElementServiceURL,'&amp;path=',$path,'#jump-expanded')"/>
      </xsl:variable>
      (<a href="{$editurl}"><xsl:value-of select="$editLink" /></a>)
    </xsl:if>
  </xsl:template>


  <xsl:template name="insert-anchor">
    <xsl:variable name="path">
      <xsl:call-template name="xpath" />
    </xsl:variable>
    <a name="{$path}" />
  </xsl:template>

  <xsl:template name="textarea-help">
    <!-- a href="{$structuredtextHelpEmphasizeUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpEmphasizeText" /></a><br /><br />
    <a href="{$structuredtextHelpLinkUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpLinkText" /></a><br /><br />  
    <a href="{$structuredtextHelpListUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpListText" /></a><br /><br />
    <a href="{$structuredtextHelpParagraphUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpParagraphText" /></a -->
    <a href="{$structuredtextHelpUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpText" /></a>
  </xsl:template> 

  <xsl:template name="textarea-help-with-reference">
    <b><a href="{$structuredtextHelpReferenceUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpReferenceText" /></a></b><br/><br/>
    <a href="{$structuredtextHelpLinkUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpLinkText" /></a><br /><br/>
    <a href="{$structuredtextHelpListUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpListText" /></a><br /><br/>
    <a href="{$structuredtextHelpParagraphUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpParagraphText" /></a>
    <!-- a href="{$structuredtextHelpUrl}" target="help-formating"><xsl:value-of select="$structuredtextHelpText" /></a -->
  </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>


  <!-- This template support path-generation for attributes -->
  <!-- Should make all documents use this one instead of numericpath -->

  <xsl:template name="xpath">
    <xsl:for-each select="ancestor::*">
      <xsl:sort select="position()" order="ascending" data-type="number"/>
      <!-- Adding a "." if the element has a parent -->
      <xsl:if test="ancestor::*"><xsl:text>.</xsl:text></xsl:if>
      <xsl:value-of select="count(preceding-sibling::*) + 1"/>
    </xsl:for-each>
    <xsl:choose>
      <!-- Checking whether current() is an attribute or not -->
      <xsl:when test="count(. | ../@*) = count(../@*)">:<xsl:value-of select="name()"/></xsl:when>
      <xsl:otherwise>
        <xsl:text>.</xsl:text><xsl:value-of select="count(preceding-sibling::*) + 1"/>
      </xsl:otherwise>
    </xsl:choose>
  </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>
