<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ include file="/includes.jsp"%>
<html>
<script type="text/javascript">
	function validateSendTestEmailFm(){
		var allFilled = true;
		if(document.getElementById("smtpHost").value == ""){
			alert("Please enter 'SMTP Host'.");
			document.getElementById("smtpHost").focus();
			allFilled = false;
			return false;
		}
		if(document.getElementById("fromAddress").value == ""){
			alert("Please enter 'From Address'.");
			document.getElementById("fromAddress").focus();
			allFilled = false;
			return false;
		}
		if(document.getElementById("toAddress").value == ""){
			alert("Please enter 'To address'.");
			document.getElementById("toAddress").focus();
			allFilled = false;
			return false;
		}if(document.getElementById("toAddress").value != "" && document.getElementById("toAddress").value.indexOf("@")==-1){
			alert("Invalid 'To address'.");
			document.getElementById("toAddress").focus();
			allFilled = false;
			return false;
		}
		
		if(document.getElementById("subject").value == ""){
			alert("Please enter 'Subject'.");
			document.getElementById("subject").focus();
			allFilled = false;
			return false;
		}
		if(document.getElementById("messageId").value == ""){
			alert("Please enter 'Message'.");
			document.getElementById("messageId").focus();
			allFilled = false;
			return false;
		}
		if (allFilled == true){
			document.getElementById("sendTestEmailFm").submit();
		}
	}
	function setFieldValue(smtpHost,fromAddress,subject){
		document.getElementById("smtpHost").value=smtpHost;
		document.getElementById("fromAddress").value=fromAddress;
		document.getElementById("subject").value=subject;
		
	}
	
</script>
<sec:authorize access="hasRole('ADMIN')">
<body onload="setFieldValue(<c:out value="${smtpHost}"/>,<c:out value="${fromAddress}"/>,'Test email from eMDT');">
<br></br>
<c:if test="${sentMailMessage ne null}">
	<c:choose>
		<c:when test="${'Test mail sent successfully' eq sentMailMessage}">
			<div><span class="info"><c:out value="${sentMailMessage}"/></span></div>
		</c:when>
		<c:otherwise>
			<div><span class="error"><c:out value="${sentMailMessage}"/></span></div>
		</c:otherwise>
	</c:choose>
</c:if>
<br>
<fieldset style="width: 500px" class="nobg"><legend>Test Email</legend>
<fieldset class="buttonFieldset">
<smvc:form id="sendTestEmailFm" action="../admin/emailSettings.form" method="post" modelAttribute="emailSettingsForm">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<table align="left" width="100%">
		<tr>
			<td colspan="2" align="center"><b>Send a test email</b>
			<hr />
			</td>
		</tr>
		<tr>
			<td colspan="2" height="25"></td>
		</tr>
		<tr>
			<td class="input"><smvc:label path="smtpHost">SMTP Host</smvc:label></td>
			<td class="input"><smvc:input path="smtpHost"	id="smtpHost" /> </td>
		</tr>
		<tr>
			<td class="input"><smvc:label path="fromAddress">From Address</smvc:label></td>
			<td class="input"><smvc:input path="fromAddress" id="fromAddress" /></td>
		</tr>
		<tr>
			<td class="input"><smvc:label path="toAddress">To Address</smvc:label></td>
			<td class="input"><smvc:input path="toAddress" id="toAddress" /></td>
		</tr>
		<tr>
			<td class="input"><smvc:label path="authenticated">authenticated</smvc:label></td>
			<td class="input"><smvc:checkbox path="authenticated" id="authenticated" /></td>
		</tr>
		<tr>
			<td class="input"><smvc:label path="subject">Subject</smvc:label></td>
			<td class="input"><smvc:input path="subject" id="subject" /></td>

		</tr>
		<tr>
			<td class="input"><smvc:label path="message">Message</smvc:label></td>
			<td class="input"><smvc:textarea  path="message" id="messageId" rows="5" cols="30"></smvc:textarea></td>
		</tr>
		<tr>
			<td colspan="2" align="right"><input type="button" value="Send"
				onclick="validateSendTestEmailFm();" /></td>
		</tr>
	</table>
</smvc:form></fieldset>
</fieldset>
</body></sec:authorize>
</html>