Sorting & Cell Merge, commons-upload

LegalAdviceAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import vo.CaseLegalAdvice;
import vo.Chapter;
import vo.Enactment;
import vo.EnactmentView;
import vo.LegalAdviceUpload;
import vo.OffenseQuote;

import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import xx.utils.DbManager;

public class LegalAdviceAction extends DispatchAction {
final ResourceBundle uploadSetting = ResourceBundle
.getBundle("xx.properties.uploadSettings");

public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

final SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy",
Locale.ENGLISH);

final String savePath = uploadSetting
.getString("upload.dir.prosecutions");

String fileNo = "";
String subject = "";
String legalAdviceStatus = "";
String legalAdviceRemarks = "";
String legalAdviceDate = "";
String authRefInd = "";
String authRef = "";
String defendant = "";
String defendantEmployer = "";
String defendantEmployee = "";
String ordinance = "";
String keepFileNos = "";
List fileContent = new ArrayList();

List keepFileNosList = new ArrayList();
List uploadFileList = new ArrayList();
String reqCode = "";
String caseNo = "";

final Timestamp now = new Timestamp(new java.util.Date().getTime());
String login_id = (String) request.getSession()
.getAttribute("login_id");

final DiskFileUpload upload = new DiskFileUpload();
final List list = upload.parseRequest(request);
final Iterator it = list.iterator();
while (it.hasNext()) {
final FileItem item = (FileItem) it.next();
if (item.isFormField()) {
if (getParamter(item, "subject")) {
subject = item.getString();
} else if (getParamter(item, "ordinance")) {
ordinance = item.getString();
} else if (getParamter(item, "legalAdviceDate")) {
legalAdviceDate = item.getString();
} else if (getParamter(item, "legalAdviceStatus")) {
if ("a".equals(item.getString())) {
legalAdviceStatus = "Accepted";
} else if ("na".equals(item.getString())) {
legalAdviceStatus = "Not Accepted";
} else {
legalAdviceStatus = "Not Mentioned";
}
} else if (getParamter(item, "legalAdviceRemarks")) {
legalAdviceRemarks = item.getString();
} else if (getParamter(item, "authRefInd")) {
authRefInd = item.getString();
} else if (getParamter(item, "defendantEmployer")) {
defendantEmployer = item.getString();
} else if (getParamter(item, "defendantEmployee")) {
defendantEmployee = item.getString();
} else if (getParamter(item, "fileNo")) {
fileNo = item.getString();
} else if (getParamter(item, "authRef")) {
authRef = item.getString();
} else if (getParamter(item, "reqCode")) {
reqCode = item.getString();
} else if (getParamter(item, "caseNo")) {
caseNo = item.getString();
} else if (getParamter(item, "keepFileNos")) {
keepFileNos = item.getString();
}
} else {
String fileName = item.getName();
if (fileName != null && fileName.length() > 0) {
if (fileName.indexOf("\\") != -1) {
PatternCompiler compiler = new Perl5Compiler();
PatternMatcher matcher = new Perl5Matcher();
Pattern pattern = compiler.compile("^.+\\\\(.+)$");
if (matcher.matches(fileName, pattern)) {
MatchResult ms = matcher.getMatch();
System.out
.println("fileName full path " + fileName);
fileName = ms.group(1);
System.out.println("fileName " + fileName);
}
}

uploadFileList.add(fileName);
fileContent.add(item);
}
}
}

if (defendantEmployer.length() > 0 && defendantEmployee.length() > 0) {
defendant = defendantEmployer + "/" + defendantEmployee;
} else if (defendantEmployer.length() > 0) {
defendant = defendantEmployer;
} else {
defendant = defendantEmployee;
}

System.out.println("caseNo " + caseNo + " , subject " + subject
+ " ,legalAdviceDate " + legalAdviceDate + " , ordinance "
+ ordinance + " , keepFileNos " + keepFileNos);

final Connection conn = DbManager.getConnection();

if ("edit".equals(reqCode)) {
System.out.println("edit legail advice");
String sql = "update case_legal_advice set offense_code = ?, file_no = ?, subject = ?, "
+ "legal_advice_date = ?, legal_advice_status = ?, legal_advice_remarks = ?, "
+ "defendant = ?, auth_ref_ind = ?, auth_ref = ?,"
+ "last_update = ?, update_by = ? where case_seq_no = ?";
PreparedStatement ps = conn.prepareStatement(sql);

ps.setString(1, ordinance);
ps.setString(2, fileNo);
ps.setString(3, subject);
ps.setDate(4, new Date(sf.parse(legalAdviceDate).getTime()));
ps.setString(5, legalAdviceStatus);
ps.setString(6, legalAdviceRemarks);
ps.setString(7, defendant);
ps.setString(8, authRefInd);
ps.setString(9, authRef);

ps.setTimestamp(10, now);
ps.setString(11, login_id);
ps.setInt(12, Integer.parseInt(caseNo));

ps.executeUpdate();
ps.close();

StringTokenizer st = new StringTokenizer(keepFileNos, "|");
while (st.hasMoreTokens()) {
String token = st.nextToken();
System.out.println("keep file No. " + token);
keepFileNosList.add(token);
}

String fileSql = "select upload_seq_no, file_name from legal_advice_upload where case_seq_no = ? order by upload_seq_no";
System.out.println("fileSql " + fileSql);
ps = conn.prepareStatement(fileSql);
ps.setString(1, caseNo);
ResultSet rs = ps.executeQuery();

List toDeleteFilesNo = new ArrayList();
while (rs.next()) {
String upload_seq_no = rs.getString("upload_seq_no");
String file = rs.getString("file_name");

if (!keepFileNosList.contains(upload_seq_no)) {
toDeleteFilesNo.add(upload_seq_no);
if (new File(savePath + file).delete()) {
System.out.println("file " + file + " is deleted.");
}
}
}
rs.close();
ps.close();

Iterator delItr = toDeleteFilesNo.iterator();
String deleteFileSql = "delete legal_advice_upload where upload_seq_no = ?";
ps = conn.prepareStatement(deleteFileSql);
while (delItr.hasNext()) {
String uploadNo = (String) delItr.next();
ps.setString(1, uploadNo);
ps.executeUpdate();
}
ps.close();

} else {
System.out.println("add legail advice.");
int seq = 0;
Statement stmt = conn.createStatement();
ResultSet rs = stmt
.executeQuery("select case_legal_advice_SQ.nextval as seq from dual");

while (rs.next()) {
seq = rs.getInt("seq");
}
rs.close();
stmt.close();

System.out.println("case_legal_advice_SQ " + seq);
caseNo = String.valueOf(seq);
String sql = "insert into case_legal_advice(case_seq_no, offense_code, file_no, subject, legal_advice_date, "
+ "legal_advice_status, legal_advice_remarks, defendant, auth_ref_ind, auth_ref, "
+ "last_update, update_by) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
final PreparedStatement ps = conn.prepareStatement(sql);

ps.setInt(1, seq);
ps.setString(2, ordinance);
ps.setString(3, fileNo);
ps.setString(4, subject);
ps.setDate(5, new Date(sf.parse(legalAdviceDate).getTime()));
ps.setString(6, legalAdviceStatus);
ps.setString(7, legalAdviceRemarks);
ps.setString(8, defendant);
ps.setString(9, authRefInd);
ps.setString(10, authRef);

ps.setTimestamp(11, now);
ps.setString(12, login_id);

ps.executeUpdate();
ps.close();
}

Iterator itr = uploadFileList.iterator();
int i = 0;
while (itr.hasNext()) {
String fileName = (String) itr.next();
int uploadReq = 0;
Statement stmt2 = conn.createStatement();
ResultSet rs2 = stmt2
.executeQuery("select legal_advice_upload_SQ.nextval as seq from dual");

while (rs2.next()) {
uploadReq = rs2.getInt("seq");
}
rs2.close();
stmt2.close();
System.out.println("legal_advice_upload_SQ " + uploadReq);

String sql2 = "insert into legal_advice_upload (upload_seq_no, case_seq_no, file_name, last_update, update_by) values(?, ?, ?, ?, ?)";
PreparedStatement ps2 = conn.prepareStatement(sql2);
ps2.setInt(1, uploadReq);
ps2.setInt(2, Integer.parseInt(caseNo));
ps2.setString(3, fileName);
ps2.setTimestamp(4, now);
ps2.setString(5, login_id);
ps2.executeUpdate();
ps2.close();

FileItem item = (FileItem) fileContent.get(i);

System.out.println("save file " + fileName + " => " + savePath
+ String.valueOf(uploadReq));
item.write(new File(savePath + String.valueOf(uploadReq)));
}

return mapping.findForward("redirectList");
}

private boolean getParamter(FileItem item, String paramName) {
if (item.getString() != null && item.getFieldName().equals(paramName)) {
return true;
}
return false;
}

public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String orderBy = request.getParameter("orderBy");
System.out.println("orderBy " + orderBy);

Object lastFieldSort = request.getSession().getAttribute(
"lastFieldSort");
FieldSort fieldSort = null;

if (lastFieldSort == null) {
fieldSort = new FieldSort();
fieldSort.setSortBy(orderBy);
fieldSort.setAsc(Boolean.TRUE);
} else {
fieldSort = (FieldSort) lastFieldSort;

if (fieldSort.getSortBy() != null
&& fieldSort.getSortBy().equals(orderBy)) {
if (fieldSort.getAsc().equals(Boolean.TRUE)) {
fieldSort.setAsc(Boolean.FALSE);
} else {
fieldSort.setAsc(Boolean.TRUE);
}
} else {
fieldSort.setSortBy(orderBy);
fieldSort.setAsc(Boolean.TRUE);
}
}

final Connection conn = DbManager.getConnection();

Statement stmt = conn.createStatement();
String sql = "select e.enact_Chap, e.title, c.chapter, o.sect_reg, o.description, cla.case_seq_no, cla.file_no, cla.subject, "
+ "cla.legal_advice_date, cla.legal_advice_status, cla.defendant, cla.auth_ref_ind, cla.auth_ref, u.upload_seq_no, u.file_name "
+ "from Enactment e, Chapter c, offense_quote o, case_legal_advice cla, legal_advice_upload u "
+ "where e.enact_chap = c.enact_chap and c.Chapter = o.Chapter and o.offense_code = cla.offense_code(+) and "
+ "cla.case_seq_no = u.case_seq_no(+)";
if (orderBy != null && orderBy.length() > 0) {
sql += " order by "
+ fieldSort.getSortBy()
+ (fieldSort.getAsc().equals(Boolean.TRUE) ? " asc"
: " desc") + ", cla.file_no";
} else {
sql += " order by c.Chapter, cla.file_no";
orderBy = "c.chapter";
fieldSort = new FieldSort();
fieldSort.setSortBy(orderBy);
fieldSort.setAsc(Boolean.TRUE);
}

request.getSession().setAttribute("lastFieldSort", fieldSort);

System.out.println("sql = " + sql);
ResultSet rs = stmt.executeQuery(sql);
List list = new ArrayList();
while (rs.next()) {
EnactmentView view = new EnactmentView();
Enactment enactment = new Enactment();
Chapter chapter = new Chapter();

...
String enactChap = rs.getString("enact_Chap");
String title = rs.getString("title");
String chapterValue = rs.getString("chapter");
...

list.add(view);
}
rs.close();
stmt.close();

request.setAttribute("list", list);
request.setAttribute("sortBy", fieldSort.getSortBy());
if (fieldSort.getAsc().equals(Boolean.FALSE)) {
request.setAttribute("order", "down");
} else {
request.setAttribute("order", "up");
}

String savePath = uploadSetting.getString("upload.dir.prosecutions");
request.setAttribute("savePath", savePath);

return mapping.findForward("list");
}

public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String id = request.getParameter("id");
String fileName = "";

final Connection conn = DbManager.getConnection();
String sql = "select file_name from legal_advice_upload where upload_seq_no = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, id);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
fileName = rs.getString("file_name");
}
rs.close();
ps.close();

System.out.println("download file : " + fileName);
String savePath = uploadSetting.getString("upload.dir.prosecutions");
FileInputStream in = new FileInputStream(savePath + "/" + id);
byte[] buffer = new byte[8092];
int i = 0;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ new String(fileName.getBytes("UTF-8"), "ISO_8859_1") + "\"");

while ((i = in.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, i);
}

response.getOutputStream().flush();
return null;
}
}

class FieldSort implements Serializable {
private String sortBy;
private Boolean asc;

public String getSortBy() {
return sortBy;
}

public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}

public Boolean getAsc() {
return asc;
}

public void setAsc(Boolean asc) {
this.asc = asc;
}
}

list.jsp (sort, cell merge)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function init(){}

function _w_table_rowspan(_w_table_id,_w_table_colnum){
_w_table_firsttd = "";
_w_table_currenttd = "";
_w_table_SpanNum = 0;
_w_table_Obj = $(_w_table_id + " tr td:nth-child(" + _w_table_colnum + ")");
_w_table_Obj.each(function(i){
if(i==0){
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
}else{
_w_table_currenttd = $(this);
if(_w_table_firsttd.text()==_w_table_currenttd.text()){
var firstSection = _w_table_firsttd.parent().find('td:eq(4)').text();
var currentSection = _w_table_currenttd.parent().find('td:eq(4)').text();
if(firstSection == currentSection){
_w_table_SpanNum++;
_w_table_currenttd.hide(); //remove();
_w_table_firsttd.attr("rowSpan",_w_table_SpanNum);
}
}else{
_w_table_firsttd = $(this);
_w_table_SpanNum = 1;
}
}
});
}

var sorting = function(title, order){
var position = 0;
var header = '';
$("#result thead th").each(function(index, dom){
if(title == $(dom).attr('id')){
position = index;
header = $(dom).text();
}
});

$("#result thead th:eq(" + position + ")").html("<font color='yellow'>" + header + "</font>&nbsp;<img src='images/arrow_" + order + ".gif'/>");
}

$(function(){
$("#result thead th").each(function(){
$(this).css({"cursor":"pointer"});
$(this).click(function(){
var id = $(this).attr('id');
$(":hidden[name='orderBy']").val(id);

var form = $("form[name='form1']");
form.submit();
});
});

var sortBy = '<bean:write name="sortBy"/>';
var order = '<bean:write name="order"/>';

sorting(sortBy, order);

for(var i = 1; i < 10; i++){
_w_table_rowspan("#result", i);
}
});

</script>

<div id="content_layer" style="z-index:1">
<form name="form1" method="post" action="legalAdvice.do?reqCode=list" class="form">
<p class="form_text_h" align="right"><font size="1">AM0101001</font></p>
<p class="form_text_h">View Legal Advice:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table id="result" border="1">
<thead>
<tr>
<th class="formheader" id="c.chapter">Chapter</th>
<th class="formheader" id="o.sect_reg">Section/Regulation</th>
<th class="formheader" id="o.description">Description of Offence</th>
<th class="formheader" id="cla.file_no">File No.</th>
<th class="formheader" id="cla.subject">Subject</th>
<th class="formheader" id="cla.legal_advice_date">Legal Advice Date</th>
<th class="formheader" id="cla.legal_advice_status">Legal Advice</th>
<th class="formheader" id="cla.defendant">Defendant</th>
<th class="formheader" id="cla.auth_ref_ind">Authority Reference</th>
<th class="formheader" id="u.file_name">File Name</th>
</tr>
</thead>
<tbody>
<logic:iterate id="view" name="list" type="vo.EnactmentView" scope="request">
<bean:define id="enactment" name="view" property="enactment" type="vo.Enactment"/>
<bean:define id="chapter" name="view" property="chapter" type="vo.Chapter"/>
<bean:define id="offenseQuote" name="view" property="offenseQuote" type="vo.OffenseQuote"/>
<bean:define id="legalAdviceUpload" name="view" property="legalAdviceUpload" type="vo.LegalAdviceUpload"/>
<bean:define id="caseLegalAdvice" name="view" property="caseLegalAdvice" type="vo.CaseLegalAdvice"/>

<tr>
<td nowrap="nowrap">Cap.<bean:write name="enactment" property="enactChap"/></td>
<td><bean:write name="offenseQuote" property="sectReg"/></td>
<td><bean:write name="offenseQuote" property="description"/></td>
<td><bean:write name="caseLegalAdvice" property="fileNo"/></td>
<td>
<logic:equal name="caseLegalAdvice" property="subject" value="Nil">
<bean:write name="caseLegalAdvice" property="subject"/>
</logic:equal>
<logic:notEqual name="caseLegalAdvice" property="subject" value="Nil">
<a href='legalAdvice.do?reqCode=view&caseSeqNo=<bean:write name="caseLegalAdvice" property="caseSeqNo"/>'><bean:write name="caseLegalAdvice" property="subject"/></a>
</logic:notEqual>
</td>
<td><bean:write name="caseLegalAdvice" property="legalAdviceDateFormat"/></td>
<td><bean:write name="caseLegalAdvice" property="legalAdviceStatus"/></td>
<td><bean:write name="caseLegalAdvice" property="defendant"/></td>
<td><bean:write name="caseLegalAdvice" property="authRefInd"/></td>
<td><a target='_blank' href='legalAdvice.do?reqCode=download&id=<bean:write name="legalAdviceUpload" property="uploadSeqNo"/>'><bean:write name="legalAdviceUpload" property="fileName"/></a></td>
</tr>
</logic:iterate>
</tbody>
</table>
</td>
</tr>
</table>
<input type="hidden" name="orderBy" value=""/>
<button onclick="addProsecution()">Legal Advice Update</button>
</form>

add.jsp (leave year checking with JS, form upload)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<%@ page import="vo.*, java.text.SimpleDateFormat"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<bean:define id="caseLegalAdvice" name="caseLegalAdvice" scope="request" type="vo.CaseLegalAdvice"/>
<bean:define id="reqCode" name="reqCode" scope="request" type="java.lang.String"/>

<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function init(){}

function calDays(year,month){
var days=0;
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31;break;
case 4: case 6: case 9: case 11: days=30;break;
case 2: if(isLeapYear(year)) days=29;
else days=28;
break;
}
return days;
}

function isLeapYear(year){
if((year %4==0 && year %100!=0) || (year %400==0)) return true;
else return false;
}

function checkFiles(){
var exist = false;
$("input[type='file']").each(function(){
if($(this).val() != ''){
exist = true;
}
});

if(!exist){
alert('No file selected.');
return false;
}
return true;
}

$(function(){
$("form[name='form1']").submit(function(){
var regexp = /^(\d{2})\/(\d{2})\/(\d{4})$/;

if($("input[name='fileNo']").val() == ''){
alert('File No. is empty.');
return false;
}


if($("input[name='subject']").val() == ''){
alert('Subject is empty.');
return false;
}

var legalAdviceDate = $("input[name='legalAdviceDate']").val();
if(legalAdviceDate == ''){
alert('Legal Advice Date is empty.');
return false;
}

if(!regexp.test(legalAdviceDate)){
alert('Legal Advice Date is invalid.');
return false;
}else{
var match = regexp.exec(legalAdviceDate);
var year = parseInt(match[3], 10);
var month = parseInt(match[2], 10);
var day = parseInt(match[1], 10);
var maxDay = calDays(year, month);
if(day > maxDay){
alert('Legal Advice Date is invalid.');
return false;
}
}

if($("#ordinance").val()==null){
alert('Related Ordinance is empty.');
return false;
}

var checked = false;
$(":radio[name='legalAdviceStatus']").each(function(){
if($(this).attr('checked')){
checked = true;
}
});

if(!checked){
alert('Legal Advice is empty.');
return false;
}

checked = false;
$(":checkbox[name^='defendant']").each(function(){
if($(this).attr('checked')){
checked = true;
}
});

if(!checked){
alert('Defendant is empty.');
return false;
}

checked = false;
$(":radio[name='authRefInd']").each(function(){
if($(this).attr('checked')){
checked = true;
}
});

if(!checked){
alert('Any Authority Reference Mentioned is empty.');
return false;
}


if('<%=reqCode%>' == 'edit'){
if($(".deleteFile").length < 1){
if(!checkFiles()){
return false;
}
}
}else{
if(!checkFiles()){
return false;
}
}

var filesNo = '';
$(".uploadFiles").each(function(){
filesNo += $(this).find('span').text() + "|" ;
});

$(":hidden[name='keepFileNos']").val(filesNo);
return true;
});

$(".deleteFile").click(function(){
$(this).parent().remove();
});

var fileName = 3;
$("#addFile").click(function(){
$("#files").append("<input type='file' accept='application/pdf' name='file" + fileName + "' size='40' /><br/>");
fileName++;
});

});
</script>

<div id="content_layer" style="z-index:1">
<form name="form1" method="post" action="legalAdvice.do?reqCode=save" class="form" enctype="multipart/form-data">
<p class="form_text_h" align="right"><font size="1">XXXX</font></p>
<p class="form_text_h">Legal Advice Update:</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="formheader" width="560">Legal Advice:</td>
</tr>
<tr>
<td valign="top" width="560">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="35%"><b>File No.:<span style="color:red">*</span></b></td>
<td width="65%">
<input type="text" name="fileNo" size="40" value="" maxlength="50" value='<bean:write name="caseLegalAdvice" property="fileNo"/>'/>
</td>
</tr>
<tr>
<td width="35%"><b>Subject:<span style="color:red">*</span></b></td>
<td width="65%">
<input type="text" name="subject" size="40" value='<bean:write name="caseLegalAdvice" property="subject"/>' maxlength="50" />
</td>
</tr>

<tr>
<td width="35%"><b>Legal Advice Date(dd/mm/yyyy):<span style="color:red">*</span></b></td>
<td width="65%">
<input type="text" name="legalAdviceDate" size="40" value='<bean:write name="caseLegalAdvice" property="legalAdviceDate" format="dd/MM/yyyy"/>' maxlength="40" >
</td>
</tr>
<tr>
<td width="35%"><b>Related Ordinance:<span style="color:red">*</span></b></td>
<td width="65%">
<select id="ordinance" name="ordinance" size="7" style="max-width:260px;width:260px">
<%
List ordinance = (List)request.getAttribute("ordinance");
String offenseCode = caseLegalAdvice.getOffenseCode();
Iterator it = ordinance.iterator();
while(it.hasNext()){
List items = (List)it.next();
if(offenseCode != null && offenseCode.equals(items.get(0))){
out.print("<option selected='selected' title='Cap." + items.get(1) + " - " + items.get(2) + " - " + items.get(3) + "' value='" + items.get(0) + "'>Cap." + items.get(1) + " - " + items.get(2) + " - " + items.get(3) + "</option>");
}else{
out.print("<option title='Cap." + items.get(1) + " - " + items.get(2) + " - " + items.get(3) + "' value='" + items.get(0) + "'>Cap." + items.get(1) + " - " + items.get(2) + " - " + items.get(3) + "</option>");
}
}
%>
</select>
</td>
</tr>
<tr>
<td><b>Legal Advice:<span style="color:red">*</span></b></td>
<td><input id="accepted" type="radio" name="legalAdviceStatus" value="a"><label for="accepted">Accepted</label></input><input id="notAccepted" type="radio" name="legalAdviceStatus" value="na"><label for="notAccepted">Not Accepted</label></input><input id="notMentioned" type="radio" name="legalAdviceStatus" value="nm"><label for="notMentioned">Not Mentioned</label></input></td>
</tr>
<tr>
<td><b>Legal Advice Remarks:</b></td>
<td><textarea name="legalAdviceRemarks" rows="5" cols="40"></textarea></td>
</tr>
<tr>
<td><b>Defendant:<span style="color:red">*</span></b></td>
<td><input id="employer" type="checkbox" name="defendantEmployer" value="employer"><label for="employer">Employer</label></input><input id="employee" type="checkbox" name="defendantEmployee" value="employee"><label for="employee">Employee</label></input></td>
</tr>
<tr>
<td><b>Any Authority Reference Mentioned(Y/N):<span style="color:red">*</span></b></td>
<td><input id="authRefIndY" type="radio" name="authRefInd" value="Y"><label for="authRefIndY">Y</label></input><input id="authRefIndN" type="radio" name="authRefInd" value="N"><label for="authRefIndN">N</label></input></td>
</tr>
<tr>
<td><b>Authority Reference:</b></td>
<td><input type="text" name="authRef" size="40" value="" maxlength="50" /></td>
</tr>
<tr>
<td width="35%" valign="top"><b>File Name:<span style="color:red">*</span></b></td>
<td width="65%">
<%
if(reqCode !=null && reqCode.equals("edit")){
final List fileList = caseLegalAdvice.getUploadFiles();
Iterator fileIt = fileList.iterator();
while(fileIt.hasNext()){
String[] file = (String[])fileIt.next();
out.print("<div class='uploadFiles'><span style='display:none'>" + file[0] + "</span><a target='_blank' href='upload/prosecutions/" + file[1] + "'>" + file[1] + "</a>&nbsp;<img class='deleteFile' style='cursor:pointer' src='images/Remove-icon.png'/>");
}
}
%>
<div id="files">
<input type='file' accept='application/pdf' name='file' size='40' />
<input type='file' accept='application/pdf' name='file1' size='40' />
<input type='file' accept='application/pdf' name='file2' size='40' />
<img src="images/Add-icon.png" style="cursor:pointer" id="addFile"/>
<br/>
</td>
</tr>
</table>
<hr noshade size="1" color="#FFFFFF" width="100%">
</td>
</tr>
<tr>
<td width="560">
<div align="right">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"> <%=(errMsg==null) ? "" : errMsg%> </td>
</tr>
<tr>
<td width="61%">
<input type="hidden" name="reqCode" value='<%=reqCode%>'/>
<input type="hidden" name="keepFileNos" value=''/>
<input type="hidden" name="caseNo" value='<%=request.getParameter("caseNo")%>'/>

<input type="submit" name="save" value="Save">
<%
if(reqCode != null && reqCode.equals("edit")){
out.print("<input type='submit' name='delete' value='Delete'>");
}
%>
</td>
<td width="39%">
<div align="right">
</td>
</tr>
</table>

</td>
</tr>
</table>
<p><br>
</p>
</form>