<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Get the parent's objectID used to build a link to each child record (this ID would be provided in XML by the business object) -->
<xsl:variable name="Parent" select="BusinessObject/@parent"/>
<!-- Get the parent businees object's name used to build a link to each child record (this name would be provided in XML by the business object) -->
<xsl:variable name="BusObjName" select="BusinessObject/@name"/>

<!-- Match the root node of the DOM document -->
<xsl:template match="/">
  <html>
    <!-- Match the document element node by name -->
    <xsl:apply-templates/>
  </html>
</xsl:template>

<xsl:template match="*">
  <head>	
	<title>
	  <xsl:value-of select="@Title"/>
	</title>
	  <xsl:variable name="submitValue" select="@action"/>
  </head>
  <body bgcolor="#FFFFFF">
    <h1><xsl:value-of select="@Title"/></h1>
	  <br/>
		<form>
		 <xsl:attribute name="action">
		    <xsl:value-of select="$submitValue"/>
		 </xsl:attribute>
		<table border="0" width="70%">
		<xsl:for-each select="*">
		  <xsl:variable name="PropertyName" select="name()"/>
			<!-- do not display System Generated properties -->
			<xsl:if test="@SystemGenerated='False'">
			   <!-- do not display hidden properties -->
			   <xsl:if test="@Access!='Hidden'">						 			
			     <tr>
			      <td valign="top" bgcolor="#CDEDFF" width="33%">
				  <xsl:value-of select="@Label"/>
					  <!-- if a property is mandatory put an indicator next to the label-->
					  <!-- include a hidden control  -->
					  <xsl:if test="@Mandatory='True'">					
						<font color="RED">
							<xsl:text> *</xsl:text>
						</font>
						<input type='hidden' value='True'>
							<xsl:attribute name="name">
								<xsl:value-of select="$PropertyName"/>
								<xsl:text>Required</xsl:text>
							</xsl:attribute>
						</input>
					  </xsl:if>
			        </td>
			        <td>
				  <xsl:choose>
					<xsl:when test="@ControlType='TextBox'">
						<input type="text">
							<xsl:attribute name="name">
								<xsl:value-of select="name()"/>
							</xsl:attribute>
							<xsl:attribute name="value">
								<xsl:value-of select="text()"/>
							</xsl:attribute>
							<xsl:if test="@Access='Read Only'">
							  <xsl:attribute name="ONFOCUS">this.blur()</xsl:attribute>
							</xsl:if>
						</input>
					</xsl:when>
					<xsl:when test="@ControlType='PromptForm'">
						<input type="text">
							<xsl:attribute name="name">
								<xsl:value-of select="name()"/>
							</xsl:attribute>
							<xsl:attribute name="value">
								<xsl:value-of select="text()"/>
							</xsl:attribute>
							<xsl:if test="@Access='Read Only'">
							  <xsl:attribute name="ONFOCUS">this.blur()</xsl:attribute>
							</xsl:if>
						</input>
					</xsl:when>
					<xsl:when test="@ControlType='DatePicker'">
						<input type="text">
							<xsl:attribute name="name">
								<xsl:value-of select="name()"/>
							</xsl:attribute>
							<xsl:attribute name="value">
								<xsl:value-of select="text()"/>
							</xsl:attribute>
						</input>
					</xsl:when>
					<xsl:when test="@ControlType='ComboBox'">
						<select>
							<xsl:attribute name="name">
								<xsl:value-of select="name()"/>
							</xsl:attribute>
						<xsl:call-template name="SelectionItems"/>
						</select>
					</xsl:when>
					<xsl:when test="@ControlType='ListBox'">
						<select>
							<xsl:attribute name="name">
								<xsl:value-of select="name()"/>
							</xsl:attribute>
							<xsl:attribute name="Multiple">
							</xsl:attribute>
						<xsl:call-template name="MultipleItems"/>
						</select>
					</xsl:when>
					<xsl:when test="@ControlType='OptionButton'">
						<xsl:call-template name="RadioItems"/>
					</xsl:when>
					<xsl:when test="@ControlType='CheckBox'">
						<xsl:call-template name="CheckBoxItems"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="text()"/>
					</xsl:otherwise>
				</xsl:choose>
			  </td>
			</tr>
		    </xsl:if>
     		  </xsl:if>
		</xsl:for-each>		
		</table>
		<br/>
		<br/>
		
		<!-- XSLT section for the tabs -->	
		<table border="0" width="100%">
		  <tr>
		
		  <!-- creates a leader space -->
		    <td></td>						
				
			<xsl:for-each select="PropColl">			
				<td colspan="3" height="100%" bgcolor="white"></td>			
			</xsl:for-each>
		
		  </tr>
		  <tr vAlign="top">
		    <td>  </td>		
		
		    <xsl:for-each select="PropColl">
			<xsl:variable name="TabNum" select="@sequence"/>
			<xsl:variable name="Name" select="@PropCollName"/>		
			
			<td width="1" bgcolor="white"><img src="white.gif" border="0" width="1" height="100%"/></td>
			<td style="cursor:hand;font-weight:normal;" bgcolor="darkgray">
				<xsl:attribute name="name">tabLabel<xsl:value-of select="$TabNum"/></xsl:attribute>
				<xsl:attribute name="id">tabLabel<xsl:value-of select="$TabNum"/></xsl:attribute>
				<xsl:attribute name="onclick">
						<xsl:text>showTab(</xsl:text>
						<xsl:value-of select="$TabNum"/>
						<xsl:text>);</xsl:text>	
				</xsl:attribute>
			<font face="verdana, arial" size="2"><xsl:value-of select="$Name"/></font>
			</td>
			<td width="1" bgcolor="black"><img src="black.gif" border="0" width="1" height="100%"/></td>
		</xsl:for-each>
		
		<td width="100%" height="1" bgcolor="white"></td>
		</tr>
		</table>		
		
		<table border="0" cellspacing="0" cellpadding="0" bgcolor="lightgrey" width="100%">
			<tr><td width="2" bgcolor="white"><img src="white.gif" border="0" width="2" height="100%"/></td>
			<td>		
		
			<xsl:for-each select="PropColl">
				<xsl:variable name="TabNum" select="@sequence"/>
				<xsl:variable name="Name" select="@PropCollName"/>						
					<div style="display=none;">		
						<xsl:attribute name="name">tabContent<xsl:value-of select="$TabNum"/></xsl:attribute>
						<xsl:attribute name="id">tabContent<xsl:value-of select="$TabNum"/></xsl:attribute>
					  <table>						
						<xsl:apply-templates select="ColumnHeaders">
							<xsl:with-param name="CollName" select="$Name"/>
						</xsl:apply-templates>
						<xsl:apply-templates select="Rows">
							<xsl:with-param name="CollName" select="$Name"/>							
						</xsl:apply-templates>						
					  </table>
					</div>					
			</xsl:for-each>		
		
			</td><td width="2" bgcolor="black"><img src="black.gif" border="0" width="2" height="100%"/></td>
			</tr>
			<tr>
			<td width="2" bgcolor="white"><img src="white.gif" border="0" width="2" height="100%"/></td>
			<td width="2" bgcolor="black"><img src="black.gif" border="0" width="2" height="100%"/></td>
			</tr>
			<tr><td colspan="3" height="2" bgcolor="black"></td></tr>
		</table>		
		
		</form>
	</body>
</xsl:template>

<xsl:template match="BusinessObject/PropColl">	
</xsl:template>

<!-- Primary template to descend down XML for the child property collection -->
<xsl:template name="Child" match="BusinessObject/PropColl">		
	
	<!-- Add column headers to table -->
	<xsl:apply-templates select="ColumnHeaders"/>
	<xsl:call-template name="ColumnHeaders"/>
	
	<!-- Add detail information to table -->
	<xsl:apply-templates select="Rows"/>
			
</xsl:template>
	
<!-- This template builds the column headers (labels) based on information submitted by the business object -->
<xsl:template match="ColumnHeaders" name="ColumnHeaders">
	<xsl:param name="CollName"/>
	<tr bgcolor="#333399">	
	  <xsl:for-each select="Column">
		<th width="200">
		  <font color="white">
		    <xsl:value-of select="@Name"/>
		  </font>
		</th>
	  </xsl:for-each>
	  	  	
		  <td></td>
	  	  <td>
		    <!-- put an 'Add' button -->
		    <INPUT type="button" id="cmdNew" name="cmdNew">
		  	  <xsl:attribute name="onclick">javascript:cmdNewChild_Click(<xsl:value-of select="$CollName"/>)</xsl:attribute>						
		  	  <xsl:attribute name="value">Add <xsl:value-of select="$CollName"/></xsl:attribute>
		    </INPUT>
		  </td>
		
	</tr>
</xsl:template>
	
<!-- This template gets each row/record from the business object -->
<xsl:template match="Rows" name="Rows">
	<xsl:param name="CollName"/>
	<xsl:for-each select="Row">
	  <tr bgcolor="#CDEDFF">	
	    <xsl:apply-templates select="Cols">
		  <xsl:with-param name="Name" select="$CollName"/>
		  <xsl:with-param name="ObjID" select="@ObjectID"/>
	    </xsl:apply-templates>			
	  </tr>	  	  
	</xsl:for-each>	
</xsl:template>
	
<!-- This template matches the columns and places them inside of table cells (td) -->
<xsl:template match="Cols">
	<xsl:param name="ObjID"/>
	<xsl:param name="Name"/>
	  <xsl:for-each select="Col">
		<td>
		  <xsl:choose>
			<xsl:when test="position()=1">
			   <!-- Build a link for this item based upon URL captured above, and record ID passed as a parameter -->		
			   <!--
			     <a>
				  <xsl:attribute name="href"><xsl:value-of select="$URL"/>&amp;ID=<xsl:value-of select="$ObjID"/></xsl:attribute>
				  <xsl:value-of select="@value"/>
			     </a>
			   -->
			   <a>
				 <xsl:attribute name="href"><xsl:value-of select="$BusObjName"/><xsl:value-of select="$Name"/>Detail.ASP?ParentID=<xsl:value-of select="$Parent"/>&amp;ID=<xsl:value-of select="$ObjID"/></xsl:attribute>
				 <xsl:value-of select="@value"/>
			   </a>			   
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@value"/>
			</xsl:otherwise>
		  </xsl:choose>			
		</td>
	</xsl:for-each>			
</xsl:template>

<xsl:template name="SelectionItems" mode="SelectionItems" match="node()">
	<xsl:variable name="PropertyValue" select="text()"/>				
	  <xsl:for-each select="node()/Value">
		<xsl:variable name="ItemValue" select="text()"/>		
		  <option>			
			  <xsl:if test="$ItemValue=$PropertyValue">
				<xsl:attribute name="SELECTED">												
				</xsl:attribute>
			  </xsl:if>
			<xsl:value-of select="$ItemValue"/>		
		  </option>
	  </xsl:for-each>	
</xsl:template>

<xsl:template name="MultipleItems" mode="MultipleItems" match="node()">			
	<xsl:variable name="defaultValue" select="text()"/>	
	  <xsl:for-each select="node()/Value">
		<xsl:variable name="ItemValue" select="text()"/>		
		  <option>			
			<xsl:if test="$ItemValue=$defaultValue">
				<xsl:attribute name="SELECTED">												
				</xsl:attribute>
			</xsl:if>
			<xsl:value-of select="$ItemValue"/>		
		  </option>
	  </xsl:for-each>
</xsl:template>
<xsl:template name="RadioItems" mode="RadioItems" match="node()">
	<xsl:variable name="PropertyValue" select="text()"/>
	<xsl:variable name="PropertyName" select="name()"/>	
	<xsl:for-each select="node()/Value">
		<xsl:variable name="ItemValue" select="text()"/>	
		<input type="radio">
			<xsl:if test="$ItemValue=$PropertyValue">
				<xsl:attribute name="checked">					
				</xsl:attribute>
			</xsl:if>
			<xsl:attribute name="name">
				<xsl:value-of select="$PropertyName"/>
			</xsl:attribute>
			<xsl:attribute name="value">
				<xsl:value-of select="$ItemValue"/>
			</xsl:attribute>
			<xsl:value-of select="text()"/>
		</input>
		<!-- put the next option on a new line -->
		<br/>
	</xsl:for-each>
</xsl:template>
<xsl:template name="CheckBoxItems" mode="CheckBoxItems" match="node()">
	<xsl:variable name="PropertyName" select="name()"/>
	<xsl:variable name="PropertyValue" select="text()"/>	
		<input type="Checkbox">			
			<xsl:attribute name="name">
				<xsl:value-of select="$PropertyName"/>
			</xsl:attribute>
			<xsl:if test="$PropertyValue='True'">
				<xsl:attribute name="checked">					
				</xsl:attribute>
			</xsl:if>			
		</input>			
</xsl:template>
</xsl:stylesheet>

