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

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


// ====================================================================
// SYNDICATE/INTERNATIONAL SITE REDIRECTION

var cookieDate = new Date ();

// set date to + 12 minutes
cookieDate.setMinutes ( cookieDate.getMinutes() + 12 );

// if you are within asia site...
if (location.hostname.indexOf ('asia.businessweek.com') != -1) {

	// set a cookie
	//document.cookie = 'sitename=asia;domain=.businessweek.com;path=/;expires=' + cookieDate.toGMTString();

// if you are within the europe site...
} else if (location.hostname.indexOf ('europe.businessweek.com') != -1) {

	// set a cookie
	//document.cookie = 'sitename=europe;domain=.businessweek.com;path=/;expires=' +
	//cookieDate.toGMTString();
	
// if you are within the aol site...
} else if (location.hostname.indexOf ('aol.businessweek.com') != -1) {

	// set a cookie
	document.cookie = 'sitename=aol;domain=.businessweek.com;path=/;expires=' +
	cookieDate.toGMTString();
	
// if you are within the compuserve site...
} else if (location.hostname.indexOf ('compuserve.businessweek.com') != -1) {

	// set a cookie
	document.cookie = 'sitename=compuserve;domain=.businessweek.com;path=/;expires=' +
	cookieDate.toGMTString();
	
// if you are within the netscape site...
} else if (location.hostname.indexOf ('netscape.businessweek.com') != -1) {

	// set a cookie
	document.cookie = 'sitename=netscape;domain=.businessweek.com;path=/;expires=' +
	cookieDate.toGMTString();

// if you are at the domestic site...
} else if (location.hostname.indexOf ('www.businessweek.com') != -1 && location.pathname.indexOf ('/magazine') == -1 && location.pathname.indexOf ('/premium') == -1 && document.cookie.indexOf ('bypassRedirect=true') == -1) {

	// if you have asia cookie...
	if (document.cookie.indexOf ('sitename=asia') != -1) {
	
		// redirect to asia hostname
		//location.hostname = 'asia.businessweek.com';
	
	// if you have the europe cookie...
	} else if (document.cookie.indexOf ('sitename=europe') != -1) {
	
		// redirect to the europe hostname
		//location.hostname = 'europe.businessweek.com';
	
	} else if (document.cookie.indexOf ('sitename=aol') != -1) {
	
		// redirect to the aol hostname
		location.hostname = 'aol.businessweek.com';
	
	} else if (document.cookie.indexOf ('sitename=netscape') != -1) {
	
		// redirect to the netscape hostname
		location.hostname = 'netscape.businessweek.com';
	
	} else if (document.cookie.indexOf ('sitename=compuserve') != -1) {
	
		// redirect to the compuserve hostname
		location.hostname = 'compuserve.businessweek.com';
	
	}

}


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

function showGlossaryTerm(term) {
	browserVer = parseInt(navigator.appVersion);
	
	if (navigator.appName == "Netscape" && browserVer < 3) 
	{
		remote=window.open("http://bwnt.businessweek.com/Glossary/home.asp?Code="+term,"glossaryWindow","width=470,height=400");
	}
	else 
	{
		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) {
	theURL = "http://host.businessweek.com/businessweek/Corporate_Snapshot.html?Symbol=" + symbol;
	top.location = theURL;
}


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

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

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

	var l = location
	
	pName = unescape(l.pathname);
	
	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)));

	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) {

// POPUP ATTRIBUTES
	var name = 'popwin';
	var attributes = 'resizable=yes,target=popwin,width=' + width + ',height=' + 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 () {

	cookies = document.cookie.split("; ");
	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";
}*/
