<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="layout.html" />
  <xi:include href="macros.html" />

<?python
	def isSelected(type,q_id,a_list,value):
		for a in a_list:
			if a['question_id'] == q_id:
				if a['answer'] == value:
					if (int(type) == 1 or int(type)==3):
						return "checked"
					else:
						return "selected"
		return None

	def getAnswer(q_id,a_list):
		for a in a_list:
			if a['question_id'] == q_id:
					return a['answer']
		return None
?>
<py:def function="getComp(idx,q_id,type,detaillist,a_list,status)">
<?python
	disabled=None
	if status==2:
		disabled='disabled'

	linefeed = False
	if len("".join(detaillist)) > 40:
		linefeed =True
?>
	<py:choose>
	  <py:when test="int(type) == 1">
	  	<py:for each="element in detaillist">
	  		<input type="radio" name="answer_${idx}" value="${element}" checked="${isSelected(type,q_id,a_list,element)}" disabled="${disabled}"/>${element}
	  		<py:if test="linefeed">
		  		<br/>
	  		</py:if>
	  	</py:for>
	  </py:when>
	  <py:when test="int(type) == 2">
	  	<select name="answer_${idx}" disabled="${disabled}">
	  	<py:for each="element in detaillist">
	  		<option value="${element}" selected="${isSelected(type,q_id,a_list,element)}" >${element}</option>
	  	</py:for>
	  	</select>
	  </py:when>
	  <py:when test="int(type) == 3">
	  	<py:for each="element in detaillist">
	  		<input type="checkbox" name="answer_${idx}" value="${element}" checked="${isSelected(type,q_id,a_list,element)}" disabled="${disabled}"/>${element}
	  		<py:if test="linefeed">
		  		<br/>
	  		</py:if>
	  	</py:for>
	  </py:when>
	  <py:when test="int(type) == 4">
	  	<input type="text" size="80" name="answer_${idx}" disabled="${disabled}" value="${getAnswer(q_id,a_list)}"/>
	  </py:when>
	  <py:when test="int(type) == 5">
	  	<textarea name="answer_${idx}" cols="80" rows="4"  disabled="${disabled}">${getAnswer(q_id,a_list)}</textarea>
	  </py:when>
	  </py:choose>
</py:def>

  <head>
    <title>アンケート回答</title>
	<script type="text/javascript">
		function eventHandler(actionName) {
			f = document.forms['questionlist'];
			document.getElementById('actionName').value = actionName;
			if(actionName == "send"){
				if(!confirm('アンケートの回答を送信します。よろしいですか？')){
					return false;
				}
				f.action="${href.enquete('answer')}";
			} else if(actionName == "enquetelist"){
				location.href="${href.enquete()}";
				return;
			}
			f.submit();
		}
	</script>
  </head>
  <body>
	<form name="questionlist" action="" method="post">
	<div>
		<B>タイトル：${title}</B><P/>
		<input type="hidden" name="idx"/>
		<input type="hidden" id="actionName"  name="actionName"/>
	</div>
	<div>
		<input type="button" name="enquetelist" value="戻る"  onclick="eventHandler('enquetelist');" />
		<table width="90%"  class="list">
			<thead>
				<tr>
					<th>No.</th>
					<th>設問</th>
					<th>回答</th>
				</tr>
			</thead>
			<tbody>
				<py:for each="idx, question in enumerate(question_list)">
<?python
mod = idx % 2
style = ''
if mod==0:
	style = 'even'
else:
	style = 'odd'
?>
					<tr class="${style}">
						<td width="40">${idx+1}</td>
						<td>${question['question_title']}</td>
						<td>${getComp(idx,question['question_id'],question['type'], (question.detail or '').splitlines(), answer_list,status)}</td>
						<input type="hidden" name="question_id" value="${question['question_id']}" />
					</tr>
				</py:for>
			</tbody>
		</table>
		<input type="hidden" name="id" value="${enquete_id}" />
		<py:if test="status!=2">
			<input type="button" name="send" value="回答送信"  onclick="eventHandler('send');" />
		</py:if>
	</div>
	</form>
  </body>
</html>
