Space to separate "&&" so that all "&$var" can be made into "$var" later
[openemr.git] / library / textformat.js
blob11fcec65ae6e2ee98d257487e770ad9a56141f7d
1 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
2 //
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, withtime) {
12  if (typeof(withtime) == 'undefined') withtime = false;
14  while(true) {
15   var delim = '';
16   var arr = new Array(0, 0, 0, 0, 0, 0);
17   var ix = 0;
18   var v = e.value;
20   // Build an array to facilitate error checking.
21   for (var i = 0; i < v.length; ++i) {
22    var c = v.charAt(i);
23    if (c >= '0' && c <= '9') {
24     ++arr[ix];
25    } else if (ix < 2 && (c == '-' || c == '/')) {
26     arr[++ix] = 0;
27    } else if (withtime && ix == 2 && c == ' ') {
28     arr[++ix] = 0;
29    } else if (withtime && (ix == 3 || ix == 4) && c == ':') {
30     arr[++ix] = 0;
31    } else {
32     e.value = v.substring(0, i);
33     return;
34    }
35   }
37   // We have finished scanning the string.  If there is a problem,
38   // drop the last character and repeat the loop.
39   if ((ix > 5) ||
40       (!withtime && ix > 2)   ||
41       (ix > 4 && arr[4] == 0) ||
42       (ix > 3 && arr[3] == 0) ||
43       (ix > 2 && arr[2] == 0) ||
44       (ix > 1 && arr[1] == 0) ||
45       (ix > 0 && arr[0] == 0) ||
46       (arr[0] > 8) ||
47       (ix > 0 && arr[0] > 2 && (arr[0] != 4 || arr[1] > 2 || arr[2] > 2)) ||
48       (arr[2] > 2 && (arr[2] > 4 || arr[0] > 2 || arr[1] > 2)))
49   {
50    e.value = v.substring(0, v.length - 1);
51   } else {
52    break;
53   }
54  }
56  // The remainder does reformatting if there is enough data for that.
57  if (arr[2] == 4 && defcc == '1') { // mm/dd/yyyy
58   e.value  = v.substring(arr[0] + arr[1] + 2, arr[0] + arr[1] + 6) + '-'; // year
59   if (arr[0] == 1) e.value += '0';
60   e.value += v.substring(0, arr[0]) + '-'; // month
61   if (arr[1] == 1) e.value += '0';
62   e.value += v.substring(arr[0] + 1, arr[0] + 1 + arr[1]); // day
63  }
64  else if (arr[2] == 4) { // dd-mm-yyyy
65   e.value  = v.substring(arr[0] + arr[1] + 2, arr[0] + arr[1] + 6) + '-'; // year
66   if (arr[1] == 1) e.value += '0';
67   e.value += v.substring(arr[0] + 1, arr[0] + 1 + arr[1]) + '-'; // month
68   if (arr[0] == 1) e.value += '0';
69   e.value += v.substring(0, arr[0]); // day
70  }
71  else if (arr[0] == 4 && arr[2] > 0) { // yyyy-mm-dd
72   e.value  = v.substring(0, arr[0]) + '-'; // year
73   if (arr[1] == 1) e.value += '0';
74   e.value += v.substring(arr[0] + 1, arr[0] + 1 + arr[1]) + '-'; // month
75   e.value += v.substring(arr[0] + arr[1] + 2, arr[0] + arr[1] + 2 + arr[2]); // day (may be 1 digit)
76  }
77  else if (arr[0] == 8 && defcc == '1') { // yyyymmdd
78   e.value  = v.substring(0, 4) + '-'; // year
79   e.value += v.substring(4, 6) + '-'; // month
80   e.value += v.substring(6, 8); // day
81  }
82  else if (arr[0] == 8) { // ddmmyyyy
83   e.value  = v.substring(4, 8) + '-'; // year
84   e.value += v.substring(2, 4) + '-'; // month
85   e.value += v.substring(0, 2); // day
86  }
87  else {
88   return;
89  }
90  if (withtime) {
91   e.value += v.substring(arr[0] + arr[1] + arr[2] + 2);
92  }
95 // Onblur handler to avoid incomplete entry of dates.
97 function dateblur(e, defcc, withtime) {
98  if (typeof(withtime) == 'undefined') withtime = false;
100  var v = e.value;
101  if (v.length == 0) return;
103  var arr = new Array(0, 0, 0, 0, 0);
104  var ix = 0;
105  for (var i = 0; i < v.length; ++i) {
106   var c = v.charAt(i);
107   if (c >= '0' && c <= '9') {
108    ++arr[ix];
109   } else if (c == '-' || c == '/' || c == ' ' || c == ':') {
110    arr[++ix] = 0;
111   } else {
112    alert('Invalid character in date!');
113    return;
114   }
117  // A birth date may be just age in years, in which case we convert it.
118  if (ix == 0 && arr[0] > 0 && arr[0] <= 3 && e.name.indexOf('DOB') >= 0) {
119   var d = new Date();
120   d = new Date(d.getTime() - parseInt(v) * 365.25 * 24 * 60 * 60 * 1000);
121   var s = '' + d.getFullYear() + '-';
122   if (d.getMonth() < 9) s += '0';
123   s += (d.getMonth() + 1) + '-';
124   if (d.getDate() < 10) s += '0';
125   s += d.getDate();
126   e.value = s;
127   return;
130  if ((!withtime && ix != 2) || (withtime && ix < 2) || arr[0] != 4 || arr[1] != 2 || arr[2] < 1) {
131   if (confirm('Date entry is incomplete! Try again?'))
132    e.focus();
133   else
134    e.value = '';
135   return;
138  if (arr[2] == 1) {
139   e.value = v.substring(0, 8) + '0' + v.substring(8);
143 // Private subroutine for US phone number formatting.
144 function usphone(v) {
145  if (v.length > 0 && v.charAt(0) == '-') v = v.substring(1);
146  var oldlen = v.length;
147  for (var i = 0; i < v.length; ++i) {
148   var c = v.charAt(i);
149   if (c < '0' || c > '9') {
150    v = v.substring(0, i) + v.substring(i + 1);
151    --i;
152   }
154  if (oldlen > 3 && v.length >= 3) {
155   v = v.substring(0, 3) + '-' + v.substring(3);
156   if (oldlen > 7 && v.length >= 7) {
157    v = v.substring(0, 7) + '-' + v.substring(7);
158    if (v.length > 12) v = v.substring(0, 12);
159   }
161  return v;
164 // Private subroutine for non-US phone number formatting.
165 function nonusphone(v) {
166  for (var i = 0; i < v.length; ++i) {
167   var c = v.charAt(i);
168   if (c < '0' || c > '9') {
169    v = v.substring(0, i) + v.substring(i + 1);
170    --i;
171   }
173  return v;
176 // Telephone country codes that are exactly 2 digits.
177 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/';
179 // Onkeyup handler for phone numbers.  Helps to ensure a consistent
180 // format and to reduce typing errors.  defcc is the default telephone
181 // country code as a string.
183 function phonekeyup(e, defcc) {
184  var v = e.value;
185  var oldlen = v.length;
187  // Deal with international formatting.
188  if (v.length > 0 && v.charAt(0) == '+') {
189   var cc = '';
190   for (var i = 1; i < v.length; ++i) {
191    var c = v.charAt(i);
192    if (c < '0' || c > '9') {
193     v = v.substring(0, i) + v.substring(i + i);
194     --i;
195     continue;
196    }
197    cc += c;
198    if (i == 1 && oldlen > 2) {
199     if (cc == '1') { // USA
200      e.value = '+1-' + usphone(v.substring(2));
201      return;
202     }
203     if (cc == '7') { // USSR
204      e.value = '+7-' + nonusphone(v.substring(2));
205      return;
206     }
207    }
208    else if (i == 2 && oldlen > 3) {
209     if (twodigitccs.indexOf(cc) >= 0) {
210      e.value = v.substring(0, 3) + '-' + nonusphone(v.substring(3));
211      return;
212     }
213    }
214    else if (i == 3 && oldlen > 4) {
215     e.value = v.substring(0, 4) + '-' + nonusphone(v.substring(4));
216     return;
217    }
218   }
219   e.value = v;
220   return;
223  if (defcc == '1') {
224   e.value = usphone(v);
225  } else {
226   e.value = nonusphone(v);
229  return;
232 // onKeyUp handler for mask-formatted fields.
233 // This feature is experimental.
234 function maskkeyup(elem, mask) {
235  if (!mask || mask.length == 0) return;
236  var i = 0; // elem and mask index
237  var v = elem.value;
238  for (; i < mask.length && i < v.length; ++i) {
239   var ec = v.charAt(i);
240   var mc = mask.charAt(i);
241   if (mc == '#' && (ec < '0' || ec > '9')) {
242    // digit required but this is not one
243    break;
244   }
245   if (mc == '@' && ec.toLowerCase() == ec.toUpperCase()) {
246    // alpha character required but this is not one
247    break;
248   }
250  v = v.substring(0, i);
251  while (i < mask.length) {
252   var mc = mask.charAt(i++);
253   if (mc == '*' || mc == '#' || mc == '@') break;
254   v += mc;
256  elem.value = v;
259 // onBlur handler for mask-formatted fields.
260 // This feature is experimental.
261 function maskblur(elem, mask) {
262  var v = elem.value;
263  var i = mask.length;
264  if (i > 0 && v.length > 0 && v.length != i) {
265   // there is a mask and a value but the value is not long enough
266   for (; i > 0 && mask.charAt(i-1) == '#'; --i);
267   // i is now index to first # in # string at end of mask
268   if (i > v.length) {
269    // value is too short even if trailing digits in the mask are ignored
270    if (confirm('Field entry is incomplete! Try again?'))
271     elem.focus();
272    else
273     elem.value = '';
274    return;
275   }
276   // if the mask ends with digits then right-justify them in the value
277   while (v.length < mask.length) {
278    v = v.substring(0, i) + '0' + v.substring(i, v.length);
279   }
280   elem.value = v;