const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; function setCookie(name, value, expires) { const d = new Date(); if (!expires) { expires = 30 * 24 * 60 * 60; // 30 days from now } d.setTime(d.getTime() + (expires * 1000)); let expiresStr = "expires=" + d.toUTCString(); document.cookie = name + "=" + value + ";" + expiresStr + ";path=/"; } function getCookie(name) { // Encode the name to ensure it's handled correctly const nameEQ = name + "="; // Split the string into an array of individual cookie strings const ca = document.cookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; // Trim leading whitespace from the cookie string while (c.charAt(0) === ' ') { c = c.substring(1, c.length); } // If the cookie string starts with the name being searched if (c.indexOf(nameEQ) === 0) { // Return the value part of the string, which starts after the name and the equals sign return c.substring(nameEQ.length, c.length); } } return null; // Return null if the cookie name is not found } async function api(method, endPoint, params, auth) { const headers = new Headers(); if (!auth) { auth = gSession; // global session token } headers.append('Authorization', `Bearer ${auth}`); let str; let maybeBody; if (method == 'GET') { const urlParams = new URLSearchParams(params); str = urlParams.size > 0 ? ('&' + urlParams.toString()) : ''; maybeBody = {}; } else { str = ''; maybeBody = { body: JSON.stringify(params) }; } const response = await fetch('/api/?p=' + endPoint + str, { method: method, headers: headers, ...maybeBody }); if (!response.ok) { return false; } const data = await response.json(); if (data.success === false && (data.error_code === 201 || data.error_code === 202)) { // authentication error window.location = '/sign_in/'; } return data; } async function apiGet(endPoint, data, auth) { return await api('GET', endPoint, data, auth); } async function apiPost(endPoint, data, auth) { return await api('POST', endPoint, data, auth); } function displayMonetary(x) { x = x.toFixed(2).toString().replace('.', ','); var pattern = /(-?\d+)(\d{3})/; while (pattern.test(x)) x = x.replace(pattern, "$1 $2"); return x + ' DA'; } function txt(eng) { return eng; } function toCentsParts(value) { tValue = value; billions = millions = thousands = 0; if (tValue >= 10000000) { billions = Math.floor(tValue / 10000000); tValue = tValue - (billions * 10000000); } if (tValue >= 10000) { millions = Math.floor(tValue / 10000); tValue = tValue - (millions * 10000); } if (tValue >= 10) { thousands = Math.floor(tValue / 10); tValue = tValue - (thousands * 10); } cents = Math.floor(tValue * 100); return [billions, millions, thousands, cents]; } function displayCurrencyInWords(value) { text = ''; [billions, millions, thousands, cents] = toCentsParts(value); if (billions > 0) text = (billions === 1 ? 'billion' : billions + ' billions'); if (millions > 0) text += (text ? ', ' : '') + (millions === 1 ? 'million' : millions + ' millions'); if (thousands > 0) text += (text ? ', ' : '') + (thousands === 1 ? 'thousand' : thousands + ' thousands'); if (cents > 0) text += (text ? ', ' : '') + (cents === 1 ? 'cent' : cents + ' cents'); return text; } var autoCloseTimeout = null; function showMessage(text, autoClose, success) { var suc = 'bg-green-100 border-green-400 text-green-700'; var err = 'bg-red-100 border-red-400 text-red-700'; $('#message-box').removeClass(success ? err : suc).addClass(success ? suc : err); $('#message-box div.body').text(txt(text)); $('#message-box-ok').toggle(!autoClose); $('#message-box').show(); clearTimeout(autoCloseTimeout); if (autoClose) { autoCloseTimeout = setTimeout(function() { $('#message-box').hide(); }, 2000); } } function escapeRegExp(string) { // Matches all special regex characters and escapes them with a backslash. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } function displayDateTime(datetimeUTC, dateOnly) { var options; if (dateOnly) { options = { timeZone: userTimeZone, year: 'numeric', month: 'numeric', day: 'numeric' }; } else { options = { timeZone: userTimeZone, year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric' }; } datetimeUTC = datetimeUTC.replace(' ', '\T') + '\Z'; return (new Date(datetimeUTC)).toLocaleString(undefined, options); }