﻿function Validation_FilterInput(filterTypes, evt, allowDecimal, allowCustom) {
    var filters = {
        'Numeric': '0123456789',
        'en-US': 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
        'he-IL': 'אבגדהוזחטיכךלמםנןסעפףצץקרשת' };

    var keyCode, Char, inputField, filter = '';
    var filter = '';
    var types = filterTypes.split(',');
    var filterNumeric = false;

    if (window.event) {
        keyCode = window.event.keyCode;
        evt = window.event;
    } else if (evt) keyCode = evt.which;
    else return true;

    for (var i = 0; i < types.length; i++) {
        var typeName = types[i];
        filter += filters[typeName];
        if (typeName == 'Numeric') {
            filterNumeric = true;
        }
    }

    if (filterNumeric && types.length > 1) {
        filter += ' ",.:;/\!@#$%^&*(){}~_+-='
    }

    if (allowCustom) filter += allowCustom;
    if (filter == '') return true;
    // Get the Element that triggered the Event 
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget;
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow 
    if ((keyCode == null) || (keyCode == 0) || (keyCode == 8) || (keyCode == 9) || (keyCode == 13) || (keyCode == 27)) return true;
    // Get the Pressed Character 
    Char = String.fromCharCode(keyCode);
    // If the Character is a number - allow 
    if ((filter.indexOf(Char) > -1)) return true;
    // Else if Decimal Point is allowed and the Character is '.' - allow 
    else if (filterNumeric && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1) return true;
    else return false;
}

function Validation_ValidateGroup(groupName) {
    var val = true;

    Page_ClientValidate(groupName);

    for (var i = 0; i < Page_Validators.length; i++) {
        var v = Page_Validators[i];
        
        if (v) {
            if (v.validationGroup == groupName) {
                var c = v.controltovalidate;
                if (c) {
                    if (!v.isvalid) {
                        val = false;
                        $("#" + c).css("background-color", "pink");
                    } else {
                        $("#" + c).css("background-color", "white");
                    }
                }
            }
        }
    }

    return val;
}

function Validation_ConfirmDelete() {
    return confirm("? האם אתה בטוח שברצונך למחוק פריט זה");
}

function Validation_CheckFloat(e, field) {
    if (!(((e.keyCode >= 48) && (e.keyCode <= 57)) || (e.keyCode == 46))) {
        alert("Only Digits Are Allowed!");
        e.keyCode = 0;
    }
    if (e.keyCode == 46) {
        var patt1 = new RegExp("\\.");
        var ch = patt1.exec(field);
        if (ch == ".") {
            alert("More then one decimal point not allowed");
            e.keyCode = 0;
        }
    }
}

