var OFFER = { offerId: 66, testRun: false, sampType: 'UNIQUEUSERS', eventType: 'LAYER', cookieDur: 8, offerRate: 0.01, offerShown: 0, offerOk: 0, offerCookieExpTime: null, offerCookieDomain: null, offerUrl: 'http://offers-service.cbsinteractive.com/offers/message.sc' + '?', offerCookieVal: -1, // defaults to -1 "was sampled" offerCookieName: 'u_offer_66', anonymousCookieName: 'XCLGFbrowser', linkFilter: ''.split('***'), onloadHandler: null, // code for working with cookies getCookie: function(n) { var cn = n + '='; var dc = document.cookie; if (dc.length > 0) { var bg = dc.indexOf(cn); if (bg != -1) { bg += cn.length; var nd = dc.indexOf(';', bg); if (nd == -1) { nd = dc.length; } return unescape(dc.substring(bg, nd)); } } return null; }, // creates/updates a cookie on the client machine storeCookie: function(name, val, exp, path, domain, secure) { document.cookie = name + '=' + escape(val) + ((exp==null) ? '' : '; expires=' + exp.toGMTString()) + ((path==null) ? '' : '; path=' + path) + ((domain==null) ? '' : '; domain=' + domain) + ((secure==null) ? '' : '; secure'); }, // appends a value to the given cookie name appendToCookie: function(name, val, exp, path, domain, secure) { var curVal = OFFER.getCookie(name); if (curVal == null) { OFFER.storeCookie(name, val, exp, path, domain, secure); } else { OFFER.storeCookie(name, (curVal + val), exp, path, domain, secure); } }, // checks the offer cookie to determine if the supplied id is in the cookie checkOfferCookie: function(offerId) { var cookieVal = OFFER.getCookie(OFFER.offerCookieName); var idxVal = cookieVal.indexOf('(' + offerId + ')', 0); return idxVal != -1; }, // handles showing the offer in a popup window if all is ok with the offer and offer cookie showOfferPopup: function() { if (!OFFER.offerOk || OFFER.offerShown || (OFFER.getCookie(OFFER.offerCookieName) == OFFER.offerCookieVal) || (OFFER.getCookie(OFFER.offerCookieName) == '-2')) { return; } var acId = escape(OFFER.getCookie(OFFER.anonymousCookieName)); OFFER.storeCookie(OFFER.offerCookieName, OFFER.offerCookieVal, OFFER.offerCookieExpTime, '/', OFFER.offerCookieDomain, null); if (OFFER.getCookie(OFFER.offerCookieName) == OFFER.offerCookieVal) { OFFER.offerShown = 1; var url = OFFER.offerUrl + 'accId=' + acId + '&offerId=' + OFFER.offerId + '&loc=' + escape(document.location); OFFER.openWindow(url, acId); } }, // opens a new pop-up window openWindow: function(url) { var type = typeof document.sDat; var newWindow = null; var windowProps = 'position=TOPLEFT,resizable=1,scrollbars=1,width=780,height=715'; var popupPos = 'TOPLEFT'; var popupWidth = '780'; var popupHeight = '715'; if (popupPos == 'DEFAULT') { } else if (popupPos == 'TOPLEFT') { windowProps += ',left=0,top=0,screenX=0,screenY=0'; } else if (screen.availHeight != 'undefined' && screen.availWidth != 'undefined') { var ax, ay; if( popupPos == 'TOPRIGHT') { ax = screen.availWidth - popupWidth; ay = 0; } else if (popupPos == 'BOTTOMLEFT') { ax = 0; ay = screen.availHeight - popupHeight; } else if (popupPos == 'BOTTOMRIGHT') { ax = screen.availWidth - popupWidth; ay = screen.availHeight - popupHeight; } else { ax = (screen.availWidth - popupWidth) / 2; ay = (screen.availHeight - popupHeight) / 2; } windowProps += ',left=' + ax + ',top=' + ay + ',screenX=' + ax + ',screenY=' + ay; } if (type == 'undefined') { newWindow = window.open(url, 'SURVEY', windowProps); } else { newWindow = window.open(url + '&dat=' + escape(document.sDat), 'SURVEY', windowProps); } newWindow.focus(); }, fetchXML: function(url, handler) { var scriptTag = document.createElement('script'); scriptTag.type = 'text/javascript'; scriptTag.src = url; document.body.appendChild(scriptTag); }, // This function checks for the All Access ad (which, // apparently, consistantly sets the "zflag_page_header" // variable"). If found, the function executes anything loaded // into the window.onload variable. Otherwise, it calls an // Ajax object and sets the survey in motion, then runs the // window.onload function. loadLayer: function() { var allAccess = false; try { if (zflag_page_header) { allAccess = true; } } catch(e) {} var acId = -1; if (!allAccess) { acId = escape(OFFER.getCookie(OFFER.anonymousCookieName)); } var urlX = OFFER.offerUrl + 'accId=' + acId + '&offerId=' + OFFER.offerId + '&loc=' + escape(document.location); OFFER.fetchXML(urlX, OFFER.populateMessage); if (typeof(OFFER.onloadHandler) != 'undefined' && OFFER.onloadHandler != null) { OFFER.onloadHandler(); } }, // This function takes the survey XML and acts on it. populateMessage: function(roXML) { var offerMsgXml = roXML.responseXML; var msgDivStyle = document.createElement("style"); msgDivStyle.setAttribute("type", "text/css"); var divStyleProps = '#offerMsg_66 { '; var layerPos = 'TOPLEFT'; if (layerPos == 'TOPLEFT') { divStyleProps = divStyleProps + 'top: 100px; left: 100px; '; } else if (layerPos == 'TOPRIGHT') { divStyleProps = divStyleProps + 'top: 100px; right: 100px; '; } else if (layerPos == 'BOTTOMRIGHT') { divStyleProps = divStyleProps + 'bottom: 100px; left: 100px; '; } else { divStyleProps = divStyleProps + 'bottom: 100px; left: 100px; '; } divStyleProps = divStyleProps + 'color: #000; '; divStyleProps = divStyleProps + 'background-color: #' + 'ffffff; '; divStyleProps = divStyleProps + 'border-width: 1' + 'px; '; divStyleProps = divStyleProps + 'border-color: #' + '000000; '; divStyleProps = divStyleProps + 'border-style: solid; '; divStyleProps = divStyleProps + 'position: absolute; '; divStyleProps = divStyleProps + 'width: 428' + 'px; '; divStyleProps = divStyleProps + 'height: 450' + 'px; '; divStyleProps = divStyleProps + 'padding: 20px; '; divStyleProps = divStyleProps + 'z-index: 10000; '; divStyleProps = divStyleProps + '}'; if (msgDivStyle.styleSheet) { // for IE msgDivStyle.styleSheet.cssText = divStyleProps; } else { // for all others msgDivStyle.appendChild(document.createTextNode(divStyleProps)); } document.body.appendChild(msgDivStyle); var msgDiv = document.createElement("div"); msgDiv.id = "offerMsg_66"; msgDiv.innerHTML = offerMsgXml.getElementsByTagName('offerMessage')[0].firstChild.data; document.body.appendChild(msgDiv); }, acceptOffer: function(url, messageId) { if (!OFFER.testRun) { // create hidden iframe to track the acceptance of the offer var acceptFrame = document.createElement("iframe"); acceptFrame.id = "offerAccept_66"; acceptFrame.style.display = "none"; acceptFrame.src = "http://offers-service.cbsinteractive.com/offers/acceptOffer.sc" + "?offerId=66" + "&messageId=" + messageId; document.body.appendChild(acceptFrame); } // Get the anonymous cookie id var acId = escape(OFFER.getCookie(OFFER.anonymousCookieName)); // Formulate the offer page URL and go to it url = url + 'accId=' + acId + '&offerId=' + OFFER.offerId + '&loc=' + escape(document.location); OFFER.openWindow(url); OFFER.closeOffer(); }, closeOffer: function() { OFFER.storeCookie(OFFER.offerCookieName, OFFER.offerCookieVal, OFFER.offerCookieExpTime, '/', OFFER.offerCookieDomain, null); if (OFFER.eventType == 'LAYER') { try { document.body.removeChild(document.getElementById("offerMsg_66")); } catch (e) {} } }, // END LAYER function // function to determine the domain for the offer cookie from the current hostname getDomain: function() { var host = window.location.host; var dotCnt = 0; var domainStartIdx = 0; var domainEndIdx = host.lastIndexOf(':'); if (domainEndIdx == -1) { domainEndIdx = host.length; } for (var i = 0; i < host.length; i++) { if (host.charAt(i) == '.') { dotCnt++; } } if (dotCnt != 3) { domainStartIdx = host.lastIndexOf('.'); if (domainStartIdx > 0) { domainStartIdx = host.lastIndexOf('.', (domainStartIdx - 1)); } if (domainStartIdx == -1) { domainStartIdx = 0; } } else { domainStartIdx = host.indexOf('.', 0); } return host.substring(domainStartIdx, domainEndIdx); }, // sampling script to determine if offer will be served sampleUser: function() { var expDate = new Date(); var expTimeMs = OFFER.cookieDur * 24 * 60 * 60 * 1000; expDate.setTime(expDate.getTime() + expTimeMs); OFFER.offerCookieExpTime = expDate; OFFER.offerCookieDomain = OFFER.getDomain(); if ((OFFER.getCookie(OFFER.offerCookieName) != OFFER.offerCookieVal) && (OFFER.getCookie(OFFER.offerCookieName) != '-2')) { var randomNum = Math.random() * 100; if (OFFER.sampType == 'UNIQUEUSER' && OFFER.checkOfferCookie(OFFER.offerId)) { OFFER.offerOk = 0; } else if (randomNum < OFFER.offerRate) { OFFER.offerOk = 1; } else { if (OFFER.sampType == 'UNIQUEUSER') { OFFER.appendToCookie(OFFER.offerCookieName, '(' + OFFER.offerId + ')', OFFER.offerCookieExpTime, '/', OFFER.offerCookieDomain, null); } OFFER.offerOk = 0; } } else { OFFER.offerOk = 0; } if (OFFER.offerOk == 1) { if (OFFER.eventType == 'PGLOAD') { OFFER.showOfferPopup(); } else if (OFFER.eventType == 'LINKCLICK') { OFFER.onloadHandler = window.onload; window.onload = OFFER.processLinks; } else if (OFFER.eventType == 'LAYER') { OFFER.onloadHandler = window.onload; window.onload = OFFER.loadLayer; } else { OFFER.onloadHandler = window.onunload; window.onunload = OFFER.handlePageUnload; } } } }; // End var OFFER = OFFER.sampleUser();