//------------------------------------------------------------------------------------
// GA setup code to go on all pages you want to track

function GetCookie(sName) {
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0]) return unescape(aCrumb[1]);
    }
    return null;
}

function SetCookie(sName, sValue) {
    //document.cookie = sName + "=" + escape(sValue);
    // Expires the cookie in one month
    var date = new Date();
    date.setFullYear(date.getFullYear() + 2);
    document.cookie = sName + "=" + escape(sValue) + ";path=/;expires=" + date.toUTCString() + ";domain=" + window.domain;
    //document.cookie += ("; expires=" + date.toUTCString());
}

function compatAttach(evt, daFunction, obj) {
    (obj) || (obj = window);
    if (window.addEventListener) {
        obj.addEventListener(evt, daFunction, false);
    }
    else if (window.attachEvent) {
        obj.attachEvent("on" + evt, daFunction);
    }
}

//------------------------------------------------------------------------------------

function hcCookieSetup() {
    var ifrm = document.createElement('iframe');
    ifrm.style.height = '1px';
    ifrm.style.width = '1px';

    //--------------------------------------------------------------------
    //--- MASTER URL FOR THE CROSSDOMAIN COOKIE --------------------------
    var master = _gaMasterTrackingPage;
    //--------------------------------------------------------------------
    var callback = window.location.host + '/Style%20Library/Scripts/gAnalytics/hcxd.htm';
    ifrm.src = master + '?' + callback;
    document.hciframe = ifrm;

    document.getElementsByTagName('body')[0].appendChild(ifrm);
}

function hcCookieCB(hcxdom) {
    document.getElementsByTagName('body')[0].removeChild(document.hciframe);
    SetCookie('hcxdom', hcxdom);
}

//------------------------------------------------------------------------------------

function hcCookie(domain) {
    var hcxdom = GetCookie('hcxdom');
    window.domain = domain

    // if we have the x-domain cookie update the cookie, else run the script to pick up the cookie
    if (!hcxdom) {
        compatAttach('load', hcCookieSetup);
    }
    else {
        SetCookie('hcxdom', hcxdom);
    }
}

function localCookie(domain) {
    window.domain = domain
    var lc = GetCookie('hcxdom');

    if (!lc) {
        lc = Math.floor(Math.random() * 110000000000000000000) + '.' + new Date().valueOf();
    }
    SetCookie('hcxdom', lc);
}
