<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<html>
<script src="../js/pswdStrength.js" type="text/javascript"></script>
<script>
	function update() {
		
		document.getElementById("cancelField").name = 'org.apache.struts.taglib.html.CANCEL';
		document.getElementById("form").submit();
	}
	
    function supportingStaff(){    	
    	enableUserBox();
    	var editsupportingStaff = document.getElementById("editsupportingStaff");
    	editsupportingStaff.value ="true";    	
    	var theUserForm = document.getElementById("form");
    	theUserForm.submit();    	
    }    
        function enableUserBox(){
    	var enabledCheckBox = document.getElementById("enabled");
    	if(enabledCheckBox){    		
    		enabledCheckBox.disabled =false;
    	}
    	saveValues('roleMember');
    }
    
    function processEnforcePassword(){   
        var chkValue =  document.getElementById("enforceStrongPassword").checked;	   	
	    var txtValue = document.getElementById("password1").value;   	    
	    if(chkValue == true){	    
	  		if(txtValue.length >0){
	  			 passwordStrength(txtValue);
	  		}	   
     	}
    }
    
</script>
<body onload="processEnforcePassword();">
<smvc:form modelAttribute="userForm" id="form" action="../referrals/userform.form">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<input type="hidden" id="containsStrongPw" name="containsStrongPw" value="<c:out value="${enforceStrngPwdVal}" />" />
	<input type="hidden" id="containsPwdExpire" name="containsPwdExpire" value="<c:out value="${passwordCanExpireVal}" />" />
	<input type="hidden" id="enforcePasswordHidden" name="enforcePasswordHidden" value="<c:out value="${enforceStrngPwdVal}" />" />
	<input type="hidden" id="pwdExpireHddn" name="pwdExpireHddn" value="false" />
	<input type="hidden" id="isPaswordChanged" name="isPaswordChanged" value="false"/>
	<input type="hidden" id="fromAdmin" name="fromAdmin" value="true" />
	<input type="hidden" name="cancelField" id="cancelField" value="true" />
	<input type="hidden" name="delete" id="delete" value="false" />
	<input type="hidden" name="userId" id="userId" value="<c:out value="${userForm.user.id}" />" />
	<smvc:hidden path="user.id"/>
	<input type="hidden" name="nopatientId" id="nopatientId" value="true" />
	<input type="hidden" name="editsupportingStaff" id="editsupportingStaff" value="false" />
	<input type="hidden" id="passwordMinimumLength" name="passwordMinimumLength" value="<c:out value="${passwordMinimumLength}" />" />
	<input type="hidden" id="passwordIncludeNumeric" name="passwordIncludeNumeric" value="<c:out value="${passwordIncludeNumeric}" />" />
	<input type="hidden" id="passwordIncludeUpperCase" name="passwordIncludeUpperCase" value="<c:out value="${passwordIncludeUpperCase}" />" />
	<input type="hidden" id="passwordIncludeSpecialChar" name="passwordIncludeSpecialChar" value="<c:out value="${passwordIncludeSpecialChar}" />" />
	<c:set var="ldapUser" value="${userForm.user.ldapUser}"/>
	<smvc:errors />
	<br />&nbsp;
	<c:if test="${ldapUser eq true}">
		 <span class="warning"> This user account is controlled from Windows Active Directory<br/> 
			and cannot be changed here: please ask your AD system administrator to make any changes.</span>
	</c:if>
	<smvc:hidden path="user.strCreationDate"/>
	
	<table align="center" width="460px">
		<tr>
			<td>
			<fieldset class="nobg"><legend>Edit User</legend>

				<c:if test="${msg ne null}">
					<span class="info"><c:out value="${msg}"/></span>
				</c:if>
				<c:if test="${userForm.errors ne null}">
					<span class="error"><c:out value="${userForm.errors}"/></span>
				</c:if>

				<table cellspacing="10" width="100%">
				<tr>
					<td valign="top">
					<fieldset>
					<table>
						<tr>
							<td class="label">Username <span class="mandatory">*</span></td>
							<td class="input">
							<sec:authorize access="hasRole('ADMIN')">
								<smvc:input path="user.username" />
							</sec:authorize> 
							<sec:authorize access="!hasRole('ADMIN')">
								<smvc:input path="user.username" disabled="true" />
							</sec:authorize></td>
						</tr>
						<sec:authorize access="hasRole('ADMIN')">
							<tr>
								<td class="label">Honorific Prefix</td>
								<td class="input"><webapp:selectAndEditList
									path="user.prefix" domain="webapp" list="userPrefix"
									includeEmpty="true"/></td>
							</tr>
						</sec:authorize>
						<tr>
							<td class="label">Forename</td>
							<td class="input"><smvc:input path="user.forename" /></td>
						</tr>
						<tr>
							<td class="label">Middle name(s)</td>
							<td class="input"><smvc:input path="user.middlename" /></td>
						</tr>
						<tr>
							<td class="label">Surname</td>
							<td class="input"><smvc:input path="user.surname" /></td>
						</tr>
						<sec:authorize access="hasRole('ADMIN')">
							<tr>
								<td class="label">Honorific Suffix</td>
								<td class="input"><webapp:selectAndEditList
									path="user.suffix" domain="webapp" list="userSuffix"
									includeEmpty="true"  /></td>
							</tr>
						</sec:authorize>
						<!--<tr>
                                            <td class="label">Organization Code</td>
                                            <td class="input">
                                                <webapp:selectFromList path="user.orgCode" domain="webapp" list="Organization Code" includeEmpty="true"/>
                                            </td>
                                        </tr>-->
						<sec:authorize access="hasRole('ADMIN')">
							<tr>
								<td class="label">Hospital</td>
								<td class="input">
									<smvc:select path="selectedHospital"  disabled="${ldapUser}"  >
										<smvc:option value="">select a hospital</smvc:option>
										<c:forEach var="hospital" items="${webapp.hospitalsList}">
											<smvc:option value="${hospital.id}" ><c:out value="${hospital.hospitalName }"/></smvc:option>
										</c:forEach>
									</smvc:select></td>
							</tr>
						</sec:authorize>
						<tr>
							<td class="label">Enabled </td>
							<td class="input"><sec:authorize access="hasRole('ADMIN')">
								<c:if test="${userForm.loginUserIsEditingUser eq 'yes' }">
									<smvc:checkbox path="user.enabled" id="enabled"
										disabled="true" />
								</c:if>
								<c:if test="${userForm.loginUserIsEditingUser ne 'yes' }">
									<smvc:checkbox path="user.enabled" id="enabled" disabled="${ldapUser}" />
								</c:if>
							</sec:authorize>
							</td>
						</tr>
						<c:choose>
							<c:when test="${enforceStrngPwdVal eq 'true'}">				
								<tr>
									<sec:authorize	access="hasRole('ADMIN')">
									<td class="label">Enforce Strong Password</td>
									<td class="input">
											<smvc:checkbox path="user.enabled" id="enforceStrongPassword" disabled="false" /></td>
									</sec:authorize> 
									<sec:authorize access="hasRole('ADMIN')">
									<td><smvc:checkbox path="user.enforceStrongPassword" cssStyle="display:none" id="enforceStrongPassword" disabled="true" value=""/></td>
									</sec:authorize>						
								</tr>
							</c:when>
							<c:otherwise><tr><td>
								<smvc:checkbox path="user.enforceStrongPassword"
								 id="enforceStrongPassword" disabled="true" cssStyle="display:none;" /></td></tr>
							</c:otherwise>
						</c:choose>
						<tr id="pwdStrength">
							<td class="label"><smvc:hidden path="pass1" id="password1"/></td>
							<td class="input"> <div id="passwordDescription" class="label"></div>
							</td>
						</tr>
						<tr>
							<td colspan="2">
							<hr />
							</td>
						</tr>
						<tr>
							<td class="label">Email <span class="mandatory">*</span></td>
							<td class="input"><smvc:input path="user.email" /></td>
						</tr>
					</table>
					</fieldset>
					</td>
					<td valign="top">&nbsp;</td>
				</tr>
			</table>
			<fieldset class="buttonFieldset">
			<input type="reset" />&nbsp; 
			<input type="button" value="Save" onclick="enableUserBox();"/>
			<c:if test="${!empty userForm.user.id and !ldapUser}">
				<sec:authorize access="hasRole('ADMIN')">
					<input type="button" value="Reset password" onclick="document.location.href ='userPassword.form'" />
				</sec:authorize>
				<sec:authorize access="!hasRole('ADMIN')">
					<c:if test="${userForm.user.id eq webapp.currentUser.id}">
						<input type="button" value="Change password" onclick="document.location.href ='userPassword.form'" />
					</c:if>
				</sec:authorize>
			</c:if>
			</fieldset>
			</fieldset>
			</td>
		</tr>
	</table>
</smvc:form>
</body>
</html>