<%@ 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"));
String type = request.getParameter("type");
Questionnaire editQ = DataAccess.retrieveQuestionnaire(questionnaireID);

if(questionPage == -1) {
	if(type.equals("F")) {
		QuestionFreeText newQ = new QuestionFreeText();
		newQ.setText("This is a new question, remember to edit it.");
		DataAccess.storeQuestion(newQ);	
		ArrayList editQPageRefs= editQ.getRefQuestionPages();
		QuestionPage QPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)editQPageRefs.get(0)).substring(1)));
		QPage.addQuestionAtPosition(newQ, 0);
		DataAccess.storeQuestionPage(QPage);
	}
	if(type.equals("M")) {
		QuestionMultipleChoice newQ = new QuestionMultipleChoice();	
		newQ.setText("This is a new question, remember to edit it.");
		String[] choices = new String[3];
		choices[0] = "Answer 1"; choices[1] = "Answer 2"; choices[2] = "Answer 3";
		newQ.setChoice(choices);
		DataAccess.storeQuestion(newQ);	
		ArrayList editQPageRefs= editQ.getRefQuestionPages();
		QuestionPage QPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)editQPageRefs.get(0)).substring(1)));
		QPage.addQuestionAtPosition(newQ, 0);
		DataAccess.storeQuestionPage(QPage);
	}

} else {
	if(type.equals("F")) {
		QuestionFreeText newQ = new QuestionFreeText();
		newQ.setText("This is a new question, remember to edit it.");
		DataAccess.storeQuestion(newQ);	
		
		ArrayList editQPageRefs= editQ.getRefQuestionPages();
		QuestionPage QPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)editQPageRefs.get(questionPage)).substring(1)));
		QPage.addQuestionAtPosition(newQ, questionNR+1);
		DataAccess.storeQuestionPage(QPage);

	}
	if(type.equals("M")) {
	
		QuestionMultipleChoice newQ = new QuestionMultipleChoice();	
		newQ.setText("This is a new question, remember to edit it.");
		String[] choices = new String[3];
		choices[0] = "Answer 1"; choices[1] = "Answer 2"; choices[2] = "Answer 3";
		newQ.setChoice(choices);
		DataAccess.storeQuestion(newQ);	
		
		ArrayList editQPageRefs= editQ.getRefQuestionPages();
		QuestionPage QPage = DataAccess.retrieveQuestionPage(Integer.parseInt(((String)editQPageRefs.get(questionPage)).substring(1)));
		QPage.addQuestionAtPosition(newQ, questionNR+1);
		DataAccess.storeQuestionPage(QPage);		

	}
}
response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);
%>