(function(n){function r(){var n=document.createElement("input"),t="onpaste";return n.setAttribute(t,""),"function"==typeof n[t]?"paste":"input"}var t,u=r()+".mask",i=navigator.userAgent,f=/iphone/i.test(i),e=/android/i.test(i);n.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},dataName:"rawMaskFn",placeholder:"_"};n.fn.extend({caret:function(n,t){var i;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof n?(t="number"==typeof t?t:n,this.each(function(){this.setSelectionRange?this.setSelectionRange(n,t):this.createTextRange&&(i=this.createTextRange(),i.collapse(!0),i.moveEnd("character",t),i.moveStart("character",n),i.select())})):(this[0].setSelectionRange?(n=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(i=document.selection.createRange(),n=0-i.duplicate().moveStart("character",-1e5),t=n+i.text.length),{begin:n,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(i,r){var a,l,o,h,c,s;return!i&&this.length>0?(a=n(this[0]),a.data(n.mask.dataName)()):(r=n.extend({placeholder:n.mask.placeholder,completed:null},r),l=n.mask.definitions,o=[],h=s=i.length,c=null,n.each(i.split(""),function(n,t){"?"==t?(s--,h=n):l[t]?(o.push(RegExp(l[t])),null===c&&(c=o.length-1)):o.push(null)}),this.trigger("unmask").each(function(){function y(n){for(;s>++n&&!o[n];);return n}function g(n){for(;--n>=0&&!o[n];);return n}function d(n,t){var u,i;if(!(0>n)){for(u=n,i=y(t);s>u;u++)if(o[u]){if(!(s>i&&o[u].test(v[i])))break;v[u]=v[i];v[i]=r.placeholder;i=y(i)}p();a.caret(Math.max(c,n))}}function nt(n){for(var u,f,t=n,i=r.placeholder;s>t;t++)if(o[t]){if(u=y(t),f=v[t],v[t]=i,!(s>u&&o[u].test(f)))break;i=f}}function tt(n){var u,i,t,r=n.which;8===r||46===r||f&&127===r?(u=a.caret(),i=u.begin,t=u.end,0==t-i&&(i=46!==r?g(i):t=y(i-1),t=46===r?y(t):t),b(i,t),d(i,t-1),n.preventDefault()):27==r&&(a.val(k),a.caret(0,w()),n.preventDefault())}function it(t){var u,h,f,c=t.which,i=a.caret();t.ctrlKey||t.altKey||t.metaKey||32>c||c&&(0!=i.end-i.begin&&(b(i.begin,i.end),d(i.begin,i.end-1)),u=y(i.begin-1),s>u&&(h=String.fromCharCode(c),o[u].test(h)&&(nt(u),v[u]=h,p(),f=y(u),e?setTimeout(n.proxy(n.fn.caret,a,f),0):a.caret(f),r.completed&&f>=s&&r.completed.call(a))),t.preventDefault())}function b(n,t){for(var i=n;t>i&&s>i;i++)o[i]&&(v[i]=r.placeholder)}function p(){a.val(v.join(""))}function w(n){var t,f,i=a.val(),u=-1;for(t=0,pos=0;s>t;t++)if(o[t]){for(v[t]=r.placeholder;pos++<i.length;)if(f=i.charAt(pos-1),o[t].test(f)){v[t]=f;u=t;break}if(pos>i.length)break}else v[t]===i.charAt(pos)&&t!==h&&(pos++,u=t);return n?p():h>u+1?(a.val(""),b(0,s)):(p(),a.val(a.val().substring(0,u+1))),h?t:c}var a=n(this),v=n.map(i.split(""),function(n){if("?"!=n)return l[n]?r.placeholder:n}),k=a.val();a.data(n.mask.dataName,function(){return n.map(v,function(n,t){return o[t]&&n!=r.placeholder?n:null}).join("")});a.attr("readonly")||a.one("unmask",function(){a.unbind(".mask").removeData(n.mask.dataName)}).bind("focus.mask",function(){clearTimeout(t);var n;k=a.val();n=w();t=setTimeout(function(){p();n==i.length?a.caret(0,n):a.caret(n)},10)}).bind("blur.mask",function(){w();a.val()!=k&&a.change()}).bind("keydown.mask",tt).bind("keypress.mask",it).bind(u,function(){setTimeout(function(){var n=w(!0);a.caret(n);r.completed&&n==a.val().length&&r.completed.call(a)},0)});w()}))}})})(jQuery)