<%@ 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 questionnaireID = Integer.parseInt(request.getParameter("QuestionnaireID"));
Questionnaire editQ = DataAccess.retrieveQuestionnaire(questionnaireID);
if(!editQ.getState()) {
	ArrayList editQPageRefs = editQ.getRefQuestionPages();
	QuestionPage editQPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)editQPageRefs.get(questionPage)).substring(1)));
	ArrayList qPageQRefs = editQPage.getRefQuestions();

	if(!qPageQRefs.isEmpty()) {
		QuestionPage mergePage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)editQPageRefs.get(questionPage-1)).substring(1)));
		for(int i = 0; i < qPageQRefs.size(); i++) {
			mergePage.addQuestion(DataAccess.retrieveQuestion(Integer.parseInt(((String)qPageQRefs.get(i)).substring(1))));
		}
		DataAccess.storeQuestionPage(mergePage);
	}

	editQ.removeQuestionPage(editQPage);
	DataAccess.storeQuestionnaire(editQ);
	DataAccess.deleteQuestionPage(editQPage.getQuestionPageID());
}
response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);
%>

