<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" omit-xml-declaration="yes"/>
	<!-- The next four lines allow you to take a data definition XSL apply it against a current-page index block -->
	<xsl:template match="system-index-block | system-folder | system-page"><xsl:apply-templates/></xsl:template>
	<xsl:template match="name | is-published | path | last-modified | last-modified-by | created-by | created-on"/>
	<xsl:template match="title | display-name | summary | description | author | start-date | end-date | teaser"/>
	<xsl:template match="last-modified | last-modified-by | created-by | created-on | last-published-on | last-published-by"/>
	
	<xsl:template match="system-data-structure">
		<form action="[system-asset]{action/path}[/system-asset]" method="post">
			<input name="_formName[]" type="hidden" value="{../name}"/>
			<xsl:for-each select="results/value"><input name="_result[]" type="hidden" value="{.}"/></xsl:for-each>
			
                <p><xsl:value-of select="opening"/></p>
			<table cellpadding="2" summary="FOR LAYOUT ONLY">
				<xsl:apply-templates select="form_item"/>
				<tr>
					<td>
					<xsl:apply-templates select="email-addresses"/>
					</td>
					<td>
					</td>
					<td>
						<br/>
						<input type="submit">
							<xsl:if test="string(submit_button_text)">
								<xsl:attribute name="value"><xsl:value-of select="submit_button_text"/></xsl:attribute>
							</xsl:if>
						</input>			
						   		
						<xsl:if test="reset/value = 'true'">
							<input type="reset" value="Reset Form"/>
						</xsl:if>
					</td>
				</tr>
			</table>
		</form>
	</xsl:template>
	
	<xsl:template match="email-addresses">		
<!--		<xsl:if test="email-address and email-address != ''">-->
		<xsl:for-each select="email-address">
			<xsl:variable name="email-address"><xsl:value-of select="."/></xsl:variable>
			<script type="text/javascript">				
				emailE=('<xsl:value-of select="substring-before($email-address,'@')"/>@' + '<xsl:value-of select="substring-after($email-address,'@')"/>')
				document.write('<input name="emails[]" type="hidden" value="' + emailE + '"/>')
			</script>
		 </xsl:for-each>
<!--		</xsl:if>-->
	</xsl:template>
	
	<xsl:template match="form_item">
		<xsl:variable name="name"><xsl:call-template name="fixName"/></xsl:variable>
		<xsl:choose>
			<xsl:when test="type='hidden'">
				<input name="{$name}[]" type="hidden" value="{value}"/>
			</xsl:when>
			<xsl:otherwise>		
				<tr>
					<td width="40%"><label for="{$name}"><xsl:value-of select="name"/>: <xsl:if test="required/value"><font color="red">*</font></xsl:if></label></td>
					<td width="10%">    </td>
					<td width="50%">
						<xsl:if test="required/value"><input name="{$name}_REQUIRED[]" type="hidden" value="T"/></xsl:if>
						<xsl:choose>
							<xsl:when test="type='dropdown'">
								<select name="{name}[]" size="1">
									<xsl:if test="string(default_value)">
										<option SELECTED="true" value="{default_value}"><xsl:value-of select="default_value"/></option>
										<xsl:value-of select="default_value"/>
									</xsl:if>
									<xsl:for-each select="value[string(text())]">
										<option value="{.}"><xsl:value-of select="."/></option>
									</xsl:for-each>
								</select>
							</xsl:when>
							<xsl:when test="type='checkbox' or type='radio'">
								<xsl:if test="string(default_value)">
									<input CHECKED="true" id="{$name}" maxlength="{maxlength}" name="{$name}[]" size="{size}" type="{type}"><xsl:value-of select="default_value"/></input>
								</xsl:if>
								<xsl:for-each select="value[string(text())]">
									<input id="{$name}" maxlength="{maxlength}" name="{$name}[]" size="{size}" type="{../type}" value="{.}">
										<xsl:value-of select="."/>
									</input>
								</xsl:for-each>
							</xsl:when>
							<xsl:when test="type='textarea'"> 
		<textarea cols="30" name="{$name}[]" rows="5">
		</textarea>
							</xsl:when>
							<xsl:otherwise>
								<input id="{$name}" maxlength="{maxlength}" name="{$name}[]" size="{size}" type="{type}" value="{default_value}"/>
							</xsl:otherwise>
						</xsl:choose>
					</td>
				</tr>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>	
	
	<xsl:template name="fixName">
		<xsl:variable name="output"><xsl:value-of select="translate(name,' ','_')"/></xsl:variable>
		<xsl:variable name="output2"><xsl:value-of select="translate($output,'/','_')"/></xsl:variable>
		<xsl:value-of select="translate($output2,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
	</xsl:template>
</xsl:stylesheet>