<%@ 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 questionPage = Integer.parseInt(request.getParameter("questionPage"));
int questionNR = Integer.parseInt(request.getParameter("question"));
int questionnaireID = Integer.parseInt(request.getParameter("QuestionnaireID"));
Questionnaire editQ = DataAccess.retrieveQuestionnaire(questionnaireID);
if(!editQ.getState()) {
	if(questionPage == -1) {
		QuestionPage newQPage = new QuestionPage();
		DataAccess.storeQuestionPage(newQPage);
		editQ.addQuestionPageAtPosition(newQPage, 0);
		DataAccess.storeQuestionnaire(editQ);
	} else {
		QuestionPage newQPage = new QuestionPage();
		DataAccess.storeQuestionPage(newQPage);
		editQ.addQuestionPageAtPosition(newQPage, questionPage+1);
		DataAccess.storeQuestionnaire(editQ);
		ArrayList editQPageRefs= editQ.getRefQuestionPages();
		QuestionPage oldQPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)editQPageRefs.get(questionPage)).substring(1)));
		ArrayList oldQPageQRefs = oldQPage.getRefQuestions();
		int tempInt;
		for(int i = oldQPageQRefs.size()-1; i > questionNR; i--) {
			tempInt = Integer.parseInt(((String)oldQPageQRefs.get(i)).substring(1));
			newQPage.addQuestionAtPosition(DataAccess.retrieveQuestion(tempInt), 0);
			oldQPage.removeQuestion(DataAccess.retrieveQuestion(tempInt));
		}
		DataAccess.storeQuestionPage(newQPage);
		DataAccess.storeQuestionPage(oldQPage);
	}
}
response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);

%>