<%@ include file="/includes.jsp"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.GregorianCalendar"%>
<script language="javascript">

function validate() {

	if(document.auditReportForm.sel1.value==""){
       		alert("Please enter From Date.");
            document.auditReportForm.sel1.focus();
            return false;
	}

//Check occurences of '/' symbol
        var checkOK = "0123456789/";
        var checkStr = document.auditReportForm.sel1.value;                    
        var allValid_f = true;                                                    

        for (i = 0; i < checkStr.length; i++) { 
       		ch = checkStr.charAt(i); 
               for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j))
                break;
                if (j == checkOK.length){ 
	               	allValid_f = false; 
	               	break; 
               } 
        }
        
         if (!allValid_f){ 
                alert("Invalid From Date.\nFormat accepted is dd/mm/yyyy.");
                document.auditReportForm.sel1.focus();
                return (false); 
        } 
        
        var k;
        var l=0;
        
        for(k = 0; k < checkStr.length; k++) { 
        		ch = checkStr.charAt(k); if(ch=='/') l++;}
            	if (l!=2){ 
                    alert("Invalid From Date.\nFormat accepted is dd/mm/yyyy.");
                    document.auditReportForm.sel1.select();
                    document.auditReportForm.sel1.focus();
                    return (false); 
                 } 
                 
                 //Checks for Invalid Date formats
		        a=document.auditReportForm.sel1.value.indexOf('/')
		        b=document.auditReportForm.sel1.value.indexOf('/',a+1)
		        fs_date=document.auditReportForm.sel1.value.substring(0,a)
		        fs_month=document.auditReportForm.sel1.value.substring(a+1,b)
		        fs_year=document.auditReportForm.sel1.value.substring(b+1,10)

        
	        	if(document.auditReportForm.sel1.value.substring(b+1,10).length !=4){
					alert("Invalid From Date.\nFormat accepted is dd/mm/yyyy.");
	                document.auditReportForm.sel1.select();
	                document.auditReportForm.sel1.focus();
	                return (false); 
	            }

		        if((document.auditReportForm.sel1.value.substring(a+1,b).length < 1) || (document.auditReportForm.sel1.value.substring(a+1,b).length > 2)){
	                alert("Invalid From Date.\nFormat accepted is dd/mm/yyyy.");
	                document.auditReportForm.sel1.select();
	                document.auditReportForm.sel1.focus();
	                return (false); 
		        }
        
		        if((document.auditReportForm.sel1.value.substring(0,a).length < 1) || (document.auditReportForm.sel1.value.substring(0,a).length > 2)){
		                alert("Invalid From Date.\nFormat accepted is dd/mm/yyyy.");
		                document.auditReportForm.sel1.select();
		                document.auditReportForm.sel1.focus();
		                return (false); 
		        }
        
		        if(fs_month>12 || fs_month<1){
		                alert("Invalid From Date in month.");
		                 document.auditReportForm.sel1.select();
		                document.auditReportForm.sel1.focus();
		                return (false); 
		         }
         
	          	if(fs_date>31 || fs_date<1){
	                alert("Invalid From Date in days.");
	                document.auditReportForm.sel1.select();
	                document.auditReportForm.sel1.focus();
	                return (false); 
	        	}
        
        		if(fs_month==4 || fs_month==6 || fs_month==9 || fs_month==11)
        			if(fs_date>30){
		                alert("Invalid From Date.");
		                 document.auditReportForm.sel1.select();
		                document.auditReportForm.sel1.focus();
		                return (false); 
  					}
        
         		if(fs_month==2)
        			if(fs_date>29){
		                alert("Invalid From Date .");
		                document.auditReportForm.sel1.select();
		                document.auditReportForm.sel1.focus();
		                return (false); 
        			}
        
        		if((fs_year%4)!=0)
        			if(fs_month==2)
        				if(fs_date>28){
                		alert("Invalid From Date.");
                 		document.auditReportForm.sel1.select();
                		document.auditReportForm.sel1.focus();
                		return (false); 
        			}
        
        		// To date validation
        
      			if(document.auditReportForm.sel2.value==""){
       				alert("Please enter To Date.");
            		document.auditReportForm.sel2.focus();
            		return false;
				}  

			var checkStr1 = document.auditReportForm.sel2.value;                    
        	var allValid_f1 = true;                                                    

        	for (i = 0; i < checkStr1.length; i++){ 
        		ch = checkStr1.charAt(i); 
                for (j = 0; j < checkOK.length; j++)
                 if (ch == checkOK.charAt(j))
                 break;
                if (j == checkOK.length){ 
                	allValid_f1 = false; break; 
                } 
        	}
        
         if (!allValid_f1){ 
                alert("Invalid To Date.\nFormat accepted is dd/mm/yyyy.");
                document.auditReportForm.sel2.focus();
                return (false); 
          } 
        
       var l1=0;
        
        for(k = 0; k < checkStr.length; k++) { ch = checkStr.charAt(k); if(ch=='/') l1++;}
            if (l1!=2){ 
                        alert("Invalid From Date.\nFormat accepted is dd/mm/yyyy.");
                        document.auditReportForm.sel2.select();
                        document.auditReportForm.sel2.focus();
                        return (false); 
                 } 
                 //Checks for Invalid Date Formats
                 c=document.auditReportForm.sel2.value.indexOf('/')
                d=document.auditReportForm.sel2.value.indexOf('/',c+1)
                r_date=document.auditReportForm.sel2.value.substring(0,c)
                r_month=document.auditReportForm.sel2.value.substring(c+1,d)
                r_year=document.auditReportForm.sel2.value.substring(d+1,10)

                
                if(document.auditReportForm.sel2.value.substring(d+1,10).length !=4){
                    alert("Invalid To Date.\nFormat accepted is dd/mm/yyyy.");
                    document.auditReportForm.sel2.select();
                    document.auditReportForm.sel2.focus();
                    return (false); 
                }
                
                if((document.auditReportForm.sel2.value.substring(c+1,d).length < 1) || (document.auditReportForm.sel2.value.substring(c+1,d).length > 2)){
                    alert("Invalid To Date.\nFormat accepted is dd/mm/yyyy.");
                    document.auditReportForm.sel2.select();
                    document.auditReportForm.sel2.focus();
                    return (false); 
                }
                
                if((document.auditReportForm.sel2.value.substring(0,c).length < 1) || (document.auditReportForm.sel2.value.substring(0,c).length > 2)){
                    alert("Invalid To Date.\nFormat accepted is dd/mm/yyyy.");
                    document.auditReportForm.sel2.select();
                    document.auditReportForm.sel2.focus();
                    return (false); 
                }
                 if(r_month>12 || r_month<1){
                    alert("Invalid To Date in month.");
                    document.auditReportForm.sel2.select();
                    document.auditReportForm.sel2.focus();
                    return (false); 
                }
                
                if(r_date>31 || r_date<1){
                    alert("Invalid To Date in days.");
                    document.auditReportForm.sel2.select();
                    document.auditReportForm.sel2.focus();
                    return (false); 
                }
                
                 if(r_month==4 || r_month==6 || r_month==9 || r_month==11)
	                if(r_date>30)
	                {
	                    alert("Invalid To Date.");
	                    document.auditReportForm.sel2.select();
	                    document.auditReportForm.sel2.focus();
	                    return (false); 
	                }
                
                if(r_month==2)
                	if(r_date>29){
	                    alert("Invalid To Date.");
	                    document.auditReportForm.sel2.select();
	                    document.auditReportForm.sel2.focus();
	                    return (false); 
                	}
                
                if((r_year%4)!=0)
                	if(r_month==2)
                		if(r_date>28){
		                    alert("Invalid To Date.");
		                    document.auditReportForm.sel2.select();
		                    document.auditReportForm.sel2.focus();
		                    return (false); 
                		}
                
                var date1,date2,dateT1,dateT2;
                var vDateSplit = document.auditReportForm.sel1.value.split("/");
				date1 = vDateSplit[1] + "/" + vDateSplit[0] + "/" + vDateSplit[2];
				var vDateSplit1 = document.auditReportForm.sel2.value.split("/");
				date2 = vDateSplit1[1] + "/" + vDateSplit1[0] + "/" + vDateSplit1[2];
				
				//Check for the Order of the Dates

                dateT1=   new Date(date1).getTime();
                dateT2 =  new Date(date2).getTime();

				//alert(new Date(date1).getMonth())	;
                if(dateT1>dateT2){
                        alert("From Date cannot be less than To Date.");
                        document.auditReportForm.sel1.select();
                        document.auditReportForm.sel1.focus();
                        return false;
                }
                
                showPleaseWait();
}
</script>
<html>
<head>
<title>Referrals Reports Menu</title>
<style type="text/css" ></style>
</head>
<sec:authorize access="hasAnyRole('EMDT','MANAGEMENT')">
<body>
<center><br />
<br />
<br />
<br />
<br />
<br />
<br />

<form name="auditReportForm" id="auditReportForm" method="post"
	action="userAuditLog.form" onsubmit="return validate();">
	<input type="hidden" name="<c:out value="${_csrf.parameterName}"/>" value="<c:out value="${_csrf.token}" />" />
<% 
	// Initialise the contents of the From and To date boxes
	String strToDate= new String();
	String strFromDate = new String();
	java.text.SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	java.util.Date date = new Date();
	strToDate = sdf.format(date);


	sdf.setCalendar(Calendar.getInstance());
	Date d = sdf.parse(strToDate);
	Calendar calendar = new GregorianCalendar();
	calendar.setTime(d);
	calendar.add(Calendar.DAY_OF_YEAR, -7);
	strFromDate = sdf.format(calendar.getTime());
	
%>
<table width="60%">
	<tr>
		<td>
		<fieldset class="nobg"><legend>Display Audit Log</legend>
		<fieldset>
		<table width="100%">
			<tr>
				<th align="left" width="20%" height="30px">Select user:</th>
				<td align="left" colspan="2" width="80%">
					<select name="selectedUser">
						<option selected="selected" value="All users">All users</option>
						<c:forEach var="usersByHospitalMap" items="${webapp.usersByHospitalIdMap}">
							<optgroup label="${webapp.hospMap[usersByHospitalMap.key]}">
								<c:forEach var="userId" items="${usersByHospitalMap.value}">
									<option value="${userId}" >${webapp.userIdNameMap[userId]}</option>
								</c:forEach>
							</optgroup>
						</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<th align="left" width="20%" height="40px">Date:</th>
				<td class="input">From <input name="fromDate"
					maxlength="10" size="12" value="<%=strFromDate%>" id="sel1"
					type="text"  placeholder="dd/mm/yyyy">&nbsp;<img title='Click to choose date'
					style='cursor: pointer' src='../images/16x16/date.png'
					id='sel1_DateTrigger' /></td>
				<td class="label">  To <input
					name="toDate" maxlength="10" size="12"  value="<%=strToDate%>"
					id="sel2" type="text"  placeholder="dd/mm/yyyy">&nbsp;<img title='Click to choose date'
					style='cursor: pointer' src='../images/16x16/date.png'
					id='sel2_DateTrigger' /></td>
			</tr>
			<tr>
				<th align="left" width="20%" height="30px">Event type:</th>
				<td align="left" colspan="2" width="80%" >
					<select name="eventType">
						<option selected="selected" value="All events">All events</option>
						<c:forEach var="auditEvent" items="${emdt.auditEventtype }">
							<option	value="${auditEvent}"><c:out value="${auditEvent}"/></option>
						</c:forEach>
					</select>
				</td>
			</tr>
			<tr><td><br/></td></tr>
			<tr>
				<td></td>
				<td colspan="2" align="right">
					<input type="reset" value="Reset" />
					<input type="submit" value="Show Audit Log" />
				</td>
			</tr>
		</table>
		</fieldset>
		</fieldset>
		</td>
	</tr>
</table></form>
<br />
<br />
<div>
	<c:choose>
		<c:when test="${str eq 'referral'}">
			<a href="../referrals/referralsReportMenu.jsp" class="noPrint"><img src="../images/16x16/arrow_undo.png" />&nbsp;&nbsp;Return to previous screen</a>
		</c:when>
		<c:otherwise>
			<a href="../emdt/reportMenu.jsp" class="noPrint"><img src="../images/16x16/arrow_undo.png" />&nbsp;&nbsp;Return to previous screen</a>
		</c:otherwise>
	</c:choose>
</div>
<br />
</center>
</body></sec:authorize>
</html>

			