﻿"use strict";

/*global document, INTEL, jQuery, rlTranslations,
addEventListener: false, blur: false, clearInterval: false, clearTimeout: false, 
close: false, closed: false, defaultStatus: false, document: false, event: false, 
focus: false, frames: false, getComputedStyle: false, history: false, Image: false,
length: false, location: false, moveBy: false, moveTo: false, name: false, navigator: false, 
onblur: true, onerror: true, onfocus: true, onload: true, onresize: true, onunload: true, 
open: false, opener: false, Option: false, parent: false, print: false, resizeBy: false, 
resizeTo: false, screen: false, scroll: false, scrollBy: false, scrollTo: false, 
setInterval: false, setTimeout: false, status: false, top: false, XMLHttpRequest: false,
window, JSON, escape, unescape
*/

var ItcCookiesConfig = {
    firstVisit: "itc1t",
    expirationDays: 30,
    fieldSeparator: "&"
};

function CookieSerializer() {
}

CookieSerializer.prototype =
{
    jsonSerialize: function (obj) {
        return JSON.stringify(obj);
    },

    jsonDeserialize: function (cookie) {
        var value = cookie.read();
        if (value === null || value.length === 0) {
            throw "CookieNotFoundException";
        }
        return JSON.parse(value);
    }
};

function Cookie(name, accessPath) {
    var i, j;
    this.name = name;
    this.fieldSeparator = ItcCookiesConfig.fieldSeparator;
    this.expires = ItcCookiesConfig.expirationDays;
    this.accessPath = accessPath;
    this.rawValue = null;
    this.fields = [];
    this.fieldnames = [];

    if (arguments.length > 2) {
        j = 0;
        for (i = 2; i < arguments.length; i += 1) {
            this.fieldnames[j] = arguments[i];
            j += 1;
        }
    }
}

Cookie.prototype = {
    getFieldValue: function (fieldname) {
        var i = this.namepos(fieldname);
        if (i >= 0) {
            return this.fields[i];
        } else {
            throw "FieldNameNotFoundException";
        }
    },

    setFieldValue: function (fieldname, fieldval) {
        var i = this.namepos(fieldname);
        if (i > -1) {
            this.fields[i] = fieldval;
        }
    },

    getFieldIndex: function (fieldname) {
        for (var i = 0; i < this.fieldnames.length; i += 1) {
            if (fieldname === this.fieldnames[i]) {
                return i;
            }
        }
        return -1;
    },

    toCookieString: function () {
        var cookietext, today, expiredate, i;
        cookietext = this.name + "=";

        if (this.fields.length === 0) {
            cookietext += this.rawValue;
        }
        else if (this.fields.length === 1) {
            cookietext += escape(this.fields[0]);
        } else {
            for (i in this.fields) {
                if (this.fields.hasOwnProperty(i)) {
                    cookietext += escape(this.fields[i]) + ItcCookiesConfig.fieldSeparator;
                }
            }
        }

        if (this.expires !== null) {
            if (typeof (this.expires) === "number") {
                today = new Date();
                expiredate = new Date();
                expiredate.setDate(today.getDate() + this.expires * 24 * 60 * 60 * 1000);
                cookietext += "; expires=" + this.getExtendedExpirationDate().toUTCString();
            } else {
                cookietext += "; expires=" + this.expires.toUTCString();
            }
        }

        if (this.accessPath) {
            cookietext += "; PATH=" + this.accessPath;
        }
        return cookietext;
    },

    save: function () {
        document.cookie = this.toCookieString();
    },

    read: function () {
        var search, CookieString, offset, end, sl, startidx, endidx, i;
        search = this.name + "=";
        CookieString = document.cookie;

        if (CookieString.length > 0) {
            offset = CookieString.indexOf(search);
            if (offset !== -1) {
                offset += search.length;
                end = CookieString.indexOf(";", offset);
                if (end === -1) {  // cookie is last item in the string, so no terminator                       
                    end = CookieString.length;
                }
                this.rawValue = CookieString.substring(offset, end);
            }
        }

        if (this.rawValue !== null) {
            sl = this.rawValue.length;
            startidx = 0;
            endidx = 0;
            i = 0;

            // Special case - single-field cookies written by other functions,
            // so without a '#' terminator

            if (this.rawValue.substr(sl - 1, 1) !== ItcCookiesConfig.fieldSeparator) {
                this.fields[0] = unescape(this.rawValue);
            } else {
                do {
                    endidx = this.rawValue.indexOf(this.fieldSeparator, startidx);
                    if (endidx !== -1) {
                        this.fields[i] = unescape(this.rawValue.substring(startidx, endidx));
                        i += 1;
                        startidx = endidx + 1;
                    }
                }
                while (endidx !== -1 && endidx !== (this.rawValue.length - 1));
            }
        }

        return this.rawValue;
    },

    remove: function () {
        this.expires = -10;
        this.save();
    },

    getExtendedExpirationDate: function () {
        var today, expirationPeriod;
        today = new Date();
        expirationPeriod = ItcCookiesConfig.expirationDays * 24 * 60 * 60 * 1000; // days * hr * min * sec * millisec
        return new Date(today.getTime() + expirationPeriod);
    }
};

var ItcCookies = (function () {
    return function () { };
}());

ItcCookies.extendCookie = function (cookie) {
    cookie.expiry = cookie.getExtendedExpirationDate();
};

ItcCookies.setOnetimeCookie = function () {
    var cookie = new Cookie(ItcCookiesConfig.firstVisit);
    cookie.rawValue = 'seen';
    cookie.save();
};

ItcCookies.renewCookies = function () {
    // to be called each time your page loads
    var oneTime = new Cookie(ItcCookiesConfig.firstVisit);
    oneTime.save();
};

ItcCookies.hasShownNagScreen = function () {
    var cookie = new Cookie(ItcCookiesConfig.firstVisit);
    return cookie.read() !== null && cookie.read() === 'seen';
};
