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