﻿var keyboardLayout = {};
var langCycle = [''];

(function() {
    var k = null;
    var titleTimeout = null;

    function showLang(langIndex) {
        var langTitle = 'Language: ';
        for (var i = 0; i < langCycle.length; i++) {
            var lang = getLangFromCycle(i) || 'default';
            langTitle += (i == langIndex ? '[' + lang + ']' : lang) + ' ';
        }

        if (titleTimeout) clearTimeout(titleTimeout);
        document.oldTitle = document.oldTitle || document.title;
        document.title = langTitle;
        titleTimeout = setTimeout(function() {
            document.title = document.oldTitle;
            document.oldTitle = undefined;
            titleTimeout = null;
        }, 1000);
    }

    function getLangFromCycle(i) {
        return langCycle[i].lang;
    }

    function getDirectionFromCycle(i) {
        return langCycle[i].direction ? langCycle[i].direction : '';
    }

    function getNextLangIndex(targetLang) {
        var nextIndex = langCycle.length ? 1 : -1;
        for (var i = 0; i < langCycle.length; i++) {
            if (getLangFromCycle(i) == targetLang) {
                nextIndex = i < langCycle.length - 1 ? i + 1 : 0;
            }
        }
        return nextIndex;
    }

    function addEvent(elem, type, handle) {
        if (elem.addEventListener)
            elem.addEventListener(type, handle, false);
        else if (elem.attachEvent)
            elem.attachEvent("on" + type, handle);
        else
            elem["on" + type] = handle;
    }

    function isTextField(elem) {
        return (elem.tagName == 'TEXTAREA') ||
            (elem.tagName == 'INPUT' && elem.type && elem.type.toUpperCase() == 'TEXT');
    }

    addEvent(document, "keydown", function(e) {
        k = e.which;
        var target = e.target || e.srcElement;
        if (!isTextField(target)) return;
        if (target.readOnly) return;
        var key = e.which || e.charCode || e.keyCode;
        var lang = (target.getAttribute('lang') || '').match(/^[a-zA-Z]{2}/) || '';
        if (e.ctrlKey && e.shiftKey && langCycle && langCycle.length > 1) {
            var nextLangIndex = getNextLangIndex(lang);
            if (nextLangIndex >= 0) {
                showLang(nextLangIndex);
                target.setAttribute('lang', getLangFromCycle(nextLangIndex));
                target.style.direction = getDirectionFromCycle(nextLangIndex);
            }
            if (e.preventDefault) e.preventDefault();
            e.returnValue = false;
            return false;
        }

        if (e.altKey && e.shiftKey && langCycle.length > 1) {
            var nextLangIndex = getNextLangIndex(lang);
            if (nextLangIndex >= 0) {
                showLang(nextLangIndex);
                target.setAttribute('lang', getLangFromCycle(nextLangIndex));
                target.style.direction = getDirectionFromCycle(nextLangIndex);
            }
            if (e.preventDefault) e.preventDefault();
            e.returnValue = false;
            return false;
        }
    }
	);
    addEvent(document, "keypress", function(e) {
        var target = e.target || e.srcElement;
        if (!isTextField(target)) return;
        if (target.readOnly) return;
        var key = e.which || e.charCode || e.keyCode;
        //read layout language from lang attribute
        var lang = (target.getAttribute('lang') || '').match(/^[a-zA-Z]{2}/) || '';
        if (!lang) {
            if (!langCycle.length) return true;
            lang = getLangFromCycle(0);
        }

        var layout = keyboardLayout[lang];
        if ((!layout) ||
            (e.ctrlKey || e.altKey || e.metaKey) ||
            (k != 0x0020 && k < 0x0030) ||  //fix for opera
            (key < 0x0020 || key > 0x007F))
            return true;

        var value =
            (key == 0x0020 && e.shiftKey && layout[0x005f]) ||  //Shift+Space may be defined in layout[0x5f]
            (layout[key - 0x0020]);

        value = typeof value == 'string' ?
            value :
            String.fromCharCode(value);

        var ds = document.selection,
            ss = target.selectionStart;

        if (typeof ss == 'number') { //standard browsers: http://www.w3.org/TR/html5/editing.html#dom-textarea-input-selectionstart
            var sl = target.scrollLeft, st = target.scrollTop;  //fix for firefox
            target.value = target.value.substring(0, ss) + value + target.value.substring(target.selectionEnd, target.value.length);
            var sr = ss + value.length;
            target.setSelectionRange(sr, sr);
            target.scrollLeft = sl;
            target.scrollTop = st;
        } else if (ds) { //IE: http://msdn.microsoft.com/en-us/library/ms535869(VS.85).aspx
            //B.Shokri 90/08/03 S
            var r = ds.createRange();
            r.text = value;
            if (target.isMultiLine) {
                r.setEndPoint('StartToEnd', r);
                r.select();
            }
            else {
                var pos = getCaretPosition(target);
                setCaretPosition(target, pos);
            }
            //B.Shokri 90/08/03 E
        } else { //unknown browsers
            target.value += value;
        }

        if (e.preventDefault) e.preventDefault();
        e.returnValue = false;
        return false;
    });
})();
//B.Shokri 90/08/03 S

function setCaretPosition(ctrl, pos) {
    var range = ctrl.createTextRange();
    if (ctrl.createTextRange) {
        if (pos == range.text.length) {
            range.collapse(true);
            range.moveStart('character', 1);
            range.moveEnd('character', 1);
            range.select();
            range.moveStart('character', pos);
            range.select();
        }
        else {
            range.collapse(true);
            range.select();
            range.moveStart('character', pos);
            range.select();
        }
    }
}


function getCaretPosition(ctrl) {
    var CaretPos = 0; // IE Support
    if (document.selection) {
        ctrl.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        CaretPos = ctrl.selectionStart;
    return (CaretPos);
}
//B.Shokri 90/08/03 E
keyboardLayout['fa'] = [
   0x0020, 0x0021, 0x061b, 0x0023, 0x0024, 0x066a, 0x0026,
   0x06af, 0x0029, 0x0028, 0x002a, 0x002b, 0x0648, 0x002d,
   0x002e, 0x002f, 0x06f0, 0x06f1, 0x06f2, 0x06f3, 0x06f4,
   0x06f5, 0x06f6, 0x06f7, 0x06f8, 0x06f9, 0x003a, 0x06a9,
   0x003e, 0x003d, 0x003c, 0x061f, 0x0040, 0x064e, 0x0625,
   0x0698, 0x0650, 0x064d, 0x0651, 0x0652, 0x0622, 0x005b,
   0x0629, 0x00bb, 0x00ab, 0x0621, 0x0623, 0x005d, 0x005c,
   0x064b, 0x064b, 0x064F, 0x066c, 0x066b, 0x0624, 0x064c,
   0x06cc, 0x061b, 0x0629, 0x062c, 0x0698, 0x0686, 0x005e,
   0x0640, 0x067e, 0x0634, 0x0630, 0x0632, 0x06cc, 0x062b,
   0x0628, 0x0644, 0x0627, 0x0647, 0x062a, 0x0646, 0x0645,
   0x0626, 0x062f, 0x062e, 0x062d, 0x0636, 0x0642, 0x0633,
   0x0641, 0x0639, 0x0631, 0x0635, 0x0637, 0x063a, 0x0638,
   0x007d, 0x007c, 0x007b, 0x007E, 0x200c
];

langCycle = [{ lang: 'en', direction: 'rtl' }, { lang: 'fa', direction: 'rtl'}];


function OnKeyPressNumeric(textbox, e) {
    var KeyCode;
    /// detect a special case of "web browser"
    var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
    var NumeralInput = true;
    if (!is_ie) {
        //FireFox,Safari,Crome and other Browsers
        if (e.target.name) {
            KeyCode = e.charCode;
            if ((e.ctrlKey || e.altKey) && KeyCode!=0) return(true);
            if (KeyCode != 0) {
                if (((KeyCode < 48) || (KeyCode > 57)) && (KeyCode != 10) && (KeyCode != 13)) {
                    NumeralInput = false;
                }

                if ((NumeralInput == false) || ((NumeralInput == true) && (parseInt(textbox.maxLength) == 10 && textbox.value.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1)))) {
                    e.preventDefault();
                }
            }
            return (true);
        }
    }
    else { //IE Browser
        if (event.srcElement.name) // Checking Existance of Source Element.
        {
            KeyCode = window.event.keyCode;
            if (((KeyCode < 48) || (KeyCode > 57)) && (KeyCode != 10) && (KeyCode != 13))
                window.event.keyCode = 0;
            else if (parseInt(textbox.maxLength) == 10 && textbox.value.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1))
                window.event.keyCode = 0;
            return (true);
        }

    }

    //old code: Only work in IE
    //    if (event.srcElement.name) // Checking Existance of Source Element.
    //    {
    //        KeyCode = window.event.keyCode;

    //        if (((KeyCode < 48) || (KeyCode > 57)) && (KeyCode != 10) && (KeyCode != 13))
    //            window.event.keyCode = 0;
    //        else if (parseInt(textbox.maxLength) == 10 && textbox.value.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1))
    //            window.event.keyCode = 0;

    //        return (true);
    //    }

}

function OnKeyPressFloat(textbox, decimalpartlen, e) {
    var KeyCode;
    /// detect a special case of "web browser"
    var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
    if (!is_ie) { //FireFox,Safari,Crome and other Browsers
        if (e.target.name) {
            KeyCode = e.charCode;
            if (KeyCode != 0) {
                if ((textbox.value.length == (parseInt(textbox.maxLength) - parseInt(decimalpartlen))) && (KeyCode != 46) && (KeyCode > 31) && (textbox.value.toString().indexOf(".", 1) <= 0) && parseInt(textbox.maxLength) > parseInt(decimalpartlen))
                    e.preventDefault();
                if ((KeyCode == 46) && (parseInt(textbox.value.toString().indexOf(".", 0)) != -1))
                    e.preventDefault();

                if (((KeyCode < 48) || (KeyCode > 57)) && (KeyCode > 31) && (KeyCode != 46) && (KeyCode != 10) && (KeyCode != 13))
                    e.preventDefault();
                if ((parseInt(textbox.value.toString().indexOf(".", 0)) != -1) && (KeyCode > 31) && parseInt(textbox.maxLength) > parseInt(decimalpartlen)) {
                    var indx = parseInt(textbox.value.toString().indexOf(".", 0));
                    if (indx != 0) {
                        var str1 = textbox.value.toString().substring(indx)
                        if (str1.length >= parseInt(decimalpartlen))//And .SelLength <> Len(.Text)
                        {
                            e.preventDefault();
                        }
                    }
                }
            }
            return (true);
        }
    }
    else { //IE Browsers
        if (event.srcElement.name) // Checking Existance of Source Element.
        {
            KeyCode = window.event.keyCode;
            if ((textbox.value.length == (parseInt(textbox.maxLength) - parseInt(decimalpartlen))) && (KeyCode != 46) && (KeyCode > 31) && (textbox.value.toString().indexOf(".", 1) <= 0) && parseInt(textbox.maxLength) > parseInt(decimalpartlen))
                window.event.keyCode = 0;
            if ((KeyCode == 46) && (parseInt(textbox.value.toString().indexOf(".", 0)) != -1))
                window.event.keyCode = 0;
            if (((KeyCode < 48) || (KeyCode > 57)) && (KeyCode > 31) && (KeyCode != 46) && (KeyCode != 10) && (KeyCode != 13))
                window.event.keyCode = 0;
            if ((parseInt(textbox.value.toString().indexOf(".", 0)) != -1) && (KeyCode > 31) && parseInt(textbox.maxLength) > parseInt(decimalpartlen)) {
                var indx = parseInt(textbox.value.toString().indexOf(".", 0));
                if (indx != 0) {
                    var str1 = textbox.value.toString().substring(indx)
                    if (str1.length >= parseInt(decimalpartlen))//And .SelLength <> Len(.Text)
                    {
                        window.event.keyCode = 0;
                    }
                }
            }
            return (true);
        }
    }
}

function OnKeyPressSignedFloat(textbox, decimalpartlen, e) {
    var KeyCode;
    /// detect a special case of "web browser"
    var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
    if (!is_ie) { //FireFox,Safari,Crome and other Browsers
        if (e.target.name) {
            KeyCode = e.charCode;
            if (KeyCode != 0) {
                if ((textbox.value.length == (parseInt(textbox.maxLength) - parseInt(decimalpartlen)))
                && (KeyCode != 46) && (KeyCode > 31)
                && (textbox.value.toString().indexOf(".", 1) <= 0)
                && parseInt(textbox.maxLength) > parseInt(decimalpartlen)
                && (KeyCode != 45) && (KeyCode > 31)
                && (textbox.value.toString().indexOf("-", 1) <= 0))
                    e.preventDefault();

                if ((KeyCode == 46) && (parseInt(textbox.value.toString().indexOf(".", 0)) != -1)
                && (KeyCode == 45) && (parseInt(textbox.value.toString().indexOf("-", 0)) != -1))
                    e.preventDefault();

                if (((KeyCode < 48) || (KeyCode > 57))
                && (KeyCode > 31) && (KeyCode != 46)
                && (KeyCode != 10) && (KeyCode != 13)
                && (KeyCode != 45))
                    e.preventDefault();

                if (((parseInt(textbox.value.toString().indexOf(".", 0)) != -1)
                || (parseInt(textbox.value.toString().indexOf("-", 0)) != -1))
                && (KeyCode > 31)
                && parseInt(textbox.maxLength) > parseInt(decimalpartlen)
                ) {
                    if (KeyCode == 45) {
                        var indx = parseInt(textbox.value.toString().indexOf("-", 0));
                        if (indx != 0) {
                            var str1 = textbox.value.toString().substring(indx)
                            if (parseInt(str1.Length) == 10 && str1.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1)) {
                                e.preventDefault();
                            }

                            if ((str1.length > 0) && (KeyCode == 45)) {
                                e.preventDefault();
                            }
                        }
                    }
                    else {
                        var indxdot = parseInt(textbox.value.toString().indexOf(".", 0));
                        if (indxdot != 0 && indxdot != -1) {
                            var str1 = textbox.value.toString().substring(indxdot)
                            if (str1.length >= parseInt(decimalpartlen))//And .SelLength <> Len(.Text)
                            {
                                e.preventDefault();
                            }
                        }
                    }
                }
            }
            return (true);
        }
    }
    else {//IE Browser
        if (event.srcElement.name) // Checking Existance of Source Element.
        {
            KeyCode = window.event.keyCode;

            if ((textbox.value.length == (parseInt(textbox.maxLength) - parseInt(decimalpartlen)))
                && (KeyCode != 46) && (KeyCode > 31)
                && (textbox.value.toString().indexOf(".", 1) <= 0)
                && parseInt(textbox.maxLength) > parseInt(decimalpartlen)
                && (KeyCode != 45) && (KeyCode > 31)
                && (textbox.value.toString().indexOf("-", 1) <= 0))
                window.event.keyCode = 0;

            if ((KeyCode == 46) && (parseInt(textbox.value.toString().indexOf(".", 0)) != -1)
                && (KeyCode == 45) && (parseInt(textbox.value.toString().indexOf("-", 0)) != -1))
                window.event.keyCode = 0;

            if (((KeyCode < 48) || (KeyCode > 57))
                && (KeyCode > 31) && (KeyCode != 46)
                && (KeyCode != 10) && (KeyCode != 13)
                && (KeyCode != 45))
                window.event.keyCode = 0;

            if (((parseInt(textbox.value.toString().indexOf(".", 0)) != -1)
                || (parseInt(textbox.value.toString().indexOf("-", 0)) != -1))
                && (KeyCode > 31)
                && parseInt(textbox.maxLength) > parseInt(decimalpartlen)
                ) {
                if (KeyCode == 45) {
                    var indx = parseInt(textbox.value.toString().indexOf("-", 0));
                    if (indx != 0) {
                        var str1 = textbox.value.toString().substring(indx)
                        if (parseInt(str1.Length) == 10 && str1.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1)) {
                            window.event.keyCode = 0;
                        }

                        if ((str1.length > 0) && (KeyCode == 45)) {
                            window.event.keyCode = 0;
                        }
                    }
                }
                else {
                    var indxdot = parseInt(textbox.value.toString().indexOf(".", 0));
                    if (indxdot != 0 && indxdot != -1) {
                        var str1 = textbox.value.toString().substring(indxdot)
                        if (str1.length >= parseInt(decimalpartlen))//And .SelLength <> Len(.Text)
                        {
                            window.event.keyCode = 0;
                        }
                    }
                }
            }
            return (true);
        }
    }
}

function OnKeyPressSignInt(textbox, e) {
    var KeyCode;
    /// detect a special case of "web browser"
    var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
    if (!is_ie) {  //FireFox,Safari,Crome and other Browsers
        if (e.target.name) {
            KeyCode = e.charCode;
            if (KeyCode != 0) {
                if ((KeyCode == 45) && (parseInt(textbox.value.toString().indexOf("-", 0)) != -1))
                    e.preventDefault();

                if (((KeyCode < 48) || (KeyCode > 57)) && (KeyCode != 10) && (KeyCode != 13) && (KeyCode != 45))
                    e.preventDefault();
                else if (parseInt(textbox.maxLength) == 10 && textbox.value.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1))
                    e.preventDefault();

                if ((parseInt(textbox.value.toString().indexOf("-", 0)) == -1) && (KeyCode > 31)) {
                    var indx = parseInt(textbox.value.toString().indexOf("-", 0));
                    if (indx != 0) {
                        var str1 = textbox.value.toString().substring(indx)
                        if (parseInt(str1.Length) == 10 && str1.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1)) {
                            e.preventDefault();
                        }
                        if ((str1.length > 0) && (KeyCode == 45)) {
                            e.preventDefault();
                        }
                    }
                }
            }
            return (true);
        }
    }
    else {  //IE Browsers

        if (event.srcElement.name) // Checking Existance of Source Element.
        {
            KeyCode = window.event.keyCode;

            if ((KeyCode == 45) && (parseInt(textbox.value.toString().indexOf("-", 0)) != -1))
                window.event.keyCode = 0;

            if (((KeyCode < 48) || (KeyCode > 57)) && (KeyCode != 10) && (KeyCode != 13) && (KeyCode != 45))
                window.event.keyCode = 0;
            else if (parseInt(textbox.maxLength) == 10 && textbox.value.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1))
                window.event.keyCode = 0;

            if ((parseInt(textbox.value.toString().indexOf("-", 0)) == -1) && (KeyCode > 31)) {
                var indx = parseInt(textbox.value.toString().indexOf("-", 0));
                if (indx != 0) {
                    var str1 = textbox.value.toString().substring(indx)
                    if (parseInt(str1.Length) == 10 && str1.length == 9 && (parseInt(textbox.value.substring(0, 1)) > 1)) {
                        window.event.keyCode = 0;
                    }
                    if ((str1.length > 0) && (KeyCode == 45)) {
                        window.event.keyCode = 0;
                    }
                }
            }

            return (true);
        }
    }
}

function OnKeyPressMultiLine(textbox, maxlenght,e) {
    var KeyCode;
    /// detect a special case of "web browser"
    var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
    if (!is_ie) {  //FireFox,Safari,Crome and other Browsers
        if (e.target.name) {
            KeyCode = e.charCode;
            if (KeyCode != 0) {
                if (parseInt(maxlenght) > 0 && textbox.value.length >= parseInt(maxlenght)) {
                    e.preventDefault();
                }
            }
            return (true);
        }
    }
    else { //IE brower
        if (event.srcElement.name) // Checking Existance of Source Element.
        {
            if (parseInt(maxlenght) > 0 && textbox.value.length >= parseInt(maxlenght)) {
                window.event.keyCode = 0;

                return (true);
            }
        }
    }
}

function OnChangeMultiLine(textbox, maxlenght) {
    if (parseInt(maxlenght) > 0 && textbox.value.length >= parseInt(maxlenght)) {
        textbox.value = textbox.value.substring(0, maxlenght)
    }
}

