(function(b){var z=(b.browser.msie?"paste":"input")+".mask",A=void 0!=window.orientation;b.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"}};b.fn.extend({caret:function(d,f){if(0!=this.length){if("number"==typeof d)return f="number"==typeof f?f:d,this.each(function(){if(this.setSelectionRange)this.focus(),this.setSelectionRange(d,f);else if(this.createTextRange){var g=this.createTextRange();g.collapse(!0);g.moveEnd("character",f);g.moveStart("character",d);g.select()}});if(this[0].setSelectionRange)d=
this[0].selectionStart,f=this[0].selectionEnd;else if(document.selection&&document.selection.createRange){var b=document.selection.createRange();d=0-b.duplicate().moveStart("character",-1E5);f=d+b.text.length}return{begin:d,end:f}}},unmask:function(){return this.trigger("unmask")},mask:function(d,f){if(!d&&0<this.length){var p=b(this[0]),g=p.data("tests");return b.map(p.data("buffer"),function(f,b){return g[b]?f:null}).join("")}f=b.extend({placeholder:"_",completed:null},f);var v=b.mask.definitions,
g=[],m=d.length,r=null,l=d.length;b.each(d.split(""),function(f,b){"?"==b?(l--,m=f):v[b]?(g.push(new RegExp(v[b])),null==r&&(r=g.length-1)):g.push(null)});return this.each(function(){function q(a){for(;++a<=l&&!g[a];);return a}function p(a){var c=b(this).caret();a=a.keyCode;t=16>a||16<a&&32>a||32<a&&41>a;0==c.begin-c.end||t&&8!=a&&46!=a||x(c.begin,c.end);if(8==a||46==a||A&&127==a){for(c=c.begin+(46==a?0:-1);!g[c]&&0<=--c;);for(a=c;a<l;a++)if(g[a]){k[a]=f.placeholder;var e=q(a);if(e<l&&g[a].test(k[e]))k[a]=
k[e];else break}u();h.caret(Math.max(r,c));return!1}if(27==a)return h.val(w),h.caret(0,n()),!1}function B(a){if(t)return t=!1,8==a.keyCode?!1:null;a=a||window.event;var c=a.charCode||a.keyCode||a.which,e=b(this).caret();if(a.ctrlKey||a.altKey||a.metaKey)return!0;if(32<=c&&125>=c||186<c)if(a=q(e.begin-1),a<l&&(c=String.fromCharCode(c),g[a].test(c))){for(var e=a,d=f.placeholder;e<l;e++)if(g[e]){var y=q(e),m=k[e];k[e]=d;if(y<l&&g[y].test(m))d=m;else break}k[a]=c;u();a=q(a);b(this).caret(a);f.completed&&
a==l&&f.completed.call(h)}return!1}function x(a,c){for(var e=a;e<c&&e<l;e++)g[e]&&(k[e]=f.placeholder)}function u(){return h.val(k.join("")).val()}function n(a){for(var c=h.val(),e=-1,b=0,d=0;b<l;b++)if(g[b]){for(k[b]=f.placeholder;d++<c.length;){var n=c.charAt(d-1);if(g[b].test(n)){k[b]=n;e=b;break}}if(d>c.length)break}else k[b]==c[d]&&b!=m&&(d++,e=b);if(!a&&e+1<m)h.val(""),x(0,l);else if(a||e+1>=m)u(),a||h.val(h.val().substring(0,e+1));return m?b:r}var h=b(this),k=b.map(d.split(""),function(a,b){if("?"!=
a)return v[a]?f.placeholder:a}),t=!1,w=h.val();h.data("buffer",k).data("tests",g);h.attr("readonly")||h.one("unmask",function(){h.unbind(".mask").removeData("buffer").removeData("tests")}).bind("focus.mask",function(){w=h.val();var a=n();u();setTimeout(function(){a==d.length?h.caret(0,a):h.caret(a)},0)}).bind("blur.mask",function(){n();h.val()!=w&&h.change()}).bind("keydown.mask",p).bind("keypress.mask",B).bind(z,function(){setTimeout(function(){h.caret(n(!0))},0)});n()})}})})(jQuery);
