<?xml version='1.0' encoding='UTF-8'?>
<!-- NOTE: Don't use &lt;=, it breaks OSX for some reason -->
<!-- noinspection ALL -->
<xsl:stylesheet version="1.0" exclude-result-prefixes="tw" xmlns:tw="http://lombardisoftware.com/schema/coachdesigner" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html"/>
   <xsl:param name="previewMode"/>

  <xsl:template match="tw:Form">
    <!-- generate server side scripts to retrieve data from database : test change -->
    <xsl:call-template name="sql"/>
    <xsl:element name="HTML">
      <xsl:call-template name="head"/>
      <xsl:call-template name="body"/>
    </xsl:element>
  </xsl:template>

  <xsl:template name="head">
    <xsl:element name="HEAD">
      <xsl:call-template name="metatagIE"/>
      <xsl:call-template name="formTitle"/>
	   <!-- Initialize portal context root global variable -->
      <xsl:call-template name="initPortalContextRootGlobalVar"/>
      <xsl:call-template name="styleSheet"/>
      <!-- IE having problems with the font size of the year on DateTextBox, override here to fix it -->
      <xsl:call-template name="cssInline">
	    <xsl:with-param name="css">
		 <xsl:text>.dijitCalendarYearLabel {font-size:12px;} </xsl:text>
	    </xsl:with-param>
	  </xsl:call-template>

      <xsl:if test="tw:CssInline and tw:CssInline != ''">
          <xsl:call-template name="cssInline">
              <xsl:with-param name="css" select="tw:CssInline"/>
          </xsl:call-template>
      </xsl:if>

	  

	  <xsl:call-template name="dojoScript"/>
      <!-- Coach designer -->
      <xsl:call-template name="coachDesignerJavascript"/>
      <!-- general purposes javascript -->
      <xsl:call-template name="generalJavascript"/>
      <!-- Standard validation functions -->
      <xsl:call-template name="standardValidation"/>
      <!-- Document attachment functions -->
      <xsl:if test="//tw:DocumentAttachment">
        <xsl:call-template name="documentAttachment"/>
      </xsl:if>
      <!-- Document viewer functions -->
      <xsl:if test="//tw:DocumentViewer">
        <xsl:call-template name="documentViewer"/>
      </xsl:if>
      <!-- Formatting support -->
      <xsl:call-template name="formattingSupport"/>
      <!-- Calendar functions -->

      <!-- DateTime Custom widget -->
      <xsl:call-template name="dateTimePicker"/>
      <!-- XmlStore Custom widget -->
      <xsl:call-template name="XmlStringStore"/>
      <!-- Initialize some dojo controls -->
      <xsl:call-template name="dojoInitScript"/>
      <!--  Hint Popup box for reports-->
      <xsl:call-template name="popupReportScript"/>
      <!-- Any client side java script -->
      <xsl:apply-templates select="tw:ClientScript" mode="head"/>
      <!-- functions to implement dependent visibility -->
      <xsl:call-template name="dependentVisibilityScript"/>
      <!-- functions to validation -->
      <xsl:call-template name="fieldValidationScript"/>

      <xsl:call-template name="addSnapshotVariable"/>

	  <xsl:if test="tw:JsInclude and string-length(tw:JsInclude) &gt; 0">
       <xsl:call-template name="jsInclude">
        <xsl:with-param name="jsInclude" select="tw:JsInclude"/>
       </xsl:call-template>
      </xsl:if>

    </xsl:element>
  </xsl:template>

  <xsl:template name="addSnapshotVariable">
      <xsl:element name="SCRIPT">
          <xsl:attribute name="type">
            <xsl:text>text/javascript</xsl:text>
          </xsl:attribute>
          <xsl:text disable-output-escaping="yes">var coachSnapshotContext = &apos;&lt;#= tw.system.model.processAppSnapshot.id#&gt;&apos;;</xsl:text>
      </xsl:element>
  </xsl:template>

  <xsl:template name="metatagIE">
      <xsl:element name="META">
          <xsl:attribute name="http-equiv">
            <xsl:text>X-UA-Compatible</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="content">
            <xsl:text>IE=5</xsl:text>
          </xsl:attribute>
      </xsl:element>
  </xsl:template>

  <xsl:template name="formTitle">
    <xsl:element name="TITLE">
      <xsl:choose>
        <xsl:when test="tw:FormTitle">
          <xsl:value-of disable-output-escaping="yes" select="tw:FormTitle"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="defaultTitle"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:template>

  <xsl:template name="styleSheet">
      <!-- Dijit soria theme -->
      <xsl:element name="LINK">
          <xsl:attribute name="REL">
              <xsl:text>stylesheet</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="HREF">
			<xsl:choose>
				<xsl:when test="$previewMode != &apos;true&apos;"><xsl:text>&lt;# try { if (com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null) != null) { buff.append(com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null)); } else {try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err2) { } } } catch(err1) { try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err) { } }#&gt;</xsl:text></xsl:when>
				<xsl:otherwise><xsl:text>&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix());  } catch(err) { } #&gt;</xsl:text></xsl:otherwise>
			</xsl:choose>
			<xsl:text>/js/dojo/1.6.1/dijit/themes/soria/soria.css</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="TYPE">
              <xsl:text>text/css</xsl:text>
          </xsl:attribute>
      </xsl:element>

      <xsl:call-template name="includeStyleSheet">
          <xsl:with-param name="styleSheet">html/common/coach_designer.css</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="includeStyleSheet">
          <xsl:with-param name="styleSheet">html/common/coach_toolbar.css</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="includeStyleSheet">
          <xsl:with-param name="styleSheet">html/common/coach_popup.css</xsl:with-param>
      </xsl:call-template>
      <xsl:choose>
          <xsl:when test="tw:CssOverrideReference and string-length(tw:CssOverrideReference) &gt; 0">
            <xsl:call-template name="includeStyleSheetFromAsset">
                <xsl:with-param name="reference" select="tw:CssOverrideReference"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
                <xsl:when test="$previewMode != &apos;true&apos;">
                  <xsl:text disable-output-escaping="yes">&lt;#  try { buff.append(Packages.com.lombardisoftware.server.core.UtilityCore.getProcessAppCssHtml()); } catch (err) { } #&gt;</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text disable-output-escaping="yes">&lt;#  try { buff.append(PreviewUtils.getProcessAppCssHtml()); } catch (err) { }  #&gt;</xsl:text>
                </xsl:otherwise>
             </xsl:choose>
          </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template name="includeStyleSheet">
      <xsl:param name="styleSheet"></xsl:param>
      <xsl:element name="LINK">
          <xsl:attribute name="REL">
              <xsl:text>stylesheet</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="HREF">
              <xsl:text disable-output-escaping="yes">&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix());  } catch(err) { } #&gt;</xsl:text><xsl:value-of select="$styleSheet"/>
          </xsl:attribute>
          <xsl:attribute name="TYPE">
              <xsl:text>text/css</xsl:text>
          </xsl:attribute>
      </xsl:element>
  </xsl:template>

  <xsl:template name="includeStyleSheetFromAsset">
      <xsl:param name="reference"/>
      <xsl:element name="LINK">
          <xsl:attribute name="REL">
              <xsl:text>stylesheet</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="HREF">
	          <xsl:call-template name="inlineAssetUrl">
    	          <xsl:with-param name="reference" select="$reference"/>
        	  </xsl:call-template>
          </xsl:attribute>
          <xsl:attribute name="TYPE">
              <xsl:text>text/css</xsl:text>
          </xsl:attribute>
      </xsl:element>
  </xsl:template>

  <xsl:template name="jsInclude">
      <xsl:param name="jsInclude"></xsl:param>
      <xsl:element name="SCRIPT">
          <xsl:attribute name="TYPE">
              <xsl:text>text/javascript</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="SRC">
              <xsl:text disable-output-escaping="yes">&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix());  } catch(err) { } #&gt;</xsl:text><xsl:value-of select="$jsInclude"/>
          </xsl:attribute>
      </xsl:element>
  </xsl:template>

    <xsl:template name="popupReportScript">
        <xsl:if test="//tw:Report">
            <xsl:call-template name="includeJavaScript">
                <xsl:with-param name="script">chart-tooltip.js</xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="standardValidation">
      <xsl:call-template name="includeJavaScript">
          <xsl:with-param name="script">validation.js</xsl:with-param>
      </xsl:call-template>
  </xsl:template>

    <xsl:template name="formattingSupport">
        <xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">formatting/number-functions.js</xsl:with-param>
        </xsl:call-template>
        <xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">formatting/lombardi-format.js</xsl:with-param>
        </xsl:call-template>
    </xsl:template>

  <xsl:template name="documentAttachment">
  		<xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">documentAttachment-util.js</xsl:with-param>
    	</xsl:call-template>
    	<xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">documentAttachment-fieldset.js</xsl:with-param>
    	</xsl:call-template>
    	<xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">documentAttachment-table.js</xsl:with-param>
    	</xsl:call-template>
    	<xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">documentAttachment-customTable.js</xsl:with-param>
    	</xsl:call-template>
    	<xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">documentAttachment-form.js</xsl:with-param>
    	</xsl:call-template>
        <xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">documentAttachment.js</xsl:with-param>
    	</xsl:call-template>
  </xsl:template>

  <xsl:template name="documentViewer">
  		<xsl:call-template name="includeJavaScript">
            <xsl:with-param name="script">documentViewer.js</xsl:with-param>
    	</xsl:call-template>
  </xsl:template>

  <xsl:template name="coachDesignerJavascript">
      <xsl:call-template name="includeJavaScript">
          <xsl:with-param name="script">coach_designer.js</xsl:with-param>
      </xsl:call-template>
  </xsl:template>

    <xsl:template name="generalJavascript">
      <xsl:call-template name="includeJavaScript">
          <xsl:with-param name="script">javascript.js</xsl:with-param>
      </xsl:call-template>
  </xsl:template>

  <xsl:template name="dateTimePicker">
      <xsl:call-template name="includeJavaScript">
          <xsl:with-param name="script">DateTimePicker.js</xsl:with-param>
      </xsl:call-template>
  </xsl:template>

  <xsl:template name="XmlStringStore">
      <xsl:call-template name="includeJavaScript">
          <xsl:with-param name="script">XmlStringStore.js</xsl:with-param>
      </xsl:call-template>
  </xsl:template>
  
  <xsl:template name="dojoScript">
      <!-- Load dojo toolkit -->
      <xsl:element name="SCRIPT">
       <xsl:attribute name="type">
         <xsl:text>text/javascript</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="src">
			<xsl:choose>
				<xsl:when test="$previewMode != &apos;true&apos;"><xsl:text>&lt;# try { if (com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null) != null) { buff.append(com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null)); } else { try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err2) { } } } catch(err1) { try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err) { } }#&gt;</xsl:text></xsl:when>
				<xsl:otherwise><xsl:text>&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix());  } catch(err) { } #&gt;</xsl:text></xsl:otherwise>
			</xsl:choose>
			<xsl:text>/js/dojo/1.6.1/dojo/dojo.js</xsl:text>
       </xsl:attribute>
       <xsl:variable name="lang" select="descendant::tw:DateSelector[@tw:lang='he' or @tw:lang='ar']/@tw:lang"/>  <!-- BDL shensis -->
       <xsl:attribute name="djConfig">
         <xsl:text>parseOnLoad:true, locale:'</xsl:text>
         <xsl:text disable-output-escaping="yes">&lt;#
           try {
             var locale = tw.system.user_locale == 'no' ? 'nb' : tw.system.user_locale;
             locale = locale.toLowerCase().replace('_', '-');
             buff.append(locale);             
           } catch(err){}; #&gt;</xsl:text>
         <xsl:text>'</xsl:text>
         <xsl:text disable-output-escaping="yes">&lt;#
           try {
             var locale = tw.system.user_locale;
         </xsl:text>  <!-- BDL shensis Start -->           
         <xsl:choose>          
             <xsl:when test="$lang">
               <xsl:text disable-output-escaping="yes">            
               if (locale != 'en')
                buff.append(', extraLocale:[\'</xsl:text><xsl:value-of select="$lang"/><xsl:text>\' , \'en\']');           
               else
                buff.append(', extraLocale:[\'</xsl:text><xsl:value-of select="$lang"/><xsl:text>\']');       
               </xsl:text>               
             </xsl:when>        
             <xsl:otherwise>
               <xsl:text disable-output-escaping="yes">
               if (locale != 'en')               
                buff.append(', extraLocale:[\'en\']');
               </xsl:text>                              
             </xsl:otherwise>
         </xsl:choose>
         <xsl:text disable-output-escaping="yes">  <!-- BDL shensis End -->                         
           } catch(err){}; #&gt;</xsl:text>
       </xsl:attribute>
      </xsl:element>
    </xsl:template>

    <xsl:template name="dojoInitScript">
	<xsl:element name="SCRIPT">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
          <xsl:text disable-output-escaping="yes">
		  <![CDATA[
	function findCssTextByClassName(cssClassName) {
	            if(cssClassName == undefined || cssClassName == "" || cssClassName== " ") {
					return "";
				}
				for(var i=0; i<document.styleSheets.length; i++) {
					var rules = undefined;
					  try{
						rules = document.styleSheets[i].cssRules;
					  } catch(e) {
						continue;
					  }
				  if(document.styleSheets[i].cssRules != undefined) {
					for(var j=0; j<document.styleSheets[i].cssRules.length; j++) {
					  if(document.styleSheets[i].cssRules[j].cssText.toString().indexOf("." + cssClassName + " ") > -1) {
						var cssText = document.styleSheets[i].cssRules[j].cssText;
						return cssText.substring(cssText.indexOf("{") + 1, cssText.indexOf("}"));
					  }
					}
				  }
				}
				
				for(var i=0; i<document.styleSheets.length; i++) {
				  var rules = undefined;
				  try{
					rules = document.styleSheets[i].rules;
				  } catch(e) {
					continue;
				  }
				  if(rules != undefined) {
					for(var j=0; j<rules.length; j++) {
					  if((!!(document.styleSheets[i].rules[j].selectorText))&&document.styleSheets[i].rules[j].selectorText.toString().indexOf("." + cssClassName + " ") > -1) {
						var cssText = document.styleSheets[i].rules[j].style.cssText;
						return cssText.toString();
					  }
					}
				  }
				}
			}]]>
		</xsl:text>
	</xsl:element>
      <xsl:element name="SCRIPT">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
          <xsl:text disable-output-escaping="yes">
			// Set BIDI is needed
            if(typeof String.prototype.trim !== 'function') {
              String.prototype.trim = function() {
                return this.replace(/^\s+|\s+$/g, '');
              }
            }

            dojo.require("dojox.data.XmlStore");
            dojo.require("dijit.form.ComboBox");
            dojo.require("DateTimePicker");
            dojo.require("dojo.number");
            dojo.require("dojo.date");
            dojo.require("lombardi.data.XmlStringStore");
			
			dojo.extend(dijit.form.ComboBox, {
				formatt:""
			});
			dijit.form.ComboBox.prototype.attributeMap.formatt = "";
			dojo.extend(dijit.form.ComboBox, {
				locale:"en"
			});
			dijit.form.ComboBox.prototype.attributeMap.locale = "";
			dojo.extend(dijit.form.ComboBox, {
				isnumeric:""
			});
			dijit.form.ComboBox.prototype.attributeMap.isnumeric = "";
			dojo.extend(dijit.form.ComboBox, {
				basetype:""
			});
			dijit.form.ComboBox.prototype.attributeMap.basetype = "";
			dojo.extend(dijit.form.ComboBox, {
				zvalmsg:""
			});
			dijit.form.ComboBox.prototype.attributeMap.zvalmsg = "";
			dojo.extend(dijit.form.ComboBox, {
				zvalidate:""
			});
			dijit.form.ComboBox.prototype.attributeMap.zvalidate = "";

            dojo.addOnLoad(function(){
              // Execute on load.

              // Set the default toolip position to appear above or below the input
              dijit.Tooltip.defaultPosition = [&quot;above&quot;,&quot;below&quot;];
              
              // Handle handshaking with social portal and task widget
              if ((!!(window.parent)) &amp;&amp; (window != window.parent)) {
                  var applicationId = '&lt;#= String(tw.system.applicationInstanceId) #&gt;';
                  var taskId = '&lt;#= String(tw.system.task_or_process_id) #&gt;';
            	  var event = {
            			  "name": "onInitialized",
            			  "taskID" : taskId,
            			  "applicationInstanceId" : applicationId,
            			  "parameters": ["oldCoach"]
            	  };
        	  		// work item 131966
				  var serializedEvent = dojo.toJson(event);
				  try {
					window.parent.postMessage(serializedEvent, "*");
				  } catch (e) {
					// postMessage() failed, use direct call onMessage instead with faked event. Container MUST be co-located
					console.log("iframe postMessage() failed, call onMessage() directly - event=", serializedEvent);
					if ((!!(window.parent.onMessage)) &amp;&amp; (typeof window.parent.onMessage === 'function')) {
						window.parent.onMessage({origin: window.location.protocol + "//" + window.location.host,
												data: serializedEvent, 
												source: window});
					} else {
						console.log("onMessage() does not exist");				
					}							
				  }				  
            	} 

            });
          </xsl:text>
      </xsl:element>

  </xsl:template>
  
	<xsl:template name="initPortalContextRootGlobalVar">
		<xsl:element name="SCRIPT">
			<xsl:attribute name="type">
				<xsl:text>text/javascript</xsl:text>
			</xsl:attribute>
			<xsl:text disable-output-escaping="yes">window._BPM_portalContextRoot="</xsl:text>
			<xsl:choose>
				<xsl:when test="$previewMode != &apos;true&apos;"><xsl:text disable-output-escaping="yes">&lt;# try { if (com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null) != null) { buff.append(com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null)); } else { try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err2) { } }  } catch(err1) { try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err) { } }#&gt;"</xsl:text></xsl:when>
				<xsl:otherwise><xsl:text disable-output-escaping="yes">&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix());  } catch(err) { } #&gt;"</xsl:text></xsl:otherwise>
			</xsl:choose>		  
		</xsl:element>
	</xsl:template>
  
  <xsl:template name="dependentVisibilityScript">
    <xsl:text>
</xsl:text>
    <xsl:element name="SCRIPT">
      <xsl:attribute name="type">
        <xsl:text>text/javascript</xsl:text>
      </xsl:attribute>
      <xsl:apply-templates select="//tw:Control" mode="head"/>
    <xsl:text>
 </xsl:text>
    </xsl:element>
   </xsl:template>

  <xsl:template name="fieldValidationScript">
    <xsl:text>
</xsl:text>
    <xsl:element name="SCRIPT">
      <xsl:attribute name="type">
        <xsl:text>text/javascript</xsl:text>
      </xsl:attribute>
      <xsl:apply-templates select="//tw:InputText[@tw:binding]|//tw:UserRoleInputText[@tw:binding]|//tw:DateSelector[@tw:binding]" mode="head"/>
    <xsl:text>
 </xsl:text>
    </xsl:element>
   </xsl:template>

    <xsl:template name="includeJavaScript">
      <xsl:param name="script"></xsl:param>
    <xsl:text>
</xsl:text>
    <xsl:element name="SCRIPT">
      <xsl:attribute name="type">
        <xsl:text>text/javascript</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="src">
        <xsl:text disable-output-escaping="yes">&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix());  } catch(err) { } #&gt;script/</xsl:text><xsl:value-of select="$script"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <xsl:template name="sql">
    <xsl:text disable-output-escaping="yes">
&lt;#
function eval_sqlstr(str) {
  var rs, li = 0;
  str.replace(
     /&amp;lt;#=(.+?)#__#/mg,
      function (m, g, i, w) {
        if (rs) rs += "+";
        else    rs = "";
        rs += "\"" + w.substring(li, i) + "\"+" + g;
        li = i + g.length + 10; // 10 is the length of "&lt;#=#__#"
      });
  if (!rs) rs = "";
  else if (li &lt; str.length) rs += "+";
  if (li &lt; str.length)
      rs += "\"" + str.substring(li) + "\"";
  java.lang.System.out.println("SQLStr Before eval= " + rs);
  return eval(rs);
}</xsl:text>
      <xsl:apply-templates select="//tw:Table[@tw:binding and @tw:binding != '']/tw:Sql[@tw:enabled='true']"/>
    <xsl:text disable-output-escaping="yes">
#&gt;</xsl:text>
  </xsl:template>

  <xsl:template match="tw:Sql">
    <xsl:text>
    </xsl:text>
    <xsl:if test="not(@tw:executeOnEveryDisplay) or @tw:executeOnEveryDisplay='false'">
        <xsl:text>if (</xsl:text>
        <xsl:call-template name="getBindingVariableIgnoringLastSquareBrackets"/>
        <xsl:text> == undefined) {
      </xsl:text>
    </xsl:if>
    <xsl:variable name="bindingVarIgnoringBrakets">
       <xsl:call-template name="getBindingVariableIgnoringLastSquareBrackets"/>
    </xsl:variable>
    <!-- escape "#>" to #__# to prevent wrong js parsing -->
    <xsl:variable name="encodedSQL">
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text">
          <xsl:call-template name="convertNewlineToSpace">
            <xsl:with-param name="s" select="."/>
          </xsl:call-template>
        </xsl:with-param>
        <xsl:with-param name="replace" select="'#>'" />
        <xsl:with-param name="by" select="'#__#'" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:text>var sqlstr=eval_sqlstr(&quot;</xsl:text><xsl:value-of select="$encodedSQL"/><xsl:text>&quot;);
</xsl:text>    
    <xsl:value-of select="$bindingVarIgnoringBrakets"/>
    <xsl:text> = tw.system.createFromPointySql(tw.system.variableTypeForVariable(&quot;</xsl:text>
    <xsl:call-template name="getBindingVariable"/>
    <xsl:text>&quot;), </xsl:text><xsl:value-of select="@tw:jndiName"/><xsl:text>, sqlstr</xsl:text>
    <xsl:text>, </xsl:text><xsl:value-of select="$bindingVarIgnoringBrakets"/><xsl:text>);</xsl:text>
    <xsl:if test="not(@tw:executeOnEveryDisplay) or @tw:executeOnEveryDisplay='false'">
        <xsl:text>
    }</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="tw:ClientScript" mode="head">
    <xsl:text>
</xsl:text>
    <xsl:element name="SCRIPT">
      <xsl:attribute name="type">
        <xsl:text>text/javascript</xsl:text>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:Control" mode="head">
    <xsl:variable name="controlId" select="@tw:id"/>
    <xsl:if test="//tw:VisibilityCondition[@tw:controlId = $controlId]">

    <!-- Get field value function: fcnFV<control-id>() -->
    <xsl:text>
  // get the value of a field depending on its type
  function fcnFV</xsl:text><xsl:value-of select="$controlId"/><xsl:text>(field) {
    </xsl:text>
    <xsl:choose>
      <xsl:when test="tw:InputText">return field.value;</xsl:when>
      <xsl:when test="tw:UserRoleInputText">return field.value;</xsl:when>
      <xsl:when test="tw:BooleanChoice">
        <xsl:choose>
          <xsl:when test="tw:BooleanChoice/tw:VisualCheckboxes">if (field.checked == true) { return &apos;true&apos;; } else { return &apos;false&apos;; }</xsl:when>
          <xsl:when test="tw:BooleanChoice/tw:VisualDropDown">return field.options[field.selectedIndex].value;</xsl:when>
          <xsl:when test="tw:BooleanChoice/tw:VisualRadioButtons">return field.value;</xsl:when>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="tw:SingleSelect">
        <xsl:choose>
          <xsl:when test="tw:SingleSelect/tw:VisualRadioButtons">return field.value;</xsl:when>
          <xsl:when test="tw:SingleSelect/tw:VisualDropDown">return field.options[field.selectedIndex].value;</xsl:when>
          <xsl:when test="tw:SingleSelect/tw:VisualList">return field.options[field.selectedIndex].value;</xsl:when>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="tw:MultipleSelect">
  var s = new Array();
  var index = 0;
  if (field.options != undefined) {
  for( i = 0; i &lt; field.options.length; i++ ){
	if (field.options[i].selected) {
		s[index++] = field.options[i].value;
	}
  }
  return s;
  }
  
  if (field.twAllOptions != undefined) {
  var toOptions = field.getElementsByTagName('SELECT').item(1);
  for( i = 0; i &lt; toOptions.length; i++ ){
	if (toOptions[i].selected) {
		s[index++] = toOptions[i].value;
	}
  }
  return s;
  }
  
  var curChild = field.parentNode.firstChild;
  while (curChild != null) {
	if (curChild.type == 'checkbox' &amp;&amp; curChild.checked) {
		s[index++] = curChild.value;
	}
	curChild = curChild.nextSibling;
  }
  return s;
      </xsl:when>
      <xsl:otherwise>return undefined;</xsl:otherwise>
    </xsl:choose>
    <xsl:text>
  }
    </xsl:text>

    <!-- Dependent visibility function: fcnDV<control-id>() -->
      <xsl:text>
</xsl:text>
        <xsl:text>function fcnDV</xsl:text>
        <xsl:value-of select="$controlId"/>
        <xsl:text>(field, validationPassed) {
  var value = undefined; // initial value
  var testValue = undefined;
  if (validationPassed) {
  </xsl:text>
   <xsl:choose>
    <xsl:when test="tw:BooleanChoice/tw:VisualRadioButtons">
      // append value to the hidden field
   <xsl:variable name="hiddenId" select="concat($controlId, '_hidden')"/>
   var var<xsl:value-of select="$controlId"/> = document.getElementById('<xsl:value-of select="$hiddenId"/>');
   var<xsl:value-of select="$controlId"/>.value = fcnFV<xsl:value-of select="$controlId"/>(field);
    </xsl:when>
  <xsl:when test="tw:BooleanChoice/tw:VisualCheckboxes">
      // append value to the hidden field
   <xsl:variable name="hiddenId" select="concat($controlId, '_hidden')"/>
   var var<xsl:value-of select="$controlId"/> = document.getElementById('<xsl:value-of select="$hiddenId"/>');
   var<xsl:value-of select="$controlId"/>.value = (fcnFV<xsl:value-of select="$controlId"/>(field) == 'true');
    </xsl:when>
 </xsl:choose>
  }
    <xsl:for-each select="//tw:VisibilityCondition[@tw:controlId = $controlId]">
      <xsl:text>var result</xsl:text><xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template><xsl:text> = document.getElementById(&apos;</xsl:text>
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
      <xsl:text>&apos;).defaultVisibility;
    </xsl:text>

    </xsl:for-each>
        <xsl:for-each select="//tw:VisibilityCondition[@tw:controlId = $controlId]">
          <xsl:text>
  if (validationPassed) {
    value = fcnFV</xsl:text><xsl:value-of select="@tw:controlId"/><xsl:text>(field);
    testValue = </xsl:text><xsl:call-template name="visibilityConditionValue"/><xsl:text>;
    if (visibilityConditionIsTrue(&apos;</xsl:text><xsl:value-of select="tw:Condition/@tw:operator"/><xsl:text>&apos;, value, testValue)) {
      </xsl:text><xsl:call-template name="visibilityConditionTrue"/><xsl:text>
    } else {
      <!-- we should not reset it to default visibility -->
      <!-- /xsl:text><xsl:call-template name="visibilityConditionFalse"/><xsl:text -->
    }
  } else {
      result</xsl:text><xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template><xsl:text> = document.getElementById(&apos;</xsl:text>
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
      <xsl:text>&apos;).defaultVisibility;
  }
</xsl:text>
      <xsl:text>

  setVisibility(document.getElementById(&apos;</xsl:text>
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
      <xsl:text>&apos;), result</xsl:text><xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template><xsl:text>);
    </xsl:text>
        </xsl:for-each>
   <xsl:text>
  }
</xsl:text>
        <!-- Only need to take care of radio buttons for dependency visibility on page load  -->
        <xsl:if test="*/tw:VisualRadioButtons">
            <xsl:text>
            addOnPageLoadFunction(function(){
                var opts = dojo.query("#</xsl:text><xsl:call-template name="getId">
                <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
                </xsl:call-template><xsl:text> input[type='radio']");
                for(var i=0; i &lt; opts.length; i++) {
                    if (opts[i].checked) {
                        fcnDV</xsl:text><xsl:value-of select="$controlId"/><xsl:text>(opts[i], true);
                        return;
                    }
                }
            });
            </xsl:text>
        </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="visibilityConditionTrue">
      <xsl:text>result</xsl:text><xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template><xsl:text> = </xsl:text>
      <xsl:call-template name="visibilityValueToJSConstant">
          <xsl:with-param name="visValue" select="../@tw:visibility"/>
      </xsl:call-template>
      <xsl:text>;</xsl:text>
  </xsl:template>

  <xsl:template name="visibilityConditionFalse">
    <xsl:text>result</xsl:text><xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template><xsl:text> = mostRestrictiveOf(document.getElementById(&apos;</xsl:text>
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
      <xsl:text>&apos;).defaultVisibility, result</xsl:text><xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template><xsl:text>);</xsl:text>
  </xsl:template>

  <xsl:template name="visibilityConditionValue">
    <xsl:text disable-output-escaping="yes">&lt;# try { var val = </xsl:text>
    <xsl:value-of select="tw:Condition/tw:Value"/>
    <xsl:text disable-output-escaping="yes">;
        if (typeof val == &apos;string&apos; &amp;&amp; val.charAt(0) == &apos;{&apos; &amp;&amp; val.charAt(val.length - 1) == &apos;}&apos;) {
          buff.append(&apos;new Array(&apos;); buff.append(val.substring(1, val.length - 1)); buff.append(&apos;)&apos;);
        } else {
          buff.append(&apos;\&apos;&apos;); buff.append(val); buff.append(&apos;\&apos;&apos;);
        }
      } catch(err) { } #&gt;</xsl:text>
  </xsl:template>

  <xsl:template match="tw:InputText|tw:UserRoleInputText|tw:DateSelector" mode="head">
    <xsl:text>
</xsl:text>
      <!-- Note: ugly instructions  &lt;# buff.append("return ("); #&gt       &lt;# buff.append(")") #&gt;
           are used istead of
            &lt;#= "return (" &lt;#          &lt;#= ")" #&gt;
           to deceive our smart Coach preview.
           Therefore, don't optimize it!
      -->
      <xsl:text>function chkFld_</xsl:text><xsl:call-template name="getBindingNameWithZeroIndexValue"/><xsl:text disable-output-escaping="yes">(field) { &lt;# buff.append("return ( "); #&gt;</xsl:text>
      <xsl:text disable-output-escaping="yes">&lt;# try { var tmp = tw.system.validationScriptFor(&apos;</xsl:text>
      <xsl:call-template name="getBindingVariable"/>
      <xsl:text disable-output-escaping="yes">&apos;); if (tmp != &apos;&apos;) { buff.append(tmp); } else { buff.append(&apos;true&apos;);}  } catch(err) { buff.append(&apos;true&apos;); } #&gt; &lt;# buff.append(")") #&gt;; }</xsl:text>
  </xsl:template>

  <!-- Generate the default title for a form if no title is -->
  <!-- provided via a tw:FormTitle element -->
  <xsl:template name="defaultTitle">
    <xsl:text>Lombardi Coach</xsl:text>
  </xsl:template>

    <xsl:template name="body">
        <xsl:element name="BODY">
            <xsl:attribute name="onLoad">
                <xsl:text>pageLoadOperations()</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="class">soria</xsl:attribute>
            <form  style="display:none" id="helperForm" name="helperForm" action="" target="targetFrame" method="POST" enctype="multipart/form-data"/>
            <iframe style="display:none" id="targetFrame" name="targetFrame" src="javascript:false" />
            <table ID="content" style="" border="0" cellspacing="0" cellpadding="0" height="100%">
			<tr><td style="padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;" valign="top" height="1%">

                <TABLE id="innerContent" cellspacing="0" cellpadding="0" valign="top">
                    <TR id="innerContentHeadingTR">
                        <TD style="padding: 0px;">
                            <DIV class="heading">

                            <table class="headingTable" style="" border="0" cellspacing="0" cellpadding="0">
                            <tr><td class="headingLeft" nowrap="true">

                                <H1>
                                    <xsl:choose>
                                        <xsl:when test="tw:FormTitle">
                                            <xsl:value-of disable-output-escaping="yes" select="tw:FormTitle"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:call-template name="defaultTitle"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </H1>

                             </td>
                             <td class="headingCenter" nowrap="true">

                                <!--
                                  Verifiy if we run service without BPD, in this case do not show
                                  toolbar with AdHoc actions and Online Help -->
                                <DIV class="heading-right" style="text-align:right">
                                <xsl:choose>
                                    <xsl:when test="$previewMode != &apos;true&apos;">
                                        <xsl:text disable-output-escaping="yes">&lt;# if(Packages.teamworks.CoachDesignerUtils.hasBPDInstance(tw.system.task_id != null ? String(tw.system.task_id) : null)) { #&gt;</xsl:text>
                                        <xsl:call-template name="adHocActions"/>
                                        <xsl:element name="script">
                                            <xsl:attribute name="type">
                                                <xsl:text>text/javascript</xsl:text>
                                            </xsl:attribute>
                                            <xsl:text>updateSeparators()</xsl:text>
                                        </xsl:element>
                                        <xsl:text disable-output-escaping="yes">&lt;# } #&gt;</xsl:text>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <!-- Nothing during preview -->
                                    </xsl:otherwise>
                                </xsl:choose>
                                </DIV>

                                </td>
                                </tr>
                                </table>

                            </DIV>
                        </TD>
                    </TR>
                    <TR id="innerContentBodyTR">
                        <TD id="innerContentBodyTD" align="center" valign="top">
                            <xsl:apply-templates select="tw:Layout"/>
                            <xsl:element name="DIV">
                                <xsl:attribute name="id">
                                    <xsl:text>coachErrorMsg</xsl:text>
                                </xsl:attribute>
                            </xsl:element>
                        </TD>
                    </TR>
                </TABLE>


            </td></tr>
            <tr id="contentBottomTR"><td height="1%" valign="bottom">

                <DIV id="content-bottom" height="100%" valign="bottom">
                  <DIV class="left"></DIV>
                  <DIV class="right"></DIV>
                </DIV>

            </td></tr>
            </table>

            <!-- Generate scripts for initial selection. This should be done before setting up of initial visibility. -->
            <xsl:apply-templates select="//tw:BooleanChoice" mode="serversidescript"/>
            <xsl:apply-templates select="//tw:SingleSelect" mode="serversidescript"/>

<script type="text/javascript">
    setupInitialVisibility();
    <![CDATA[
	<#
		var userLanguage = Packages.com.lombardisoftware.client.LocaleUtils.getCurrentUserLocale().getLanguage();
		if(userLanguage=="ar"||userLanguage=="iw"){
	#>
		document.dir="rtl";
		var getElementsByClassName = function(klass, sc, tag){
			//Init
			var elements, i, results = [], curClass;

			//Default scope is document
			sc = sc || document;

			//Get all children of the scope node
			if(tag){
				elements = sc.getElementsByTagName(tag);
			} else {
				elements = sc.getElementsByTagName('*');
			}

			for( i=0; i < elements.length; i++ ){
				curClass = elements[i].className;
				if(curClass != null&&curClass==klass){
					results.push( elements[i] );
				}
			}
			return results;
		};
		var titles = getElementsByClassName("sectionHeaderTitle twSectionHeaderTitle");
		for(var j=0; j<titles.length; j++) {
			var title = titles[j];
			var tmpCss = title.style.cssText;
			tmpCss += " ;float: right;";
			title.style.cssText = tmpCss;
		}

		var leftHeadings = getElementsByClassName("sectionHeaderLeft");
		var rightHeadings = getElementsByClassName("sectionHeaderRight");
		for(var j=0; j<leftHeadings.length; j++) {
			var leftHeading = leftHeadings[j];
			leftHeading.attributes["class"].value = "sectionHeaderRight";
		}

		for(var j=0; j<rightHeadings.length; j++) {
			var rightHeading = rightHeadings[j];
			rightHeading.attributes["class"].value = "sectionHeaderLeft";
		}

		leftHeadings = getElementsByClassName("layoutHeaderLeft");
		rightHeadings = getElementsByClassName("layoutHeaderRight");
		for(var j=0; j<leftHeadings.length; j++) {
			var leftHeading = leftHeadings[j];
			leftHeading.attributes["class"].value = "layoutHeaderRight";
		}

		for(var j=0; j<rightHeadings.length; j++) {
			var rightHeading = rightHeadings[j];
			rightHeading.attributes["class"].value = "layoutHeaderLeft";
		}

		leftHeadings = getElementsByClassName("sectionBodyLeft sectionBodyLeftStyled");
		rightHeadings = getElementsByClassName("sectionBodyRight sectionBodyRightStyled");
		for(var j=0; j<leftHeadings.length; j++) {
			var leftHeading = leftHeadings[j];
			leftHeading.attributes["class"].value = "sectionBodyRight sectionBodyRightStyled";
		}

		for(var j=0; j<rightHeadings.length; j++) {
			var rightHeading = rightHeadings[j];
			rightHeading.attributes["class"].value = "sectionBodyLeft sectionBodyLeftStyled";
		}

		leftHeadings = getElementsByClassName("layoutBodyLeft layoutBodyLeftStyled");
		rightHeadings = getElementsByClassName("layoutBodyRight layoutBodyRightStyled");
		for(var j=0; j<leftHeadings.length; j++) {
			var leftHeading = leftHeadings[j];
			leftHeading.attributes["class"].value = "layoutBodyRight layoutBodyRightStyled";
		}

		for(var j=0; j<rightHeadings.length; j++) {
			var rightHeading = rightHeadings[j];
			rightHeading.attributes["class"].value = "layoutBodyLeft layoutBodyLeftStyled";
		}

		var head = getElementsByClassName("headingCenter");
		for(var i=0;i<head.length;i++){
			head[i].parentNode.removeChild(head[i]);
		}
		var headTitle = getElementsByClassName("headingLeft")[0];
		var tmpCss = headTitle.style.cssText;
		tmpCss += " ;float: right; margin-right: 10px;";
		headTitle.style.cssText = tmpCss;
		var headTitleH1 = document.getElementsByTagName("h1")[0];
		var tmpCss = headTitleH1.style.cssText;
		tmpCss += " ;float: right; margin-right: 10px;";
		headTitleH1.style.cssText = tmpCss;
<#}#>]]>
</script>
        </xsl:element>
    </xsl:template>

  <xsl:template name="formAction">
    <xsl:text>coach.lsw</xsl:text>
  </xsl:template>

  <xsl:template name="formMethod">
    <xsl:text>POST</xsl:text>
  </xsl:template>

  <xsl:template name="adHocActions">
    <xsl:text disable-output-escaping="yes">&lt;# if(Packages.teamworks.SecurityUtils.adHocEvent_canRun() &amp;&amp; Packages.teamworks.CoachDesignerUtils.hasAdHocEvents(String(tw.system.task_id))) { #&gt;</xsl:text>
    <xsl:element name="span">
      <xsl:attribute name="id">
        <xsl:text>adHocActions</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="style">
        <xsl:text>position:relative;</xsl:text>
      </xsl:attribute>
      <xsl:element name="span">
        <xsl:attribute name="name">
          <xsl:text>separator</xsl:text>
        </xsl:attribute>
        <xsl:text> | </xsl:text>
      </xsl:element>
      <xsl:element name="a">
        <xsl:attribute name="id">
          <xsl:text>newLink</xsl:text>
          </xsl:attribute>
        <xsl:attribute name="href">
          <xsl:text>#</xsl:text>
            </xsl:attribute>
            <xsl:element name="img">
	          <xsl:attribute name="src">
	            <xsl:text>images/adhoc_actions.gif</xsl:text>
	          </xsl:attribute>
          <xsl:attribute name="border">
            <xsl:text>0</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="style">
            <xsl:text>position:relative;top:5px;padding-right:4px;</xsl:text>
          </xsl:attribute>
        </xsl:element>
        Actions
        <xsl:element name="img">
          <xsl:attribute name="src">
            <xsl:text>images/popup.gif</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="border">
            <xsl:text>0</xsl:text>
          </xsl:attribute>
        </xsl:element>
      </xsl:element>
      <xsl:element name="div">
        <xsl:attribute name="id">
          <xsl:text>new</xsl:text>
        </xsl:attribute>
	          <xsl:attribute name="class">
          <xsl:text>popup</xsl:text>
	          </xsl:attribute>
        <xsl:attribute name="style">
          <xsl:text>position:absolute; z-index:1000; background: #056DA8;</xsl:text>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&lt;#= Packages.teamworks.CoachDesignerUtils.buildAdHocPopupMenuItemFromTemplate(String(tw.system.task_id), &quot;&lt;div style=\"padding-left: 3px; overflow: hidden; text-align: left;\"&gt;&lt;a href=\"javascript:doStartEvent(''{0,number,#}'',''{1}'');\"&gt;{2}&lt;/a&gt;&lt;/div&gt;&quot;) #&gt;</xsl:text>        
      </xsl:element>
      <xsl:element name="iframe">
        <xsl:attribute name="id">
          <xsl:text>newShim</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="src">
          <xsl:text>javascript:false</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="scrolling">
          <xsl:text>no</xsl:text>
	          </xsl:attribute>
        <xsl:attribute name="frameborder">
	            <xsl:text>0</xsl:text>
	          </xsl:attribute>
        <xsl:attribute name="style">
          <xsl:text>position:absolute; top:18px; left:0px; display:none;</xsl:text>
        </xsl:attribute>
      </xsl:element>
      <xsl:element name="script">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
	          </xsl:attribute>
        <xsl:text>
          // add hanlder for new from favorites toolbar item
          document.getElementById('newLink').onclick = function(e) {
              toggleNewPopup();
              
              // this event not suppose to be handled by body.onclick() handler
              var e = e || event;
              e.cancelBubble=true;
          };
          
          // handler for closing this popup
          document.body.onclick = function(){ 
              closeNewPopup();
          }
        </xsl:text>
	        </xsl:element>
          </xsl:element>
          <xsl:text disable-output-escaping="yes">&lt;# } #&gt;</xsl:text>
  </xsl:template>

  <!-- These standard fields are needed for all coaches -->
  <xsl:template name="standardHiddenFields">
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>zTaskId</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:text>&lt;#= String(tw.system.task_or_process_id) #&gt;</xsl:text>
      </xsl:attribute>
    </xsl:element>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>zComponentId</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:text>&lt;#= String(tw.system.component_id) #&gt;</xsl:text>
      </xsl:attribute>
    </xsl:element>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>zComponentName</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:text>Coach</xsl:text>
      </xsl:attribute>
    </xsl:element>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>zWorkflowState</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:text>3</xsl:text>
      </xsl:attribute>
    </xsl:element>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>zDbg</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:text>&lt;#= String(tw.system.debug_mode) #&gt;</xsl:text>
      </xsl:attribute>
    </xsl:element>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>zButtonName</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select=".//tw:Button[1]/@tw:id"/>
      </xsl:attribute>
    </xsl:element>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>zButtonId</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select=".//tw:Button[1]/@tw:id"/>
      </xsl:attribute>
    </xsl:element>
    <!-- zActType value of CoachDesigner is handled in CoachWebUI -->
      <xsl:element name="INPUT">
        <xsl:attribute name="type">
          <xsl:text>hidden</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="name">
          <xsl:text>zActType</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="value">
          <xsl:text>CoachDesigner</xsl:text>
        </xsl:attribute>
      </xsl:element>
      <!-- needed for debugging -->
      <xsl:element name="INPUT">
        <xsl:attribute name="type">
          <xsl:text>hidden</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="name">
          <xsl:text>applicationInstanceId</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="value">
          <xsl:text>&lt;#= String(tw.system.applicationInstanceId) #&gt;</xsl:text>
        </xsl:attribute>
      </xsl:element>
      <xsl:element name="INPUT">
        <xsl:attribute name="type">
          <xsl:text>hidden</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="name">
          <xsl:text>applicationId</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="value">
          <xsl:text>&lt;#= String(tw.system.applicationId) #&gt;</xsl:text>
        </xsl:attribute>
      </xsl:element>
  </xsl:template>

  <xsl:template match="tw:Layout">
    <!-- One form per layout -->
    <xsl:element name="FORM">
      <xsl:attribute name="action">
        <xsl:call-template name="formAction"/>
      </xsl:attribute>
      <xsl:attribute name="method">
        <xsl:call-template name="formMethod"/>
      </xsl:attribute>
      <xsl:attribute name="onSubmit">
        <xsl:text>clickSubmitOnSubmit(this);</xsl:text>
      </xsl:attribute>
      <xsl:if test="//tw:DocumentAttachment">
        <xsl:attribute name="enctype">
          <xsl:text>multipart/form-data</xsl:text>
        </xsl:attribute>
      </xsl:if>
      <xsl:call-template name="standardHiddenFields"/>
      <xsl:apply-templates select="tw:Section"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:Section">
    <xsl:element name="DIV">
      <xsl:attribute name="class">
        <xsl:text>sectionPadding</xsl:text>
        <xsl:call-template name="ie6ElementStyle">
          <xsl:with-param name="prefix">sectionPadding</xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
    <xsl:element name="TABLE">
      <xsl:attribute name="class">
        <xsl:call-template name="getClass">
          <xsl:with-param name="default">
            <xsl:call-template name="layoutOrSection"/>
          </xsl:with-param>
          <xsl:with-param name="override">
              <xsl:text> tw</xsl:text>
              <xsl:call-template name="layoutOrSectionCap"/>
              <xsl:text> </xsl:text>
            <xsl:for-each select="tw:Override[@tw:className]">
                <xsl:value-of select="@tw:className"/>
                <xsl:text> </xsl:text>
            </xsl:for-each>
          </xsl:with-param>
        </xsl:call-template>
        <!-- Add class to outline a section if it is tw:showHide enabled -->
        <xsl:if test="@tw:showHide or ../tw:Override[@tw:key='showHide']">
            <xsl:text> styled</xsl:text>
        </xsl:if>
        <!-- Section with title will get twStyledSection class -->
        <xsl:if test="tw:SectionTitle[@tw:visible='true']">
            <xsl:text> twStyled</xsl:text>
            <xsl:call-template name="layoutOrSectionCap"/>
        </xsl:if>
      </xsl:attribute>
        <xsl:if test="tw:HtmlOverride[@tw:title] and tw:HtmlOverride[@tw:title != '']">
            <xsl:attribute name="title">
                <xsl:value-of select="tw:HtmlOverride/@tw:title"/>
            </xsl:attribute>
        </xsl:if>
        <xsl:if test="tw:HtmlOverride[@tw:style] and tw:HtmlOverride[@tw:style != '']">
            <xsl:attribute name="style">
                <xsl:value-of select="tw:HtmlOverride/@tw:style"/>
            </xsl:attribute>
        </xsl:if>
      <xsl:attribute name="cellpadding">
          <xsl:choose>
              <xsl:when test="tw:Customization[@tw:cellpadding] and tw:Customization[@tw:cellpadding != '']">
                  <xsl:value-of select="tw:Customization/@tw:cellpadding"/>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:text>0</xsl:text>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:attribute>
      <xsl:attribute name="cellspacing">
          <xsl:choose>
              <xsl:when test="tw:Customization[@tw:cellspacing] and tw:Customization[@tw:cellspacing != '']">
                  <xsl:value-of select="tw:Customization/@tw:cellspacing"/>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:text>0</xsl:text>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:attribute>
        <xsl:if test="tw:Customization[@tw:border] and tw:Customization[@tw:border != '']">
            <xsl:attribute name="border">
                <xsl:value-of select="tw:Customization/@tw:border"/>
            </xsl:attribute>
        </xsl:if>
        <xsl:attribute name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:if test="tw:SectionTitle[@tw:visible = 'true']">
        <xsl:call-template name="sectionHeader"/>
      </xsl:if>
      <xsl:call-template name="sectionBody"/>
      <xsl:call-template name="sectionFooter"/>
    </xsl:element>
    </xsl:element>
    <xsl:apply-templates select="../tw:Visibility"/>

  </xsl:template>

  <xsl:template name="sectionHeader">
    <xsl:variable name="classPrefix">
      <xsl:call-template name="layoutOrSection"/>
      <xsl:choose>
        <xsl:when test="tw:SectionTitle[@tw:visible = 'true']">
          <xsl:text>Header</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>Body</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:element name="TR">
      <xsl:attribute name="class">
        <xsl:if test="tw:SectionTitle[@tw:cssClass]">
            <xsl:value-of select="tw:SectionTitle/@tw:cssClass"/>
            <xsl:text> </xsl:text>
        </xsl:if>
          <xsl:value-of select="$classPrefix"/>
          <xsl:text> tw</xsl:text>
          <xsl:call-template name="layoutOrSectionCap"/>
          <xsl:text>Title</xsl:text>
      </xsl:attribute>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Left</xsl:text>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Center</xsl:text>
        </xsl:attribute>
        <xsl:choose>
          <xsl:when test="tw:SectionTitle[@tw:visible = 'true']">
            <xsl:attribute name="id">
              <xsl:call-template name="getId">
                <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
              </xsl:call-template>
              <xsl:text>_SectionTitle</xsl:text>
            </xsl:attribute>
            <xsl:call-template name="styleOverride"/>
              <!-- This section was provides the Show / Hide functionality for a section -->
              <!-- This next area adds the clickable image for expanding / collapsing -->
            <xsl:if test="@tw:showHide or ../tw:Override[@tw:key='showHide']">
              <xsl:variable name="controlId">
                <xsl:call-template name="getId">
                  <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
                </xsl:call-template>
                <xsl:text>_controls</xsl:text>
              </xsl:variable>

              <xsl:variable name="defaultImage">
                <xsl:choose>
                  <xsl:when test="@tw:showHide='hidden' or ../tw:Override[@tw:key='showHide'] and ../tw:Override[@tw:value='hidden']">
                    <xsl:text disable-output-escaping="yes">&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix());  } catch(err) { } #&gt;</xsl:text><xsl:text>images/EventManagerMonitor/nextpage.gif</xsl:text>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:text disable-output-escaping="yes">&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getServer().getWebImages().getPrefix());  } catch(err) { } #&gt;</xsl:text><xsl:text>images/EventManagerMonitor/sortdesc.gif</xsl:text>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:variable>

              <xsl:element name="DIV">
                <xsl:attribute name="class">
                  <xsl:value-of select="$classPrefix"/>
                  <xsl:text>Twisty twisty</xsl:text>
                </xsl:attribute>

                <xsl:attribute name="onClick">
                  <xsl:text>fcnToggleHide('</xsl:text>
                  <xsl:value-of select="$controlId" />
                  <xsl:text>')</xsl:text>
                </xsl:attribute>

                <xsl:element name="img">
                  <xsl:attribute name="src">
                    <xsl:value-of select="$defaultImage" />
                  </xsl:attribute>
                    <xsl:attribute name="name">
                    <xsl:value-of select="$controlId" />
                    <xsl:text>_image_</xsl:text>
                  </xsl:attribute>
                </xsl:element>

              </xsl:element>
            </xsl:if>

            <!-- A DIV needs to be added to align the this title with Show / Hide div -->
            <xsl:element name="DIV">
              <xsl:attribute name="class">
                <xsl:text>sectionHeaderTitle twSectionHeaderTitle</xsl:text>
              </xsl:attribute>
            <xsl:value-of select="tw:SectionTitle" disable-output-escaping="yes"/>
            </xsl:element>

            <!-- Added for displaying Header content bar -->
            <xsl:if test="tw:SectionHeaderDisplay">
              <xsl:element name="DIV">
                <xsl:attribute name="class">
                  <xsl:text>sectionHeaderContent twSectionHeaderContent</xsl:text>
                </xsl:attribute>
                <xsl:apply-templates select="tw:SectionHeaderDisplay"/>
              </xsl:element>
            </xsl:if>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:element>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Right</xsl:text>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
    </xsl:element>
    <xsl:if test="tw:SectionTitle[@tw:visible = 'true']">
      <xsl:element name="TR">
        <xsl:attribute name="class">
          <xsl:call-template name="layoutOrSection"/>
          <xsl:text>SubHeader tw</xsl:text>
            <xsl:call-template name="layoutOrSectionCap"/>
            <xsl:text>SubHeader</xsl:text>
        </xsl:attribute>
        <xsl:element name="TD">
          <xsl:attribute name="class">
            <xsl:call-template name="layoutOrSection"/>
            <xsl:text>SubHeader</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="colspan">
            <xsl:text>3</xsl:text>
          </xsl:attribute>
          <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
        </xsl:element>
      </xsl:element>
    </xsl:if>
  </xsl:template>

<!-- This is applied to each sectionHeaderDisplay -->
  <xsl:template match="tw:SectionHeaderDisplay">
    <xsl:call-template name="getBindingValue">
      <xsl:with-param name="noEscape">1</xsl:with-param>
    </xsl:call-template>
</xsl:template>

  <!-- add attributes to control -->
  <xsl:template match="tw:Attribute">
    <xsl:attribute name="{@tw:name}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>



  <xsl:template name="sectionBody">
    <xsl:variable name="classPrefix">
      <xsl:call-template name="layoutOrSection"/>
      <xsl:text>Body</xsl:text>
    </xsl:variable>
    <xsl:element name="TR">
      <xsl:attribute name="class">
        <xsl:value-of select="$classPrefix"/>
          <xsl:text> tw</xsl:text>
          <xsl:call-template name="layoutOrSectionCap"/>
          <xsl:text>Body</xsl:text>
      </xsl:attribute>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Left</xsl:text>
          <xsl:call-template name="ie6ElementStyle">
            <xsl:with-param name="prefix"><xsl:value-of select="$classPrefix"/>Left</xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Center</xsl:text>
          <xsl:call-template name="ie6ElementStyle">
            <xsl:with-param name="prefix"><xsl:value-of select="$classPrefix"/>Center</xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:call-template name="controlsAndButtons"/>
      </xsl:element>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Right</xsl:text>
          <xsl:call-template name="ie6ElementStyle">
            <xsl:with-param name="prefix"><xsl:value-of select="$classPrefix"/>Right</xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template name="sectionFooter">
    <xsl:variable name="classPrefix">
      <xsl:call-template name="layoutOrSection"/>
      <xsl:text>Footer</xsl:text>
    </xsl:variable>
    <xsl:element name="TR">
      <xsl:attribute name="class">
        <xsl:value-of select="$classPrefix"/>
        <xsl:text> tw</xsl:text>
        <xsl:call-template name="layoutOrSectionCap"/>
        <xsl:text>Footer</xsl:text>
        <xsl:call-template name="ie6ElementStyle">
          <xsl:with-param name="prefix"><xsl:value-of select="$classPrefix"/></xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Left</xsl:text>
          <xsl:call-template name="ie6ElementStyle">
            <xsl:with-param name="prefix"><xsl:value-of select="$classPrefix"/>Left</xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Center</xsl:text>
          <xsl:call-template name="ie6ElementStyle">
            <xsl:with-param name="prefix"><xsl:value-of select="$classPrefix"/>Center</xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
      <xsl:element name="TD">
        <xsl:attribute name="class">
          <xsl:value-of select="$classPrefix"/>
          <xsl:text>Right</xsl:text>
          <xsl:call-template name="ie6ElementStyle">
            <xsl:with-param name="prefix"><xsl:value-of select="$classPrefix"/>Right</xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template name="ie6ElementStyle">
    <xsl:param name="prefix"/>
    <xsl:if test="tw:SectionTitle[@tw:visible='true']">
      <xsl:text> </xsl:text>
      <xsl:value-of select="$prefix"/>
      <xsl:text>Styled</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template name="controlsAndButtons">
    <xsl:call-template name="sectionHeading"/>

<!-- Define variables used by tw:showHide default settings -->
    <xsl:variable name="controlId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
      <xsl:text>_controls</xsl:text>
    </xsl:variable>

    <xsl:variable name="defaultFunction">
      <xsl:choose>
        <xsl:when test="@tw:showHide='hidden' or ../tw:Override[@tw:key='showHide' and @tw:value='hidden']">
          <xsl:text>fcnToggleHide_Hide</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="starts-with(@tw:showHide,'&lt;#=')">
              <xsl:text disable-output-escaping="yes">&lt;#  try { if ( </xsl:text>
              <xsl:value-of select="substring-after(substring-before(@tw:showHide, '#&gt;'), '&lt;#=')" />
              <xsl:text disable-output-escaping="yes"> == 'hidden') { buff.append(&quot;fcnToggleHide_Hide&quot;); } else { buff.append(&quot;fcnToggleHide_Show&quot;);} } catch(err) { } #&gt;</xsl:text>
            </xsl:when>
            <xsl:when test="../tw:Override[@tw:key='showHide' and starts-with(@tw:value, '&lt;#=')]">
              <xsl:text disable-output-escaping="yes">&lt;#  try { if ( </xsl:text>
              <xsl:value-of select="substring-after(substring-before(../tw:Override[@tw:key='showHide']/@tw:value, '#&gt;'), '&lt;#=')" />
              <xsl:text disable-output-escaping="yes"> == 'hidden') { buff.append(&quot;fcnToggleHide_Hide&quot;); } else { buff.append(&quot;fcnToggleHide_Show&quot;);} } catch(err) { } #&gt;</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>fcnToggleHide_Show</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
<!-- End tw:showHide default settings -->

<!-- For scrollable sections and tw:showHide -->
    <xsl:element name="DIV">
      <xsl:attribute name="class">
        <xsl:text>sectionBody twSectionBody</xsl:text>
        <xsl:if test="@tw:height">
          <xsl:text>Scrollable</xsl:text>
        </xsl:if>
      </xsl:attribute>

      <xsl:if test="@tw:height">
        <xsl:attribute name="style">
          <xsl:text>height:</xsl:text>
          <xsl:value-of select="@tw:height"/>
          <xsl:text>;</xsl:text>
        </xsl:attribute>
      </xsl:if>
<!-- End DIV section -->

<!-- ID needed for tw:showHide -->
      <xsl:attribute name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
        <xsl:text>_controls</xsl:text>
      </xsl:attribute>

    <xsl:element name="TABLE">
      <xsl:attribute name="class">
        <xsl:text>controlLayout</xsl:text>
      </xsl:attribute>
      <xsl:call-template name="controls"/>
    </xsl:element>

    </xsl:element>

    <!-- tw:showHide set initial image and visibility -->
    <xsl:if test="@tw:showHide or ../tw:Override[@tw:key='showHide']">
      <xsl:element name="SCRIPT">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
        <xsl:value-of select="$defaultFunction" />
        <xsl:text>('</xsl:text>
        <xsl:value-of select="$controlId" />
        <xsl:text>');</xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template name="sectionHeading">
    <xsl:if test="tw:SectionHeading[@tw:visible = 'true']">
      <xsl:element name="P">
        <xsl:attribute name="id">
          <xsl:call-template name="getId">
            <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
          </xsl:call-template>
          <xsl:text>_SectionHeading</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="class">
          <xsl:call-template name="getClass">
            <xsl:with-param name="default">
              <xsl:call-template name="layoutOrSection"/>
              <xsl:text>Heading</xsl:text>
            </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text> tw</xsl:text>
                <xsl:call-template name="layoutOrSectionCap"/>
                <xsl:text>Heading</xsl:text>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:call-template name="styleOverride"/>
        <xsl:value-of select="tw:SectionHeading" disable-output-escaping="yes"/>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template name="controls">
    <xsl:apply-templates select="tw:Row"/>
  </xsl:template>

  <xsl:template match="tw:Row">
    <xsl:call-template name="layoutControls">
      <xsl:with-param name="rowIndex">
        <xsl:text>1</xsl:text>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="layoutControls">
    <xsl:param name="rowIndex"/>
    <xsl:if test="$rowIndex &lt; @tw:rows + 1">
      <xsl:element name="TR">
          <xsl:attribute name="class">
              <xsl:text>twControlTR</xsl:text>
          </xsl:attribute>
        <!-- Use the id of the section as the basis of the id for the row -->
        <xsl:variable name="startingId">
          <xsl:value-of select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
          <xsl:text>_Row</xsl:text>
          <xsl:value-of select="position() - 1"/>
        </xsl:variable>
        <xsl:attribute name="id">
          <xsl:call-template name="getId">
            <xsl:with-param name="startingId" select="$startingId"/>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:call-template name="layoutColumn">
            <xsl:with-param name="rowIndex" select="$rowIndex"/>
            <xsl:with-param name="colIndex">
                <xsl:text>1</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:element>
      <xsl:call-template name="layoutControls">
        <xsl:with-param name="rowIndex" select="$rowIndex + 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
	
	<xsl:template name="getControlClasses">
		<xsl:param name="twControl"/>
		<xsl:for-each select="$twControl">
			<xsl:for-each select="tw:Override">
				<xsl:text> </xsl:text>
				<xsl:value-of select="@tw:className"/>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
	
    <xsl:template name="layoutColumn">
        <xsl:param name="rowIndex"/>
        <xsl:param name="colIndex"/>
        <xsl:variable name="classes">
            <xsl:call-template name="getControlClasses">
				<xsl:with-param name="twControl" select="tw:Column/tw:Control[@tw:row = $rowIndex and @tw:col = $colIndex][tw:Override/@tw:className]"/>
			</xsl:call-template>
        </xsl:variable>
        <xsl:variable name="classPrefix">
            <xsl:call-template name="layoutOrSection"/>
            <xsl:text>BodyCenter</xsl:text>
        </xsl:variable>
        <xsl:variable name="rowCount" select="count(tw:Column)"/>
        <xsl:if test="$colIndex &lt; count(tw:Column) + 1">
		    <xsl:variable name="columnWidth" select="tw:Column[position() = $colIndex]/@tw:width"/>
            <xsl:choose>
                <xsl:when test="tw:Column/tw:Control[@tw:col = $colIndex and @tw:row = $rowIndex]">
                    <xsl:for-each select="tw:Column/tw:Control[@tw:col = $colIndex and @tw:row = $rowIndex]">
                        <xsl:if test="*/tw:Label[@tw:visible = 'true' and (not(@tw:position) or (@tw:position != 'top' and @tw:position != 'control'))]">
                            <xsl:element name="TD">
                                <xsl:attribute name="class">
                                    <xsl:choose>
                                        <xsl:when test="@tw:cssClass and @tw:cssClass != ''">
                                            <xsl:value-of select="@tw:cssClass"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="$classPrefix"/>
                                            <xsl:text>Label</xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:text> twLabelTD </xsl:text>
                                    <xsl:value-of select="$classes"/>
                                </xsl:attribute>
                                <xsl:if test="@tw:cssStyle and @tw:cssStyle != ''">
                                    <xsl:attribute name="style">
                                        <xsl:value-of select="@tw:cssStyle"/>
                                    </xsl:attribute>
                                </xsl:if>
                                <xsl:attribute name="width">
                                    <xsl:text>1%</xsl:text>
                                </xsl:attribute>
                                <xsl:attribute name="valign">
                                    <xsl:text>top</xsl:text>
                                </xsl:attribute>
                                <xsl:attribute name="colspan">
                                    <xsl:text>1</xsl:text>
                                    </xsl:attribute>
                                    <xsl:apply-templates select="*/tw:Label" mode="label"/>
                            <!-- Comment out for positioning
                            <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                            -->
                          </xsl:element>
                        </xsl:if>
                        <xsl:element name="TD">
                            <xsl:attribute name="class">
                                <xsl:choose>
                                    <xsl:when test="@tw:cssClass and @tw:cssClass != ''">
                                        <xsl:value-of select="@tw:cssClass"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="$classPrefix"/>
                                        <xsl:text>Control</xsl:text>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <xsl:text> twControlTD </xsl:text>
                                <xsl:value-of select="$classes"/>
                            </xsl:attribute>
                            <xsl:if test="@tw:cssStyle and @tw:cssStyle != ''">
                                <xsl:attribute name="style">
                                    <xsl:value-of select="@tw:cssStyle"/>
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:attribute name="valign">
                                <xsl:text>top</xsl:text>
                            </xsl:attribute>
                            <xsl:choose>
                                <xsl:when test="$columnWidth != ''">
	                                <xsl:attribute name="width">
	                                    <xsl:value-of select="$columnWidth"/>
	                                </xsl:attribute>
                                </xsl:when>
                                <xsl:otherwise>
	                                <xsl:attribute name="width">
	                                    <xsl:value-of select="floor( 100 div ($rowCount*2))"/>
	                                    <xsl:text>%</xsl:text>
	                                </xsl:attribute>
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:if test="@tw:rowspan">
                                <xsl:attribute name="rowspan">
                                    <xsl:value-of select="@tw:rowspan"/>
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:attribute name="colspan">
                                <xsl:choose>
                                    <xsl:when test="@tw:colspan">
                                        <xsl:choose>
                                            <xsl:when
                                                    test="*/tw:Label[@tw:visible = 'true' and (@tw:position = 'top' or @tw:position = 'control')]">
                                                <xsl:value-of select="@tw:colspan * 2 "/>
                                            </xsl:when>
                                            <xsl:when test="*/tw:Label[@tw:visible = 'true']">
                                                <xsl:value-of select="@tw:colspan * 2 - 1"/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:value-of select="@tw:colspan * 2"/>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:choose>
                                            <xsl:when
                                                    test="*/tw:Label[@tw:visible = 'true' and (@tw:position = 'top' or @tw:position = 'control')]">
                                                <xsl:text>2</xsl:text>
                                            </xsl:when>
                                            <xsl:when test="*/tw:Label[@tw:visible = 'true']">
                                                <xsl:text>1</xsl:text>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:text>2</xsl:text>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:attribute>

                            <!-- Changed line to include label position logic -->
                            <!-- There is a cleaner way to do this instead of copying the entire section of xml -->
                            <xsl:if test="*/tw:Label[@tw:visible = 'true' and @tw:position = 'top']">
                                <xsl:element name="DIV">
                                    <xsl:attribute name="class">
                                        <xsl:choose>
                                            <xsl:when test="@tw:cssClass and @tw:cssClass != ''">
                                                <xsl:value-of select="@tw:cssClass"/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:value-of select="$classPrefix"/>
                                                <xsl:text>Label</xsl:text>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:attribute>
                                    <xsl:if test="@tw:cssStyle and @tw:cssStyle != ''">
                                        <xsl:attribute name="style">
                                            <xsl:value-of select="@tw:cssStyle"/>
                                        </xsl:attribute>
                                    </xsl:if>

                                    <xsl:attribute name="style">
                                        <xsl:value-of select="@tw:cssStyle"/>
                                        <xsl:text>float:left</xsl:text>
                                    </xsl:attribute>

                                    <xsl:apply-templates select="*/tw:Label" mode="label"/>

                                    <!-- Commented out for top position label
                                    <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                                    -->

                                </xsl:element>
                            </xsl:if>
                            <xsl:apply-templates
                                    select="tw:Report|tw:OutputText|tw:InputText|tw:UserRoleInputText|tw:BooleanChoice|tw:SingleSelect|tw:MultipleSelect|tw:DateSelector|tw:Table|tw:Section|tw:CustomHTML|tw:LabeledCustomHTML|tw:ButtonGroup|tw:DocumentAttachment|tw:DocumentViewer|tw:Image|tw:FileAttachment"/>
                            <xsl:apply-templates
                                    select="tw:Visibility[not(@tw:overrideParentVisibility) or @tw:overrideParentVisibility = 'true']"/>
                            <xsl:call-template name="recordVisibilityFunction"/>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:if test="not(tw:Column/tw:Control[@tw:col &lt; ($colIndex + 1) and @tw:row &lt; ($rowIndex + 1) and not(@tw:col = $colIndex and @tw:row = $rowIndex) and (@tw:rowspan + @tw:row - 1) &gt;= $rowIndex and (@tw:colspan + @tw:col - 1) &gt;= $colIndex])">
                        <xsl:element name="TD">
                            <xsl:if test="../tw:Column[position() = $colIndex]/@tw:width">
                                <xsl:attribute name="width">
                                    <xsl:value-of select="../tw:Column[position() = $colIndex]/@tw:width"/>
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:attribute name="class">
                                <xsl:value-of select="$classPrefix"/>
                                <xsl:text>Control</xsl:text>
                            </xsl:attribute>
                            <xsl:attribute name="colspan">
                                <xsl:text>2</xsl:text>
                            </xsl:attribute>
                            <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                        </xsl:element>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:call-template name="layoutColumn">
                <xsl:with-param name="rowIndex" select="$rowIndex"/>
                <xsl:with-param name="colIndex" select="$colIndex + 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="applyAllControls">
    <xsl:variable name="isInCell">
      <xsl:call-template name="isInCell"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$isInCell = &apos;1&apos;">
        <xsl:for-each select="tw:Control">
          <xsl:apply-templates select="tw:Report|tw:OutputText|tw:InputText|tw:UserRoleInputText|tw:BooleanChoice|tw:SingleSelect|tw:MultipleSelect|tw:DateSelector|tw:Table|tw:Section|tw:CustomHTML|tw:LabeledCustomHTML|tw:ButtonGroup|tw:DocumentAttachment|tw:DocumentViewer|tw:Image|tw:FileAttachment"/>
          <xsl:apply-templates select="tw:Visibility[not(@tw:overrideParentVisibility) or @tw:overrideParentVisibility = 'true']"/>
          <xsl:call-template name="recordVisibilityFunction"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="tw:Control">
          <xsl:element name="DIV">
            <xsl:apply-templates select="tw:Report|tw:OutputText|tw:InputText|tw:UserRoleInputText|tw:BooleanChoice|tw:SingleSelect|tw:MultipleSelect|tw:DateSelector|tw:Table|tw:Section|tw:CustomHTML|tw:LabeledCustomHTML|tw:ButtonGroup|tw:DocumentAttachment|tw:DocumentViewer|tw:Image|tw:FileAttachment"/>
            <xsl:apply-templates select="tw:Visibility[not(@tw:overrideParentVisibility) or @tw:overrideParentVisibility = 'true']"/>
            <xsl:call-template name="recordVisibilityFunction"/>
          </xsl:element>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="recordVisibilityFunction">
      <xsl:variable name="control" select="."/>
      <xsl:variable name="controlId" select="@tw:id"/>
      <xsl:if test="//tw:VisibilityCondition[@tw:controlId = $controlId]">
          <xsl:variable name="functionName">
              <xsl:text>fcnDV</xsl:text>
              <xsl:value-of select="$controlId"/>
          </xsl:variable>
          <xsl:element name="SCRIPT">
              <xsl:attribute name="type">
                  <xsl:text>text/javascript</xsl:text>
              </xsl:attribute>
              <xsl:text>var element = document.getElementById(&apos;</xsl:text>
              <xsl:value-of select="$controlId"/>
              <xsl:if test="$control/tw:BooleanChoice/tw:VisualCheckboxes">
                <xsl:text>_checkbox</xsl:text>
              </xsl:if>
              <xsl:text>&apos;).visibilityFunction = &apos;</xsl:text>
              <xsl:value-of select="$functionName"/>
              <xsl:text>&apos;;</xsl:text>
          </xsl:element>
      </xsl:if>
  </xsl:template>

  <xsl:template match="tw:Visibility">
    <xsl:variable name="thisId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="string-length($thisId) &gt; 0">
    <xsl:if test="@tw:overrideParentVisibility = 'true'">
    <xsl:element name="SCRIPT">
      <xsl:attribute name="type">
        <xsl:text>text/javascript</xsl:text>
      </xsl:attribute>
      <xsl:text>          
          var result = &apos;&apos;;
      </xsl:text>
      <xsl:text disable-output-escaping="yes">&lt;#</xsl:text>
      <xsl:apply-templates select="tw:DependsOnRole"/>
      <xsl:apply-templates select="tw:ServerScript" mode="visibility"/>
      <xsl:text disable-output-escaping="yes">
   #&gt;
   </xsl:text>
      <xsl:text>if (result == &apos;&apos;) { result = </xsl:text>
      <xsl:call-template name="visibilityValueToJSConstant">
          <xsl:with-param name="visValue" select="@tw:default"/>
      </xsl:call-template>
      <xsl:text>; }</xsl:text>
      <xsl:text>
   addInitialVisibilitySetup(document.getElementById(&apos;</xsl:text><xsl:value-of select="$thisId"/><xsl:text>&apos;), result);
    </xsl:text>
    </xsl:element>
    </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="visibilityValueToJSConstant">
      <xsl:param name="visValue"><xsl:text>full</xsl:text></xsl:param>
      <xsl:choose>
        <xsl:when test="$visValue = &apos;none&apos;">
          <xsl:text>&apos;NONE&apos;</xsl:text>
        </xsl:when>
        <xsl:when test="$visValue = &apos;read&apos;">
          <xsl:text>&apos;READ&apos;</xsl:text>
        </xsl:when>
        <xsl:when test="$visValue = &apos;required&apos;">
          <xsl:text>&apos;REQUIRED&apos;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>&apos;FULL&apos;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template match="tw:DependsOnRole">
    <xsl:text>
	var pg = tw.system.org.findParticipantGroupByName(&quot;</xsl:text>
    <xsl:value-of select="@tw:role"/>
    <xsl:text>&quot;);  
  if (tw.system.user.isInRole(pg.associatedRole.name)) { buff.append(&quot; result = mostRestrictiveOf(</xsl:text>
      <xsl:call-template name="visibilityValueToJSConstant">
          <xsl:with-param name="visValue" select="@tw:visibility"/>
      </xsl:call-template>
    <xsl:text>, result);&quot;); }</xsl:text>
  </xsl:template>

  <xsl:template match="tw:ServerScript" mode="visibility">
    <xsl:variable name="controlId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        <xsl:with-param name="stripLastIndex">1</xsl:with-param>
        <xsl:with-param name="indexSeparator">_</xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="functionName">
      <xsl:text>fcnV</xsl:text>
      <xsl:value-of select="$controlId"/>_<xsl:value-of select="position()"/>
    </xsl:variable>
    <xsl:text>
  function </xsl:text>
    <xsl:value-of select="$functionName"/>
    <xsl:text>() {
</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>
  }
  buff.append(&quot; result = mostRestrictiveOf(&quot;);
  switch (</xsl:text>
    <xsl:value-of select="$functionName"/>
    <xsl:text>()) {
    case &apos;NONE&apos;:
      buff.append(&quot;&apos;NONE&apos;&quot;);
      break;
    case &apos;READ&apos;:
      buff.append(&quot;&apos;READ&apos;&quot;);
      break;
    case &apos;REQUIRED&apos;:
      buff.append(&quot;&apos;REQUIRED&apos;&quot;);
      break;
    default:
      buff.append(&quot;&apos;FULL&apos;&quot;);
  }
  buff.append(&quot;, result);&quot;);</xsl:text>
  </xsl:template>

  <xsl:template match="tw:OutputText">

    <xsl:variable name="isInCell">
      <xsl:call-template name="isInCell"/>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$isInCell = &apos;1&apos;">
            <xsl:element name="DIV">
                <xsl:attribute name="id">
                    <xsl:call-template name="getBindingName"/>
                </xsl:attribute>
                <xsl:call-template name="htmlOutputText"/>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="htmlOutputText"/>
        </xsl:otherwise>
    </xsl:choose>


    <xsl:if test="tw:ContentDependency and tw:BasedOn[@tw:type = 'Ajax Service']">
        <xsl:variable name="id">
            <xsl:call-template name="getId">
            <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:element name="SCRIPT">
            <xsl:attribute name="type">
                <xsl:text>text/javascript</xsl:text>
            </xsl:attribute>
            <xsl:text>
            addOnPageLoadFunction( function(){
                 var serviceRef="</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:srcServiceRef"/><xsl:text>";
                 var controlId="</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:controlId"/><xsl:text>";
                 var selfId = "</xsl:text><xsl:value-of select="$id"/><xsl:text>";
                 var element = document.getElementById(controlId);
                 
                 function handleOnchange(){
                    tw.coach.refreshDependentElementById("</xsl:text><xsl:value-of select="$id"/><xsl:text>","</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:srcServiceRef"/><xsl:text>","</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:controlId"/><xsl:text>","</xsl:text><xsl:call-template name="getBindingValue"></xsl:call-template><xsl:text>","&lt;# try{buff.append(tw.system.model.processAppSnapshot.id)} catch(err) { } #&gt;");
                 }
                 
                 if(element.tagName == "INPUT") {
                    element.refreshDependent = handleOnchange;
                    registerDeferredOnInputChange(element);
                 } else {
                    addEventHandler(element, "onchange", handleOnchange);
                 }
                 
                 handleOnchange();
            });
             </xsl:text>
        </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template name="htmlOutputText">

    <xsl:variable name="controlId">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:element name="P">
      <xsl:if test="$previewMode != 'true'">

        <xsl:variable name="bindingVar">
          <xsl:call-template name="getBindingVariable"/>
        </xsl:variable>

        <xsl:if test="@tw:format and @tw:format != ''">
           <xsl:attribute name="formatt">
             <xsl:value-of select="@tw:format" />
           </xsl:attribute>
        </xsl:if>

        <!-- This is helper attribute to work with locales for non-formatted fields -->
        <xsl:attribute name="locale">
          <xsl:text>&lt;#= tw.system.user_locale #&gt;</xsl:text>
        </xsl:attribute>

        <xsl:attribute name="isNumeric">
          <xsl:text disable-output-escaping="yes">&lt;#= tw.system.isBaseTypeNumber('</xsl:text>
          <xsl:value-of select="$bindingVar"/>
          <xsl:text disable-output-escaping="yes">') #&gt;</xsl:text>
        </xsl:attribute>

        <xsl:if test="$bindingVar != ''">
          <xsl:attribute name="baseType">
            <xsl:text disable-output-escaping="yes">&lt;#= tw.system.getBaseType('</xsl:text>
            <xsl:value-of select="$bindingVar"/>
            <xsl:text disable-output-escaping="yes">') #&gt;</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="dateFormat">
            <xsl:text disable-output-escaping="yes">&lt;# var format = ''; try { if (tw.system.isBaseTypeDate('</xsl:text>
            <xsl:value-of select="$bindingVar"/>
            <xsl:text disable-output-escaping="yes">')) { format = tw.system.dateFormatForVariable('</xsl:text>
            <xsl:value-of select="$bindingVar"/>
            <xsl:text disable-output-escaping="yes">'); } } catch(err) { } buff.append(String(format)); #&gt;</xsl:text>
          </xsl:attribute>
        </xsl:if>

      </xsl:if>

      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>outputText</xsl:text>
          </xsl:with-param>
          <xsl:with-param name="override">
              <xsl:text>twOutputText</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="$controlId"/></xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:call-template name="eventHandlers"/>
      <!-- If there is a binding then ignore any Text element -->
      <xsl:choose>
        <xsl:when test="@tw:binding">
          <xsl:choose>
            <xsl:when test="@tw:escapeContent = 'true'">
              <xsl:call-template name="getBindingValue">
              <!-- JR42280: Escape Output Text content to avoid cross site scripting vulnerability -->
                 <!--<xsl:with-param name="noEscape">1</xsl:with-param>-->
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="getBindingValue">
              <xsl:with-param name="noEscape">1</xsl:with-param>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:when test="tw:Text">
          <xsl:apply-templates select="tw:Text"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
    <!--
    <xsl:call-template name="dataChanged">
      <xsl:with-param name="elementId"><xsl:value-of select="$controlId"/></xsl:with-param>
    </xsl:call-template>
    -->

  </xsl:template>

  <!-- Text element just outputs its contents, which may include embedded javascript -->
  <xsl:template match="tw:Text">
    <xsl:value-of select="." disable-output-escaping="yes"/>
  </xsl:template>

  <!-- xsl:template match="tw:UserRoleInputText">

    <xsl:call-template name="htmlInputTypeText"/>

    <xsl:variable name="bindingVar">
      <xsl:call-template name="getBindingVariable"/>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="@tw:lines and @tw:lines &gt; 1">
        <xsl:call-template name="htmlTextArea"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="htmlInputTypeText"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:variable name="bindingVar">
      <xsl:call-template name="getBindingVariable"/>
    </xsl:variable>
    <xsl:text disable-output-escaping="yes">&lt;# try { if (tw.system.isBaseTypeDate('</xsl:text>
    <xsl:value-of select="$bindingVar"/>
    <xsl:text>') || tw.system.isBaseTypeTime('</xsl:text>
    <xsl:value-of select="$bindingVar"/>
    <xsl:text disable-output-escaping="yes">')) { #&gt;</xsl:text>
    <xsl:element name="INPUT">
        <xsl:attribute name="type">hidden</xsl:attribute>
        <xsl:attribute name="name">
            <xsl:text>DATE_FORMAT_FOR_</xsl:text>
            <xsl:call-template name="getBindingName"/>
        </xsl:attribute>
        <xsl:attribute name="value">
            <xsl:text disable-output-escaping="yes">&lt;# try { buff.append(tw.system.dateFormatForVariable('</xsl:text>
            <xsl:value-of select="$bindingVar"/>
            <xsl:text disable-output-escaping="yes">')); } catch(err) { } #&gt;</xsl:text>
        </xsl:attribute>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;# } else if (tw.system.isBaseTypeNumber('</xsl:text>
    <xsl:value-of select="$bindingVar"/>
    <xsl:text disable-output-escaping="yes">')) { #&gt;</xsl:text>
    <xsl:element name="INPUT">
        <xsl:attribute name="type">hidden</xsl:attribute>
        <xsl:attribute name="name">
            <xsl:text>NUMBER_FORMAT_FOR_</xsl:text>
            <xsl:call-template name="getBindingName"/>
        </xsl:attribute>
        <xsl:attribute name="value">
            <xsl:text></xsl:text>
        </xsl:attribute>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;# } </xsl:text>
    <xsl:text disable-output-escaping="yes">} catch(err) { } #&gt;</xsl:text-->

    <!-- xsl:variable name="controlId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="usersRolesType">
      <xsl:value-of select="@tw:usersRoles"/>
    </xsl:variable>
    <xsl:variable name="filter">
      <xsl:value-of select="@tw:filter"/>
    </xsl:variable>

    <xsl:text disable-output-escaping="yes">
    &lt;script&gt;
    registerForUsersRolesAutoCompletion('</xsl:text>
    <xsl:value-of select="$controlId" />
    <xsl:text disable-output-escaping="yes">', '</xsl:text>
    <xsl:value-of select="$usersRolesType" />
    <xsl:text disable-output-escaping="yes">', '</xsl:text>
    <xsl:value-of select="$filter" />
    <xsl:text disable-output-escaping="yes">'</xsl:text>
    <xsl:text disable-output-escaping="yes">);
    &lt;/script&gt;
    </xsl:text>
  </xsl:template -->

  <xsl:template match="tw:InputText">
    <xsl:variable name="controlId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="@tw:lines and @tw:lines &gt; 1">
        <xsl:call-template name="htmlTextArea"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="htmlInputTypeText"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:variable name="bindingVar">
      <xsl:call-template name="getBindingVariable"/>
    </xsl:variable>
    <xsl:text disable-output-escaping="yes">&lt;# try { if (tw.system.isBaseTypeDate('</xsl:text>
    <xsl:value-of select="$bindingVar"/>
    <xsl:text>') || tw.system.isBaseTypeTime('</xsl:text>
    <xsl:value-of select="$bindingVar"/>
    <xsl:text disable-output-escaping="yes">')) { #&gt;</xsl:text>
    <xsl:element name="INPUT">
        <xsl:attribute name="type">hidden</xsl:attribute>
        <xsl:attribute name="id"><xsl:value-of select="$controlId"/><xsl:text disable-output-escaping="yes">_Format</xsl:text></xsl:attribute>
        <xsl:attribute name="name">
            <xsl:text>DATE_FORMAT_FOR_</xsl:text>
            <xsl:call-template name="getBindingName"/>
        </xsl:attribute>
        <xsl:attribute name="value">
            <xsl:text disable-output-escaping="yes">&lt;# try { buff.append(tw.system.dateFormatForVariable('</xsl:text>
            <xsl:value-of select="$bindingVar"/>
            <xsl:text disable-output-escaping="yes">')); } catch(err) { } #&gt;</xsl:text>
        </xsl:attribute>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;# } else if (tw.system.isBaseTypeNumber('</xsl:text>
    <xsl:value-of select="$bindingVar"/>
    <xsl:text disable-output-escaping="yes">')) { #&gt;</xsl:text>
    <xsl:element name="INPUT">
        <xsl:attribute name="type">hidden</xsl:attribute>
        <xsl:attribute name="id"><xsl:value-of select="$controlId"/><xsl:text disable-output-escaping="yes">_Format</xsl:text></xsl:attribute>
        <xsl:attribute name="name">
            <xsl:text>NUMBER_FORMAT_FOR_</xsl:text>
            <xsl:call-template name="getBindingName"/>
        </xsl:attribute>
        <xsl:attribute name="value">
            <xsl:text></xsl:text>
        </xsl:attribute>
    </xsl:element>
    <xsl:text disable-output-escaping="yes">&lt;# } </xsl:text>
    <xsl:text disable-output-escaping="yes">} catch(err) { } #&gt;</xsl:text>

  </xsl:template>

  <xsl:template name="addAutocompleteDataStore">
    <xsl:variable name="elementId">
       <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
       </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="serviceId"><xsl:value-of select="@tw:autoCompleteServiceRef"/></xsl:variable>
    <xsl:variable name="snapshotId"><xsl:text>&lt;# try{buff.append(tw.system.model.processAppSnapshot.id)} catch(err) { } #&gt;</xsl:text></xsl:variable>
    <xsl:element name="div">
      <xsl:attribute name="dojoType">lombardi.data.XmlStringStore</xsl:attribute>
      <xsl:attribute name="jsId"><xsl:value-of select="$elementId"/><xsl:text>Store</xsl:text></xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="$elementId"/><xsl:text>Store</xsl:text></xsl:attribute>
      <xsl:attribute name="rootItem"><xsl:text>item</xsl:text></xsl:attribute>
      <xsl:attribute name="url">
			<xsl:choose>
				<xsl:when test="$previewMode != &apos;true&apos;"><xsl:text>&lt;# try { if (com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null) != null) { buff.append(com.ibm.bpm.endpoint.EndpointServiceFactory.getInstance().getEndpointUrl(com.ibm.bpm.endpoint.EndpointServiceScenario.PROCESS_PORTAL_SUPPORT, com.ibm.bpm.endpoint.EndpointServiceScenario.RELATIVE, com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT, null)); } else {try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err2) { } }  } catch(err1) { try { if(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService() != null) buff.append(com.ibm.bpm.crconfig.BPMContextRootConfigServiceFactory.getInstance().getContextRootConfigService().getContextRoot(com.ibm.bpm.crconfig.BPMContextRootConfigService.BPM_WARNAME_PROCESSPORTALSUPPORT));  } catch(err) { } }#&gt;</xsl:text></xsl:when>
				<xsl:otherwise><xsl:text>&lt;# try { if(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix() != null) buff.append(com.lombardisoftware.core.config.TWConfiguration.getInstance().getAuthoringEnvironment().getPortalPrefix());  } catch(err) { } #&gt;</xsl:text></xsl:otherwise>
			</xsl:choose>
          <xsl:text disable-output-escaping="yes">/jsp/callService.do?serviceRef=</xsl:text>
          <xsl:value-of select="$serviceId"/>
          <xsl:text disable-output-escaping="yes">&amp;snapshotId=</xsl:text>
          <xsl:value-of select="$snapshotId"/>
          <xsl:text disable-output-escaping="yes">&amp;input=</xsl:text>
          <xsl:value-of select="$elementId"/>
      </xsl:attribute>
    </xsl:element>

  </xsl:template>

  <xsl:template name="htmlInputTypeText">

    <!-- This was missing for some reason -->
    <xsl:if test="@tw:cssStyle and @tw:cssStyle != ''">
       <xsl:attribute name="style">
           <xsl:value-of select="@tw:cssStyle"/>
       </xsl:attribute>
    </xsl:if>

   <!-- Add symbol display to beginning of input field -->
    <xsl:element name="div">
    <xsl:attribute name="class">
      <xsl:text>inputField</xsl:text>
    </xsl:attribute>

    <xsl:element name="div">
      <xsl:attribute name="class">
          <xsl:text>symbolPre</xsl:text>
      </xsl:attribute>
      <xsl:if test="@tw:symbolPre or ../tw:Override[@tw:key='symbolPre']">
      <xsl:value-of select="../tw:Override[@tw:key='symbolPre']/@tw:value"/>
      </xsl:if>
    </xsl:element>

    <!-- Create Dojo storage for autocompletion -->
    <xsl:if test="@tw:autoCompleteServiceRef and string-length(@tw:autoCompleteServiceRef) &gt; 0">
      <xsl:call-template name="addAutocompleteDataStore"/>
    </xsl:if>

    <xsl:element name="INPUT">

        <!-- Set autocompletion Dojo type and bind to storage -->
        <xsl:if test="@tw:autoCompleteServiceRef and string-length(@tw:autoCompleteServiceRef) &gt; 0">
          <xsl:attribute name="dojoType"><xsl:text>dijit.form.ComboBox</xsl:text></xsl:attribute>
          <xsl:attribute name="searchAttr"><xsl:text>text()</xsl:text></xsl:attribute>
          <xsl:attribute name="store">
              <xsl:call-template name="getId">
                <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
              </xsl:call-template>
              <xsl:text>Store</xsl:text>
          </xsl:attribute>
        </xsl:if>
		
		<xsl:if test="@tw:autoCompleteServiceRef and string-length(@tw:autoCompleteServiceRef) &gt; 0">
			<xsl:attribute name="zvalmsg">
				<xsl:text disable-output-escaping="yes">&lt;# try { buff.append(tw.system.validationMessageFor(&apos;</xsl:text>
				<xsl:call-template name="getBindingVariable"/>
				<xsl:text disable-output-escaping="yes">&apos;));  } catch(err) { } #&gt;</xsl:text>
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@tw:autoCompleteServiceRef and string-length(@tw:autoCompleteServiceRef) &gt; 0">
			<xsl:attribute name="zvalidate">
				<xsl:text>true</xsl:text>
			</xsl:attribute>
		</xsl:if>

      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>inputText</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twInputText</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
	  </xsl:attribute>
   <xsl:if test="@tw:cssStyle and @tw:cssStyle != ''">
  <xsl:attribute name="style">
   <xsl:value-of select="@tw:cssStyle"/>
  </xsl:attribute>
   </xsl:if>
      <xsl:attribute name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="getBindingName"/>
      </xsl:attribute>
      <xsl:attribute name="type">
        <xsl:text>text</xsl:text>
      </xsl:attribute>
      <xsl:variable name="bindingValue">
        <xsl:call-template name="getBindingValue"/>
      </xsl:variable>
      <xsl:attribute name="value">
        <xsl:value-of select="$bindingValue"/>
      </xsl:attribute>
      <xsl:attribute name="initialValue">
        <xsl:value-of select="$bindingValue"/>
      </xsl:attribute>
        <xsl:if test="tw:Customization[@tw:maxlength] and tw:Customization[@tw:maxlength != '']">
            <xsl:attribute name="maxlength">
            <xsl:value-of select="tw:Customization/@tw:maxlength"/>
            </xsl:attribute>
        </xsl:if>
      <!-- tw:Attribute - apply an HTML attribute to an input field -->
      <xsl:apply-templates select="tw:Attribute"/>

     <!-- precision -->
      <xsl:if test="@tw:precision or ../tw:Override[@tw:key='precision']">
      <xsl:attribute name="onBlur">
        <xsl:text>return preciseValue(this,</xsl:text>
        <xsl:value-of select="../tw:Override[@tw:key='precision']/@tw:value" />
        <xsl:text>);</xsl:text>
      </xsl:attribute>
     </xsl:if>

     <xsl:if test="$previewMode != 'true'">
       <xsl:variable name="bindingVar">
         <xsl:call-template name="getBindingVariable"/>
       </xsl:variable>

       <xsl:if test="@tw:format and @tw:format != ''">
         <xsl:attribute name="formatt">
           <xsl:value-of select="@tw:format" />
         </xsl:attribute>
      </xsl:if>

      <!-- This is helper attribute to work with locales for non-formatted fields -->
      <xsl:attribute name="locale">
        <xsl:text disable-output-escaping="yes">&lt;#= tw.system.user_locale #&gt;</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="isNumeric">
        <xsl:text disable-output-escaping="yes">&lt;#= tw.system.isBaseTypeNumber('</xsl:text>
        <xsl:value-of select="$bindingVar"/>
        <xsl:text disable-output-escaping="yes">') #&gt;</xsl:text>
      </xsl:attribute>

      <xsl:if test="$bindingVar != ''">
        <xsl:attribute name="baseType">
          <xsl:text disable-output-escaping="yes">&lt;#= tw.system.getBaseType('</xsl:text>
          <xsl:value-of select="$bindingVar"/>
          <xsl:text disable-output-escaping="yes">') #&gt;</xsl:text>
        </xsl:attribute>
      </xsl:if>

     </xsl:if>

      <xsl:call-template name="eventHandlers"/>
      <!--xsl:attribute name="onClick">
         <xsl:text>event.cancelBubble=true;</xsl:text>
      </xsl:attribute-->
    </xsl:element>
    <xsl:call-template name="dataChanged">
      <xsl:with-param name="dataChangedEvent">
        <xsl:text>onChange</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="elementId">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
          <xsl:with-param name="stripLastIndex" select="'1'"/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>
    <xsl:if test="@tw:binding and string-length(@tw:binding) &gt; 0">
      <xsl:call-template name="inputTextValidation"/>
    </xsl:if>
    <!-- Add symbol display at the end of an input field -->
    <xsl:element name="div">
        <xsl:attribute name="class">
            <xsl:text>symbolPost</xsl:text>
        </xsl:attribute>
        <xsl:if test="@tw:symbolPost or ../tw:Override[@tw:key='symbolPost']">
        <xsl:value-of select="../tw:Override[@tw:key='symbolPost']/@tw:value"/>
        </xsl:if>
    </xsl:element>

    </xsl:element>
  </xsl:template>

  <xsl:template name="htmlTextArea">
    <xsl:element name="TEXTAREA">
      <xsl:attribute name="rows">
          <xsl:value-of select="@tw:lines"/>
      </xsl:attribute>
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>inputText</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twTextArea</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
   <xsl:if test="@tw:cssStyle and @tw:cssStyle != ''">
      <xsl:attribute name="style">
      <xsl:value-of select="@tw:cssStyle"/>
    </xsl:attribute>
   </xsl:if>
      <xsl:attribute name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="getBindingName"/>
      </xsl:attribute>
      <xsl:attribute name="locale">
        <xsl:text disable-output-escaping="yes">&lt;#= tw.system.user_locale #&gt;</xsl:text>
      </xsl:attribute>
		<xsl:if test="tw:Customization[@tw:maxlength] and tw:Customization[@tw:maxlength != '']">
            <xsl:attribute name="maxlength">
            <xsl:value-of select="tw:Customization/@tw:maxlength"/>
            </xsl:attribute>
			<xsl:attribute name="onkeyup">
            <xsl:text>return isTextAreaMaxLength(this);</xsl:text>
            </xsl:attribute>
        </xsl:if>
      <!-- tw:Attribute - apply an HTML attribute to an input field -->
      <xsl:apply-templates select="tw:Attribute"/>
      <xsl:call-template name="eventHandlers"/>
      <xsl:apply-templates select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Button&apos;][1]/tw:EventHandler"/>
      <xsl:call-template name="getBindingValue"/>
    </xsl:element>
    <xsl:call-template name="dataChanged">
      <xsl:with-param name="elementId"><xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template></xsl:with-param>
    </xsl:call-template>
    <xsl:if test="@tw:binding and string-length(@tw:binding) &gt; 0">
      <xsl:call-template name="inputTextValidation"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="inputTextValidation">
    <xsl:element name="SCRIPT">
      <xsl:attribute name="type">
        <xsl:text>text/javascript</xsl:text>
      </xsl:attribute>
      <xsl:text>defFld(document.getElementById(&apos;</xsl:text>
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
      <xsl:text>&apos;), &apos;</xsl:text><xsl:value-of select="tw:Label"/><xsl:text>&apos;, &quot;</xsl:text>
      <xsl:text disable-output-escaping="yes">&lt;# try { buff.append(tw.system.validationMessageFor(&apos;</xsl:text>
      <xsl:call-template name="getBindingVariable"/>
      <xsl:text disable-output-escaping="yes">&apos;));  } catch(err) { } #&gt;&quot;);</xsl:text>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:BooleanChoice">
    <!-- Need to add other possiblities here -->
    <xsl:choose>
      <xsl:when test="tw:VisualCheckboxes">
        <xsl:call-template name="htmlBooleanCheckboxes"/>
      </xsl:when>
      <xsl:when test="tw:VisualDropDown">
        <xsl:call-template name="htmlBooleanDropDown"/>
      </xsl:when>
      <xsl:when test="tw:VisualRadioButtons">
        <xsl:call-template name="htmlBooleanRadioButtons"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">
          <xsl:text>Found tw:BooleanChoice with unsupported visualization</xsl:text>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>
    <!--<xsl:call-template name="javaScriptSingleSelection"/>-->
  </xsl:template>

  <!-- put the javascript that sets the selection at the bottom of the page so all controls are available -->
  <xsl:template match="tw:BooleanChoice" mode="serversidescript">
    <xsl:call-template name="javaScriptSingleSelection"/>
  </xsl:template>

  <xsl:template name="htmlBooleanCheckboxes">
    <xsl:variable name="bindingName">
      <xsl:text>BOOLEAN_FOR_</xsl:text>
      <xsl:call-template name="getBindingName"/>
    </xsl:variable>
    <xsl:variable name="controlClass">
      <xsl:call-template name="getControlClass">
        <xsl:with-param name="default">
          <xsl:text>booleanChoice</xsl:text>
        </xsl:with-param>
          <xsl:with-param name="override">
              <xsl:text>twBooleanCheckbox</xsl:text>
          </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="commonId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="hiddenId" select="concat($commonId, '_hidden')"/>
    <xsl:variable name="checkboxId" select="concat($commonId, '_checkbox')"/>
    <xsl:variable name="valueLabelId" select="concat($commonId, '_valuelabel')"/>
    <xsl:variable name="visibilityDependants" select="//tw:VisibilityCondition[@tw:controlId = $commonId]"/>
    <xsl:variable name="divId" select="concat($commonId, '_div')"/>
    <div class="{$controlClass}" id="{$commonId}">
      <!-- Hidden field so that the server is notified of the presence of this multi select -->
      <input id="{$hiddenId}" type="hidden" name="{$bindingName}" value="false"/>
      <!-- visual checkbox -->
      <xsl:call-template name="htmlCheckbox">
        <xsl:with-param name="bindingName" select="$bindingName"/>
        <xsl:with-param name="selectValue">
          <xsl:text>true</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="display" select="tw:TrueChoice"/>
        <xsl:with-param name="id" select="$checkboxId"/>
      </xsl:call-template>
      <span id="{$valueLabelId}"><xsl:value-of select="tw:FalseChoice"/></span>
      <!-- /visual checkbox -->
      <!-- event handler -->
      <script type="text/javascript">
        var var<xsl:value-of select="$commonId"/> = document.getElementById('<xsl:value-of select="$hiddenId"/>');
        var checked = '<xsl:call-template name="getBindingValue"/>';
        if((checked == 'true') || (checked == 1)) {
          document.getElementById('<xsl:value-of select="$checkboxId"/>').setAttribute("checked","true");
          document.getElementById('<xsl:value-of select="$hiddenId"/>').value="true";
          document.getElementById('<xsl:value-of select="$valueLabelId"/>').innerHTML=<xsl:text>"</xsl:text><xsl:value-of select="tw:TrueChoice"/><xsl:text>";</xsl:text>
        } else{
          document.getElementById('<xsl:value-of select="$checkboxId"/>').setAttribute("checked",null);
          document.getElementById('<xsl:value-of select="$checkboxId"/>').removeAttribute("checked");
          document.getElementById('<xsl:value-of select="$hiddenId"/>').value="false";
          document.getElementById('<xsl:value-of select="$valueLabelId"/>').innerHTML=<xsl:text>"</xsl:text><xsl:value-of select="tw:FalseChoice"/><xsl:text>";</xsl:text>
        }
        <xsl:if test="$visibilityDependants">
        <!-- Set the visibility function while we have access to $checkboxId -->
        document.getElementById('<xsl:value-of select="$checkboxId"/>').visibilityFunction = 'fcnDV<xsl:value-of select="$commonId"/>';
        </xsl:if>
        addEventHandler('<xsl:value-of select="$checkboxId"/>', "onclick", function() {
          if (var<xsl:value-of select="$commonId"/>.value == "#IGNORE#") {
            // This is to account for the case that the default visibility is disable but it is also depends on other value to enabling the visibility again
            var<xsl:value-of select="$commonId"/>.value = !!(document.getElementById('<xsl:value-of select="$checkboxId"/>').checked);
          } else {
            var<xsl:value-of select="$commonId"/>.value = !(var<xsl:value-of select="$commonId"/>.value == "true");
          }
          
          if ((var<xsl:value-of select="$commonId"/>.value == 'true') || (var<xsl:value-of select="$commonId"/>.value == 1)) {
          	document.getElementById('<xsl:value-of select="$valueLabelId"/>').innerHTML=<xsl:text>"</xsl:text><xsl:value-of select="tw:TrueChoice"/><xsl:text>";</xsl:text>
          } else {
          	document.getElementById('<xsl:value-of select="$valueLabelId"/>').innerHTML=<xsl:text>"</xsl:text><xsl:value-of select="tw:FalseChoice"/><xsl:text>";</xsl:text>
          }
          
          clearErrorMsg();
          try {
            <!-- This function is only generated when a control's visibility depends on this checkbox -->
            <xsl:if test="$visibilityDependants">
            fcnDV<xsl:value-of select="$commonId"/>(this, true);
            </xsl:if>
          } catch(err) { }
        });
      </script>

      <!-- /event handler -->
    </div>
  </xsl:template>

  <xsl:template name="htmlCheckbox">
    <xsl:param name="bindingName"/>
    <xsl:param name="selectValue"/>
    <xsl:param name="display"/>
    <xsl:param name="id"/>
    <xsl:text>
</xsl:text>
    <input type="checkbox" id="{$id}" checked='checked'>
    <xsl:call-template name="eventHandlers"/>
    <!-- tw:Attribute - apply an HTML attribute to an input field -->
    <xsl:apply-templates select="tw:Attribute"/>
    <xsl:call-template name="styleOverride"/>
    </input>
    <xsl:call-template name="dataChanged">
      <xsl:with-param name="elementId"><xsl:value-of select="$id"/></xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="htmlBooleanDropDown">
    <xsl:text>
</xsl:text>
    <xsl:element name="SELECT">
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>booleanChoice</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twBooleanDropDown</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <!-- Disable if in table
      <xsl:if test="count(ancestor::tw:Cell)&gt;0">
          <xsl:attribute name="disabled">true</xsl:attribute>
      </xsl:if>
      -->
      <xsl:attribute name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>BOOLEAN_FOR_</xsl:text>
        <xsl:call-template name="getBindingName"/>
      </xsl:attribute>
      <xsl:attribute name="size">
        <xsl:text>1</xsl:text>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <!-- tw:Attribute - apply an HTML attribute to an input field -->
      <xsl:apply-templates select="tw:Attribute"/>
      <xsl:call-template name="eventHandlers"/>
      <xsl:choose>
        <xsl:when test="tw:TrueChoice">
          <xsl:call-template name="htmlOption">
            <xsl:with-param name="selectValue">
              <xsl:text>true</xsl:text>
            </xsl:with-param>
            <xsl:with-param name="display" select="tw:TrueChoice"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="htmlOption">
            <xsl:with-param name="selectValue">
              <xsl:text>true</xsl:text>
            </xsl:with-param>
            <xsl:with-param name="display">
              <xsl:text>TRUE</xsl:text>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:choose>
        <xsl:when test="tw:FalseChoice">
          <xsl:call-template name="htmlOption">
            <xsl:with-param name="selectValue">
              <xsl:text>false</xsl:text>
            </xsl:with-param>
            <xsl:with-param name="display" select="tw:FalseChoice"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="htmlOption">
            <xsl:with-param name="selectValue">
              <xsl:text>false</xsl:text>
            </xsl:with-param>
            <xsl:with-param name="display">
              <xsl:text>FALSE</xsl:text>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
    <xsl:call-template name="dataChanged">
    	<xsl:with-param name="elementId"><xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template></xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="htmlOption">
    <xsl:param name="selectValue"/>
    <xsl:param name="display"/>
    <xsl:text>
</xsl:text>
    <xsl:element name="OPTION">
      <xsl:attribute name="value">
        <xsl:value-of select="$selectValue"/>
      </xsl:attribute>
      <xsl:call-template name="convertText">
        <xsl:with-param name="s" select="$display"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

  <xsl:template name="htmlBooleanRadioButtons">
    <xsl:variable name="bindingName">
      <xsl:text>BOOLEAN_FOR_</xsl:text>
      <xsl:call-template name="getBindingName"/>
    </xsl:variable>
    <xsl:variable name="id">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="controlClass">
      <xsl:call-template name="getControlClass">
        <xsl:with-param name="default">
          <xsl:text>booleanChoice</xsl:text>
        </xsl:with-param>
          <xsl:with-param name="override">
              <xsl:text>twBooleanRadio</xsl:text>
          </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <!-- wrapper element -->
    <div class="{$controlClass}" id="{$id}">
      <!-- content -->
      <xsl:call-template name="htmlBooleanRadioButtonElements">
        <xsl:with-param name="bindingName" select="$bindingName"/>
        <xsl:with-param name="commonId" select="$id"/>
      </xsl:call-template>
      <!-- /content -->
    </div>
    <!-- /wrapper element -->
  </xsl:template>

  <xsl:template name="htmlBooleanRadioButtonElements">
    <xsl:param name="bindingName"/>
    <xsl:param name="commonId"/>
    <xsl:variable name="trueChoiceDisplay">
      <xsl:choose>
        <xsl:when test="string-length(tw:TrueChoice/text()) &gt; 0">
          <xsl:value-of select="tw:TrueChoice"/>
        </xsl:when>
        <xsl:otherwise><xsl:text>TRUE</xsl:text></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="falseChoiceDisplay">
      <xsl:choose>
        <xsl:when test="string-length(tw:FalseChoice/text()) &gt; 0">
          <xsl:value-of select="tw:FalseChoice"/>
        </xsl:when>
        <xsl:otherwise><xsl:text>FALSE</xsl:text></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
 <xsl:variable name="hiddenId" select="concat($commonId, '_hidden')"/>
 <xsl:variable name="trueId" select="concat($commonId, '_true')"/>
 <xsl:variable name="falseId" select="concat($commonId, '_false')"/>
 <xsl:variable name="visibilityDependants" select="//tw:VisibilityCondition[@tw:controlId = $commonId]"/>
    <!-- real control which submits data to server -->
    <input type="hidden" name="{$bindingName}" id="{$hiddenId}" value=""/>
    <!-- /real control which submits data to server -->
    <!-- generating true/false options -->
    <xsl:call-template name="htmlRadioButton">
      <xsl:with-param name="bindingName" select="$commonId"/>
      <xsl:with-param name="selectValue">
        <xsl:text>true</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="display" select="$trueChoiceDisplay"/>
      <xsl:with-param name="id" select="$trueId"/>
    </xsl:call-template>
    <xsl:call-template name="htmlRadioButton">
      <xsl:with-param name="bindingName" select="$commonId"/>
      <xsl:with-param name="selectValue">
        <xsl:text>false</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="display" select="$falseChoiceDisplay"/>
      <xsl:with-param name="id" select="$falseId"/>
    </xsl:call-template>
    <!-- /generating true/false options -->
    <!-- script for event handler -->
    <script type="text/javascript">
        addOnPageLoadFunction(function(){
          var var<xsl:value-of select="$commonId"/> = document.getElementById('<xsl:value-of select="$hiddenId"/>');
          addEventHandler('<xsl:value-of select="$trueId"/>', 'onclick',function() {
              var<xsl:value-of select="$commonId"/>.value = "true";
              clearErrorMsg();
              <xsl:if test="$visibilityDependants">
              try {
                <!-- This function is only generated when a control's visibility depends on this radio button -->
                fcnDV<xsl:value-of select="$commonId"/>(this, true);
              } catch(err) { }
              </xsl:if>
          });

          addEventHandler('<xsl:value-of select="$falseId"/>', 'onclick', function() {
              var<xsl:value-of select="$commonId"/>.value = "false";
              clearErrorMsg();
              <xsl:if test="$visibilityDependants">
              try {
                <!-- This function is only generated when a control's visibility depends on this radio button -->
                fcnDV<xsl:value-of select="$commonId"/>(this, true);
              } catch(err) { }
              </xsl:if>
          });

          if (document.getElementById('<xsl:value-of select="$trueId"/>').checked) {
              var<xsl:value-of select="$commonId"/>.value = "true";
          } else if (document.getElementById('<xsl:value-of select="$falseId"/>').checked) {
              var<xsl:value-of select="$commonId"/>.value = "false";
          }
        })
 </script>
  </xsl:template>

  <xsl:template name="htmlRadioButton">
    <xsl:param name="bindingName"/>
    <xsl:param name="selectValue"/>
    <xsl:param name="display"/>
    <xsl:param name="id"/>
    <xsl:text>
</xsl:text>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>radio</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name"><xsl:value-of select="$bindingName"/></xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
      <xsl:attribute name="controlId">
        <xsl:value-of select="$bindingName"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:value-of select="$selectValue"/>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:call-template name="eventHandlers">
        <xsl:with-param name="nodes" select="ancestor-or-self::node()[local-name() = 'Control'][1]/tw:EventHandler"/>
      </xsl:call-template>
    </xsl:element>
    <xsl:element name="LABEL">
      <xsl:attribute name="class"><xsl:value-of select="'radio_label'"/></xsl:attribute>
      <xsl:attribute name="for"><xsl:value-of select="$id"/></xsl:attribute>
      <xsl:call-template name="eventHandlers">
        <xsl:with-param name="events" select="'onClick,onDblClick,onMouseDown,onMouseUp,onMouseOver,onMouseOut'"/>
        <xsl:with-param name="nodes" select="ancestor-or-self::node()[local-name() = 'Control'][1]/tw:EventHandler"/>
      </xsl:call-template>
      <xsl:call-template name="convertText">
        <xsl:with-param name="s" select="$display"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:SingleSelect">
    <!-- Need to add other possiblities here -->
    <xsl:choose>
      <xsl:when test="tw:VisualDropDown">
        <xsl:call-template name="htmlSingleSelect"/>
      </xsl:when>
      <xsl:when test="tw:VisualList">
        <xsl:call-template name="htmlSingleSelect">
          <xsl:with-param name="size" select="tw:VisualList/@tw:size"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="tw:VisualRadioButtons">
        <xsl:call-template name="htmlSingleRadioButtons">
            <xsl:with-param name="orientation" select="tw:VisualRadioButtons/@tw:orientation"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">
          <xsl:text>Found tw:SingleSelect with unsupported visualization</xsl:text>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:if test="tw:ContentDependency and tw:BasedOn[@tw:type = 'Ajax Service']">
        <xsl:variable name="id">
            <xsl:call-template name="getId">
            <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:element name="SCRIPT">
            <xsl:attribute name="type">
                <xsl:text>text/javascript</xsl:text>
            </xsl:attribute>
            <xsl:text> 
            addOnPageLoadFunction( function(){
                 var serviceRef="</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:srcServiceRef"/><xsl:text>";
                 var controlId="</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:controlId"/><xsl:text>";
                 var selfId = "</xsl:text><xsl:value-of select="$id"/><xsl:text>";
                 var element = document.getElementById(controlId);
                 
                 function handleOnchange(){
                    tw.coach.refreshDependentElementById("</xsl:text><xsl:value-of select="$id"/><xsl:text>","</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:srcServiceRef"/><xsl:text>","</xsl:text><xsl:value-of select="tw:ContentDependency/@tw:controlId"/><xsl:text>","</xsl:text><xsl:call-template name="getBindingValue"></xsl:call-template><xsl:text>","&lt;# try{ buff.append(tw.system.model.processAppSnapshot.id)} catch(err) { } #&gt;");
                 }
                 
                 if(element.tagName == "INPUT"){
                    element.refreshDependent = handleOnchange;
                    registerDeferredOnInputChange(element);
                 } else {
                    addEventHandler(element, "onchange", handleOnchange);
                 }
                 
                 handleOnchange();
            });
             </xsl:text>
        </xsl:element>
    </xsl:if>
    <!--<xsl:call-template name="javaScriptSingleSelection"/>-->
  </xsl:template>

  <!-- put the javascript that sets the selection at the bottom of the page so all controls are available -->
  <xsl:template match="tw:SingleSelect" mode="serversidescript">
    <xsl:call-template name="javaScriptSingleSelection"/>
  </xsl:template>

  <!-- put server side script that create the report  -->
  <xsl:template match="tw:Report">
      <xsl:choose>
          <xsl:when test="$previewMode = &apos;true&apos;">
              <xsl:element name="IMG">
                  <xsl:attribute name="src">
                      <xsl:text>images/report_32x32.gif</xsl:text>
                  </xsl:attribute>
                  <xsl:attribute name="border">
                      <xsl:text>0</xsl:text>
                  </xsl:attribute>
              </xsl:element>
              <xsl:value-of select="@tw:name"/>
          </xsl:when>
          <xsl:when test="not(@tw:name) or string-length(@tw:name) = 0">
             <xsl:call-template name="warningMessage">
                <xsl:with-param name="text">Cannot preview Report, it has not been set.</xsl:with-param>
             </xsl:call-template>
          </xsl:when>
          <xsl:when test="not(tw:DisplayPage) or string-length(tw:DisplayPage) = 0">
             <xsl:call-template name="warningMessage">
                <xsl:with-param name="text">Cannot preview Report, no page has been set.</xsl:with-param>
             </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <!-- Get the Report by Report Name -->
            <xsl:text disable-output-escaping="yes">
              &lt;# 
                    var report = null;
                    try {
                      report = TWReport.getByReference("</xsl:text><xsl:value-of select="@tw:reference"/><xsl:text disable-output-escaping="yes">");
                    }catch(exception) {              			
              #&gt;              	
            </xsl:text>
            <xsl:call-template name="warningMessage">
                <xsl:with-param name="text">
                    Cannot preview Report Section, the Report "<xsl:value-of select="@tw:name"/>" not found.
                </xsl:with-param>
            </xsl:call-template>
            <xsl:text disable-output-escaping="yes">
              &lt;# }  #&gt;
            </xsl:text>

            <!-- Set the Report filter -->
            <xsl:text disable-output-escaping="yes">
              &lt;# if(report != null) { #&gt;
            </xsl:text>
            <xsl:for-each select="tw:Filter">
                <xsl:text disable-output-escaping="yes">
                  &lt;#                    
                  report.setFilterValue("</xsl:text><xsl:value-of select="@tw:name"/><xsl:text disable-output-escaping="yes">", </xsl:text><xsl:value-of select="node()"/><xsl:text disable-output-escaping="yes">);
                  #&gt;
                </xsl:text>
            </xsl:for-each>

            <!-- Display Report Page -->
            <xsl:text disable-output-escaping="yes">
              &lt;#     try {   #&gt;
            </xsl:text>
            <xsl:text disable-output-escaping="yes">
              &lt;#= report.displayPage("</xsl:text><xsl:value-of select="tw:DisplayPage"/><xsl:text disable-output-escaping="yes">") #&gt;
            </xsl:text>            
            <xsl:text disable-output-escaping="yes">
              &lt;# } catch(exception) { #&gt;
            </xsl:text>
            <xsl:call-template name="warningMessage">
                <xsl:with-param name="text">
                    Cannot preview Report, the page "<xsl:value-of select="tw:DisplayPage"/>" not found.
                </xsl:with-param>
            </xsl:call-template>
            <xsl:text disable-output-escaping="yes">
              &lt;# } } #&gt;              	
            </xsl:text>
         </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="StripPointyHashes">
    <xsl:param name="hashedString"/>
    <xsl:param name="var"/>
    <xsl:choose>
      <xsl:when test="contains($hashedString,'&lt;#') and contains($hashedString,'#&gt;')">
        <xsl:variable name="afterFirstPointy" select="substring-after($hashedString,'#&gt;')"/>
        <xsl:variable name="uptoFirstPointy" select="substring-before($hashedString,'#&gt;')"/>
        <xsl:variable name="beforeFirstPointy" select="substring-before($uptoFirstPointy,'&lt;#')"/>
        <xsl:variable name="firstPointy" select="substring-after($uptoFirstPointy,'&lt;#')"/>
        <xsl:variable name="beforeNextPointy" select="substring-before($afterFirstPointy,'&lt;#')"/>
        <xsl:variable name="nextPointy" select="concat('&lt;#',substring-after($afterFirstPointy,'&lt;#'))"/>
        <xsl:value-of select="$var"/> += "<xsl:value-of select="$beforeFirstPointy"/>";
        <xsl:choose>
          <xsl:when test="starts-with($firstPointy,'=')">
            <xsl:value-of select="$var"/> += <xsl:value-of select="substring-after($firstPointy,'=')"/>;
            <xsl:choose>
              <xsl:when test="contains($afterFirstPointy,'&lt;#')">
                <xsl:value-of select="$var"/> += "<xsl:value-of select="$beforeNextPointy"/>";
                <xsl:call-template name="StripPointyHashes">
                  <xsl:with-param name="hashedString" select="$nextPointy"/>
                  <xsl:with-param name="var" select="$var"/>
                </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$var"/> += "<xsl:value-of select="$afterFirstPointy"/>";
              </xsl:otherwise>
           </xsl:choose>
         </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$firstPointy"/>
            <xsl:call-template name="StripPointyHashes">
              <xsl:with-param name="hashedString" select="$afterFirstPointy"/>
              <xsl:with-param name="var" select="$var"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$var"/> += "<xsl:value-of select="$hashedString"/>";
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="tw:DocumentAttachment">

    <script type="text/javascript">
      var controlId = getUniqueControlId();
      document.write("&lt;div class='twControl twDocAttachment'&gt;&lt;fieldset class='twDocAttachmentBorder' id=\"" + controlId + "\"&gt;");
    </script>

    <xsl:if test="tw:Label/@tw:visible='true'">
      <xsl:element name="legend">
        <xsl:attribute name="class">
         // for WLE-5167 
	 // somehow IE(6,7,8) is not able to handle attribute class  having value "twLabel" hence changed it to "tw_Label".
          <xsl:text>tw_Label</xsl:text>
        </xsl:attribute>
        <xsl:value-of select="tw:Label" disable-output-escaping="yes"/>
      </xsl:element>
    </xsl:if>

    <xsl:variable name="widgetId" select="../@tw:id"/>

    <xsl:variable name="viewerAssociated">
      <xsl:choose>
        <xsl:when test="//tw:DocumentViewer[@tw:associationType = 1 and @tw:associationControlId = $widgetId]">true</xsl:when>
        <xsl:otherwise>false</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <script type="text/javascript">
      var root = document.getElementById(controlId);
      if (root != null) {
        var control = new DocumentAttachmentControl(root, controlId);
        // save reference to this control in window object
        window[controlId] = control;

        // initialize instance and user properties
        &lt;# <!-- Begin Server javaScript -->
          var instanceId = -1;
          try {
            instanceId = Packages.teamworks.CoachDesignerUtils.getInstanceIdByTaskId(String(tw.system.task_id));
          } catch (err) {}
        <!-- Encrypt username and password -->
        var plainUser = "";
        <xsl:call-template name="StripPointyHashes">
          <xsl:with-param name="hashedString" select="normalize-space(tw:ConnectionProperties/tw:username)"/>
          <xsl:with-param name="var" select="'plainUser'"/>
        </xsl:call-template>
		try {
			var encryptedUser = Packages.teamworks.CoachDesignerUtils.encrypt(plainUser.replace(/\s+$/,""));
		} catch (err) {}
        var plainPassword = "";
        <xsl:choose>
          <xsl:when test="tw:ConnectionProperties/tw:passwordMode = '1'">
          
            <!-- plainPassword = <xsl:value-of select="tw:ConnectionProperties/tw:password"/>; -->
            <xsl:call-template name="StripPointyHashes">
          		<xsl:with-param name="hashedString" select="normalize-space(tw:ConnectionProperties/tw:password)"/>
          		<xsl:with-param name="var" select="'plainPassword'"/>
        	</xsl:call-template>            
			try {
				var encryptedPassword = Packages.teamworks.CoachDesignerUtils.encrypt(plainPassword.replace(/\s+$/,""));
			} catch (err) {}
          </xsl:when>
          <xsl:otherwise>
            plainPassword += "<xsl:value-of select="tw:ConnectionProperties/tw:freeTextPassword"/>";
			try {
				var encryptedPassword = Packages.teamworks.CoachDesignerUtils.encrypt(plainPassword);
			} catch (err) {}
          </xsl:otherwise>
        </xsl:choose>        
        #&gt; <!-- End Server JavaScript -->
        control.instanceId     = "&lt;#= String(instanceId)#&gt;";
        control.userId 	       = "&lt;#= String(tw.system.user_id)#&gt;";
        control.connectionType = <xsl:choose><xsl:when test="@tw:connectionType"><xsl:value-of select="@tw:connectionType" /></xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose>;

        // initialize display properties
        <xsl:choose>
        <xsl:when test="tw:DisplayProperties/@tw:associateWithProcessInstance = 'true'">
        	control.byInstance=true;
        </xsl:when>
         <xsl:otherwise>
        	control.byInstance=false;
         </xsl:otherwise>
         </xsl:choose>
        control.matchingMode   = "<xsl:value-of select="tw:DisplayProperties/@tw:matchingMode" />";
        control.matchingProperties = {
          <xsl:for-each select="tw:DisplayProperties/tw:Property">
            "<xsl:value-of select="@tw:name"/>" : "<xsl:value-of select="@tw:value"/>",
          </xsl:for-each>
          "" : ""
        };
        // initialize connection properties
        control.snapshotId         = "&lt;#= String(tw.system.model.processAppSnapshot.id) #&gt;";
        control.processId          = "<xsl:value-of select="tw:ConnectionProperties/tw:processId" />";
        control.connectionUrl      = "<xsl:value-of select="tw:ConnectionProperties/tw:url" />";
        control.connectionRepoName = "<xsl:value-of select="tw:ConnectionProperties/tw:repositoryID" />";
        control.connectionUsername = "&lt;#= String(encryptedUser)#&gt;";
        control.passwordMode       = <xsl:choose><xsl:when test="tw:ConnectionProperties/tw:passwordMode"><xsl:value-of select="tw:ConnectionProperties/tw:passwordMode"/></xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose>;
        control.freeTextPassword   = "&lt;#= String(encryptedPassword)#&gt;";
        control.password           = "&lt;#= String(encryptedPassword)#&gt;";

        // initialize remote display properties
        control.remoteMatchingMode = "<xsl:value-of select="tw:RemoteDisplayProperties/@tw:remoteMatchingMode" />";
        control.itemClassName      = "<xsl:value-of select="tw:RemoteDisplayProperties/@tw:itemClassName" />";
        control.maxResultSize      = <xsl:choose><xsl:when test="tw:RemoteDisplayProperties/@tw:maxResultSize"><xsl:value-of select="tw:RemoteDisplayProperties/@tw:maxResultSize"/></xsl:when><xsl:otherwise>25</xsl:otherwise></xsl:choose>;
        control.remoteMatchingProperties = {
          <xsl:for-each select="tw:RemoteDisplayProperties/tw:Property">
            "<xsl:value-of select="@tw:name"/>" : "<xsl:value-of select="translate(@tw:value,'&quot;','')"/>",
          </xsl:for-each>
          "" : ""
        };
        control.columnDisplayProperties = {
          <xsl:for-each select="tw:ColumnDisplayProperties/tw:Property">
            "<xsl:value-of select="@tw:name"/>" : "<xsl:value-of select="@tw:value"/>",
          </xsl:for-each>
          "" : ""
        };
        control.columnDisplayPropertyTypes = {
          <xsl:for-each select="tw:ColumnDisplayProperties/tw:Property">
            "<xsl:value-of select="@tw:name"/>" : "<xsl:value-of select="@tw:datatype"/>",
          </xsl:for-each>
          "" : ""
        };

        // initialize upload properties
        control.enableUpload = <xsl:value-of select="tw:UploadProperties/@tw:enabled" />;
        control.defaultName  = "<xsl:value-of select="tw:UploadProperties/@tw:defaultName" />";
        control.parentFolder = "<xsl:value-of select="tw:UploadProperties/@tw:parentFolder" />";
        control.userEditable =
        <xsl:choose>
        <xsl:when test="tw:UploadProperties/@tw:userEditable = 'true'">
        	 true;
        </xsl:when>
         <xsl:otherwise>
        	false;
         </xsl:otherwise>
         </xsl:choose>
        control.hideInPortal = 
        <xsl:choose>
        <xsl:when test="tw:UploadProperties/@tw:hideInPortal = 'true'">
        	 true;
        </xsl:when>
         <xsl:otherwise>
        	false;
         </xsl:otherwise>
         </xsl:choose>
        control.attachedProperties = {
          <xsl:for-each select="tw:UploadProperties/tw:Property">
            "<xsl:value-of select="@tw:name"/>" : "<xsl:value-of select="@tw:value"/>",
          </xsl:for-each>
          "" : ""
        };

        control.viewerAssociated = <xsl:value-of select="$viewerAssociated"/>;
        control.viewerFunction = function(url) {
          click<xsl:value-of select="../@tw:id"/>(url);
        }

        // construct GUI elements
        control.init();
        // load documents
        control.loadDocuments();
        control = null;
      } else {
        document.write("Error: root element cannot found");
      }
      // this function is called when form submitted and reponse was recieved
      if (window.callbackDispatcher == null) {
        window.callbackDispatcher = function() {
          refreshControls();
        }
      }
    </script>

    <script type="text/javascript">
      document.write("&lt;/fieldset&gt;&lt;/div&gt;");
    </script>
  </xsl:template>

  <xsl:template match="tw:DocumentViewer">

     <script type="text/javascript">
      // var controlId = getUniqueControlId();
	  var controlId = "<xsl:value-of select="../@tw:id"/>";
      document.write("&lt;div class='twControl twDocAttachment'&gt;&lt;fieldset class='twDocAttachmentBorder' id=\"" + controlId + "\"&gt;");
    </script>
      <xsl:if test="tw:Label/@tw:visible='true'">
          <xsl:element name="legend">
              <xsl:attribute name="class">
                  <xsl:text>twLabel</xsl:text>
              </xsl:attribute>
              <xsl:value-of select="tw:Label" disable-output-escaping="yes"/>
          </xsl:element>
      </xsl:if>
    <xsl:element name="div">
      <xsl:attribute name="class">twControl twDocViewer</xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="../@tw:id"/></xsl:attribute>
    </xsl:element>



       <script type="text/javascript">
      var controlId = "<xsl:value-of select="../@tw:id"/>";
      var root = document.getElementById(controlId);
      var control;
      if (root != null) {
        control = new DocumentViewerControl(root, controlId);
        // save reference to this control in window object
        window[controlId] = control;

        // initialize instance and user properties
        &lt;# <!-- Begin Server javaScript -->
          var instanceId = -1;
          try {
            instanceId = Packages.teamworks.CoachDesignerUtils.getInstanceIdByTaskId(String(tw.system.task_id));
          } catch (err) {}
          var portalPrefix = "";
          try {
            portalPrefix = Packages.com.lombardisoftware.client.delegate.ClientServicesDelegateFactory.getInstance().newInstance().getConfiguration("ENVIRONMENT_EVENT_MANAGER").getCommon().getPortalPrefix();
          } catch (err) {}
        <!-- Encrypt username and password -->
        var plainUser = "";
        <xsl:call-template name="StripPointyHashes">
          <xsl:with-param name="hashedString" select="normalize-space(tw:ConnectionProperties/tw:username)"/>
          <xsl:with-param name="var" select="'plainUser'"/>
        </xsl:call-template>
		try {
			var encryptedUser = Packages.teamworks.CoachDesignerUtils.encrypt(plainUser.replace(/\s+$/,""));
		} catch (err) {}
        var plainPassword = "";
        <xsl:choose>
          <xsl:when test="tw:ConnectionProperties/tw:passwordMode = '1'">
            <!-- plainPassword = <xsl:value-of select="tw:ConnectionProperties/tw:password"/>; -->
            <xsl:call-template name="StripPointyHashes">
          		<xsl:with-param name="hashedString" select="normalize-space(tw:ConnectionProperties/tw:password)"/>
          		<xsl:with-param name="var" select="'plainPassword'"/>
        	</xsl:call-template>
			try {
				var encryptedPassword = Packages.teamworks.CoachDesignerUtils.encrypt(plainPassword.replace(/\s+$/,""));
			} catch (err) {}
          </xsl:when>
          <xsl:otherwise>
            plainPassword += "<xsl:value-of select="tw:ConnectionProperties/tw:freeTextPassword"/>";
			try {
				var encryptedPassword = Packages.teamworks.CoachDesignerUtils.encrypt(plainPassword);
			} catch (err) {}
          </xsl:otherwise>
        </xsl:choose>        
        #&gt; <!-- End Server JavaScript -->
        control.instanceId = "&lt;#= String(instanceId)#&gt;";
        control.portalPrefix = "&lt;#= String(portalPrefix)#&gt;";
        control.userId = "&lt;#= String(tw.system.user_id)#&gt;";

        // association properties
        control.associationType = <xsl:value-of select="@tw:associationType"/>;
        control.associationControlId = "<xsl:value-of select="@tw:associationControlId"/>";

        // connection type
        control.connectionType = <xsl:value-of select="tw:ConnectionTypeProperties/@tw:value"/>;

        // local display properties
        control.byInstance = <xsl:value-of select="tw:DisplayProperties/@tw:associateWithProcessInstance"/>;
        control.matchingMode = "<xsl:value-of select="tw:DisplayProperties/@tw:matchingMode"/>";
        control.matchingProperties = {
          <xsl:for-each select="tw:DisplayProperties/tw:Property">
            "<xsl:value-of select="@tw:name"/>" : "<xsl:value-of select="@tw:value"/>",
          </xsl:for-each>
          "" : ""
        };

        // remote connection properties
        control.snapshotId         	= "&lt;#= String(tw.system.model.processAppSnapshot.id) #&gt;";
        control.processId          	= "<xsl:value-of select="tw:ConnectionProperties/tw:processId" />";
        control.connectionUrl 		= "<xsl:value-of select="tw:ConnectionProperties/tw:url"/>";
        control.connectionRepoName 	= "<xsl:value-of select="tw:ConnectionProperties/tw:repositoryID"/>";
        control.passwordMode 		= <xsl:value-of select="tw:ConnectionProperties/tw:passwordMode"/>;
        control.connectionUsername 	= encodeURIComponent("&lt;#= String(encryptedUser)#&gt;");
        control.freeTextPassword 	= encodeURIComponent("&lt;#= String(encryptedPassword)#&gt;");
        control.password 			= encodeURIComponent("&lt;#= String(encryptedPassword)#&gt;");

        // remote display properties
        control.remoteMatchingMode = "<xsl:value-of select="tw:ViewerRemoteDisplayProperties/@tw:remoteMatchingMode"/>";
        control.itemClassName = "<xsl:value-of select="tw:ViewerRemoteDisplayProperties/@tw:itemClassName"/>";
        control.remoteMatchingProperties = {
          <xsl:for-each select="tw:ViewerRemoteDisplayProperties/tw:Property">
            "<xsl:value-of select="@tw:name"/>" : "<xsl:value-of select="@tw:value"/>",
          </xsl:for-each>
          "" : ""
        };

        // URL display properties
        control.displayUrl = "<xsl:value-of select="tw:URLDisplayProperties/@tw:url"/>";

        // construct GUI elements
        control.init();
        // load document if necessary
        control.loadDocument();
        control = null;
      } else {
        document.write("Error: root element cannot found");
      }
    </script>

    <xsl:if test="@tw:associationType = 1 and @tw:associationControlId != '-none-'">
      <script type="text/javascript">
        function click<xsl:value-of select="@tw:associationControlId"/>(url) {
          window["<xsl:value-of select="../@tw:id"/>"].setUrl(url);
        }
      </script>
    </xsl:if>
     <script type="text/javascript">
      document.write("&lt;/fieldset&gt;&lt;/div&gt;");
    </script>
  </xsl:template>

  <xsl:template match="tw:MultipleSelect">
    <!-- Need to add other possiblities here -->
    <xsl:choose>
      <xsl:when test="tw:VisualList">
        <xsl:call-template name="htmlMultipleSelect">
          <xsl:with-param name="size" select="tw:VisualList/@tw:size"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="tw:VisualCheckboxes">
        <xsl:call-template name="htmlMultipleCheckboxes">
          <xsl:with-param name="orientation" select="tw:VisualCheckboxes/@tw:orientation"/>
          </xsl:call-template>
      </xsl:when>
      <xsl:when test="tw:VisualTwoLists">
        <xsl:call-template name="htmlTwoLists">
          <xsl:with-param name="size" select="tw:VisualTwoLists/@tw:size"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">
          <xsl:text>Found tw:MultipleSelect with unsupported visualization</xsl:text>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="htmlSingleSelect">
    <xsl:param name="size">
      <xsl:text>1</xsl:text>
    </xsl:param>
    <xsl:text>
</xsl:text>
    <xsl:variable name="elementId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="SELECT">
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>singleSelect</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twSingleSelect</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:value-of select="$elementId"/>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="getBindingName"/>
      </xsl:attribute>
      <xsl:attribute name="size">
        <xsl:value-of select="$size"/>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <!-- tw:Attribute - apply an HTML attribute to an input field -->
      <xsl:apply-templates select="tw:Attribute"/>
      <xsl:call-template name="eventHandlers"/>
      <xsl:apply-templates select="node()[local-name() = &apos;StaticOption&apos; or local-name() = &apos;DynamicOption&apos;]" mode="selectOption"/>
    </xsl:element>
    <xsl:call-template name="dataChanged">
      <xsl:with-param name="elementId" select="$elementId"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="javaScriptSingleSelection">
    <!-- The contents of the script below are generated on the server by evaluating the script inside the <# and #> -->
    <!-- This server side script outputs a call to selectValue if there is a selected value. So here we have XSL generating -->
    <!-- server side javascript that in turn generates client side javascript -->
    <xsl:variable name="bindingValue">
      <xsl:apply-templates select="." mode="calculateBindingValue"/>
    </xsl:variable>
    <xsl:if test="$bindingValue != &apos;&apos;">
      <xsl:element name="SCRIPT">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&lt;#  </xsl:text>
        <xsl:call-template name="openForLoop">
            <xsl:with-param name="bv" select="$bindingValue"/>
        </xsl:call-template>
        <xsl:text disable-output-escaping="yes">try {var selectedValue = </xsl:text>
        <xsl:call-template name="substituteIndexLetters">
          <xsl:with-param name="s" select="$bindingValue"/>
        </xsl:call-template>
        <xsl:text disable-output-escaping="yes">;  if (selectedValue != undefined) { var id = &quot;</xsl:text>
        <xsl:call-template name="getId">
          <xsl:with-param name="inPointyHash">1</xsl:with-param>
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
        <xsl:text disable-output-escaping="yes">&quot;; </xsl:text>
        <xsl:text disable-output-escaping="yes">buff.append(&apos;selectValue(document.getElementById(\&apos;&apos; + id + &apos;\&apos;), \&apos;&apos;);</xsl:text>
        <xsl:text disable-output-escaping="yes">;  if (typeof selectedValue === 'string') { selectedValue = selectedValue.replace(/\\/g,&quot;\\\\&quot;).replace(/[\&apos;]/g,&quot;\\&apos;&quot;);};</xsl:text>
	    <xsl:text disable-output-escaping="yes">buff.append(selectedValue);</xsl:text>
        <xsl:text disable-output-escaping="yes">buff.append(&apos;\&apos;);&apos;);</xsl:text>
        <xsl:text>} } catch(err) { }</xsl:text>
        <xsl:call-template name="closeForLoop">
            <xsl:with-param name="bv" select="$bindingValue"/>
        </xsl:call-template>
        <xsl:text disable-output-escaping="yes">#&gt;</xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template name="htmlTwoLists">
    <xsl:param name="size">
      <xsl:text>5</xsl:text>
    </xsl:param>
    <xsl:text>
</xsl:text>
    <xsl:variable name="selectId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        <xsl:with-param name="stripLastIndex">
          <xsl:text>1</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:text>hidden</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:text>MLT_SELECTION_LIST_</xsl:text>
        <xsl:value-of select="$selectId"/>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>MLT_SELECTION_LIST_</xsl:text>
        <xsl:call-template name="getBindingNameIgnoringLastSquareBrackets"/>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:text> </xsl:text>
      </xsl:attribute>
    </xsl:element>
    <xsl:element name="TABLE">
      <xsl:attribute name="id">
        <xsl:value-of select="$selectId"/>
      </xsl:attribute>
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>multipleSelect</xsl:text>
          </xsl:with-param>
          <xsl:with-param name="override">
              <xsl:text>twMultipleSelect</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:element name="TR">
      	<xsl:variable name="fromSelectId" select="concat($selectId, 'From')"/>
	    <xsl:variable name="toSelectId" select="concat($selectId, 'To')"/>
	    <xsl:variable name="topButtonId" select="concat($selectId, '_top')"/>
		<xsl:variable name="bottomButtonId" select="concat($selectId, '_bottom')"/>
        <xsl:attribute name="width">
          <xsl:text>100%</xsl:text>
        </xsl:attribute>
        <xsl:element name="TD">
          <xsl:attribute name="width">
            <xsl:text>48%</xsl:text>
          </xsl:attribute>
          <xsl:element name="SELECT">
            <xsl:attribute name="class">
                <xsl:text>twMultipleSelectLeftList</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="id">
              <xsl:value-of select="$fromSelectId"/>
            </xsl:attribute>
            <xsl:attribute name="name">
              <xsl:text>MLT_SELECTION_SOURCE_</xsl:text>
              <xsl:call-template name="getBindingNameIgnoringLastSquareBrackets"/>
            </xsl:attribute>
            <xsl:attribute name="size">
              <xsl:value-of select="$size"/>
            </xsl:attribute>
            <xsl:attribute name="multiple">
              <xsl:text>true</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="style">
              <xsl:text>width: 100%</xsl:text>
            </xsl:attribute>
            <xsl:call-template name="dualListEventHandlers"/>
            <xsl:apply-templates select="node()[local-name() = &apos;StaticOption&apos; or local-name() = &apos;DynamicOption&apos;]" mode="selectOption"/>
          </xsl:element>
        </xsl:element>
        <xsl:element name="TD">
          <xsl:attribute name="width">
            <xsl:text>4%</xsl:text>
          </xsl:attribute>
          <xsl:element name="BUTTON">
              <xsl:attribute name="class">
                  <xsl:text>twMultipleSelectTopButton</xsl:text>
              </xsl:attribute>
            <xsl:attribute name="type">
              <xsl:text>button</xsl:text>
            </xsl:attribute>
              <xsl:attribute name="id">
                  <xsl:value-of select="$topButtonId"/>
              </xsl:attribute>
            <xsl:call-template name="dualListEventHandlers"/>
            <xsl:text>&gt;&gt;</xsl:text>
          </xsl:element>
          <br/>
          <xsl:element name="BUTTON">
            <xsl:attribute name="class">
                <xsl:text>twMultipleSelectBottomButton</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="type">
              <xsl:text>button</xsl:text>
            </xsl:attribute>
              <xsl:attribute name="id">
                  <xsl:value-of select="$bottomButtonId"/>
              </xsl:attribute>
            <xsl:call-template name="dualListEventHandlers"/>
            <xsl:text>&lt;&lt;</xsl:text>
          </xsl:element>
        </xsl:element>
        <xsl:element name="TD">
          <xsl:attribute name="width">
            <xsl:text>48%</xsl:text>
          </xsl:attribute>
          <xsl:variable name="selectId">
            <xsl:call-template name="getId">
              <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
              <xsl:with-param name="stripLastIndex">
                <xsl:text>1</xsl:text>
              </xsl:with-param>
            </xsl:call-template>
          </xsl:variable>
          <xsl:element name="SELECT">
              <xsl:attribute name="class">
                  <xsl:text>twMultipleSelectRightList</xsl:text>
              </xsl:attribute>
            <xsl:attribute name="id">
              <xsl:value-of select="$toSelectId"/>
            </xsl:attribute>
            <xsl:attribute name="name">
              <xsl:text>MLT_SELECTION_TARGET_</xsl:text>
              <xsl:call-template name="getBindingNameIgnoringLastSquareBrackets"/>
            </xsl:attribute>
            <xsl:attribute name="size">
              <xsl:value-of select="$size"/>
            </xsl:attribute>
            <xsl:attribute name="multiple">
              <xsl:text>true</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="style">
              <xsl:text>width: 100%</xsl:text>
            </xsl:attribute>
            <xsl:call-template name="dualListEventHandlers"/>
            <xsl:call-template name="dualListTargetEventHandlers">
              <xsl:with-param name="target_id" select="$selectId"/>
            </xsl:call-template>
          </xsl:element>
          <xsl:call-template name="dataChanged"><xsl:with-param name="elementId"><xsl:value-of select="$selectId"/></xsl:with-param>
          </xsl:call-template>
        </xsl:element>
        <script type="text/javascript">
            addEventHandler('<xsl:value-of select="$fromSelectId"/>', "ondblclick", function() {
            	dualListSelect(this);
            	onChangeClientScript<xsl:value-of select="$selectId"/>();
            });
			addEventHandler('<xsl:value-of select="$topButtonId"/>', "onclick", function() {
            	dualListSelect(this);
            	onChangeClientScript<xsl:value-of select="$selectId"/>();
            });
			addEventHandler('<xsl:value-of select="$bottomButtonId"/>', "onclick", function() {
            	dualListDeselect(this);
            	onChangeClientScript<xsl:value-of select="$selectId"/>();
            });
            addEventHandler('<xsl:value-of select="$toSelectId"/>', "ondblclick", function() {
            	dualListDeselect(this);
            	onChangeClientScript<xsl:value-of select="$selectId"/>();
            });
        </script>
      </xsl:element>
    </xsl:element>
    <xsl:element name="SCRIPT">
      <xsl:attribute name="type">
        <xsl:text>text/javascript</xsl:text>
      </xsl:attribute>
      <xsl:text>var table = document.getElementById(&apos;</xsl:text>
      <xsl:value-of select="$selectId"/>
      <xsl:text>&apos;);
table.twAllOptions = new Array();
var options = table.getElementsByTagName(&apos;SELECT&apos;).item(0).getElementsByTagName(&apos;OPTION&apos;);
for (var i = 0; i &lt; options.length; ++i) {
  table.twAllOptions[i] = options[i];
}</xsl:text>
    </xsl:element>
    <xsl:call-template name="javaScriptMultipleSelection">
      <xsl:with-param name="controlId" select="$selectId"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="htmlMultipleSelect">
    <xsl:param name="size">
      <xsl:text>1</xsl:text>
    </xsl:param>
    <xsl:text>
</xsl:text>
    <xsl:variable name="selectId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        <xsl:with-param name="stripLastIndex">
          <xsl:text>1</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="SELECT">
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>multipleSelect</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twMultipleSelect</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:attribute name="id">
        <xsl:value-of select="$selectId"/>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:text>MLT_SELECTION_LIST_</xsl:text>
        <xsl:call-template name="getBindingNameIgnoringLastSquareBrackets"/>
      </xsl:attribute>
      <xsl:attribute name="size">
        <xsl:value-of select="$size"/>
      </xsl:attribute>
      <xsl:attribute name="multiple">
        <xsl:text>true</xsl:text>
      </xsl:attribute>
      <!-- tw:Attribute - apply an HTML attribute to an input field -->
      <xsl:apply-templates select="tw:Attribute"/>
      <xsl:call-template name="eventHandlers"/>
      <xsl:apply-templates select="node()[local-name() = &apos;StaticOption&apos; or local-name() = &apos;DynamicOption&apos;]" mode="selectOption"/>
    </xsl:element>
    <xsl:call-template name="dataChanged" >
    <xsl:with-param name="elementId"><xsl:value-of select="$selectId"/></xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="javaScriptMultipleSelection">
      <xsl:with-param name="controlId" select="$selectId"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="javaScriptMultipleSelection">
    <xsl:param name="controlId"/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="." mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="endsWithSquareBraces">
      <xsl:call-template name="endsWith">
        <xsl:with-param name="s" select="$valueBinding"/>
        <xsl:with-param name="e">
          <xsl:text>[]</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="index">
      <xsl:call-template name="getLastIndex">
        <xsl:with-param name="s" select="$valueBinding"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$endsWithSquareBraces = 1">
      <xsl:element name="SCRIPT">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&lt;# try {</xsl:text>
        <xsl:text>  for (var </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text> = 0; </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text disable-output-escaping="yes"> &lt; </xsl:text>
        <xsl:value-of select="substring($valueBinding, 1, string-length($valueBinding) - 2)"/>
        <xsl:text>.arrayLength; </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text>++) {
</xsl:text>
        <xsl:text>    buff.append(&quot;selectValue(document.getElementById(&apos;</xsl:text>
        <xsl:value-of select="$controlId"/>
        <xsl:text>&apos;), &apos;&quot;);</xsl:text>
        <xsl:text disable-output-escaping="yes">try {var selectedValue = </xsl:text>
        <xsl:call-template name="substituteIndexLetters">
          <xsl:with-param name="s" select="$valueBinding"/>
        </xsl:call-template>
        <xsl:text>} catch(err) { }</xsl:text>
        <xsl:text disable-output-escaping="yes">;  if (typeof selectedValue === 'string') { selectedValue = selectedValue.replace(/\\/g,&quot;\\\\&quot;).replace(/[\&apos;]/g,&quot;\\&apos;&quot;);};</xsl:text>
	    <xsl:text disable-output-escaping="yes">buff.append(selectedValue);</xsl:text>	
        <xsl:text>    buff.append(&quot;&apos;);&quot;);</xsl:text>
        <xsl:text disable-output-escaping="yes">  }
 } catch(err) { } #&gt;</xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template match="tw:StaticOption" mode="selectOption">
    <xsl:text>
</xsl:text>
    <xsl:element name="OPTION">
      <xsl:attribute name="value">
          <xsl:choose>
              <xsl:when test="@tw:value">
                  <xsl:value-of disable-output-escaping="yes" select="@tw:value"/>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:value-of select="."/>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:attribute>
      <xsl:value-of disable-output-escaping="yes" select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:DynamicOption" mode="selectOption">
    <xsl:if test="not(../tw:BasedOn/node()) or ../tw:BasedOn[@tw:type = 'List Variable']">
      <xsl:variable name="controlValueBinding">
        <xsl:apply-templates select=".." mode="calculateBindingValue">
          <xsl:with-param name="rhs">
            <xsl:text/>
          </xsl:with-param>
        </xsl:apply-templates>
      </xsl:variable>
      <xsl:variable name="valueBinding">
        <xsl:apply-templates select="." mode="calculateBindingValue">
          <xsl:with-param name="rhs">
            <xsl:text/>
          </xsl:with-param>
        </xsl:apply-templates>
      </xsl:variable>
      <xsl:variable name="endsWithSquareBraces">
        <xsl:call-template name="endsWith">
          <xsl:with-param name="s" select="$valueBinding"/>
          <xsl:with-param name="e">
            <xsl:text>[]</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="controlIndex">
        <xsl:call-template name="calculateIndexDepth">
          <xsl:with-param name="s" select="$controlValueBinding"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="index">
        <xsl:call-template name="getLastIndex">
          <xsl:with-param name="s" select="$valueBinding"/>
          <xsl:with-param name="d" select="$controlIndex"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:if test="$endsWithSquareBraces = 1">
        <xsl:text disable-output-escaping="yes">
&lt;# for (var </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text> = 0; </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text disable-output-escaping="yes"> &lt; </xsl:text>
        <xsl:value-of select="substring($valueBinding, 1, string-length($valueBinding) - 2)"/>
        <xsl:text>.arrayLength; </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text disable-output-escaping="yes">++) { #&gt;
</xsl:text>
        <xsl:element name="OPTION">
          <xsl:attribute name="value">
            <xsl:choose>
              <xsl:when test="@tw:valueBinding">
                <xsl:call-template name="getBindingValue">
                  <xsl:with-param name="rhs" select="@tw:valueBinding"/>
                  <xsl:with-param name="d" select="$controlIndex"/>
                </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:call-template name="getBindingValue">
                  <xsl:with-param name="d" select="$controlIndex"/>
                </xsl:call-template>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:choose>
            <xsl:when test="@tw:displayBinding">
              <xsl:call-template name="getBindingValue">
                <xsl:with-param name="rhs" select="@tw:displayBinding"/>
                <xsl:with-param name="d" select="$controlIndex"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:when test="@tw:valueBinding">
              <xsl:call-template name="getBindingValue">
                <xsl:with-param name="rhs" select="@tw:valueBinding"/>
                <xsl:with-param name="d" select="$controlIndex"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="getBindingValue">
                <xsl:with-param name="d" select="$controlIndex"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:element>
        <xsl:text disable-output-escaping="yes">
&lt;# } #&gt;
</xsl:text>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="htmlSingleRadioButtons">
    <xsl:param name="orientation"/>
    <xsl:variable name="bindingName">
      <xsl:call-template name="getBindingName"/>
    </xsl:variable>
    <xsl:variable name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:element name="DIV">
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>singleSelect</xsl:text>
          </xsl:with-param>
          <xsl:with-param name="override">
              <xsl:text>twSingleSelectRadio</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:attribute name="id">
        <xsl:value-of select="$id"/>
      </xsl:attribute>
      <xsl:apply-templates select="node()[local-name() = &apos;StaticOption&apos; or local-name() = &apos;DynamicOption&apos;]" mode="radioOrCheck">
        <xsl:with-param name="bindingName" select="$bindingName"/>
        <xsl:with-param name="orientation" select="$orientation"/>
        <xsl:with-param name="type">
          <xsl:text>radio</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="id">
            <xsl:value-of select="$id"/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:StaticOption" mode="radioOrCheck">
    <xsl:param name="bindingName"/>
    <xsl:param name="orientation"/>
    <xsl:param name="type"/>
    <xsl:param name="id"/>
      <xsl:text>
</xsl:text>
    <xsl:element name="INPUT">
      <xsl:attribute name="type">
        <xsl:value-of select="$type"/>
      </xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="$id"/>_<xsl:value-of select="position()"/></xsl:attribute>
      <xsl:attribute name="name">
        <xsl:choose>
            <xsl:when test="string-length($bindingName) &gt; 0">
                <xsl:value-of select="$bindingName"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$id"/>
            </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:attribute name="value">
        <xsl:choose>
          <xsl:when test="@tw:value">
            <xsl:value-of disable-output-escaping="yes" select="@tw:value"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="."/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:call-template name="eventHandlers"/>
    </xsl:element>
    <xsl:variable name="paramId"><xsl:value-of select="$id"/><xsl:text>_</xsl:text><xsl:value-of select="position()"/></xsl:variable>
    <xsl:call-template name="dataChanged">
      <xsl:with-param name="dataChangedEvent">
        <xsl:text>onClick</xsl:text>
      </xsl:with-param>
      <xsl:with-param name="elementId">
        <xsl:value-of select="$paramId"/>
      </xsl:with-param>
    </xsl:call-template>
    <label class="radio_label" for="{$id}_{position()}">
      <xsl:call-template name="eventHandlers"/>
      <xsl:value-of disable-output-escaping="yes" select="."/>
    </label>
    <xsl:if test="$orientation = 1">
        <xsl:element name="br" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="tw:DynamicOption" mode="radioOrCheck">
    <xsl:param name="bindingName"/>
    <xsl:param name="orientation"/>
    <xsl:param name="type"/>
    <xsl:param name="id"/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="." mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="endsWithSquareBraces">
      <xsl:call-template name="endsWith">
        <xsl:with-param name="s" select="$valueBinding"/>
        <xsl:with-param name="e">
          <xsl:text>[]</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="index">
      <xsl:call-template name="getLastIndex">
        <xsl:with-param name="s" select="$valueBinding"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$endsWithSquareBraces = 1">
      <xsl:text disable-output-escaping="yes">
&lt;# for (var </xsl:text>
      <xsl:value-of select="$index"/>
      <xsl:text> = 0; </xsl:text>
      <xsl:value-of select="$index"/>
      <xsl:text disable-output-escaping="yes"> &lt; </xsl:text>
      <xsl:value-of select="substring($valueBinding, 1, string-length($valueBinding) - 2)"/>
      <xsl:text>.arrayLength; </xsl:text>
      <xsl:value-of select="$index"/>
      <xsl:text disable-output-escaping="yes">++) { #&gt;
</xsl:text>
      <xsl:element name="INPUT">
        <xsl:attribute name="type">
          <xsl:value-of select="$type"/>
        </xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of disable-output-escaping="yes" select="concat($id,'_','&lt;#=',$index,'#&gt;')"/></xsl:attribute>
      <xsl:attribute name="name">
        <xsl:choose>
            <xsl:when test="$bindingName">
                <xsl:value-of select="$bindingName"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$id"/>
            </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
        <xsl:attribute name="value">
          <xsl:choose>
            <xsl:when test="@tw:valueBinding">
              <xsl:call-template name="getBindingValue">
                <xsl:with-param name="rhs" select="@tw:valueBinding"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="getBindingValue"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        <xsl:call-template name="styleOverride"/>
        <xsl:call-template name="eventHandlers"/>
      </xsl:element>
      <label class="radio_label">
        <xsl:attribute name="for"><xsl:value-of disable-output-escaping="yes" select="concat($id,'_','&lt;#=',$index,'#&gt;')"/></xsl:attribute>
        <xsl:choose>
          <xsl:when test="@tw:displayBinding">
            <xsl:call-template name="getBindingValue">
              <xsl:with-param name="rhs" select="@tw:displayBinding"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="@tw:valueBinding">
            <xsl:call-template name="getBindingValue">
              <xsl:with-param name="rhs" select="@tw:valueBinding"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="getBindingValue"/>
          </xsl:otherwise>
        </xsl:choose>
      </label>
      <xsl:call-template name="dataChanged">
        <xsl:with-param name="dataChangedEvent">
          <xsl:text>onClick</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="elementId">
          <xsl:value-of select="concat($id,'_','&lt;#=',$index,'#&gt;')"/>
        </xsl:with-param>
      </xsl:call-template>
      <xsl:if test="$orientation = 1">
          <xsl:element name="br" />
      </xsl:if>
      <xsl:text disable-output-escaping="yes">
&lt;# } #&gt;
</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template name="htmlMultipleCheckboxes">
    <xsl:param name="orientation"/>
    <xsl:variable name="bindingName">
      <xsl:text>MLT_SELECTION_LIST_</xsl:text>
      <xsl:call-template name="getBindingNameIgnoringLastSquareBrackets"/>
    </xsl:variable>
    <xsl:variable name="divId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        <xsl:with-param name="stripLastIndex">
          <xsl:text>1</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="DIV">
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>multipleSelect</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twMultipleSelect</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:attribute name="id">
        <xsl:value-of select="$divId"/>
      </xsl:attribute>
      <!-- Hidden field so that the server is notified of the presence of this multi select -->
      <xsl:element name="INPUT">
        <xsl:attribute name="type">
          <xsl:text>hidden</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="name">
          <xsl:value-of select="$bindingName"/>
        </xsl:attribute>
        <xsl:attribute name="value">
          <xsl:text> </xsl:text>
        </xsl:attribute>
      </xsl:element>
      <xsl:apply-templates select="node()[local-name() = &apos;StaticOption&apos; or local-name() = &apos;DynamicOption&apos;]" mode="radioOrCheck">
        <xsl:with-param name="bindingName" select="$bindingName"/>
        <xsl:with-param name="orientation" select="$orientation"/>
        <xsl:with-param name="type">
          <xsl:text>checkbox</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="id">
          <xsl:value-of select="$divId"/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:element>
    <xsl:call-template name="javaScriptMultipleSelection">
      <xsl:with-param name="controlId" select="$divId"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="tw:DateSelector">

    <xsl:variable name="bindingVar">
      <xsl:call-template name="getBindingVariable"/>
    </xsl:variable>

    <xsl:variable name="lang"> <!-- BDL shensis -->
      <xsl:value-of select="@tw:lang"/>
    </xsl:variable>
    
    <xsl:variable name="inputFieldId">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
    </xsl:variable>

	<xsl:variable name="defaultDateFormat">
		<xsl:choose>
			<xsl:when test="@tw:dateFormat and string-length(@tw:dateFormat) &gt; 0">
				<xsl:value-of select="@tw:dateFormat"/>
			</xsl:when>
		</xsl:choose>
    </xsl:variable>

    <!-- Extract date format to display -->
    <xsl:variable name="evalDateFormat">
      <xsl:choose>
        <xsl:when test="@tw:evaluatedDateFormat and string-length(@tw:evaluatedDateFormat) &gt; 0">
          <xsl:choose>
            <xsl:when test="contains(@tw:evaluatedDateFormat,'&lt;#=')">
                <xsl:value-of select="translate(@tw:evaluatedDateFormat, '&lt;#=&gt; ', '')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@tw:evaluatedDateFormat" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
			<xsl:value-of select="$defaultDateFormat"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="dateFormat">
      <xsl:call-template name="getDateFormat">
        <xsl:with-param name="bindingVar" select="$bindingVar" />
        <xsl:with-param name="defaultDateFormat" select="$defaultDateFormat" />
        <xsl:with-param name="evalDateFormat" select="$evalDateFormat" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="dateFormatAsString">
      <xsl:call-template name="getDateFormat">
        <xsl:with-param name="bindingVar" select="$bindingVar" />
        <xsl:with-param name="defaultDateFormat" select="$defaultDateFormat" />
        <xsl:with-param name="evalDateFormat" select="$evalDateFormat" />
        <xsl:with-param name="notInline">
          <xsl:text>1</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

    <xsl:element name="div">
     <xsl:attribute name="style">
        <xsl:text>padding-right: 20px;</xsl:text>
     </xsl:attribute>

     <xsl:element name="div">
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>inputText</xsl:text>
          </xsl:with-param>
          <xsl:with-param name="override">
            <xsl:text>twDateSelector</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>

      <xsl:attribute name="id">
        <xsl:text>dateWrap-</xsl:text>
        <xsl:value-of select="$inputFieldId"/>
      </xsl:attribute>

      <xsl:attribute name="style">
        <xsl:text>white-space:nowrap;</xsl:text>
      </xsl:attribute>

      <xsl:attribute name="name">
        <xsl:call-template name="getBindingName"/>
      </xsl:attribute>

      <xsl:variable name="bindingValue">
        <xsl:call-template name="getDateTimeBindingValue">
          <xsl:with-param name="dateTimeFormat" select="$dateFormatAsString"/>
          <xsl:with-param name="includePointyHash"><xsl:text>true</xsl:text></xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <xsl:attribute name="value">
        <xsl:value-of select="$bindingValue"/>
      </xsl:attribute>
      <xsl:attribute name="initialValue">
        <xsl:value-of select="$bindingValue"/>
      </xsl:attribute>

      <xsl:call-template name="styleOverride"/>
     </xsl:element>
    </xsl:element>
    <xsl:call-template name="dateSelectorEventHandlers">
      <xsl:with-param name="controlId" select="$inputFieldId"/>
    </xsl:call-template>
    <xsl:element name="INPUT">
        <xsl:attribute name="type">hidden</xsl:attribute>
        <xsl:attribute name="class">twDateFormatFor</xsl:attribute>
        <xsl:attribute name="name">
            <xsl:text>DATE_FORMAT_FOR_</xsl:text>
            <xsl:call-template name="getBindingName"/>
        </xsl:attribute>
        <xsl:attribute name="value">
            <xsl:call-template name="getDateFormat">
                <xsl:with-param name="bindingVar" select="$bindingVar" />
                <xsl:with-param name="defaultDateFormat" select="$defaultDateFormat" />
                <xsl:with-param name="evalDateFormat" select="$evalDateFormat" />
            </xsl:call-template>
        </xsl:attribute>
    </xsl:element>

    <xsl:variable name="overrideLocaleDateFormat">
      <xsl:choose>
        <xsl:when test="@tw:overrideLocaleDateFormat and string-length(@tw:overrideLocaleDateFormat) &gt; 0">
          <xsl:value-of select="@tw:overrideLocaleDateFormat"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text disable-output-escaping="yes">&lt;# var overrideLocaleDateFormat=true</xsl:text>
          <xsl:text disable-output-escaping="yes">;</xsl:text>
          <xsl:text disable-output-escaping="yes">try {</xsl:text>
          <xsl:text disable-output-escaping="yes">if (tw.system.isBaseTypeDate('</xsl:text>
          <xsl:value-of select="$bindingVar"/>
          <xsl:text disable-output-escaping="yes">') &amp;&amp; '</xsl:text><xsl:value-of select="$dateFormatAsString"/><xsl:text>'=='MM-dd-yyyy') {</xsl:text>
          <xsl:text disable-output-escaping="yes">overrideLocaleDateFormat=false;</xsl:text>
          <xsl:text disable-output-escaping="yes">}</xsl:text>
          <xsl:text disable-output-escaping="yes">} catch(err){ };</xsl:text>
          <xsl:text disable-output-escaping="yes">buff.append(overrideLocaleDateFormat);</xsl:text>
          <xsl:text disable-output-escaping="yes">#&gt;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="baseType">
      <xsl:text disable-output-escaping="yes">&lt;#= tw.system.getBaseType('</xsl:text>
      <xsl:value-of select="$bindingVar"/>
      <xsl:text disable-output-escaping="yes">') #&gt;</xsl:text>
    </xsl:variable>

    <xsl:call-template name="initializeDateSelector">
      <xsl:with-param name="controlId" select="$inputFieldId"/>
      <xsl:with-param name="lang" select="$lang"/>  <!-- BDL shensis -->
      <xsl:with-param name="dateTimeFormat" select="$dateFormat"/>
      <xsl:with-param name="dateTimeFormatAsString" select="$dateFormatAsString"/>
      <xsl:with-param name="overrideLocaleDateFormat" select="$overrideLocaleDateFormat"/>
      <xsl:with-param name="baseType" select="$baseType"/>
      <xsl:with-param name="bindingVar" select="$bindingVar" />
    </xsl:call-template>
    <xsl:call-template name="dataChanged">
      <xsl:with-param name="elementId"><xsl:value-of select="$inputFieldId"/></xsl:with-param>
    </xsl:call-template>
    <xsl:if test="@tw:binding and string-length(@tw:binding) &gt; 0">
      <xsl:call-template name="inputTextValidation"/>
    </xsl:if>
  </xsl:template>

  <!-- Write out a server side script to determine the dateformat -->
  <xsl:template name="getDateFormat">
    <xsl:param name="bindingVar"/>
    <xsl:param name="defaultDateFormat"/>
    <xsl:param name="evalDateFormat"/>
    <xsl:param name="notInline"/>
    <xsl:choose>
      <xsl:when test="$notInline = 1">
	    <xsl:choose>
		  <xsl:when test="$evalDateFormat and string-length($evalDateFormat) &gt; 0 and not(contains($evalDateFormat,'&lt;#='))" >
            <xsl:value-of select="translate($evalDateFormat,'&quot;', '')"/>
		  </xsl:when>
		  <xsl:otherwise>
            <xsl:value-of select="translate($defaultDateFormat, '&quot;', '')"/>
		  </xsl:otherwise>
	    </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
    <xsl:choose>
      <!-- If it's bound to a variable set datePattern = (bindingVar == "Date" ? defaultDateFormat : evalDateFormat) -->
      <xsl:when test="$bindingVar and string-length($bindingVar) &gt; 0">
        <xsl:text disable-output-escaping="yes">&lt;#= tw.system.variableTypeForVariable('</xsl:text>
        <xsl:value-of select="translate($bindingVar,'#','.')"/>
        <xsl:text disable-output-escaping="yes">') == 'Date' ? </xsl:text>
	       <xsl:choose>
		       <xsl:when test="$evalDateFormat and string-length($evalDateFormat) &gt; 0 and not(contains($evalDateFormat,'&lt;#='))" >
			       <xsl:value-of select="translate($evalDateFormat,'&quot;',&quot;'&quot;)"/>
		       </xsl:when>
		   <xsl:otherwise>
			       <xsl:value-of select="translate($defaultDateFormat, '&quot;', '')"/>
		   </xsl:otherwise>
	       </xsl:choose>
		<xsl:text disable-output-escaping="yes">:</xsl:text>
        <xsl:value-of select="translate($evalDateFormat,'&quot;',&quot;'&quot;)"/>
        <xsl:text disable-output-escaping="yes">;#&gt;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$defaultDateFormat"/>
      </xsl:otherwise>
    </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="initializeDateSelector">
      <xsl:param name="controlId"/>
      <xsl:param name="lang"/> <!-- BDL shensis -->
      <xsl:param name="dateTimeFormat"/>
      <xsl:param name="dateTimeFormatAsString"/>
      <xsl:param name="overrideLocaleDateFormat"/>
      <xsl:param name="baseType"/>
      <xsl:param name="bindingVar"/>
      <xsl:element name="SCRIPT">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>

        <xsl:text>
            var nameAttribute = "</xsl:text><xsl:call-template name="getBindingName"/><xsl:text>";
            var options = {
                lang: "</xsl:text><xsl:value-of select="$lang"/><xsl:text>",
            	name: nameAttribute
            };
            var valueAttribute="</xsl:text><xsl:call-template name="getDateTimeBindingValue">
											<xsl:with-param name="dateTimeFormat" select="$dateTimeFormatAsString"/>
											<xsl:with-param name="includePointyHash"><xsl:text>true</xsl:text></xsl:with-param>
										</xsl:call-template><xsl:text>";
			var adjustedDateFormat = null;
			</xsl:text>
			<xsl:choose>
				<xsl:when test="$overrideLocaleDateFormat != &apos;true&apos;"><xsl:text>
			// Using the locale date format
			if (valueAttribute) {
				try {
</xsl:text>
      <xsl:text disable-output-escaping="yes">					&lt;# if('String' == tw.system.getBaseType('</xsl:text>
      <xsl:value-of select="$bindingVar"/>
      <xsl:text disable-output-escaping="yes">')) {</xsl:text>
      <xsl:text disable-output-escaping="yes">buff.append('// Binding variable type is String\n');</xsl:text>
      <xsl:text disable-output-escaping="yes">buff.append('					var dateVal = dojo.date.locale.parse(valueAttribute, {selector: "date", datePattern: "MM-dd-yyyy"});');</xsl:text>
      <xsl:text disable-output-escaping="yes"> } else {</xsl:text>
      <xsl:text disable-output-escaping="yes">buff.append('// Binding variable type is not String\n');</xsl:text>
      <xsl:text disable-output-escaping="yes">buff.append('					var dateVal = new Date(');</xsl:text><xsl:call-template name="getDateTimeBindingValue">
														<xsl:with-param name="dateTimeFormat"><xsl:text>yyyy, MM-1, dd</xsl:text></xsl:with-param>
														<xsl:with-param name="includePointyHash"><xsl:text>false</xsl:text></xsl:with-param>
													  </xsl:call-template><xsl:text disable-output-escaping="yes">buff.append(');'); } #&gt;</xsl:text>
<xsl:text>
					valueAttribute = dojo.date.locale.format(dateVal, {selector: "date", fullYear: true});
				} catch(err) {
					//console.log(err);
				}
			}

			adjustedDateFormat = dojo.date.locale._parseInfo().bundle["dateFormat-short"];
			if (adjustedDateFormat.search("yyyy") == -1) {
				if (adjustedDateFormat.search("yy") > -1) {
					adjustedDateFormat = adjustedDateFormat.replace("yy", "yyyy")
				}
			}
					</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>
			// Using a custom date format
	        options.dateTimePattern = "</xsl:text><xsl:value-of select="$dateTimeFormat"/><xsl:text>";
	                </xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text>
            var elementId = "</xsl:text><xsl:value-of select="$controlId"/><xsl:text>";
			var elementClasses = "</xsl:text><xsl:call-template name="getControlClasses">
											<xsl:with-param name="twControl" select=".."/>
										</xsl:call-template>".trim().split(' ');<xsl:text>
			<![CDATA[var elementStyles = "";
			if(elementClasses.length > 0) {
				for(var i=0; i < elementClasses.length; i++) {
					elementStyles = elementStyles.concat(findCssTextByClassName(elementClasses[i]));
				}
			}]]>
			options.id = elementId;
			
            var dateWidget = new DateTimePicker(options);
            dateWidget.attr("value", valueAttribute);
            dojo.byId("dateWrap-" + elementId).appendChild(dateWidget.domNode);
            if (adjustedDateFormat) {
            	var formatInput = dojo.query("input.twDateFormatFor", dojo.byId("dateWrap-" + elementId).parentNode.parentNode)[0];
            	formatInput.value = adjustedDateFormat;
            }
            dateWidget.startup();

            var element = dojo.byId(elementId);

            dojo.attr(element, "name", nameAttribute);
            dojo.attr(element, "value", valueAttribute);
			
			var dateWidgetElement = dojo.byId(dijit.byId(elementId).dateBoxWidget.id);
			var widgetElement = dateWidgetElement ? dateWidgetElement : dojo.byId(dijit.byId(elementId).timeBoxWidget.id);
			if (widgetElement) {
				widgetElement.style.cssText = elementStyles;
				if(navigator.appVersion.indexOf("MSIE") > -1) {
					var cssText = widgetElement.parentNode.parentNode.style.cssText;
					widgetElement.parentNode.parentNode.style.cssText = cssText + " " + elementStyles + "; background: none;";
				}
			}
        </xsl:text>
      </xsl:element>
  </xsl:template>


  <!-- Generate HTML for a table. -->
  <xsl:template match="tw:Table">
    <xsl:variable name="tableId">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        <xsl:with-param name="stripLastIndex" select="1"/>
      </xsl:call-template>
    </xsl:variable>
    <!-- Don't generate list of selected ids if type is none -->
    <xsl:if test="@tw:selectionType != &apos;none&apos;">
      <xsl:apply-templates select="tw:DataRow" mode="selectionList">
        <xsl:with-param name="tableId" select="$tableId"/>
      </xsl:apply-templates>
    </xsl:if>

<!-- Scrolling Table. A div is needed to apply scrolling -->
    <xsl:element name="DIV">
      <xsl:attribute name="id">
        <xsl:value-of select="$tableId"/>
        <xsl:text>-div</xsl:text>
      </xsl:attribute>

      <xsl:attribute name="class">
        <xsl:text>repeatingTable</xsl:text>
        <xsl:if test="@tw:height">
   <xsl:text>Scrollable</xsl:text>
        </xsl:if>
      </xsl:attribute>

      <xsl:if test="@tw:height">
        <xsl:attribute name="style">
          <xsl:text>height:</xsl:text>
          <xsl:value-of select="@tw:height"/>
          <xsl:text>;</xsl:text>
        </xsl:attribute>
      </xsl:if>
<!-- End Scrolling Table -->

    <xsl:element name="TABLE">
      <xsl:attribute name="cellspacing">
        <xsl:text>0</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>tableControl</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twTable</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:attribute name="id">
        <xsl:value-of select="$tableId"/>
      </xsl:attribute>
      <xsl:apply-templates select="tw:Ajax[@tw:enabled='true']">
        <xsl:with-param name="tableId" select="$tableId"/>
      </xsl:apply-templates>

      <!-- tw:Attribute - apply and HTML attribute to an input field -->
      <xsl:apply-templates select="tw:Attribute"/>
      <xsl:apply-templates select="tw:DataRow" mode="header"/>
      <xsl:apply-templates select="tw:DataRow" mode="dataRow">
          <xsl:with-param name="tableId" select="$tableId"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="tw:Footer"/>
      <xsl:if test="@tw:showAddControl='true'">
          <xsl:call-template name="addRow"/>
      </xsl:if>

     <!-- Scrolling tabel DIV end -->
     </xsl:element>
    </xsl:element>
    <!-- Don't set the selections on page loading if the type is none -->
    <xsl:if test="@tw:selectionType != &apos;none&apos;">
      <xsl:apply-templates select="tw:DataRow" mode="setSelections">
          <xsl:with-param name="tableId" select="$tableId"/>
      </xsl:apply-templates>
    </xsl:if>
  </xsl:template>

  <xsl:template match="tw:Ajax">
        <xsl:param name="tableId"/>
        <xsl:element name="SCRIPT">
            <xsl:attribute name="type">
                <xsl:text>text/javascript</xsl:text>
            </xsl:attribute>
            <xsl:text>  
            addOnPageLoadFunction( function(){
                 var selfId = "</xsl:text><xsl:value-of select="$tableId"/><xsl:text>";
                 var controlId="</xsl:text><xsl:value-of select="@tw:controlId"/><xsl:text>";
                 var element = document.getElementById(controlId);
                 
                 function handleOnchange(){
                    tw.coach.refreshDependentTableElementById("</xsl:text><xsl:value-of select="$tableId"/><xsl:text>","</xsl:text><xsl:value-of select="@tw:srcServiceRef"/><xsl:text>","</xsl:text><xsl:value-of select="@tw:controlId"/><xsl:text>","&lt;# try {buff.append(tw.system.model.processAppSnapshot.id)} catch(err) { } #&gt;");
                 }
                 
                 if(element.tagName == "INPUT") {
                    element.refreshDependent = handleOnchange;
                    registerDeferredOnInputChange(element);
                 } else {
                    addEventHandler(element, "onchange", handleOnchange);
                 }
                 
                 handleOnchange();
            });
             </xsl:text>
        </xsl:element>
  </xsl:template>

  <xsl:template match="tw:DataRow" mode="header">
    <xsl:variable name="rowBinding">
      <xsl:call-template name="stripLastSquareBrackets">
        <xsl:with-param name="s">
          <xsl:apply-templates select="." mode="calculateBindingValue">
            <xsl:with-param name="rhs">
              <xsl:text/>
            </xsl:with-param>
          </xsl:apply-templates>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="indexValues">
      <xsl:call-template name="concatenateIndexValues">
        <xsl:with-param name="s" select="$rowBinding"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="TR">
      <xsl:attribute name="class">
        <xsl:call-template name="getClass">
          <xsl:with-param name="default">
            <xsl:text>tableControlHeader</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twTableHeaderTR</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:value-of select="@tw:id"/>
        <xsl:value-of select="$indexValues"/>
        <xsl:text>_Header</xsl:text>
      </xsl:attribute>
      <xsl:call-template name="selectionHeader"/>
      <xsl:for-each select="tw:Cell">
        <xsl:element name="TH">

          <!-- Repeating table column width -->
          <!-- Also default width of column with button to min width -->
          <xsl:attribute name="width">
            <xsl:choose>
            <xsl:when test="@tw:width">
              <xsl:value-of select="@tw:width"/>
            </xsl:when>
            <xsl:when test="tw:Control/tw:ButtonGroup">
              <xsl:text>1%</xsl:text>
            </xsl:when>
            </xsl:choose>

          </xsl:attribute>
          <xsl:attribute name="class">
              <xsl:text>twTableTH</xsl:text>
              <xsl:for-each select="tw:Control">
                  <xsl:for-each select="tw:Override">
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="@tw:className"/>
                  </xsl:for-each>
              </xsl:for-each>
          </xsl:attribute>
          <xsl:choose>
            <xsl:when test="tw:Control/node()/tw:Label[@tw:visible = 'true']">
              <xsl:apply-templates select="tw:Control/node()/tw:Label" mode="header">
                <xsl:with-param name="indexValues" select="$indexValues"/>
              </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:element>
      </xsl:for-each>
      <xsl:call-template name="rowActionHeader"/>
    </xsl:element>
  </xsl:template>

  <!-- Generate the header for the selection column, if required -->
  <xsl:template name="selectionHeader">
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:if test="$tableElement/@tw:selectionType != &apos;none&apos; and $tableElement/@tw:showSelectionControl = &apos;true&apos;">
      <xsl:element name="TH">
        <!-- Make the selection column minimum width -->
        <xsl:attribute name="WIDTH"><xsl:text>1%</xsl:text></xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <!-- Generate the header for the delete control column, if required -->
  <xsl:template name="rowActionHeader">
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:if test="$tableElement/@tw:showDeleteControl = &apos;true&apos; or $tableElement/@tw:showEditControl = &apos;true&apos;">
      <xsl:element name="TH">
        <!-- Set the width of the selction column in a repeating table for better visual layout -->
        <xsl:attribute name="width">
          <xsl:text>1%</xsl:text>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <!-- Generate a hidden variable to hold the list of selected items -->
  <xsl:template match="tw:DataRow" mode="selectionList">
    <xsl:param name="tableId"/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="." mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <!-- A hidden field with a value of a single space. This is used so that the backend is aware of the presense of the -->
    <!-- table even if nothing is selected. -->
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:if test="$tableElement/@tw:selectionType != &apos;none&apos;">
      <xsl:element name="INPUT">
        <xsl:attribute name="type">
          <xsl:text>hidden</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="id">
          <xsl:text>TBL_SELECTION_LIST_</xsl:text>
          <xsl:value-of select="$tableId"/>
        </xsl:attribute>
        <xsl:attribute name="name">
          <xsl:text>TBL_SELECTION_LIST_</xsl:text>
          <xsl:call-template name="substituteIndexLetters">
            <xsl:with-param name="s">
              <xsl:call-template name="stripLastSquareBrackets">
                <xsl:with-param name="s" select="translate($valueBinding, &quot;.&quot;, &quot;#&quot;)"/> <!-- change from _ to #  -->
              </xsl:call-template>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:attribute name="value">
          <xsl:text> </xsl:text>
        </xsl:attribute>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template match="tw:DataRow" mode="dataRow">
    <xsl:param name="tableId"/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="." mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="endsWithSquareBraces">
      <xsl:call-template name="endsWith">
        <xsl:with-param name="s" select="$valueBinding"/>
        <xsl:with-param name="e">
          <xsl:text>[]</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="index">
      <xsl:call-template name="getLastIndex">
        <xsl:with-param name="s" select="$valueBinding"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$endsWithSquareBraces = 1">
      <xsl:variable name="arrayName" select="substring($valueBinding, 1, string-length($valueBinding) - 2)"/>
      <xsl:text disable-output-escaping="yes">
&lt;# if (</xsl:text>
      <xsl:value-of select="$arrayName"/>
      <xsl:text> != undefined) {
  for (var </xsl:text>
      <xsl:value-of select="$index"/>
      <xsl:text> = 0; </xsl:text>
      <xsl:value-of select="$index"/>
      <xsl:text disable-output-escaping="yes"> &lt; </xsl:text>
      <xsl:value-of select="$arrayName"/>
      <xsl:text>.arrayLength; </xsl:text>
      <xsl:value-of select="$index"/>
      <xsl:text disable-output-escaping="yes">++) { #&gt;
</xsl:text>
    </xsl:if>
    <xsl:text>
</xsl:text>
    <xsl:element name="TR">
      <xsl:attribute name="class">
        <xsl:call-template name="getClass">
          <xsl:with-param name="default">
            <!-- Repeating Table alternating row colors by adding extra class -->
            <xsl:text>tableControlDataRow </xsl:text>
            <xsl:text disable-output-escaping="yes">&lt;#= ((</xsl:text>
            <xsl:value-of select="$index"/>
            <xsl:text disable-output-escaping="yes">+0)%2) ? 'odd' : 'even' #&gt;</xsl:text>
            <xsl:text>Row</xsl:text>
          </xsl:with-param>
          <xsl:with-param name="override">
              <xsl:text>twTableTR</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:value-of select="$tableId"/>
        <xsl:call-template name="getId"/>
      </xsl:attribute>
      <xsl:variable name="index">
        <xsl:call-template name="calculateIndexVarName">
          <xsl:with-param name="valueBinding" select="$valueBinding"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:call-template name="selectionControl">
        <xsl:with-param name="valueBinding" select="$valueBinding"/>
        <xsl:with-param name="index" select="$index"/>
      </xsl:call-template>
      <xsl:for-each select="tw:Cell">
        <xsl:text>
</xsl:text>
        <xsl:element name="TD">
            <xsl:attribute name="class">
                <xsl:text>twTableTD</xsl:text>
                <xsl:for-each select="tw:Control">
                    <xsl:for-each select="tw:Override">
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="@tw:className"/>
                    </xsl:for-each>
                </xsl:for-each>
            </xsl:attribute>

            <xsl:for-each select="tw:Control[not(tw:DateSelector)]">
                <xsl:call-template name="rowSelectionEvent">
                    <xsl:with-param name="index" select="$index"/>
                </xsl:call-template>
            </xsl:for-each>

            <xsl:choose>
            <xsl:when test="node()">
              <xsl:call-template name="applyAllControls"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:element>
      </xsl:for-each>
      <xsl:call-template name="rowActionControl"/>
    </xsl:element>
    <xsl:if test="$endsWithSquareBraces = 1">
      <xsl:text disable-output-escaping="yes">
&lt;# }
 }#&gt;
</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template name="calculateIndexVarName">
    <xsl:param name="valueBinding"/>
    <xsl:variable name="endsWithSquareBraces">
      <xsl:call-template name="endsWith">
        <xsl:with-param name="s" select="$valueBinding"/>
        <xsl:with-param name="e">
          <xsl:text>[]</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$endsWithSquareBraces = 1">
        <xsl:call-template name="getLastIndex">
          <xsl:with-param name="s" select="$valueBinding"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>0</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Generate the radio button or checkbox for a table row in a table with selection -->
  <xsl:template name="selectionControl">
    <xsl:param name="valueBinding"/>
    <xsl:param name="index"/>
    <!-- Is the row in a table with selection enabled -->
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:if test="$tableElement/@tw:selectionType != &apos;none&apos;">
      <xsl:if test="$tableElement/@tw:showSelectionControl = &apos;true&apos;">
        <xsl:element name="TD">
          <xsl:call-template name="rowSelectionEvent">
            <xsl:with-param name="index" select="$index"/>
          </xsl:call-template>
          <xsl:element name="INPUT">
            <xsl:attribute name="type">
              <xsl:choose>
                <xsl:when test="$tableElement/@tw:selectionType = &quot;single&quot;">
                  <xsl:text>radio</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>checkbox</xsl:text>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:attribute name="name">
              <xsl:text>TBL_SELECTION_</xsl:text>
              <xsl:call-template name="substituteIndexLetters">
                <xsl:with-param name="s">
                  <xsl:call-template name="stripLastSquareBrackets">
                    <xsl:with-param name="s" select="translate($valueBinding, &quot;.&quot;, &quot;#&quot;)"/> <!-- change from _ to #  -->
                  </xsl:call-template>
                </xsl:with-param>
              </xsl:call-template>
            </xsl:attribute>
            <xsl:attribute name="value">
              <xsl:text disable-output-escaping="yes">&lt;#= </xsl:text>
              <xsl:value-of select="$index"/>
              <xsl:text disable-output-escaping="yes"> #&gt;</xsl:text>
            </xsl:attribute>
          </xsl:element>
        </xsl:element>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- Generate the hidden input field for a table row in a table with selection. This must be put into -->
  <!-- the first table cell on the row for it to be positioned correctly in the dom -->
  <xsl:template name="hiddenSelectionControl">
    <xsl:param name="valueBinding"/>
    <xsl:param name="index"/>
    <!-- Is the row in a table with selection enabled -->
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:if test="$tableElement/@tw:selectionType != &apos;none&apos;">
      <xsl:if test="$tableElement/@tw:showSelectionControl != &apos;true&apos;">
        <xsl:element name="INPUT">
          <xsl:attribute name="type">
            <xsl:text>hidden</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="name">
            <xsl:text>TBL_SELECTION_</xsl:text>
            <xsl:call-template name="substituteIndexLetters">
              <xsl:with-param name="s">
                <xsl:call-template name="stripLastSquareBrackets">
                  <xsl:with-param name="s" select="translate($valueBinding, &quot;.&quot;, &quot;#&quot;)"/> <!-- change from _ to #  -->
                </xsl:call-template>
              </xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:attribute name="value">
            <xsl:text> </xsl:text>
          </xsl:attribute>
        </xsl:element>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- Generate the onClick code for TD and INPUT elements -->
  <xsl:template name="rowSelectionEvent">
    <xsl:param name="index"/>
    <!-- Is the cell in a table with selection enabled -->
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:if test="$tableElement/@tw:selectionType != &apos;none&apos;">
      <xsl:attribute name="onClick">
        <xsl:text disable-output-escaping="yes">selectRow(this, &apos;</xsl:text>
        <xsl:value-of select="$tableElement/@tw:selectionType"/>
        <xsl:text>&apos;, </xsl:text>
        <xsl:value-of select="$tableElement/@tw:showSelectionControl"/>
        <xsl:text disable-output-escaping="yes">, &lt;#= </xsl:text>
        <xsl:value-of select="$index"/>
        <!-- Pass parameter to selectRow javascript function indicating if deselect should be disabled -->
        <xsl:text disable-output-escaping="yes"> #&gt;</xsl:text>
        <xsl:choose>
          <xsl:when test="tw:Control/tw:ButtonGroup">
            <xsl:text>, null, true</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>, null, false</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:text>);</xsl:text>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <!-- Because the CHECKED attribute must either be present or not, and because using the attribute element I can't on the -->
  <!-- server generate the attribute conditionally this script is generated instead. It selects the rows that should be -->
  <!-- selected. This does have the benefit of correctly switching the style so it's not all bad -->
  <xsl:template match="tw:DataRow" mode="setSelections">
    <xsl:param name="tableId"/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="." mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:variable name="index">
      <xsl:call-template name="getLastIndex">
        <xsl:with-param name="s" select="$valueBinding"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="endsWithSquareBraces">
      <xsl:call-template name="endsWith">
        <xsl:with-param name="s" select="$valueBinding"/>
        <xsl:with-param name="e">
          <xsl:text>[]</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <!-- if binding is to array then setup selection -->
    <xsl:if test="$endsWithSquareBraces = 1">
      <xsl:variable name="arrayName" select="substring($valueBinding, 1, string-length($valueBinding) - 2)"/>
      <xsl:element name="SCRIPT">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
        <xsl:text disable-output-escaping="yes">&lt;# if (</xsl:text>
        <xsl:value-of select="$arrayName"/>
        <xsl:text> != undefined) {
      for (var </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text> = 0; </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text disable-output-escaping="yes"> &lt; </xsl:text>
        <xsl:value-of select="$arrayName"/>
        <xsl:text>.arrayLength; </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text disable-output-escaping="yes">++) {
  if (</xsl:text>
        <xsl:call-template name="substituteIndexValues">
          <xsl:with-param name="s">
            <xsl:call-template name="stripLastSquareBrackets">
              <xsl:with-param name="s" select="$valueBinding"/>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
        <xsl:text>.listIsSelected(</xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text disable-output-escaping="yes">)) { #&gt;</xsl:text>
        <xsl:text disable-output-escaping="yes">selectRow(document.getElementById(&apos;</xsl:text>
        <xsl:value-of select="$tableId"/>
        <xsl:call-template name="getId"/>
        <xsl:text>&apos;), &apos;</xsl:text>
        <xsl:value-of select="$tableElement/@tw:selectionType"/>
        <xsl:text disable-output-escaping="yes">&apos;, </xsl:text>
        <xsl:value-of select="$tableElement/@tw:showSelectionControl"/>
        <xsl:text disable-output-escaping="yes">, &lt;#= </xsl:text>
        <xsl:value-of select="$index"/>
        <xsl:text disable-output-escaping="yes"> #&gt;, true); </xsl:text>
        <xsl:text disable-output-escaping="yes">&lt;# }
   }
   }
   #&gt;</xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <!-- Generate the header for the delete control column, if required -->
    <xsl:template name="rowActionControl">
        <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
        <xsl:if test="$tableElement/@tw:showDeleteControl = &apos;true&apos; or $tableElement/@tw:showEditControl = &apos;true&apos;">
            <xsl:element name="TD">
                <xsl:if test="$tableElement/@tw:showEditControl = &apos;true&apos;">
                    <xsl:element name="A">
                        <xsl:attribute name="class">
                            <xsl:call-template name="getClass">
                                <xsl:with-param name="default">
                                    <xsl:text>layoutButton</xsl:text>
                                </xsl:with-param>
                            </xsl:call-template>
                        </xsl:attribute>
                        <xsl:attribute name="HREF">
                            <xsl:text>#</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="name">
                            <xsl:text>_SAVE_EDIT_</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="onClick">
                            <xsl:text>editRow(this); return false;</xsl:text>
                        </xsl:attribute>
                        <script type="text/javascript">
                 			document.write(coachDesigner_link_edit);
                		</script>
                    </xsl:element>
                </xsl:if>
                <xsl:if test="$tableElement/@tw:showDeleteControl = &apos;true&apos;">
                    <xsl:if test="$tableElement/@tw:showEditControl = &apos;true&apos;">
                        <xsl:text>|</xsl:text>
                    </xsl:if>

		    <xsl:variable name="defVisibility">
			<xsl:call-template name="findCurrentVisibility" />
		    </xsl:variable>
		     
		    <xsl:choose>

			<xsl:when test="$defVisibility = 'read'">
				<script type="text/javascript">
					document.write(coachDesigner_link_delete);
                		</script>
			</xsl:when>

		        <xsl:otherwise>
				<xsl:element name="A">
					<xsl:attribute name="class">
						<xsl:call-template name="getClass">
							<xsl:with-param name="default">
								<xsl:text>layoutButton</xsl:text>
							</xsl:with-param>
						</xsl:call-template>
					 </xsl:attribute>
					<xsl:attribute name="HREF">
						<xsl:text>#</xsl:text>
					</xsl:attribute>
					<xsl:attribute name="name">
						<xsl:text>_DELETE_ROW_</xsl:text>
					<xsl:call-template name="getBindingName">
						<xsl:with-param name="startingLocation" select="ancestor-or-self::tw:Table[position() = 1]"/>
					</xsl:call-template>
					</xsl:attribute>
					<xsl:attribute name="onClick">
						<xsl:text>try {new Formatter().toNumbers();} catch (e) {}; if (!isFormSubmitted(this, false)) { submitEnclosingForm(this); } return false;</xsl:text>
					</xsl:attribute>
					<script type="text/javascript">
                 				document.write(coachDesigner_link_delete);
                			</script>
				</xsl:element>
			</xsl:otherwise>
		    </xsl:choose>
                </xsl:if>
            </xsl:element>
        </xsl:if>
    </xsl:template>

  <xsl:template match="tw:Footer">
    <xsl:param name="defaultStyle"/>
    <xsl:element name="TR">
      <xsl:attribute name="class">
        <xsl:call-template name="getClass">
          <xsl:with-param name="default">
            <xsl:text>tableControlFooter</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:call-template name="getId"/>
      </xsl:attribute>
      <xsl:call-template name="selectionFooter"/>
      <xsl:for-each select="tw:Cell">
        <xsl:element name="TD">
          <xsl:choose>
            <xsl:when test="node()">
              <xsl:call-template name="applyAllControls"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

  <!-- Generate the footer for the selection column, if required -->
  <xsl:template name="selectionFooter">
    <!-- Is the cell in a table with selection enabled -->
    <xsl:variable name="tableElement" select="ancestor::tw:Table[position()=1]"/>
    <xsl:if test="$tableElement/@tw:selectionType != &apos;none&apos; and $tableElement/@tw:showSelectionControl = &apos;true&apos;">
      <xsl:element name="TD">
        <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template name="addRow">
    <xsl:variable name="tableElement" select="ancestor-or-self::tw:Table[position()=1]"/>
    <xsl:variable name="showSelection">
        <xsl:choose>
            <xsl:when test="$tableElement/@tw:selectionType != &apos;none&apos; and $tableElement/@tw:showSelectionControl = &apos;true&apos;">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="hasRowControl">
        <xsl:choose>
            <xsl:when test="$tableElement/@tw:showDeleteControl = &apos;true&apos; or $tableElement/@tw:showEditControl = &apos;true&apos;">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:element name="TR">
      <xsl:attribute name="class">
        <xsl:call-template name="getClass">
          <xsl:with-param name="default">
            <xsl:text>tableControlDataRow</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
        <xsl:element name="TD">
            <xsl:attribute name="colspan">
                <xsl:value-of select="count(tw:DataRow/tw:Cell) + $showSelection + $hasRowControl"/>
            </xsl:attribute>
            <xsl:element name="BUTTON">
                <xsl:attribute name="class">
                    <xsl:call-template name="getClass">
                        <xsl:with-param name="default">
                            <xsl:text>layoutButton</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:attribute>
                <xsl:attribute name="type">
                    <xsl:text>submit</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="name">
                    <xsl:text>_ADD_ROW_</xsl:text>
                    <xsl:call-template name="getBindingNameIgnoringLastSquareBrackets">
                        <xsl:with-param name="startingLocation" select="ancestor-or-self::tw:Table[position() = 1]"/>
                    </xsl:call-template>
                </xsl:attribute>
                <xsl:attribute name="onClick">
                    <xsl:text> try {new Formatter().toNumbers();} catch (e) {}; if (isFormSubmitted(this, false)) { return false; } else { return true; }</xsl:text>
                </xsl:attribute>
                <xsl:call-template name="styleOverride"/>
                <script type="text/javascript">
                 	document.write(coachDesigner_button_add_new_role);
                </script>
            </xsl:element>
        </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:Label" mode="header">
    <xsl:param name="indexValues"/>
    <xsl:element name="P">
      <xsl:choose>
        <xsl:when test="@tw:cssStyle">
          <xsl:attribute name="style">
            <xsl:value-of select="@tw:cssStyle"/>
          </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="align">
            <xsl:text>left</xsl:text>
          </xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:attribute name="class">
         <xsl:text>twLabel</xsl:text>
            <xsl:if test="@tw:cssClass">
                <xsl:text> </xsl:text>
                <xsl:value-of select="@tw:cssClass"/>
            </xsl:if>
      </xsl:attribute>

      <xsl:call-template name="convertText">
        <xsl:with-param name="s" select="."/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:Label" mode="label">
    <xsl:text>
</xsl:text>
    <xsl:variable name="control" select="ancestor-or-self::*[@tw:id][position() = 1]"/>
    <xsl:variable name="controlId">
      <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="$control/@tw:id"/>
          <xsl:with-param name="stripLastIndex">
            <xsl:text>1</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
    </xsl:variable>
    <xsl:element name="LABEL">
      <xsl:attribute name="id">
        <xsl:value-of select="$controlId"/>
        <xsl:text>_Label</xsl:text>
      </xsl:attribute>

      <xsl:attribute name="for">
        <xsl:value-of select="$controlId"/>
        <!-- Check whether the control is checkbox (note: we should NOT use control ID for checking because ID can be changed by an author). -->
        <xsl:if test="$control/tw:BooleanChoice/tw:VisualCheckboxes">
          <xsl:text>_checkbox</xsl:text>
        </xsl:if>
      </xsl:attribute>

      <!-- accessKey attribute on table -->
      <xsl:if test="@tw:accessKey">
        <xsl:attribute name="ACCESSKEY">
          <xsl:value-of select="@tw:accessKey"/>
        </xsl:attribute>
      </xsl:if>

      <xsl:if test="@tw:cssStyle and @tw:cssStyle != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="@tw:cssStyle"/>
        </xsl:attribute>
      </xsl:if>

      <xsl:choose>
        <xsl:when test="@tw:cssClass and @tw:cssClass != ''">
        <xsl:attribute name="class">
          <xsl:value-of select="@tw:cssClass"/>
        </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">
            <xsl:choose>
              <xsl:when test="@tw:allowWrap='true' or $control/tw:Override[@tw:key='tw:allowWrap' and @tw:value='true']">
                <xsl:text>wrap </xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>defaultWrapping </xsl:text>
              </xsl:otherwise>
            </xsl:choose>
            <xsl:text>twLabel </xsl:text>
            <xsl:call-template name="getLabelClass">
            </xsl:call-template>
            <!-- Include label position logic -->
            <!-- This adds a top class to the style -->
            <xsl:if test="@tw:position = 'top'">
              <xsl:text> top</xsl:text>
            </xsl:if>
          </xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:choose>
        <xsl:when test="$control/tw:BooleanChoice/tw:VisualCheckboxes">
          <xsl:call-template name="checkboxlabelEventHandlers"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="eventHandlers">
              <xsl:with-param name="events" select="'onClick,onDblClick,onMouseDown,onMouseUp,onMouseOver,onMouseOut'"/>
              <xsl:with-param name="nodes" select="ancestor-or-self::node()[local-name() = 'Control'][1]/tw:EventHandler"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:call-template name="convertText">
        <xsl:with-param name="s" select="./text()"/>
      </xsl:call-template>

      <xsl:variable name="defVisibility">
         <xsl:call-template name="findCurrentVisibility" />
      </xsl:variable>
      <xsl:if test="$defVisibility = 'required'">
        <xsl:text disable-output-escaping="yes">&lt;span  class="requiredStar"&gt;*&lt;/span&gt;</xsl:text>
      </xsl:if>

    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:CustomHTML">
    <xsl:text>
</xsl:text>
    <xsl:value-of select="." disable-output-escaping="yes"/>
  </xsl:template>

  <xsl:template match="tw:LabeledCustomHTML">
    <xsl:element name="DIV">
      <xsl:attribute name="class">
        <xsl:text>customHTML</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:value-of select="tw:Content" disable-output-escaping="yes"/>
    </xsl:element>
  </xsl:template>


  <xsl:template match="tw:ButtonGroup">
    <xsl:element name="DIV">
      <xsl:attribute name="class">
        <xsl:call-template name="getControlClass">
          <xsl:with-param name="default">
            <xsl:text>buttonGroup</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twButtonGroup</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="align">
        <xsl:choose>
          <xsl:when test="tw:Customization[@tw:align] and tw:Customization[@tw:align != '']">
            <xsl:value-of select="tw:Customization/@tw:align"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>right</xsl:text>
          </xsl:otherwise>
          </xsl:choose>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:call-template name="getId">
          <xsl:with-param name="startingId" select="ancestor-or-self::*[@tw:id][position() = 1]/@tw:id"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:call-template name="styleOverride"/>
      <xsl:apply-templates select="tw:Button"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="tw:Button">
    <xsl:variable name="generateValidationScript" select="string-length(tw:EventHandler[@tw:event=&apos;onClick&apos;]/tw:ClientScript/text()) &gt; 0"/>
    <xsl:variable name="generateButtonOnClickScript" select="@tw:decisionPoint"/>
    <xsl:variable name="button_id">
      <xsl:call-template name="getId">
        <xsl:with-param name="startingId" select="ancestor::*[@tw:id][position() = 1]/@tw:id"/>
      </xsl:call-template>
      <xsl:text>_</xsl:text>
      <xsl:value-of select="@tw:id"/>
    </xsl:variable>
    <xsl:variable name="button_hint">
      <xsl:value-of select="ancestor::*[@tw:id][position() = 1]/@tw:id"/>
      <xsl:text>#</xsl:text> <!-- change from _ to #  -->
      <xsl:value-of select="@tw:id"/>
    </xsl:variable>
    <xsl:element name="BUTTON">
      <xsl:attribute name="class">
        <xsl:call-template name="getClass">
          <xsl:with-param name="default">
            <xsl:text>layoutButton</xsl:text>
          </xsl:with-param>
            <xsl:with-param name="override">
                <xsl:text>twButton</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$button_id"/>
      </xsl:attribute>
      <xsl:attribute name="type">
        <xsl:choose>
          <xsl:when test="@tw:decisionPoint = &apos;true&apos;">
            <xsl:text>submit</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>button</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:value-of select="$button_id"/>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:value-of select="$button_hint"/>
      </xsl:attribute>
      <xsl:attribute name="buttonId">
        <xsl:text disable-output-escaping="yes">&lt;#=(typeof tw.system.buttonHintToId != 'undefined')?tw.system.buttonHintToId.get('</xsl:text>
        <xsl:value-of select="translate($button_hint,'#','_')"/>
        <xsl:text disable-output-escaping="yes">'):'x'; #&gt;</xsl:text>
      </xsl:attribute>
      <!-- tw:Attribute - apply and HTML attribute to an input field -->
      <xsl:apply-templates select="tw:Attribute"/>
      <xsl:call-template name="styleOverride"/>
      <xsl:choose>
          <xsl:when test="@tw:decisionPoint = &apos;true&apos;">
            <xsl:attribute name="onClick">
              <xsl:if test="$generateButtonOnClickScript = true()">
                <xsl:text>return onClickScript</xsl:text>
                <xsl:value-of select="$button_id" />
                <xsl:text>();</xsl:text>
              </xsl:if>
            </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="eventHandlers"/>
          </xsl:otherwise>
      </xsl:choose>
      <xsl:value-of select="tw:Label" disable-output-escaping="yes"/>
    </xsl:element>
    <xsl:choose>
          <xsl:when test="@tw:decisionPoint = &apos;true&apos;">
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="dataChanged"/>
          </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="$generateButtonOnClickScript = true()">
      <xsl:element name="script">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
        <xsl:text>
        function onClickScript</xsl:text><xsl:value-of select="$button_id"/><xsl:text>() {
          if (!window._BPM_skipTimer) {
            if (window._BPM_timerQueued &amp;&amp; !window._BPM_qTimerButtonClick) {
              return false;
            } else {
              if (!window._BPM_qTimerButtonClick) {
                setTimeout(function() {
                  // get button
                  var button = document.getElementById('</xsl:text> <xsl:value-of select="$button_id"/><xsl:text>');

                  // click button
                  if (button &amp;&amp; button.click) {
                    window._BPM_qTimerButtonClick = true;
                    button.click();
                    window._BPM_qTimerButtonClick = false;
                    window._BPM_timerQueued = false;
                    try {
                      delete window._BPM_qTimerButtonClick;
                      delete window._BPM_timerQueued;
                    } catch(ex) {
                      // IE has bug on deleting window property. Those 2 properties have been assigned to proper value. It's ok if they are not deleted properly
                    }
                  }
                },1);
                window._BPM_timerQueued = true;
                return false;
              }
            }
          }
        </xsl:text>
              <xsl:if test="$generateValidationScript = true()">
                <xsl:text>if (validationScript</xsl:text>
                <xsl:value-of select="$button_id" />
                <xsl:text>()) { </xsl:text>
              </xsl:if>
        <xsl:text>
            if (isFormSubmitted(document.getElementById('</xsl:text> <xsl:value-of select="$button_id"/><xsl:text>'), </xsl:text>
        <xsl:choose>
            <xsl:when test="@tw:isCancel='true'">false</xsl:when>
            <xsl:otherwise>true</xsl:otherwise>
        </xsl:choose>
        <xsl:text>)) {
              return false;
            } else {
              return true;
            }</xsl:text>
        <xsl:if test="$generateValidationScript = true()">
          <xsl:text>
          } else {
            return false;
          }</xsl:text>
        </xsl:if>
        <xsl:text>
        }
        </xsl:text>
      </xsl:element>
    </xsl:if>
    <xsl:if test="$generateValidationScript = true()">
      <xsl:element name="script">
        <xsl:attribute name="type">
          <xsl:text>text/javascript</xsl:text>
        </xsl:attribute>
        <xsl:text>
          function validationScript</xsl:text><xsl:value-of select="$button_id"/><xsl:text>() {
        </xsl:text>
        <xsl:value-of select="tw:EventHandler[@tw:event=&apos;onClick&apos;]/tw:ClientScript"/>
        <xsl:text>
          }
        </xsl:text>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <!-- Generate event handlers for the control or button that contains the current context node -->
  <xsl:template name="eventHandlers">
    <xsl:apply-templates select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Button&apos;][1]/tw:EventHandler"/>
  </xsl:template>
  
  <xsl:template name="checkboxlabelEventHandlers" >
  <!-- <xsl:apply-templates select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Button&apos;][1]/tw:EventHandler[@tw:event and @tw:event != &apos;onFocus&apos; and @tw:event != &apos;onClick&apos; and @tw:event != &apos;onBlur&apos; and @tw:event != &apos;onKeyPress&apos; and @tw:event != &apos;onKeyUp&apos; and @tw:event != &apos;onKeyDown&apos;]"/> -->
    <xsl:apply-templates select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Button&apos;][1]/tw:EventHandler[@tw:event and @tw:event != &apos;onClick&apos;]"/>
  </xsl:template>

  <xsl:template name="dualListEventHandlers">
  	<xsl:apply-templates select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Button&apos;][1]/tw:EventHandler[@tw:event and @tw:event != &apos;onChange&apos;]"/>
  </xsl:template>
  
  <xsl:template name="dualListTargetEventHandlers">
    <xsl:param name="target_id"></xsl:param>
    <xsl:element name="script">
      <xsl:attribute name="type"><xsl:text>text/javascript</xsl:text></xsl:attribute>
      <xsl:text>function onChangeClientScript</xsl:text><xsl:value-of select="$target_id"/><xsl:text>() {</xsl:text>
  	  <xsl:for-each select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Button&apos;][1]/tw:EventHandler[@tw:event and @tw:event = &apos;onChange&apos;]">
	    <xsl:value-of select="./tw:ClientScript"/>
  	  </xsl:for-each>
  	  <xsl:text>}</xsl:text>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="tw:EventHandler">
    <xsl:attribute name="{@tw:event}">
      <xsl:value-of select="tw:ClientScript"/>
    </xsl:attribute>
  </xsl:template>

  <!-- Special handling of eventHandlers for DateSelector control -->
  <xsl:template name="dateSelectorEventHandlers">
    <xsl:param name="controlId"/>
    <script type="text/javascript">
      addOnPageLoadFunction(function() {
      <xsl:apply-templates select="ancestor-or-self::node()[local-name() = 'Control'][1]/tw:EventHandler" mode="DateSelector">
        <xsl:with-param name="controlId" select="$controlId"/>
      </xsl:apply-templates>
      });
    </script>
  </xsl:template>

  <xsl:template match="tw:EventHandler" mode="DateSelector">
    <xsl:param name="controlId"/>
    <xsl:text>addEventHandler('</xsl:text><xsl:value-of select="$controlId"/><xsl:text>','</xsl:text><xsl:value-of select="@tw:event"/><xsl:text>',function(){</xsl:text><xsl:value-of select="tw:ClientScript"/><xsl:text>});</xsl:text>
  </xsl:template>

  <xsl:template name="dataChanged">
    <xsl:param name="dataChangedEvent">
      <xsl:text>onChange</xsl:text>
    </xsl:param>
    <xsl:param name="elementId" select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Layout&apos;][1]/@tw:id"/>
    <xsl:variable name="controlId" select="ancestor-or-self::node()[local-name() = &apos;Control&apos; or local-name() = &apos;Layout&apos;][1]/@tw:id"/>
    <xsl:variable name="functionName">
      <xsl:text>fcnDV</xsl:text>
      <xsl:value-of select="$controlId"/>
    </xsl:variable>
    <xsl:variable name="lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>
    <xsl:variable name="upper">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <!-- Lowercasing event name -->
    <xsl:variable name="event" select="translate($dataChangedEvent,$upper,$lower)"/>
    <xsl:variable name="visibilityDependants" select="//tw:VisibilityCondition[@tw:controlId = $controlId]"/>
    <script type="text/javascript">
	  <xsl:if test="@tw:autoCompleteServiceRef and string-length(@tw:autoCompleteServiceRef) &gt; 0">
		<xsl:text>
			dojo.addOnLoad(function(){
				dojo.connect(dojo.byId('</xsl:text><xsl:value-of select="$elementId"></xsl:value-of><xsl:text>'), 'onchange', null, function(){clearErrorMsg();if (vldFld(this)) { } else { } });
			});
		</xsl:text>
	  </xsl:if>
      <xsl:text>addEventHandler('</xsl:text><xsl:value-of select="$elementId"></xsl:value-of><xsl:text>', '</xsl:text><xsl:value-of select="$event"></xsl:value-of><xsl:text>', function() {
      clearErrorMsg();</xsl:text>
      <xsl:if test="(local-name() = &apos;InputText&apos; or local-name() = &apos;DateSelector&apos;) and string-length(@tw:binding) &gt; 0">
        <xsl:text>if (vldFld(this)) { </xsl:text>
      </xsl:if>
      <xsl:if test="$visibilityDependants">
        <xsl:value-of select="$functionName"/>
        <xsl:text>(this, true);</xsl:text>
      </xsl:if>
      <xsl:if test="(local-name() = &apos;InputText&apos; or local-name() = &apos;DateSelector&apos;) and string-length(@tw:binding) &gt; 0">
        <xsl:text> } else { </xsl:text>
        <xsl:if test="$visibilityDependants">
          <xsl:value-of select="$functionName"/>
          <xsl:text>(this, false);</xsl:text>
        </xsl:if>
        <xsl:text> } </xsl:text>
      </xsl:if>
      });

    </script>
  </xsl:template>

  <xsl:template name="styleOverride">
      <xsl:choose>
          <xsl:when test="@tw:cssStyle and @tw:cssStyle != ''">
              <xsl:attribute name="style">
                  <xsl:value-of select="@tw:cssStyle"/>
              </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="ancestor-or-self::tw:Control[position() = 1][@tw:cssStyleOverride]">
                <xsl:attribute name="style">
                    <xsl:value-of select="ancestor-or-self::tw:Control[position() = 1]/@tw:cssStyleOverride"/>
                </xsl:attribute>
            </xsl:if>
          </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

    <xsl:template name="getBindingName">
    <xsl:param name="startingLocation" select="."/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="$startingLocation" mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:call-template name="substituteIndexValues">
      <xsl:with-param name="s" select="translate($valueBinding, &quot;.&quot;, &quot;#&quot;)"/> <!-- change from _ to #  -->
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="getBindingVariable">
    <xsl:param name="startingLocation" select="."/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="$startingLocation" mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:call-template name="substituteIndexLetters">
      <xsl:with-param name="s" select="$valueBinding"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="getBindingVariableIgnoringLastSquareBrackets">
    <xsl:param name="startingLocation" select="."/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="$startingLocation" mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:call-template name="substituteIndexLetters">
        <xsl:with-param name="s">
        <xsl:call-template name="stripLastSquareBrackets">
            <xsl:with-param name="s" select="$valueBinding"/>
        </xsl:call-template>
        </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="getBindingNameIgnoringLastSquareBrackets">
    <xsl:param name="startingLocation" select="."/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="$startingLocation" mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:call-template name="substituteIndexValues">
      <xsl:with-param name="s">
        <xsl:call-template name="stripLastSquareBrackets">
          <xsl:with-param name="s" select="translate($valueBinding, &quot;.&quot;, &quot;#&quot;)"/> <!-- change from _ to #  -->
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="getBindingNameWithZeroIndexValue">
    <xsl:param name="startingLocation" select="."/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="$startingLocation" mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:call-template name="substituteIndexValuesWithZero">
      <xsl:with-param name="s" select="translate($valueBinding, &quot;.&quot;, &quot;_&quot;)"/>
    </xsl:call-template>
  </xsl:template>

  <!-- Server side JavaScript to calculate the value of the binding. The generated code uses try catch and outputs -->
  <!-- nothing if any errors occur during variable evaluation. This is to deal with bindings that access undefined -->
  <!-- values. The code being generated directly accesses the out variable set up when the generated html with embedded -->
  <!-- javascript is evaluated. The downside is that if you misstype a variable name you don't get an error, you just -->
  <!-- get no value. -->
  <xsl:template name="getBindingValue">
    <!-- ability to specify noEscaping of bindingValue -->
    <xsl:param name="noEscape"/>
    <xsl:param name="rhs">
      <xsl:text/>
    </xsl:param>
    <xsl:param name="d">
      <xsl:text>0</xsl:text>
    </xsl:param>
    <xsl:variable name="bindingValue">
        <xsl:call-template name="substituteIndexLetters">
            <xsl:with-param name="s">
                <xsl:apply-templates select="." mode="calculateBindingValue">
                    <xsl:with-param name="rhs" select="$rhs"/>
                </xsl:apply-templates>
            </xsl:with-param>
            <xsl:with-param name="d" select="$d+1"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:if test="string-length($bindingValue) &gt; 0">
        <xsl:text disable-output-escaping="yes">&lt;# try{ var tmp = </xsl:text>
        <xsl:value-of select="$bindingValue"/>
       <!-- Precision implementation -->
       <xsl:if test="@tw:precision or ../tw:Override[@tw:key='precision']">
          <xsl:text>.toFixed(</xsl:text>
          <xsl:value-of select="../tw:Override[@tw:key='precision']/@tw:value"/>
          <xsl:text>);</xsl:text>
        </xsl:if>
        <xsl:text>; if (tmp != undefined) { if (tw.system.isBaseTypeDate('</xsl:text>
        <xsl:value-of select="$bindingValue"/>
        <xsl:choose>
          <!-- dojo DateSelectors are MM-dd-yyyy -->
          <xsl:when test="../tw:DateSelector">
            <xsl:text>')) { tmp = (tmp.format('MM-dd-yyyy</xsl:text>
          </xsl:when>
          <xsl:otherwise>
        <xsl:text>')) { tmp = tmp.format(tw.system.dateFormatForVariable('</xsl:text>
        <xsl:value-of select="$bindingValue"/>
          </xsl:otherwise>
        </xsl:choose>
	<xsl:text>')) } else if (tw.system.isBaseTypeTime('</xsl:text>
        <xsl:value-of select="$bindingValue"/>
	<xsl:text>')) { tmp = tmp.format(tw.system.dateFormatForVariable('</xsl:text>
        <xsl:value-of select="$bindingValue"/>
        <!-- Implement noEscape of output -->
        <xsl:choose>
          <xsl:when test="$noEscape">
            <xsl:text disable-output-escaping="yes">')); } buff.append(String(tmp)); } } catch(err) { } #&gt;</xsl:text>
          </xsl:when>
          <xsl:otherwise>
  <xsl:text disable-output-escaping="yes">')); } buff.append(tw.system.escapeHtmlInternal(tmp)); } } catch(err) { } #&gt;</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:template match="node()" mode="calculateBindingValue">
    <xsl:param name="rhs"/>
    <xsl:choose>
      <xsl:when test="@tw:binding">
        <xsl:variable name="newRhs">
          <xsl:value-of select="@tw:binding"/>
          <xsl:value-of select="$rhs"/>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="not(starts-with($newRhs, &quot;.&quot;))">
            <xsl:value-of select="$newRhs"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test=". != /node()">
                <xsl:apply-templates select=".." mode="calculateBindingValue">
                  <xsl:with-param name="rhs" select="$newRhs"/>
                </xsl:apply-templates>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$newRhs"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test=". != /node()">
            <xsl:apply-templates select=".." mode="calculateBindingValue">
              <xsl:with-param name="rhs" select="$rhs"/>
            </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$rhs"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Server side JavaScript to calculate the value of the binding. The generated code uses try catch and outputs -->
  <!-- nothing if any errors occur during variable evaluation. This is to deal with bindings that access undefined -->
  <!-- values. The code being generated directly accesses the out variable set up when the generated html with embedded -->
  <!-- javascript is evaluated. The downside is that if you misstype a variable name you don't get an error, you just -->
  <!-- get no value. -->
  <xsl:template name="getDateTimeBindingValue">
    <!-- ability to specify noEscaping of bindingValue -->
    <xsl:param name="dateTimeFormat"/>
    <xsl:param name="noEscape"/>
    <xsl:param name="includePointyHash"/>
    <xsl:param name="rhs">
      <xsl:text/>
    </xsl:param>
    <xsl:param name="d">
      <xsl:text>0</xsl:text>
    </xsl:param>
    <xsl:variable name="bindingValue">
        <xsl:call-template name="substituteIndexLetters">
            <xsl:with-param name="s">
                <xsl:apply-templates select="." mode="calculateBindingValue">
                    <xsl:with-param name="rhs" select="$rhs"/>
                </xsl:apply-templates>
            </xsl:with-param>
            <xsl:with-param name="d" select="$d+1"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:if test="string-length($bindingValue) &gt; 0">
      <xsl:if test="$includePointyHash = 'true'">
        <xsl:text disable-output-escaping="yes">&lt;# </xsl:text>
      </xsl:if>
      <xsl:text disable-output-escaping="yes">try{ var tmp = </xsl:text>
      <xsl:value-of select="$bindingValue"/>
      <!-- Precision implementation -->
      <xsl:if test="@tw:precision or ../tw:Override[@tw:key='precision']">
        <xsl:text>.toFixed(</xsl:text>
        <xsl:value-of select="../tw:Override[@tw:key='precision']/@tw:value"/>
        <xsl:text>);</xsl:text>
      </xsl:if>
      <xsl:text>; if (tmp != undefined) { if (tw.system.isBaseTypeDate('</xsl:text>
      <xsl:value-of select="$bindingValue"/>
      <xsl:choose>
        <xsl:when test="../tw:DateSelector">
          <xsl:choose>
            <xsl:when test="starts-with($dateTimeFormat, 'tw.')">
              <xsl:text>')) { tmp = (tmp.format(</xsl:text><xsl:value-of select="$dateTimeFormat"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>')) { tmp = (tmp.format('</xsl:text><xsl:value-of select="$dateTimeFormat"/><xsl:text>'</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
      <xsl:text>')) { tmp = tmp.format(tw.system.dateFormatForVariable('</xsl:text>
      <xsl:value-of select="$bindingValue"/>
        </xsl:otherwise>
      </xsl:choose>
	<xsl:text>)) } else if (tw.system.isBaseTypeTime('</xsl:text>
      <xsl:value-of select="$bindingValue"/>
	<xsl:text>')) { tmp = tmp.format(tw.system.dateFormatForVariable('</xsl:text>
      <xsl:value-of select="$bindingValue"/>
      <!-- Implement noEscape of output -->
      <xsl:choose>
        <xsl:when test="$noEscape">
          <xsl:text disable-output-escaping="yes">')); } buff.append(String(tmp)); } } catch(err) { }</xsl:text>
        </xsl:when>
        <xsl:otherwise>
  <xsl:text disable-output-escaping="yes">')); } buff.append(tw.system.escapeHtmlInternal(tmp)); } } catch(err) { }</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="$includePointyHash = 'true'">
        <xsl:text disable-output-escaping="yes"> #&gt;</xsl:text>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="controlSupportsVisibilityCss">
    <xsl:param name="ctrlName"/>
    <!--
      inputText:      for text-<input> and for <TEXTAREA>, ...
      singleSelect:   for <SELECT> and <DIV> around of radio-<INPUT>, ...
      booleanChoice:  for <DIV> around of checkbox-<INPUT>, ...
      multipleSelect: for tavle around of <TABLE> 'with multipleSelect' control.
    -->
    <xsl:value-of select="
           $ctrlName = 'inputText'
        or $ctrlName = 'userRoleInputText'
        or $ctrlName = 'singleSelect'
        or $ctrlName = 'singleSelect'
        or $ctrlName = 'multipleSelect'
        or $ctrlName = 'booleanChoice'
     " />
  </xsl:template>

  <!-- Return the value to be used for the class attribute of the HTML element representing a control. If there is a -->
  <!-- styleClass attribute on the XML  element use that. Otherwise use the default but append NoLabel if the control -->
  <!-- does not have a lable. Must be called in the context of a control implementation such as OuputText or InputText -->
  <xsl:template name="getControlClass">
    <xsl:param name="default"/>
    <xsl:param name="override"/>
    <xsl:choose>
      <xsl:when test="@tw:cssClass and @tw:cssClass != ''">
        <xsl:value-of select="@tw:cssClass"/>
      </xsl:when>
      <xsl:otherwise>

        <xsl:variable name="useVisibilityCss">
          <xsl:call-template name="controlSupportsVisibilityCss">
            <xsl:with-param name="ctrlName" select="$default"/>
          </xsl:call-template>
        </xsl:variable>


        <xsl:value-of select="$default"/>
        <!-- for-each [ancestor::tw:Table[position()=1]] { -->
        <xsl:variable name="isShowingLabel">
          <xsl:call-template name="isShowingLabel"/>
        </xsl:variable>
        <xsl:if test="$isShowingLabel = &apos;0&apos;">
          <xsl:text>NoLabel</xsl:text>
        </xsl:if>

        <xsl:if test="$useVisibilityCss = 'true'">
          <xsl:variable name="defVisibility">
            <xsl:call-template name="findCurrentVisibility" />
          </xsl:variable>
          <!-- possible values: full, required, read (for disabled) -->
          <xsl:choose>
            <xsl:when test="$defVisibility = 'full'">
              <xsl:text>_Full</xsl:text>
            </xsl:when>
            <xsl:when test="$defVisibility = 'required'">
              <xsl:text>_Req</xsl:text>
            </xsl:when>
            <xsl:when test="$defVisibility = 'read'">
              <xsl:text>_Dis</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <!-- TODO: maybe would be better to use nothing, V Melnykov -->
              <xsl:text>_Full</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>


      </xsl:otherwise>
    </xsl:choose>
      <xsl:text> twControl </xsl:text>
      <xsl:value-of select="$override"/>
  </xsl:template>

  <!-- Return the value to be used for the class attribute of the HTML element representing a control. If there is a  -->
  <!-- styleClass attribute on the XML  element use that. Otherwise use the default but append NoLabel if the control -->
  <!-- does not have a lable. Must be called in the context of tw:Label (when tw:Label it is used in context of a     -->
  <!-- control implementation such as OuputText or InputText)                                                         -->
  <xsl:template name="getLabelClass">
    <xsl:choose>
      <xsl:when test="@tw:cssClass and @tw:cssClass != ''">
        <xsl:value-of select="@tw:cssClass"/>
      </xsl:when>
      <xsl:otherwise>

      <xsl:variable name="defVisibility">
        <xsl:call-template name="findCurrentVisibility" />
      </xsl:variable>

      <!-- possible values: full, required, read (for disabled) -->
      <xsl:choose>
        <xsl:when test="$defVisibility = 'full'">
           <xsl:text>label_Full</xsl:text>
        </xsl:when>
        <xsl:when test="$defVisibility = 'required'">
           <xsl:text>label_Req</xsl:text>
        </xsl:when>
        <xsl:when test="$defVisibility = 'read'">
           <xsl:text>label_Dis</xsl:text>
        </xsl:when>
        <xsl:otherwise>
              <!-- TODO: maybe would be better to use just 'label', V Melnykov -->
           <xsl:text>label_Full</xsl:text>
        </xsl:otherwise>
      </xsl:choose>

      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Returns the nearest effective tw:Visibility node -->
  <!--
  <xsl:template name="findVisibilityNode">
    <xsl:param name="node"/>

    <xsl:if test="$node != ''">
      <xsl:choose>
      <xsl:when test=" $node/tw:Visibility and $node/tw:Visibility/@tw:overrideParentVisibility
                      and ($node/tw:Visibility/@tw:overrideParentVisibility = 'true')">
        <xsl:value-of select="xalan:nodeset($node/tw:Visibility)" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="findVisibilityNode">
          <xsl:with-param name="node" select="$node/.."/>
        </xsl:call-template>
      </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template> -->

  <!-- Returns the nearest effective tw:Visibility for node -->
  <xsl:template name="findVisibilityFor">
    <xsl:param name="node"/>

    <xsl:if test="$node != ''">
      <xsl:choose>
      <xsl:when test=" $node/tw:Visibility and $node/tw:Visibility/@tw:overrideParentVisibility
                      and ($node/tw:Visibility/@tw:overrideParentVisibility = 'true')">
        <xsl:value-of select="$node/tw:Visibility/@tw:default" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="findVisibilityFor">
          <xsl:with-param name="node" select="$node/.."/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
    </xsl:if>
  </xsl:template>

  <!-- Returns the nearest effective tw:Visibility for node -->
  <xsl:template name="findCurrentVisibility">
    <xsl:call-template name="findVisibilityFor">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:template>

  <!-- Return the value to be used for the class attribute of the HTML element. If there is a styleClass attribute on -->
  <!-- the XML element use that. Otherwise use the default but append "InCell" to the end if the element is -->
  <!-- in a tw:Cell, i.e. if it's in a table -->
  <xsl:template name="getClass">
    <xsl:param name="default"/>
    <xsl:param name="override"/>
    <xsl:choose>
        <xsl:when test="@tw:cssClass and @tw:cssClass != ''">
          <xsl:value-of select="@tw:cssClass"/>
        </xsl:when>
      <xsl:when test="@tw:styleClass">
        <xsl:value-of select="@tw:styleClass"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$default"/>
        <xsl:variable name="isInCell">
          <xsl:call-template name="isInCell"/>
        </xsl:variable>
        <xsl:if test="$isInCell = 1">
          <xsl:text>InCell</xsl:text>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
    <!-- Add Scrollable if a height is specified -->
    <xsl:if test="@tw:height">
      <xsl:text>Scrollable</xsl:text>
    </xsl:if>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$override"/>
  </xsl:template>

  <!-- stripLastIndex if 1 remove the last index from the generated id -->
  <xsl:template name="getId">
    <xsl:param name="inPointyHash">0</xsl:param>
    <xsl:param name="startingId" select="@tw:id"/>
    <xsl:param name="stripLastIndex">
      <xsl:text>0</xsl:text>
    </xsl:param>
    <!--
    <xsl:variable name="theId">
    -->
    <xsl:value-of select="translate($startingId, &quot;#&quot;, &quot;_&quot;)"/>
    <!--
    <xsl:value-of select="$startingId"/>
    -->
        <xsl:call-template name="getIdPostfix">
            <xsl:with-param name="stripLastIndex" select="$stripLastIndex"/>
            <xsl:with-param name="inPointyHash" select="$inPointyHash"/>
        </xsl:call-template>
    <!--
    </xsl:variable>
    -->
    <!--
    <xsl:value-of select="translate($theId, &quot;#&quot;, &quot;_&quot;)"/>
    -->
  </xsl:template>

  <!-- Strip [] off end of string if it ends with [] -->
  <xsl:template name="stripLastSquareBrackets">
    <xsl:param name="s"/>
    <xsl:variable name="endsWithSquareBrackets">
      <xsl:call-template name="endsWith">
        <xsl:with-param name="s" select="$s"/>
        <xsl:with-param name="e">
          <xsl:text>[]</xsl:text>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$endsWithSquareBrackets = 1">
        <xsl:value-of select="substring($s, 1, string-length($s) - 2)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$s"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="substituteIndexLetters">
    <xsl:param name="s"/>
    <xsl:param name="d">
      <xsl:text>1</xsl:text>
    </xsl:param>
    <xsl:choose>
      <xsl:when test="contains($s, &quot;[]&quot;)">
        <xsl:value-of select="substring-before($s, &quot;[]&quot;)"/>
        <xsl:text>[</xsl:text>
        <xsl:value-of select="substring(&quot;ijklmnopqrstu&quot;, $d, 1)"/>
        <xsl:text>]</xsl:text>
        <xsl:call-template name="substituteIndexLetters">
          <xsl:with-param name="s" select="substring-after($s, &quot;[]&quot;)"/>
          <xsl:with-param name="d" select="$d + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$s"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="substituteIndexValues">
    <xsl:param name="s"/>
    <xsl:param name="d">
      <xsl:text>1</xsl:text>
    </xsl:param>
    <xsl:choose>
      <xsl:when test="contains($s, &quot;[]&quot;)">
        <xsl:value-of select="substring-before($s, &quot;[]&quot;)"/>
        <xsl:text disable-output-escaping="yes">#&lt;#= </xsl:text> <!-- change from _ to #  -->
        <xsl:value-of select="substring(&quot;ijklmnopqrstu&quot;, $d, 1)"/>
        <xsl:text disable-output-escaping="yes"> #&gt;</xsl:text>
        <xsl:call-template name="substituteIndexValues">
          <xsl:with-param name="s" select="substring-after($s, &quot;[]&quot;)"/>
          <xsl:with-param name="d" select="$d + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$s"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="substituteIndexValuesWithZero">
    <xsl:param name="s"/>
    <xsl:choose>
      <xsl:when test="contains($s, &quot;[]&quot;)">
        <xsl:value-of select="substring-before($s, &quot;[]&quot;)"/>
        <xsl:text disable-output-escaping="yes">_0</xsl:text>
        <xsl:call-template name="substituteIndexValuesWithZero">
          <xsl:with-param name="s" select="substring-after($s, &quot;[]&quot;)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$s"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="concatenateIndexValues">
    <xsl:param name="inPointyHash">0</xsl:param>
    <xsl:param name="indexSeparator">#</xsl:param>
    <xsl:param name="s"/>
    <xsl:param name="d">
      <xsl:text>1</xsl:text>
    </xsl:param>
    <xsl:if test="contains($s, &quot;[]&quot;)">
      <xsl:choose>
          <xsl:when test="$inPointyHash=1"><xsl:value-of select="$indexSeparator"/>&quot; + </xsl:when> <!-- change from _ to #  -->
          <xsl:otherwise><xsl:value-of select="$indexSeparator"/><xsl:text disable-output-escaping="yes">&lt;#= </xsl:text></xsl:otherwise> <!-- change from _ to #  -->
      </xsl:choose>
      <xsl:value-of select="substring(&quot;ijklmnopqrstu&quot;, $d, 1)"/>
      <xsl:choose>
          <xsl:when test="$inPointyHash=1"> + &quot;</xsl:when>
          <xsl:otherwise><xsl:text disable-output-escaping="yes"> #&gt;</xsl:text></xsl:otherwise>
      </xsl:choose>
      <xsl:call-template name="concatenateIndexValues">
        <xsl:with-param name="s" select="substring-after($s, &quot;[]&quot;)"/>
        <xsl:with-param name="d" select="$d + 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="getLastIndex">
    <xsl:param name="s"/>
    <xsl:param name="d">0</xsl:param>
    <xsl:variable name="indexDepth">
      <xsl:call-template name="calculateIndexDepth">
        <xsl:with-param name="s" select="$s"/>
        <xsl:with-param name="d" select="$d"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="substring(&quot;ijklmnopqrstu&quot;, $indexDepth, 1)"/>
  </xsl:template>

  <xsl:template name="getIdPostfix">
    <xsl:param name="inPointyHash">0</xsl:param>
    <xsl:param name="stripLastIndex"/>
    <xsl:variable name="valueBinding">
      <xsl:apply-templates select="." mode="calculateBindingValue">
        <xsl:with-param name="rhs">
          <xsl:text/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:call-template name="concatenateIndexValues">
      <xsl:with-param name="inPointyHash" select="$inPointyHash"/>
      <xsl:with-param name="indexSeparator">_</xsl:with-param>
      <xsl:with-param name="s">
        <xsl:choose>
          <xsl:when test="$stripLastIndex=1">
            <xsl:call-template name="stripLastSquareBrackets">
              <xsl:with-param name="s" select="$valueBinding"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$valueBinding"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="calculateIndexDepth">
    <xsl:param name="s"/>
    <xsl:param name="d">
      <xsl:text>0</xsl:text>
    </xsl:param>
    <xsl:choose>
      <xsl:when test="contains($s, &quot;[]&quot;)">
        <xsl:call-template name="calculateIndexDepth">
          <xsl:with-param name="s" select="substring-after($s, &quot;[]&quot;)"/>
          <xsl:with-param name="d" select="$d + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$d"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="openForLoop">
      <xsl:param name="bv"/>
      <xsl:param name="prefix"/>
      <xsl:choose>
          <xsl:when test="contains($bv, &quot;[]&quot;)">
              <xsl:variable name="currentPrefix">
                  <xsl:value-of select="$prefix"/>
                  <xsl:value-of select="substring-before($bv, &quot;[]&quot;)"/>
                  <xsl:text>[]</xsl:text>
              </xsl:variable>
              <xsl:variable name="index">
                  <xsl:call-template name="getLastIndex">
                      <xsl:with-param name="s" select="$currentPrefix"/>
                  </xsl:call-template>
              </xsl:variable>
              <xsl:text> try { for (var </xsl:text><xsl:value-of select="$index"/><xsl:text> = 0; </xsl:text><xsl:value-of select="$index"/><xsl:text> &lt; </xsl:text>
              <xsl:call-template name="substituteIndexLetters">
                  <xsl:with-param name="s">
                      <xsl:value-of select="$prefix"/>
                      <xsl:value-of select="substring-before($bv, &quot;[]&quot;)"/>
                  </xsl:with-param>
              </xsl:call-template>
              <xsl:text>.arrayLength; ++</xsl:text><xsl:value-of select="$index"/><xsl:text>) { </xsl:text>
              <xsl:call-template name="openForLoop">
                  <xsl:with-param name="bv" select="substring-after($bv, &quot;[]&quot;)"/>
                  <xsl:with-param name="prefix">
                      <xsl:value-of select="substring-before($bv, &quot;[]&quot;)"/>
                      <xsl:text>[]</xsl:text>
                  </xsl:with-param>
              </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
          </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template name="closeForLoop">
      <xsl:param name="bv"/>
      <xsl:choose>
          <xsl:when test="contains($bv, &quot;[]&quot;)">
              <xsl:text> } } catch(err) { } </xsl:text>
              <xsl:call-template name="closeForLoop">
                  <xsl:with-param name="bv" select="substring-after($bv, &quot;[]&quot;)"/>
              </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
          </xsl:otherwise>
      </xsl:choose>
  </xsl:template>


  <xsl:template name="endsWith">
    <xsl:param name="s"/>
    <xsl:param name="e"/>
    <xsl:variable name="stringLength" select="string-length($s)"/>
    <xsl:variable name="endingLength" select="string-length($e)"/>
    <xsl:choose>
      <xsl:when test="$endingLength &lt; $stringLength">
        <xsl:choose>
          <xsl:when test="substring($s, $stringLength - $endingLength + 1, $endingLength) = $e">
            <xsl:text>1</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>0</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$endingLength = $stringLength">
        <xsl:choose>
          <xsl:when test="$s = $e">
            <xsl:text>1</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>0</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>0</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Returns 1 if the control that is the current context element is showing a label -->
  <xsl:template name="isShowingLabel">
    <xsl:choose>
      <xsl:when test="tw:Label">
        <xsl:variable name="isInCell">
          <xsl:call-template name="isInCell"/>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="$isInCell = 1">
            <xsl:text>0</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>1</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>0</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Returns 1 if the current control is inside a Cell. Must be called in the context of -->
  <!-- an InputText or other direct child of control -->
  <xsl:template name="isInCell">
    <xsl:choose>
      <xsl:when test="name(../..) = &quot;tw:Cell&quot;">
        <xsl:text>1</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>0</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="convertNewlineToSpace">
    <xsl:param name="s"/>
    <xsl:variable name="nl"><xsl:text>
</xsl:text></xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($s, $nl)">
        <xsl:value-of select="substring-before($s, $nl)"/>
        <xsl:text> </xsl:text>
        <xsl:call-template name="convertNewlineToSpace">
          <xsl:with-param name="s" select="substring-after($s, $nl)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$s"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="convertText">
    <xsl:param name="s"/>
    <xsl:choose>
      <xsl:when test="contains($s, &quot;&lt;#&quot;)">
        <xsl:call-template name="convertText">
          <xsl:with-param name="s" select="substring-before($s, &quot;&lt;#&quot;)"/>
        </xsl:call-template>
        <xsl:call-template name="pointyHashInConvertText">
          <xsl:with-param name="s" select="substring-after($s, &quot;&lt;#&quot;)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$s" disable-output-escaping="yes"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="pointyHashInConvertText">
    <xsl:param name="s"/>
    <xsl:choose>
      <xsl:when test="contains($s, &quot;#&gt;&quot;)">
        <xsl:text disable-output-escaping="yes">&lt;#</xsl:text>
        <xsl:value-of select="substring-before($s, &quot;#&gt;&quot;)" disable-output-escaping="yes"/>
        <xsl:text disable-output-escaping="yes">#&gt;</xsl:text>
        <xsl:call-template name="convertText">
          <xsl:with-param name="s" select="substring-after($s, &quot;#&gt;&quot;)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>&lt;#</xsl:text>
        <xsl:call-template name="convertText">
          <xsl:with-param name="s" select="$s"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="layoutOrSection">
    <xsl:choose>
      <xsl:when test="ancestor::tw:Section">
        <xsl:text>section</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>layout</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

    <xsl:template name="layoutOrSectionCap">
        <xsl:choose>
            <xsl:when test="ancestor::tw:Section">
                <xsl:text>Section</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>Layout</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


  <!-- Template creates <DIV> element with warning message -->
  <xsl:template name="warningMessage">
    <xsl:param name="text"/>
    <xsl:element name="div">
      <xsl:attribute name="class">
        <xsl:text>warningMessage</xsl:text>
      </xsl:attribute>
      <xsl:value-of select="$text"/>
    </xsl:element>
  </xsl:template>

    <xsl:template name="cssInline">
        <xsl:param name="css"/>
        <xsl:element name="style">
            <xsl:attribute name="type">
                <xsl:text>text/css</xsl:text>
            </xsl:attribute>
            <xsl:value-of select="$css"/>
        </xsl:element>
    </xsl:template>

    <xsl:template name="inlineAssetUrl">
        <xsl:param name="reference"/>
		<xsl:choose>
			<xsl:when test="$previewMode != &apos;true&apos;">&lt;#  try { buff.append(Packages.teamworks.CoachDesignerUtils.getAssetUrl('<xsl:value-of select="$reference"/>')); } catch (err) { } #&gt;</xsl:when>
			<xsl:otherwise>&lt;#  try { buff.append(PreviewUtils.getAssetUrl('<xsl:value-of select="$reference"/>')); } catch (err) { }  #&gt;</xsl:otherwise>
		</xsl:choose>
    </xsl:template>

    <xsl:template match="tw:Image">
    	<xsl:element name="img">
			<xsl:attribute name="alt"><xsl:value-of select="@tw:altText"/></xsl:attribute>
			<xsl:attribute name="src">
	          <xsl:call-template name="inlineAssetUrl">
    	          <xsl:with-param name="reference" select="@tw:reference"/>
        	  </xsl:call-template>
			</xsl:attribute>
    	</xsl:element>
    </xsl:template>

    <xsl:template match="tw:FileAttachment">
    	<xsl:element name="a">
			<xsl:attribute name="href">
	          <xsl:call-template name="inlineAssetUrl">
    	          <xsl:with-param name="reference" select="@tw:reference"/>
        	  </xsl:call-template>
			</xsl:attribute>
			<xsl:value-of select="@tw:anchorText"/>
    	</xsl:element>
    </xsl:template>

    <xsl:template name="string-replace-all">
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
            <xsl:when test="contains($text, $replace)">
                <xsl:value-of select="substring-before($text,$replace)" />
                <xsl:value-of select="$by" />
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="substring-after($text,$replace)" />
                    <xsl:with-param name="replace" select="$replace" />
                    <xsl:with-param name="by" select="$by" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
