// JavaScript Document

var chartLoaded = false; //Flag indicating whether our chart has loaded

/*
function showURL() {
	var apir = document.chartOptions.apir.value;
	var period = document.chartOptions.period.value;
	var type = document.chartOptions.type.value;
	var fs = document.chartOptions.fs.value;

	if (fs) {
		newurl = '/fund-fs/' + apir + '/?period=' + period + '&type=' + type + "&width=" + (getwidth() - 20) + "&height=" + (getheight() - 80);
	}
	else {
		newurl = '/fund/' + apir + '/?period=' + period + '&type=' + type + "&display=chart";
	}
	window.location= newurl;
}
*/

function showFullScreen() {
	var apir = document.chartOptions.apir.value;
	var period = document.chartOptions.period.value;
	var type = document.chartOptions.type.value;

	newurl = '/fund-fs/' + apir + '/?period=' + period + '&type=' + type + "&width=" + (getwidth() - 20) + "&height=" + (getheight() - 80);
	window.location= newurl;
}

function showNormal() {
	var apir = document.chartOptions.apir.value;
	var period = document.chartOptions.period.value;
	var type = document.chartOptions.type.value;

	newurl = '/fund/' + apir + '/?period=' + period + '&type=' + type + "&display=chart";
	window.location= newurl;
}

function switchFund() {
	var Current = document.fundForm.fund_select.selectedIndex;
	var apir = document.fundForm.fund_select.options[Current].value;
	var period = document.chartOptions.period.value;
	var type = document.chartOptions.type.value;
	var display = document.chartOptions.display.value;
	var fs = document.chartOptions.fs.value;
	
	if (fs) {
		newurl = '/fund-fs/' + apir + '/?period=' + period + '&type=' + type + "&width=" + (getwidth() - 20) + "&height=" + (getheight() - 80);
	}
	else {
		newurl = '/fund/' + apir + '/?period=' + period + '&type=' + type + "&display=" + display;
	}
	window.location= newurl;
}

function switchPrevNext(apir) {
	var period = document.chartOptions.period.value;
	var type = document.chartOptions.type.value;
	var display = document.chartOptions.display.value;
	var fs = document.chartOptions.fs.value;
	
	if (fs) {
		newurl = '/fund-fs/' + apir + '/?period=' + period + '&type=' + type + "&width=" + (getwidth() - 20) + "&height=" + (getheight() - 80);
	}
	else {
		newurl = '/fund/' + apir + '/?period=' + period + '&type=' + type + "&display=" + display;
	}
	window.location= newurl;	
}

function updatePageLink() {
	var apir = document.chartOptions.apir.value;
	var period = document.chartOptions.period.value;
	var type = document.chartOptions.type.value;
	var fs = document.chartOptions.fs.value;
	var display = document.chartOptions.display.value;
	var fundname = document.chartOptions.fundname.value;
	var today = document.chartOptions.today.value;
	

	if (fs) {
		newurl = '/fund-fs/' + apir + '/?period=' + period + '&type=' + type + '&width=' + (getwidth() - 20) + '&height=' + (getheight() - 80);
		document.getElementById('bbcode').value='[chart=CF;' + apir + ';' + period + ';' + type + ';' + today + ']' + fundname + '[/chart]';		
	}
	else if (display == "chart") {
		newurl = '/fund/' + apir + '/?period=' + period + '&type=' + type + '&display=chart';
		document.getElementById('bbcode').value='[chart=CF;' + apir + ';' + period + ';' + type + ';' + today + ']' + fundname + '[/chart]';		
	}
	else if (display == "yearly") {
		newurl = '/fund/' + apir + '/?display=yearly';
	}
	else if (display == "quarterly") {
		newurl = '/fund/' + apir + '/?display=quarterly';
	}
	else {
		newurl = '/fund/' + apir + '/';		
	}
	document.getElementById('pagelink').href=newurl;
}

function copyBBCode() {
	var bbcode = document.getElementById('bbcode').value;
	window.clipboardData.setData('Text', bbcode);
}

function switchTab(tab) {
	if (tab == 'profile') {
		document.getElementById('profilediv').style.display="block";
		document.getElementById('menuprofiletd').className="fundmenu-selected";
	}
	else {
		document.getElementById('profilediv').style.display="none";
		document.getElementById('menuprofiletd').className="fundmenu";
	}
	
	if (tab == 'chart') {
		document.getElementById('chartdiv').style.display="block";
		document.getElementById('menucharttd').className="fundmenu-selected";
	}
	else {
		document.getElementById('chartdiv').style.display="none";
		document.getElementById('menucharttd').className="fundmenu";
	}
	
	if (tab == 'yearly') {
		document.getElementById('yearlydiv').style.display="block";
		document.getElementById('menuyearlytd').className="fundmenu-selected";
	}
	else {
		document.getElementById('yearlydiv').style.display="none";
		document.getElementById('menuyearlytd').className="fundmenu";
	}
	
	if (tab == 'quarterly') {
		document.getElementById('quarterlydiv').style.display="block";
		document.getElementById('menuquarterlytd').className="fundmenu-selected";
	}
	else {
		document.getElementById('quarterlydiv').style.display="none";
		document.getElementById('menuquarterlytd').className="fundmenu";
	}
	
}

function showProfile() {
	document.chartOptions.display.value = "profile";
	updatePageLink();
	switchTab('profile');
}

function showFundChart() {
	document.chartOptions.display.value = "chart";
	updatePageLink();
	switchTab('chart');
}

function showYearly() {
	document.chartOptions.display.value = "yearly";
	updatePageLink();
	switchTab('yearly');
}

function showQuarterly() {
	document.chartOptions.display.value = "quarterly";
	updatePageLink();
	switchTab('quarterly');
}

function updateChart() {
	var apir = document.chartOptions.apir.value;
	var period = document.chartOptions.period.value;
	var type = document.chartOptions.type.value;

	if (chartLoaded) { //Update only if chart has loaded
		//Get reference to chart object using Dom ID
		var chartObj = getChartFromId("FundChart");
		var chartUrl = '/getchart.php?apir=' + apir.toUpperCase() + '&period=' + period + '&type=' + type;
		chartUrl = escape(chartUrl);
		chartObj.setDataURL(chartUrl);	
	}	
}

function selectType() {
	var Current = document.typeForm.type_select.selectedIndex;
	var type = document.typeForm.type_select.options[Current].value;
	document.chartOptions.type.value = type;
	updatePageLink();
	updateChart();
}

function selectPeriod() {
	var Current = document.periodForm.period_select.selectedIndex;
	var period = document.periodForm.period_select.options[Current].value;
	document.financialyearForm.financialyear_select.selectedIndex = 0;
	document.quarterForm.quarter_select.selectedIndex = 0;
	document.chartOptions.period.value = period;
	updatePageLink();
	updateChart();
}

function selectFinancialYear() {
	var Current = document.financialyearForm.financialyear_select.selectedIndex;
	var period = document.financialyearForm.financialyear_select.options[Current].value;
	document.periodForm.period_select.selectedIndex = 0;
	document.quarterForm.quarter_select.selectedIndex = 0;
	document.chartOptions.period.value = period;
	updatePageLink();
	updateChart();
}

function linkFinancialYear(period, index) {
	document.periodForm.period_select.selectedIndex = 0;
	document.financialyearForm.financialyear_select.selectedIndex = index;
	document.quarterForm.quarter_select.selectedIndex = 0;
	document.chartOptions.period.value = period;
	updatePageLink();
	showFundChart();
	updateChart();
}

function selectQuarter() {
	var Current = document.quarterForm.quarter_select.selectedIndex;
	var period = document.quarterForm.quarter_select.options[Current].value;
	document.periodForm.period_select.selectedIndex = 0;
	document.financialyearForm.financialyear_select.selectedIndex = 0;
	document.chartOptions.period.value = period;
	updatePageLink();
	updateChart();
}

function linkQuarter(period, index) {
	showFundChart();
	document.periodForm.period_select.selectedIndex = 0;
	document.financialyearForm.financialyear_select.selectedIndex = 0;
	document.quarterForm.quarter_select.selectedIndex = index;
	document.chartOptions.period.value = period;
	updatePageLink();
	showFundChart();
	updateChart();
}

function FC_Rendered(DOMId) {
	//This method is called whenever a FusionCharts chart is loaded.
	
	if (DOMId == "FundChart") { //Check if it's the required chart using ID
		
		chartLoaded = true;
		//Invoke updateChart() method to update chart with new data
		updateChart();
	}
	
	return true;
}


