<%@ include file="/includes.jsp"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.GregorianCalendar"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Referrals Users Statistics Report</title>
<script language="javascript">

function validate() {

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

//Check occurences of '/' symbol
        var checkOK = "0123456789/";
        var checkStr = document.summaryReportForm.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.summaryReportForm.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.summaryReportForm.sel1.select();
                    document.summaryReportForm.sel1.focus();
                    return (false); 
                 } 
                 
                 //Checks for Invalid Date formats
		        a=document.summaryReportForm.sel1.value.indexOf('/')
		        b=document.summaryReportForm.sel1.value.indexOf('/',a+1)
		        fs_date=document.summaryReportForm.sel1.value.substring(0,a)
		        fs_month=document.summaryReportForm.sel1.value.substring(a+1,b)
		        fs_year=document.summaryReportForm.sel1.value.substring(b+1,10)

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

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

			var checkStr1 = document.summaryReportForm.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.summaryReportForm.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.summaryReportForm.sel2.select();
                        document.summaryReportForm.sel2.focus();
                        return (false); 
                 } 
                 //Checks for Invalid Date Formats
                 c=document.summaryReportForm.sel2.value.indexOf('/')
                d=document.summaryReportForm.sel2.value.indexOf('/',c+1)
                r_date=document.summaryReportForm.sel2.value.substring(0,c)
                r_month=document.summaryReportForm.sel2.value.substring(c+1,d)
                r_year=document.summaryReportForm.sel2.value.substring(d+1,10)

                
                if(document.summaryReportForm.sel2.value.substring(d+1,10).length !=4){
                    alert("Invalid To Date.\nFormat accepted is dd/mm/yyyy.");
                    document.summaryReportForm.sel2.select();
                    document.summaryReportForm.sel2.focus();
                    return (false); 
                }
                
                if((document.summaryReportForm.sel2.value.substring(c+1,d).length < 1) || (document.summaryReportForm.sel2.value.substring(c+1,d).length > 2)){
                    alert("Invalid To Date.\nFormat accepted is dd/mm/yyyy.");
                    document.summaryReportForm.sel2.select();
                    document.summaryReportForm.sel2.focus();
                    return (false); 
                }
                
                if((document.summaryReportForm.sel2.value.substring(0,c).length < 1) || (document.summaryReportForm.sel2.value.substring(0,c).length > 2)){
                    alert("Invalid To Date.\nFormat accepted is dd/mm/yyyy.");
                    document.summaryReportForm.sel2.select();
                    document.summaryReportForm.sel2.focus();
                    return (false); 
                }
                 if(r_month>12 || r_month<1){
                    alert("Invalid To Date in month.");
                    document.summaryReportForm.sel2.select();
                    document.summaryReportForm.sel2.focus();
                    return (false); 
                }
                
                if(r_date>31 || r_date<1){
                    alert("Invalid To Date in days.");
                    document.summaryReportForm.sel2.select();
                    document.summaryReportForm.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.summaryReportForm.sel2.select();
	                    document.summaryReportForm.sel2.focus();
	                    return (false); 
	                }
                
                if(r_month==2)
                	if(r_date>29){
	                    alert("Invalid To Date.");
	                    document.summaryReportForm.sel2.select();
	                    document.summaryReportForm.sel2.focus();
	                    return (false); 
                	}
                
                if((r_year%4)!=0)
                	if(r_month==2)
                		if(r_date>28){
		                    alert("Invalid To Date.");
		                    document.summaryReportForm.sel2.select();
		                    document.summaryReportForm.sel2.focus();
		                    return (false); 
                		}
                
                var date1,date2,dateT1,dateT2;
                var vDateSplit = document.summaryReportForm.sel1.value.split("/");
				date1 = vDateSplit[1] + "/" + vDateSplit[0] + "/" + vDateSplit[2];
				var vDateSplit1 = document.summaryReportForm.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.summaryReportForm.sel1.select();
                        document.summaryReportForm.sel1.focus();
                        return false;
                }
                
                showPleaseWait();
}
</script>
</head>
<body>
<% 
	// 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());
	
%>

<br/>
&nbsp;
<br/>
&nbsp;
<br/>
<sec:authorize access="hasAnyRole('EMDT','MANAGEMENT')">
<form name="summaryReportForm" id="summaryReportForm" method="post" action="../referrals/refSummaryReport.form?${_csrf.parameterName}=${_csrf.token}" onsubmit="return validate();">
	<%String str1 = (String)request.getParameter("module");%>
		<%request.setAttribute("module",str1);%>
	<input type="hidden" name="module" value="<%=(String)request.getParameter("module") %>"/>
<table width="56%" border="2" cellpadding="0" cellspacing="0"
	align="center">
	
	<tbody>
		<tr>
			<td>
			<table class="reportSummary" width="100%" border="0"
				cellpadding="0" cellspacing="0">
				<tbody>
					<tr>
						<td class="dialogTitle"><b>Referrals Summary Report</b></td>
					</tr>
					<tr>
						<td>
						<table width="100%" border="0" cellpadding="10" cellspacing="0">

							<tbody>
								<tr>
									<td>
									<table border="0" cellpadding="0" cellspacing="0" width="90%">
										<tbody>
											<tr>
												<td class="label" style="width:10%"><b>Date range</b>&nbsp;</td>
												<td class="input"  style="width:8%">From</td>
												<td class="input" width="35%"><input name="fromDate"
													maxlength="10" size="12" value="<%=strFromDate%>" id="sel1"
													type="text"  placeholder="dd/mm/yyyy"><img title='Click to choose date' style='cursor: pointer' src='../images/16x16/date.png' id='sel1_DateTrigger'/></td>
												<td class="label" colspan="2" style="width:3%">To</td>
												<td class="input" width="40%" align="left"><input
													name="toDate" maxlength="10" size="12" value="<%=strToDate%>"
													id="sel2" type="text"  placeholder="dd/mm/yyyy"><img title='Click to choose date' style='cursor: pointer' src='../images/16x16/date.png' id='sel2_DateTrigger'/></td>
											</tr>
										</tbody>
									</table>
									<br>
									<br>
									
									<table border="0" cellpadding="0" cellspacing="0" width="100%">
										<tbody>
											<tr>
												<td width="2"></td>
												<td class="input" align="right"><input value="Submit" type="submit">
												</td>
												<td class="input">&nbsp;</td>
											</tr>
										</tbody>
									</table>
									</td>
								</tr>
							</tbody>
						</table>
						</td>
					</tr>
					<tr>
						<td colspan="2" class="dialogSpacer"></td>
					</tr>
					<tr>
						<td colspan="2" class="dialogFooter"></td>
					</tr>
				</tbody>
			</table>
			</td>
		</tr>
	</tbody>
</table>
</form></sec:authorize>
<br/>
<br/>
<center><div><a href="referralsReportMenu.jsp" class="noPrint"><img
	src="../images/16x16/action_back.png" border="0" alt="Back" /> Return
to previous screen</a></div>

</center>
</body>
</html>