window.validateEmail = function(email) {
    var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
    return re.test(String(email).toLowerCase());
};

window.validatePhone = function(phone) {
    var re = /^[\+\d]{0,2}[\d\(\)\s\-]{10,16}\d$/;
    return re.test(String(phone).toLowerCase());
};

window.dateFormat = function(date, format = 'Ymd') {
    if (format == 'Ymd') {
        return date.getFullYear()
            + ('0' + (date.getMonth()+1)).slice(-2)
            + ('0' + date.getDate()).slice(-2);
    } else if (format == 'd-m-Y') {
        return ('0' + date.getDate()).slice(-2) 
            + '-' + ('0' + (date.getMonth()+1)).slice(-2)
            + '-' + date.getFullYear();
    } else if (format == 'Y-m-d') {
        return date.getFullYear()
            + '-' + ('0' + (date.getMonth()+1)).slice(-2)
            + '-' + ('0' + date.getDate()).slice(-2);
    }  else if (format == 'd.m.Y') {
        return ('0' + date.getDate()).slice(-2) 
            + '.' + ('0' + (date.getMonth()+1)).slice(-2)
            + '.' + date.getFullYear();
    }
};

window.getWeekDay = function(date) {
    let days = [
        'Воскресенье', 
        'Понедельник', 
        'Вторник', 
        'Среда', 
        'Четверг', 
        'Пятница', 
        'Суббота'
    ];

    return days[date.getDay()];
}

window.newDate = function(date) {
    const regex = /([0-9]{4})([0-9]{2})([0-9]{2})/gm;
    let m;

    while ((m = regex.exec(date)) !== null) {
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        var dateArray = {'year': null, 'month': null, 'day': null};

        m.forEach((match, groupIndex) => {
            if (groupIndex == 1) {
                dateArray.year = match;
            } else if (groupIndex == 2) {
                dateArray.month = match;
            } else if (groupIndex == 3) {
                dateArray.day = match;
            }
        });

        return new Date(dateArray.year + '-' + dateArray.month + '-' + dateArray.day);
    }
};
