function getWindowWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
	return window.undefined;
}

function getWindowHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return window.undefined;
}
	
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
	
	
function getPageSize(){	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}


sUrlFund = "/chart/fund.php";
sUrlNav = "/chart/nav.php";


function showFund(response) {
	var fundDiv = document.getElementById("fundDiv");
	if (fundDiv == null) {
		openPopupFund();
		var fundDiv = document.getElementById("fundDiv");
	} 
	fundDiv.innerHTML = response;
}



var handleSuccessFund = function(o){
	if(o.responseText !== undefined){
		showFund(o.responseText);
	}
};

var handleFailureFund = function(o){
	if(o.responseText !== undefined){
		alert("Connection Error");
	}
};

var callbackFund = {
  success:handleSuccessFund,
  failure:handleFailureFund,
  argument:['foo','bar']
};


function getNav(fondo,startdate,enddate,lingua) {
	var fundDiv = document.getElementById("fundDiv");
	if (fundDiv == null) {
		openPopupFund();
		var fundDiv = document.getElementById("fundDiv");
	} 
	var objOverlay = document.getElementById('overlay');
	objOverlay.style.zIndex = 80;
	objOverlay.style.visibility = "visible";
	fundDiv.style.zIndex = 100;
	fundDiv.style.visibility = "visible";
	var scrollTop = getScrollXY()[1];
	var popupPositionTop = Math.round(scrollTop + getWindowHeight()/2 - parseInt(fundDiv.style.height)/2);
	var popupPositionLeft = Math.round(getWindowWidth()/2 - parseInt(fundDiv.style.width)/2);
	fundDiv.style.top  = popupPositionTop + "px";
	fundDiv.style.left = popupPositionLeft + "px";
	fundDiv.innerHTML = "<div class=\"fundclass\"><div style=\"background-color: #ffffff;\" align=\"center\"><br>Loading...<br><br><img src" + "=\"/chart/ajax-loader.gif\"><br><br></div></div>";
	
	
	params = "lingua="+ encodeURIComponent(lingua);
	params += "&fondo=" + encodeURIComponent(fondo);
	params += "&startdate=" + encodeURIComponent(startdate);
	params += "&enddate=" + encodeURIComponent(enddate);
	
	request = YAHOO.util.Connect.asyncRequest('POST', sUrlNav, callbackFund, params);
}


function getFund(ID,settore) {	
	var fundDiv = document.getElementById("fundDiv");
	if (fundDiv == null) {
		openPopupFund();
		var fundDiv = document.getElementById("fundDiv");
	} 
	var objOverlay = document.getElementById('overlay');
	objOverlay.style.zIndex = 80;
	objOverlay.style.visibility = "visible";
	fundDiv.style.zIndex = 100;
	fundDiv.style.visibility = "visible";
	var scrollTop = getScrollXY()[1];
	var popupPositionTop = Math.round(scrollTop + getWindowHeight()/2 - parseInt(fundDiv.style.height)/2);
	var popupPositionLeft = Math.round(getWindowWidth()/2 - parseInt(fundDiv.style.width)/2);
	fundDiv.style.top  = popupPositionTop + "px";
	fundDiv.style.left = popupPositionLeft + "px";
	fundDiv.innerHTML = "<div class=\"fundclass\"><div style=\"background-color: #ffffff;\" align=\"center\"><br>Loading...<br><br><img src" + "=\"/chart/ajax-loader.gif\"><br><br></div></div>";
	
	var theURL = sUrlFund+'?ID='+ID;
	
	if (typeof(settore) != "undefined") {
		theURL += '&settore=' + encodeURIComponent(settore);
	} 
	request = YAHOO.util.Connect.asyncRequest('GET', theURL, callbackFund, '');
};

function openPopupFund() {
	
	var objBody = document.getElementsByTagName('body').item(0);
	
	var objOverlay = document.createElement('div');
	objOverlay.setAttribute('id','overlay');
	var arrayPageSize = getPageSize();
	arrayPageSize[1] += 200;
	objOverlay.style.backgroundColor = "#000000";
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = "0px";
	objOverlay.style.left = "0px";
	objOverlay.style.zIndex = 80;
	objOverlay.style.opacity = "0.8";
	objOverlay.style.filter = 'alpha(opacity=80)';
	objOverlay.style.width = arrayPageSize[0] + "px";
	objOverlay.style.height = arrayPageSize[1] + "px";
	objOverlay.onclick = function() { closePopupFund(); }
	objBody.appendChild(objOverlay);

	var objPopup = document.createElement('div');
	objPopup.setAttribute('id','fundDiv');
	objPopup.style.position = 'absolute';
	objPopup.style.width = "500px";
	objPopup.style.height = "300px";
	

	
	/*
	objPopup.style.visibility = 'visible';
	objPopup.style.zIndex = 100;
	*/
	objBody.appendChild(objPopup);
	
}

function closePopupFund() {

	var objOverlay = document.getElementById('overlay');
	var objPopup = document.getElementById('fundDiv');
	objOverlay.style.visibility = 'hidden';
	objOverlay.style.zIndex = -1;
	objPopup.style.visibility = 'hidden';
	objPopup.style.zIndex = -1;
	showSelectBoxes();
}
