<%@ 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" />
<%

String location = request.getParameter("location");
String button = request.getParameter("button");
int questionnaireID = Integer.parseInt(request.getParameter("QuestionnaireID"));
Questionnaire editQ = DataAccess.retrieveQuestionnaire(questionnaireID);

if(location.equals("firstTab")) {
	String QIntro = request.getParameter("QIntro");
	String QName = request.getParameter("QName");
	editQ.setIntroText(QIntro);
	editQ.setQuestionnaireName(QName);
	DataAccess.storeQuestionnaire(editQ);
	
	if(button.equals("Save & Exit")) {
		response.sendRedirect("admin_main.jsp");
	}
	if(button.equals("Next")) {
		response.sendRedirect("admin_usergroup.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.equals("Add/remove usergroups")) {
		response.sendRedirect("admin_usergroup.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.equals("Activate")) {
		response.sendRedirect("admin_activate.jsp?QuestionnaireID=" + questionnaireID);
	}
}

if(location.equals("secondTab")) {

	if(button.equals("Save & Exit")) {
		response.sendRedirect("admin_main.jsp");
	}
	if(button.equals("Previous")) {
		response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.equals("Next")) {
		response.sendRedirect("admin_activate.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.equals("Edit questionnaire")) {
		response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.equals("Activate")) {
		response.sendRedirect("admin_activate.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.matches("Add.+")) {
		%>
		<c:forEach items="${paramValues.userGroups}" var="current">
			<%
			String current = (String)pageContext.getAttribute("current");
			int userGroupID = Integer.parseInt(current);
			UserGroup tempGroup = DataAccess.retrieveUserGroup(userGroupID);
			editQ.addUserGroup(tempGroup);
			%>
		</c:forEach> 
		<%
		DataAccess.storeQuestionnaire(editQ);
		response.sendRedirect("admin_usergroup.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.matches(".+Remove")) {
		%>
		<c:forEach items="${paramValues.attechedUserGroups}" var="current">
			<%
			String current = (String)pageContext.getAttribute("current");
			int userGroupID = Integer.parseInt(current);
			UserGroup tempGroup = DataAccess.retrieveUserGroup(userGroupID);
			editQ.removeUserGroup(tempGroup);
			%>
		</c:forEach> 
		<%
		DataAccess.storeQuestionnaire(editQ);
		response.sendRedirect("admin_usergroup.jsp?QuestionnaireID=" + questionnaireID);
	}
}

if(location.equals("thirdTab")) {
	try {
		String state = request.getParameter("state");
		if(state.equals("on")) {
			// the if is only to force a nullpointer exception for when
			// the "Active" checkmark has not been checked but to succed
			// if it is checked.
			editQ.setActive();
		} 
	} catch(NullPointerException e) {
		DataAccess.deleteEvaluationsForQuestionnaire(editQ.getQuestionnaireID());
		editQ.setInactive();
	}
	
	DataAccess.storeQuestionnaire(editQ);
	out.print(editQ.getState());
	if(button.equals("Save & Exit")) {
		response.sendRedirect("admin_main.jsp");
	}
	if(button.equals("Previous")) {
		response.sendRedirect("admin_usergroup.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.equals("Add/remove usergroups")) {
		response.sendRedirect("admin_usergroup.jsp?QuestionnaireID=" + questionnaireID);
	}
	if(button.equals("Edit questionnaire")) {
		response.sendRedirect("admin_questionnaire.jsp?QuestionnaireID=" + questionnaireID);
	}
}
out.print(button);
%>