﻿function GetApiToken() {
    // YRI: This method doesn't store new cookies, it only accesses an authentication cookie. GDPR allows both use cases.

    var name = 'CWApiToken=';
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function GetParameterValues(param) {
    let reg = new RegExp("[?&]" + param + "=([^&#]*)", "i")
    let queryString = reg.exec(window.location.href)
    return queryString ? queryString[1] : null
}

function GetParameterByName(name, url) {
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

function SetCookie(cookieName, cookieValue, cookieExpiryDate) {
    if (IsCookieAccepted()) {
        if (cookieExpiryDate) {
            $.cookie(cookieName, cookieValue, { expires: cookieExpiryDate, path: '/' });
        }
        else {
            $.cookie(cookieName, cookieValue, { path: '/' });
        }
    }
}

function GetCookie(cookieName) {
    // YRI: This method doesn't store new cookies, it only accesses existing cookies. GDPR allows this.
    return $.cookie(cookieName);
}

function ShowModalOnLoad(id) {
    $('#' + id).appendTo("body").modal('show');
}

function CloseModal(id) {
    $('#' + id).modal('hide');
}

function GetCookieConsentType() {
    return GetCookie('cookieConsent')
}

function SetCookieConsentType(type) {
    // YRI: This is a special case, we can't use the `SetCookie()` helper as it depends on the `cookieConsent` cookie already existing.
    $.cookie('cookieConsent', type, { path: '/' });
}

function IsCookieAccepted() {
    return "all" == GetCookie('cookieConsent');
}