<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="js" uri="net.sourceforge.jsurveyor.jsurveyorlib" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sourceforge.jsurveyor.*" %>
<jsp:useBean id="Data" class="net.sourceforge.jsurveyor.DataAccess">
</jsp:useBean>
<js:noCache />
<js:login access="admin" />
<js:border title="jSurveyor - Questionnaires">
<%
int questionnaireID = Integer.parseInt(request.getParameter("QuestionnaireID"));

Questionnaire editQ = DataAccess.retrieveQuestionnaire(questionnaireID);
if (editQ.getState()) {
	response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);
}
int questionPageID = Integer.parseInt(request.getParameter("QuestionPageID"));
int questionID = Integer.parseInt(request.getParameter("Question"));
QuestionPage QPage = DataAccess.retrieveQuestionPage(questionPageID);
QuestionMultipleChoice editQuestion = (QuestionMultipleChoice)DataAccess.retrieveQuestion(questionID);
%>
<form name='form' action='admin_multiplechoice1_edit.jsp' method='post'>
<input type='hidden' name='questionID' value='<%= questionID %>' />
<input type='hidden' name='questionPageID' value='<%= questionPageID %>' />
<input type='hidden' name='questionnaireID' value='<%= questionnaireID %>' />
<input type='hidden' name='reload' value='false' />

	<tr>
	  <td class='mainmenu_headline'>Question</td>
	  <td class='mainmenu_headline_emptyfield'></td>
	</tr>


	<tr>
	  <td colspan='2' class='mainmenu_blueframe'>
	    <table border='0' cellspacing='0' cellpadding='0'>
	      <tr>
                <td class='mainmenu_inner_workframe'> <!-- Main workframe start -->
	                   		
<!-- JSP stuff to read questionnaire from the database, and use the following lines to generate the questionnaire and to generate the introduction to the specific questionnaire -->
			  	

	            <table border='0' cellspacing='0' cellpadding='0'>
		      <tr>
		        <td style='width:40%;'>Multiple Choice<br />
                        <small>Use this type when the respondent is to choose<br />one option among multiple choices<br /><br />
                        </small>Sample<br /><img src='multiplechoice1.png' alt='Preview of multiplechoice1' 
                        title='Sample of how the question will look like'/></td>
		        <td style='width:60%;' colspan='2'>Question:<br />
                        <textarea name='text' cols='69' rows='3' title='Type your question here'><%= editQuestion.getText() %></textarea><br />
                        <br />Possible answers:<br /><small>Each answer must be on different lines<br /></small>
                        <textarea name='choices' cols='69' rows='3' title='Type the different answers to the question here'><%
                        String[] choicesSelf = editQuestion.getChoice();
                        for(int i = 0; i < choicesSelf.length; i++) {
	                        out.print(choicesSelf[i]);
	                        if(i < choicesSelf.length-1) {
	                        out.print("\n");
	                        }
                        }
                        %></textarea><br />
                        <br /><br /><br /></td></tr>
		      <tr>
		        <td style='border-top:1px solid #000000; border-right:1px solid #000000; padding-right:5px;'><br />If you only want to ask this question depending on what the respondent answers to a previous question, this can be selected to the right.<br /><br /> In the dropdownbox you select the question, you want this question to depent on. <br /><br />Then select the answer that should invoke this question, in the right box containing the answers to the chosen question.</td> 
		        <td style='border-top:1px solid #000000; padding-left:20px; width:20%;' valign='top'>
			  <table>
			  <tr>
			  <td>                        

			 <br />Select question: 
			 <br />
<select name='Depend' onChange='document.form.reload.value="true"; submit();' title='Here you select which question the current question can depend on'>
			  <%
			  int depQID = 0;
			  //String dependIntegers = "";
			  
			  //String depQAnswers;
			  String dependIntegerSet = "";
			  if (editQuestion.getDepends() || request.getParameter("reload") != null ) {
			  	
			  	if ( request.getParameter("reload") != null ) {
			  		depQID = Integer.parseInt(request.getParameter("reload"));
			  	}
			  	else {
			  		ArrayList dependency = editQuestion.getDependency();
			  		depQID = Integer.parseInt(((String) dependency.get(0)).substring(1));
			  		dependIntegerSet = (String)dependency.get(1);
   				}
		  	
				%>
			  	<option value='-1'>none</option>
			  	<%
			  } //end if
			  else {
			  	%>
			  	<option value='-1' selected='selected'>none</option>
			  	<%
			  } //end else
			  
			  
			  //retrieve all questions (maybe only the previous ones)
			  ArrayList qPages = editQ.getRefQuestionPages();
			  String[] choices = {"null"};
			  for(int i = 0; i < qPages.size(); i++) {
			  		QuestionPage tempQPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)qPages.get(i)).substring(1)));
			  		ArrayList questions = tempQPage.getRefQuestions();
			  		for(int j = 0; j < questions.size(); j++) {
			  			String questionRef = (String)questions.get(j);
			  			if(questionRef.substring(0,1).equals("F")) {
			  				QuestionFreeText tempQ = (QuestionFreeText)DataAccess.retrieveQuestion(Integer.parseInt(questionRef.substring(1)));
			  			} //end if
			  			else if(questionRef.substring(0,1).equals("M")) {
			  				QuestionMultipleChoice tempQ = (QuestionMultipleChoice)DataAccess.retrieveQuestion(Integer.parseInt(questionRef.substring(1)));
			  				if (tempQ.getQuestionID() == depQID){
			  					choices = tempQ.getChoice();
			  					String text;
								if(tempQ.getText().length() > 48) {
									text = tempQ.getText().substring(0,45) + "...";
								} else {
									text = tempQ.getText();
								}
								%><option value='<%= tempQ.getQuestionID() %>' selected='selected'><%= text %></option><%
			  				} //end if
			  				else {
				  				String text;
								if(tempQ.getText().length() > 48) {
									text = tempQ.getText().substring(0,45) + "...";
								} else {
									text = tempQ.getText();
								}
								%><option value='<%= tempQ.getQuestionID() %>' ><%= text %></option><%
				  			} // end else
			  			} // end else if
			  			
			  			
			  		}// end inner for
			  } //end outer for
			  
			  %>
			  </select>

		        </td>
			</tr>
			<tr>
		          <td><br /> Select answers:<br />
                              <select name='Answers' title='Here you select the answer that triggers the current question' size='6' style='width:90%;' multiple>
			      <%
			      //String[] dependChoices = tempQ.getChoice();
			      //choices = tempQ.getChoice();
					
					if ( editQuestion.getDepends() && !(request.getParameter("reload") != null) ) {
						String[] dependIntegers = dependIntegerSet.split(";");
						
						boolean selected;
						for(int k = 0; k < choices.length; k++ ) {
							selected = false;
							for (int m = 0 ; m < dependIntegers.length; m++) {
								if ( dependIntegers[m] != null && Integer.parseInt(dependIntegers[m]) == k) {
									selected = true;
								} //end if
							} // end inner for
							if (selected) {
									String text;
									if(choices[k].length() > 48) {
										text = choices[k].substring(0,45) + "...";
									} else {
										text = choices[k];
									}
									%><option value='<%= k %>' selected='selected'><%= text %></option><%
							} //end if
							else {
									String text;
									if(choices[k].length() > 48) {
										text = choices[k].substring(0,45) + "...";
									} else {
										text = choices[k];
									}
									%><option value='<%= k %>' ><%= text %></option><%
							} //end else
						} //end for
					} else if(!(request.getParameter("reload") != null)) {
						%>
						<option value='-1'>No dependency</option>
						<%
					} else if((request.getParameter("reload")).equals("-1")) {
						%>
						<option value='-1'>No dependency</option>
						<%
					} else {
						for(int k = 0; k < choices.length; k++ ) {
							String text;
							if(choices[k].length() > 48) {
								text = choices[k].substring(0,45) + "...";
							} else {
								text = choices[k];
							}
							%><option value='<%= k %>' ><%= text %></option><%						
						} //end for
					}
			      %>
			      </select>
		          </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' title='Click to cancel and return to the question gallery'/> 
          <input class='stdbutton' name='button' type='submit' value='Save' title='Click to create the question'/> 
          </td>
        </tr>

</form>
</js:border>