/**
 * String utilities [ end ]
 *
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */

// =============================================================================================
// Utils for string, date, number [ end ]
// =============================================================================================
var ValidationUtil = {
    // Required Text
    CheckRequiredForText: function (value) {
        if (value == null || value == '') {
            return false;
        }
        return true;
    },

    // get byte count
    GetByteCount: function (value) {
        var escapedStr = encodeURI(value);
        if (escapedStr.indexOf('%') != -1) {
            var count = escapedStr.split('%').length - 1;
            if (count == 0) count++; // perverse case; can't happen with real UTF-8
            var tmp = escapedStr.length - count * 3;
            count = count + tmp;
        } else {
            count = escapedStr.length;
        }
        return count;
    },

    // check text min length
    CheckMinLengthForByte: function (value, len) {
        if (this.GetByteCount(value) < len) return false;
        return true;
    },

    // check text max length
    CheckMaxLengthForByte: function (value, len) {
        if (this.GetByteCount(value) > len) return false;
        return true;
    },

    // check if the text is a number
    IsNumber: function (value) {
        var reg = new RegExp('^[0-9]+$');
        return reg.test(value);
    },

    // check if the text is a alphabet
    IsAlphabet: function (value) {
        var reg = new RegExp('^[a-zA-Z]+$');
        return reg.test(value);
    },

    // check if the text is a symbol
    IsSymbol: function (value) {
        var reg = new RegExp(
            '\u005b\u005e\u0027\u0060\u0027\u007e\u0027\u0021\u0027\u0040\u0027\u0023\u0027\u0024\u0027\u0025\u0027\u005e\u0027\u0026\u0027\u002a\u0027\u0028\u0027\u0029\u0027\u005f\u0027\u002b\u0027\u003d\u0027\u007b\u0027\u007d\u0027\u007c\u0027\u003a\u0027\u0022\u0027\u003b\u0027\u0027\u0027\u003c\u0027\u003e\u0027\u003f\u0027\u002f\u0027\u002e\u0027\u002c\u005c\u002d\u005b\u005c\u005d\u005c\u005c\u005d',
        );
        return !reg.test(value);
    },

    // check if the text is a emailAddress
    CheckEmailValid: function (value) {
        // Check if string is a valid email address
        var reg = new RegExp('^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\\.]{1}[0-9a-zA-Z]+[\\.]?[0-9a-zA-Z]+$');
        return reg.test(value);
    },
    // Password
    CheckPasswordValid: function (value) {
        // Check if string is a valid email address
        var reg = new RegExp('^(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#+-\\./:_]).{1,47}$');
        return reg.test(value);
    },
    // Alphabet + Nunber + Symbol
    IsAlphabetOrNumberOrSymbol: function (value) {
        // Check if string is alphabet or number or symbol
        var reg = new RegExp(
            '\u005b\u005e\u0061\u002d\u007a\u0041\u002d\u005a\u0030\u002d\u0039\u0027\u0060\u0027\u007e\u0027\u0021\u0027\u0040\u0027\u0023\u0027\u0024\u0027\u0025\u0027\u005e\u0027\u0026\u0027\u002a\u0027\u0028\u0027\u0029\u0027\u005f\u0027\u002b\u0027\u003d\u0027\u007b\u0027\u007d\u0027\u007c\u0027\u003a\u0027\u0022\u0027\u003b\u0027\u0027\u0027\u003c\u0027\u003e\u0027\u003f\u0027\u002f\u0027\u002e\u0027\u002c\u005c\u002d\u005b\u005c\u005d\u005c\u005c\u005d',
        );
        return !reg.test(value);
    },
    // Get the total of types in array
    CheckNumberOfTypeInString: function (value) {
        var list = new Array();
        var c;
        for (var i = 0; i < value.length; i++) {
            c = value[i];
            for (var j = i + 1; j < value.length; j++) {
                if (value[j] == c) {
                    value = value.slice(0, j) + value.slice(j + 1, value.len);
                    j = j - 1;
                }
            }
            list[i] = c;
        }
        var count = list.length;
        return count;
    },
    // Special character: *
    IsCharacterSpecial: function (value) {
        for (var i = 0; i < value.length; i++) {
            if (value[i] == '*') return true;
        }
        return false;
    },

    // Symbol check for password
    IsPasswordSymbol: function (value) {
        var reg = new RegExp('\u005b\u005e\u0027\u0023\u0027\u002b\u005c\u002d\u0027\u002e\u0027\u002f\u0027\u003a\u0027\u005f\u005d');
        return !reg.test(value);
    },

    // text check for password(Alphabet Or Number Or Symbol)
    IsPasswordAlphabetOrNumberOrSymbol: function (value) {
        var reg = new RegExp('\u005b\u005e\u0061\u002d\u007a\u0041\u002d\u005a\u0030\u002d\u0039\u0027\u0023\u0027\u002b\u005c\u002d\u0027\u002e\u0027\u002f\u0027\u003a\u0027\u005f\u005d');
        return !reg.test(value);
    },

    // Check at consecutive characters
    HasSeqChar: function (str, num) {
        var count = 0;
        var prev = 0;

        for (var i = 0; i < str.length; i++) {
            var c = str.charAt(i);
            if (i > 0 && prev == c) {
                count++;
                if (count == num - 1) {
                    return true;
                }
            } else {
                count = 0;
            }
            prev = c;
        }

        return false;
    },

    // Check same characters in text
    ContainSameSeqChar: function (str1, str2, num) {
        if (str2.length < num || str1.length < num) {
            return false;
        }

        for (var i = 0; i <= str2.length - num; i++) {
            var target = str2.substring(i, i + num);
            if (str1.contains(target)) {
                return true;
            }
        }
        return false;
    },
};