<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet [
<!ENTITY and "&amp;&amp;" > <!-- javascript && -->
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:strip-space elements="*"/>
	<xsl:output method="html" indent="yes" encoding="utf-8"/>
	
	<xsl:template match="DELEGATED-CONTENT">
		<xsl:param name="additionalContent" />
		<xsl:param name="status" />
		
		<script type="text/javascript">
			getMode = function() { 
				return $('use_current', 'use_existing', 'create_new').find(
					function(o) { 
						return o &and; o.checked;
					}).value;
			}

			function validateAttach() {
				mode = getMode();
				return (
					(mode == 'use_current')
					|| (mode == 'use_existing'
						&and; validateField('SB_Validator.notEmpty', 'fi_user_name_id', 'EMPTY-FIELDS')
				        &and; validateField('SB_Validator.notEmpty', 'fi_password', 'EMPTY-FIELDS'))
					|| (mode == 'create_new'
						&and; validateField('SB_Validator.notEmpty', 'fi_user_name_id', 'EMPTY-FIELDS')
				        &and; validateField('SB_Validator.notEmpty', 'fi_password', 'EMPTY-FIELDS')
						&and; validateField('SB_Validator.isEmail', 'fi_user_email', 'INVALID-EMAIL'))
				);
			}

			SB_Validator.arePasswordsEqual = function() {
				return SB_Validator.isEqual($('new_fi_password').value,
											$('confirm_new_fi_password').value);
			}

			function validateUpdate() {
				return validateField('SB_Validator.isEmail', 'fi_user_email', 'INVALID-EMAIL')
					&and; validateField('SB_Validator.notEmpty', 'current_fi_password', 'EMPTY-FIELDS')
					&and; validateField('SB_Validator.notEmpty', 'new_fi_password', 'EMPTY-FIELDS')
					&and; validateField('SB_Validator.notEmpty', 'confirm_new_fi_password', 'EMPTY-FIELDS')
					&and; validateField('SB_Validator.arePasswordsEqual', 'confirm_new_fi_password', 'PASSWORDS-ARE-NOT-EQUAL');
			}

			function getValidator(mode) {
				map = {
					'attach': validateAttach,
					'update': validateUpdate,
					'detach': function() { return true;} // this form is always valid (:
				};
				return map[mode];
			}

			function onSubmitClick() {
				formValidator = getValidator('<xsl:value-of select="@FEDERATED-ACTION" />');
				if (typeof(formValidator) == "function" &and; formValidator()) {
					$('federatedForm').submit();
				}
			}
	
			function onCancelClick() {
				$('confirmed').disabled = true;
				$('federatedForm').submit();
			}
	
			function disableForm() {
				$('confirmed').disabled = true;
			}

			function modeChanged(e) {
				mode = getMode();
				$("fi_user_name_id").disabled = (mode == "use_current");
				$("fi_password").disabled = (mode == "use_current");
				$("fi_user_email").disabled = (mode != "create_new");
			}
		</script>

		<form name="federatedForm" action="{@ACTION}" method="post" id="federatedForm">
			<xsl:apply-templates select="$additionalContent" />
			<div style="padding:5">
				<!--<span><xsl:value-of select="@DESCRIPTION"/></span>-->

				<xsl:apply-templates select="." mode="content" />
			</div>
			<!-- input image needed for work default submit via Enter -->
			<input type="image" style="border-width: 0px; height: 0px; width: 0px;"/>
			<xsl:call-template name="SUBMIT-BUTTONS" />
		</form>
	</xsl:template>

	<!-- action specific templates, should not be applied directly -->
	<xsl:template match="DELEGATED-CONTENT[@FEDERATED-ACTION='attach']" mode="content">
		<table cellpadding="0" cellspacing="0" border="0" class="control-title-input">
			<tr>
				<td class="title-block"><xsl:apply-templates select="CONTROL[@NAME='mode']" mode="label"/></td>
				<td class="input-block">
					<table cellpadding="0" cellspacing="0" border="0" class="control-input-title">
						<xsl:for-each select="CONTROL[@NAME='mode']/CONTROL[@TYPE='RADIO-OPTION']">
							<tr>
								<td class="input-block">
									<xsl:apply-templates select="." mode="control">
										<xsl:with-param name="id" select="@ID"/>
										<xsl:with-param name="tabindex">0</xsl:with-param>
										<xsl:with-param name="onclick">modeChanged(this);</xsl:with-param>
									</xsl:apply-templates>
								</td>
								<td class="title-block">
									<xsl:apply-templates select="." mode="label"/>
								</td>
							</tr>
						</xsl:for-each>
					</table>
				</td>
			</tr>
			<tr>
				<td class="title-block"><xsl:apply-templates select="CONTROL[@NAME='fi_user_name_id']" mode="label"/></td>
				<td class="input-block">
					<xsl:apply-templates select="CONTROL[@NAME='fi_user_name_id']" mode="control">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
			</tr>
			<tr>
				<td class="title-block"><xsl:apply-templates select="CONTROL[@NAME='fi_password']" mode="label"/></td>
				<td class="input-block">
					<xsl:apply-templates select="CONTROL[@NAME='fi_password']" mode="control">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
			</tr>
			<tr>
				<td class="title-block"><xsl:apply-templates select="CONTROL[@NAME='fi_user_email']" mode="label"/></td>
				<td class="input-block">
					<xsl:apply-templates select="CONTROL[@NAME='fi_user_email']" mode="control">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
			</tr>
		</table>
		
		
		
		
		
		
		<table width="100%" border="0">
			<xsl:call-template name="TABLE-FOOTER" />
		</table>
		<script type="text/javascript">
			modeChanged(document.getElementById('mode'));
		</script>
	</xsl:template>

	<xsl:template match="DELEGATED-CONTENT[@FEDERATED-ACTION='update']" mode="content">
		<table cellpadding="0" cellspacing="0" border="0" class="control-title-input">
			<tr>
				<td class="title-block">
					<xsl:apply-templates select="CONTROL[@NAME='fi_id']" mode="label" />
				</td>
				<td class="input-block">
					<xsl:value-of select="CONTROL[@NAME='fi_id']"/>
				</td>
			</tr>
			<tr>
				<td class="title-block">
					<xsl:apply-templates select="CONTROL[@NAME='fi_user_email']" mode="label">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
				<td class="input-block">
					<xsl:apply-templates select="CONTROL[@NAME='fi_user_email']" mode="control">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
			</tr>
			<tr>
				<td class="title-block">
					<xsl:apply-templates select="CONTROL[@NAME='current_fi_password']" mode="label">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
				<td class="input-block">
					<xsl:apply-templates select="CONTROL[@NAME='current_fi_password']" mode="control">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
			</tr>
			<tr>
				<td class="title-block">
					<xsl:apply-templates select="CONTROL[@NAME='new_fi_password']" mode="label">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
				<td class="input-block">
					<xsl:apply-templates select="CONTROL[@NAME='new_fi_password']" mode="control">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
			</tr>
			<tr>
				<td class="title-block">
					<xsl:apply-templates select="CONTROL[@NAME='confirm_new_fi_password']" mode="label">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
				<td class="input-block">
					<xsl:apply-templates select="CONTROL[@NAME='confirm_new_fi_password']" mode="control">
						<xsl:with-param name="require">true</xsl:with-param>
					</xsl:apply-templates>
				</td>
			</tr>
		</table>
		
		<table width="100%" border="0" cellpadding="2" cellspacing="0">
			<xsl:call-template name="TABLE-FOOTER" />
		</table>
	</xsl:template>

	<xsl:template name="TABLE-FOOTER">
		<tr class="sb-formtablefooter"><th style="width:100%" /></tr>
	</xsl:template>

	<xsl:template match="DELEGATED-CONTENT[@FEDERATED-ACTION='detach']" mode="content">
		<table style="width:100%" cellspacing="0" cellpadding="0" border="0">
			<tr><td style="padding:10"><xsl:value-of select="@DESCRIPTION"/></td></tr>
			<xsl:call-template name="TABLE-FOOTER" />
		</table>
	</xsl:template>

	<xsl:template name="SUBMIT-BUTTONS">
		<xsl:call-template name="OK-CANCEL-BUTTONS">
			<xsl:with-param name="okAction">javascript:onSubmitClick();</xsl:with-param>
			<xsl:with-param name="cancelAction">javascript:onCancelClick();</xsl:with-param>
			<xsl:with-param name="isRequiredFieldsExists">
				<xsl:if test="@FEDERATED-ACTION != 'detach'">1</xsl:if>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>	
	
</xsl:stylesheet>

