function out (str) { document.write (str) }

function cgObj(str) {
	this.str = str;
	var pairs = str.split("&");
	for (i=0; i<pairs.length; i++) {
		nv = pairs[i].split("=");
		if (nv[0]) this[nv[0]] = ( nv[1] ? (nv[1] != "" ? nv[1] : true) : true);
	}
}

function sProp (prop, val) {
	adOb[prop] = cgi[prop] ? cgi[prop] : val;
}

// defines the default ad position variables
function defPP() {
	var asz = "adsize=";
	var pp = "pagepos=";
	var sd = "spacedesc=";
	adOb.pp10 = adOb.chSb+asz+"none&"+pp+"10";
	adOb.pp4 = adOb.chSb+asz+"120x90&"+pp+"4";
	adOb.pp7 = adOb.chSb+asz+"140x800&"+pp+"7";
	adOb.pp5 = adOb.chSb+asz+"140x800&"+pp+"5";
	adOb.pp9 = adOb.chSb+asz+"300x250&"+pp+"9";
	adOb.pp13 = adOb.chSb+asz+"140x300&"+pp+"13";
	adOb.ppHeader = adOb.chSb+sd+"adsections";
	adOb.ppEditPromo = adOb.chSb+sd+"editpromo";
}

function setExclude() {
	var ee = "";
	var pn = location.pathname;

	if (!pn.match(/\/$/) && !pn.match("index.html?$")) {
		ee = pn.replace(/.*\/([^\/]+)\.[A-z]{0,4}$/, "$1");
		ee = ee.replace(/_PG\d+/, "");
	}
	sProp("editExclude", ee);
}

function setSite() {
	var site = "bw";
	if (window.region) site = window.region;
	if (location.host.match(/bwads/)) site = "staging";
	sProp("site",site);
}

function mkAdVar(adVar) {
	return ( adOb[adVar] ? adVar + "=" + adOb[adVar] + "&" : "" );
}

// Builds site, exclude and country variables and then returns a string
function buildCommonVars() {
	var params = mkAdVar("site") +
	mkAdVar("editExclude") +
	mkAdVar("rnd");
	adOb.commonAdVars = params;
}

function writeManager () {
		var params = adOb.commonAdVars + adOb.chSb + "spacedesc=roadblock&spacedesc=manager";
		debugAd(params, "", "Manager");
		out ('<scr'+'ipt language="javascript" src="http://ads.businessweek.com/html.ng/'+ params +'?"><\/scr'+'ipt>');
}

function hasVal (jsVar) {
	return (jsVar && jsVar != "") ? true : false;
}

function debugAd (adCodeStr, adFile, adLabel) {
	if (cgi["debug"]) {
		if (!hasVal(adLabel)) adLabel = "Ad Debug";
		var adCodeStrOrig = adCodeStr;
		if (hasVal(adFile)) adCodeStr = "ssi=" + adFile + "&" + adCodeStr;
		adCodeStr = adCodeStr.replace(/=/g, '= ');
		adCodeStr = adCodeStr.replace(/\&/g, ';<br>');
		var method = "Method: " + (adOb.noIfrm ? "script" : "iframe") + "<br>";
		out ('<div style="font-family:verdana;font-size:11px;"><B>'+adLabel+':<\/B><br>' + method + adCodeStr);
		out ('<br><a href="'+adURL+'/html.ng/'+adCodeStrOrig+'" target="_blank">Open Ad<\/a>');
		out ('<\/div><br>');
	}
}

function writeAd (ppParam, ssiFile, pos, w, h, ovrd, type) {
	var param =  ovrd ? ppParam : adOb.commonAdVars + ppParam;
	debugAd(param, ssiFile, pos);
	if (!cgi["noads"]) {
		//if (adOb.noIfrm) {
			if (type == "iframe") {
				out (	'<iframe frameborder=0 border=0 marginheight=0 marginwidth=0 vspace=0 scrolling=no ' +
						'style="margin:0px;width:'+w+'px;height:'+h+'px;border:0px;" ' +
						' src="http://ads.businessweek.com/html.ng/'+param+'"><\/iframe>');
			} else {
				out ('<SCR'+'IPT LANGUAGE="JavaScript" SRC="'+adURL+'/js.ng/'+param+'?"><\/SCRIPT>');
			}
		//} else {
		//	out('<iframe name="'+pos+'" id="'+pos+'" src="/common_adcode/ad_sizer.htm?'+param+'" marginheight=0 marginwidth=0 border=0 scrolling=no frameborder=0 style="display:block;width:'+w+'px;height:'+h+'px;"><\/iframe>');
		//}
	}
}

function writeRawAd (code) {
	if (!cgi["noads"]) {
		out (code);
	}
}

function setAdProps (ch, sub, oob) {
	sProp("chan", ch);
	sProp("sub", sub);
	sProp("OOB", oob);
	adOb.chSb = mkAdVar("chan") + mkAdVar("sub");
	buildCommonVars();
	defPP();
	window.chan = ch;
	window.sub = sub;
}

var undefined;
adOb = new Object;
cgi = new cgObj(location.search.substring(1));
window.debugFlag = cgi["debug"] ? true : false;
adOb.rnd = Math.random();
adURL = "http://ads.businessweek.com";
UA = navigator.userAgent;
adOb.noIfrm = (UA.indexOf("MSIE") != -1 && UA.indexOf("Mac") != -1) || 
			  (UA.indexOf("Opera") != -1) || 
			  (UA.indexOf("Safari") != -1) ? true : false;
//setExclude();
setSite();
