// JavaScript Document


var apirsChecked = 0;

function checkAPIRs(cb) {
	if (cb.checked) {
		if (apirsChecked == 5) cb.checked = false;
		else apirsChecked++;
	}
	else apirsChecked--;
	
	return true;
}

function submitAPIRs() {
	var chooseFunds = document.chooseFunds;
	var apirs = "";
	for (i=0; i < chooseFunds.elements.length; i++) {
		if (chooseFunds.elements[i].type == 'checkbox' && chooseFunds.elements[i].checked) {
			if (apirs.length > 0) apirs = apirs + ",";
			apirs = apirs + chooseFunds.elements[i].name;
		}
	}
	
	newurl = '/compare-charts/' + apirs + '/';
	//alert(newurl);
	window.location= newurl;
	//return false;
}

function reloadTable() {
	var sort = document.tableOptions.sort.value;
	var dir = document.tableOptions.dir.value;
	var page = document.tableOptions.page.value;
	//var perpage = document.tableOptions.perpage.value;
	var period = document.tableOptions.period.value;
	//var type = document.tableOptions.type.value;
	//var status = document.tableOptions.status.value;
	var fmid = document.tableOptions.fmid.value;
	var region = document.tableOptions.region.value;
	var sector = document.tableOptions.sector.value;
	var gearing = document.tableOptions.gearing.value;
	var index = document.tableOptions.index.value;

	//newurl = '/showtable.php';
	newurl = '/statistics/';
	//if (sort.length > 0) newurl = newurl + '?sort=' + sort;
	newurl = newurl + '?sort=' + sort;
	if (dir.length > 0) newurl = newurl + '&dir=' + dir;
	if (page.length > 0) newurl = newurl + '&page=' + page;
	//if (perpage.length > 0) newurl = newurl + '&perpage=' + perpage;
	if (period.length > 0) newurl = newurl + '&period=' + period;
	//if (type.length > 0) newurl = newurl + '&type=' + type;
	//if (status.length > 0) newurl = newurl + '&status=' + status;
	if (fmid.length > 0) {
		if  (fmid != "all") newurl = newurl + '&fmid=' + fmid;
	}
	if (region.length > 0) {
		if  (region != "all") newurl = newurl + '&region=' + region;
	}
	if (sector.length > 0) {
		if  (sector != "all") newurl = newurl + '&sector=' + sector;
	}
	if (gearing.length > 0) newurl = newurl + '&gearing=' + gearing;
	if (index.length > 0) newurl = newurl + '&index=' + index;
	
	newurl = newurl + '#stats';
	
	window.location=newurl;
}

/*

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 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 resetPeriod() {
	document.tableOptions.sort.value = "";
	document.tableOptions.dir.value = "";
	document.tableOptions.period.value = "";
}

function selectPeriod() {
	var Current = document.periodForm.period_select.selectedIndex;
	var period = document.periodForm.period_select.options[Current].value;
	if (period == "-select-") resetPeriod();
	else document.tableOptions.period.value = period;
	
	// reset sorting if changing period
	document.tableOptions.sort.value = "";
	document.tableOptions.dir.value = "";

	reloadTable();
}

function selectFinancialYear() {
	var Current = document.financialyearForm.financialyear_select.selectedIndex;
	var period = document.financialyearForm.financialyear_select.options[Current].value;
	if (period == "-select-") resetPeriod();
	else document.tableOptions.period.value = period;

	// reset sorting if changing period
	document.tableOptions.sort.value = "";
	document.tableOptions.dir.value = "";

	reloadTable();
}

function selectQuarter() {
	var Current = document.quarterForm.quarter_select.selectedIndex;
	var period = document.quarterForm.quarter_select.options[Current].value;
	if (period == "-select-") resetPeriod();
	else document.tableOptions.period.value = period;

	// reset sorting if changing period
	document.tableOptions.sort.value = "";
	document.tableOptions.dir.value = "";

	reloadTable();
}

/*
function selectType() {
	var Current = document.typeForm.type_select.selectedIndex;
	var type = document.typeForm.type_select.options[Current].value;
	if (type == "retail") type = "";
	document.tableOptions.type.value = type;

	reloadTable();
}

function selectStatus() {
	var Current = document.statusForm.status_select.selectedIndex;
	var status = document.statusForm.status_select.options[Current].value;
	if (status == "open") status = "";
	document.tableOptions.status.value = status;

	reloadTable();
}
*/

function selectFM() {
	var Current = document.fmForm.fm_select.selectedIndex;
	var fmid = document.fmForm.fm_select.options[Current].value;
	if (fmid == "all") fmid = "all";
	document.tableOptions.fmid.value = fmid;

	reloadTable();
}

function selectRegion() {
	var Current = document.regionForm.region_select.selectedIndex;
	var region = document.regionForm.region_select.options[Current].value;
	if (region == "all") region = "all";
	document.tableOptions.region.value = region;

	reloadTable();
}

function selectSector() {
	var Current = document.sectorForm.sector_select.selectedIndex;
	var sector = document.sectorForm.sector_select.options[Current].value;
	if (sector == "all") sector = "all";
	document.tableOptions.sector.value = sector;

	reloadTable();
}

function selectGearing() {
	var Current = document.gearingForm.gearing_select.selectedIndex;
	var gearing = document.gearingForm.gearing_select.options[Current].value;
	if (gearing == "all") gearing = "";
	document.tableOptions.gearing.value = gearing;

	reloadTable();
}

function selectIndex() {
	var Current = document.indexForm.index_select.selectedIndex;
	var index = document.indexForm.index_select.options[Current].value;
	if (index == "all") index = "";
	document.tableOptions.index.value = index;

	reloadTable();
}

