Home directory for Malawi's wwwroot
Duncan Ewan
2021-02-19 3e758c29e0fde36fc088efcfc88f9a3014432b64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// ADOBE SYSTEMS INCORPORATED Copyright 2007 Adobe Systems Incorporated All Rights Reserved. 
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the
// terms of the Adobe license agreement accompanying it. If you have received this file from 
// a source other than Adobe, then your use, modification, or distribution of it requires the 
// prior written permission of Adobe.
 
    var KT_focusedEl = null;
 
    /********** 
    KT_validateSingle function
        description:
            Validates single character agains single mask component
        params: 
            ascchar: character
                single character, to test if it is correct in the context of the mask character 
            maskchar: character
                single mask character, see below for meaning
        returns: 
            boolean
     **********/
    KT_validateSingle = function(ascchar, maskchar) {
        var cchar = ascchar.charCodeAt(0);
        switch (maskchar) {
            case "9": //numeric
                if (cchar < 58 && cchar > 47) {
                    return true;
                }
                break;
            case "A": //letter
                if ((cchar < 91 && cchar > 64) || (cchar < 123 && cchar > 96)) {
                    return true;
                }
                break;
            case "X": //letter or numeric
                if ((cchar < 91 && cchar > 64) || (cchar < 123 && cchar > 96) || (cchar < 58 && cchar > 47)) {
                    return true;
                }
                break;
            case "?": //any character
                return true;
                break;
            default:
                return true;
                break;
        }
    }
    
    /********** 
    KT_maskDefaultValue function
        description:
            Called with a special mask character, returns the default value for that type of mask
        params:
            maskchar: character
                single mask character, see below for meaning
        returns:
            character
    **********/
    KT_maskDefaultValue = function(maskchar) {
        switch (maskchar) {
            case "9": //numeric
                return '0';
                break;
            case "A": //letter
                return 'a';
                break;
            case "X": //letter or numeric
                return '0';
                break;
            case "?": //any character
                return '0';
                break;
            default:
                return '0';
                break;
        }
    }
 
    /********** 
    KT_isSpecialChar function
        description:
            Checks if the parameter is a special mask character
        params:
            ascchar: character
                Special mask characters: 
                9 - numeric
                A - letters
                X - letter or number
                ? - any character
        returns:
            boolean
    **********/
    KT_isSpecialChar = function(ascchar) {
        if (ascchar == '9' || ascchar == 'A' || ascchar == 'X' || ascchar == '?')  {
            return true;
        } else {
            return false;
        }
    }
 
    /**********
    mask_onValueChanged function
        description:
            Called when 
                - the user types something in the input
                - the input loses the focus
            It validates the input's value against the input's mask
        params: 
            none
        returns:
            none
    **********/
    mask_onValueChanged = function() {
        if ((typeof window.getSelection == 'undefined' && typeof document.selection == 'undefined')) {
            // if the current browser is not compatible, do nothing
            return;
        }
        
        if (KT_focusedEl == null || KT_focusedEl.mask == null || KT_focusedEl.mask == '') {
            return;
        }
 
        var mask = KT_focusedEl.mask;
        var val = KT_focusedEl.value;
        var i = 0;
        var moveCursor = false;
 
        if (val == KT_focusedEl.oldText) {
            // if the field content did not change since the last update, do nothing
            return;
        }
        if (val.length > mask.length) {
            // strip trailing characters if text length is bigger than mask length
            val = val.substr(0, mask.length);
            moveCursor = true;
        }
        for (; i < mask.length ; i++) {
            if (val.charCodeAt(i).toString() != 'NaN') {
                //if the char is inserted
                if (KT_isSpecialChar(mask.charAt(i))) {
                    if (KT_validateSingle(val.charAt(i), mask.charAt(i))) {
                        //character is correct, go to next
                        continue;
                    } else {
                        //revert to the last known good value, increase index to break loop
                        val = KT_focusedEl.oldText;
                        i = mask.length;
                        break;
                    }
                } else {
                    //normal character in the mask
                    if (val.charAt(i) != mask.charAt(i)) {
                        //if the character is different from the mask
                        if (i == val.length - 1) {
                            //append last character and move cursor to the end
                            var lastChar = val.substr(val.length -1, val.length);
                            val = val.substr(0, val.length -1) + mask.charAt(i) + lastChar;
                            moveCursor = true;
                            continue;
                        } else {
                            //revert to the last known good value, increase index to break loop
                            val = KT_focusedEl.oldText;
                            i = mask.length;
                        }
                        break;
                    }
                }
            } else {
                //if the current char is not inserted
                if (val.length < KT_focusedEl.oldText.length) {
                    //deleted character
                    break;
                }
                for (;i<mask.length;i++) {
                    if (!KT_isSpecialChar(mask.charAt(i))) {
                        //re-enter the mask characters if it has been deleted
                        val += mask.charAt(i);
                        moveCursor = true;
                    } else {
                        break;
                    }
                }
                break;
            }
        }
        if (val.length > mask.length) {
            // strip trailing characters if text length is bigger than mask length
            val = val.substr(0, mask.length);
            moveCursor = true;
        }
        if (KT_focusedEl.value != val) {
            KT_focusedEl.value = val; //last calculated correct value
        }
        KT_focusedEl.oldText = val; //update so we can check on next character
        if (moveCursor) {
            // no need to move the cursor, it is automatically moved at the end of the sellection by IE and Mozilla
        }
    }
 
    /********** 
    mask_parseFirstTime function
        description:
            Called from mask_onSetFocus, only the first time ( when obj.mask is undefined)
            Tries to parse the initial value into a valid format, with the following algorithm:
            - normalizes the string adding non special mask characters if they do not exist
            - it strips the mask of all non special characters
            - rebuilds the string using the stripped mask
            - compares the string with the stripped mask and changes the values if they are invalid
            - rebuild the string and returns
        params:
            none
        returns:
            none
    **********/
    mask_parseFirstTime = function(value, mask) {
        var strippedmask = ''; var strippedvalue = '';
        cond = 1;imask = 0; ival = 0;cnt = 0;
 
        //NORMALIZE VALUE: add non special characters
        while (cond == 1) {
            cond = 1;
            if (!KT_isSpecialChar(mask.charAt(imask))) {
                if (value.charCodeAt(ival).toString() != 'NaN') {
                    if (mask.charAt(imask) == value.charAt(ival)) {
                        imask++;ival++;
                    } else {
                        value = value.substr(0, ival) + mask.charAt(imask) + value.substr(ival, value.length);
                        imask = 0; ival = 0; cond = 1;
                    }
                } else {
                    value += KT_maskDefaultValue(mask.charAt(imask));
                }
            } else {
                imask++;ival++;
            }
            if (imask >= mask.length || ival >= value.length) {
                cond = 0;
            }
        }
 
        //save only the special chars in a mask
        for (i=0;i<mask.length;i++) {
            if (KT_isSpecialChar(mask.charAt(i))) {
                strippedmask += mask.charAt(i);
                if (value.charCodeAt(i).toString() != 'NaN') {
                    strippedvalue += value.charAt(i);
                } else {
                    strippedvalue += KT_maskDefaultValue(mask.charAt(i));
                }
            }
        }
 
        oldvalue = value; //save the old value for reference
        value = strippedvalue;
        var newvalue = '';
 
        //rebuild the string removing invalid values and replacing them with defaults
        for (i=0;i<strippedmask.length;i++) {
                if (!KT_validateSingle(value.charAt(i), strippedmask.charAt(i))) {
                    newvalue += KT_maskDefaultValue(strippedmask.charAt(i));
                } else {
                    newvalue += value.charAt(i);
                }
        }
 
        //rebuild the value,by adding the initial non special mask characters
        var toret = ''; var j = 0; //j holds the index in the stripped mask
        for (i=0;i<mask.length;i++) {
            if (KT_isSpecialChar(mask.charAt(i))) {
                toret += newvalue.charAt(j++);
            } else {
                toret += mask.charAt(i);
            }
        }
        return toret;
    }
 
    /********** 
    mask_onSetFocus function
        description:
            Called when the input gets the focus
            Saved the current input in a global variable and also the current value
        params:
            none
        returns:
            none
    **********/
    mask_onSetFocus = function(obj, mask) {
        if ((typeof window.getSelection == 'undefined' && typeof document.selection == 'undefined')) {
            // if the current browser is not compatible, do nothing
            return;
        }
        if (typeof obj.mask == 'undefined') {
            ret = '';
            if (obj.value != '') {
                ret = mask_parseFirstTime(obj.value, mask);
            }
            obj.value = ret;
            obj.mask = mask;
        }
        KT_focusedEl = obj; // store the current input object in a global variable
        if (typeof KT_focusedEl.oldText == 'undefined') {
            KT_focusedEl.oldText = obj.value; // save the input current value
            mask_onValueChanged(); // validates the current input value
        }
    }
 
    /********** 
    mask_onKillFocus function
        description:
            Called when the input loses the focus
            Verifies the input's value
        params:
            none
        returns:
            none
    **********/
    mask_onKillFocus = function() {
        if ((typeof window.getSelection == 'undefined' && typeof document.selection == 'undefined')) {
            // if the current browser is not compatible, do nothing
            return;
        }
        mask_onValueChanged(); // validates the current input value
        KT_focusedEl = null;
    }