<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<script>
	//<!--
	function update() {
		
		document.getElementById("cancelField").name = 'org.apache.struts.taglib.html.CANCEL';
		document.getElementById("form").submit();
	}
	
	function deleteUser() {
	   var confirmed;
	   confirmed = confirm("Are you sure you want to delete the user?");
       if(confirmed){
           document.getElementById("delete").value = "true"; 
           return true;            
       }
       else{
          return false;
       }
    }

    function saveSupportingStaff(){
    	var ssidBox = document.getElementById("supportingStaffIds");
    	var ssIdLength = ssidBox.length;
    	for(var i=0; i<ssIdLength; i++){
    		var suppStaff = ssidBox[i];
    		suppStaff.selected = true;
    	}
    }
    function resetSupportingStaff(){
    	var resetSupportingStaffLocation= document.getElementById("resetSupportingStaffLocation").value;
    	document.location=resetSupportingStaffLocation;
    
    }
  </script>
   	<input type="hidden" id="resetSupportingStaffLocation" value="../admin/supportingStaff.form?userId=<c:out value="${currentUser.id}"/>"/>
		<c:choose>
		    <c:when test="${isCurrentUser == true}">
		    	<c:set var="messageString"  value="The following users can access all patients to which you have access:"/>
		    </c:when>
		    <c:otherwise>
		    	<c:choose>
					<c:when test="${!empty (fn:trim(currentUser.fullName))}">
						<c:set var="userFullName" value="${currentUser.forename} ${currentUser.surname}" />
					</c:when>
					<c:otherwise>
						<c:set var="userFullName" />
					</c:otherwise>
				</c:choose>
		    	<c:set var="messageString"  value="The following users can access all patients to which ${userFullName} (${currentUser.username})  has access:"/>
		    </c:otherwise>
	    </c:choose>

	
<smvc:form action="../admin/supportingStaff.form" id="form" modelAttribute="supportingStaffForm">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<input type="hidden" name="cancelField" id="cancelField" value="true"/>
	<input type="hidden" name="delete" id="delete" value="false"/>
	<table>
		<tr>
			<td>
				<fieldset class="nobg">
					<legend>Clinical Support Staff</legend>
					
					<div class="message"><c:out value="${msg}"/></div>
					<c:if test="${staffInfoSaved == true }">
						<span class="info">Support Staff Information has been saved.</span>
					</c:if>
					<table cellspacing="10" width="100%">
						<tr>
							<td valign="top">
								<fieldset>
									<table>	
										<tr>
											<td class="label">Username : </td>
											
											<td class="label"> &nbsp;&nbsp;&nbsp;&nbsp;</td>
											<td class="input">
													<smvc:input path="user.username" disabled="true"/>
											</td>
										</tr>					
									</table>
								</fieldset>
							</td>
						</tr>
						<tr>
							<td valign="top">							
								<fieldset>
									<c:out value="${messageString}"/>
								</fieldset>
							</td>
						</tr>		
						<tr>
							<td>
							<fieldset>
								<table>
								
									<tr>
										<th>
											<B> USERS LIST </B>
										</th>
										<th>
											&nbsp;
										</th>
										<th>
											<B> SUPPORT STAFF </B>
										</th>
									</tr>
									<tr>
										<td>
											<smvc:select path="allUserIds" id = "allUserIds" multiple="true"  cssStyle="width:300px; height: auto" size="10">												
												<c:forEach var="user" items="${allUsers}" varStatus="status">
													<c:choose>
														<c:when test="${!empty (fn:trim(user.fullName))}">
															<c:set var="userFullName" >(${user.forename} ${user.surname})</c:set>
														</c:when>
														<c:otherwise>
															<c:set var="userFullName" />
														</c:otherwise>
													</c:choose>
													<smvc:option value="${user.id}"><c:out value="${user.username}"/> <c:out value="${userFullName}"/></smvc:option>	
												</c:forEach>
											</smvc:select>
											<script>										
	 											sortOptions(document.all('allUserIds'));
											</script>	
										</td>
										<td>
											<table>
												<tr>
													<td align="center">
														<input type="button" value="   >>  Add  >>  " 
															onclick = 
															"fnMoveItems('allUserIds','supportingStaffIds')"/>
													</td>
												</tr>
												<tr>
													<td align="center">
														&nbsp;
													</td>
												</tr>												
												<tr>
													<td align="center">
														<input type="button" value=" <<  Remove  <<" 
														onclick="fnMoveItems('supportingStaffIds','allUserIds')"/>
													</td>
												</tr>
											</table>
										</td>
										<td>										
											<smvc:select path="supportingStaffIds" id = "supportingStaffIds" multiple="true" cssStyle="width:300px; height: auto" size="10" >
												<c:forEach var="user" items="${supportingStaffs}" varStatus="status">
													<c:choose>
														<c:when test="${!empty (fn:trim(user.fullName))}">
															<c:set var="userFullName" >(${user.forename} ${user.surname})</c:set>
														</c:when>
														<c:otherwise>
															<c:set var="userFullName" />
														</c:otherwise>
													</c:choose>
													<smvc:option value="${user.id}"><c:out value="${user.username}"/> <c:out value="${userFullName}"/></smvc:option>	
												</c:forEach>
											</smvc:select>	
											
											<script>										
	 											sortOptions(document.all('supportingStaffIds'));
											</script>	
										</td>
									</tr>
								</table>
								</fieldset>							
							</td>
						</tr>
					</table>
					<fieldset class="buttonFieldset">
						<input type="button" value="Reset" onclick="return resetSupportingStaff();"/>
						&nbsp; 
						<input type="submit" name="action" onclick="return saveSupportingStaff();" value="Save" />
					</fieldset>

				</fieldset>
			</td>
		</tr>	
	</table>
</smvc:form>