/** * Most new visitors will enter our website using the homepage. While the * visitor stays on this page, we run JavaScript to get his/her timezone offset * (which is the best we can get directly from JavaScript).<br> * <br> * We use both summer and winter offsets to determine a timezone estimate for * the client's browser. When multiple time zones have the same summer and * winter offsets, we're using the most widely used - based on Google hits). We * then save this information in a cookie.<br> * <br> * By the time this visitor reaches an information page (i.e. a page containing * matches or other time-sensitive data) by using this route, we already have * enough information in order to present him/her with accurate time-sensitive * data (his/hers 'today matches', with kickoffs expressed in his/her timezone, * whether this timezone is named correctly or not - a Romanian visitor will see * 'Europe/Athens' instead of 'Europe/Bucharest', but 14:00 will mean 14:00).<br> * <br> * When a visitor reaches a time-sensitive page directly, without passing first * through the homepage, the default 'Europe/Athens' timezone is used. We then * run on these pages the same process as on the homepage and, if the obtained * timezone offset is different than the default one, we need to change the page * content (since the timezone offsets are different, the client may not find * what he/she is looking for, especially if the match fall in a different day * in his timezone). We then use a dialog box to inform the user about the * time-sensitive nature of our data and let him choose the desired timezone. We * also inform him about a possible page reload in case of the non-default * timezone.<br> */ var DEFAULT_TIMEZONE = 'Europe/Athens'; var DEFAULT_TIMEZONE_SUMMER_OFFSET = 180; var DEFAULT_TIMEZONE_WINTER_OFFSET = 120; var TIMEZONE_COOKIE_KEY = 'regionName'; var TIMEZONE_COOKIE_EXPIRY_PERIOD = 365; var TIMEOUT_BEFORE_TIMEZONE_CHECK = 10000; var TEXT1 = "Welcome, dear visitor!\n\nAs the information we provide on our website " + "is time-sensitive, we need to know your time zone.\n\n" + "Our guess is your time zone is '"; var TEXT2 = "'.\n\nIf this information is correct, please just click the OK button. " + "We will reload this page and use this information to find " + "the right games for your time zone.\n\n" + "If you want us to just use the default time zone, " + DEFAULT_TIMEZONE + ", just press the Cancel button.\n\n" + "Please note that you can change the time zone at anytime by selecting 'Time Zone' " + "from the menu above the score table.\n" + "You need to have the cookies activated in order to use this feature. Otherwise, " + "the default '" + DEFAULT_TIMEZONE + "' time zone will be used."; /** * This function represents the only "public" function of this module, and it is * meant to take care that the client will see meaningful data, which means data * formated in its time zone or time zone equivalent (same offset from GMT). */ function ensureCorrectTimezone() { debug("entering ensureCorrectTimezone()..."); if (checkCookiesEnabled() == false) { debug("Cookies are disabled, so there's little we can do... Just exit for now!"); return; } var timezoneName = getTimezoneCookie(); if (timezoneName != "") { debug("Timezone Cookie is already set!"); return; } debug("No cookie found!"); timezoneName = getClientTimezoneEstimatedName(); if (getClientSummerOffset() == DEFAULT_TIMEZONE_SUMMER_OFFSET && getClientWinterOffset() == DEFAULT_TIMEZONE_WINTER_OFFSET) { debug("Client has the same timezone as our default, so keep the default!"); setTimezoneCookie(timezoneName, TIMEZONE_COOKIE_EXPIRY_PERIOD); return; } debug("Default time zone does not match! Presenting the options:"); var userChoice = confirm(TEXT1 + timezoneName + TEXT2); if (userChoice == true) { debug("User agreed to use our timezone estimate!"); setTimezoneCookie(timezoneName, TIMEZONE_COOKIE_EXPIRY_PERIOD); window.location.reload(true); } else { debug("User wants to stick with the default timezone!"); setTimezoneCookie(DEFAULT_TIMEZONE, TIMEZONE_COOKIE_EXPIRY_PERIOD); } } /** * Checks to see if cookies are enabled on the client's browser * * @return true if cookies are enabled, false otherwise */ function checkCookiesEnabled() { var key = 'test'; var path = '/'; setCookie(key, 'enabled', 1, path); if (getCookie(key)) { deleteCookie(key); return true; } else { return false; } } /** * Set a cookie with a given key to a given value and to expire after a given * number of days, for a specified path. * * @param key * cookie's key * @param value * cookie's value * @param expireDays * validity period for this cookie (in days) * @param path * cookie's path */ function setCookie(key, value, expireDays, path) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expireDays); document.cookie = key + "=" + escape(value) + "; path=" + path + ((expireDays == null) ? "" : "; expires = " + exdate.toGMTString()); } /** * Find and return the value for a cookie with the given key, if present. * * @return the value for the cookie, if found, or the empty string ("") if not * found. */ function getCookie(key) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(key + "="); if (c_start != -1) { c_start = c_start + key.length + 1; c_end = document.cookie.indexOf(";", c_start); if (c_end == -1) { c_end = document.cookie.length; } return unescape(document.cookie.substring(c_start, c_end)); } } return ""; } /** * Deletes the given cookie by setting its expiry date. * * @param key * the key * @param path * the path for the cookie (optional) * @return true if the cookie was deleted, false if it was not found */ function deleteCookie(key, path) { if (getCookie(key) != "") { var now = new Date(); now.setTime(now.getTime() + 10 * 1000); if (typeof path == 'undefined' || path == null) { path = '/'; } var newCookieValue = key + "=deleted; path=" + path + "; expires=" + now.toGMTString(); document.cookie = newCookieValue; debug("Deleted '" + key + "' cookie: " + newCookieValue); return true; } return false; } /** * Set timezone cookie to a given value and to expire after a specific number of * days. The cookie will be set for the root path ("/"). * * @param value * cookie's value * @param expireDays * validity period for this cookie (in days) */ function setTimezoneCookie(value, expireDays) { setCookie(TIMEZONE_COOKIE_KEY, value, expireDays, "/"); } /** * Find and return the value for the timezone cookie, if present. * * @return the value for the timezone cookie, if found, or the empty string ("") * if not found */ function getTimezoneCookie() { return getCookie(TIMEZONE_COOKIE_KEY); } /** * * @return The offset (in minutes) for the client browser's timezone during * Summer */ function getClientSummerOffset() { var tmSummer = new Date(Date.UTC(2010, 6, 30, 0, 0, 0, 0)); var so = -1 * tmSummer.getTimezoneOffset(); return so; } /** * @return The offset (in minutes) for the client browser's timezone during * Winter */ function getClientWinterOffset() { var tmWinter = new Date(Date.UTC(2000, 12, 30, 0, 0, 0, 0)); var wo = -1 * tmWinter.getTimezoneOffset(); return wo; } /** * This function uses both summer and winter offsets to determine a time zone * estimate for the client's browser. When multiple time zones had the same * summer and winter offsets, the most widely used one was chosen (based on * Google hits). * * The code has been adapted from the article: * "http://blog.redfin.com/devblog/2007/08/getting_the_time_zone_from_a_web_browser.html". * * @return the name of the chosen timezone */ function getClientTimezoneEstimatedName() { var so = getClientSummerOffset(); var wo = getClientWinterOffset(); if (-660 == so && -660 == wo) return 'Pacific/Midway'; if (-600 == so && -600 == wo) return 'Pacific/Tahiti'; if (-570 == so && -570 == wo) return 'Pacific/Marquesas'; if (-540 == so && -600 == wo) return 'America/Adak'; if (-540 == so && -540 == wo) return 'Pacific/Gambier'; if (-480 == so && -540 == wo) return 'US/Alaska'; if (-480 == so && -480 == wo) return 'Pacific/Pitcairn'; if (-420 == so && -480 == wo) return 'US/Pacific'; if (-420 == so && -420 == wo) return 'US/Arizona'; if (-360 == so && -420 == wo) return 'US/Mountain'; if (-360 == so && -360 == wo) return 'America/Guatemala'; if (-360 == so && -300 == wo) return 'Pacific/Easter'; if (-300 == so && -360 == wo) return 'US/Central'; if (-300 == so && -300 == wo) return 'America/Bogota'; if (-240 == so && -300 == wo) return 'US/Eastern'; if (-240 == so && -240 == wo) return 'America/Caracas'; if (-240 == so && -180 == wo) return 'America/Santiago'; if (-180 == so && -240 == wo) return 'Canada/Atlantic'; if (-180 == so && -180 == wo) return 'America/Montevideo'; if (-180 == so && -120 == wo) return 'America/Sao_Paulo'; if (-150 == so && -210 == wo) return 'America/St_Johns'; if (-120 == so && -180 == wo) return 'America/Godthab'; if (-120 == so && -120 == wo) return 'America/Noronha'; if (-60 == so && -60 == wo) return 'Atlantic/Cape_Verde'; if (0 == so && -60 == wo) return 'Atlantic/Azores'; if (0 == so && 0 == wo) return 'Africa/Casablanca'; if (60 == so && 0 == wo) return 'Europe/London'; if (60 == so && 60 == wo) return 'Africa/Algiers'; if (60 == so && 120 == wo) return 'Africa/Windhoek'; if (120 == so && 60 == wo) return 'Europe/Amsterdam'; if (120 == so && 120 == wo) return 'Africa/Harare'; if (180 == so && 120 == wo) return 'Europe/Athens'; if (180 == so && 180 == wo) return 'Africa/Nairobi'; if (240 == so && 180 == wo) return 'Europe/Moscow'; if (240 == so && 240 == wo) return 'Asia/Dubai'; if (270 == so && 210 == wo) return 'Asia/Tehran'; if (270 == so && 270 == wo) return 'Asia/Kabul'; if (300 == so && 240 == wo) return 'Asia/Baku'; if (300 == so && 300 == wo) return 'Asia/Karachi'; if (330 == so && 330 == wo) return 'Asia/Calcutta'; if (345 == so && 345 == wo) return 'Asia/Katmandu'; if (360 == so && 300 == wo) return 'Asia/Yekaterinburg'; if (360 == so && 360 == wo) return 'Asia/Colombo'; if (390 == so && 390 == wo) return 'Asia/Rangoon'; if (420 == so && 360 == wo) return 'Asia/Almaty'; if (420 == so && 420 == wo) return 'Asia/Bangkok'; if (480 == so && 420 == wo) return 'Asia/Krasnoyarsk'; if (480 == so && 480 == wo) return 'Australia/Perth'; if (540 == so && 480 == wo) return 'Asia/Irkutsk'; if (540 == so && 540 == wo) return 'Asia/Tokyo'; if (570 == so && 570 == wo) return 'Australia/Darwin'; if (570 == so && 630 == wo) return 'Australia/Adelaide'; if (600 == so && 540 == wo) return 'Asia/Yakutsk'; if (600 == so && 600 == wo) return 'Australia/Brisbane'; if (600 == so && 660 == wo) return 'Australia/Sydney'; if (630 == so && 660 == wo) return 'Australia/Lord_Howe'; if (660 == so && 600 == wo) return 'Asia/Vladivostok'; if (660 == so && 660 == wo) return 'Pacific/Guadalcanal'; if (690 == so && 690 == wo) return 'Pacific/Norfolk'; if (720 == so && 660 == wo) return 'Asia/Magadan'; if (720 == so && 720 == wo) return 'Pacific/Fiji'; if (720 == so && 780 == wo) return 'Pacific/Auckland'; if (765 == so && 825 == wo) return 'Pacific/Chatham'; if (780 == so && 780 == wo) return 'Pacific/Enderbury'; if (840 == so && 840 == wo) return 'Pacific/Kiritimati'; return DEFAULT_TIMEZONE; }
