Step toward a one-page application
[vchess.git] / public / javascripts / utils / misc.js
index 0b68ac4..287c0ea 100644 (file)
@@ -1,5 +1,5 @@
 // Source: https://www.quirksmode.org/js/cookies.html
-function setCookie(name,value)
+function setCookie(name, value)
 {
        var date = new Date();
        date.setTime(date.getTime()+(183*24*60*60*1000)); //6 months
@@ -7,7 +7,7 @@ function setCookie(name,value)
        document.cookie = name+"="+value+expires+"; path=/";
 }
 
-function getCookie(name) {
+function getCookie(name, defaut) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i=0;i < ca.length;i++)
@@ -18,7 +18,7 @@ function getCookie(name) {
                if (c.indexOf(nameEQ) == 0)
                        return c.substring(nameEQ.length,c.length);
        }
-       return null;
+       return defaut; //cookie not found
 }
 
 // Random (enough) string for socket and game IDs
@@ -27,3 +27,21 @@ function getRandString()
        return (Date.now().toString(36) + Math.random().toString(36).substr(2, 7))
                .toUpperCase();
 }
+
+// Used both on index and variant page, to switch language
+function setLanguage(e)
+{
+       setCookie("lang", e.target.value);
+       location.reload(); //to include the right .pug file
+}
+
+// Shortcut for an often used click (on a modal)
+function doClick(elemId)
+{
+       document.getElementById(elemId).click(); //or ".checked = true"
+}
+
+function translate(msg)
+{
+       return translations[msg];
+}