			<%@ 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="groupCatch" class="net.sourceforge.jsurveyor.UserGroup">
				<jsp:setProperty name="groupCatch" property="*" />
			</jsp:useBean>
			<js:noCache />
			<js:login access="root" />
			<js:border title="jSurveyor - Edit usergroup">
				<%

UserGroup editUserGroup;
//TODO: cleanup admin usergroup

if ( ((String) session.getAttribute("firstLoad")).equals("yes") ) {
	editUserGroup = DataAccess.retrieveUserGroup(groupCatch.getUserGroupID());
	session.setAttribute("firstLoad", "no");
	session.setAttribute("editUserGroup", editUserGroup);
} // end if
if ( request.getParameter("create") != null ) {
	editUserGroup = new UserGroup("New usergroup");
		
	if ( request.getParameter("ok") != null ) {
		editUserGroup.setGroupName(groupCatch.getGroupName());
		DataAccess.storeUserGroup(editUserGroup);
	}//end if
}//end else if
else { editUserGroup = (UserGroup) session.getAttribute("editUserGroup"); }

int c = 0;
if (request.getParameter("ok") != null) c=1;
if (request.getParameter("cancel") != null) c=2;
switch(c) {
	case 1: { 
		if ( editUserGroup.getRefParents().size() != 0 ) {
			String a = (String) editUserGroup.getRefParents().get(0);
			int parentID = Integer.parseInt(a.substring(1));
			UserGroup parent = DataAccess.retrieveUserGroup(parentID);
			editUserGroup.removeParent(parent);
			DataAccess.storeUserGroup(parent);
			DataAccess.storeUserGroup(editUserGroup);
		}

		if ( !request.getParameter("userGroups").equals("0") ) {
			UserGroup tmp = DataAccess.retrieveUserGroup(Integer.parseInt( request.getParameter("userGroups"))); 
			editUserGroup.addParent(tmp); 
			DataAccess.storeUserGroup(tmp);
		} // end if
		DataAccess.storeUserGroup(editUserGroup);
		
			
		response.sendRedirect("root_usergroup_list.jsp");
	} break; // end case

	case 2: response.sendRedirect("root_usergroup_list.jsp");
	break;
} // end switch

%>
				<form name='form' action='root_usergroup_edit.jsp' method='post'>
				<%
				if ( request.getParameter("create") != null ) {
				%>
				<input type='hidden' name='create' value='yes' />
				<%
				}
				%>			
				
				
					<tr>
						<td class='mainmenu_headline'>
							<%
										if ( request.getParameter("create") != null ) {
										%>
											Create usergroup
										<%
										}
										
										else {
										%>
											Edit usergroup
										<%
										}
										%>
						</td>
						<td class='mainmenu_headline_emptyfield'>
						</td>
					</tr>
					<tr>
						<td colspan='2' class='mainmenu_blueframe'>
							<table border='0' cellspacing='0' cellpadding='0'>
								<tr>
									<td class='mainmenu_inner_workframe' colspan='3'>
										Group name:
										<b>
										<input type='text' class='textinput' title='Type the name of the usergroup' name='groupName' value='<%=editUserGroup.getGroupName() %>' />
											<br />
										</td>
									</tr>
												<tr>
													<td class='mainmenu_workframe' align='center' style='width:45%;'>
														Select parent usergroup
													</td>
													<td class='mainmenu_workframe' >
													</td>
													<td class='mainmenu_workframe'>
													</td>
												</tr>
												<tr>
													<td class='mainmenu_workframe' align='center'>
														<select name='userGroups' size='1' style='width:90%;'>
														<option value='0' title='Select the parent to this usergroup here'>No parent</option>
															<%
            if (request.getParameter("create") != null) {
	            ArrayList userGroups = DataAccess.retrieveAllUserGroups();
				for(int i = 0; i < userGroups.size(); i++) {
	            	UserGroup tempUserGroup = (UserGroup)userGroups.get(i);
	            //  out.print("<option value='" + tempUserGroup.getUserGroupID() + "'>" + tempUserGroup.getGroupName() + " Refchildren: " + tempUserGroup.getRefChildren() + " Refparents: " + tempUserGroup.getRefParents() + "</option>");
	               	if ( tempUserGroup.getUserGroupID() != editUserGroup.getUserGroupID() ) {
		               	out.print("<option value='" + tempUserGroup.getUserGroupID() + "'>" + tempUserGroup.getGroupName()  + "</option>");
		             } //end if
	            } // end for
            } //end if
            else {
            	//init tmp2 as int - to avoid out of scope
            	int tmp2;
            	
            	if ( editUserGroup.getRefParents().size() != 0 ) {
            		String tmp = ( (String) editUserGroup.getRefParents().get(0) ).substring(1) ;
            		tmp2 = Integer.parseInt(tmp);
            	} // end if
            	
            	else {
            		tmp2 = 0;
            	} // end else
            	
                        	
	            ArrayList userGroups = DataAccess.retrieveAllUserGroups();
				for(int i = 0; i < userGroups.size(); i++) {
	            	UserGroup tempUserGroup = (UserGroup)userGroups.get(i);
	            //  out.print("<option value='" + tempUserGroup.getUserGroupID() + "'>" + tempUserGroup.getGroupName() + " Refchildren: " + tempUserGroup.getRefChildren() + " Refparents: " + tempUserGroup.getRefParents() + "</option>");
	               	if ( tempUserGroup.getUserGroupID() != editUserGroup.getUserGroupID() ) {
		               	if ( tempUserGroup.getUserGroupID() == tmp2 ) {
		               		out.print("<option value='" + tempUserGroup.getUserGroupID() + "' selected='selected'>" + tempUserGroup.getGroupName()  + "</option>");
		               	} // end if
		               	else {
		               		out.print("<option value='" + tempUserGroup.getUserGroupID() + "'>" + tempUserGroup.getGroupName()  + "</option>");
		               	} //end else
		             } //end if
	            } // end for
	            
            } //end else
            %>
														</select>
													</td>
													<td colspan='2'>
													</td>
												</tr>
												
												
												
												
									<tr>
										<td>
											<table>
												<tr>
													<td>
														<!-- Makes space between blue border and top of buttons -->
													</td>
												</tr>
											</table>
										</td>
									</tr>
									<tr>
										<td>
										</td>
										<td class='mainmenu_buttonline'>
											<input class='stdbutton' type='submit' name='ok' title='Click to create this usergroup' value='Ok'/>
											<input class='stdbutton' name='cancel' title='Click to cancel creating this usergroup' type='submit' value='Cancel'/>
										</td>
									</tr>
									</table>
									</td>
									</tr>
								</js:border>