<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                xmlns:system="clr-namespace:System;assembly=mscorlib"
                xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
                xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                xmlns:ddwrt2="urn:frontpage:internal"
                xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:pcm="urn:PageContentManager"
                exclude-result-prefixes="xsl ddwrt">
  <xsl:output method="html" indent="no"/>
  <xsl:param name="ItemID" select="'0'" />
  <xsl:param name="XmlDefinition" />
  <xsl:template match="/">
    <xsl:apply-templates  mode="Form" select="$XmlDefinition/*[1]" />
  </xsl:template>
  <xsl:template mode="Component" match="TabControl">
    <xsl:variable name="thisNode" select="."/>
    <ul>
      <xsl:for-each select="TabItem">
        <li id="TabCell{position()}">
          <xsl:attribute name="class">
            <xsl:choose>
              <xsl:when test="position()!=1">
                <xsl:call-template name="ClassHelper">
                  <xsl:with-param name="style" select="$thisNode/@Style"/>
                </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$thisNode/TabControl.Resources/system:String[@x:Key='StyleSelected']"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <a href="#" onclick="javascript:SwitchTab('{position()}')" onmouseover="javascript:HoverOver('{position()}')" onmouseout="javascript:HoverOut('{position()}')">
            <xsl:call-template name="ResourceHandlerHelper">
              <xsl:with-param name="id">TabName<xsl:value-of select="position()"/>_Literal</xsl:with-param>
              <xsl:with-param name="Name" select="@Header" />
            </xsl:call-template>
          </a>
        </li>
      </xsl:for-each>
    </ul>
    <div id="TabsContent">
      <xsl:for-each select="TabItem">
        <div id="Tab{position()}" class="TabContent">
          <xsl:attribute name="style">
            <xsl:choose>
              <xsl:when test="position()!=1">display:none;</xsl:when>
              <xsl:otherwise>display:block;</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:apply-templates mode="Section" select="StackPanel" />
        </div>
      </xsl:for-each>
    </div>
  </xsl:template>
  <xsl:template name="ClassHelper">
    <xsl:param name="style"/>
    <xsl:choose>
      <xsl:when test="contains($style, '}') and contains($style, ' ')">
        <xsl:value-of select ="substring-after(substring-before($style, '}'), ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select ="$style"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template mode="Section" match="StackPanel">
    <xsl:apply-templates mode="Component" select="*"/>
  </xsl:template>
  <xsl:template mode="Component" match="Grid">
    <table class="ms-formtable" style="margin-top: 8px;" border="0" cellpadding="0" cellspacing="0" width="100%">
      <xsl:if test="@Style != ''">
        <xsl:attribute name="class">
          <xsl:call-template name="ClassHelper">
            <xsl:with-param name="style" select="@Style"/>
          </xsl:call-template>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@Height != '' or @Width != ''">
        <xsl:attribute name="style">
          <xsl:if test="@Height != ''">
            height: <xsl:value-of select="@Height"/>;
          </xsl:if>
          <xsl:if test="@Width != ''">
            width: <xsl:value-of select="@Width"/>;
          </xsl:if>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates mode="Row" select="Grid.RowDefinitions/RowDefinition"/>
    </table>
  </xsl:template>
  <xsl:template mode="Component" match="HeaderedContentControl">
    <fieldset>
      <xsl:if test="@ID">
        <xsl:attribute name="id"><xsl:value-of select="@ID"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="not(@ShowLabel='0' or @ShowLabel='false')">
        <legend>
          <xsl:call-template name="ResourceHandlerHelper">
            <xsl:with-param name="id">SectionName<xsl:value-of select="@ID"/>_Literal</xsl:with-param>
            <xsl:with-param name="Name" select="@Header" />
          </xsl:call-template>
        </legend>
      </xsl:if>
      <xsl:apply-templates mode="Component" select="*"/>
    </fieldset>
  </xsl:template>
  <xsl:template mode="Row" match="RowDefinition">
    <tr>
      <xsl:if test="@Height != ''">
        <xsl:attribute name="style"> height:<xsl:value-of select="@Height"/>;</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates mode="Cell" select="../../Grid.ColumnDefinitions/ColumnDefinition">
        <xsl:with-param name="rowPosition" select="position()"/>
      </xsl:apply-templates>
    </tr>
  </xsl:template>
  <xsl:template mode="Cell" match="ColumnDefinition">
    <xsl:param name="rowPosition" select="1"/>
    <xsl:variable name="colPosition" select="position()"/>
    <xsl:apply-templates mode="Grid" select="../../*[@Grid.Column+1=$colPosition and @Grid.Row+1=$rowPosition]">
      <xsl:with-param name="thisNode" select="."/>
      <xsl:with-param name="position" select="$colPosition"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template mode="Grid" match="*">
    <xsl:param name="thisNode" select="."/>
    <xsl:param name="position" select="1"/>
    <td valign="top">
      <xsl:if test="$thisNode/@Colspan">
        <xsl:attribute name="colspan">
          <xsl:value-of select="$thisNode/@Colspan"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="$thisNode/@Rowspan">
        <xsl:attribute name="rowspan">
          <xsl:value-of select="$thisNode/@Rowspan"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="$thisNode/@Height != '' or $thisNode/@Width != '' or $thisNode/@BottomPadding != '' or $thisNode/@TopPadding != '' or $thisNode/@LeftPadding != '' or $thisNode/@RightPadding != ''">
        <xsl:attribute name="style">
          <xsl:if test="$thisNode/@Height != ''">height: <xsl:value-of select="$thisNode/@Height"/>;</xsl:if>
          <xsl:if test="$thisNode/@Width != ''">width: <xsl:value-of select="$thisNode/@Width"/>;</xsl:if>
          <xsl:if test="$thisNode/@BottomPadding != ''">padding-bottom: <xsl:value-of select="$thisNode/@BottomPadding"/>;</xsl:if>
          <xsl:if test="$thisNode/@TopPadding != ''">padding-top: <xsl:value-of select="$thisNode/@TopPadding"/>;</xsl:if>
          <xsl:if test="$thisNode/@LeftPadding != ''">padding-left: <xsl:value-of select="$thisNode/@LeftPadding"/>;</xsl:if>
          <xsl:if test="$thisNode/@RightPadding != ''">padding-right: <xsl:value-of select="$thisNode/@RightPadding"/>;</xsl:if>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="$thisNode/@Style != ''">
        <xsl:attribute name="class">
          <xsl:call-template name="ClassHelper">
            <xsl:with-param name="style" select="$thisNode/@Style"/>
          </xsl:call-template>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="$position=1">
        <xsl:attribute name="width">165px</xsl:attribute>
        <xsl:attribute name="nowrap">nowrap</xsl:attribute>
      </xsl:if>
      <xsl:if test="$position=2">
        <xsl:attribute name="width">450px</xsl:attribute>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="$position=1">
          <h3 class="ms-standardheader">
            <xsl:apply-templates mode="Control" select="."/>
          </h3>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates mode="Control" select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </td>
  </xsl:template>
  <xsl:template mode="Form" match="StackPanel">
    <div id="FormXML_Form">
      <xsl:if test="@Height or @Width">
        <xsl:attribute name="style">
          <xsl:if test="@Height"> height: <xsl:value-of select="@Height"/>;</xsl:if>
          <xsl:if test="@Width"> width: <xsl:value-of select="@Width"/>;</xsl:if>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@Style != ''">
        <xsl:attribute name="class">
          <xsl:value-of select="@Style"/>
        </xsl:attribute>
      </xsl:if>
      <!-- add in the form actions -->
      <xsl:choose>
        <xsl:when test="StackPanel.Resources/system:String[@x:Key='FormType'] = 'Workflow'">
          <xsl:call-template name="dvt_1.workflowformactions">
            <xsl:with-param name="id" select="1" />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="dvt_1.formactions">
            <xsl:with-param name="id" select="1" />
            <xsl:with-param name="header" select="1" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
      <!-- Header -->
      <xsl:apply-templates mode="Section" select="StackPanel[@x:Name='Header']"/>
      <!-- Sections -->
      <xsl:apply-templates mode="Section" select="StackPanel[@x:Name='MainSections']"/>
      <!-- Footer -->
      <xsl:apply-templates mode="Section" select="StackPanel[@x:Name='Footer']"/>
      <!-- separator -->
      <table cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="ms-formline">
            <img src="/_layouts/15/images/blank.gif?rev=23" width='1' height='1' alt="" />
          </td>
        </tr>
      </table>
      <!-- add in the form actions -->
      <xsl:choose>
        <xsl:when test="StackPanel.Resources/system:String[@x:Key='FormType'] = 'Workflow'">
          <xsl:call-template name="dvt_1.workflowformactions">
            <xsl:with-param name="id" select="2" />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="dvt_1.formactions">
            <xsl:with-param name="id" select="2" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
      <span style="display:none">
         <xsl:variable name="scriptbody">
            <xsl:text disable-output-escaping="yes">
                <![CDATA[
                var currentTab = '1';
                function SwitchTab(id)
                {
                    var eOldTabDiv = document.getElementById('Tab'+currentTab);
                    var eOldTabCell = document.getElementById('TabCell'+currentTab);
                    var eNewTabDiv = document.getElementById('Tab'+id);
                    var eNewTabCell = document.getElementById('TabCell'+id);
                    eOldTabDiv.style.display = 'none';
                    eNewTabDiv.style.display = 'block';
                    eOldTabCell.className = ']]></xsl:text>
                    <xsl:call-template name="ClassHelper">
                    <xsl:with-param name="style" select="StackPanel[@x:Name='MainSections']/TabControl/@Style"/>
                    </xsl:call-template>
                    <xsl:text disable-output-escaping="yes"><![CDATA[';
                        eNewTabCell.className = ']]></xsl:text>
                    <xsl:value-of select="StackPanel[@x:Name='MainSections']/TabControl/TabControl.Resources/system:String[@x:Key='StyleSelected']"/>
                    <xsl:text disable-output-escaping="yes"><![CDATA[';
                    currentTab = id;
                }
                function HoverOver(id)
                {
                    var eNewTabCell = document.getElementById('TabCell'+id);
                    eNewTabCell.className = ']]></xsl:text>
                    <xsl:value-of select="StackPanel[@x:Name='MainSections']/TabControl/TabControl.Resources/system:String[@x:Key='StyleHover']"/>
                    <xsl:text disable-output-escaping="yes"><![CDATA[';
                }
                function HoverOut(id)
                {
                    var eNewTabCell = document.getElementById('TabCell'+id);
                    if (currentTab == id)
                    {
                    eNewTabCell.className = ']]></xsl:text>
                    <xsl:value-of select="StackPanel[@x:Name='MainSections']/TabControl/TabControl.Resources/system:String[@x:Key='StyleSelected']"/>
                    <xsl:text disable-output-escaping="yes"><![CDATA[';		
		            }
		            else 
		            {
			                eNewTabCell.className = ']]></xsl:text>
                    <xsl:call-template name="ClassHelper">
                        <xsl:with-param name="style" select="StackPanel[@x:Name='MainSections']/TabControl/@Style"/>
                    </xsl:call-template>
                    <xsl:text disable-output-escaping="yes"><![CDATA[';
                    }
                } 
                ]]>
            </xsl:text>
        </xsl:variable>
        <xsl:value-of select="pcm:RegisterScriptBlock('formxmlscriptblock', string($scriptbody))"/>
      </span>
        </div>
  </xsl:template>
  <xsl:template mode="Control" match="*[@Association!='']">
    <xsl:variable name="ID" select ="generate-id()"></xsl:variable>
    <Label For="{@Association}">
      <xsl:call-template name="ResourceHandlerHelper">
        <xsl:with-param name="id"><xsl:value-of select="$ID"/>_Literal</xsl:with-param>
        <xsl:with-param name="Name" select="Properties/Property[@Name='Text']/@Value" />
      </xsl:call-template>
    </Label>    
  </xsl:template>
  <xsl:template mode="Control" match="Comment">
    <xsl:text disable-output-escaping="yes"><![CDATA[   
							<!-- FieldName="]]></xsl:text>
    <xsl:value-of select="@FieldName"/>
    <xsl:text disable-output-escaping="yes"><![CDATA[" 
								FieldInternalName="]]></xsl:text>
    <xsl:value-of select="@FieldInternalName"/>
    <xsl:text disable-output-escaping="yes"><![CDATA[" 
								FieldType="]]></xsl:text>
    <xsl:value-of select="@TypeAsString"/>
    <xsl:text disable-output-escaping="yes"><![CDATA[" 
							  --> ]]></xsl:text>
  </xsl:template>
  <xsl:template mode="Control" match="*">
    <xsl:variable name="FormMode">
      <xsl:choose>
        <xsl:when test="@ControlMode">
          <xsl:value-of select="@ControlMode"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select ="$XmlDefinition/*[1]/StackPanel.Resources/system:String[@x:Key='FormMode']"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="ID" select ="generate-id()"></xsl:variable>
    <xsl:copy>
      <xsl:attribute name="runat">server</xsl:attribute>
      <xsl:attribute name="ControlMode">
        <xsl:value-of select ="$FormMode"/>
      </xsl:attribute>
      <xsl:if test="@FieldName">
        <xsl:attribute name="id">ff_<xsl:value-of select="$ID"/></xsl:attribute>
        <xsl:attribute name="FieldName">
          <xsl:value-of select="@FieldName"/>
        </xsl:attribute>
        <xsl:if test="name()='SharePoint:FormField'">
          <xsl:attribute name="__designer:bind" >
            <xsl:choose>
              <!-- __designer:bind="{ddwrt:DataBind('u',concat('ff4',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string($ItemID)),'@Checkboxes117412921_x0020_Choice')}" -->
              <xsl:when test="$FormMode='New'     or $FormMode='new'"    >
                <xsl:value-of select="ddwrt:DataBind('i',concat('ff_',$ID),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string($ItemID)),@FieldName)" />
              </xsl:when>
              <xsl:when test="$FormMode='Edit'    or $FormMode='edit'"   >
                <xsl:value-of select="ddwrt:DataBind('u',concat('ff_',$ID),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string($ItemID)),@FieldName)" />
              </xsl:when>
              <xsl:when test="$FormMode='Display' or $FormMode='display'">
                <xsl:value-of select="ddwrt:DataBind('d',concat('ff_',$ID),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string($ItemID)),@FieldName)" />
              </xsl:when>
            </xsl:choose>
          </xsl:attribute>
        </xsl:if>
      </xsl:if>
    </xsl:copy>
    <!-- <SharePoint:FormField runat="server" id="ff4{$Pos}" ControlMode="Edit" FieldName="Checkboxes117412921_x0020_Choice" __designer:bind="{ddwrt:DataBind('u',concat('ff4',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Checkboxes117412921_x0020_Choice')}"/>-->
    <!-- <SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="Checkboxes117412921_x0020_Choice" ControlMode="Edit"/> -->
    <xsl:if test="@IncludeDescription">
      <SharePoint:FieldDescription runat="server" id="fd_{$ID}" ControlMode="{$FormMode}" FieldName="{@FieldName}"></SharePoint:FieldDescription>
    </xsl:if>
  </xsl:template>
  <xsl:template name="ResourceHandlerHelper">
    <xsl:param name="id" />
    <xsl:param name="Name" />
    <xsl:choose>
      <xsl:when test="starts-with($Name,'$Resources:')">
        <SharePoint:EncodedLiteral runat="server" id="{$id}" text="&lt;% {$Name} %&gt;"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$Name" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="dvt_1.formactions">
    <xsl:param name="id" select="0" />
    <xsl:param name="header" select="0" />
    <xsl:variable name="FormMode" select="StackPanel.Resources/system:String[@x:Key='FormMode']" />
    <table class="ms-formtoolbar" cellpadding="2" cellspacing="0" border="0" width="100%">
      <tr>
        <td width="99%" class="ms-toolbar" nowrap="nowrap">
          <img SRC="/_layouts/15/images/blank.gif?rev=23" width="1" height="18" alt=""/>
        </td>
        <td class="ms-toolbar" nowrap="nowrap" align="right">
          <SharePoint:SaveButton runat="server" ControlMode="{$FormMode}" id="savebutton{$id}" />
        </td>  
        <td class="ms-separator">
            <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text>
        </td>
        <td class="ms-toolbar" nowrap="nowrap" align="right">
            <SharePoint:GoBackButton runat="server" ControlMode="{$FormMode}" id="gobackbutton{$id}" />
        </td>
      </tr>
      <xsl:if test="$header = 1">
        <tr>
          <td class="ms-toolbar" nowrap="nowrap">
            <SharePoint:FormToolBar runat="server" ControlMode="{StackPanel.Resources/system:String[@x:Key='FormMode']}"/>
          </td>
        </tr>
      </xsl:if>
    </table>
  </xsl:template>
  <xsl:template name="dvt_1.workflowformactions">
    <xsl:param name="id" select="0" />
    <xsl:variable name="FormMode" select="StackPanel.Resources/system:String[@x:Key='FormMode']" />
    <table>
      <tr>
        <td width="99%" class="ms-toolbar" nowrap="nowrap">
          <img SRC="/_layouts/15/images/blank.gif?rev=23" width="1" height="18" alt="" />
        </td>
        <td class="ms-toolbar" nowrap="nowrap">
        <!-- Need to make the text = <SharePoint:EncodedLiteral runat="server" text="&lt;%$Resources:wss,htmledit_save%&gt;"/> or something similar, but be usable
	     as an attribute  (either Text on asp:Button, or Value on html input[type=button]-->
          <asp:button runat="server" onclientclick="javascript: {ddwrt:GenFireServerEvent(concat('__update;__workflowTaskCompleteThis={',$ItemID,'};__commit;__redirectsource'))}" Text="Complete Task" />
        </td>
        <td class="ms-separator">
          <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text>
        </td>
        <td class="ms-toolbar" nowrap="nowrap">
        <!-- Need to make the text = <SharePoint:EncodedLiteral runat="server" text="&lt;%$Resources:wss,htmledit_save%&gt;"/> or something similar, but be usable
	     as an attribute  (either Text on asp:Button, or Value on html input[type=button]-->
          <asp:button runat="server" onclientclick="javascript: {ddwrt:GenFireServerEvent('__update;__commit;__redirectsource')}" Text="Save Draft" />
        </td>
        <td class="ms-separator">
          <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text>
        </td>
        <td class="ms-toolbar" nowrap="nowrap" align="right">
          <SharePoint:GoBackButton runat="server" ControlMode="{$FormMode}" id="gobackbutton{$id}" />
        </td>
      </tr>
    </table>
  </xsl:template>	
</xsl:stylesheet>
