<%@ 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.*" %>
<jsp:useBean id="Data" class="net.sourceforge.jsurveyor.DataAccess">
</jsp:useBean>
<js:noCache />
<js:login access="admin" />
<js:border title="jSurveyor - Questionnaires">
<form action='admin_save_redirect.jsp' method='post'>
<input name='location' type='hidden' value='firstTab' />
<%
Questionnaire editQ = new Questionnaire();
%>
<c:forEach items="${paramValues.QuestionnaireID}" var="current">
		<%
		String current = (String)pageContext.getAttribute("current");
		editQ = DataAccess.retrieveQuestionnaire(Integer.parseInt(current));
		%>
</c:forEach>
 
<input name='QuestionnaireID' type='hidden' value='<%= editQ.getQuestionnaireID() %>' />
	 <tr>
	  <td class='mainmenu_headline' title='Here you can edit this questionnaire'>Edit questionnaire</td>
	  <td class='mainmenu_headline_unselected_rightborder'><input class='tab' name='button' type='submit' value='Add/remove usergroups' title='Here you can add and remove usergroups' /><!-- <a class='tab' href='admin_usergroup.jsp' title='Here you can add and remove usergroups'>Add/remove usergroups</a> --></td>
	  <td class='mainmenu_headline_unselected'><input class='tab' name='button' type='submit' value='Activate' title='Here you can set the status of the questionnaire to active' /><!-- <a class='tab' href='admin_activate.jsp' title='Here you can set the status of the questionnaire to active'>Activate</a> --></td>
	<td class='mainmenu_headline_emptyfield'>&nbsp;</td>
	 </tr>
	
	<tr>
	  <td colspan='4' class='mainmenu_blueframe'>
	    <table border='0' cellspacing='0' cellpadding='0'>
		  <tr>
		  <td class='mainmenu_inner_workframe'> <!-- Main workframe start -->
	        <table border='0' cellspacing='0' cellpadding='0'> 
			<%
				boolean state = editQ.getState();
				if(state) {
			%>
			<tr>
				<td class='mainmenu_spacer' colspan='3' align='center'><font color='red'>
				  This questionnaire is active, if you wish to edit anything, deactivate it first.<br />
				  All edit requests will be ignored.
				  </font>
				</td>
			</tr>
			<%
			}
			%>	
			<tr><td class='admin_questionnaire_fieldspacer'><b>Name:</b></td></tr>
			<tr><td class='admin_questionnaire_fieldspacer'><input type='text' class='textinput' size='90' name='QName' title='Type the name of this questionnaire here' value='<%= editQ.getQuestionnaireName() %>' /></td></tr>
			<tr><td class='admin_questionnaire_fieldspacer'><b>Introduction to the questionnarie:</b></td></tr>
			<tr><td class='admin_questionnaire_fieldspacer'><textarea name='QIntro' cols='145' rows='10' title='Type an introduction to the questionnaire here'><%= editQ.getIntroText() %></textarea></td></tr>
			
			<tr><td align='center'>
			<input class='stdbutton_long' type='button' value='Insert Question' title='Click to insert a new question here' onClick="parent.location='admin_gallery_types.jsp?questionPage=-1&question=-1&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
			<input class='stdbutton_long' type='button' value='Insert Pagebreak' title='Click to insert a pagebreak here' onClick="parent.location='admin_insert_pagebreak.jsp?questionPage=-1&question=-1&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
			</td>
			</tr>
			</table>
			<table>
<!--			<tr>
			  <td class='admin_questionnaire_fieldspacer'></td>
			</tr> -->
			</table>
			<%
			ArrayList qPages = editQ.getRefQuestionPages();
			int questionNR = 0;
			Hashtable questionHash = new Hashtable();
			for(int i = 0; i < qPages.size(); i++) {
				String tempQPageRef = (String)qPages.get(i);
				QuestionPage tempQPage = DataAccess.retrieveQuestionPage(Integer.parseInt(tempQPageRef.substring(1)));
				ArrayList questions = tempQPage.getRefQuestions();
				if(i > 0) {
				%>
				
				<table border='0'>
					<tr>
						<td style='width:15%; border-bottom:4px dotted #000000;'>&nbsp;
						</td>
						<td rowspan='2' class='centeralign'>
							<b>Pagebreak</b>
						</td>
						<td style='width:15%; border-bottom:4px dotted #000000;'>&nbsp;
						</td>
						<td rowspan='2' class='centeralign'>
							<input type='button' name='button' class='stdbutton_long' value='Delete pagebreak'  title='Click to delete this pagebreak' onClick="parent.location='admin_delete_pagebreak.jsp?questionPage=<%= i %>&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
						</td>
						<td style='width:15%; border-bottom:4px dotted #000000;'>&nbsp;
						</td>
						<td rowspan='2' class='centeralign' valign='center'>
							<b>Pagebreak</b>
						</td>
						
						<td style='width:15%; border-bottom:4px dotted #000000;'>&nbsp;
						</td>
					</tr>
					<tr>
						<td>&nbsp;
						</td>
						<td>&nbsp;
						</td>
						
						<td>&nbsp;
						</td>
						<td>&nbsp;
						</td>
					</tr>
				</table>	
				
				
				
			<table> <!-- spacer and buttons begin -->
			<tr>
			  <td align='center'>
								<input class='stdbutton_long' type='button' value='Insert Question' title='Click to insert a new question here' onClick="parent.location='admin_gallery_types.jsp?questionPage=<%= i %>&question=-1&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
			  </td>
			</tr>
			<tr>
			  <td class='admin_questionnaire_fieldspacer'></td>
			</tr>
			</table> <!-- spacer and buttons begin -->
				
				<%
				}
				for(int j = 0; j < questions.size(); j++) {
					String tempQRef = (String)questions.get(j);
					if(tempQRef.substring(0,1).equals("M")) {
						questionNR++;
				 		QuestionMultipleChoice tempQ = (QuestionMultipleChoice)DataAccess.retrieveQuestion(Integer.parseInt(tempQRef.substring(1)));
				 		questionHash.put("" + tempQ.getQuestionID(), new Integer(questionNR));
						%>
			<table border='0' cellspacing='0' cellpadding='0'> <!-- Questionsection start (for autogenerate in JSP). This is a multiple choice -->
			  <tr>
				<td class='mainmenu_inner_workframe_with_blueborder'>
				  <table border='0'>
					<tr>
					  <td style='padding-right:30px;'>
						<b>Question <%= questionNR %>: </b><%= tempQ.getText()%>
					  </td>
					  <td style='width:30%;' align='center'><b>
					  <%
					  if(tempQ.getDepends()) {
					  ArrayList dependency = tempQ.getDependency();
  					  String dependQ = ((String)dependency.get(0)).substring(1);
   					  String dependIntegerSet = (String)dependency.get(1);
   					  String[] dependIntegers = dependIntegerSet.split(";");
					  int dependQID = Integer.parseInt(dependQ);
					  Integer qNR = (Integer)questionHash.get(dependQ);
					  QuestionMultipleChoice dependQuestion = (QuestionMultipleChoice)DataAccess.retrieveQuestion(dependQID);
					  String[] dependChoices = dependQuestion.getChoice();
						


					  %>
					  	Will be shown if the answer to question <%= qNR %> is:<br />
					  	<%
					  	for(int u = 0; u < dependIntegers.length; u++) { 
					  	%>
					  	<%= dependChoices[Integer.parseInt(dependIntegers[u])] %>&nbsp;
					  	
					  	<%
					  		}
					  	}
					  	%>
					  	</b></td>
					</tr>
					<tr>
					  <td align='center' colspan='2'>
					    <table border='0'>
						  <tr>
						    <td rowspan='2' class='centeralign' valign='middle'>
							  <table border='0'>
									<tr>
								  <%
								  String[] choices = tempQ.getChoice();
								  for(int k = 0; k < choices.length; k++ ) {
								  
  								  if(k != 0 && k%5 == 0) { 
  								  	%>
  								  	<tr>
  								  	<% 
  								  	} 
  								  %>
  								  <td class='centeralign' style='padding-bottom:5px;'>
								  <input type='radio' name='radiobutton' value='radio1' /><br /><%= choices[k] %>
								  </td>
								  <%
								  if(k != 0 && k+1%5 == 0) { 
  								  	%>
  								  	</tr>
  								  	<% 
  								  	}
								  }
								  %>
							
								</tr>
							  </table>
							</td>
							<td align='right' valign='middle' rowspan='2' style='width:165px;'>
							  <input type='button' class='stdbutton' value='Edit' title='Click to edit this question' onClick="parent.location='admin_multiplechoice1.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ.getQuestionID()%>'"/>
							  <input type='button' class='stdbutton' value='Delete' title='Click to delete this question' onClick="parent.location='admin_delete_question.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ %>'"/>
							</td>
							<td align='left' style='width:30px;'>
							  <table>
								<tr>
								  <td>
								    <input type='button' class='minibutton' value='&uarr;' title='Click to move this question one position upwards' onClick="parent.location='admin_move_question.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ %>&direction=up&questionPage=<%= i %>&question=<%= j %>'"/>
								  </td>
								</tr>
								<tr>
								  <td>
								    <input type='button' class='minibutton' value='&darr;'  title='Click to move this question one position downwards' onClick="parent.location='admin_move_question.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ %>&direction=down&questionPage=<%= i %>&question=<%= j %>'"/>
								  </td>
								</tr>
							  </table>
							</td>
						  </tr>
						</table>
					  </td>
					</tr>
				  </table>
				</td>
			  </tr>
			</table> <!-- Questionsection end (for autogenerate in JSP) -->

			<table> <!-- spacer and buttons begin -->
			<tr>
			  <td class='admin_questionnaire_fieldspacer'></td>
			</tr>
			<tr>
			  <td align='center'>
				<input class='stdbutton_long' type='button' value='Insert Question' title='Click to insert a new question here' onClick="parent.location='admin_gallery_types.jsp?questionPage=<%= i %>&question=<%= j %>&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
				
				<% if(j < questions.size()-1 || i == qPages.size()-1) {
				%>
				<input class='stdbutton_long' type='button' value='Insert Pagebreak' title='Click to insert a pagebreak here' onClick="parent.location='admin_insert_pagebreak.jsp?questionPage=<%= i %>&question=<%= j %>&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
				
				<%
				}
				%>
				</td>
			</tr>
				<% if(j < questions.size()-1 || i == qPages.size()-1) {
				%>
			<tr>
			  <td class='admin_questionnaire_fieldspacer'></td>
			</tr>
				
				<%
				}
				%>
			
			</table> <!-- spacer and buttons begin -->
						
						<%
					}
					if(tempQRef.substring(0,1).equals("F")) {
						questionNR++;
				 		QuestionFreeText tempQ = (QuestionFreeText)DataAccess.retrieveQuestion(Integer.parseInt(tempQRef.substring(1)));
				 		questionHash.put("" + tempQ.getQuestionID(), new Integer(questionNR));
						%>
			<table border='0' cellspacing='0' cellpadding='0'>
			  <tr>
				<td class='mainmenu_inner_workframe_with_blueborder'>
				  <table border='0'>
					<tr>
					  <td>
						<b>Question <%= questionNR %>: </b><%= tempQ.getText() %>
					  </td>
					  <td style='width:30%;' align='center'><b>
					  <%
					  if(tempQ.getDepends()) {
					  ArrayList dependency = tempQ.getDependency();
  					  String dependQ = ((String)dependency.get(0)).substring(1);
   					  String dependIntegerSet = (String)dependency.get(1);
   					  String[] dependIntegers = dependIntegerSet.split(";");
					  int dependQID = Integer.parseInt(dependQ);
					  Integer qNR = (Integer)questionHash.get(dependQ);
					  QuestionMultipleChoice dependQuestion = (QuestionMultipleChoice)DataAccess.retrieveQuestion(dependQID);
					  String[] dependChoices = dependQuestion.getChoice();
						


					  %>
					  	Will be shown if the answer to question <%= qNR %> is:<br />
					  	<%
					  	for(int u = 0; u < dependIntegers.length; u++) { 
					  	%>
					  	<%= dependChoices[Integer.parseInt(dependIntegers[u])] %>&nbsp;
					  	
					  	<%
					  		}
					  	}
					  	%>
					  </b></td>
					</tr>
					<tr>
					  <td align='center' colspan='2'>
					    <table border='0'>
						  <tr>
						    <td rowspan='2' align='center' valign='middle'>
							  <table border='0'>
								<tr>
								  <td class='centeralign'>
								    <textarea name='FreetextQ1' cols='60' rows='2' readonly='readonly'>This is where a user can write his answer.</textarea>
								  </td>
								</tr>
							  </table>
							</td>
							<td align='right' valign='middle' rowspan='2' style='width:165px;'>
							  <input type='button' class='stdbutton' value='Edit' title='Click to edit this question' onClick="parent.location='admin_freetext.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ.getQuestionID()%>'"/>
							  <input type='button' class='stdbutton' value='Delete' title='Click to delete this question' onClick="parent.location='admin_delete_question.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ %>'"/>
							</td>
							<td align='left' style='width:30px;'>
							  <table>
								<tr>
								  <td>
								    <input type='button' class='minibutton' value='&uarr;' title='Click to move this question one position upwards' onClick="parent.location='admin_move_question.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ %>&direction=up&questionPage=<%= i %>&question=<%= j %>'"/>
								  </td>
								</tr>
								<tr>
								  <td>
								    <input type='button' class='minibutton' value='&darr;' title='Click to move this question one position downwards' onClick="parent.location='admin_move_question.jsp?QuestionnaireID=<%= editQ.getQuestionnaireID() %>&QuestionPageID=<%= tempQPage.getQuestionPageID() %>&Question=<%= tempQ %>&direction=down&questionPage=<%= i %>&question=<%= j %>'"/>
								  </td>
								</tr>
							  </table>
							</td>
						  </tr>
						</table>
					  </td>
					</tr>
				  </table>
				</td>
			  </tr>
			</table> <!-- Questionsection end (for autogenerate in JSP) -->

			<table> <!-- spacer and buttons begin -->
			<tr>
			  <td class='admin_questionnaire_fieldspacer'></td>
			</tr>
			<tr>
			  <td align='center'>
								<input class='stdbutton_long' type='button' value='Insert Question' title='Click to insert a new question here' onClick="parent.location='admin_gallery_types.jsp?questionPage=<%= i %>&question=<%= j %>&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
				<% if(j < questions.size()-1 || i == qPages.size()-1) {
				%>
								<input class='stdbutton_long' type='button' value='Insert Pagebreak' title='Click to insert a pagebreak here' onClick="parent.location='admin_insert_pagebreak.jsp?questionPage=<%= i %>&question=<%= j %>&QuestionnaireID=<%= editQ.getQuestionnaireID() %>'"/>
				<%
				}
				%>
				</td>
			</tr>
			<% if(j < questions.size()-1 || i == qPages.size()-1) {
				%>
				<tr>
			  <td class='admin_questionnaire_fieldspacer'></td>
			</tr>
				<%
				}
				%>

			</table> <!-- spacer and buttons begin -->
						
						<%
					}
				}
			}	
			%>

		  </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 class='mainmenu_buttonline' colspan='4'><input class='stdbutton' name='button' type='submit' title='Move on to add/remove usergroups' value='Next'/> <input class='stdbutton' name='button' type='submit' title='Save this questionnaire and return to Questionnaire list page' value='Save & Exit' />
      </td>
    </tr>
</form>
</js:border>