<%@ include file="/includes.jsp" %>
<br/>
<script>
	function update() {
		document.getElementById('inviteForm').submit();
	}
	
	function promptMessage(){
		
		var selectedListValue = document.getElementById('loadattendees').value;
		
		if(selectedListValue==""){
			if(confirm("Are you sure you want to create new MDT List?")){
				
			}else{
				return false;
			}

		}else{
		  if(confirm("Are you sure you want to update this MDT List?")){
				
			}else{
				return false;
			}
		}
		  
	}
	
	function promptDeleteMessage(){
		
		var selectedListValue = document.getElementById('loadattendees').value;
		
		if(selectedListValue==""){
				alert("Please select one MDT List");
				return false;
		}else{
		  if(confirm("Are you sure you want to delete this MDT List?")){
				
			}else{
				return false;
			}
		}
		  
	}
	
	function validateAddNewAttendee(){
        var name, email, selectedUserId, isExternal,isInternal, validInputs="false";
        name = document.getElementById('newAttendeeName').value;
        email = document.getElementById('newAttendeeEmail').value;
        selectedUserId = document.getElementById('selectedUserId').value;
        
        if(selectedUserId==0) {
            if(name.length!=0 && name!=" "){
                if(email.length!=0 && email!=" " && validateEmailId(email)!=false) {
	                isInternal = confirm("Are you sure you want to add the non eMDT user?");
		            if(isInternal){
		                return true;            
		            }
		            else{
		                return false;
		            }
	            }
	            else {
	                alert("Please enter valid email id or select an eMDT user");
	                return false;
            	}
            }
            else {
               alert("Please enter valid attendee name or select an eMDT user");
               return false;
            }
        }
        else {
            isExternal = confirm("Are you sure you want to add the eMDT user?");
            if(isExternal){
                return true;            
            }
            else{
                return false;
            }
        }
    }
    
    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>

<div> <%@include file="meetingHeadingPanel.jsp" %></div>



<s:form action="/meetings/changeInvites" styleId="inviteForm">
	<input type="hidden" name="page" value="Invites"/>
	
	<table>
	     <tr>
				<td align="center">
					<br/>
					<c:if test="${not empty message}">
						<span class="info"><c:out value="${message}"/></span>
					</c:if>
				</td>
		</tr>
	</table>
	
	
    	
	<table cellpadding="0" cellspacing="0" width="400">
		<tr>
			<td align="center">
				<c:set var="timeNow" value="<%=new java.util.Date() %>"/>
				<c:if test="${emdt.currentMeeting.endDate.before(timeNow) }"><span class="warning">The date of this meeting is in the past.</span></c:if>
			</td>
		</tr>
		<tr>
			<td>
				<tags:dialogBox title="Invite Attendees">
                    <table>
                        <tr>
                            <td class="label" size="1">Invite\Remove?</td>
                            <td class="label" title="Enter a system username to have this meeting appear in their diary">Attendee name</td>
                            <td class="label" title="Click here to populate System user details in the Attendee record"></td>
                            <td class="label" title="Optional: Enter if you want to invite this attendee via an e-mail">E-Mail</td>
                            <td class="label" title="Optional: Enter if you want to invite this attendee via telephone">Telephone</td>
                            <td class="label" title="Optional: Enter if you want to invite this attendee via fax">Fax</td>
                            <td class="label" title="Optional: Enter if you want to invite this attendee via letter">Letter</td>
                            <td class="label" title="Which invitation method to use">Use</td>
                        </tr>
                        <c:forEach var="attendee" items="${meetingInviteForm.meeting.attendees }" varStatus="s">
                            <tr>
                                <td class="input" align="center" title="Check to remove attendee"><html:multibox property="selected" value="${attendee.id }"/></td>
                                <td class="input"  title="Optional: Enter if you want to invite this attendee via an e-mail">
                                    <html:text property="meeting.attendees[${s.index}].name" />
                                </td>
                                <td></td>
                                <td class="input"  title="Optional: Enter if you want to invite this attendee via an e-mail">
                                    <html:text property="meeting.attendees[${s.index}].email" size="12"/>
                                </td>
                                <td class="input"><html:text property="meeting.attendees[${s.index}].telephone" size="12"/></td>
                                <td class="input"><html:text property="meeting.attendees[${s.index}].fax" size="12"/></td>
                                <td class="input"><html:text property="meeting.attendees[${s.index}].letter" size="10"/></td>
                                <td class="input">
                                    <html:select property="meeting.attendees[${s.index}].inviteMethod">
                                        <html:option value="email"> E-Mail</html:option>
                                        <html:option value="telephone"> Tel.</html:option>
                                        <html:option value="fax"> Fax</html:option>
                                        <html:option value="letter"> Letter</html:option>
                                    </html:select>
                                </td>
                            </tr>
                        </c:forEach>
                        <tr>
                            <td>
                            <c:if test="${meetingInviteForm.locked ne true}">
                                <c:if test="${meetingInviteForm.meeting.totAttendees ne 0}">
                                &nbsp;&nbsp;&nbsp;<html:submit property="action" value="Delete" disabled="false"/>
                                </c:if>
                            </c:if>
                            <c:if test="${meetingInviteForm.locked eq true}">
                                &nbsp;&nbsp;&nbsp;<html:submit property="action" value="Delete" disabled="true"/>
                            </c:if>
                            </td>
                        </tr>
                            </table>
                </tags:dialogBox>
                <tags:dialogBox title="">
                <table>
                    <tr>
                        <td colspan="7" class="label" style="text-align: center; font-weight: bold">New Attendee</td>
                    </tr>
                    <tr>
                        <td class="label" title="Enter a system username to have this meeting appear in their diary">Attendee name</td>
                        <td class="label" title="Select an item from the drop down for a system user">User</td>
                        <td class="label" title="Click hera to invite an existing system user as an attendee"></td>
                        <td class="label" title="Optional: Enter if you want to invite this attendee via an e-mail">E-Mail</td>
                        <td class="label" title="Optional: Enter if you want to invite this attendee via telephone">Telephone</td>
                        <td class="label" title="Optional: Enter if you want to invite this attendee via fax">Fax</td>
                        <td class="label" title="Optional: Enter if you want to invite this attendee via letter">Letter</td>
                        <td class="label" title="Which invitation method to use">Use</td>
                    </tr>
                    <tr>
                        <td class="input" title="You can enter a system username to have this meeting appear in their diary">
                            <html:text property="newAttendee.name" styleId="newAttendeeName"/></td>
                        <td class="input"  title="Optional: Enter if you want to invite this attendee via an e-mail">
                        <html:select property="selectedUserId" styleId="selectedUserId">
                        <html:option value=""/>
                        <c:forEach var="user" items="${meetingInviteForm.users}">
                            <html:option value="${user.id}"><c:out value="${user.username}"/></html:option>
                        </c:forEach>
                        </html:select>
                        </td>
                        <td>
                             <c:if test="${meetingInviteForm.locked ne true}">
                                <html:submit property="action" value="Add" disabled="false" onclick="return validateAddNewAttendee()"/>
                            </c:if>
                             <c:if test="${meetingInviteForm.locked eq true}">
                                <html:submit property="action" value="Add" disabled="true" onclick="return validateAddNewAttendee()"/>
                            </c:if>                         
                        </td>
                        <td class="input"  title="Optional: Enter if you want to invite this attendee via an e-mail">
                                <html:text property="newAttendee.email" size="12" styleId="newAttendeeEmail"/>
                            </td>
                            <td class="input"><html:text property="newAttendee.telephone" size="12"/></td>
                            <td class="input"><html:text property="newAttendee.fax" size="12"/></td>
                            <td class="input"><html:text property="newAttendee.letter" size="10"/></td>
                            <td class="input">
                                <html:select property="newAttendee.inviteMethod">
                                    <html:option value="email"> E-Mail</html:option>
                                    <html:option value="telephone"> Tel.</html:option>
                                    <html:option value="fax"> Fax</html:option>
                                    <html:option value="letter"> Letter</html:option>
                                </html:select>
                            </td>
                        </tr>
                </table>
                </tags:dialogBox>
				
				
				
				
				<br/>
				<tags:dialogBox title="Email settings">
					<table cellpadding="4">
						<tr>
							<td>
							Meeting organiser email address (replies go here)</td>
							<td><html:text property="meeting.organiserEmailAddress" size="30" /></td>
						</tr>
						<tr>
							<td>Include a summary for each patient?</td>
							<td><html:select property="meeting.emailSummaryId">
								<html:option value="">-- no summary selected --</html:option>
								<c:forEach var="summary" items="${emdt.patientSummaries}">
									<html:option value="${summary.id}"><c:out value="${summary.title}"/></html:option>
								</c:forEach>
							</html:select>						
							</td>
						</tr>
					</table>
				</tags:dialogBox>
			</td>
		</tr>
		
	</table>
	
	<br/>
	<table cellpadding="0" cellspacing="0" width="765">
    <tr>
    <td>
       <tags:dialogBox title="MDT Lists">
       <table>
        <tr>
            <td>
                 &nbsp;&nbsp;MDT List     
            </td>
            <td>
                    <!-- <html:select property="selectedmdtList"> 
                        <logic:iterate name="meetingInviteForm" property="mdtListLists" id="bingBong" indexId="index">
                            <html:option value="${bingBong.id}"><c:out value="${bingBong.mdtListName}"/></html:option> <br>
                        </logic:iterate>
                        </html:select> -->
                   
                    <html:select property="selectedmdtList" styleId="loadattendees">
                        <html:option value="">--Choose One--</html:option>
                        <c:forEach var="summary" items="${meetingInviteForm.mdtListLists}">
                            <html:option value="${summary.id}"><c:out value="${summary.mdtListName}"/></html:option>
                        </c:forEach>
                    </html:select> 
                    
                    <!-- <html:select property="selectedmdtList" >
                            <html:option value="">SELECT</html:option>
                            <html:options collection="mdtLists" property="id" labelProperty="mdtListName" />
                          </html:select> -->  
              </td>
            <td>
                 &nbsp;<html:submit property="action" value="Load MDT"/>
            </td>
            
            <td>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MDT Name      
            </td>
            <td>
            <html:text property="mdtlistname" /> 
            </td>
            <td>
                &nbsp;<html:submit property="action" value="Save MDT" onclick="return promptMessage();"/>
            </td>
            <td>
                &nbsp;<html:submit property="action" value="Delete MDT" onclick="return promptDeleteMessage();"/>
            </td>
        </tr>
       </table>
       </tags:dialogBox>
    </td>
    </tr>
    </table>
    <br/>
    
  <c:if test="${meetingInviteForm.locked ne true}">
	    <html:submit property="action" value="Save" disabled="false"/>&nbsp;
		<html:submit property="action" value="Save and Invite" disabled="false"/>
    </c:if>
     <c:if test="${meetingInviteForm.locked ne true}">
        <html:submit property="action" value="Save" disabled="true"/>&nbsp;
        <html:submit property="action" value="Save and Invite" disabled="true"/>
    </c:if>
</s:form>