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