 <%@ taglib uri="/tags/struts-bean-el" prefix="bean-el" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html-el" %>
<%@ taglib uri="/tags/esr-core" prefix="esrcore" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/tags/fw" prefix="fw" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ page import='gov.va.med.esr.common.model.lookup.SourceDesignation' %>
<%@ page import="gov.va.med.esr.common.model.lookup.VAFacility" %>

<%@ taglib uri="/tags/authz" prefix="auth" %>
<c:set var="preferredFacilitySDConstant"><%= SourceDesignation.CODE_VISTA.getName() %></c:set>
<c:set var="preferredFacilityConstant"><%= VAFacility.CODE_HEC.getName() %></c:set>

<c:set var="requiredText"><bean-el:message key="label.required"/></c:set>

<script type="text/javascript">
// If a Preferred facility is changed then set the source designation to ESR
function changeSD( c ) {
    var list = document.getElementsByName("preferredFacilitySD"+c);
    list[0].options[1].selected=true;
    
    // Change the value in the hidden field
   list = document.getElementById("preferredFAcilitySD2"+c);
   list.value="E";
}

</script>

<tr>

	<td class="moduleLabel"><bean-el:message key="label.required.prefix" />&nbsp;
	     <bean-el:message key="label.personal.preferredFacility"/>:</td>
	
	<td class="moduleValue">
	<!-- Table containg the list and the buttons -->
		<table width="50%" border="0" cellpadding="0" cellspacing="1">
			<tr>
	            <c:set var="counter" value="-1"/>
				<td>
				<div style="overflow:auto;  ">
				<!--  table of preferred facilities -->
					<table border="0" cellspacing="4" cellpadding="0" size=100%">

                       <c:forEach  items="${demographicPersonalForm.preferredFacilitySDs}" var="sdStatus" varStatus="status" >
                          <c:set var="counter" value="${status.count - 1}"/>
                			  <tr>
             			     <td><html-el:hidden title="${pId} ${status.count}" property="preferredFacilityId[${counter}]" /></td>

								<c:choose>
									<c:when test="${sdStatus == 'V'}">
										<c:set var="pfSiteReadOnly" value="false"/>
										<c:set var="pfSite2ReadOnly" value="true"/>
										<c:set var="pfSDReadOnly" value="true"/>
										<c:set var="pfSD2ReadOnly" value="false"/>
									</c:when>
									<c:when test="${sdStatus == 'E'}">
										<c:set var="pfSiteReadOnly" value="false"/>
										<c:set var="pfSite2ReadOnly" value="true"/>
										<c:set var="pfSDReadOnly" value="true"/>
										<c:set var="pfSD2ReadOnly" value="false"/>
									</c:when>
									<c:when test="${sdStatus == 'PI'}">
										<c:set var="pfSiteReadOnly" value="false"/>
										<c:set var="pfSite2ReadOnly" value="true"/>
										<c:set var="pfSDReadOnly" value="true"/>
										<c:set var="pfSD2ReadOnly" value="false"/>
									</c:when>
									<c:when test="${sdStatus == 'PA'}">
										<c:set var="pfSiteReadOnly" value="true"/>
										<c:set var="pfSite2ReadOnly" value="false"/>
										<c:set var="pfSDReadOnly" value="true"/>
										<c:set var="pfSD2ReadOnly" value="false"/>
									</c:when>
									<c:otherwise>
										<c:set var="pfSiteReadOnly" value="false"/>
										<c:set var="pfSite2ReadOnly" value="true"/>
										<c:set var="pfSDReadOnly" value="true"/>
										<c:set var="pfSD2ReadOnly" value="false"/>										
 									</c:otherwise>
								</c:choose>
								
   					            <td >
                                    <c:set var="preferredFacilityText"><bean-el:message key="label.personal.preferredFacility"/></c:set>
                                    <fw:label property="preferredFacilitySite${counter}" forId="preferredFacilitySite${counter}"><span style="display:none">preferredFacility</span></fw:label>
 							         <html-el:select 
 							         	 title="${requiredText}, ${preferredFacilityText}" 
 							         	 styleClass="moduleEdit" 
	 							         styleId="preferredFacilitySite${counter}"
								         property="preferredFacilitySite[${counter}]" 
										 onchange="changeSD(${counter});"								    
								         disabled="${pfSiteReadOnly}">
							           <html-el:option value="">
							              <bean-el:message key="label.select"/>
							           </html-el:option>
 							           <esrcore:options collection="gov.va.med.esr.ui.common.beans.PreferredFacility" property="code" labelProperty="siteNumberAndName"/> 
							         </html-el:select>
                                    <html-el:hidden name="demographicPersonalForm" 
                                    	styleId="preferredFacilitySite2${counter}" 
                                    	property="preferredFacilitySite[${counter}]"
                                    	disabled="${pfSite2ReadOnly}"></html-el:hidden>
   					            </td>


  					            <td>
  					            <c:set var="preferredFacilitySDText"><bean-el:message key="label.personal.preferredFacilitySourceDesignation"/></c:set>
  					            	<fw:label property="preferredFacilitySD[${counter}]" forId="preferredFacilitySD${counter}"><span style="display:none">preferredFacilitySDText</span></fw:label>
								    <html-el:select
								    	title="${requiredText}, ${preferredFacilitySDText}" 
								    	styleId="preferredFacilitySD${counter}" 
								    	styleClass="moduleEdit" 
								        property="preferredFacilitySD[${counter}]" 
								        disabled="${pfSDReadOnly}">
								      <html-el:option value="">
								         <bean-el:message key="label.select"/>
								      </html-el:option>
								      <esrcore:options collection="gov.va.med.esr.common.model.lookup.SourceDesignation" property="code" labelProperty="name"/>
								    </html-el:select>
                                    <html-el:hidden name="demographicPersonalForm" 
                                    	styleId="preferredFacilitySD2${counter}" 
                                    	property="preferredFacilitySD[${counter}]"
                                    	disabled="${pfSD2ReadOnly}"></html-el:hidden>
							    </td>

    					    </tr>
				 	      </c:forEach>
	 					
					</table>
				 </div>
				</td>

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