<%@page import="com.ardeo.entities.Hospitals"%>
<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<script type="text/javascript">
function processEditDelete(id) {
	var isProceed  = confirm("Are you sure you want to delete this Meeting Schedule?");
	if (isProceed) {
	    document.location.href="../meetings/meetingSchedule.form?actionRequired=delete&meetingScheduleId="+id;
    }else{
        return false;
     }
}
function validateSchedule(){
	if(document.getElementById('dayOfWeek').value.length<=0){
		alert('Please Select Day of Week');
		return false;
	}else if(document.getElementById('startTime').value.length<=0){
		alert('Please Select Start Time for The Meeting');
		document.getElementById('startTime').focus();
		return false;
	}else if(document.getElementById('endTime').value.length<=0){
		alert('Please Select End Time for The Meeting');
		document.getElementById('endTime').focus();
		return false;
	}
	if(document.getElementById('startTime').value.length>0){
		document.getElementById('startTime').value=document.getElementById('startTime').value.replace('.',':');
		document.getElementById('startTime').value=document.getElementById('startTime').value.replace(',',':');
		document.getElementById('startTime').value=document.getElementById('startTime').value.replace(';',':');
		var result=validateTime(document.getElementById('startTime').value);
		if(result){
		  return false;
		}
	}
	if(document.getElementById('endTime').value.length>0){
	  document.getElementById('endTime').value=document.getElementById('endTime').value.replace('.',':');
	  document.getElementById('endTime').value=document.getElementById('endTime').value.replace(',',':');
	  document.getElementById('endTime').value=document.getElementById('endTime').value.replace(';',':');
		var result=validateTime(document.getElementById('endTime').value);
		if(result)
		return false;
	}
	return true;
}
function validateTime(time){
    if(time.length>5){
		alert('invalid time format');
		return true;
	}
	var val=time.replace('.',':');
	val=val.replace(':',''); 
	if(val.length>4){
		alert('invalid time format');
		return true;
	}
	if(!(/^\d+$/.test(val))){
		alert('invalid time format');
	 	return true;
	}
	var index=time.indexOf(':');
	if(index>=0){
		var str1=time.substring(0, index);
		if(str1<0 || str1>24){
		  alert('invalid time format');
			return true;
		}
		var str2=time.substring(index+1,time.length);
		if(str2<0 || str2>60){
		  alert('invalid time format');
			return true;
		}
	}else{
	  if(time.length>2){
		alert('invalid time format');
		return true;
		}
	}
	return false;
}
function addAttendee(){
	document.getElementById('action').value="addAttendee";
	document.getElementById('meetingScheduleForm').submit();
}
function removeAttendee(userId){
	document.getElementById('newAttendeeId').value=userId;
	document.getElementById('action').value="removeAttendee";
	document.getElementById('meetingScheduleForm').submit();
}
function saveSchedule(){
	var result=validateSchedule();
	if(result){
		document.getElementById('action').value="";
		document.getElementById('meetingScheduleForm').submit();
	}else{
		return false;
	}
}
function changeLocationByHospital(){
	var selectedHospitalId= document.getElementById("selectedHospital").value;
	var select=document.getElementById("meetingLocationSelect");
	while (select.options.length>1){
		var j=1;
			select.remove(j);
			select=document.getElementById("meetingLocationSelect");
	 }
     
	if(selectedHospitalId==""){
		
	}else{
		var locationList=document.getElementById("locationList"+selectedHospitalId).value;
		var res = locationList.split(",");
		for(var i = 0; i <res.length; i++) {
			if(res[i].length>0){
				var opt = document.createElement('option');
		        opt.value = res[i];
		        opt.innerHTML = res[i];
		        select.appendChild(opt);
			}
		}
	}
	
}
function meetingScheduleFormSubmit(){
	document.getElementById('action').value="filterList";
	document.getElementById("meetingScheduleForm").submit();
}
</script>
<br />
<c:set var="hospitals" value="${webapp.hospitalsList}"/>
<c:forEach var="hospitals" items="${hospitals}">
	<%
		Hospitals hospitals = (Hospitals) pageContext.getAttribute("hospitals");
		String locationList = "";
			for (String location : hospitals.getMeetingLocations()) {
				locationList = locationList + "," + location;
			}
	%>
	<input type="hidden" id="locationList<c:out value="${hospitals.id}"/>" value="<%=locationList%>" />
</c:forEach>
<table width="100%">
	<tr>
		<td valign="top" align="center"><br/>
		
		<smvc:form id="meetingScheduleForm" action="../meetings/meetingSchedule.form" modelAttribute="meetingSchedule"  method="post" onsubmit="return validateSchedule();">
		<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}"/>" />
			<smvc:hidden path="id" />
			<smvc:hidden path="action" id="action"/> 
			<smvc:hidden path="specialityDescription" />
			
			
			<table width="700">
				<tr>
				<td>
				<!--  testing start here-->
			    	Filter By &nbsp;Hospital&nbsp;
					<smvc:select path="hospitalFilter"  onchange="meetingScheduleFormSubmit();">
						<smvc:option value="">Any</smvc:option>
						<c:forEach var="hospitals" items="${webapp.hospMap}">
						    <smvc:option value="${hospitals.key}"><c:out value="${hospitals.value}"/></smvc:option>
						</c:forEach>
					</smvc:select>&nbsp;Speciality&nbsp;
					<webapp:namedList name="speciality" domain="EMDT" id="nl"/>
					<smvc:select path="specialityFilter" onchange="meetingScheduleFormSubmit();">
						<smvc:option value="">Any</smvc:option>
						<c:forEach var="pair" items="${nl.pairIterator }">
						<smvc:option value="${pair.first}" ><c:out value="${pair.second }"/></smvc:option>
						</c:forEach>
					</smvc:select>
					<a href="javascript:meetingScheduleFormSubmit();"><img src="../images/16x16/bullet_go.png" /></a>
				<!--  testing end here -->
				</td>
				</tr>
			</table>
			
			<c:if test="${not empty successMsg}">
				<div class="message"><img
					src="../images/16x16/information.png" alt="i" /> <c:out value="${successMsg}"/></div>
			</c:if>
			<fieldset style="width: 50%" class="nobg"><legend>Meeting Schedules</legend>
			<fieldset>
			 <c:choose>
				<c:when
					test="${meetingScheduleList!=null and fn:length(meetingScheduleList)>0}">
					<table>
						<tr>
							<td colspan="9"><B>List of Meeting Schedules</B></td>
						</tr>
						<tr>
							<td colspan="9">&#160;</td>
						</tr>
					</table>
					<table width="100%" border="1" bordercolor="#F8F8FF"
						cellspacing="0" cellpadding="4">
						<tr>
							<th align="left">Hospital</th>
							<th align="left">Speciality</th>
							<th align="left">Day</th>
							<th align="left">Time</th>
							<th align="left">Edit</th>
						</tr>
						<c:forEach var="meetingSchedule" items="${meetingScheduleList}">
							<tr>
								<td><c:out value="${meetingSchedule.hospital.hospitalName}"/></td>
								<td><c:out value="${meetingSchedule.specialityDescription}"/></td>
								<td><c:out value="${meetingSchedule.dayOfWeek}"/></td>
								<td><c:out value="${meetingSchedule.startTime}"/></td>
								<td><a
									href="../meetings/meetingSchedule.form?actionRequired=edit&meetingScheduleId=<c:out value="${meetingSchedule.id}"/>"
									title="Edit Meeting Schedule"
									onmouseover="this.style.cursor = cursorPointer; "> <img
									src="../images/page_white_edit.png" border="0" alt="Edit" /></a></td>
							</tr>
						</c:forEach>
					</table>
				</c:when>
				<c:otherwise>
					<table width="100%" cellspacing="0" cellpadding="4">
						<tr>
							<th>-- No Meeting Schedules defined --</th>
						</tr>
					</table>
				</c:otherwise>
			</c:choose>
		</fieldset>
		
		
		
			<c:if test="${meetingSchedule.id ne null}">
				<fieldset>
				<table width="100%" cellspacing="0" cellpadding="4">
					<tr>
						<td align="right"><input type="button"
							value="Add New Meeting schedule.."
							onclick="document.location.href='../meetings/meetingSchedule.form'" /></td>
					</tr>
				</table>
				</fieldset>
			</c:if>
			</fieldset>
			
			
			<c:if test="${not empty errMsg}">
				<div class="error"><img src="../images/16x16/error.png"
					alt="!" border="0" />&nbsp;&nbsp;<c:out value="${errMsg}"/></div>
			</c:if>		
			<table width="500">
				<tr>
				<td>
			<fieldset class="dialogBox">
			<table class="dialogTitle" width="100%">
				<tr>
					<th>
					<c:choose>
						<c:when test="${meetingSchedule.id ne null}">
							<div><c:out value="${meetingSchedule.specialityDescription}"/> Meeting Schedule</div>
						</c:when>
						<c:otherwise><div>Create New Meeting Schedule</div></c:otherwise>
					</c:choose>
					
					</th>
				</tr>
			</table>
			<br />
			<br />
			
			<table id="editMeetingSpecialityTable">
				<tr>
					<td width="20%">Speciality:</td>
					<td align="left" valign="top" width="70%">
						<webapp:selectAndEditList domain="EMDT" path="speciality"
							list="speciality" includeEmpty="true"
							currentValue="${meetingSchedule.speciality}"/>
					</td>
				</tr>
				<tr>
					<td>Day:</td>
					<td><smvc:select path="dayOfWeek" id="dayOfWeek">
					    <smvc:option value=""></smvc:option>
						<smvc:option value="Monday">Monday</smvc:option>
						<smvc:option value="Tuesday">Tuesday</smvc:option>
						<smvc:option value="Wednesday">Wednesday</smvc:option>
						<smvc:option value="Thursday">Thursday</smvc:option>
						<smvc:option value="Friday">Friday</smvc:option>
						<smvc:option value="Saturday">Saturday</smvc:option>
						<smvc:option value="Sunday">Sunday</smvc:option>
					</smvc:select></td>
				</tr>
				<tr>
					<td style="text-align: right;">Start time: </td>
					<td><smvc:input  path="startTime" size="5" id="startTime" cssStyle="width: 5em;"/>
					  End: <smvc:input  path="endTime" size="5" id="endTime"  cssStyle="width: 5em;"/> (24 hour format)</td>
				</tr>
				<tr><td class="label">Hospital</td>
					<td class="input">
						<smvc:select path="selectedHospital"  id="selectedHospital" onchange="changeLocationByHospital();">
							<smvc:option value="">---select a hospital----</smvc:option>
							<c:forEach var="hospitals" items="${webapp.hospMap}">
								<smvc:option value="${hospitals.key}"><c:out value="${hospitals.value }"/></smvc:option>
							</c:forEach>
						</smvc:select>
					</td>
				</tr>
				<tr>
					<td width="30%">Summary:</td>
					<td align="left" valign="top" width="70%">
						<smvc:select path="patientSummaryId">
							<smvc:option value=""></smvc:option>
							<c:forEach var="summary" items="${emdt.patientSummaries}">
						 		<smvc:option value="${summary.id}" ><c:out value="${summary.title}"/></smvc:option>
							</c:forEach>
						</smvc:select>
					</td>
				</tr>
				<tr>
					<td>Meeting Notes Form:</td>
					<td class="input"> <smvc:select path="formPath">
						<smvc:option value=""></smvc:option>
								<c:forEach var="formGroup" items="${emdt.formDomain.children}">
									<optgroup label="<c:out value="${formGroup.label}"/>">
										<c:forEach var="formDef" items="${formGroup.children}">
											<c:if test="${formDef.liveStatus != 0}">
												<smvc:option value="${formDef.path}" ><c:out value="${formDef.label}"/></smvc:option>
											</c:if>
										</c:forEach>
									</optgroup>
								</c:forEach>
						</smvc:select>
					</td>
				</tr>
				<tr>
                    <td>Location:</td>
                    <td class="input">
	                    <smvc:select path="location" id="meetingLocationSelect">
                        	<smvc:option value=""></smvc:option>
                        	<c:forEach var="hospitals" items="${webapp.hospitalsList}">
                        		<c:if test="${hospitals.id eq meetingSchedule.selectedHospital}">
                        			<c:forEach var="meetingLocations" items="${hospitals.meetingLocations}">
										<c:choose>
											<c:when test="${meetingSchedule.location eq meetingLocations}">
												<smvc:option value="${meetingLocations }" selected="selected"><c:out value="${meetingLocations }"/></smvc:option>
											</c:when>
											<c:otherwise>
												<smvc:option value="${meetingLocations }"><c:out value="${meetingLocations }"/></smvc:option>
											</c:otherwise>
										</c:choose>
									</c:forEach>
                        		</c:if>
                        	</c:forEach>
                        </smvc:select>
                    </td>
                </tr>
                <tr>
				<td>Virtual Meeting?</td>
					<td class="input">
						<smvc:select path="virtualMeeting"  id="virtualMeeting">
							<smvc:option value="true">Yes, enable Virtual Meeting discussion</smvc:option>
							<smvc:option value="false">No, this is a physical meeting</smvc:option>
							<smvc:option value=""></smvc:option>
						</smvc:select>
			
					</td>
				</tr>
				<tr>
					<td valign="top">Meeting Team:</td>
					<c:if test="${fn:length(meetingSchedule.attendees)>0}">
						<td><table id="editMeetingScheduleClinicians">
								<c:forEach var="user" items="${meetingSchedule.attendees}" varStatus="i">
									<tr><td>
									<smvc:hidden path="attendeesIdList[${i.count-1}]"/>
									<c:out value="${user.name}"/>&nbsp;</td>
									<td><img src="../images/delete.png" border="0" alt="Delete"
										onmouseover="this.style.cursor = cursorPointer; " onclick="removeAttendee(<c:out value="${user.id}"/>);" />
									</td>
									</tr>
								</c:forEach>
							</table>
						</td>
					</c:if>
				</tr>
				<tr>
                    <td class="label"/>
                    <td class="input">
                    	<smvc:select path="newAttendeeId" id="newAttendeeId">
                    	<c:forEach var="usersByHospitalMap" items="${webapp.usersByHospitalIdMap}">
							<optgroup label="<c:out value="${webapp.hospMap[usersByHospitalMap.key]}"/>">
								<c:forEach var="userId" items="${usersByHospitalMap.value}">
									<smvc:option value="${userId}" ><c:out value="${webapp.userIdUsernameMap[userId]}"/> [<c:out value="${webapp.userIdNameMap[userId]}"/>]</smvc:option>
								</c:forEach>
							</optgroup>
						</c:forEach>
						</smvc:select>
                     	&nbsp;&nbsp;&nbsp;
                   		<input type="button" value="Add" onclick="addAttendee();return false;"/>
                   	</td>
                </tr>
				<tr>
					<td height="25"></td>
				</tr>
			</table>
			<table width="100%" style="background: #DFE5EB;height : 40px;padding: 2px 5px 2px 10px;">
				<tr>  
					<td width="65%">
						<c:if test="${meetingSchedule.id ne null}">
							<input type="button" onclick="processEditDelete(<c:out value="${meetingSchedule.id}"/>);return false;" value="Delete" />
						</c:if>
					</td>
					<td style="text-align: right;"><input type="button" value="Save" onclick="saveSchedule();return false;"/></td>
				</tr>
			</table>
			</fieldset>
		<br/> <br />


			<div class="meetingNavigationPanel">
				<table>
					<tr>
						<th>Meeting actions</th>
					</tr>
					<tr>
						<td>
							<a href="setMeetingFilter.form?filterId=0">Select Meeting <img src="../images/16x16/bullet_go.png" /></a>
							 <br /> <br />
							<c:if test="${emdt.currentMeeting != null}">
								<a href="resumeMeeting.form">Return to Current Meeting <img src="../images/16x16/arrow_undo.png" /> </a> <br/>
							</c:if>
							 <br /> <br />
						 </td>
					</tr>
				</table>
			</div>
					</td>
				</tr>
			</table>

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