]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/html/include/snortupdateutility.js
2 File Info: utility.js - JavaScript library
6 Date: 2/26/2003 - 8/12/2004 (or present day)
8 Description: Utility functions in JavaScript
9 Drew S. Dupont <dsdupont@alumni.indiana.edu>
11 // Show/Hide HTML Span
12 function showHideHTML(id
, content
) {
14 var ie45
, ns6
, ns4
, dom
= false;
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;
25 // Return if using an old Netscape browser
28 // Check for type of call supported
29 el
= document
.all
? document
.all
[id
] : dom
? document
.getElementById(id
) : document
.layers
[id
];
31 // Check if content to be "switched" is ""
33 // Return old content and replace with ""
34 content
= el
.innerHTML
;
37 // Replace current content with new content and return ""
38 el
.innerHTML
= content
;
42 // Return content (either old or "")
46 // Check for special chars
47 function checkForSpecialChars(field
, alphaStart
, specialCheckChars
) {
49 var alphaStartChars
= /^[a-zA-Z]/;
50 var noSpecialChars
= /([^a-zA-Z0-9 _,?!':;\r\t\n\/\\\-\.#@]+)/;
52 // Check if should start with an alpha char
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
)) {
61 // Check for specialCheckChars
62 if (specialCheckChars
&& (specialCheckChars
.test(field
.value
))) {
75 // Check if contains any special chars
76 if (noSpecialChars
.test(field
.value
)) {
80 // Check for specialCheckChars
81 if (specialCheckChars
&& (specialCheckChars
.test(field
.value
))) {
90 } // End checkForSpecialChars
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);
100 function imageOn(imageName
) {
101 document
[imageName
].src
= eval(imageName
+ "_over.src");
105 function imageOff(imageName
) {
106 document
[imageName
].src
= eval(imageName
+ ".src");
110 function imageDown(imageName
) {
111 document
[imageName
].src
= eval(imageName
+ "_down.src");
115 function imageButtonOn(item
, imageName
) {
116 item
.src
= eval(imageName
+ "_over.src");
120 function imageButtonOff(item
, imageName
) {
121 item
.src
= eval(imageName
+ ".src");
125 function imageButtonDown(item
, imageName
) {
126 item
.src
= eval(imageName
+ "_down.src");
130 function changeStatus(message
) {
132 window
.status
= message
;
136 } // End changeStatus
138 // isNumeric function
139 function isNumeric(num
) {
141 var bolValidNum
= true;
142 var digits
= "1234567890";
143 var len
= num
.length
;
146 for (i
= 0; i
< len
; ++i
) {
147 numSub
= num
.substring(i
, i
+ 1);
149 // Test for numeric match
150 if (digits
.indexOf(numSub
) == -1) {
155 // Return boolean var
159 // Check for numeric and display nice error
160 function checkNumeric(field
, message
) {
162 if (!isNumeric(field
.value
)) {
166 } // End checkNumeric
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
);
174 if (isNumeric(token
)) {
183 // Function dateCheck, requires global err variable for passing error messages
184 // and requires the isNumeric function
185 function dateCheck(date
, humanname
, dateFormat
) {
189 // If no dateFormat, then set one
190 if (dateFormat
== null) {
191 format
= "mm/dd/yyyy";
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
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');
213 // Trim the leading spaces from the string
214 while (date_s
.charAt(0) == ' ') {
215 date_s
= date_s
.slice(1);
218 while (i_format
< format
.length
) {
219 // Get next token from format string
220 c
= format
.charAt(i_format
);
223 while ((format
.charAt(i_format
) == c
) && (i_format
< format
.length
)) {
224 token
+= format
.charAt(i_format
);
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
234 year
= getInt(date_s
, i_date_s
, x
, y
);
235 bYearProvided
= true;
237 if ((year
== null) || (year
.length
!= token
.length
)) {
241 i_date_s
+= year
.length
;
243 if (token
== "mmm") { // Month name
246 for (var i
= 0; i
< MONTH_NAMES
.length
; ++i
) {
247 var month_name
= MONTH_NAMES
[i
];
249 if (date_s
.substring(i_date_s
, (i_date_s
+ month_name
.length
)).toLowerCase() == month_name
.toLowerCase()) {
256 i_date_s
+= month_name
.length
;
261 if ((month
== 0) || (month
< 1) || (month
> 12)) {
265 if ((token
== "mm") || (token
== "m")) {
266 x
= token
.length
; y
= 2;
267 month
= getInt(date_s
, i_date_s
, x
, y
);
269 if ((month
== null) || (month
< 1) || (month
> 12)) {
273 i_date_s
+= month
.length
;
275 if (token
=="dd" || token
=="d") {
276 x
= token
.length
; y
= 2;
277 date
= getInt(date_s
, i_date_s
, x
, y
);
279 if ((date
== null) || (date
< 1) || (date
> 31)) {
283 i_date_s
+= date
.length
;
285 if (date_s
.substring(i_date_s
, (i_date_s
+ token
.length
)) != token
) {
288 i_date_s
+= token
.length
;
296 // If there are any trailing characters left in the date_s, it doesn't match
297 if (i_date_s
!= date_s
.length
) {
301 // Is date valid for month?
302 if ((month
== 4) || (month
== 6) || (month
== 9) || (month
== 11)) {
308 // Check for leap year
309 if ((((year
% 4) == 0) && ((year
% 100) != 0)) || ((year
% 400) == 0)) {
326 // Add to the error message, if needed
328 err
+= "\n - The " + humanname
+ " must be a valid date in the format " + format
+ ".";
331 // Add to the error message, if needed
332 if (date_month_err
!= 0) {
333 err
+= "\n - The month must be between 1-12.";
336 // Add to the error message, if needed
337 if (date_year_err
!= 0) {
338 err
+= "\n - The " + humanname
+ " must have a valid year.";
341 // Add to the error message, if needed
342 if (date_day_err
!= 0) {
343 switch (date_day_err
) {
345 err
+= "\n - The month you entered in the " + humanname
+ " can only have between 1 and 31 days.";
348 err
+= "\n - The month you entered in the " + humanname
+ " can only have between 1 and 30 days.";
351 err
+= "\n - The month you entered in the " + humanname
+ " can only have between 1 and 29 days in a Leap Year.";
354 err
+= "\n - The month you entered in the " + humanname
+ " can only have between 1 and 28 days in a non-Leap Year.";
362 // Compares two MM/DD/YYY dates for less than (-1), equal to (0), or
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));
369 if (localDate1
.getTime() > localDate2
.getTime()) {
373 if (localDate1
.getTime() < localDate2
.getTime()) {
382 // All-purpose form validation script
383 function checkForm(dataForm
) {
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
= "";
405 // Loop over form elements
406 for (var i
= 0; i
< dataForm
.length
; ++i
) {
407 var element
= dataForm
.elements
[i
];
409 // Check for select box
410 if (element
.selectbox
) {
411 // Check for required
412 if (element
.required
) {
414 if (element
.options
[element
.selectedIndex
].value
== "") {
415 error_fields
+= "\n - " + element
.humanname
+ " requires a selection.";
421 // Strip the leading and trailing blanks
422 element
.value
= element
.value
.replace(stripBlanksStart
, '');
423 element
.value
= element
.value
.replace(stripBlanksEnd
, '');
425 // If it is required and is empty, alert
426 if (element
.required
&& (!element
.value
.length
)) {
427 error_fields
+= "\n - " + element
.humanname
+ " is required.";
430 // If it isn't required and doesn't have any length, skip it
431 if ((! element
.required
) && (! element
.value
.length
)) {
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.";
444 // Convert to uppercase if necessary
445 if (element
.uppercase
) {
446 element
.value
= element
.value
.toUpperCase();
449 // Convert to uppercase if necessary
450 if (element
.lowercase
) {
451 element
.value
= element
.value
.toLowerCase();
454 // UCFirst if necessary
455 if (element
.ucfirst
) {
456 // Squeeze the blanks
457 rs
= element
.value
.replace(squeezeBlanks
, ' ');
458 dsegs
= rs
.split(' ');
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();
471 // Check for first value
472 element
.value
= j
? element
.value
+ ' ' + dsegs
[j
] : dsegs
[j
];
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
;
482 // Check for value equality
483 if (elementValue1
!= elementValue2
) {
484 error_fields
+="\n - " + element
.humanname
+ " is not the same as " + element
.equaltovalue
.humanname
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
553 // Check a price (sort of)
555 // Strip out currency stuff
556 element
.value
= element
.value
.replace(stripNotDollars
, '');
560 // Check a telephone number
561 if (element
.telephone
) {
562 // Strip out parens and spaces
563 rs
= element
.value
.replace(stripNonNumbers
, '');
565 if (rs
.length
== 7) {
566 element
.value
= rs
.substr(0, 3) + "-" + rs
.substr(3, 4);
568 if (rs
.length
== 10) {
569 element
.value
= rs
.substr(0, 3) + "-" + rs
.substr(3, 3) + "-" + rs
.substr(6, 4);
571 error_fields
+= "\n - " + element
.humanname
+ " is an invalid telephone number.";
578 if (element
.zipcode
) {
579 if (!zipCodeCheck
.test(element
.value
)) {
580 error_fields
+="\n - " + element
.humanname
+ " is an invalid zipcode.";
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.";
593 if (!passwordNumbers
.test(element
.value
)) {
594 error_fields
+= "\n - " + element
.humanname
+ " must contain at least one number.";
598 if (!passwordLetters
.test(element
.value
)) {
599 error_fields
+= "\n - " + element
.humanname
+ " must contain at least one letter.";
604 // Check for all numbers
605 if (element
.numeric
) {
606 if (!allNumbers
.test(element
.value
)) {
607 error_fields
+= "\n - " + element
.humanname
+ " is not numeric.";
612 // Check an email address for validity
614 element
.value
= element
.value
.replace(noSpaces
, '');
616 if (!emailPattern
.test(element
.value
)) {
617 error_fields
+= "\n - " + element
.humanname
+ " is not a valid email address.";
624 error_fields
+= dateCheck(element
.value
, element
.humanname
, element
.format
);
630 // Check for 24 hour time
631 if (element
.time24
) {
633 if (!time24Format
.test(element
.value
)) {
634 error_fields
+= "\n - " + element
.humanname
+ " is not a valid 24 hour time.";
638 if (!time12Format
.test(element
.value
)) {
639 error_fields
+= "\n - " + element
.humanname
+ " is not a valid 12 hour time.";
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.";
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.";
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";
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";
674 if (element
.value
.indexOf(":") > -1) {
676 } else if (element
.value
.indexOf("-") > -1) {
680 if (searchChar
!= '') {
681 portArray
= element
.value
.split(searchChar
);
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";
694 // Check for any errors
695 if (error_fields
== "") {
698 msg
= "The following fields have errors:\n";
706 function clearData(field
, data
) {
707 // Check if they equal
708 if (field
.value
== data
) {
715 function setEmptyData(field
, data
) {
716 // Check if they equal
717 if (! field
.value
.length
) {
723 // Trim whitespace from beginning and end
724 function trim(data
) {
725 var objRegExp
= /^(\s*)$/;
727 // Check for all spaces
728 if (objRegExp
.test(data
)) {
729 data
= data
.replace(objRegExp
, '');
731 if (data
.length
== 0)
735 // Check for leading & trailing spaces
736 objRegExp
= /^(\s*)([\W\w]*)(\b\s*$)/;
738 if (objRegExp
.test(data
)) {
739 // Remove leading and trailing whitespace characters
740 data
= data
.replace(objRegExp
, '$2');