﻿/// Write cookie using javascript
/// ElementBase.JavascriptEnabled is based on the following cookie

// is cookie enabled? returnes true or false
cookieEnabled = function() {
    var cookieEnabled = false;
    try {
        cookieEnabled = (navigator.cookieEnabled) ? true : false;
    } catch (e) {
        document.cookie = "testcookie"
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    return cookieEnabled;
};

// returns value of the given cookie if it exists, otherwise returns null
readCookie = function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
};

writeCookie = function(key, value) {
    if (String.prototype.format)
        document.cookie = "{0}={1}; path=/;".format(key, value);
    else
        document.cookie = key + '=' + value + ';path=/;';
};

removeCookie = function(key) {
    writeCookie(key, '');
};

/* self executing function to set javascript cookie flag */
checkJavascript = function() {
    if (!cookieEnabled()) return; /* no cookie support */

    var cookie_name = 'js';
    var cookie_value = readCookie(cookie_name);
    if (cookie_value == null || cookie_value != '1') {
        // cookie not found, so set it and reload the page
        writeCookie(cookie_name, 1);
        window.location.reload();
    }
} ();
