<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
		<xsl:apply-templates select="calling-page/system-page"/>
	</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-page">
		<xsl:text><!--#START-QUIZ--></xsl:text>
		<script language="javascript">
			<xsl:comment><![CDATA[
			
			//This function borrowed from online
			function TrimString(sInString) {
				sInString = sInString.replace( /^\s+/g, "" );// strip leading
				return sInString.replace( /\s+$/g, "" );// strip trailing
			}
			
            function verify(surveyform,action) { 
				var reqtext = "";
				var valtext = "";
				var choice = "dropdowncheckboxradio";         

                var inputs = surveyform.getElementsByTagName('input');
                
                for (var i = 0; i < inputs.length; i++) {
					if (inputs[i].className == 'required' && !reqtext.match(inputs[i].name)) {
						reqtext += inputs[i].name;
					}
				}
				
				for (var i = 0; i < inputs.length; i++) {
                    if (inputs[i].className == 'required' && !valtext.match(inputs[i].name) && 
						((choice.match(inputs[i].type) && inputs[i].checked == true) || 
                        (inputs[i].type == 'text' && inputs[i].value != ""))) {
							valtext += inputs[i].name;
                    }
                }
				
				//Now check for text areas
				inputs = surveyform.getElementsByTagName('textarea');
				
				for (var i = 0; i < inputs.length; i++) {
					if (inputs[i].className == 'required' && !reqtext.match(inputs[i].name)) {
						reqtext += inputs[i].name;
					}
				}
				
				for (var i = 0; i < inputs.length; i++) {
                    if (inputs[i].className == 'required' && !valtext.match(inputs[i].name) && TrimString(inputs[i].value) != "") {
							valtext += inputs[i].name;
                    }
                }
                
                if (valtext == reqtext) {
                    surveyform.action = action;
					surveyform.submit();
                }
				else {
					alert("Required fields missing");
				}
            }
            
            
       				
				]]></xsl:comment>
		</script>
		<xsl:text><!--#START-FORM--></xsl:text>
		<form method="POST">
			<input name="id" type="hidden" value="{@id}"/>
			<input name="title" type="hidden" value="{title}"/>
			<xsl:apply-templates select="system-data-structure"/>
		</form>
		<xsl:text><!--#END-FORM--></xsl:text>
		<xsl:text><!--#END-QUIZ--></xsl:text>
	</xsl:template>
	
	<xsl:template match="system-data-structure">	
		<xsl:variable name="inputnames">
			<xsl:for-each select="form_item">
				<xsl:call-template name="fixName"/>
				<xsl:text>,</xsl:text>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="inputvalues">
			<xsl:for-each select="form_item">
				<xsl:value-of select="type"/>
				<xsl:text>,</xsl:text>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="inputanswers">
			<xsl:for-each select="form_item">
				<xsl:value-of select="answer"/>
				<xsl:text>,</xsl:text>
			</xsl:for-each>
		</xsl:variable>
		
			<p>
				<xsl:value-of select="opening"/>
			</p>
			<font color="red" size="4">*</font><font color="red" size="2">Indicates a required field</font>
			<table cellpadding="2" summary="FOR LAYOUT ONLY">
				<tr>
					<td>
						<input type="hidden" name="thanks" value="[system-asset:page]{thanks/path}[/system-asset:page]"/>
					</td>
				</tr>
				<tr>
					<td>
						<input type="hidden" name="inputnames" value="{$inputnames}"/>
					</td>
				</tr>
				<tr>
					<td>
						<input type="hidden" name="inputvalues" value="{$inputvalues}"/>
					</td>
				</tr>
				<tr>
					<td>
						<input type="hidden" name="inputanswers" value="{$inputanswers}"/>
					</td>
				</tr>
				<tr>
					<td>
						<input type="hidden" name="id" value="{id}"/>
					</td>
				</tr>
				
				<xsl:apply-templates select="form_item"/>
				<tr>
					<td>
					</td>
					<td>
						<br/>
						<input type="button" onClick="verify(document.forms[0],{action/path})">
							<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>
		
	</xsl:template>
	<xsl:template match="form_item">
		<xsl:variable name="name">
			<xsl:call-template name="fixName"/>
		</xsl:variable>
		<xsl:variable name="req">
			<xsl:if test="required!=''">
				required
			</xsl:if>
			<xsl:if test="required=''">
				optional
			</xsl:if>
		</xsl:variable>
		
		<tr>
			<td colspan="2">
				<h4>
					<xsl:value-of select="name"/>:<xsl:if test="required!=''">
						<font color="red" size="4">*</font>
					</xsl:if>
				</h4>
			</td>
		</tr>
		<tr>
			<td width="10%">    </td>
			<td width="90%">
				<xsl:choose>
					<xsl:when test="type='dropdown'">
						<select name="{name}" size="1">
							<xsl:if test="string(_value)">
								<option SELECTED="true" value="{default_value}" id="{$req}">
									<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" class="{$req}" 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 class="{$req}" maxlength="{maxlength}" name="{$name}" size="{size}" type="{../type}" value="{.}">
								<xsl:value-of select="."/>
							</input>
							<br/>
						</xsl:for-each>
					</xsl:when>
					<xsl:when test="type='textarea'">
						<textarea class="{$req}" cols="30" name="{$name}" rows="5">
							<xsl:text></xsl:text>
						</textarea>
					</xsl:when>
					<xsl:otherwise>
						<input class="{$req}" maxlength="{maxlength}" name="{$name}" size="{size}" type="{type}" value="{default_value}"/>
					</xsl:otherwise>
				</xsl:choose>
			</td>
		</tr>
	</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:choose>
			<xsl:when test="string-length(substring-before($output2,'?')) != string-length($output2) and
							string-length(substring-before($output2,'?')) > 0">
				<xsl:value-of select="concat(substring-before($output2,'?'),'3Q3')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$output2"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>