<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <xsl:output method="xml" encoding="utf-8"/>
   <xsl:decimal-format name="european" decimal-separator="." grouping-separator=","/>
   <xsl:param name="mode"/>

   <!-- Maken 'bon' (XML) -->
   <xsl:template match="bestelopdracht">
      <!--<xsl:comment>Hotelselect booking approval.</xsl:comment>-->
      <request>
         <requestHeader>
            <userId>MSELECT</userId>
            <password>MSELECT</password>
            <requestType>confirmRequest</requestType>
            <language>en</language>
            <currency>EUR</currency>
            <bookingReference>O-<xsl:value-of select="id"/></bookingReference>
			<costCentre><xsl:value-of select="bestelling/kostenplaats/nr"/></costCentre>
         </requestHeader>
         <requestBody>
            <requestNumber><xsl:value-of select="bestelopdrachtitem/bestelitem/srtdeel/artikel_nummer"/></requestNumber>
            <approved>Y</approved>
            <creditCardType>MC</creditCardType>
            <creditCardNumber>5525383924657906</creditCardNumber>
            <creditCardCVC>645</creditCardCVC>
            <expiryDate>0916</expiryDate>
            <nameCreditCardOwner>K. Oosterhof</nameCreditCardOwner>
         </requestBody>
      </request>
   </xsl:template>

   <!-- Maken 'bon' (XML) -->
   <xsl:template match="facilitor">
      <xsl:apply-templates select="bestelopdracht"/>
   </xsl:template>
   
   <!-- Inpakken 'bon' in SOAP envelope -->
   <xsl:template match="SOAPEnvelope">
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
         <soap:Body>
            <MTServiceRequestString xmlns="http://tempuri.org/">
               <requestString>
                  <!-- Escape XML-header: &lt;?xml version="1.0" encoding="utf-8"?&gt; -->
                  <xsl:value-of select="substring-after(. , '?&gt;')"/>
               </requestString>
            </MTServiceRequestString>
         </soap:Body>
      </soap:Envelope>
   </xsl:template>


   <!-- Uitpakken SOAP envelope -->
   <xsl:template match="soap:Envelope">
      <MTServiceRequestStringResult>
         <xsl:value-of select="soap:Body"/>
      </MTServiceRequestStringResult>
   </xsl:template>

   <!-- Decoderen XML response -->
   <xsl:template match="MTServiceRequestStringResult">
      <Result>
         <xsl:choose>
            <xsl:when test="substring-before( substring-after(., 'resultOk&gt;'), '&lt;')='N'">
               Error #<xsl:value-of select="substring-before( substring-after(. , 'errorId&gt;'), '&lt;')"/>: <xsl:value-of select="substring-before( substring-after(. , 'errorMessage&gt;'), '&lt;')"/>
            </xsl:when>
            <!--
            <xsl:when test="substring-before( substring-after(., 'resultOk&gt;'), '&lt;')='Y'">
               Confirmation number: <xsl:value-of select="substring-before( substring-after(. , '&lt;confirmationNumber&gt;'), '&lt;')"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="."/>
            </xsl:otherwise>
            -->
         </xsl:choose>
         <!--
         <xsl:choose>
            <xsl:when test="MTResponse/responseHeader/resultOk='Y'">
               <xsl:value-of select="MTResponse/responseBody/confirmationNumber"/>
            </xsl:when>
            <xsl:when test="MTResponse/responseHeader/resultOk='N'">
               <xsl:value-of select="MTResponse/error/errorId"/>
               <xsl:value-of select="MTResponse/error/errorMessage"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="."/>
            </xsl:otherwise>
         </xsl:choose>
         -->
      </Result>
   </xsl:template>


   <!-- Root -->
   <xsl:template match="/">
      <xsl:choose>
         <xsl:when test="$mode='getExtension'">
            <xsl:element name="format">
               <!-- Hotelselect gebruikt geen cXML, maar SendFile.cls verwacht wel deze parameter om een XML response in te lezen -->
               <xsl:element name="extension">cxml</xsl:element>
            </xsl:element>
         </xsl:when>
         <xsl:otherwise>
            <!-- door SendFile.cls gebruikt voor maken 'bon' (XML die via SOAP verstuurd wordt) -->
            <xsl:apply-templates select="facilitor"/>
            <!-- door SendSOAP.cls gebruikt voor inpakken van 'bon' in SOAP envelope -->
            <xsl:apply-templates select="SOAPEnvelope"/>
            <!-- door SendSOAP.cls gebruikt voor uitpakken van response uit SOAP envelope -->
            <xsl:apply-templates select="soap:Envelope"/>
            <!-- door SendFile.cls gebruikt voor lezen van status uit response -->
            <xsl:apply-templates select="MTServiceRequestStringResult"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

</xsl:stylesheet>