var IE4 = (document.all && !document.getElementById)?true:false;
var W3C = (document.getElementById)?true:false;

// ####### RETURNS CHARTS #######
// set default selected tab
if ((''+typeof(returnsBarGraphSelected)+'').toLowerCase() == "undefined") {	returnsBarGraphSelected = 3; }
// set default periods
if ((''+typeof(aReturnsBarGraphPeriods)+'').toLowerCase() == "undefined") {	aReturnsBarGraphPeriods = new Array(90, 180, 365, 1095, 1825, 3650); }

function changeTabClass(tabnum, status) {
	document.getElementById('l'+tabnum).className = status+'TabLeft';
	document.getElementById('c'+tabnum).className = status+'TabCenter';
	document.getElementById('r'+tabnum).className = status+'TabRight';
	returnsBarGraphSelected = tabnum;
}

function selectOptions() {
	var returnform = document.getElementById('returnsGraphForm');
	var graphcompare =  document.getElementById('graphCompare');
	var graphCuriso = document.getElementById('graphCuriso');

	// Index
	if (graphcompare != null) {
	    for (i=0;i<graphcompare.options.length;i++) {
    	    if ( (graphcompare.options[i].value * 1) == (iGraphIndexId * 1) ) {
		        graphcompare.options[i].selected = true;
				break;
			}
	    }
	}
	// Currency
	if (graphCuriso != null) {
	    for (i=0;i<graphCuriso.options.length;i++) {
		    if ( graphCuriso.options[i].value.toUpperCase() == sGraphCuriso.toUpperCase() ) {
			    graphCuriso.options[i].selected = true;
				break;
			}
	    }
	}
}

function resetTab() {
	var totTabs = document.getElementById('returnsGraph').rows[0].cells.length / 3;
	for (var i=0; i<totTabs; i++) {
		changeTabClass(i, 'inactive');
	}
}

function updateReturnsGraph(tabnum) {
	resetTab();
	changeTabClass(tabnum, 'active');
	updateCharts();
}

function updateCharts() {
	var s = "";
	var aString;
	var sFieldName = "";
	var sFieldValue = "";
	var idx = 0;
	var sUrl = "";
	
	s = Trim(document.images.returnsChart.src.toLowerCase());
	
	var returnform = document.getElementById('returnsGraphForm');
	var graphcompare =  document.getElementById('graphCompare');
	var graphCuriso = document.getElementById('graphCuriso');

	// Array fields=value
	aString = s.split("&");

	for (var i = 0; i < aString.length; i++) {
		// index_id
		sFieldName = "idx=";
		idx = aString[i].indexOf(sFieldName);
		if (idx != -1 && graphcompare != null) {
			aString[i] = sFieldName + graphcompare.options[graphcompare.selectedIndex].value;
		}
		// period
		sFieldName = "period=";
		idx = aString[i].indexOf(sFieldName);
		if (idx != -1) {
			aString[i] = sFieldName + aReturnsBarGraphPeriods[returnsBarGraphSelected];
		}
		// currency_iso
		sFieldName = "iso=";
		idx = aString[i].indexOf(sFieldName);
		if (idx != -1 && graphCuriso != null) {
			aString[i] = sFieldName + graphCuriso.options[graphCuriso.selectedIndex].value;
		}
	}
	
	sUrl = aString.join("&");
	// update graph
	document.images.returnsChart.src = sUrl;
	// update index name
	if(graphcompare != null) {
	    document.getElementById('categoryIndexName').innerHTML = graphcompare.options[graphcompare.selectedIndex].text;
	}
}
// ####### RETURNS CHARTS: END #######


// ####### STRING FUNCTIONS #######
function Trim(stringa) {
	reTrim=/\s+$|^\s+/g;
	return stringa.replace(reTrim,"");
}

function Left(str, n){
	if (n <= 0)
			return "";
	else if (n > String(str).length)
			return str;
	else
			return String(str).substring(0, n);
}

function Right(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else {
			var iLen = String(str).length;
			return String(str).substring(iLen, iLen - n);
		}
}
// ####### STRING FUNCTIONS: END #######

// ####### LAYER FUNCTIONS #######
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getObj(obj) {
	if (IE4){var o=document.all[obj]}
	else if (W3C){var o=document.getElementById(obj)}
	return o;
}

function moveObj(obj, x, y) {
	var o = getObj(obj);
	o.style.position = 'absolute';
	o.style.top = y + 'px';
	o.style.left = x + 'px';
}

function showHideObj(obj, action) {
	var o = getObj(obj);
	o.style.visibility = action;
}
// ####### LAYER FUNCTIONS: END #######

function clearCurrentLink(strClassName) {
		var a = document.getElementsByTagName("A");
		for(var i=0;i<a.length;i++) {
				if(a[i].className == strClassName) {
						removeNode(a[i]);
						i--;
				}
		}
}
function removeNode(n) {
		if(n.hasChildNodes()) {
				for(var i=0;i<n.childNodes.length;i++) {
						n.parentNode.insertBefore(n.childNodes[i].cloneNode(true),n);
				}
		}
		n.parentNode.removeChild(n);
}

// onload event
function initMain() {
	if (bRemoveGlossaryLinks) {clearCurrentLink("definitionsLink");}
	// try to find RETURNS CHARTS table
	if ((''+document.getElementById('returnsGraph')+'').toLowerCase() != "null") {
		// show default tab
		changeTabClass(returnsBarGraphSelected, 'active');
		selectOptions();
	}
	
	// move fund arrow: "Risk and Rating" --> "Risk Profile Standard Deviation"
	if ((''+typeof(riskFundIndicator)+'').toLowerCase() != "undefined" && riskFundIndicatorX != 0) {
		/*alert('onload event');
		showHideObj('riskFundIndicator', 'visible');
		alert('riskFundIndicator start X value: ' + findPosX(getObj('riskFundIndicator')));
		alert('riskFundIndicator moving right ' + riskFundIndicatorX + 'px');
		moveObj('riskFundIndicator', findPosX(getObj('riskFundIndicator')) + riskFundIndicatorX, findPosY(getObj('riskFundIndicator')));
		alert('riskFundIndicator end X value: ' + findPosX(getObj('riskFundIndicator')));*/
		moveObj('riskFundIndicator', findPosX(getObj('riskFundIndicator')) + riskFundIndicatorX, findPosY(getObj('riskFundIndicator')));
		showHideObj('riskFundIndicator', 'visible');
	}
	// move category arrow: "Risk and Rating" --> "Risk Profile Standard Deviation"
	if ((''+typeof(riskCatIndicator)+'').toLowerCase() != "undefined" && riskCatIndicatorX != 0) {
		moveObj('riskCatIndicator', findPosX(getObj('riskCatIndicator')) + riskCatIndicatorX, findPosY(getObj('riskCatIndicator')));
		showHideObj('riskCatIndicator', 'visible');
	}
}

// postload event
//Obsolete: Using local virtual dir. /graph/ (24/11/2008)
function resolveLegacyGraphLocation(){
	var arrImages = document.getElementsByTagName("IMG");
	for(var iImage = 0;iImage < arrImages.length;iImage++){
		var elImage = arrImages[iImage];
		var strLegacyGraphImagePath = new String(elImage.getAttribute("legacy-graph-image-path"));
		if(strLegacyGraphImagePath != "" & strLegacyGraphImagePath != null & strLegacyGraphImagePath != "undefined" & strLegacyGraphImagePath != "null"){
			var strAppropriateGraphRoot = "http://graph.morningstareurope.com";
			if(document.location.href.indexOf("https://") == 0){
				strAppropriateGraphRoot = "https://secure.morningstareurope.com/graph";
			}
			if(strLegacyGraphImagePath.charAt(0) != "/"){
				strLegacyGraphImagePath = "/" + strLegacyGraphImagePath;
			}
			strLegacyGraphImagePath = strAppropriateGraphRoot + strLegacyGraphImagePath;
			elImage.src = strLegacyGraphImagePath;
			elImage.removeAttribute("legacy-graph-image-path");
		}
	}
}