-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency-mask.js
920 lines (791 loc) · 32.3 KB
/
currency-mask.js
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
// Library: CurrencyMaskJS
//
// Adapted from original work from Dan Switzer ([email protected])
// Original source http://www.pengoworks.com/workshop/js/mask/
// Enhanced by Philippe Monnet ([email protected]) in 2008-2014
// Last revision: v0.9 2014-10-30 01:22PM (bug fixes)
// Note: this library depends on the jQuery library.
//
// Usage examples:
// var housePrice_mask = new Mask("$#,###", "number");
// housePrice_mask.attach($('#housePrice')[0]);
//
function _MaskAPI() {
this.version = "0.9";
this.instances = 0;
this.objects = {};
this.GetMaskForInputWithId = function (id) {
var mask = null;
var keys = Object.keys(this.objects);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var m = this.objects[key];
if (typeof (m) != 'undefined' &&
m.target != null &&
m.target.id === id) {
mask = m;
break;
}
}
return mask;
}
this.RefreshAllInputs = function () {
var keys = Object.keys(this.objects);
var keysCount = keys.length;
for (var i = 0; i < keysCount; i++) {
var key = keys[i];
var m = this.objects[key];
if (typeof (m) != 'undefined' &&
m.target != null) {
m.target.value = m.format(m.target.value);
}
}
return keysCount;
}
this.RefreshInputWithId = function (id) {
var m = this.GetMaskForInputWithId(id);
if (m == null || m.target == null) {
return;
}
m.target.value = m.format(m.target.value);
}
}
MaskAPI = new _MaskAPI();
function Mask(m, t, r) {
this.mask = m;
this.type = (typeof t == "string") ? t : "string";
this.error = [];
this.errorCodes = [];
this.value = "";
this.logLevel = "debug";
this.strippedValue = "";
this.allowPartial = false;
this.numericMaskDisplayFlags = {};
this.target = null;
this.id = MaskAPI.instances++;
this.ref = "MaskAPI.objects['" + this.id + "']";
this.round = (typeof (r) != 'undefined') ? r : true; // PFM - triggers rounding in numbers
MaskAPI.objects[this.id] = this;
}
// define the attach(oElement) function
Mask.prototype.attach = function (o) {
// PFM: Identify IE once and for all
var ua = navigator.userAgent;
this.msie = ua.match(/MSIE/g) || ua.match(/Trident/);
this.msie9 = false;
this.msie9AndAbove = false;
if (this.msie) {
var ieVersion = 1;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null) {
ieVersion = parseFloat(RegExp.$1);
}
this.msie9 = (ieVersion == 9);
this.msie9AndAbove = (ieVersion >= 9) || ua.match(/Trident/);
}
this.webkit = ua.match(/WebKit/g);
this.mozilla = ua.match(/Mozilla/g);
$addEvent(o, "onkeydown", "return " + this.ref + ".isAllowKeyPress(event, this);", true);
$addEvent(o, "onkeyup", "return " + this.ref + ".getKeyPress(event, this);", true);
this.target = o; // PFM added
var m = this; // PFM added
// PFM : changed the logic for IE9 as the blur event is not triggering a change
// (works in all browsers inc. IE8 but not IE9!!!)
if ((this.msie && !this.msie9) || this.webkit) { //use to include this.msie9AndAbove
// PFM: needs to rely on jQuery
// 1. Don't reformat the value!
// 2. Eplicitly trigger the change event too!
$(o).blur(function () {
$(this).change();
m.log('Mask ' + m + ' for input: #' + this.id + ' triggered change event', 'info');
return true;
});
} else {
if (this.mozilla) {
$addEvent(o, "onblur", "this.value = " + this.ref + ".format(this.value);" + this.ref + ".target.value=this.value;$('#'+this.id).change();return this.value;", true);
}
}
// Apply the mask immediately
var maskedValue = this.format(o.value);
o.value = maskedValue;
this.log('Mask ' + this.mask + ' now attached to input: #' + o.id, 'debug');
}
Mask.prototype.isAllowKeyPress = function (e, o) {
if (this.type != "string") return true;
var xe = new qEvent(e);
if (((xe.keyCode > 47) && (o.value.length >= this.mask.length)) && !xe.ctrlKey) return false;
return true;
}
Mask.prototype.getKeyPress = function (e, o, _u) {
this.allowPartial = true;
var xe = new qEvent(e);
// PFM: Save current length and current position of the caret
// so we can set the appropriate caret position
// later once the mask has been applied
var caretPosition = this.getCaretPosition(o);
var currentLength = o.value.length;
// var k = String.fromCharCode(xe.keyCode);
if ((xe.keyCode > 47) || (_u == true) || (xe.keyCode == 8 || xe.keyCode == 46 || xe.keyCode == 110)) {
var v = o.value, d;
// Find out if it is a decimal point character
// Used to include: xe.keyCode == 8 || xe.keyCode == 46
d = (xe.keyCode == 190 || xe.keyCode == 110) ? true : false;
if (this.type == "number") {
this.log('Mask for ' + o.id + ' type:' + this.type + ' _u:' + _u + ' key:' + xe.keyCode + ' decoded:' + String.fromCharCode(xe.keyCode)
+ ' setNumber(' + v + ',' + d + ')', 'debug');
this.value = this.setNumber(v, d);
}
else if (this.type == "date") {
this.value = this.setDateKeyPress(v, d);
}
else {
this.value = this.setGeneric(v, d);
}
o.value = this.value;
// PFM: Figure out and set the new position of the caret
// since it could be offset by 1 if the masking shifted
// the new character to the right
var newCaretPosition = (o.value.length > currentLength)
? (currentLength > 1 ? caretPosition + 1 : o.value.length)
: caretPosition;
this.setCaretPosition(o, newCaretPosition);
this.log('Mask for ' + o.id + ' type:' + this.type + ' _u:' + _u + ' key:' + xe.keyCode + ' decoded:' + String.fromCharCode(xe.keyCode)
+ ' new value:' + o.value
+ ' oc' + caretPosition + ' nc:' + newCaretPosition, 'debug');
} else {
this.log('Mask for ' + o.id + ' _u:' + _u + ' key:' + xe.keyCode + ' decoded:' + String.fromCharCode(xe.keyCode) + ' ignore', 'debug');
}
this.allowPartial = false;
return true;
}
Mask.prototype.getCaretPosition = function (ctrl) {
var caretStart = 0;
var aSelection = null;
var aRange = null;
// IE Support
if (this.msie) {
if (typeof (document.selection) != 'undefined') {
aSelection = document.selection;
aRange = aSelection.createRange();
} else {
aSelection = window.getSelection();
if (aSelection != null) {
aRange = (aSelection.rangeCount > 0)
? aSelection.getRangeAt(0)
: null;
if (aRange == null) {
return 0;
}
}
}
var selLength = aRange.text.length;
aRange.moveStart('character', -ctrl.value.length);
caretStart = aRange.text.length - selLength;
caretEnd = caretStart + aRange.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
caretStart = ctrl.selectionStart;
return (caretStart);
}
Mask.prototype.setCaretPosition = function (ctrl, iCaretPos) {
var aSelection = null;
var aRange = null;
if (this.msie) {
// IE Support
// Create empty selection range
if (typeof (document.selection) != 'undefined') {
aSelection = document.selection;
aRange = aSelection.createRange();
} else {
aSelection = window.getSelection();
if (aSelection != null) {
aRange = (aSelection.rangeCount > 0)
? aSelection.getRangeAt(0)
: null;
if (aRange != null) {
aSelection.removeAllRanges();
aSelection.addRange(iCaretPos);
}
return;
}
return;
}
// Move selection start and end to 0 position
aRange.moveStart('character', -ctrl.value.length);
// Move selection start and end to desired position
aRange.collapse(true);
aRange.moveEnd('character', iCaretPos);
aRange.moveStart('character', iCaretPos);
aRange.select();
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
ctrl.selectionStart = iCaretPos;
ctrl.selectionEnd = iCaretPos;
}
}
Mask.prototype.format = function (s, _r) {
if (this.type == "number") this.value = this.setNumber(s);
else if (this.type == "date") this.value = this.setDate(s);
else this.value = this.setGeneric(s);
return this.value;
}
Mask.prototype.throwError = function (c, e, v) {
this.error[this.error.length] = e;
this.errorCodes[this.errorCodes.length] = c;
if (typeof v == "string") return v;
return true;
}
Mask.prototype.setGeneric = function (_v, _d) {
var v = _v, m = this.mask;
var r = "x#*", rt = [], nv = "", t, x, a = [], j = 0, rx = { "x": "A-Za-z", "#": "0-9", "*": "A-Za-z0-9" };
// strip out invalid characters
v = v.replace(new RegExp("[^" + rx["*"] + "]", "gi"), "");
if ((_d == true) && (v.length == this.strippedValue.length)) v = v.substring(0, v.length - 1);
this.strippedValue = v;
var b = [];
for (var i = 0; i < m.length; i++) {
// grab the current character
x = m.charAt(i);
// check to see if current character is a mask, escape commands are not a mask character
t = (r.indexOf(x) > -1);
// if the current character is an escape command, then grab the next character
if (x == "!") x = m.charAt(i++);
// build a regex to test against
if ((t && !this.allowPartial) || (t && this.allowPartial && (rt.length < v.length))) rt[rt.length] = "[" + rx[x] + "]";
// build mask definition table
a[a.length] = { "chr": x, "mask": t };
}
var hasOneValidChar = false;
// if the regex fails, return an error
if (!this.allowPartial && !(new RegExp(rt.join(""))).test(v)) return this.throwError(1, "The value \"" + _v + "\" must be in the format " + this.mask + ".", _v);
// loop through the mask definition, and build the formatted string
else if ((this.allowPartial && (v.length > 0)) || !this.allowPartial) {
for (i = 0; i < a.length; i++) {
if (a[i].mask) {
while (v.length > 0 && !(new RegExp(rt[j])).test(v.charAt(j))) v = (v.length == 1) ? "" : v.substring(1);
if (v.length > 0) {
nv += v.charAt(j);
hasOneValidChar = true;
}
j++;
} else nv += a[i].chr;
if (this.allowPartial && (j > v.length)) break;
}
}
if (this.allowPartial && !hasOneValidChar) nv = "";
if (this.allowPartial) {
if (nv.length < a.length) this.nextValidChar = rx[a[nv.length].chr];
else this.nextValidChar = null;
}
return nv;
}
Mask.prototype.normalizeNumberDecimal = function (v) {
// make sure there's only one decimal point
v = v.replace(/\./, "d").replace(/\./g, "").replace(/d/, ".");
return v;
}
Mask.prototype.normalizeNumberSign = function (_v) {
var v = String(_v).replace(/[^\d.-]*/gi, "");
// PFM - If the mask does not include a + or a - then reject the key pressed
if ((v == "+" || v == "-") && this.mask.indexOf(v) == -1)
return "";
// PFM - If data has already been entered then reject any sign indicator (as it should have been typed first!)
if ((this.value.length > 0) && (v.indexOf("+") != -1 || v.indexOf("-") != -1)) {
v = v.replace(/\+/g, "");
v = v.replace(/\-/g, "");
}
// PFM - Make sure there's only sign indicator
v = v.replace(/\+/, "d").replace(/\+/g, "").replace(/d/, "+");
v = v.replace(/\-/, "d").replace(/\-/g, "").replace(/d/, "-");
return v;
}
Mask.prototype.normalizeNumberValue = function (_v, _isDecimalCharacter, v) {
// check to see if an invalid mask operation has been entered
if (!/^[\$]?((\$?[\+-]?([0#]{1,3},)?[0#]*(\.[0#]*)?)|([\+-]?\([\+-]?([0#]{1,3},)?[0#]*(\.[0#]*)?\)))$/.test(this.mask))
return this.throwError(1, "An invalid mask was specified for the \nMask constructor.", _v);
if ((_isDecimalCharacter == true) &&
(v.length == this.strippedValue.length)) {
v = v.substring(0, v.length - 1);
}
if (this.allowPartial &&
(v.replace(/[^0-9]/, "").length == 0))
return v;
//
this.strippedValue = v;
var vn = (v.length == 0) ? NaN : Number(v);
if (isNaN(vn))
return this.throwError(2, "The value entered was not a number.", _v);
var isNegative = (vn != 0 && Math.abs(vn) * -1 == vn);
// if no mask, stop processing
if (this.mask.length == 0)
return v;
var m = this.mask;
// check for masking operations
this.numericMaskDisplayFlags["$"] = /^[\$]/.test(m);
this.numericMaskDisplayFlags["("] = (isNegative && (m.indexOf("(") > -1));
this.numericMaskDisplayFlags["+"] = ((m.indexOf("+") != -1) &&
!isNegative);
this.numericMaskDisplayFlags["-"] = (isNegative &&
(!this.numericMaskDisplayFlags["("] || (m.indexOf("-") != -1)));
return v;
}
Mask.prototype.padNumericDecimalPart = function (v) {
// replace all non-place holders from the mask
var m = this.mask.replace(/[^#0.,]*/gi, "");
var hasDecimal = v.indexOf(".") > -1;
// get the value after the decimal point
var vd = hasDecimal
? v.split(".")[1]
: "";
var _vd = vd;
// get number of digits after decimal point in mask
var dm = hasDecimal
? m.split(".")[1]
: "";
if (dm.length == 0) {
vd = "";
} else {
// find the last zero, which indicates the minimum number
// of decimal places to show
var md = dm.lastIndexOf("0") + 1;
// if the number of decimal places is greater than the mask, then round off
if (vd.length > dm.length) {
// PFM - If rounding is on then round otherwise cut off
if (this.round) {
// PFM - Prefix with 1 to ensure the rounding is correct if the first decimals are zeroes
var decs = '1' + vd.substring(0, dm.length + 1);
var decn = Number(decs);
var decr = Math.round(Number(decs) / 10); // compensate for the leading 1
var decrs = String(decr);
vd = decrs.substring(1); // lop off the leading 1 as we no longer need it
} else {
// PFM - lop off the last entered digit
vd = vd.substring(0, dm.length);
}
}
else {
// otherwise, pad the string w/the required zeros
while (vd.length < md) vd += "0";
}
}
var vd2 = '';
if ((vd.length > 0 && !this.allowPartial) ||
((dm.length > 0) &&
this.allowPartial &&
hasDecimal &&
(_vd.length >= vd.length))) {
vd2 = vd;
} else if ((dm.length > 0) &&
this.allowPartial &&
hasDecimal &&
(_vd.length < vd.length)) {
vd2 = _vd;
} else {
vd2 = '';
}
this.log('vd=' + vd + ' l=' + vd.length + ' _vd=' + _vd + ' l=' + _vd.length + ' aP=' + this.allowPartial + ' hD=' + hasDecimal + ' vd2=' + vd2, 'debug');
return vd2;
}
Mask.prototype.padNumericIntegerPart = function (v) {
// replace all non-place holders from the mask
var m = this.mask.replace(/[^#0.,]*/gi, "");
// get the value before the decimal point
var hasDecimal = v.indexOf(".") > -1;
var vi = hasDecimal
? v.split(".")[0]
: v;
var ni = Math.abs(vi);
if (!hasDecimal) {
ni = Math.round(ni);
}
vi = String(ni);
// pad
// get number of digits before decimal point in mask
var im = (m.indexOf(".") > -1)
? m.split(".")[0]
: m;
im = im.replace(/[^0#]+/gi, "");
// find the first zero, which indicates the minimum length
// that the value must be padded w/zeros
var mv = im.indexOf("0") + 1;
// if there is a zero found, make sure it's padded
if (mv > 0) {
mv = im.length - mv + 1;
while (vi.length < mv) vi = "0" + vi;
}
return vi;
}
Mask.prototype.applyNumericDisplayFlags = function (v) {
if (this.numericMaskDisplayFlags["$"]) v = this.mask.replace(/(^[\$])(.+)/gi, "$") + v;
if (this.numericMaskDisplayFlags["+"]) v = "+" + v;
if (this.numericMaskDisplayFlags["-"]) v = "-" + v;
if (this.numericMaskDisplayFlags["("]) v = "(" + v + ")";
return v;
}
Mask.prototype.applyNumericThousandsSeparators = function (vi) {
// replace all non-place holders from the mask
var m = this.mask.replace(/[^#0.,]*/gi, "");
if (!/[#0]+,[#0]{3}/.test(m))
return vi;
try {
// add the commas as the place holder
var x = [], i = 0, n = Number(vi);
while (n > 999) {
x[i] = "00" + String(n % 1000);
x[i] = x[i].substring(x[i].length - 3);
n = Math.floor(n / 1000);
i++;
}
x[i] = String(n % 1000);
vi = x.reverse().join(",");
}
catch (ex) {
this.log('applyThousandsSeparators error' + ex);
}
return vi;
}
Mask.prototype.setNumber = function (_v, _isDecimalCharacter) {
var v = this.normalizeNumberSign(_v);
if (v.length == 0)
return v;
v = this.normalizeNumberDecimal(v);
var m = this.mask;
v = this.normalizeNumberValue(_v, _isDecimalCharacter, v);
if (v.length == 0 && !_isDecimalCharacter)
return v;
this.strippedValue = v;
// pad the int with any necessary zeros
var vi = this.padNumericIntegerPart(v);
// pad the decimal part
var vd = this.padNumericDecimalPart(v);
// PFM - Reupdated the stripped value
this.strippedValue = vi;
if (vd.length > 0)
this.strippedValue += "." + vd;
// check to see if we need commas in the thousands place holder
vi = this.applyNumericThousandsSeparators(vi);
// combine the new value together
v = ((vd.length > 0) || _isDecimalCharacter)
? vi + "." + vd
: vi;
// if needed the apply the currency and sign indicators
v = this.applyNumericDisplayFlags(v);
return v;
}
Mask.prototype.setDate = function (_v) {
var v = _v, m = this.mask;
var a, e, mm, dd, yy, x, s;
// split mask into array, to see position of each day, month & year
a = m.split(/[^mdy]+/);
// split mask into array, to get delimiters
s = m.split(/[mdy]+/);
// convert the string into an array in which digits are together
e = v.split(/[^0-9]/);
if (s[0].length == 0) s.splice(0, 1);
for (var i = 0; i < a.length; i++) {
x = a[i].charAt(0).toLowerCase();
if (x == "m") mm = parseInt(e[i], 10) - 1;
else if (x == "d") dd = parseInt(e[i], 10);
else if (x == "y") yy = parseInt(e[i], 10);
}
// if year is abbreviated, guess at the year
if (String(yy).length < 3) {
yy = 2000 + yy;
if ((new Date()).getFullYear() + 5 < yy) yy = yy - 100;
}
// create date object
var d = new Date(yy, mm, dd);
if (d.getDate() != dd) return this.throwError(1, "An invalid day was entered.", _v);
else if (d.getMonth() != mm) return this.throwError(2, "An invalid month was entered.", _v);
var nv = "";
for (i = 0; i < a.length; i++) {
x = a[i].charAt(0).toLowerCase();
if (x == "m") {
mm++;
if (a[i].length == 2) {
mm = "0" + mm;
mm = mm.substring(mm.length - 2);
}
nv += mm;
} else if (x == "d") {
if (a[i].length == 2) {
dd = "0" + dd;
dd = dd.substring(dd.length - 2);
}
nv += dd;
} else if (x == "y") {
if (a[i].length == 2) nv += d.getYear();
else nv += d.getFullYear();
}
if (i < a.length - 1) nv += s[i];
}
return nv;
}
Mask.prototype.setDateKeyPress = function (_v, _d) {
var v = _v, m = this.mask, k = v.charAt(v.length - 1);
var a, e, c, ml, vl, mm = "", dd = "", yy = "", x, p, z;
if (_d == true) {
while ((/[^0-9]/gi).test(v.charAt(v.length - 1))) v = v.substring(0, v.length - 1);
if ((/[^0-9]/gi).test(this.strippedValue.charAt(this.strippedValue.length - 1))) v = v.substring(0, v.length - 1);
if (v.length == 0) return "";
}
// split mask into array, to see position of each day, month & year
a = m.split(/[^mdy]/);
// split mask into array, to get delimiters
s = m.split(/[mdy]+/);
// mozilla wants to add an empty array element which needs removed
for (i = 0; i < s.length; i++) {
if (s[i].length == 0) {
s.splice(i, 1);
i = i - 1;
}
}
// convert the string into an array in which digits are together
e = v.split(/[^0-9]/);
for (i = 0; i < e.length; i++) {
if (e[i].length == 0) {
e.splice(i, 1);
i = i - 1;
}
}
// position in mask
p = (e.length > 0) ? e.length - 1 : 0;
// determine what mask value the user is currently entering
c = a[p].charAt(0);
// determine the length of the current mask value
ml = a[p].length;
for (var i = 0; i < e.length; i++) {
x = a[i].charAt(0).toLowerCase();
if (x == "m") mm = parseInt(e[i], 10) - 1;
else if (x == "d") dd = parseInt(e[i], 10);
else if (x == "y") yy = parseInt(e[i], 10);
}
var nv = "";
var j = 0;
for (i = 0; i < e.length; i++) {
x = a[i].charAt(0).toLowerCase();
if (x == "m") {
z = ((/[^0-9]/).test(k) && c == "m");
mm++;
if ((e[i].length == 2 && mm < 10) || (a[i].length == 2 && c != "m") || (mm > 1 && c == "m") || (z && a[i].length == 2)) {
mm = "0" + mm;
mm = mm.substring(mm.length - 2);
}
vl = String(mm).length;
ml = 2;
nv += mm;
} else if (x == "d") {
z = ((/[^0-9]/).test(k) && c == "d");
if ((e[i].length == 2 && dd < 10) || (a[i].length == 2 && c != "d") || (dd > 3 && c == "d") || (z && a[i].length == 2)) {
dd = "0" + dd;
dd = dd.substring(dd.length - 2);
}
vl = String(dd).length;
ml = 2;
nv += dd;
} else if (x == "y") {
z = ((/[^0-9]/).test(k) && c == "y");
if (c == "y") yy = String(yy);
else {
if (a[i].length == 2) yy = d.getYear();
else yy = d.getFullYear();
}
if ((e[i].length == 2 && yy < 10) || (a[i].length == 2 && c != "y") || (z && a[i].length == 2)) {
yy = "0" + yy;
yy = yy.substring(yy.length - 2);
}
ml = a[i].length;
vl = String(yy).length;
nv += yy;
}
if (((ml == vl || z) && (x == c) && (i < s.length)) || (i < s.length && x != c)) nv += s[i];
}
if (nv.length > m.length) nv = nv.substring(0, m.length);
this.strippedValue = (nv == "NaN") ? "" : nv;
return this.strippedValue;
}
// PFM
Mask.prototype.updateFormattedValue = function (value) {
var formatted_value = this.format(value);
this.target.value = formatted_value;
return formatted_value;
}
Mask.prototype.log = function (message, level) {
if (this.webkit) {
switch (level) {
case "debug":
if (this.logLevel === 'debug')
try { console.debug(message); } catch (ex) { var a = 1; }
break;
case "error":
try { console.error(message); } catch (ex) { var a = 1; }
break;
case "info":
if (this.logLevel === 'debug' || this.logLevel === 'info')
try { console.info(message); } catch (ex) { var a = 1; }
break;
default:
try { console.debug(message); } catch (ex) { var a = 1; }
break;
}
return message;
}
if (this.msie) {
if (level === this.logLevel ||
(this.logLevel === 'debug') ||
(this.logLevel === 'info' && (level === 'info' || level === 'error')))
try { console.log(message); } catch (ex) { var a = 1; }
}
return message;
}
function qEvent(e) {
// PFM: Added explicit check for the browser agent
// since IE 8 now implements window.Event
var ua = navigator.userAgent;
if (ua.match(/MSIE/g)) {
// routine for Internet Explorer DOM browsers
e = window.event;
this.keyCode = parseInt(e.keyCode, 10);
this.button = e.button;
this.srcElement = e.srcElement;
this.type = e.type;
if (document.all) {
this.x = e.clientX + document.body.scrollLeft;
this.y = e.clientY + document.body.scrollTop;
} else {
this.x = e.clientX;
this.y = e.clientY;
}
this.screenX = e.screenX;
this.screenY = e.screenY;
this.altKey = e.altKey;
this.ctrlKey = e.ctrlKey;
this.shiftKey = e.shiftKey;
}
else {
// routine for NS, Opera, etc DOM browsers
var isKeyPress = (e.type.substring(0, 3) == "key");
this.keyCode = (isKeyPress) ? parseInt(e.which, 10) : 0;
this.button = (!isKeyPress) ? parseInt(e.which, 10) : 0;
this.srcElement = e.target;
this.type = e.type;
this.x = e.pageX;
this.y = e.pageY;
this.screenX = e.screenX;
this.screenY = e.screenY;
if (document.layers) {
this.altKey = ((e.modifiers & Event.ALT_MASK) > 0);
this.ctrlKey = ((e.modifiers & Event.CONTROL_MASK) > 0);
this.shiftKey = ((e.modifiers & Event.SHIFT_MASK) > 0);
this.keyCode = this.translateKeyCode(this.keyCode);
} else {
this.altKey = e.altKey;
this.ctrlKey = e.ctrlKey;
this.shiftKey = e.shiftKey;
}
}
if (this.button == 0) {
this.setKeyPressed(this.keyCode);
this.keyChar = String.fromCharCode(this.keyCode);
}
}
// this method will try to remap the keycodes so the keycode value
// returned will be consistent. this doesn't work for all cases,
// since some browsers don't always return a unique value for a
// key press.
qEvent.prototype.translateKeyCode = function (i) {
var l = {};
// remap NS4 keycodes to IE/W3C keycodes
if (!!document.layers) {
if (this.keyCode > 96 && this.keyCode < 123) return this.keyCode - 32;
l = {
96: 192, 126: 192, 33: 49, 64: 50, 35: 51, 36: 52, 37: 53, 94: 54, 38: 55, 42: 56, 40: 57, 41: 48, 92: 220, 124: 220, 125: 221,
93: 221, 91: 219, 123: 219, 39: 222, 34: 222, 47: 191, 63: 191, 46: 190, 62: 190, 44: 188, 60: 188, 45: 189, 95: 189, 43: 187,
61: 187, 59: 186, 58: 186,
"null": null
}
}
return (!!l[i]) ? l[i] : i;
}
// try to determine the actual value of the key pressed
qEvent.prototype.setKP = function (i, s) {
this.keyPressedCode = i;
this.keyNonChar = (typeof s == "string");
this.keyPressed = (this.keyNonChar) ? s : String.fromCharCode(i);
this.isNumeric = (parseInt(this.keyPressed, 10) == this.keyPressed);
this.isAlpha = ((this.keyCode > 64 && this.keyCode < 91) && !this.altKey && !this.ctrlKey);
return true;
}
// try to determine the actual value of the key pressed
qEvent.prototype.setKeyPressed = function (i) {
var b = this.shiftKey;
if (!b && (i > 64 && i < 91)) return this.setKP(i + 32);
if (i > 95 && i < 106) return this.setKP(i - 48);
switch (i) {
case 49: case 51: case 52: case 53: if (b) i = i - 16; break;
case 50: if (b) i = 64; break;
case 54: if (b) i = 94; break;
case 55: if (b) i = 38; break;
case 56: if (b) i = 42; break;
case 57: if (b) i = 40; break;
case 48: if (b) i = 41; break;
case 192: if (b) i = 126; else i = 96; break;
case 189: if (b) i = 95; else i = 45; break;
case 187: if (b) i = 43; else i = 61; break;
case 220: if (b) i = 124; else i = 92; break;
case 221: if (b) i = 125; else i = 93; break;
case 219: if (b) i = 123; else i = 91; break;
case 222: if (b) i = 34; else i = 39; break;
case 186: if (b) i = 58; else i = 59; break;
case 191: if (b) i = 63; else i = 47; break;
case 190: if (b) i = 62; else i = 46; break;
case 188: if (b) i = 60; else i = 44; break;
case 106: case 57379: i = 42; break;
case 107: case 57380: i = 43; break;
case 109: case 57381: i = 45; break;
case 110: i = 46; break;
case 111: case 57378: i = 47; break;
case 8: return this.setKP(i, "[backspace]");
case 9: return this.setKP(i, "[tab]");
case 13: return this.setKP(i, "[enter]");
case 16: case 57389: return this.setKP(i, "[shift]");
case 17: case 57390: return this.setKP(i, "[ctrl]");
case 18: case 57388: return this.setKP(i, "[alt]");
case 19: case 57402: return this.setKP(i, "[break]");
case 20: return this.setKP(i, "[capslock]");
case 32: return this.setKP(i, "[space]");
case 91: return this.setKP(i, "[windows]");
case 93: return this.setKP(i, "[properties]");
case 33: case 57371: return this.setKP(i * -1, "[pgup]");
case 34: case 57372: return this.setKP(i * -1, "[pgdown]");
case 35: case 57370: return this.setKP(i * -1, "[end]");
case 36: case 57369: return this.setKP(i * -1, "[home]");
case 37: case 57375: return this.setKP(i * -1, "[left]");
case 38: case 57373: return this.setKP(i * -1, "[up]");
case 39: case 57376: return this.setKP(i * -1, "[right]");
case 40: case 57374: return this.setKP(i * -1, "[down]");
case 45: case 57382: return this.setKP(i * -1, "[insert]");
case 46: case 57383: return this.setKP(i * -1, "[delete]");
case 144: case 57400: return this.setKP(i * -1, "[numlock]");
}
if (i > 111 && i < 124) return this.setKP(i * -1, "[f" + (i - 111) + "]");
return this.setKP(i);
}
// define the addEvent(oElement, sEvent, sCmd, bAppend) function
function $addEvent(o, _e, c, _b) {
var e = _e.toLowerCase(), b = (typeof _b == "boolean") ? _b : true, x = (o[e]) ? o[e].toString() : "";
// strip out the body of the function
x = x.substring(x.indexOf("{") + 1, x.lastIndexOf("}"));
x = ((b) ? (x + c) : (c + x)) + "\n";
return o[e] = (!!window.Event) ? new Function("event", x) : new Function(x);
}
// Define the keys function on Object if the current browser does not yet implement it (sigh!)
if (!Object.keys) Object.keys = function (o) {
if (o !== Object(o))
throw new TypeError('Object.keys called on non-object');
var ret = [], p;
for (p in o) if (Object.prototype.hasOwnProperty.call(o, p)) ret.push(p);
return ret;
}