<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output indent="yes" omit-xml-declaration="yes" method="html" encoding="UTF-8"/>
   <xsl:variable name="php-script-location">data-def-submit.php</xsl:variable>
   <xsl:variable name="javascript-error-message">Please enter all required fields marked with *</xsl:variable>
   <xsl:variable name="separator">_X__</xsl:variable>
   
   <!-- I don't think this is used anymore; shouldn't be -->
   <xsl:variable name="hyphen-replace">_HYPHENREPLACE__</xsl:variable>
   
   <!-- These two variables are appended to the input field's name to flag to the PHP script what kind of input it needs to upload to the CMS -->
   <xsl:variable name="selector-flag">__CONTENTXMLSELECTOR</xsl:variable>
   <xsl:variable name="checkbox-flag">__CONTENTXMLCHECKBOX</xsl:variable>

	<xsl:template name="getFieldName">
		<xsl:param name="fieldName"/>
		
		<xsl:for-each select="..">
			<xsl:choose>
				<xsl:when test="name(.)='group'">
					<xsl:call-template name="getFieldName">
						<xsl:with-param name="fieldName" select="concat(@identifier, $separator, $fieldName)"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$fieldName"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="getGroupLabel">
		<xsl:param name="group-label"/>
		
		<xsl:for-each select="..">
			<xsl:choose>
				<xsl:when test="name(.)='group'">
					<xsl:call-template name="getGroupLabel">
						<xsl:with-param name="group-label" select="concat(@label, ' &#187; ', $group-label)"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$group-label"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>

   <xsl:template match="/system-data-structure">
		<script type="text/javascript">
			<xsl:comment>
				<![CDATA[
				function validateOnSubmit()
				{
				  var num_errors = 0;
				  var requiredFields = document.cascade_server_cms_submit.data_definition__required_fields.value;
				  var requiredFieldsArray = requiredFields.split(",");
				  if(requiredFields == "")
				  {
				    // Do nothing
				  }
				  else if(requiredFields.indexOf(",") == -1)
				  {
				    var field = document.getElementById("id_" + requiredFields);
				    if(field.value == ""){ num_errors++; }
				  }
				  else
				  {
				    for(var i = 0; i < requiredFieldsArray.length; i++)
				    {
				      var field = document.getElementById("id_" + requiredFieldsArray[i]);
				      if(field.value == ""){ num_errors++; }
				    }
				  }
				  
				  if(num_errors == 0){ document.cascade_server_cms_submit.submit(); }
				  else{
				    alert(']]><xsl:value-of select="$javascript-error-message"/><![CDATA[');
				  }
				}
				]]>
			</xsl:comment>
		</script>

		<form name="cascade_server_cms_submit" action="{$php-script-location}" method="post">
			<input type="hidden" name="data_definition__required_fields">
				<xsl:attribute name="value">
					<xsl:for-each select="//text[@required='true']">
						<xsl:call-template name="getFieldName">
							<xsl:with-param name="fieldName" select="@identifier"/>
						</xsl:call-template>
						<xsl:if test="position()!=last()">
							<xsl:text>,</xsl:text>
						</xsl:if>
					</xsl:for-each>
				</xsl:attribute>
			</input>
			
			<fieldset class="data-definition">
				<legend>Form</legend>
				<xsl:call-template name="printFieldSet" />
			</fieldset>
			<p class="submit">
				<input type="button" name="formsubmit" accesskey="s" title="Access key 'S' to submit" value="Submit" onclick="validateOnSubmit()"/>
			</p>
		</form>
   </xsl:template>

   <xsl:template name="printFieldSet">
      <xsl:param name="name-prefix" select="''" />

      <xsl:for-each select="group | text">
         <xsl:choose>
            <xsl:when test="name(.)='group'">
               <fieldset>
                  <legend>
                  	 <xsl:call-template name="getGroupLabel">
                  	 	<xsl:with-param name="group-label" select="@label"/>
                  	 </xsl:call-template>
                  </legend>

                  <xsl:call-template name="printFieldSet">
                     <xsl:with-param name="name-prefix" select="concat($name-prefix, @identifier, $separator)" />
                  </xsl:call-template>
               </fieldset>
            </xsl:when>

            <xsl:when test="name(.)='text'">
               <xsl:choose>
               	  <!-- Textarea -->
                  <xsl:when test="@multi-line='true'">
                     <p class=" ">
                        <label for="id_{concat($name-prefix, @identifier)}" title="{@help-text}">
                        	<xsl:choose>
                        		<xsl:when test="@required='true'">
                        			<b><xsl:value-of select="@label"/>*</b>
                        		</xsl:when>
                        		<xsl:otherwise>
                           			<xsl:value-of select="@label" />
                           		</xsl:otherwise>
                           	</xsl:choose>
                        </label>

                        <a name="sdanchor{concat($name-prefix, @identifier)}"> </a>

                        <textarea name="{concat($name-prefix, @identifier)}" id="id_{concat($name-prefix, @identifier)}">
                           <xsl:if test="@cols">
                              <xsl:attribute name="cols">
                                 <xsl:value-of select="@cols" />
                              </xsl:attribute>
                           </xsl:if>

                           <xsl:if test="@rows">
                              <xsl:attribute name="rows">
                                 <xsl:value-of select="@rows" />
                              </xsl:attribute>
                           </xsl:if>

                           <xsl:value-of select="@default" />
                        </textarea>
                     </p>
                  </xsl:when>
				  <!-- An ordinary text input field -->
                  <xsl:when test="not(@type)">
                     <p class=" ">
                        <label for="id_{concat($name-prefix, @identifier)}" title="{@help-text}">
                        	<xsl:choose>
                        		<xsl:when test="@required='true'">
                        			<b><xsl:value-of select="@label"/>*</b>
                        		</xsl:when>
                        		<xsl:otherwise>
                           			<xsl:value-of select="@label" />
                           		</xsl:otherwise>
                           	</xsl:choose>
                        </label>

                        <a name="sdanchor{concat($name-prefix, @identifier)}">
                        </a>

                        <input type="text" name="{concat($name-prefix, @identifier)}" id="id_{concat($name-prefix, @identifier)}">
                           <xsl:if test="@maxlength">
                              <xsl:attribute name="maxlength">
                                 <xsl:value-of select="@maxlength" />
                              </xsl:attribute>
                           </xsl:if>

                           <xsl:if test="@size">
                              <xsl:attribute name="size">
                                 <xsl:value-of select="@size" />
                              </xsl:attribute>
                           </xsl:if>

                           <xsl:attribute name="value">
                              <xsl:if test="@default">
                                 <xsl:value-of select="@default" />
                              </xsl:if>
                           </xsl:attribute>
                        </input>
                     </p>
                  </xsl:when>

                  <xsl:when test="@type='dropdown'">
                     <p class=" ">
                        <label for="id_{concat($name-prefix, @identifier)}" title="{@help-text}">
                        	<xsl:choose>
                        		<xsl:when test="@required='true'">
                        			<b><xsl:value-of select="@label"/>*</b>
                        		</xsl:when>
                        		<xsl:otherwise>
                           			<xsl:value-of select="@label" />
                           		</xsl:otherwise>
                           	</xsl:choose>
                        </label>

                        <a name="sdanchor{concat($name-prefix, @identifier)}">
                        </a>

                        <select name="{concat($name-prefix, @identifier)}" id="id_{concat($name-prefix, @identifier)}">
                           <xsl:for-each select="dropdown-item">
                              <option value="{@value}">
                                 <xsl:if test="../@default=@value">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                 </xsl:if>

                                 <xsl:value-of select="@value" />
                              </option>
                           </xsl:for-each>
                        </select>
                     </p>
                  </xsl:when>

                  <xsl:when test="@type='multi-selector'">
                     <p class=" ">
                        <label for="id_{concat($name-prefix, @identifier, $selector-flag)}" title="{@help-text}">
                        	<xsl:choose>
                        		<xsl:when test="@required='true'">
                        			<b><xsl:value-of select="@label"/>*</b>
                        		</xsl:when>
                        		<xsl:otherwise>
                           			<xsl:value-of select="@label" />
                           		</xsl:otherwise>
                           	</xsl:choose>
                        </label>

                        <a name="sdanchor{concat($name-prefix, @identifier)}">
                        </a>

                        <select name="{concat($name-prefix, @identifier, $selector-flag)}[]" multiple="multiple" id="id_{concat($name-prefix, @identifier)}">
                           <xsl:if test="@size">
                              <xsl:attribute name="size">
                                 <xsl:value-of select="@size" />
                              </xsl:attribute>
                           </xsl:if>

                           <xsl:for-each select="selector-item">
                              <option value="{@value}">
                                 <xsl:if test="@selected='true'">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                 </xsl:if>

                                 <xsl:value-of select="@value" />
                              </option>
                           </xsl:for-each>
                        </select>
                     </p>
                  </xsl:when>

                  <xsl:when test="@type='radiobutton'">
                     <p class=" ">
                        <label for="id_{concat($name-prefix, @identifier)}" title="{@help-text}">
                        	<xsl:choose>
                        		<xsl:when test="@required='true'">
                        			<b><xsl:value-of select="@label"/>*</b>
                        		</xsl:when>
                        		<xsl:otherwise>
                           			<xsl:value-of select="@label" />
                           		</xsl:otherwise>
                           	</xsl:choose>
                        </label>

                        <a name="sdanchor{concat($name-prefix, @identifier)}">
                        </a>

                        <xsl:for-each select="radio-item">
                        <input type="radio" name="{concat($name-prefix, ../@identifier)}" value="{@value}">
                           <xsl:if test="../@default=@value">
                              <xsl:attribute name="checked">checked</xsl:attribute>
                           </xsl:if>
                        </input>

                        
                        <xsl:value-of select="@value" />
                        </xsl:for-each>
                     </p>
                  </xsl:when>

                  <xsl:when test="@type='checkbox'">
                     <p class=" ">
                        <label for="id_{concat($name-prefix, @identifier, $checkbox-flag)}" title="{@help-text}">
                        	<xsl:choose>
                        		<xsl:when test="@required='true'">
                        			<b><xsl:value-of select="@label"/>*</b>
                        		</xsl:when>
                        		<xsl:otherwise>
                           			<xsl:value-of select="@label" />
                           		</xsl:otherwise>
                           	</xsl:choose>
                        </label>
                        <a name="sdanchor{concat($name-prefix, @identifier)}"> </a>

                        <xsl:for-each select="checkbox-item">
	                        <input type="checkbox" name="{concat($name-prefix, ../@identifier, $checkbox-flag)}[]" value="{@value}">
	                           <xsl:if test="@checked='true'">
	                              <xsl:attribute name="checked">checked</xsl:attribute>
	                           </xsl:if>
	                        </input>
	                        <xsl:value-of select="@value" />
                        </xsl:for-each>
                     </p>
                  </xsl:when>
               </xsl:choose>
            </xsl:when>
         </xsl:choose>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

