// This script detects the user status as defined by the ITG Curtain cookie 
// or in the case of AOL users, client-side detect or PF Target cookie
// then displays the desired ad.

//Browser flags
var isW3C_CM, isIE4_CM, isIE_CM, isNN4_CM, isMac_CM;
var nu_CM = navigator.userAgent.toLowerCase();

isW3C_CM = (document.getElementById)?true:false;
isIE4_CM = (document.all)?true:false;
isIE_CM  = (isIE4_CM||(nu_CM.indexOf('msie')!=-1))?true:false;
isNN4_CM = (document.layers)?true:false;
isMac_CM = (nu_CM.indexOf("mac")!=-1)

// ad names
var RichMediaAd    = "EWRICHMED";   // Rich Media ad - Site minus Home Page
var RichMediaAdHP  = "EWHPRICHMED"; // Rich Media ad - Home Page Only
var defaultNSBAd   = "CMTOPRT610X60";
var defaultSUBAd   = "410X30SUB";

// function getCookie(name) returns the value for the cookie saved as 'name'
//                 or returns false if the cookie is not found
function getCookie(name){ 
	// Read the cookie property, returns all cookies for this document
	var cookies = document.cookie;
	
	// If the cookie exists, read it's value
	if (cookies.indexOf(name) != -1){
		var startpos = cookies.indexOf(name)+name.length+1;
		var endpos = cookies.indexOf(";",startpos);
		if (endpos == -1) endpos = cookies.length;
		return unescape(cookies.substring(startpos,endpos));
	} else { 
	  // the cookie couldn't be found! it was never set before, or it expired.	
		return false; 
	}
}

// function P_detect(sp) uses client-side detect(AOL client + browser only)
// or ITG's PF_TARGET cookie to determine service provider -
// returns true if service provider 'sp' is detected, false if not
// In this case P_detect(sp) is used to check for AOL service, 
//  but could target other PF_TARGET supported service providers.
function P_detect(sp){
	// client-side detect for AOL client
	var ua = navigator.userAgent.toLowerCase();
	if(sp=="AOL" && ua.indexOf("aol") != -1) return true; 
  
	// pfTarget Object stores possible PF_TARGET cookie values
	pfTarget=new Object();
	
	pfTarget.AOLbasic    = 1;
	pfTarget.AOLtv       = 64; 
	pfTarget.AOLatSchool = 512; 
	pfTarget.CSbasic     = 2; 
	pfTarget.CSgateway   = 4;
	pfTarget.NRB         = 16384; 
	
	pfTarget.AOL         = pfTarget.AOLbasic|pfTarget.AOLtv;
	pfTarget.CS          = pfTarget.CSbasic|pfTarget.CSgateway;
	pfTarget.AOL_CS      = pfTarget.AOL|pfTarget.CS;
	
	pft_c = getCookie("PF_TARGET");
	
	// return true if service provider 'sp' is detected, false if not
	if(pft_c&pfTarget[sp])return true;
	else return false;
}

// function userStatus() returns the status of the user
// Based on the ITG Curtain Cookie and PFTarget Cookie
function userStatus(){
	var user;
	
	ITG_CUR_c = getCookie("ITG_CUR");
	
	if (!ITG_CUR_c){
		if (P_detect("AOL")) user="AOL"; 
		else user = "DEFAULT";           
	}
	else user = ITG_CUR_c.match(/NEWSUB|NEWSSTAND|MAGICDN/i);
	
	if(user == null)user = "CURRENTSUB";
	
	return user;
}

// Constructor function for C_Ad Class
// is needed, this is avoided by using Custom Objects
function CM_Ad(pagepos, w, h){
	this.pagepos = pagepos;
	this.w      = w;
	this.h      = h;
}

// Build 'associative array' of CM_AdPlc objects based on user status 
CM_AdPlc = new Object();

CM_AdPlc[RichMediaAdHP] = new CM_Ad("cmhomepage", "515", "300"); 
CM_AdPlc[RichMediaAd]   = new CM_Ad("cmhomepage", "515", "300");
CM_AdPlc[defaultNSBAd]  = new CM_Ad("cmtopright", "510", "60");
CM_AdPlc[defaultSUBAd]  = new CM_Ad("cm_homepage_subs", "410", "60");

// function getAdName() returns an ad depending on the user status
function getAdName() {
	var loc   = window.location.href;
	var today = new Date;
	var user  = userStatus();
	
	if(user != "CURRENTSUB" && user != "NEWSUB" && user != "MAGICDN"  && user != "AOL"){
		// Return Rich Media ad if 5 day cookie has expired and user has
		// IE ver 5 or above and the Flash player ver 5 or above
		if(!getCookie("CM_Ad")&&(isW3C_CM && isIE_CM && (!isMac_CM) && hasReqestedVersion)){
			writeCMCookie();
			
			if(loc.search(/ew\.com\/ew(|\/|\/index.html|\/home.*)$/) != -1)
				return defaultNSBAd;  //return RichMediaAdHP;
			else
				return defaultNSBAd;  //return RichMediaAd;
		}
		else{
			return defaultNSBAd;
		}
	}
	return defaultSUBAd;
	// return false; //return false to remove ad
}

// function writeCMAD() calls getAdName() to determine the ad to be displayed
// then writes the appropriate ad tag 
function writeCMAD(){
	var finalAd = "";
	var thisAd = getAdName();
	
	if(thisAd){
		finalAd = getAd(thisAd);
		document.write(finalAd);
	}
}

// get Double-click script tag
function getAd(thisAd){
	return '<scr'+'ipt language="JavaScript1.1" SRC="http://adremote.timeinc.net/js.ng/site=ew&channel=cm&adsize='+CM_AdPlc[thisAd].w+'x'+CM_AdPlc[thisAd].h+'&pagepos='+CM_AdPlc[thisAd].pagepos+'"></scr'+'ipt>';
}

// Write 5 day cookie for Rich Media ad
function writeCMCookie(){
	var today  = new Date();
	var expire = new Date(today.setTime(today.getTime() + 5*24*60*60*1000));
	document.cookie = "CM_Ad=set;expires=" + expire.toGMTString();
	return true;
}
