<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="js" uri="net.sourceforge.jsurveyor.jsurveyorlib" %>
<%@ page import="net.sourceforge.jsurveyor.*" %>
<%@ page import="java.util.ArrayList" %>
<js:noCache />
<js:login access="admin" />
<%
int questionnaireID = Integer.parseInt(request.getParameter("QuestionnaireID"));
Questionnaire editQ = DataAccess.retrieveQuestionnaire(questionnaireID);

if (!editQ.getState()) {

	int questionPageID = Integer.parseInt(request.getParameter("QuestionPageID"));
	int questionPageNR = Integer.parseInt(request.getParameter("questionPage"));
	int questionID = Integer.parseInt(request.getParameter("Question").substring(1));
	String QType = request.getParameter("Question").substring(0,1);
	int questionNR = Integer.parseInt(request.getParameter("question"));
	String direction = request.getParameter("direction");
	QuestionPage editPage = DataAccess.retrieveQuestionPage(questionPageID);

	if(direction.equals("up")) {
		if(questionNR != 0) {
			if(QType.equals("F")) {
				QuestionFreeText moveQ = (QuestionFreeText)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				editPage.addQuestionAtPosition(moveQ, questionNR-1);
				DataAccess.storeQuestionPage(editPage);
			}
			if(QType.equals("M")) {
				QuestionMultipleChoice moveQ = (QuestionMultipleChoice)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				editPage.addQuestionAtPosition(moveQ, questionNR-1);
				DataAccess.storeQuestionPage(editPage);	
			}	
		} else if(questionPageNR != 0){
			if(QType.equals("F")) {
				QuestionFreeText moveQ = (QuestionFreeText)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				DataAccess.storeQuestionPage(editPage);
				ArrayList pageList = editQ.getRefQuestionPages();
				QuestionPage pageDestination = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)pageList.get(questionPageNR-1)).substring(1)));
				pageDestination.addQuestion(moveQ);
				DataAccess.storeQuestionPage(pageDestination);
			}
			if(QType.equals("M")) {
				QuestionMultipleChoice moveQ = (QuestionMultipleChoice)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				DataAccess.storeQuestionPage(editPage);	
				ArrayList pageList = editQ.getRefQuestionPages();
				QuestionPage pageDestination = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)pageList.get(questionPageNR-1)).substring(1)));
				pageDestination.addQuestion(moveQ);
				DataAccess.storeQuestionPage(pageDestination);			
			}	
		}
	}
	if(direction.equals("down")) {
		if(questionNR != (editPage.getRefQuestions()).size()-1) {
			if(QType.equals("F")) {
				QuestionFreeText moveQ = (QuestionFreeText)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				editPage.addQuestionAtPosition(moveQ, questionNR+1);
				DataAccess.storeQuestionPage(editPage);
			}
			if(QType.equals("M")) {
				QuestionMultipleChoice moveQ = (QuestionMultipleChoice)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				editPage.addQuestionAtPosition(moveQ, questionNR+1);
				DataAccess.storeQuestionPage(editPage);	
			}	
		} else if(questionPageNR != (editQ.getRefQuestionPages()).size()-1){
			if(QType.equals("F")) {
				QuestionFreeText moveQ = (QuestionFreeText)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				DataAccess.storeQuestionPage(editPage);
				ArrayList pageList = editQ.getRefQuestionPages();
				QuestionPage pageDestination = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)pageList.get(questionPageNR+1)).substring(1)));
				pageDestination.addQuestionAtPosition(moveQ, 0);
				DataAccess.storeQuestionPage(pageDestination);
			}
			if(QType.equals("M")) {
				QuestionMultipleChoice moveQ = (QuestionMultipleChoice)DataAccess.retrieveQuestion(questionID);
				editPage.removeQuestion(moveQ);
				DataAccess.storeQuestionPage(editPage);	
				ArrayList pageList = editQ.getRefQuestionPages();
				QuestionPage pageDestination = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)pageList.get(questionPageNR+1)).substring(1)));
				pageDestination.addQuestionAtPosition(moveQ, 0);
				DataAccess.storeQuestionPage(pageDestination);			
			}	
		}
	}
}
response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);
%>