// Globals to keep selected continent and country.
var nGlobSelIdxContinent = -1;
var nGlobSelIdxCountry = -1;

/*document.observe("dom:loaded", function() {
	// Temporarily select national team as default.
	Event.observe(window, 'load', function() {
		//$('idRadioNational').checked = true;
		//nationalTeamSelected();
	});
});*/

function statPageLoaded(isNat, sport, continent, country, season, tournament, page) {
	// Set selections from parameters.
	nGlobSelIdxContinent = -1;
	nGlobSelIdxCountry = -1;
	$('idRadioNational').checked = isNat == 1;
	if (isNat == 1) {
		nationalTeamSelected(false);
	}
	selectOptionByVal('idListSport', sport);
	if (continent >= 0) {
		selectOptionByVal('idListContinent', continent);
		updateCountryList(false, false);
	}
	selectOptionByVal('idListCountry', country);
	if (season >= 0) {
		updateSeasonList(false, false);
	}
	selectOptionByVal('idListSeason', season);
	updateTournamentList(false, false);
	selectOptionByVal('idListTournament', tournament);
	selectOptionByVal('idListPage', page);
	selectedPage();
}

function selectOptionByVal(idSelect, value) {
	var selectedIdx = -1, idx = 0;
	$$('select#' + idSelect + ' option').each(function(o) {
      if (o.value == value) {
			o.selected = true;
			selectedIdx = idx;
			$break;
		}
		idx++;
	});
	if (selectedIdx >= 0) {
		$(idSelect).selectedIndex = selectedIdx;
	}
}

function clubTeamSelected() {
	// Enable selections.
	$('idHLCountry').className = 'HeaderLabel';
	$('idListCountry').className = 'HeaderList';
	$('idListCountry').disabled = false;
	$('idHLSeason').className = 'HeaderLabel';
	$('idListSeason').className = 'HeaderList';
	$('idListSeason').disabled = false;
	$('idHLContinent').className = 'HeaderLabel';
	$('idListContinent').className = 'HeaderList';
	$('idListContinent').disabled = false;

	// Update tournament list.
	if (nGlobSelIdxContinent >= 0 && nGlobSelIdxContinent < $('idListContinent').length) {
		$('idListContinent')[nGlobSelIdxContinent].selected = true;
		nGlobSelIdxContinent = -1;
	} else {
		$('idListContinent')[1].selected = true;
	}
	updateTournamentList();
	
	// Update country list.
	updateCountryList();
}

function nationalTeamSelected(doUpdateTournamentList) {
	// Disable selection of country, and temporarily selection of season and continent.
	$('idHLCountry').className = 'HeaderLabelDisabled';
	$('idListCountry').className = 'HeaderListDisabled';
	$('idListCountry').disabled = true;
	$('idHLSeason').className = 'HeaderLabelDisabled';
	$('idListSeason').className = 'HeaderListDisabled';
	$('idListSeason').disabled = true;
	$('idHLContinent').className = 'HeaderLabelDisabled';
	$('idListContinent').className = 'HeaderListDisabled';
	$('idListContinent').disabled = true;

	// Keep selected continent index.
	for (var nContinentSelIdx = 0, nContinents = $('idListContinent').length; nContinentSelIdx < nContinents; ++nContinentSelIdx) {
		if ($('idListContinent')[nContinentSelIdx].selected) {
			nGlobSelIdxContinent = nContinentSelIdx;
			break;
		}
	}
	// Keep selected country index.
	for (var nCountrySelIdx = 0, nCountries = $('idListCountry').length; nCountrySelIdx < nCountries; ++nCountrySelIdx) {
		if ($('idListCountry')[nCountrySelIdx].selected) {
			nGlobSelIdxCountry = nCountrySelIdx;
			break;
		}
	}

	// Update tournament list.
	$('idListContinent')[0].selected = true;
	if (doUpdateTournamentList === undefined || doUpdateTournamentList) {
		updateTournamentList();
	}
}

function selectedSport() {
	// Sport has been selected.

	// Update season list.
	updateSeasonList();

	// Update country list.
	updateCountryList();
}

function selectedContinent() {
	// Continent has been selected.

	// Update country list.
	updateCountryList();
}

function selectedCountry() {
	// Country has been selected.

	// Put back selection.
	if (nGlobSelIdxCountry >= 0 && nGlobSelIdxCountry < $('idListCountry').length) {
		$('idListCountry')[nGlobSelIdxCountry].selected = true;
		nGlobSelIdxCountry = -1;
	}
	
	// Update season list.
	updateSeasonList();
}

function selectedSeason() {
	// Season has been selected.

	// Update tournament list.
	updateTournamentList();
}

function selectedTournament() {
	// Tournament has been selected.

	// Update page list.
	var isnat = $('idRadioNational').checked ? 1 : 0;
	var sport = statGetSelectedSport();
	var country = $('idListCountry').getValue();
	if (country == null) country = '';
	var season = $('idListSeason').getValue();
	if (season == null) season = '';
	var tournament = $('idListTournament').getValue();
	var pageSel = $('idListPage').getValue();
	var params = 'isnat=' + isnat + '&sport=' + sport + '&country=' + country + '&season=' + season + '&tournament='
		+ encodeURIComponent(tournament) + '&pageSel=' + pageSel;
	new Ajax.Updater({success: 'idListPage'}, 'statupdpages.asp', {method: 'get', parameters: params, onComplete: selectedPage});
}

function selectedPage() {
	// Page has been selected.

	// Update page.
	var isnat = $('idRadioNational').checked ? 1 : 0;
	var sport = statGetSelectedSport();
	var country = $('idListCountry').getValue();
	var countryName = $('idListCountry').options[$('idListCountry').selectedIndex].text;
	var season = $('idListSeason').getValue();
	var tournament = $('idListTournament').getValue();
	var page = $('idListPage').getValue();
	var pageName = $('idListPage').options[$('idListPage').selectedIndex].text;
	var params = 'isnat=' + isnat + '&sport=' + sport + '&country=' + country + '&countryName='
		+ encodeURIComponent(countryName) + '&season=' + season + '&tournament='
		+ encodeURIComponent(tournament) + '&page=' + page + '&pageName=' + pageName;
	new Ajax.Updater(
		{
			success: 'idTablePage'
		},
		'statupdpage.asp',
		{
			method: 'get',
			parameters: params,
			onComplete: function() {
				resize_parent_iframe();
			}
		}
	);
}

function updateCountryList(select, async) {
	// Update country list.
	var continentId = $('idListContinent').getValue();
	var countrySel = $('idListCountry').getValue();
	var sportSel = statGetSelectedSport();
	var params = 'continentId=' + continentId + '&sportSel=' + sportSel + '&countrySel=' + countrySel;
	new Ajax.Updater({
		success: 'idListCountry'
	},
		'statupdcount.asp', {
			method: 'get',
			parameters: params,
			asynchronous: async === undefined ? true : async,
			onComplete: select === undefined || select ? selectedCountry : function(){}
		}
	);
}

function updateTournamentList(select, async) {
	// Update tournament list.
	var isnat = $('idRadioNational').checked ? 1 : 0;
	var sport = statGetSelectedSport();
	var country = $('idListCountry').getValue();
	if (country == null) country = '';
	var season = $('idListSeason').getValue();
	if (season == null) season = '';
	var tournamentSel = $('idListTournament').getValue();
	var params = 'isnat=' + isnat + '&sport=' + sport + '&country=' + country + '&season=' + season
		+ '&tournamentSel=' + encodeURIComponent(tournamentSel);
	new Ajax.Updater({
		success: 'idListTournament'
	},
		'statupdtourn.asp', {
			method: 'get',
			parameters: params,
			asynchronous: async === undefined ? true : async,
			onComplete: select === undefined || select ? selectedTournament : function(){}
		}
	);
}

function updateSeasonList(select, async) {
	// Update season list.
	var isnat = $('idRadioNational').checked ? 1 : 0;
	var sport = statGetSelectedSport();
	var country = $('idListCountry').getValue();
	if (country == null) country = '';
	var seasonSel = $('idListSeason').getValue();
	var params = 'isnat=' + isnat + '&sport=' + sport + '&country=' + country + '&seasonSel=' + seasonSel;
	new Ajax.Updater({
		success: 'idListSeason'
	},
		'statupdseas.asp', {
			method: 'get',
			parameters: params,
			asynchronous: async === undefined ? true : async,
			onComplete: select === undefined || select ? selectedSeason : function(){}
		}
	);
}

function statGetSelectedSport() {
	// Return selected sport.
	var sport = $('idListSport').getValue();
	while (sport.substring(sport.length - 1, sport.length) == ' ') {
		sport = sport.substring(0, sport.length - 1);
	}
	return sport;
}


