1 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
8 // Onkeyup handler for dates. Converts dates that are keyed in to a
9 // consistent format, and helps to reduce typing errors.
11 function datekeyup(e
, defcc
) {
14 var arr
= new Array(0, 0, 0);
18 // Build an array to facilitate error checking.
19 for (var i
= 0; i
< v
.length
; ++i
) {
21 if (c
>= '0' && c
<= '9') {
23 } else if (c
== '-' || c
== '/') {
26 e
.value
= v
.substring(0, i
);
31 // We have finished scanning the string. If there is a problem,
32 // drop the last character and repeat the loop.
34 (ix
> 1 && arr
[1] == 0) ||
35 (ix
> 0 && arr
[0] == 0) ||
37 (ix
> 0 && arr
[0] > 2 && (arr
[0] != 4 || arr
[1] > 2 || arr
[2] > 2)) ||
38 (arr
[2] > 2 && (arr
[2] > 4 || arr
[0] > 2 || arr
[1] > 2)))
40 e
.value
= v
.substring(0, v
.length
- 1);
46 if (arr
[2] == 4 && defcc
== '1') { // mm/dd/yyyy
47 e
.value
= v
.substring(arr
[0] + arr
[1] + 2) + '-'; // year
48 if (arr
[0] == 1) e
.value
+= '0';
49 e
.value
+= v
.substring(0, arr
[0]) + '-'; // month
50 if (arr
[1] == 1) e
.value
+= '0';
51 e
.value
+= v
.substring(arr
[0] + 1, arr
[0] + 1 + arr
[1]); // day
53 else if (arr
[2] == 4) { // dd-mm-yyyy
54 e
.value
= v
.substring(arr
[0] + arr
[1] + 2) + '-'; // year
55 if (arr
[1] == 1) e
.value
+= '0';
56 e
.value
+= v
.substring(arr
[0] + 1, arr
[0] + 1 + arr
[1]) + '-'; // month
57 if (arr
[0] == 1) e
.value
+= '0';
58 e
.value
+= v
.substring(0, arr
[0]); // day
60 else if (arr
[0] == 4 && arr
[2] > 0) { // yyyy-mm-dd
61 e
.value
= v
.substring(0, arr
[0]) + '-'; // year
62 if (arr
[1] == 1) e
.value
+= '0';
63 e
.value
+= v
.substring(arr
[0] + 1, arr
[0] + 1 + arr
[1]) + '-'; // month
64 e
.value
+= v
.substring(arr
[0] + arr
[1] + 2); // day (may be 1 digit)
66 else if (arr
[0] == 8 && defcc
== '1') { // yyyymmdd
67 e
.value
= v
.substring(0, 4) + '-'; // year
68 e
.value
+= v
.substring(4, 6) + '-'; // month
69 e
.value
+= v
.substring(6); // day
71 else if (arr
[0] == 8) { // ddmmyyyy
72 e
.value
= v
.substring(4) + '-'; // year
73 e
.value
+= v
.substring(2, 4) + '-'; // month
74 e
.value
+= v
.substring(0, 2); // day
78 // Onblur handler to avoid incomplete entry of dates.
80 function dateblur(e
, defcc
) {
82 if (v
.length
== 0) return;
84 var arr
= new Array(0, 0, 0);
86 for (var i
= 0; i
< v
.length
; ++i
) {
88 if (c
>= '0' && c
<= '9') {
90 } else if (c
== '-' || c
== '/') {
93 alert('Invalid character in date!');
98 if (ix
!= 2 || arr
[0] != 4 || arr
[1] != 2 || arr
[2] < 1) {
99 if (confirm('Date entry is incomplete! Try again?'))
107 e
.value
= v
.substring(0, 8) + '0' + v
.substring(8);
111 // Private subroutine for US phone number formatting.
112 function usphone(v
) {
113 if (v
.length
> 0 && v
.charAt(0) == '-') v
= v
.substring(1);
114 var oldlen
= v
.length
;
115 for (var i
= 0; i
< v
.length
; ++i
) {
117 if (c
< '0' || c
> '9') {
118 v
= v
.substring(0, i
) + v
.substring(i
+ 1);
122 if (oldlen
> 3 && v
.length
>= 3) {
123 v
= v
.substring(0, 3) + '-' + v
.substring(3);
124 if (oldlen
> 7 && v
.length
>= 7) {
125 v
= v
.substring(0, 7) + '-' + v
.substring(7);
126 if (v
.length
> 12) v
= v
.substring(0, 12);
132 // Private subroutine for non-US phone number formatting.
133 function nonusphone(v
) {
134 for (var i
= 0; i
< v
.length
; ++i
) {
136 if (c
< '0' || c
> '9') {
137 v
= v
.substring(0, i
) + v
.substring(i
+ 1);
144 // Telephone country codes that are exactly 2 digits.
145 var twodigitccs
= '/20/30/31/32/33/34/36/39/40/41/43/44/45/46/47/48/49/51/52/53/54/55/56/57/58/60/61/62/63/64/65/66/81/82/84/86/90/91/92/93/94/95/98/';
147 // Onkeyup handler for phone numbers. Helps to ensure a consistent
148 // format and to reduce typing errors. defcc is the default telephone
149 // country code as a string.
151 function phonekeyup(e
, defcc
) {
153 var oldlen
= v
.length
;
155 // Deal with international formatting.
156 if (v
.length
> 0 && v
.charAt(0) == '+') {
158 for (var i
= 1; i
< v
.length
; ++i
) {
160 if (c
< '0' || c
> '9') {
161 v
= v
.substring(0, i
) + v
.substring(i
+ i
);
166 if (i
== 1 && oldlen
> 2) {
167 if (cc
== '1') { // USA
168 e
.value
= '+1-' + usphone(v
.substring(2));
171 if (cc
== '7') { // USSR
172 e
.value
= '+7-' + nonusphone(v
.substring(2));
176 else if (i
== 2 && oldlen
> 3) {
177 if (twodigitccs
.indexOf(cc
) >= 0) {
178 e
.value
= v
.substring(0, 3) + '-' + nonusphone(v
.substring(3));
182 else if (i
== 3 && oldlen
> 4) {
183 e
.value
= v
.substring(0, 4) + '-' + nonusphone(v
.substring(4));
192 e
.value
= usphone(v
);
194 e
.value
= nonusphone(v
);