<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:qv="urn:QuickViewXmlUtil"
                xmlns:p="urn:PropertyXmlUtil">
   <xsl:template match="/">
      <xsl:text disable-output-escaping="yes">
      <![CDATA[<!DOCTYPE html>]]>
      </xsl:text>
      <html xmlns="http://www.w3.org/1999/xhtml">
         <head>
            <title>Configuration: Quick View</title>
            <style type="text/css">
               html { overflow:auto; }
               body { margin:6px; background-color:#e9edf1; font-family:Segoe UI, Tahoma, Arial, sans-serif; }
               div.outer { border:solid 1px #a2a2a2; }
               div.infoBar { font-family:Segoe UI, Tahoma, Arial, sans-serif; padding:5px 10px; border-bottom:1px solid saddlebrown; background:cornsilk; color:SaddleBrown; font-size:15px; background-image:url(../../site/styles/mscrm50/iconInformation.png); background-repeat:no-repeat; background-position:7px 7px; }
               div.infoBar span { margin-left:20px; display:block; }
               div.modelHeader { padding:4px 10px; font-weight:bold; font-size:16px; color:black; font-size:17px; background-color:#f3f3f3; background-image:url(../../site/styles/mscrm50/pgHeader.png); background-repeat:repeat-x; border-bottom:solid 1px #a2a2a2; }
               table.optionTable { width:100%; font-size:11px; border-collapse:collapse }
               tr.optionHeader td { background-image:url(../../site/styles/mscrm50/rgnTbBack.png); border-bottom:solid 1px #a2a2a2; background-color:white; background-position:left top; background-repeat:repeat-x; }
               tr.optionRow td { background-image:url(../../site/styles/mscrm50/rgnTbBack.png); border-bottom:solid 1px #a2a2a2; background-color:white; background-position:left top; background-repeat:repeat-x; }
               tr.optionHeader td { font-weight:bold; border-bottom-style:none; background-color:white; border-bottom:solid 1px #a2a2a2; }
               tr.optionProperty td { background-color:white; color:black; border-top:solid 1px #EDF1D5; }
               tr.totalRow td { font-weight:bold; border-top:solid 1px #a2a2a2; background-color:white; text-align:right; }
               tr.optionRow td.expandButton { width:20px; text-align:center; cursor:pointer; font-size:14px; color:white; font-weight:bold; background-image:url(../../site/styles/mscrm50/catRgnHdrDark.png); border-bottom:solid 1px #769AD2; background-color:#253B75; background-position:left top; background-repeat:repeat-x; }
               tr.prop { display:none; } /* by default collapse the table */
               tr.prop td.unused { background-color:white; }

               @media screen {
               body { background-repeat:no-repeat; background-position:right bottom; }
               }

               @media print {
               .noPrint, .expandButton { visibility:hidden;   /* suppressed, but still allocate the space for them */ }
               }
            </style>
            <script type="text/javascript">
               <![CDATA[
               function tog(opt) {
                  var model = opt.parentNode.parentNode;
                  if ( "" == opt.innerHTML ) return;
                  
                  var tr = model.getElementsByTagName("TR");
                  
                  // we cannot simply use a for loop with a less-than symbol due to how the server HtmlEncodes the the CDATA, resulting in
                  // a &lt;.  therefore, resort to a different loop mechanism
                  var i = 0;
                  while ( i != tr.length ) {
                     if ( tr[i].getAttribute('parent') == opt.id ) {
                        try{
                           tr[i].style.display = ( "+" == opt.innerHTML ) ? "table-row" : "none";
                        }
                        catch ( e ) {
                           tr[i].style.display = ( "+" == opt.innerHTML ) ? "block" : "none";
                        }
                     }
                     i++;
                  }
                  
                  opt.innerHTML = ( "-" == opt.innerHTML ) ? "+" : "-";
                  window.parent.resizeIFrame();
               }
               ]]>
            </script>
         </head>
         <body>
            <xsl:apply-templates />
         </body>
      </html>
   </xsl:template>

   <xsl:template match="mpcconfiguration">
      <xsl:apply-templates select="notfound" />
      <xsl:apply-templates select="error" />
      <xsl:apply-templates select="lineitem" />
   </xsl:template>

   <xsl:template match="error">
      <div style="margin:8px;">
         <span style="font-size:14px;font-weight:bold;">Unable to display the configuration in the Quick View.</span>
         <br />Please contact your administrator for assistance.
      </div>
   </xsl:template>

   <xsl:template match="notfound">
      <div style="margin:8px;">
         <span style="font-size:14px;font-weight:bold;">There is no configuration to display.</span>
         <br />A configuration must be saved before it can be displayed in the Quick View.
      </div>
   </xsl:template>

   <xsl:template match="lineitem">
      <xsl:variable name="lineItemId" select="@id" />
      <xsl:variable name="hasErrors" select="@haserrors" />
      <xsl:variable name="isDraft" select="@isdraft" />
      <div class="outer">
         <xsl:if test="$hasErrors > 0 or $isDraft > 0">
            <div class="infoBar">
               <xsl:if test="$hasErrors > 0">
                  <span>
                     <xsl:value-of select="qv:GetHasErrorsText()" />
                  </span>
               </xsl:if>
               <xsl:if test="$isDraft > 0">
                  <span>
                     <xsl:value-of select="qv:GetIsDraftModeText()" />
                  </span>
               </xsl:if>
            </div>
         </xsl:if>
         <div class="modelHeader">
            <xsl:choose>
               <xsl:when test="qv:HasConfigurationDisplayName($lineItemId) = true()">
                  <nobr>
                     <b>Name: </b>
                     <xsl:value-of select="qv:GetConfigurationDisplayName($lineItemId)" disable-output-escaping="yes" />
                  </nobr>
               </xsl:when>
               <xsl:otherwise>
                  <nobr>
                     <b>Series: </b>
                     <xsl:choose>
                        <xsl:when test="not(seriesdesc)">
                           <xsl:value-of select="@seriesid" />
                        </xsl:when>
                        <xsl:otherwise>
                           <xsl:value-of select="seriesdesc" />
                        </xsl:otherwise>
                     </xsl:choose>
                  </nobr>
                  &#160;&#160;&#160;
                  <nobr>
                     <b>Model: </b>
                     <xsl:choose>
                        <xsl:when test="not(modeldesc)">
                           <xsl:value-of select="@modelid" />
                        </xsl:when>
                        <xsl:otherwise>
                           <xsl:value-of select="modeldesc" />
                        </xsl:otherwise>
                     </xsl:choose>
                  </nobr>
               </xsl:otherwise>
            </xsl:choose>
         </div>
         <div style="height:1%">
            <table class="optionTable" cellpadding="3px" cellspacing="0" id="configuration">
               <tr class="optionHeader">
                  <td class="noPrint"></td>
                  <td style="width:170px">Category</td>
                  <td style="width:110px">Option ID</td>
                  <td>Description</td>
                  <td style="width:30px;text-align:center">Qty</td>
                  <td style="width:60px;text-align:right">Price</td>
               </tr>
               <xsl:apply-templates select="(category[not(@hide)]|category[@hide='0'])/option" />
               <tr class="totalRow">
                  <td colspan="5">Total Price:</td>
                  <td>
                     <xsl:value-of select="p:FormatProperty('Price', unitprice)" />
                  </td>
               </tr>
            </table>
         </div>
      </div>
   </xsl:template>

   <xsl:template match="option">
      <tr class="optionRow">
         <td class="expandButton" id="{generate-id(.)}" onclick="tog(this);">
            <xsl:if test="count(property) > 0">+</xsl:if>
         </td>
         <td style="width:170px">
            <xsl:value-of select="../description" />
         </td>
         <td style="width:110px">
            <xsl:choose>
               <xsl:when test="@runtimeId">
                  <xsl:value-of select="@runtimeId" />
               </xsl:when>
               <xsl:otherwise>
                  <xsl:value-of select="@id" />
               </xsl:otherwise>
            </xsl:choose>
         </td>
         <td>
            <xsl:value-of select="description" />
         </td>
         <td style="text-align:center;width:30px">
            <xsl:value-of select="quantity" />
         </td>
         <td style="text-align:right;width:60px">
            <xsl:value-of select="p:FormatProperty('Price', quantity * unitprice)" />
         </td>
      </tr>
      <xsl:apply-templates select="property[not(@hide)]|property[@hide='0']" />
   </xsl:template>

   <xsl:template match="property">
      <tr parent="{generate-id(..)}" class="prop">
         <td class="unused" colspan="2"></td>
         <td colspan="4">
            <xsl:call-template name="propLabel">
               <xsl:with-param name="propId" select="@id" />
            </xsl:call-template>
            <xsl:call-template name="formatProperty">
               <xsl:with-param name="propId" select="@id" />
               <xsl:with-param name="value" select="." />
            </xsl:call-template>
         </td>
      </tr>
   </xsl:template>

   <xsl:template name="propLabel">
      <xsl:param name="propId" />
      <xsl:choose>
         <xsl:when test="@label">
            <xsl:value-of select="@label" />: &#160;
         </xsl:when>
         <xsl:when test="ancestor::lineitem/labels/label[@id=$propId]">
            <xsl:value-of select="ancestor::lineitem/labels/label[@id=$propId]" />: &#160;
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$propId" />: &#160;
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <xsl:template name="formatProperty">
      <xsl:param name="propId" />
      <xsl:param name="value" />
      <xsl:choose>
         <xsl:when test="p:HasHtmlFormat($propId) = true()">
            <xsl:value-of select="p:FormatPropertyNoEscaping($propId, $value)" disable-output-escaping="yes" />
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="p:FormatProperty($propId, $value)" />
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>