<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:eat="http://msqr.us/xsd/ieat"
	xmlns:xweb="http://msqr.us/xsd/jaxb-web"
	exclude-result-prefixes="eat xweb">
	
    <!-- imports -->
	<xsl:import href="tmpl/default-layout.xsl"/>
	
	<!-- layout variables -->
	<xsl:variable name="layout.global.nav.page" select="''"/>
	
	<!-- helper vars -->
	<xsl:variable name="meal" select="xweb:x-data/xweb:x-model/eat:edit/eat:meal"/>
	<xsl:variable name="is-new" select="not(boolean($meal/@mealId))"/>

	<xsl:template match="xweb:x-data" mode="page-title">
		<xsl:choose>
			<xsl:when test="$is-new = 'true'">
				<xsl:value-of select="key('i18n','edit-meal.create.title')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="key('i18n','edit-meal.edit.title')"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
		
	<xsl:template match="xweb:x-data" mode="page-head-content">
		<script type="text/javascript" src="{$web-context}/js/jquery-1.3.2.js" xml:space="preserve"> </script>
		<script type="text/javascript" src="{$web-context}/js/jquery.ajaxQueue.js" xml:space="preserve"> </script>
		<script type="text/javascript" src="{$web-context}/js/xweb-locale.js" xml:space="preserve"> </script>
		<script type="text/javascript" src="{$web-context}/js/ieat-util.js" xml:space="preserve"> </script>
		<script type="text/javascript" src="{$web-context}/js/edit-meal.js" xml:space="preserve"> </script>
		<script type="text/javascript">
			<xsl:comment>
			var msg_setRecipe = '<xsl:value-of select="key('i18n','meal.recipe.change')"/>';
			var numRecipes = <xsl:value-of select="count($meal/eat:recipe)"/>;
			if ( numRecipes == 0 ) {
				numRecipes++;
			}
			function doChooseRecipe(rowIndex) {
				searchForRecipe(webContext,rowIndex);
			}
			//</xsl:comment>
		</script>
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="page-body">
		<div class="intro">
			<xsl:choose>
				<xsl:when test="$is-new = 'true'">
		            <xsl:value-of select="key('i18n','edit-meal.create.intro')"/>
				</xsl:when>
				<xsl:otherwise>
		            <xsl:value-of select="key('i18n','edit-meal.edit.intro')"/>
				</xsl:otherwise>
			</xsl:choose>
		</div>

		<form method="post" action="{$web-context}{$ctx/xweb:path}">
			<xsl:attribute name="action">
				<xsl:value-of select="$web-context"/>
				<xsl:choose>
					<xsl:when test="$is-new = 'true'">
						<xsl:text>/addMeal.do</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>/editMeal.do</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<table class="form">
				<tr>
					<th>
						<xsl:if test="$err[@field='meal.name']">
							<xsl:attribute name="class">error</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="key('i18n','meal.name.displayName')"/>
					</th>
					<td>
						<input type="text" name="meal.name" value="{$meal/eat:name}"
							maxlength="128"/>
						<br/>
						<span class="caption">
							<xsl:value-of select="key('i18n','meal.name.caption')"/>
						</span>
					</td>
				</tr>
			</table>

			<p>
				<xsl:value-of select="key('i18n','meal.recipes.explain')" 
					disable-output-escaping="yes"/>
			</p>

			<div class="formLabel">
				<xsl:choose>
					<xsl:when test="$err[starts-with(@field,'meal.recipe')]">
						<span class="error">
							<xsl:value-of select="key('i18n','meal.recipes.displayName')"/>
						</span>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="key('i18n','meal.recipes.displayName')"/>:
					</xsl:otherwise>
				</xsl:choose>
			</div>

			<table class="form" id="recipe_table">
				<xsl:for-each select="$meal/eat:recipe">
					<xsl:call-template name="meal-recipe"/>
				</xsl:for-each>
				<xsl:if test="not($meal/eat:recipe)">
					<xsl:call-template name="meal-recipe">
						<xsl:with-param name="index" select="0"/>
					</xsl:call-template>
				</xsl:if>
				<tr id="recipe_add_row">
					<td>
						<a href="javascript:addRecipe()" 
							title="{key('i18n','meal.recipes.add')}" class="plus">
							<span class="alt">
								<xsl:value-of select="key('i18n','meal.recipes.add')"/>
							</span>
						</a>
					</td>
					<td></td>
				</tr>
			</table>
			
			<hr class="sep"/>
			
			<table class="form">
				<tr>
					<th/>
					<td>
						<input type="submit" name="_target1" value="{key('i18n','continue.displayName')}"/>
						<xsl:text> </xsl:text>
						<input type="submit" name="_cancel" value="{key('i18n','cancel.displayName')}"/>
					</td>
				</tr>
			</table>
        </form>
	</xsl:template>

	<xsl:template name="meal-recipe">
		<xsl:param name="index"/>
		<xsl:variable name="row-index">
	    		<xsl:choose>
	    			<xsl:when test="string-length($index) &gt; 0">
	    				<xsl:value-of select="$index"/>
	    			</xsl:when>
	    			<xsl:otherwise>
	    				<xsl:value-of select="position() - 1"/>
	    			</xsl:otherwise>
	    		</xsl:choose>
	    	</xsl:variable>
	    	<xsl:variable name="me" select="."/>
		<tr valign="middle">
			<xsl:if test="not(self::eat:recipe) or position() = 1">
				<xsl:attribute name="id">recipe_row</xsl:attribute>
			</xsl:if>
			<xsl:variable name="errkey">
				<xsl:text>meal.recipe[</xsl:text>
				<xsl:value-of select="$row-index"/>
				<xsl:text>]</xsl:text>
			</xsl:variable>
			<td>
				<xsl:if test="$err[starts-with(@field,$errkey)]">
					<span class="error">*</span>
					<xsl:text> </xsl:text>
				</xsl:if>
				
				<input type="text" name="meal.recipe[{$row-index}].quantity" class="smallnum"
					value="{@quantity}" maxlength="8"/>
			</td>
			<td>
				<select name="meal.recipe[{$row-index}].course.courseId">
					<option value="0"></option>
					<xsl:for-each select="$aux/eat:ui/eat:course">
						<xsl:call-template name="recipe-course">
							<xsl:with-param name="recipe" select="$me"/>
						</xsl:call-template>
					</xsl:for-each>
				</select>
			</td>
			<td class="read-only">
				<input type="hidden" name="meal.recipe[{$row-index}].recipe.recipeId">
					<xsl:attribute name="value">
						<xsl:choose>
							<xsl:when test="eat:recipe/@recipeId">
								<xsl:value-of select="eat:recipe/@recipeId"/>
							</xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
				</input>
				<a id="meal.recipe[{$row-index}].recipe.name" href="javascript:doChooseRecipe({$row-index})">
					<xsl:choose>
						<xsl:when test="eat:recipe/@recipeId &gt; 0">
							<xsl:variable name="recipe-id" select="eat:recipe/@recipeId"></xsl:variable>
							<xsl:value-of select="$aux/eat:ui/eat:recipe[@recipeId = $recipe-id]/eat:name"/>
						</xsl:when>
						<xsl:otherwise>
							<span class="hilite">
								<xsl:text>(</xsl:text>
								<xsl:value-of select="key('i18n','meal.recipe.change')"/>
								<xsl:text>)</xsl:text>
							</span>
						</xsl:otherwise>
					</xsl:choose>
					
				</a>
			</td>
			<td>
				<a href="javascript:removeRecipe({$row-index})"
					title="{key('i18n','meal.recipes.remove')}" class="minus">
					<span class="alt">
						<xsl:value-of select="key('i18n','meal.recipes.remove')"/>
					</span>
				</a>
			</td>
		</tr>
    </xsl:template>

	<xsl:template name="recipe-course">
		<xsl:param name="recipe"/>
		<option value="{@courseId}">
			<xsl:if test="@courseId = $recipe/eat:course/@courseId">
				<xsl:attribute name="selected">selected</xsl:attribute>
			</xsl:if>
			<xsl:value-of select="."/>
		</option>
	</xsl:template>

</xsl:stylesheet>
