<%@ 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 mainScreenView() {
	window.location.href = 'viewMeetingAttendee.form';
}
function setAction(action){
	document.getElementById('action').value=action;
}
function promptMessage(action){
	var selectedListValue = document.getElementById('loadattendees').value;
	if(selectedListValue==""){
		if(!confirm("Are you sure you want to create new MDT List?")){
			return false;
		}
	}else{
	  if(!confirm("Are you sure you want to update this MDT List?")){
			return false;
		}
	}
	setAction(action);
}
	
function promptDeleteMessage(action){
	var selectedListValue = document.getElementById('selectedmdtListDel').value;
	if(selectedListValue==""){
		alert("Please select one MDT List");
		return false;
	}else{
	  if(!confirm("Are you sure you want to delete this MDT List?")){
			return false;
		}
	}
	setAction(action);
}
function addAttendee(){
	setAction("Add Attendee");
}
function removeAttendee(username) {
	document.getElementById('attendeeToDeleteUserName').value=username;
	setAction("Remove Attendee");
	document.getElementById("mdtListForm").submit();
}
function getAttendees(){
	var mdtId=document.getElementById('mdtId').value;
	document.getElementById('mdtBeingEdited').value=mdtId;
	if(mdtId.length<=0){
		alert("Please select the MDT, that you wish to edit.");
		document.getElementById('mdtId').focus();
		return false;
	}
	var tbl= document.getElementById('editMDTAttendees');
	if(tbl.rows.length>0){
		for(var i = tbl.rows.length-1 ; i >= 0; i--){
			tbl.deleteRow(i);
		}
	}
	jQuery.ajax({
		type: "POST",
		url:  "mdtListEditAttendee.form",
		data: "mdtId="+mdtId,		   
		success: function(msg){   
		   if(msg.attendeesListSize>0){
		       var rowCount=tbl.rows.length;
			   for(var index=0;index<msg.attendeesListSize; index++){
					var row = tbl.insertRow(rowCount);
					var td = document.createElement("TD");
				 	var content = document.createTextNode(msg.attendeesList[index].user.forename);
					td.appendChild(content);
					
					var content = document.createTextNode(" "+msg.attendeesList[index].user.surname);
					td.appendChild(content);
				 	row.appendChild(td);
				 	
				 	var td = document.createElement("TD");  
					element = document.createElement("img");
					element.setAttribute("src", "../images/delete.png");
					element.setAttribute("onclick", " removeAttendee('"+msg.attendeesList[index].user.username+"');");
					element.setAttribute("onmouseover", "this.style.cursor = cursorPointer");
					td.appendChild(element);
					row.appendChild(td);
			   }
			   document.getElementById('editrow1').style.display='block';
			   document.getElementById('editrow2').style.display='block';
			   document.getElementById('editrow3').style.display='block';
			   document.getElementById('editrow4').style.display='block';
	       }
	 	}
	});
}
</script>
<table width="100%">
	<tr>
		<td valign="top" align="center"><br /> <smvc:form id="mdtListForm"
				modelAttribute="mdtListForm" action="../meetings/mdtList.form">
				<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
				<smvc:hidden path="attendeeToDeleteUserName" />
				<input type="hidden" name="page" value="Invites" />
				<smvc:hidden path="action" />
				<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> <c:if test="${error ne null}">
								<table width="100%" cellpadding="4"
									style="border: 2px Blue solid">
									<tr>
										<td><img src="../images/16x16/error.png" alt="!"
											border="0" />
										</td>
										<td>
											<div class="error"><c:out value="${error}"/></div> <br />
										</td>
									</tr>

								</table>
							</c:if></td>
					</tr>
				</table>
				<br />
				<table cellpadding="0" cellspacing="0" width="765">
					<tr>
						<td><tags:dialogBox
								title="Add current meeting attendees from MDT list">
								<table>
									<tr>
										<td colspan="3">&nbsp;</td>
									</tr>
									<tr>
										<td>&nbsp;&nbsp;MDT List</td>
										<td><smvc:select path="selectedmdtList"
												id="loadattendees">
												<smvc:option value="">--Choose One--</smvc:option>
												<c:forEach var="summary" items="${mdtListForm.mdtListLists}">
													<smvc:option value="${summary.id}"><c:out value="${summary.mdtListName}"/></smvc:option>
												</c:forEach>
											</smvc:select></td>
										<td>&nbsp;<input type="submit"
											value="Add current meeting attendees from MDT list"
											onclick="setAction('Add current meeting attendees from MDT list')" />
										</td>
									</tr>
									<tr>
										<td colspan="3">&nbsp;</td>
									</tr>
								</table>
							</tags:dialogBox> <br />
						<br /> <tags:dialogBox
								title="Remember current attendees as MDT list">
								<table>
									<tr>
										<td colspan="3">&nbsp;</td>
									</tr>
									<tr>
										<td>&nbsp;&nbsp;MDT Name</td>
										<td><smvc:input path="mdtlistname" /></td>
										<td>&nbsp;<input type="submit"
											value="Remember current attendees as MDT list"
											onclick="return promptMessage('Remember current attendees as MDT list');" />
										</td>
									</tr>
									<tr>
										<td colspan="3">&nbsp;</td>
									</tr>
								</table>
							</tags:dialogBox> <br />
							<br />
							<tags:dialogBox title="Edit Attendees in MDT List">
								<table>
									<tr>
										<td colspan="3">&nbsp;<smvc:hidden path="mdtBeingEdited"/></td>
									</tr>
									<tr>
										<td>&nbsp;&nbsp;MDT List</td>
										<td><select id="mdtId">
												<option value="">--Choose One--</option>
												<c:forEach var="mdt" items="${mdtListForm.mdtListLists}">
													<option value="<c:out value="${mdt.id}"/>"><c:out value="${mdt.mdtListName}"/></option>
												</c:forEach>
											</select></td>
										<td>&nbsp;<input type="button" value="Edit Attendees in MDT" onclick="getAttendees();" />
										</td>
									</tr>
									<tr id="editrow1" style="display: none">
										<td colspan="3">&nbsp;</td>
									</tr>
									<tr id="editrow2" style="display: none">
										<td valign="top">&nbsp;&nbsp;Meeting Team:</td>
										<td><table id="editMDTAttendees">
											</table>
										</td>
									</tr>
									<tr id="editrow3" style="display: none">
										<td colspan="3">&nbsp;</td>
									</tr>
									<tr id="editrow4" style="display: none">
										<td></td>
					                    <td class="input">
					                     	<smvc:select  path="newAttendeeId">
					                     		<smvc:option value="" ></smvc:option>
												<c:forEach var="user" items="${webapp.userIdNameMap}">
													<smvc:option value="${user.key}"><c:out value="${user.value}"/></smvc:option>
												</c:forEach>                     	
					                     	</smvc:select>
					                     	&nbsp;&nbsp;&nbsp;
					                   		<input type="submit" value="Add" onclick="addAttendee();"/>
					                   	</td>
					                </tr>
									<tr>
										<td colspan="3">&nbsp;</td>
									</tr>
								</table>
							</tags:dialogBox> <br />
							<br />  
							<tags:dialogBox title="Delete MDT List">
								<table>
									<tr>
										<td colspan="3">&nbsp;</td>
									</tr>
									<tr>
										<td>&nbsp;&nbsp;MDT List</td>
										<td><smvc:select path="selectedmdtListDel"
												id="selectedmdtListDel">
												<smvc:option value="">--Choose One--</smvc:option>
												<c:forEach var="summary" items="${mdtListForm.mdtListLists}">
													<smvc:option value="${summary.id}"><c:out value="${summary.mdtListName}"/></smvc:option>
												</c:forEach>
											</smvc:select></td>
										<td>&nbsp;<input type="submit" value="Delete MDT"
											onclick="return promptDeleteMessage('Delete MDT');" /></td>
									</tr>
									<tr>
										<td colspan="3">&nbsp;</td>
									</tr>
								</table>
							</tags:dialogBox></td>
					</tr>
					<tr>
						<td>
							<table align="right">
								<tr>
									<td colspan="3">&nbsp;</td>
								</tr>
								<tr>
									<td colspan="3" align="right"><input type="button"
										value="Cancel" onclick="mainScreenView();" />
									</td>
								</tr>
								<tr>
									<td colspan="3">&nbsp;</td>
								</tr>
							</table></td>
					</tr>
				</table>

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