// initialize global variables
function initVars () {

	if (window.adDebug) {
		if (!window.commonAdVars) {
		setSub("")
		window.commonAdVars = buildCommonVars()
		
		}
	}

	window.specialParams = window.commonAdVars + "chan=sp" + window.chan + "&spacedesc=special";
	window.popunderParams = window.commonAdVars + "chan=sp" + window.chan + "&spacedesc=popunder";

	document.cookie = "BW_co=1; domain=.businessweek.com; path=/;";

	cookieOn = true;

	if (document.cookie.indexOf("BW_co=1") == -1) cookieOn = false;
	
	// indicates whether the special ad code should be written
	if (window.suppressOOB + "" != "true") suppressOOB = false;
	
}



// gets the value of a given cgi var or returns ""
function extractQueryVar (term, queryStr) {

	if (queryStr.indexOf("?") != 0) queryStr = "?" + queryStr;

	var pos = queryStr.indexOf ("?" + term + "=")
	
	if (pos == -1) pos = queryStr.indexOf ("&" + term + "=")
	
	if (pos != -1) {
		pos += term.length + 2
		var endPos = queryStr.indexOf("&", pos)
		if (endPos == -1) endPos = queryStr.length
		term = queryStr.substring(pos, endPos)
	} else {
		term = "";
	}
	
	return term;

}



// defines channel index objects
function makeChanObject (KLCode, url, width, height) {

	this.KLCode = KLCode;
	this.url = url;
	this.width = width;
	this.height = height;

}





// sets the default popup depending on the site, etc.
function editPop () {

	popFile = "";			// popup file variable
	defaultWidth = "";
	defaultHeight = "";

	// if in the aol site...
	if (location.hostname.indexOf ("aol.businessweek.com") != -1) {

		popFile = "http://aol.businessweek.com/ads/aol_exit.htm";
		defaultWidth = 300;
		defaultHeight = 420;

	} else {

		popFile = "/ads/subscribe_popup_entry.htm";
		defaultWidth = 300;
		defaultHeight = 490;

	}
	
	bwChannel = new Array ();
	bwChannel[bwChannel.length] = new makeChanObject ("cg1=daily_briefing,cg2=index", "/ads/insight.htm", 320, 600);
	bwChannel[bwChannel.length] = new makeChanObject ("cg1=investing,cg2=index",  "/ads/insight.htm", 320, 600);
	//bwChannel[bwChannel.length] = new makeChanObject ("cg1=technology,cg2=index", "/ads/tech_special_popup.htm", 320, 600);
	bwChannel[bwChannel.length] = new makeChanObject ("cg1=globalbiz,cg2=index", "/ads/insight.htm", 320, 600);
	//bwChannel[bwChannel.length] = new makeChanObject ("cg1=smallbiz,cg2=index", "/ads/insight.htm", 320, 600);
	
	//bwChannel[bwChannel.length] = new makeChanObject ("cg1=bschools,cg2=mbapremium", "http://ads.businessweek.com/html.ng/site=bw", 320, 600);
	
	bwChannel[bwChannel.length] = new makeChanObject ("cg1=bschools,cg2=index", "/ads/insight.htm", 320, 600);
	//bwChannel[bwChannel.length] = new makeChanObject ("cg1=bschools,cg2=index", popFile, defaultWidth, defaultHeight);
	bwChannel[bwChannel.length] = new makeChanObject ("cg1=careers,cg2=index", "/ads/insight.htm", 320, 600);
	//bwChannel[bwChannel.length] = new makeChanObject ("", "/ads/intl_special_report.htm", 250, 223);


	// loop thru channel indexes
	for (var i=0; i < bwChannel.length; i++) {

		// if a match occurs...
		if (bwChannel[i].KLCode == window.epulse_content_group) {

			suppressOOB = true;
	
			// add edit code to special ad cookie
			incrementCookie("ED");
			
			// launch popup after a brief delay
			setTimeout ("popupWin = window.open('"+bwChannel[i].url+"', 'POPUP', 'width=" + bwChannel[i].width + ",height=" + bwChannel[i].height +"' )",1000)
								
			// break out of the loop
			break;
		}
	}

}



function changeIndexLinks () {

	if (window.region == "bw_as") {
		for (i = 0; i < document.links.length; i++) {
			/*if (document.links[i].href == "/index.html" || document.links[i].href.indexOf (".businessweek.com/index.html") != -1) {
				document.links[i].href = "/asia_index.html";
			}*/
			if (document.links[i].href.indexOf("/magazine/toc/") != -1) {
			// calls function from common.js
				document.links[i].href = makeRegionalTocLinks (document.links[i].href);
			}
		}
	}
	
	if (window.region == "bw_eu") {
		for (i = 0; i < document.links.length; i++) {
			/*if (document.links[i].href == "/index.html" || document.links[i].href.indexOf (".businessweek.com/index.html") != -1) {
				document.links[i].href = "/europe_index.html";
			}*/
			if (document.links[i].href.indexOf("/magazine/toc/") != -1) {
				// calls function from common.js
				document.links[i].href = makeRegionalTocLinks (document.links[i].href);
			}
		}
	}

}

function debugText (text) {
	if (location.search.indexOf("debug") != -1) {
		document.write ("<B><TT>" + text + "</TT></B><BR><BR>");
	}
}


// MAIN SCRIPT

// ADDED 8-20-03
// MAKE LINKS TO INDEX REGIONAL
changeIndexLinks();

// initialize variables
initVars();

adCookie = extractCookie();

// if being served off the businessweek.com site...
if (location.hostname.indexOf ("businessweek.com") != -1 && adCookie.indexOf ("bypass") == -1) {
	
	// if popunder has not launched...
	if ( launchCheck("PU", 1, 0) && navigator.userAgent.indexOf("AOL") == -1) {

		if (window.adDebug) adDebug(window.popunderParams, "", "<br>Popunder");

		document.write('<SCR'+'IPT LANGUAGE="JavaScript" SRC="http://ads.businessweek.com/js.ng/'+window.popunderParams+'?"></SCRIPT>');

		// for dubugging only
		// document.write('<NOLAYER><IFRAME SRC="/sponsors/prosavvy/launcher.htm" width=1 height=1 frameborder=0 border=0 marginheight=0 marginwidth=0 vspace=0 scrolling=no><\/IFRAME><\/NOLAYER><LAYER  SRC="/sponsors/prosavvy/launcher.htm" visibility=hide width=1 height=1><\/LAYER>');


	}


	if (launchCheck("SA", 1, 0) && suppressOOB == false) {

		if (window.adDebug) adDebug(window.specialParams, "", "<br>Out-of-banner");

		// write adcode for paid out-of-banner
		document.write('<SCR'+'IPT LANGUAGE="JavaScript" SRC="http://ads.businessweek.com/js.ng/'+window.specialParams+'?"><\/SCRIPT>');
	
	
		// for debugging only
		//document.write ('http://ads.businessweek.com/html.ng/' + specialParams+rnd);
		
	}

	
}

