<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="js" uri="net.sourceforge.jsurveyor.jsurveyorlib" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="net.sourceforge.jsurveyor.*" %>
<js:noCache />
<js:login access="normal" />
<%
int questionnaireID = Integer.parseInt(request.getParameter("questionnaireID"));
Questionnaire editQ = DataAccess.retrieveQuestionnaire(questionnaireID);
int questionPageNR = Integer.parseInt(request.getParameter("page"));
QuestionPage questionPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)(editQ.getRefQuestionPages()).get(questionPageNR)).substring(1)));
Evaluation tempEval;
if(questionPageNR == 0) {
	User user = (User)session.getAttribute("userInfo");
	tempEval = new Evaluation(user.getUserName(), questionnaireID);
	session.setAttribute("tempEval", tempEval);
} else {
	tempEval = (Evaluation)session.getAttribute("tempEval");
}
String button = request.getParameter("button");
if(button.equals("Cancel")) { 
	ArrayList deleting = tempEval.getAnswers();
	for(int i = 0; i < deleting.size(); i++) {
		DataAccess.deleteAnswer(Integer.parseInt(((String)deleting.get(i)).substring(1)));
	}
	%>
	<c:redirect url="normal_questionnaire_list.jsp">
	</c:redirect>
	<%
} 
ArrayList questionRefs = questionPage.getRefQuestions();
for(int i = 0; i < questionRefs.size(); i++) {
	String questionRef = (String)questionRefs.get(i);
	int qRef = Integer.parseInt(questionRef.substring(1));
	if(questionRef.substring(0,1).equals("F")) {
			String freetext = request.getParameter(Integer.toString(qRef));
			if(!freetext.equals("")) {
				AnswerFreeText tempAnswer = new AnswerFreeText(qRef, freetext);
				DataAccess.storeAnswer(tempAnswer);
				tempEval.addAnswer(tempAnswer);
			} // we ignore the case where no answer has been submitted
	} else if(questionRef.substring(0,1).equals("M")) {
		try {
			String choice = request.getParameter(Integer.toString(qRef));
			int choiceInt = Integer.parseInt(choice);
			AnswerMultipleChoice tempAnswer = new AnswerMultipleChoice(qRef, choiceInt);
			DataAccess.storeAnswer(tempAnswer);
			tempEval.addAnswer(tempAnswer);
		} catch(NumberFormatException e) {
			// Answer havent been submitted, we just ignore it.	
			// the exception is cast when we try to format Null to an integer.
		}	
	}	
}

if(button.equals("Finish")) {
	DataAccess.storeEvaluation(tempEval);
	session.removeAttribute("tempEval");
	%>
	<c:redirect url="normal_questionnaire_list.jsp">
	</c:redirect>
	<%
}
session.setAttribute("tempEval", tempEval);
%>
<js:border title="jSurveyor">
<form action='normal_next.jsp' method='post'>
<input name='questionnaireID' type='hidden' value='<%= editQ.getQuestionnaireID() %>' />
<input name='page' type='hidden' value='<%= questionPageNR+1 %>' />
<tr>
	  <td class='mainmenu_headline'><%= editQ.getQuestionnaireName() %></td>
 	  <td class='mainmenu_headline_emptyfield'>&nbsp;</td>
</tr>

	<tr>
	  <td colspan='2' class='mainmenu_blueframe'>
	    <table border='0' cellspacing='0' cellpadding='0'>
		  <tr>
		  	<td align='right'>
		  	<table cellpadding='0' cellspacing='0' style='width:150px'>
<!--		  	<tr>
			  	<td align='center'>
			  	<table cellpadding='0' cellspacing='0' style='border:1px solid #000000'>
			  		<tr>
				  		<% 
				  		ArrayList questionPages = editQ.getRefQuestionPages();
				  		int size = questionPages.size();
			 			%>
			  			<td style='width:0;background-color:#8697b0'>&nbsp;</td>
			  			<td style='width:100%;background-color:#ffffff'>&nbsp;</td>
			  		</tr>
			  	</table>
			  	</td>
		  	</tr> -->
		  	<tr>
			  <td colspan='2' align='center'>
			  <small>page <%= questionPageNR+2 %> of <%= size %></small>
			  </td>
			  </tr>
		  	</table>
		  	</td>
		  </tr>
		  <td class='mainmenu_workframe'> <!-- Main workframe start -->
	        <table border='0' cellspacing='0' cellpadding='0'> 
			  <tr>
			  <td class='mainmenu_inner_workframe'>

			  <table>
			  
		      <tr>
				<td class='centeralign'>
				  <h2><%= editQ.getQuestionnaireName() %></h2>  <!-- Headline of questionnaire -->
				</td>
			  </tr>

			  <tr>
			    <td rowspan='2' valign='middle'>
				<%
				QuestionPage QPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)questionPages.get(questionPageNR+1)).substring(1)));
				ArrayList questions = QPage.getRefQuestions();
				boolean show = false;
				ArrayList answers = tempEval.getAnswers();
				for(int i = 0; i < questions.size(); i++) {
					show = false;
					String tempQRef = (String)questions.get(i);
					if(tempQRef.substring(0,1).equals("M")) {
						QuestionMultipleChoice tempQ = (QuestionMultipleChoice)DataAccess.retrieveQuestion(Integer.parseInt(tempQRef.substring(1)));
						if(tempQ.getDepends()) {
							ArrayList dependency = tempQ.getDependency();
							int dependQ = Integer.parseInt(((String)dependency.get(0)).substring(1));
							String[] validChoices = ((String)dependency.get(1)).split(";");
							for(int a = 0; a < answers.size(); a++) {
								String answerRef = (String)answers.get(a);
								if(answerRef.substring(0,1).equals("M")) {
									int answerID = Integer.parseInt(answerRef.substring(1));
									AnswerMultipleChoice tempAnswer = (AnswerMultipleChoice)DataAccess.retrieveAnswer(answerID);
									int tempQID = tempAnswer.getQuestionID();
									if(tempQID == dependQ) {
										int choice = tempAnswer.getChoice();
										for(int c = 0; c < validChoices.length; c++) {
											if(choice == Integer.parseInt(validChoices[c])) {
												show = true;
											} // end if valid answer
										} // end run through valid choices
									} // end if we have found the right question
								} //end if its a multiple choice answer
							} // end for run of answers
						} else {
						show = true;
						}
						if(show) {
						%>
						<table  class='user_question_border'> 
					<tr>
					  <td colspan='4' class='centeralign'>
					  <%= tempQ.getText() %>
					  </td>
					</tr>
					<tr>
					  <td class='centeralign'>

					  <table border='0'>
					  <tr>
					  <td style='width:10%;'>
					  </td>
					    <td>
					  <table border='0'>
					  <tr>
					  <%
					  String[] choices = tempQ.getChoice();
					  for(int j = 0; j < choices.length; j++ ) {
					  
  					  if(j != 0 && j%5 == 0) { 
  					  	%>
  					  	<tr>
  					  	<% 
  					  	} 
  					  %>
  					  <td class='centeralign' style='width:<%= 80/(choices.length)%>%;'><input type='radio' name='<%= tempQ.getQuestionID()%>' value='<%= j %>' /><br /><%= choices[j] %></td>
					  <%
					  if(j != 0 && j+1%5 == 0) { 
  					  	%>
  					  	</tr>
  					  	<% 
  					  	}
					  }
					  %>
					  </tr>
					  </table>
					  </td>
						<td width='10%'></td>
					  </tr>
					  </table>
					  </td>
					</tr>
				  </table> 
	  			  
				  <br /> 
						<%
						} else {
							%><input name='<%= tempQ.getQuestionID()%>' type='hidden' /><%
						}
					}
					if(tempQRef.substring(0,1).equals("F")) {
						QuestionFreeText tempQ = (QuestionFreeText)DataAccess.retrieveQuestion(Integer.parseInt(tempQRef.substring(1)));
						if(tempQ.getDepends()) {
							ArrayList dependency = tempQ.getDependency();
							int dependQ = Integer.parseInt(((String)dependency.get(0)).substring(1));
							String[] validChoices = ((String)dependency.get(1)).split(";");
							for(int a = 0; a < answers.size(); a++) {
								if(((String)answers.get(a)).substring(0,1).equals("M")) {
									AnswerMultipleChoice tempAnswer = (AnswerMultipleChoice)DataAccess.retrieveAnswer(Integer.parseInt(((String)answers.get(a)).substring(1)));
									int tempQID = tempAnswer.getQuestionID();
									if(tempQID == dependQ) {
										int choice = tempAnswer.getChoice();
										for(int c = 0; c < validChoices.length; c++) {
											if(choice == Integer.parseInt(validChoices[c])) {
												show = true;
											} // end if valid answer
										} // end run through valid choices
									} // end if we have found the right question
								} //end if its a multiple choice answer
							} // end for run of answers
						} else {
						show = true;
						}
						if(show) {
						%>
						<table class='user_question_border'> 
					<tr>
					  <td colspan='4' class='centeralign'>
					  <%= tempQ.getText() %>
					  </td>
					</tr>
					<tr>

					  <td class='centeralign'>
					  <table>
					  <tr>
					  	<td style='width:10%;'>
					  	</td>
					    <td style='width:80%;'>
						  <textarea name='<%= tempQ.getQuestionID()%>' cols='100' rows='2'></textarea>
						</td>
					  	<td style='width:10%;'>
					  	</td>
					  </tr>
					  </table>

					  </td>
					</tr>
				  </table> 
					<br />	
						<%
						} else {
						%><input name='<%= tempQ.getQuestionID()%>' type='hidden' /><%
						}
					}
				}
				%>
				
				
				
				  

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


			</table>

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

	<tr>
	<td colspan='2' style='padding:2px;'> <!-- Makes space between blue border and top of buttons -->
	</td>

	</tr>
	
	
	<tr>
		<td></td>
	  <td class='mainmenu_buttonline'><input class='stdbutton' name='button' type='submit' value='Cancel'/> 
	  <%
	  if(editQ.getRefQuestionPages().size() == questionPageNR+2) {
	  %>
	  <input class='stdbutton' name='button' type='submit' value='Finish'/> 
      <%
      } else {
      %>
  	  <input class='stdbutton' name='button' type='submit' value='Next'/> 
      <%
      }
      %>
      </td>
    </tr>

</form>
</js:border>