Snort Oberflaeche erweitert.
[people/pmueller/ipfire-2.x.git] / html / html / include / snortupdateutility.js
1 /*
2 File Info: utility.js - JavaScript library
3
4 Author: Drew S. Dupont
5
6 Date: 2/26/2003 - 8/12/2004 (or present day)
7
8 Description: Utility functions in JavaScript
9 Drew S. Dupont <dsdupont@alumni.indiana.edu>
10 */
11 // Show/Hide HTML Span
12 function showHideHTML(id, content) {
13 // Browser variables
14 var ie45, ns6, ns4, dom = false;
15
16 // Basic browser parse
17 if (navigator.appName == "Microsoft Internet Explorer") {
18 ie45 = parseInt(navigator.appVersion) >= 4;
19 } else if (navigator.appName == "Netscape") {
20 ns6 = parseInt(navigator.appVersion) >= 5;
21 ns4 = parseInt(navigator.appVersion) < 5;
22 }
23 dom = ie45 || ns6;
24
25 // Return if using an old Netscape browser
26 if(ns4) return;
27
28 // Check for type of call supported
29 el = document.all ? document.all[id] : dom ? document.getElementById(id) : document.layers[id];
30
31 // Check if content to be "switched" is ""
32 if (content == "") {
33 // Return old content and replace with ""
34 content = el.innerHTML;
35 el.innerHTML = "";
36 } else {
37 // Replace current content with new content and return ""
38 el.innerHTML = content;
39 content = "";
40 }
41
42 // Return content (either old or "")
43 return content;
44 }
45
46 // Check for special chars
47 function checkForSpecialChars(field, alphaStart, specialCheckChars) {
48 // Local vars
49 var alphaStartChars = /^[a-zA-Z]/;
50 var noSpecialChars = /([^a-zA-Z0-9 _,?!':;\r\t\n\/\\\-\.#@]+)/;
51
52 // Check if should start with an alpha char
53 if (alphaStart) {
54 // Make sure starts with a alpha char
55 if (alphaStartChars.test(field.value)) {
56 // Check for special chars
57 if (noSpecialChars.test(field.value)) {
58 // Return true
59 return true;
60 } else {
61 // Check for specialCheckChars
62 if (specialCheckChars && (specialCheckChars.test(field.value))) {
63 // Return true
64 return true;
65 } else {
66 // Return false
67 return false;
68 }
69 }
70 } else {
71 // Return true
72 return true;
73 }
74 } else {
75 // Check if contains any special chars
76 if (noSpecialChars.test(field.value)) {
77 // Return true
78 return true;
79 } else {
80 // Check for specialCheckChars
81 if (specialCheckChars && (specialCheckChars.test(field.value))) {
82 // Return true
83 return true;
84 } else {
85 // Return false
86 return false;
87 }
88 }
89 }
90 } // End checkForSpecialChars
91
92 // Launch help
93 function launchHelp(helpSrc) {
94 helpWindow = window.open(helpSrc, "helpWindow", "resizable=yes,menubar=no,statusbar=no,titlebar=no,scrollbars=yes,width=400,height=400")
95 helpWindow.moveTo(25, 25);
96 helpWindow.focus();
97 }
98
99 // Image On
100 function imageOn(imageName) {
101 document[imageName].src = eval(imageName + "_over.src");
102 }
103
104 // Image Off
105 function imageOff(imageName) {
106 document[imageName].src = eval(imageName + ".src");
107 }
108
109 // Image Down
110 function imageDown(imageName) {
111 document[imageName].src = eval(imageName + "_down.src");
112 }
113
114 // Image button On
115 function imageButtonOn(item, imageName) {
116 item.src = eval(imageName + "_over.src");
117 }
118
119 // Image button Off
120 function imageButtonOff(item, imageName) {
121 item.src = eval(imageName + ".src");
122 }
123
124 // Image button Down
125 function imageButtonDown(item, imageName) {
126 item.src = eval(imageName + "_down.src");
127 }
128
129 // changeStatus
130 function changeStatus(message) {
131 // Set window status
132 window.status = message;
133
134 // Return true
135 return true;
136 } // End changeStatus
137
138 // isNumeric function
139 function isNumeric(num) {
140 // Boolean var
141 var bolValidNum = true;
142 var digits = "1234567890";
143 var len = num.length;
144
145 // Loop over num
146 for (i = 0; i < len; ++i) {
147 numSub = num.substring(i, i + 1);
148
149 // Test for numeric match
150 if (digits.indexOf(numSub) == -1) {
151 bolValidNum = false;
152 }
153 }
154
155 // Return boolean var
156 return bolValidNum;
157 } // End isNumeric
158
159 // Check for numeric and display nice error
160 function checkNumeric(field, message) {
161 // Is it valid
162 if (!isNumeric(field.value)) {
163 alert(message);
164 field.focus();
165 }
166 } // End checkNumeric
167
168 // Function getInt which return numeric value of passed in string
169 function getInt(str, i, minlength, maxlength) {
170 for (x = maxlength; x >= minlength; --x) {
171 var token = str.substring(i, i + x);
172
173 // Check for numeric
174 if (isNumeric(token)) {
175 return token;
176 }
177 }
178
179 // Return null
180 return null;
181 }
182
183 // Function dateCheck, requires global err variable for passing error messages
184 // and requires the isNumeric function
185 function dateCheck(date, humanname, dateFormat) {
186 // Date validation
187 var date_s = date;
188
189 // If no dateFormat, then set one
190 if (dateFormat == null) {
191 format = "mm/dd/yyyy";
192 } else {
193 format = dateFormat;
194 }
195
196 var date_err = 0; // Possible values are 0, 1
197 var date_year_err = 0; // Possible values are 0, 1
198 var date_month_err = 0; // Possible values are 1-12
199 var date_day_err = 0; // Possible values are 0, 1, 2, 3, 4
200 var i_date_s = 0;
201 var i_format = 0;
202 var err = "";
203 var c = "";
204 var token = "";
205 var token2 = "";
206 var x, y;
207 var year = 0;
208 var month = 0;
209 var date = 0;
210 var bYearProvided = false;
211 var MONTH_NAMES = new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
212
213 // Trim the leading spaces from the string
214 while (date_s.charAt(0) == ' ') {
215 date_s = date_s.slice(1);
216 }
217
218 while (i_format < format.length) {
219 // Get next token from format string
220 c = format.charAt(i_format);
221 token = "";
222
223 while ((format.charAt(i_format) == c) && (i_format < format.length)) {
224 token += format.charAt(i_format);
225 ++i_format;
226 }
227
228 // Extract contents of value based on format token
229 if ((token == "yyyy") || (token == "yy") || (token == "y")) {
230 if (token == "yyyy") { x = 4; y = 4; } // 4-digit year
231 if (token == "yy") { x = 2; y = 2; } // 2-digit year
232 if (token == "y") { x = 2; y = 4; } // 2-or-4-digit year
233
234 year = getInt(date_s, i_date_s, x, y);
235 bYearProvided = true;
236
237 if ((year == null) || (year.length != token.length)) {
238 date_year_err = 1;
239 }
240
241 i_date_s += year.length;
242 } else {
243 if (token == "mmm") { // Month name
244 month = 0;
245
246 for (var i = 0; i < MONTH_NAMES.length; ++i) {
247 var month_name = MONTH_NAMES[i];
248
249 if (date_s.substring(i_date_s, (i_date_s + month_name.length)).toLowerCase() == month_name.toLowerCase()) {
250 month = i + 1;
251
252 if (month > 12) {
253 month -= 12;
254 }
255
256 i_date_s += month_name.length;
257 break;
258 }
259 }
260
261 if ((month == 0) || (month < 1) || (month > 12)) {
262 date_month_err = 1;
263 }
264 } else {
265 if ((token == "mm") || (token == "m")) {
266 x = token.length; y = 2;
267 month = getInt(date_s, i_date_s, x, y);
268
269 if ((month == null) || (month < 1) || (month > 12)) {
270 date_month_err = 1;
271 }
272
273 i_date_s += month.length;
274 } else {
275 if (token=="dd" || token=="d") {
276 x = token.length; y = 2;
277 date = getInt(date_s, i_date_s, x, y);
278
279 if ((date == null) || (date < 1) || (date > 31)) {
280 date_day_err = 1;
281 }
282
283 i_date_s += date.length;
284 } else {
285 if (date_s.substring(i_date_s, (i_date_s + token.length)) != token) {
286 date_err = 1;
287 } else {
288 i_date_s += token.length;
289 }
290 }
291 }
292 }
293 }
294 }
295
296 // If there are any trailing characters left in the date_s, it doesn't match
297 if (i_date_s != date_s.length) {
298 date_err = 1;
299 }
300
301 // Is date valid for month?
302 if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
303 if (date > 30) {
304 date_day_err = 2;
305 }
306 } else {
307 if (month == 2) {
308 // Check for leap year
309 if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) {
310 // Leap year
311 if (date > 29) {
312 date_day_err = 3
313 }
314 } else {
315 if (date > 28) {
316 date_day_err = 4;
317 }
318 }
319 } else {
320 if (date > 31) {
321 date_day_err = 1;
322 }
323 }
324 }
325
326 // Add to the error message, if needed
327 if (date_err != 0) {
328 err += "\n - The " + humanname + " must be a valid date in the format " + format + ".";
329 }
330
331 // Add to the error message, if needed
332 if (date_month_err != 0) {
333 err += "\n - The month must be between 1-12.";
334 }
335
336 // Add to the error message, if needed
337 if (date_year_err != 0) {
338 err += "\n - The " + humanname + " must have a valid year.";
339 }
340
341 // Add to the error message, if needed
342 if (date_day_err != 0) {
343 switch (date_day_err) {
344 case 1:
345 err += "\n - The month you entered in the " + humanname + " can only have between 1 and 31 days.";
346 break;
347 case 2:
348 err += "\n - The month you entered in the " + humanname + " can only have between 1 and 30 days.";
349 break;
350 case 3:
351 err += "\n - The month you entered in the " + humanname + " can only have between 1 and 29 days in a Leap Year.";
352 break;
353 default:
354 err += "\n - The month you entered in the " + humanname + " can only have between 1 and 28 days in a non-Leap Year.";
355 break;
356 }
357 }
358
359 return err;
360 } // End dateCheck
361
362 // Compares two MM/DD/YYY dates for less than (-1), equal to (0), or
363 // greater than (1)
364 function dateCompare(date1, date2) {
365 var localDate1 = new Date(date1.substring(6,10), date1.substring(0,2), date1.substring(3,5));
366 var localDate2 = new Date(date2.substring(6,10), date2.substring(0,2), date2.substring(3,5));
367
368 // Greater than
369 if (localDate1.getTime() > localDate2.getTime()) {
370 return 1;
371 } else {
372 // Less than
373 if (localDate1.getTime() < localDate2.getTime()) {
374 return -1;
375 } else {
376 // Equal
377 return 0;
378 }
379 }
380 } // End dateCompare
381
382 // All-purpose form validation script
383 function checkForm(dataForm) {
384 var msg = "";
385 var stripBlanksStart = /^\s+/g;
386 var stripBlanksEnd = /\s+$/g;
387 var squeezeBlanks = /\s+/g;
388 var stripNonNumbers = /\D+/g;
389 var stripNotDollars = /[^0-9\.]/g;
390 var noSpaces = /\s+/g;
391 var allNumbers = /^\d+$/;
392 var zipCodeCheck = /^(\d{5})$|^(\d{5}-\d{4})$/;
393 var passwordNumbers = /\d{1,}/;
394 var passwordLetters = /\D{1,}/;
395 var emailPattern = /^[a-zA-Z0-9]([a-zA-Z0-9_\-\.]*)@([a-zA-Z0-9_\-\.]*)(\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,2})$/i;
396 var replaceSeps = /[-,\.\/]/g;
397 var time24Format = /^(([0-1]?\d)|(2[0-3])):[0-5]\d(:([0-5]\d))?/;
398 var time12Format = /^(\d|0\d|1[0-2]):[0-5]\d(:[0-5]\d)?( (A|P)\.?M\.?)?/;
399 var ipNetworkAddress = /^((\d{1,2}|[1]\d{2}|2[0-4]\d|25[0-5])(\.(\d{1,2}|[1]\d{2}|2[0-4]\d|25[0-5])){3}){1}((\/(0\.0\.0\.0|128\.0\.0\.0|192\.0\.0\.0|224\.0\.0\.0|240\.0\.0\.0|248\.0\.0\.0|252\.0\.0\.0|254\.0\.0\.0|(255\.(0\.0\.0|128\.0\.0|192\.0\.0|224\.0\.0|240\.0\.0|248\.0\.0|252\.0\.0|254\.0\.0|(255\.(0\.0|128\.0|192\.0|224\.0|240\.0|248\.0|252\.0|254\.0|(255\.(0|128|192|224|240|248|252|254|255))))))))|(\/(\d|[1-2]\d|3[0-2]))){0,1}$/;
400 var ipNetworkPort = /^(\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]){1}((\:|\-)(\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])){0,1}$/;
401 var passwordLength = 6;
402 var error_fields = "";
403 var errors = "";
404
405 // Loop over form elements
406 for (var i = 0; i < dataForm.length; ++i) {
407 var element = dataForm.elements[i];
408
409 // Check for select box
410 if (element.selectbox) {
411 // Check for required
412 if (element.required) {
413 // Check for value
414 if (element.options[element.selectedIndex].value == "") {
415 error_fields += "\n - " + element.humanname + " requires a selection.";
416 }
417 }
418 continue;
419 }
420
421 // Strip the leading and trailing blanks
422 element.value = element.value.replace(stripBlanksStart, '');
423 element.value = element.value.replace(stripBlanksEnd, '');
424
425 // If it is required and is empty, alert
426 if (element.required && (!element.value.length)) {
427 error_fields += "\n - " + element.humanname + " is required.";
428 continue;
429 } else {
430 // If it isn't required and doesn't have any length, skip it
431 if ((! element.required) && (! element.value.length)) {
432 continue;
433 }
434 }
435
436 // Check for special characters
437 if (element.checkspecialchars) {
438 if (checkForSpecialChars(element, element.alphaStart, element.specialChars)) {
439 error_fields += "\n - " + element.humanname + " contains invalid characters.";
440 continue;
441 }
442 }
443
444 // Convert to uppercase if necessary
445 if (element.uppercase) {
446 element.value = element.value.toUpperCase();
447 }
448
449 // Convert to uppercase if necessary
450 if (element.lowercase) {
451 element.value = element.value.toLowerCase();
452 }
453
454 // UCFirst if necessary
455 if (element.ucfirst) {
456 // Squeeze the blanks
457 rs = element.value.replace(squeezeBlanks, ' ');
458 dsegs = rs.split(' ');
459 element.value = "";
460
461 // Loop over chars
462 for (j = 0; j < dsegs.length; ++j) {
463 if (dsegs[j].length > 1) {
464 fl = dsegs[j].substr(0, 1);
465 fl = fl.toUpperCase();
466 rn = dsegs[j].substr(1);
467 rn = rn.toLowerCase();
468 dsegs[j] = fl + rn;
469 }
470
471 // Check for first value
472 element.value = j ? element.value + ' ' + dsegs[j] : dsegs[j];
473 }
474 }
475
476 // Check for equality test
477 if (element.equalto) {
478 // Check for truevalue and use if found, otherwise use value
479 var elementValue1 = element.truevalue ? element.truevalue : element.value;
480 var elementValue2 = element.equaltovalue.truevalue ? element.equaltovalue.truevalue : element.equaltovalue.value;
481
482 // Check for value equality
483 if (elementValue1 != elementValue2) {
484 error_fields +="\n - " + element.humanname + " is not the same as " + element.equaltovalue.humanname;
485 continue;
486 }
487 }
488
489 // Check for less than
490 if (element.lessthan) {
491 // Check for truevalue and use if found, otherwise use value
492 var elementValue1 = element.truevalue ? element.truevalue : element.value;
493 var elementValue2 = element.lessthanvalue.truevalue ? element.lessthanvalue.truevalue : element.lessthanvalue.value;
494
495 // Check for values
496 if ((elementValue1 != '') && (elementValue2 != '')) {
497 // Check for value less than
498 if (elementValue1 >= elementValue2) {
499 error_fields +="\n - " + element.humanname + " must be less than " + element.lessthanvalue.humanname;
500 continue;
501 }
502 }
503 }
504
505 // Check for less than equalto
506 if (element.lessthanequalto) {
507 // Check for truevalue and use if found, otherwise use value
508 var elementValue1 = element.truevalue ? element.truevalue : element.value;
509 var elementValue2 = element.lessthanequaltovalue.truevalue ? element.lessthanequaltovalue.truevalue : element.lessthanequaltovalue.value;
510
511 // Check for values
512 if ((elementValue1 != '') && (elementValue2 != '')) {
513 // Check for value less than equalto
514 if (elementValue1 > elementValue2) {
515 error_fields +="\n - " + element.humanname + " must be less than or equal to " + element.lessthanequaltovalue.humanname;
516 continue;
517 }
518 }
519 }
520
521 // Check for greater than
522 if (element.greaterthan) {
523 // Check for truevalue and use if found, otherwise use value
524 var elementValue1 = element.truevalue ? element.truevalue : element.value;
525 var elementValue2 = element.greaterthanvalue.truevalue ? element.greaterthanvalue.truevalue : element.greaterthanvalue.value;
526
527 // Check for values
528 if ((elementValue1 != '') && (elementValue2 != '')) {
529 // Check for value greater than
530 if (elementValue1 <= elementValue2) {
531 error_fields +="\n - " + element.humanname + " must be greater than " + element.greaterthanvalue.humanname;
532 continue;
533 }
534 }
535 }
536
537 // Check for greater than equalto
538 if (element.greaterthanequalto) {
539 // Check for truevalue and use if found, otherwise use value
540 var elementValue1 = element.truevalue ? element.truevalue : element.value;
541 var elementValue2 = element.greaterthanequaltovalue.truevalue ? element.greaterthanequaltovalue.truevalue : element.greaterthanequaltovalue.value;
542
543 // Check for values
544 if ((elementValue1 != '') && (elementValue2 != '')) {
545 // Check for value greater than equalto
546 if (elementValue1 < elementValue2) {
547 error_fields +="\n - " + element.humanname + " must be greater than or equal to " + element.greaterthanequaltovalue.humanname;
548 continue;
549 }
550 }
551 }
552
553 // Check a price (sort of)
554 if (element.price) {
555 // Strip out currency stuff
556 element.value = element.value.replace(stripNotDollars, '');
557 continue;
558 }
559
560 // Check a telephone number
561 if (element.telephone) {
562 // Strip out parens and spaces
563 rs = element.value.replace(stripNonNumbers, '');
564
565 if (rs.length == 7) {
566 element.value = rs.substr(0, 3) + "-" + rs.substr(3, 4);
567 } else {
568 if (rs.length == 10) {
569 element.value = rs.substr(0, 3) + "-" + rs.substr(3, 3) + "-" + rs.substr(6, 4);
570 } else {
571 error_fields += "\n - " + element.humanname + " is an invalid telephone number.";
572 }
573 }
574 continue;
575 }
576
577 // Check a zip code
578 if (element.zipcode) {
579 if (!zipCodeCheck.test(element.value)) {
580 error_fields +="\n - " + element.humanname + " is an invalid zipcode.";
581 }
582 continue;
583 }
584
585 // Check a password (sort of)
586 if (element.password) {
587 if (element.value.length < passwordLength) {
588 error_fields += "\n - " + element.humanname + " is too short";
589 error_fields += "\n Minimum length is " + passwordLength + " characters.";
590 continue;
591 }
592
593 if (!passwordNumbers.test(element.value)) {
594 error_fields += "\n - " + element.humanname + " must contain at least one number.";
595 continue;
596 }
597
598 if (!passwordLetters.test(element.value)) {
599 error_fields += "\n - " + element.humanname + " must contain at least one letter.";
600 continue;
601 }
602 }
603
604 // Check for all numbers
605 if (element.numeric) {
606 if (!allNumbers.test(element.value)) {
607 error_fields += "\n - " + element.humanname + " is not numeric.";
608 }
609 continue;
610 }
611
612 // Check an email address for validity
613 if (element.email) {
614 element.value = element.value.replace(noSpaces, '');
615
616 if (!emailPattern.test(element.value)) {
617 error_fields += "\n - " + element.humanname + " is not a valid email address.";
618 }
619 continue;
620 }
621
622 // Check a date
623 if (element.date) {
624 error_fields += dateCheck(element.value, element.humanname, element.format);
625 continue;
626 }
627
628 // Check a time
629 if (element.time) {
630 // Check for 24 hour time
631 if (element.time24) {
632 // Check for valid
633 if (!time24Format.test(element.value)) {
634 error_fields += "\n - " + element.humanname + " is not a valid 24 hour time.";
635 }
636 } else {
637 // Check for valid
638 if (!time12Format.test(element.value)) {
639 error_fields += "\n - " + element.humanname + " is not a valid 12 hour time.";
640 }
641 }
642 continue;
643 }
644
645 // Check the lengths
646 if (element.minlen && (element.value.length < element.minlen)) {
647 error_fields += "\n - " + element.humanname + " is too short";
648 error_fields += "\n Minimum length is " + element.minlen + " characters.";
649 continue;
650 }
651
652 if (element.maxlen && (element.value.length > element.maxlen)) {
653 error_fields +="\n - " + element.humanname + " is too long";
654 error_fields +="\n Maximum length is " + element.maxlen + " characters.";
655 continue;
656 }
657
658 // Check for ip/network address
659 if (element.ipnetworkaddress) {
660 if (!ipNetworkAddress.test(element.value)) {
661 error_fields +="\n - " + element.humanname + " is not a valid ip/network address";
662 }
663 continue;
664 }
665
666 // Check for ip/network port
667 if (element.ipnetworkport) {
668 if (!ipNetworkPort.test(element.value)) {
669 error_fields +="\n - " + element.humanname + " is not a valid ip/network port";
670 } else {
671 var searchChar = "";
672 var portArray = "";
673
674 if (element.value.indexOf(":") > -1) {
675 searchChar = ":";
676 } else if (element.value.indexOf("-") > -1) {
677 searchChar = "-";
678 }
679
680 if (searchChar != '') {
681 portArray = element.value.split(searchChar);
682
683 if (portArray.length == 2) {
684 if (parseInt(portArray[0]) > parseInt(portArray[1])) {
685 error_fields +="\n - " + element.humanname + " can not have a start port greater than an end port";
686 }
687 }
688 }
689 }
690 continue;
691 }
692 }
693
694 // Check for any errors
695 if (error_fields == "") {
696 return true;
697 } else {
698 msg = "The following fields have errors:\n";
699 msg += error_fields;
700 alert(msg);
701 return false;
702 }
703 }
704
705 // Clear data
706 function clearData(field, data) {
707 // Check if they equal
708 if (field.value == data) {
709 // Clear data
710 field.value = '';
711 }
712 }
713
714 // Set empty data
715 function setEmptyData(field, data) {
716 // Check if they equal
717 if (! field.value.length) {
718 // Clear data
719 field.value = data;
720 }
721 }
722
723 // Trim whitespace from beginning and end
724 function trim(data) {
725 var objRegExp = /^(\s*)$/;
726
727 // Check for all spaces
728 if (objRegExp.test(data)) {
729 data = data.replace(objRegExp, '');
730
731 if (data.length == 0)
732 return data;
733 }
734
735 // Check for leading & trailing spaces
736 objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
737
738 if (objRegExp.test(data)) {
739 // Remove leading and trailing whitespace characters
740 data = data.replace(objRegExp, '$2');
741 }
742
743 return data;
744 }