added popup insurance search/add
[openemr.git] / library / textformat.js
blob3fedddab4aada15dac732a70480a26edc88aeb15
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) {
12 while(true) {
13 var delim = '';
14 var arr = new Array(0, 0, 0);
15 var ix = 0;
16 var v = e.value;
18 // Build an array to facilitate error checking.
19 for (var i = 0; i < v.length; ++i) {
20 var c = v.charAt(i);
21 if (c >= '0' && c <= '9') {
22 ++arr[ix];
23 } else if (c == '-' || c == '/') {
24 arr[++ix] = 0;
25 } else {
26 e.value = v.substring(0, i);
27 return;
31 // We have finished scanning the string. If there is a problem,
32 // drop the last character and repeat the loop.
33 if ((ix > 2) ||
34 (ix > 1 && arr[1] == 0) ||
35 (ix > 0 && arr[0] == 0) ||
36 (arr[0] > 8) ||
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);
41 } else {
42 break;
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) {
81 var v = e.value;
82 if (v.length == 0) return;
84 var arr = new Array(0, 0, 0);
85 var ix = 0;
86 for (var i = 0; i < v.length; ++i) {
87 var c = v.charAt(i);
88 if (c >= '0' && c <= '9') {
89 ++arr[ix];
90 } else if (c == '-' || c == '/') {
91 arr[++ix] = 0;
92 } else {
93 alert('Invalid character in date!');
94 return;
98 if (ix != 2 || arr[0] != 4 || arr[1] != 2 || arr[2] < 1) {
99 if (confirm('Date entry is incomplete! Try again?'))
100 e.focus();
101 else
102 e.value = '';
103 return;
106 if (arr[2] == 1) {
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) {
116 var c = v.charAt(i);
117 if (c < '0' || c > '9') {
118 v = v.substring(0, i) + v.substring(i + 1);
119 --i;
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);
129 return v;
132 // Private subroutine for non-US phone number formatting.
133 function nonusphone(v) {
134 for (var i = 0; i < v.length; ++i) {
135 var c = v.charAt(i);
136 if (c < '0' || c > '9') {
137 v = v.substring(0, i) + v.substring(i + 1);
138 --i;
141 return v;
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) {
152 var v = e.value;
153 var oldlen = v.length;
155 // Deal with international formatting.
156 if (v.length > 0 && v.charAt(0) == '+') {
157 var cc = '';
158 for (var i = 1; i < v.length; ++i) {
159 var c = v.charAt(i);
160 if (c < '0' || c > '9') {
161 v = v.substring(0, i) + v.substring(i + i);
162 --i;
163 continue;
165 cc += c;
166 if (i == 1 && oldlen > 2) {
167 if (cc == '1') { // USA
168 e.value = '+1-' + usphone(v.substring(2));
169 return;
171 if (cc == '7') { // USSR
172 e.value = '+7-' + nonusphone(v.substring(2));
173 return;
176 else if (i == 2 && oldlen > 3) {
177 if (twodigitccs.indexOf(cc) >= 0) {
178 e.value = v.substring(0, 3) + '-' + nonusphone(v.substring(3));
179 return;
182 else if (i == 3 && oldlen > 4) {
183 e.value = v.substring(0, 4) + '-' + nonusphone(v.substring(4));
184 return;
187 e.value = v;
188 return;
191 if (defcc == '1') {
192 e.value = usphone(v);
193 } else {
194 e.value = nonusphone(v);
197 return;