BWAdSite = new Array ()
BWAdSite[BWAdSite.length] = new adSiteObj("investortoolbox@","investortool")
BWAdSite[BWAdSite.length] = new adSiteObj("asia.businessweek.com","bw_as")
BWAdSite[BWAdSite.length] = new adSiteObj("europe.businessweek.com","bw_eu")
BWAdSite[BWAdSite.length] = new adSiteObj("bwstage.businessweek.com","staging")	// NOTE: TEMPORARY FOR TESTING

function adDebug (adCodeStr, adFile, adLabel) {
	if (location.search.indexOf ("debug") != -1) {
		var adCodeStrOrig = adCodeStr;
		if (adFile + "" != "undefined" && adFile + "" != "") adCodeStr = "ssi=" + adFile + "&" + adCodeStr;
		if (adCodeStr.replace) params = adCodeStr.replace(/=/g, '=<img src="/common_images/bw_1x1.gif" height="1" width="1">');
		if (adLabel + "" == "undefined" || adLabel + "" == "") adLabel = "Ad Debug"
		params = params.split("&");
		params = params.join(";<br>");
		document.write ('<br><font style="color:#000000;font-size:11px;"><code><B>'+adLabel+':</B><br>' + params);
		document.write ('<br><a href="http://ads.businessweek.com/html.ng/'+adCodeStrOrig+'" target="_blank">Open Ad</a>');
		document.write ('</font></code><br>')
	}
}

function write728 () {
	
	adDebug(window.bannerParams, window.posit10File, "728");
	document.write ('<\/TD><\/TR><TR><TD colspan="99" align="center">');
	document.write ('<img src="/common_images/bw_1x1.gif" width="1" height="90" align="left" border="0">');
	document.write('<SCR'+'IPT LANGUAGE="JavaScript" SRC="http://ads.businessweek.com/js.ng/'+window.bannerParams+'?"><\/SCRIPT>')
}

function writeGenericPP10 () {

	adDebug(window.bannerParams, window.posit10File, "Generic pagepos 10");
	
	document.write ('<\/TD><\/TR><TR><TD colspan="99" align="right">');
	document.write ('<img src="/common_images/bw_1x1.gif" width="1" height="60" align="left" border="0">');
	document.write('<div id="topAdDiv"><SCR'+'IPT LANGUAGE="JavaScript" SRC="http://ads.businessweek.com/js.ng/'+window.bannerParams+'?"><\/SCRIPT></div>')

//	document.write('<TABLE BGCOLOR="#FFFFFF" BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="768" align="left">');
//	document.write('<TR><TD valign="bottom" align="left">');
//	document.write ('<IMG SRC="http://www.businessweek.com/common_images/bw_1x1.gif" WIDTH=1 HEIGHT=1 BORDER=0 HSPACE=144 /><BR />');
//	document.write ('<FONT CLASS="smalltext">');
//	document.write ('<A HREF="/register.htm" TARGET="_top">Register<\/A><BR>');
//	document.write ('<A HREF="https://www.neodata.com/ITPS2.cgi?ItemCode=BWEK&OrderType=Reply+Only&iResponse=BWEK.SUBSCRIBEUSCAN" TARGET="_top">Subscribe<\/A><BR>');
//	document.write ('<\/FONT>');
//	document.write ('<\/td>');
//	document.write('<td width="468" align="right">');
//	document.write('<NOLAYER><IFRAME SRC="http://ads.businessweek.com/html.ng/'+window.bannerParams+'?" width=468 height=60 frameborder=0 border=0 marginheight=0 marginwidth=0 vspace=0 scrolling=no><A HREF="http://ads.businessweek.com/click.ng/'+bannerParams+rnd+'?"><IMG SRC="http://ads.businessweek.com/image.ng/'+bannerParams+rnd+'?" border=0 width=468 height=60><\/A><\/IFRAME><\/NOLAYER><ILAYER id=banner visibility=hide width=468 height=60><\/ILAYER>');
//	document.write('<\/td><\/tr><\/table><br clear="all">');
}

function writeNoTablePP10 () {

	adDebug(window.bannerParams, window.posit10File, "Pagepos 10 (No Table Wrapper)");
	document.write('<SCR'+'IPT LANGUAGE="JavaScript" SRC="http://ads.businessweek.com/js.ng/'+window.bannerParams+'?"><\/SCRIPT>')
	window.adWritten = true;
	
}

function write468 () {

	adDebug(window.bannerParams, window.posit10File, "468 only");
	document.write ('<img src="/common_images/bw_1x1.gif" width="1" height="60" align="left" border="0">');
	document.write('<SCR'+'IPT LANGUAGE="JavaScript" SRC="http://ads.businessweek.com/js.ng/'+window.bannerParams+'?"><\/SCRIPT>')
}

function adSiteObj (siteName, adSite) {
	this.siteName = siteName
	this.adSite = adSite
}

// 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 unescape(term);

}

// gets the site value either from the hostname or the querystring
function getAdSite () {

	if (document.cookie.indexOf("perm_region=bw_as") != -1)
	{
			adSite= "bw_as";
	} else if (document.cookie.indexOf("perm_region=bw_eu") != -1)
	{
			adSite="bw_eu";
	} else {
		adSite="bw"
	}
	
	adSite = "site=" + adSite;
	
	return adSite;
}

// Extracts page name and passes as exclusion var
function getExclude () {

	var editExclude = location.pathname
	var pos = editExclude.lastIndexOf("/")
	
	if (pos == (editExclude.length -1) || editExclude.indexOf ("index.htm") != -1) {
	
		editExclude = ""

	} else {
	
		editExclude = editExclude.substring (pos + 1)
		pos = editExclude.indexOf ("_PG")
		
		if ( pos != -1) {
		
			editExclude = editExclude.substring (0, pos) + editExclude.substring(pos+3)
		
		}
		
		pos = editExclude.lastIndexOf(".")
		editExclude = editExclude.substring(0, pos)
		editExclude = "editexclude=" + editExclude + '&'

	}
	
	return editExclude

}

// sets the chan value either from the script or the querystring
function setChannel (chanVal) {
	var queryVal = extractQueryVar("chan", location.search)
	if (queryVal != "") chanVal = queryVal
	window.chan = chanVal
	defineAdPositionDefaults()
}

// sets the sub value either from the script or the querystring
function setSub (subVal) {
	var queryVal = extractQueryVar("sub", location.search)
	if (queryVal != "") subVal = queryVal
	window.sub = ( subVal == "" ? "" : "sub=" + subVal + "&")
}


// Builds site, exclude and country variables and then returns a string
function buildCommonVars() {
	window.site = getAdSite()
	window.editExclude = getExclude()
	var commonStr = window.site + "&" +
					window.editExclude + 
					window.sub +
					"var=" + window.rnd +
					"&"
	return commonStr
}

// defines the default ad position variables
function defineAdPositionDefaults() {
	window.posit_10 = "adsize=none&pagepos=10"
	window.posit_4 ="adsize=120x90&pagepos=4"
	window.posit_7 ="adsize=140x800&pagepos=7"
	window.posit_5 ="adsize=140x800&pagepos=5"
	window.posit_9 ="adsize=300x250&pagepos=9"
	window.posit_13 ="adsize=140x300&pagepos=13"
}

// builds the banner querystring for each position
function buildAdParams() {
	window.bannerParams = window.commonAdVars  + posit_10 + "&chan=" + window.chan + "&"
	window.bigbannerParams = window.bannerParams + "&chan=" + window.chan + "&"
	window.sponsorAdParams = window.commonAdVars + posit_4 + "&chan=" + window.chan + "&"
	window.skyParams = window.commonAdVars + posit_7 + "&chan=" + window.chan + "&"
	window.sky2Params = window.commonAdVars + posit_5 + "&chan=" + window.chan + "&"
	window.boxAdParams = window.commonAdVars + posit_9 + "&chan=" + window.chan + "&"
	window.subOfferParams =  window.commonAdVars + posit_13 + "&chan=" + window.chan + "&"
}

function writeAds() {
	if (window.regsub && bannerParams.indexOf("adsize=728x90") != -1) {
		write728()
	} else if (window.regsub && bannerParams.indexOf("adsize=468x60") != -1 || bannerParams.indexOf("adsize=none") != -1) {
		writeGenericPP10()
	} else if (!window.regsub) {
		if ( bannerParams.indexOf("adsize=728x90") != -1 ) {
			window.bannerParams = window.commonAdVars + "adsize=468x60&pagepos=10&"
		}	
		write468()
	}
}

function writeManager () {

	//if (window.suppressOOB == false) {
		window.managerParams = window.commonAdVars + "chan=" + window.chan + "&spacedesc=roadblock&spacedesc=manager";
		adDebug(window.managerParams, "", "Manager")
		document.write ('<scr'+'ipt language="javascript" src="http://ads.businessweek.com/html.ng/'+ window.managerParams +'?"><\/scr'+'ipt>')
	//}

}

if(window.rnd+""=="undefined"){
	var axel = Math.random() + ""
	window.rnd = axel * 1000000000000000000
}

if (location.pathname.indexOf("/common_adcode/") != -1) window.regsub = "regsub";

defineAdPositionDefaults()
