//navigation
var openwindow;
var imgOpen = "/images/opened.gif";
var imgClose= "/images/closed.gif";
var imgHighlight = "/images/navbox_on_highlight.gif";
var imgNavOff = "/images/navbox_off.gif";
var imgNavOn = "/images/navbox_on.gif";
var imgSpacer = "/images/spacer.gif";
var highlightColor = "#cc9900";
var collapseOnSel = true;
var oldDiv = null;

// initialize local browser
var is=new Browser();

var jsTxt = "", stylesTxt = "";

if ( !is.ns4 ) {
	jsTxt = "<script language=\"JavaScript\" src=\"/js/domutils.js\"></script>\n";
	jsTxt += "<script language=\"JavaScript\" src=\"/js/utils.js\"></script>\n";
	stylesTxt = "<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/styles.css\">";
 }else {
	jsTxt = "<script language=\"JavaScript\" src=\"/js/ns_utils.js\"></script>\n";
	stylesTxt = "<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/ns_styles.css\">";
}

// add standard script includes to the page
jsTxt += "<script language=\"JavaScript\" src=\"/js/CGMenu.js\"></script>\n";
jsTxt += "<script language=\"JavaScript\" src=\"/js/commentsnav.js\"></script>\n";
jsTxt += "<script language=\"JavaScript\" src=\"/js/privilegedredirects.js\"></script>\n";
jsTxt += "<script language=\"JavaScript1.2\" src=\"/js/tooltip.js\"></script>\n";

document.write( jsTxt );
document.write( stylesTxt );

/*
 * ------------------ ARCHIVED CONTENT FUNCTIONS start ---------------------
 * Documentum templates should be changed to use the href of the format
 * <a class="basecolor" 
 *	href="javascript:navigateInBodyLink(escape('/capgroup/action/getContent/GIG/Europe/Market_Insights/World_Markets_Review/WMRDetail/<%=vMITimePeriod%>/North_America'));"><b>North_America</b></a>
 * to use these functions.
 */

/*
 * If the current request (address bar) is a getContent request,
 * let the url from Documentum (getContent) pass through.
 * If the current request (address bar) is a getArchiveResult request,
 * replace the getContent in Documentum url with getArchiveResult,
 * add the from date to querystring and make the request.
 */
function navigateInBodyLink( docUrl ) {
	var currentUrl = new String(unescape(window.location.href));
	var pathName = new String(unescape(window.location.pathname));
	var fromDate = null;
	var isCurrentContentActive =  isActiveUrl( pathName );
	var isCurrentContentArchived = isArchivedUrl( pathName );
	var newUrl = null;

	if ( isCurrentContentArchived ) {
		var fromDateQry = new String( "/:fromDate=" );
		var pos = pathName.indexOf( fromDateQry );
		if ( pos > 0 ) { 
			// extract fromDate from queryString
			fromDate = pathName.substring( pos  );
		} else {
			// queryString does not contain fromDate, use today's date
			fromDate = fromDateQry + getCurentDateForArchive() + ":/" ;
		}
		// replace 'getContent' with 'getArchiveResult'
		newUrl = swapActiveToArchive( docUrl, fromDate );
	}

	/*
	alert ( "location.pathName = [" + pathName + "]\n" +
			"docUrl = [" + docUrl + "]\n" +
			"isCurrentContentActive = [" + isCurrentContentActive + "]\n" +
			"isCurrentContentArchived = [" + isCurrentContentArchived + "]\n" + 
			"fromDate = [" + fromDate + "]\n" +
			"newUrl = [" + newUrl + "]"
			);
	*/

	if ( newUrl == null ) {
		window.location.href = docUrl; // current request is active, use 'getContent'
	} else {
		window.location.href = newUrl; // current request is archived, use 'getArchiveResult'
	}
	
}

/*
 * Replaces 'getContent' in the url with 'getArchiveResult'
 */
function swapActiveToArchive( originalUrl, fromDateQryPart ) {
	if ( originalUrl == null || fromDateQryPart == null ) {
		return null;
	}

	if ( originalUrl.indexOf( "/capgroup/action/getArchiveResult/" ) != -1 ) {
		// if it's already a request for archived content, check 'fromDate'
		// argument and return the value
		if ( originalUrl.indexOf ( "/:fromDate=" ) == -1 ) {
			return originalUrl + fromDateQryPart ;
		} else {
			return originalUrl;
		}
	} else {
		// it's a getContent request
		var getContentUrlPart = "/getContent/";
		var pos = originalUrl.indexOf( getContentUrlPart );
		var swappedUrl = null;
		if ( pos > 0 ) {
			// replace getContent with getArchiveResult
			swappedUrl = originalUrl.substring( 0, pos ) + 
							"/getArchiveResult/" + 
							originalUrl.substring( pos + getContentUrlPart.length ) +
							fromDateQryPart;
		} else {
			swappedUrl = originalUrl;
		}
		return swappedUrl;
	}
}

/*
 * Determines if the current url is a request for Archived content
 */
function isArchivedUrl( url ) {
	if ( url == null ) 	{
		return false;
	} else {
		return url.indexOf( "/capgroup/action/getArchiveResult/" ) != -1 &&
				url.indexOf( "/capgroup/action/getArchiveResult/file/" ) == -1 &&
				url.indexOf( "/capgroup/action/getArchiveResult/image/" ) == -1 &&
				url.indexOf( "/capgroup/action/getContent/image/" ) == -1 &&
				url.indexOf( "/capgroup/action/getContent/file/" ) == -1;
	}
}

/*
 * Determines if the current url is a request for Active content
 */
function isActiveUrl( url ) {
	if ( url == null ) 	{
		return false;
	} else {
		return url.indexOf( "/capgroup/action/getContent/" ) != -1 &&
				url.indexOf( "/capgroup/action/getContent/file/" ) == -1 &&
				url.indexOf( "/capgroup/action/getContent/image/" ) == -1;
	}
}

/*
 * returns the current date in yyyy-mm-dd format
 */
function getCurentDateForArchive() {
	var today = new Date();
	var day   = today.getDate();
	var month = today.getMonth();
	var year  = today.getYear();
	if (year < 1900){
		year += 1900;
	}
	return ( year + "-" + padLeft(month) + "-" + padLeft(day) );
}

/*
 * Returns a value padded on the left by a 
 * zero for values between 0 and 9
 */
function padLeft(val) {
	return ( val <= 9 && val >= 0  ?  "0": "" ) + val;
}

/*
 * ------------------ ARCHIVED CONTENT FUNCTIONS End ---------------------
 */

/**
 * General purpose decode helper
 */
function decodeApostrophe(encodeString) {
  // Create a regular expression to search all ' in the string
  var lsRegExp = /\'/g;
  // Return the decoded string
  return unescape(String(encodeString).replace(lsRegExp, "\'")); 
}

/**
   * Write out the menu to the page
   */
function writeNavLinks( itemToHighlight, htmlScript ) {
    var defaultMenu = parseInt( itemToHighlight );

	var tmpStr = setColor ( itemToHighlight, htmlScript );	// defined in ns_utils and utils
	var updatedScript = null;
	
	if ( tmpStr ) {
		updatedScript = tmpStr;
	} else {
		updatedScript = htmlScript;
	}

    document.write( updatedScript );
	if ( defaultMenu ) {
    	initializeMenu( defaultMenu, updatedScript );
	}
}


/************** DHTML handlers start ********************/
/*
 * Builds a div (including table) representing a leaf (clickable link) 
 * Leaves out any divs with the id of 9999 as defined in *nav.xml files
 * Resulting HTML sample:
 <div id='e300' class='item2'>
	<table cellspacing=0 cellpadding=0 border=0>
		<tr>
			<td valign=top id='e300square'>
				<img src='/images/navbox_off.gif' width=10 height=11 border=0 name='e300square'>
			</td>
			<td valign=top id='e300td' class='navbold'>
				<a href='/capgroup/action/getContent/GIG/North_America/Product/Start%20Page/'  
					onmouseover="document.e300square.src='/images/navbox_on.gif'" 
					onmouseout="document.e300square.src='/images/navbox_off.gif'">Product Range</a>
			</td>
		</tr>
	</table>
</div>
 */
function buildPageNavHTML( divName, level, displayName, url, style ) {
	var content = new String( "" );
	var boldClass = (  style == "bold"  ?  "navbold" :  "nav" );
	
	if ( divName != "e9999" ) {
		content = "<div id='" + divName + "' class='item" + level + "'>";
		content += "<table cellspacing=0 cellpadding=0 border=0><tr><td valign=top id='" + divName + "square'>";
		content += "<img src='" + imgNavOff + "' width=10 height=11 border=0 name='" + divName + "square'></td>";
		content += "<td valign=top id='" + divName + "td' class='" + boldClass + "'>";
		content += "<a href='" + url +"'  onmouseover=\"document." + divName + "square.src='" + imgNavOn + "'" ;
		content += "\" onmouseout=\"document." + divName + "square.src='" + imgNavOff + "'\">";
		content += displayName + "</a></td></tr></table></div>";
	}
	
	return content;
}

/*
 * Builds a div (including table) representing a Node 
 * (returns 2 open ended div)
 * Resulting HTML sample:
 <div id='a301' class='title1'>
	<table cellspacing=0 cellpadding=0 border=0>
		<tr>
			<td valign=top nowrap>
				<a href='javascript:showMenu(301)' class='nav'>
					<img src='/images/spacer.gif' border=0 width=8 height=8>
					<img src='/images/closed.gif' class='arrow' id='i301' border='0' width='10' height='8'>
				</a>
			</td>
			<td class='cell1' valign=top>
				<a href='javascript:showMenu(301)' class='nav' style='font-weight:bold;'>Equity</a>
			</td>
		</tr>
	</table>
	<div id='s301' class='canvas'>
 */
function buildSectionNavHTML( divName, className, displayName, url ) {
	var boldClass = ( className == "title1"  ?  "font-weight:bold;" : new String("")  );
	var content = new String( "" );
	var menuText = new String( "" );

	if ( is.ns4 ) {
		menuText = "<a href='javascript:showMenu(" + divName + ")' class='nav' style='";
		menuText += boldClass + "'><a href='javascript:showMenu(" + divName + ")' class='nav'";
		menuText += "style='" + boldClass + "'>" + displayName + "</a></a>";
	} else {
		menuText = "<a href='javascript:showMenu(" + divName + ")' class='nav' style='" + boldClass + "'>";
		menuText += displayName + "</a>";
	}
	
	content = "<div id='a" + divName + "' class='" + className + "'>";
	content += "<table cellspacing=0 cellpadding=0 border=0><tr><td valign=top nowrap>" ;
	content += "<a href='javascript:showMenu(" + divName + ")' class='nav'>" ;
	content += "<img src='" + imgSpacer + "' border=0 width=8 height=8>" ;
	content += "<img src='" + imgClose + "' class='arrow' id='i" + divName + "' border='0' width='10' height='8'></a>";
	content += "</td><td class='cell1' valign=top>" + menuText + "</td></tr></table><div id='s" + divName + "' class='canvas'>";
	
	return content;
}


/*
 * Opens a new popup window for the specified Commingled Fund.
 */
function GotoCommingledFund(lob, acct, fundDate) {
	windowFocusForIE("/capgroup/action/getReport/:lob=" + lob 
		+ "&acct=" + acct + 
		"&fundDate=" + fundDate + 
		":/", "commingledFund", "resizable=yes,height=575,width=720,scrollbars=yes,status=no");
}

/*
 * Used in PE Company Description pages, replaces the 
 * current address with the external url
 */
function GotoSomeOtherCompanySite(url) {
  alert("You are leaving capgroup.com and are entering an unrelated site. " +
  			"The Capital Group Companies, Inc. and its subsidiaries are not responsible " +
			"for any content provided on the following site.");
  window.location=URL;
}

/*
 * Prompts the user for logout
 */
function confirmLogout() {
	var msg = "This will remove you from the secure area of the site. Are you sure you want to logout?";
	var logOutUrl = "/capgroup/action/logout/";
	if ( window.confirm( msg ) ) {
		window.location = logOutUrl ;
	}
}

/*
 * DEPRECATED but retained to maintain old content pages
 * Spawns a new window with the specified features.
 */
function windowFocus(URL, windowname, features)	{
	if (openwindow && !openwindow.closed) {
		openwindow.close();
	}
	openwindow = window.open( URL, windowname, features);
}

/*
 * Spawns a new window with the specified features.
 * For Mac and/or NetScape, the parent window's address
 * is replaced as the browsers do not support OLE in-place objects
 */
function windowFocusForIE(URL, windowname, features)	{
	if (navigator.platform == 'MacPPC')	{
		window.location=URL;
	}  else  {
		if (navigator.appName=="Netscape"){
			window.location=URL;
		}	else	{	
			if (openwindow && !openwindow.closed) {
				openwindow.close();
			}
			openwindow = window.open( URL, windowname, features);
		}	
	}
}

/*
 * Opens the Change Business - Region window
 */
function openChangeBizRegion() {
	windowFocus('/capgroup/action/changeBizRegion/', 
		'_blank', 
		'resizable=yes,height=200,menu=no,width=400,scrollbars=no,status=no');
}

/*
 * Appends parameters to the url passed in from the header file
 * and launches a new Subscriptions popup window with the address
 */
function openSubscriptionsWindow( url, canUnsubsribe, 
													 accountNumber, clientName, 
													 accountDescription ) {

	var completeUrl = url + "canUnsubscribe=" + canUnsubsribe;

	if ( accountNumber != null ) {
		completeUrl += "&accountNumber=" + accountNumber;
	}

	if ( clientName != null )	{
		completeUrl += "&clientName=" + clientName;
	}
	
	if ( accountDescription != null )	{
		completeUrl += "&accDesc=" + accountDescription;
	}

	completeUrl += ":/";

	windowFocus( completeUrl, "_blank", 
			"dependent=yes,scrollbars=no,resizable=no,height=260,width=340,status=no,menubar=no" );
}

/*
 * Opens a new window to retrieve pdf, doc, and xls content
 */
function openDownloadsWindow( url, windowName  ) {
	windowFocusForIE( url, 
								windowName, 
								"resizable,height=575,width=720,scrollbars=yes,status=yes,menubar=yes");
}

/*
 * Repositions the footer based on the height of the exhibit
 * and left nav being displayed on the page.
 */
function repositionFooter() {
	// initialize variables
	var top="0";
	var height="0";
	var intTop=0;
	var intHeight=0;
	var intMax=0;
	var x=null;
	
	// have a fudge factor available if necessary to make the positioning look good
	var fudgeFactor=-90;
	var divFromTop = 260;
	
	if ( divHeight != 0 ){
		if (document.all) {	
			// locateObject() is in tooltip.js			  
			intMax = divFromTop + divHeight + fudgeFactor;
			if ( intMax > 600 )	{
				locateObject("leftSpacerBlock").height = intMax;
			}
		}	else if ( document.getElementById ) {
			intMax = divFromTop + divHeight + fudgeFactor;
	
			// use the larger of the hard-coded minimum position for the top of the footer
			// or the actual largest top/height found
			if ( intMax > 600 )	{
	      		document.getElementById("leftSpacerBlock").style.height = intMax;
			}		 
		}
		
		// account for Left Nav height and exhibits
		var menuDiv = document.getElementById( "body" );
		var cs = null;
		var menuHeight = 0;

		if ( menuDiv != null ) {
			if ( document.defaultView ) {
				cs = document.defaultView.getComputedStyle( menuDiv, null );
				menuHeight = parseInt( cs.getPropertyValue ( "height" ) );
			} else if ( window.getComputedStyle ) {
				cs = window.getComputedStyle ( menuDiv, null );
				menuHeight = parseInt( cs.getPropertyValue ( "height" ) );
			} else {
				menuHeight = parseInt( menuDiv.offsetHeight ) ;
			}
		}
		// see if the LN is taller than the body
		if ( ( menuHeight > intMax ) && (intMax > 0) ) {
			// resize the spacer block
			if ( document.all ){
				locateObject("leftSpacerBlock").height = menuHeight;
			} else if ( document.getElementById ) {
				document.getElementById("leftSpacerBlock").style.height = menuHeight;
			}
		}
		
	}
}

/**
   * Browser detection
   */
function Browser() {
	var b = navigator.appName;

	if ( b== "Netscape" ) {
		this.b = "ns";
	} else if ( b == "Microsoft Internet Explorer" ) {
		this.b = "ie";
	} else {
		this.b=b;
	}

	this.name = b;
	this.version=navigator.appVersion;
	this.v=parseInt(this.version);
	this.ns=(this.b=="ns" && this.v>=4);
	this.ns4=(this.b=="ns" && this.v==4);
	this.ns5=(this.b=="ns" && this.v==5);
	this.ie=(this.b=="ie" && this.v>=4);
	this.ie4=(this.version.indexOf('MSIE 4')>0);
	this.ie5=(this.version.indexOf('MSIE 5')>0);
	this.ie55=(this.version.indexOf('MSIE 5.5')>0);
	this.dom=((document.createRange&&(document.createRange().createContextualFragment))?true:false);
	this.min=(this.ns||this.ie);
	
	var ua=navigator.userAgent.toLowerCase();

	if ( ua.indexOf("win") > -1 ) {
		this.platform="win32";
	} else if ( ua.indexOf("mac") > -1 ) {
		this.platform="mac";
	}  else {
		this.platform = "other";
	}
}