// the following is the main script, to be placed in the includes.  If it is divided into three segments as listed below, it will be more flexible in the future, but it is not necessary

// section 1: initialize
if(!document.cookie) { 
	document.cookie = "PopUpCalls=0;path=/;";
}
if ((document.layers || document.all) && document.cookie) { 
	var P_showPop=true;
} else {
	var P_showPop=false; 
}

if (P_showPop) {
	P_Rules = new Array();

// section 2: magazine rules

	P_Mag="SIMagLvng";
	P_Rules[0] = "~sportsillustrated.cnn.com"; // rules array contains definitions of internal links (links that will not get a popup).  "~" means contains and "!" means does not contain.  Rules can be combined with logical "and" using "&&": ~time.com/&&!/digital/ is one way of combining these tools.
	P_Rules[1] = "~cnnsi.com"; // each rule in the rule array is a logical "or"
	P_Rules[2] = "~fenway";
	P_Rules[3] = "~uptown";
	P_Rules[4] = "~si.cnn.com";
	P_Rules[5] = "~cgi3.cnnsi.com";
	P_Rules[6] = "~adman.turner.com";
	P_Rules[7] = "~magsubs/cc/sub";
	P_Rules[8] = "~secure.customersvc.com";
	P_Rules[9] = "~ar.atwola.com";

	P_Hours = 48; // number of hours till next popup
	P_Source = "http://cgi.timeinc.net/cgi-bin/magsubs/cc/subscribe/sipop?EFFORTKEY=1"; // URL of popup
	P_Height = 320; // height of popup window
	P_Width = 320; // width of popup window
	P_cookieDomain = false; // if you want to specify a domain for this cookie, add it here, e.g. ".pathfinder.com";

// section 3: program. minimal comments follow

	P_today = new Date();
	P_todayNum = P_today.valueOf();
	var P_LeftChip="";
	var P_RightChip="";
	var P_Chip="";
	var P_DocCookie = unescape(document.cookie);
	if(P_DocCookie.length>0) {
		P_begin=P_DocCookie.indexOf("PopUpCalls=");
		if(P_begin != -1) {
			P_begin += 11; // 11 is length of string "PopUpCalls="
			P_end=P_DocCookie.indexOf(";", P_begin);
			if (P_end==-1) P_end=P_DocCookie.length;
			P_Cookie=P_DocCookie.substring(P_begin, P_end);
			P_ChipStart=P_Cookie.indexOf(P_Mag+"=");
			if(P_ChipStart>-1) {
				P_LeftChip=P_Cookie.substring(0,P_ChipStart);
				P_ChipStart+=1+P_Mag.length
				P_ChipEnd=P_Cookie.indexOf("|",P_ChipStart);
				if(P_ChipEnd==-1) P_ChipEnd = P_Cookie.length;
				P_Chip=P_Cookie.substring(P_ChipStart,P_ChipEnd);
				P_RightChip=P_Cookie.substring(P_ChipEnd);
				if(P_todayNum < parseInt(P_Chip)) P_showPop = false;
			} else {
				P_LeftChip=P_Cookie;
				P_RightChip="";
			}
		}
	}
}
function P_checkClick(ev) { // onClick function
	if(P_showPop) {
		P_Link="";
		if(document.layers) {
                        P_Test=ev.target.toString().toLowerCase();

                        if(P_Test.substring(0,6)=="<input") {
                                P_showPop = false;
                        } else {
                                P_Link=P_Test;
                        }
                } else if (document.all) {
						P_myElement = window.event.srcElement;
						while(P_myElement.tagName.toLowerCase()!="a" &&
						  P_myElement.tagName.toLowerCase()!="body" &&
						  P_myElement.tagName.toLowerCase()!="input" &&
						  P_myElement.tagName.toLowerCase()!="area" ) {
							P_myElement = P_myElement.parentElement;
	  					}
                        P_tag = P_myElement.tagName.toLowerCase();
                        if (P_tag=="a" || P_tag=="area") {
                                P_Link=P_myElement.href;
                        } else if(P_tag=="input") {
                                P_showPop=false;
                        }

                }
		P_Link=P_Link.toLowerCase();

        var P_ToReplace = /\x2F/g;
        P_URL = self.location.pathname;
        P_URL = P_URL.replace(P_ToReplace, "\[\/\]"); 
        var P_ToMatch = new RegExp(P_URL + "[/]{0,1}(index.|default.){0,1}[^.]*#", "i");
		var P_RV = P_Link.search(P_ToMatch);

		if(P_Link!="" && (P_RV==-1) && (P_Link.substring(0,4)=="http")) {
			if(P_isInternal(P_Link)) {
				P_showPop=false;
			}
		}
		if(document.layers) {
			routeEvent(ev);
			return true;
		}
	}
}
function P_isInternal(P_URL) { // function to evaluate whether a URL is internal (should not get a popup)
	for (i=0;i < P_Rules.length;i++) {
		P_Parts = P_Rules[i].split("&&");
		P_Result = true;
		for (j=0;j < P_Parts.length;j++) {
			P_Search = P_URL.indexOf(P_Parts[j].substring(1));
			P_Call = P_Parts[j].charAt(0);
			if ((P_Search==-1 && P_Call=="~") || (P_Search>-1 && P_Call=="!")) {
				P_Result = false;
				break;
			}
		}
		if(P_Result) return true; 
	}
	return false; 
}
function P_closeDown() { // onunload function
	if(P_showPop) { 
		popupWin = window.open(P_Source, "POPUP", "height=" + P_Height + ",width=" + P_Width + ",scrollbars=yes"); 
		P_Chip = P_todayNum + (P_Hours * 60 * 60 * 1000);
		P_today.setTime(P_today.getTime() + (31 * 24 * 60 * 60 * 1000));
		document.cookie = "PopUpCalls=" + escape(P_LeftChip + P_Mag + "=" + P_Chip + (P_RightChip.charAt(0)=="|"?"":"|") + P_RightChip) + ";path=/; expires=" + P_today.toGMTString()+(P_cookieDomain?";domain="+P_cookieDomain:"")+";";  //
	}
} 

function P_popupoff() { // added by JQ 3/27/00 to disable popup
    P_showPop = false;
}

function P_NSform (ev) { // Netscape 4.x form submit capture
 P_showPop = false;
    routeEvent(ev);
    return true;
}
 
if (P_showPop) { // setting up click capture
  if(document.layers) { 
    window.captureEvents(Event.CLICK | Event.SUBMIT);
    window.onClick=P_checkClick;
    window.onSubmit=P_NSform;
  }
  if(document.all) {
    document.onclick=P_checkClick;
  }
}

// code (vanity plate): Gary Poster, with essential assists from Galyn Norwood, Phiroon Phihakendr, Jose Quijada, Pavandeep Kalra. Copyright 2000 Time Inc.
// note: we tried to eliminate the need for all body tag event handlers but onunload proved to be necessary because otherwise Netscape produces security errors when the cookie is set (even when the unload event is captured).

P_isLoaded=true;
