/*** UTILITIES ***/ // parent is optional function getById(id, parent) { return parent ? parent.querySelector("#" + id) : document.getElementById(id); } // Same as calling document.getElementById(id).value // parent is optional function valueFromId(id, parent) { return parent ? parent.querySelector("#" + id).value : getById(id).value; } // Same as calling document.getElementById(id).value // if value is blank, it returns "cbResetParam=1" // otherwise return param=value of id // parent is optional function paramFromId(param, id, parent) { var v = parent ? parent.querySelector("#" + id).value : getById(id).value; return v ? (param + "=" + v) : "cbResetParam=1" } // Get the form of specified DP key function appkeyToForm(key) { return document.querySelector('form[action^="' + caspioUrl + '/dp/' + key + '"]'); } function refreshDatapage(divId, appkey, params='') { var container = getById(divId); $(container).empty(); const newDataPage = document.createElement('script'); newDataPage.src = caspioUrl + '/dp/' + appkey + '/emb?' + params; container.appendChild(newDataPage); return false; } // Set an input field from virtual "calculated" field // Must wait till calculated field is ready function setValueFromVirtualField(virtualElem, targetElem) { if (virtualElem) { virtualElem.addEventListener("change", function() { targetElem.value = virtualElem.value; }, { once: true }); } } // Set an input field from virtual "calculated" field // Must wait till calculated field is ready function setHTMLFromVirtualField(virtualElem, targetElem) { if (virtualElem) { virtualElem.addEventListener("change", function() { targetElem.innerHTML = virtualElem.value; }, { once: true }); } } /*** END UTILITIES ***/ /*** DATES ***/ // Return now formatting suitable to feed the database // YYYY-MM-DD HH:MM:SS function nowForDatabase(timezone='America/Chicago') { var now = new Date(); var dt = now.toLocaleDateString('fr-CA', { timeZone: timezone }); var tm = now.toLocaleTimeString('it-IT', { timeZone: timezone }); return dt + ' ' + tm; } // Today's date in local timezone function todayLocal() { return dateLocal(new Date()); } // Convert the given date (mm/dd/yyyy) to local function dateLocal(date) { var tzOffset = date.getTimezoneOffset() * 60000; // in ms return new Date (date.getTime() + tzOffset); } // Convert the given date (mm/dd/yyyy) to UTC function dateUTC(date) { var tzOffset = date.getTimezoneOffset() * 60000; // in ms return new Date (date.getTime() - tzOffset); } // format date mm/dd/yyyy function dateMDY(date) { return date.toLocaleString("en-US", { day: "2-digit", month: "2-digit", year: "numeric" }); } // format date YYYY-MM-DD function dateYMD(date) { return date.toLocaleString("fr-CA", { day: "2-digit", month: "2-digit", year: "numeric" }); } // Convert caspio date in a query for display it in a form // mm/dd/yyyy hh:mm:ss function caspioDateForm(dateStr) { return new Date(dateStr).toLocaleString("en-US", { hour12: false }).replace(',', ''); } /*** END DATES ***/ /*** FUNCTIONS THAT INTERACTS WITH REST API ***/ const caspioUrl = 'https://c2acz083.caspio.com'; /*** END FUNCTIONS THAT INTERACTS WITH REST API ***/