<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="js" uri="net.sourceforge.jsurveyor.jsurveyorlib" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sourceforge.jsurveyor.*" %>
<jsp:useBean id="Data" class="net.sourceforge.jsurveyor.DataAccess">
</jsp:useBean>
<js:noCache />
<js:login access="admin" />
<js:border title="jSurveyor - Questionnaires">
<form action='admin_redirect.jsp' method='post'>
   <c:if test="${errorPosition == 'questionnaire'}">
	  <tr>
	<td class='mainmenu_spacer' colspan='3' align='center'><font color='red'>
	  <c:out value="${error}" />
	  </font>
	  </td>
	  </tr>
	  </c:if>
	<tr>
	  <td class='mainmenu_headline'>Questionnaires</td>
	  <td class='mainmenu_headline_emptyfield'>&nbsp;</td>
	</tr>


	<tr>
	  <td colspan='2' class='mainmenu_blueframe'>
	    <script language='javascript' type='text/javascript'>
		function checkall( elem ){
			var form = document.form;
			for( i  =0; i<form.length; i++ ){
				if( form.elements[ i ].type == 'checkbox' ) {
					form.elements[ i ].checked = elem.checked;
				} //end if
			} //end for
		} //end function
		</script>	
	    <table border='0' cellspacing='0' cellpadding='0'>
		  <tr>
		  <td class='mainmenu_workframe'> <!-- Main workframe start -->
	        <table border='0' cellspacing='0' cellpadding='2'><!-- cellpadding='2' might fuck it up --> 
			  <tr>
			    <td class='title'><input type='checkbox' name='questionnaireID' title='Check this box to select all questionnaires' onClick='checkall(this)'/></td>
   			    <td class='title'>Title</td>
			    <td class='title'>Attached usergroups</td>
			    <td class='title' style='text-align:center;'>Status</td>
			    <td class='title'>Change status</td>
			  </tr>
			<%
    	        ArrayList questionnaires = Data.retrieveQuestionnairesForOwner((User)session.getAttribute("userInfo"));
        	    String tdClass = "body_odd";
           		for(int i = 0; i < questionnaires.size(); i++) {
            		if ( i%2 == 0 ) {
            			tdClass = "body_even";
	            	}
    	        	else {
        	    		tdClass = "body_odd";
            		}
            		Questionnaire tempQuestionnaire = (Questionnaire)questionnaires.get(i);
				
            		ArrayList tempUserGroups = tempQuestionnaire.getRefUserGroups();
            		String printUserGroups = "No usergroups attached";
            		for(int j = 0; j < tempUserGroups.size(); j++) {
            			UserGroup tempUG = Data.retrieveUserGroup(Integer.parseInt(((String)tempUserGroups.get(j)).substring(1)));
            			if(j == 0) { 
            				printUserGroups = tempUG.getGroupName();
            			} else {
            				printUserGroups = printUserGroups + ", " + tempUG.getGroupName();
            			}            			
            		}
		    %>
					<tr>
					    <td class='<%= tdClass %>'<input type='checkbox' name='QuestionnaireID' title='Check this box in order to edit or delete this questionnaire, using the buttons below' value='<%= tempQuestionnaire.getQuestionnaireID() %>' /></td>
					    <td class='<%= tdClass %>'><a href='admin_questionnaire.jsp?QuestionnaireID=<%= tempQuestionnaire.getQuestionnaireID() %>' title='Click here to edit this questionnaire'><%= tempQuestionnaire.getQuestionnaireName() %></a>  <small>(<a href='servlet/ReportOutput?questionnaireID=<%= tempQuestionnaire.getQuestionnaireID() %>' target='_new' title='Click here to show the replies to this questionnaire'>Show replies</a>)</small></td>
						<td class='<%= tdClass %>'><%= printUserGroups %></td>
			<%
				if(tempQuestionnaire.getState()) {
			%>
					<td class='<%= tdClass %>' style='text-align:center;'><img src='pics/green_dot.png' align='top' alt='This questionnaire is active' title='This questionnaire is active' border='0' /></td>
					<td class='<%= tdClass %>' style='text-align:center;'><input class='activation' type='button' title='Click to deactivate this questionnaire' value='Deactivate' onClick="location.href='admin_redirect.jsp?button=Deactivate&QuestionnaireID=<%= tempQuestionnaire.getQuestionnaireID() %>'"/></td>
			<%
				} else {
			%>
					<td class='<%= tdClass %>' style='text-align:center;'><img src='pics/red_dot.png' align='top' alt='This questionnaire is not active' title='This questionnaire is not active' border='0' /></td>
			    	<td class='<%= tdClass %>' style='text-align:center;'><input class='activation' type='button' title='Click to activate this questionnaire' value='Activate' onClick="location.href='admin_redirect.jsp?button=Activate&QuestionnaireID=<%= tempQuestionnaire.getQuestionnaireID() %>'"/></td>
			<%
				}
			%>
					</tr>
			<%
        		}
           	%>

			</table>
		  </td> 
		  </tr>
	    </table>
      </td>
	</tr>

	<tr>
	<td>
	  <table border='0'><tr><td> <!-- Makes space between blue border and top of buttons -->
	  </td></tr>
	  </table>
	</td>
	</tr>
	
	
	<tr>
	  <td colspan='2'><input class='stdbutton' name='button' type='submit' value='Delete' title='Click here to delete selected questionnaires'/> <input class='stdbutton' name='button' type='submit' value='Edit' title='Click here to edit the selected questionnaire'/> <input class='stdbutton' name='button' type='button' title='Click here to create a new questionnaire' value='Create' onClick="location.href='admin_new_questionnaire.jsp?userName=<%= ((User)session.getAttribute("userInfo")).getUserName() %>'"/></td>
<!--	  <td class='mainmenu_buttonline'>&nbsp;</td> -->
	</tr>
 </form>
 <% 
 session.removeAttribute("error");
 session.removeAttribute("errorPosition");
 %>
</js:border>