<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<script type="text/javascript">
function submitform(action){
	document.getElementById("action").value = action;
	document.getElementById("addAttendeeForm").submit();
}
function addNewAttendee(action){
	var res=validateAddNewAttendee();
	if(res){
		submitform(action);
    }
	
}

function validateAddNewAttendee() {
	var selectedUserId = document.getElementById('selectedUserId').value;
	if (selectedUserId == "") {
		alert("Please select an eMDT user");
		return false;
	} else {
		return true;
	}
}

function validateEmailId(str) {
	var at = "@"
	var dot = "."
	var lat = str.indexOf(at)
	var lstr = str.length
	var ldot = str.indexOf(dot)
	if (str.indexOf(at) == -1) {
		return false
	}
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0
			|| str.indexOf(at) == lstr) {
		return false
	}
	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0
			|| str.indexOf(dot) == lstr) {
		return false
	}
	if (str.indexOf(at, (lat + 1)) != -1) {
		return false
	}
	if (str.substring(lat - 1, lat) == dot
			|| str.substring(lat + 1, lat + 2) == dot) {
		return false
	}
	if (str.indexOf(dot, (lat + 2)) == -1) {
		return false
	}
	if (str.indexOf(" ") != -1) {
		return false
	}
	return true
}
</script>
<table width="100%">
	<tr>
		<td valign="top" align="center">
			<smvc:form modelAttribute="addAttendeeForm" id="addAttendeeForm"  action="../meetings/addMeetingAttendee.form">
				<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
				<smvc:hidden path="action" id="action" />
				<input type="hidden" name="page" value="Invites" />
				<table>
					<tr>
						<td align="center"><br /> <c:if test="${message ne null}">
								<table width="100%" cellpadding="4"
									style="border: 2px Blue solid">
									<tr>
										<td><img src="../images/16x16/information.png" alt="!"
											border="0" /></td>
										<td>
											<div class="message"><c:out value="${message}"/></div>
											<br /></td>
									</tr>
								</table>
							</c:if></td>
					</tr>
				</table>
				<br />
				<table cellpadding="0" cellspacing="0" width="750" border="0">
					<tr>
						<td><tags:dialogBox title="Add Meeting Attendees">
								<table>
									<c:if test="${requestScope.error ne null}">
									<tr height="40px" valign="bottom">
										<td colspan="2" class="input" align="center">
											<div class="error">
												<img src="images/16x16/error.png" alt="e" /><c:out value="${requestScope.error}"/>
											</div></td>
									</tr></c:if>
									<tr>
										<td colspan="6" class="label"
											style="text-align: center; font-weight: bold">New
											Attendee</td>
									</tr>
									<tr>
										<td class="label"
											title="Select an item from the drop down for a system user">User</td>
										<td class="label"
											title="Click here to invite an existing system user as an attendee"></td>
										<td class="label">&nbsp;</td>
										<td class="label">&nbsp;</td>
										<td class="label">&nbsp;</td>
										<td class="label">&nbsp;</td>
									</tr>
									<tr>
										<td class="input" colspan="3"
											title="Optional: Enter if you want to invite this attendee via an e-mail">
											<smvc:select path="selectedUserId" id="selectedUserId">
												<smvc:option value="" ></smvc:option>
												<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>
											</td>
										<td class="input"><input type="button" value="Add"
											onclick="addNewAttendee('Add');" />
										</td>
										<td class="input">&nbsp;</td>
										<td class="input">&nbsp;</td>
									</tr>
									<tr>
										<td colspan="6" class="label"
											style="text-align: center; font-weight: bold">&nbsp;</td>
									</tr>
								</table>
							</tags:dialogBox></td>
					</tr>
					<tr>
						<td>&nbsp;</td>
					</tr>
					<tr>
					<td><tags:table title="New attendees">
								<table width="100%" class="table" border="1">
									<tr>
										<th
											title="Enter a system username to have this meeting appear in their diary">Attendee
											name</th>
										<th
											title="Optional: Enter if you want to invite this attendee via an e-mail">E-Mail</th>
										<th
											title="Optional: Enter if you want to invite this attendee via telephone">Telephone</th>
										<th
											title="Optional: Enter if you want to invite this attendee via fax">Fax</th>
										<th
											title="Optional: Enter if you want to invite this attendee via letter">Letter</th>
										<th title="Which invitation method to use">Use</th>
									</tr>

									<c:if
										test="${addAttendeeForm.addListAttendees ne null and  fn:length(addAttendeeForm.addListAttendees) gt 0}">
										<c:forEach var="attendee" items="${addAttendeeForm.addListAttendees}" varStatus="index">
											<tr class='${(index.count-1)%2 == 0 ? "odd" : "even" }Row'>
												<td width="20%"
													title="Optional: Enter if you want to invite this attendee via an e-mail">
													<smvc:hidden path="addListAttendees[${index.count-1}].user.username"/>
													<smvc:input path="addListAttendees[${index.count-1}].user.name" size="20" maxlength="250" readonly="true"/></td>
												<td width="20%" title="Optional: Enter if you want to invite this attendee via an e-mail">
													<smvc:input path="addListAttendees[${index.count-1}].user.email" size="20" maxlength="250"  /></td>
												<td width="14%"><smvc:input path="addListAttendees[${index.count-1}].telephone" size="12" />
												</td>
												<td width="14%"><smvc:input
														path="addListAttendees[${index.count-1}].fax" size="12" />
												</td>
												<td width="14%"><smvc:input
														path="addListAttendees[${index.count-1}].letter" size="12" />
												</td>
												<td width="13%"><smvc:select
														path="addListAttendees[${index.count-1}].inviteMethod">
														<smvc:option value="email"> E-Mail</smvc:option>
														<smvc:option value="telephone"> Tel.</smvc:option>
														<smvc:option value="fax"> Fax</smvc:option>
														<smvc:option value="letter"> Letter</smvc:option>
													</smvc:select>
												</td>
											</tr>
										</c:forEach>
									</c:if>
								</table>
							</tags:table></td>
					</tr>
					<tr>
						<td>
							<table width="100%">
								<tr>
									<td align="right"><input type="submit" value="Save"
										onclick="submitform('Save');" /> <input type="button"
										value="Cancel"
										onclick="window.location.href = 'viewMeetingAttendee.form'" />
									</td>
							</table></td>
					</tr>
				</table>
			</smvc:form>
		</td>
	</tr>
</table>