////////////////////////////////////////////////
//	        COMMON JAVASCRIPT FUNCTIONS       //
////////////////////////////////////////////////

// random number for ng ads
var axel = Math.random() + "";
window.rnd = axel * 1000000000000000000;

function handleError() {}

function out (str) { document.write (str) }


// ====================================================================
// GLOSSARY

function showGlossaryTerm(term) {
	var browserVer = parseInt(navigator.appVersion);
	remote=window.open("http://bwnt.businessweek.com/Glossary/home.asp?Code="+term,"glossaryWindow","width=470,height=400,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=yes,resizable=yes");
	remote.focus();
}

// ====================================================================
// FLOATING TICKER

function showTicker(symbol, channel) {
	top.location = "http://host.businessweek.com/businessweek/Corporate_Snapshot.html?Symbol=" + symbol;
}


// ====================================================================
// PRINTER FRIENDLY LINK CODE

function openPrintWin () {
	
	if (window.chan + "" == "undefined") {
		chan = "bw";
	}

	if (self.name=="") self.name = "mainWin";

	var l = location
	
	var pName = unescape(l.pathname);
	
	var pgStart = pName.indexOf("_PG");
	
	if (pgStart != -1) {
		pName = pName.substring(0,pgStart) + pName.substring(pgStart + 4, pName.length);
	}
	
	pName = ( (pName.indexOf("@@") == -1) ? pName : pName = pName.substring (pName.indexOf("/", 1)));

	var printDoc = l.protocol + "//" + l.hostname + ":" + l.port + "/print" + pName + "?" + chan;

	printFriendly = window.open(printDoc, "printWin");

	if (printFriendly) setTimeout ("printFriendly.focus()", 100);

}


// ====================================================================
// GENERIC POPUP WINDOW FUNCTION

// POPUP WINDOW ADJUSTMENTS
function popup(file,width,height,sbars) {

// POPUP ATTRIBUTES
	var name = 'popwin';
	sbars = sbars + "" == "undefined" ? "yes" : sbars;
	width = width + "" == "undefined" ? "" : "width="+width+",";
	height = height + "" == "undefined" ? "" : "height="+height+",";
	var attributes = 'scrollbars='+sbars+',resizable=yes,target=popwin,' + width + height + '';
	p = window.open(file, name, attributes);
}

// ====================================================================
// EXTRACTS A CGI FORMATTED VARIABLE

// 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);

}

// ====================================================================
// SETS THE REGION AND COUNTRY VARS

function setRegionVars () {

	var cookies = document.cookie.split("; ");
	var cookieStr = cookies.join ("&");
	
	window.country = extractQueryVar ("country", cookieStr);
	
	window.region = extractQueryVar ("temp_region", cookieStr);
	
	if (window.region == "") window.region = extractQueryVar ("perm_region", cookieStr);

}

setRegionVars ();


// ====================================================================
// SETS REGION COOKIE

function setRegionCookie (region, country) {

	var futureDate = new Date();
	
	futureDate.setFullYear(futureDate.getFullYear() + 10);
	
	document.cookie = "perm_region=" + escape(region) + "; domain=.businessweek.com; path=/; expires=" + futureDate.toGMTString();

	document.cookie = "country=" + escape(country) + "; domain=.businessweek.com; path=/; expires=" + futureDate.toGMTString();

	setRegionVars ();
	
}

// ====================================================================
// MAKES REGIONAL TOC URLS

function makeRegionalTocLinks (url) {

	urlParts = url.split("/");
	
	filename = urlParts[urlParts.length-1];
	
	issue = urlParts[urlParts.length-2].split("_");
	
	convWeek = "335";
	
	currWeek = parseInt(issue[0]) + "" + parseInt(issue[1]);
	
	if (window.region == "bw_as" && currWeek >= convWeek) {
		filename = "asia" + filename;
	} else if (window.region == "bw_eu" && currWeek >= convWeek) {
		filename = "europe" + filename;
	}
	
	urlParts[urlParts.length-1] = filename;
	
	url = urlParts.join("/");
	
	return url;

}

// ====================================================================
// REDIRECT ASIA AND EUROPE

/*if (location.href.indexOf("http://asia.businessweek.com") != -1 || location.href.indexOf("http://europe.businessweek.com") != -1) {
	self.location = "http://www.businessweek.com/common/international_bridge.htm";
}*/

// returns an accurate top position for a given element
function getRealTopPos(el) {
	iPos = 0
	while (el!=null) {
	 	iPos += el.offsetTop
		el = el.offsetParent
	}
	return iPos
}

// returns an accurate left position for a given element
function getRealLeftPos(el) {
	iPos = 0
	while (el!=null) {
	 	iPos += el.offsetLeft
		el = el.offsetParent
	}
	return iPos
}


function getHeadline () {
	var metas = document.getElementsByTagName("META");

	for (i = 0; i < metas.length; i++) {
	
		if (metas[i].name == "headline") {
		
			return (metas[i].content);
		
		}
	
	}
}