(function(n){function r(){var n=document.createElement("input"),t="onpaste";return n.setAttribute(t,""),typeof n[t]=="function"?"paste":"input"}var u=r()+".mask",t=navigator.userAgent,f=/iphone/i.test(t),e=/android/i.test(t),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(this.length!==0&&!this.is(":hidden"))return typeof n=="number"?(t=typeof t=="number"?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(t,r){var a,l,o,h,c,s;return!t&&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=t.length,c=null,n.each(t.split(""),function(n,t){t=="?"?(s--,h=n):l[t]?(o.push(new RegExp(l[t])),c===null&&(c=o.length-1)):o.push(null)}),this.trigger("unmask").each(function(){function y(n){while(++n<s&&!o[n]);return n}function g(n){while(--n>=0&&!o[n]);return n}function d(n,t){var u,i;if(!(n<0)){for(u=n,i=y(t);u<s;u++)if(o[u]){if(i<s&&o[u].test(v[i]))v[u]=v[i],v[i]=r.placeholder;else break;i=y(i)}p();a.caret(Math.max(c,n))}}function nt(n){for(var u,f,t=n,i=r.placeholder;t<s;t++)if(o[t])if(u=y(t),f=v[t],v[t]=i,u<s&&o[u].test(f))i=f;else break}function tt(n){var r=n.which,u,i,t;r===8||r===46||f&&r===127?(u=a.caret(),i=u.begin,t=u.end,t-i==0&&(i=r!==46?g(i):t=y(i-1),t=r===46?y(t):t),k(i,t),d(i,t-1),n.preventDefault()):r==27&&(a.val(b),a.caret(0,w()),n.preventDefault())}function it(t){var h=t.which,i=a.caret(),u,c,f;t.ctrlKey||t.altKey||t.metaKey||h<32||h&&(i.end-i.begin!=0&&(k(i.begin,i.end),d(i.begin,i.end-1)),u=y(i.begin-1),u<s&&(c=String.fromCharCode(h),o[u].test(c)&&(nt(u),v[u]=c,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 k(n,t){for(var i=n;i<t&&i<s;i++)o[i]&&(v[i]=r.placeholder)}function p(){a.val(v.join(""))}function w(n){var i=a.val(),u=-1,t,f;for(t=0,pos=0;t<s;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():u+1<h?(a.val(""),k(0,s)):(p(),a.val(a.val().substring(0,u+1))),h?t:c}var a=n(this),v=n.map(t.split(""),function(n){if(n!="?")return l[n]?r.placeholder:n}),b=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(i);var n;b=a.val();n=w();i=setTimeout(function(){p();n==t.length?a.caret(0,n):a.caret(n)},10)}).bind("blur.mask",function(){w();a.val()!=b&&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)