var form = document.getElementById('form'),
    replacement = {
        a: 'ა', b: 'ბ', g: 'გ', d: 'დ',
        e: 'ე', v: 'ვ', z: 'ზ', T: 'თ',
        i: 'ი', k: 'კ', l: 'ლ', m: 'მ',
        n: 'ნ', o: 'ო', p: 'პ', J: 'ჟ',
        r: 'რ', s: 'ს', t: 'ტ', u: 'უ',
        f: 'ფ', q: 'ქ', R: 'ღ', y: 'ყ',
        S: 'შ', C: 'ჩ', c: 'ც', Z: 'ძ',
        w: 'წ', W: 'ჭ', x: 'ხ', j: 'ჯ',
        h: 'ჰ'
    },
    keypressWorks = false;

form.protocolNo.onkeypress = function (e) {
    if (e) {
        if (e.ctrlKey || e.altKey || e.metaKey) return;
        var keypressWorks = true;
        var c = String.fromCharCode(e.charCode || e.keyCode);
        if (replacement[c]) {
            var start = this.selectionStart;
            this.value = this.value.substr(0, start) + replacement[c] + this.value.substr(this.selectionEnd);
            this.selectionEnd = this.selectionStart = start + 1;
            return false;
        }
    }
};

form.vehicleNo1.onkeypress = form.vehicleNo1.onkeypress = function (e) {
    if (e) {
        if (e.ctrlKey || e.altKey || e.metaKey) return;
        var keypressWorks = true;
        var c = String.fromCharCode(e.charCode || e.keyCode);
        if (c >= 'a' && c <= 'z') {
            var start = this.selectionStart;
            this.value = this.value.substr(0, start) + c.toUpperCase() + this.value.substr(this.selectionEnd);
            this.selectionEnd = this.selectionStart = start + 1;
            return false;
        }
    }
};

form.protocolNo.onblur = function () {
    if (!keypressWorks) {
        this.value = this.value.replace(/[a-z]/ig, function (a) {
            return replacement[a] || a;
        });
    }
};

form.vehicleNo1.onblur = form.vehicleNo2.onblur = function () {
    if (!keypressWorks) {
        this.value = this.value.replace(/[a-z]/ig, function (a) {
            return a.toUpperCase();
        });
    }
};

