function toggle(obj) {
	var el = document.getElementById(obj);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
}

function showOnline(node) {
	var subMenu = node.getElementsByTagName("div")[0];
	subMenu.style.display == "none" ? subMenu.style.display = "block" : subMenu.style.display = "none";
}

var ajax = new sack();

function updateGames(location){
	ajax.reset();
	ajax.setVar("type", "update");
	ajax.requestFile = "http://rusaoc.ru/aoc/client/online_games_requests.php";
	ajax.onLoading = document.getElementById(location).innerHTML = "<p>Идет загрузка...</p>";
	ajax.method = 'post';
	ajax.element = location;
	ajax.runAJAX();
}

function getAnalyse(location){
	ajax.reset();
	ajax.setVar("gid", location);
	ajax.requestFile = "http://rusaoc.ru/aoc/client/games_analyse.php";
	ajax.method = 'post';
	ajax.runAJAX();	
}

function showStats(location,type,ell){
	ajax.reset();
	ajax.setVar("type", type);
	ajax.setVar("gid", location);
	ajax.setVar("opt", document.forms['sf_'+location].opt.value);
	ajax.requestFile = "http://rusaoc.ru/aoc/client/games_requests.php";
	ajax.method = 'post';
	ajax.element = 's_'+location;
	ajax.runAJAX();	
}

function showResults(obj,ell) {
	changeText(ell);
	var el = document.getElementsByTagName("div");
	for(var i=0; i<el.length; i++) {
		if(el[i].id == obj) {
			el[i].style.display = (el[i].style.display != 'none' ? 'none' : '' );
		}
	}
}

function changeText(el) {
	if(el.innerHTML == "показать") el.innerHTML = "скрыть";
	else if(el.innerHTML == "скрыть") el.innerHTML = "показать";
	else if(el.innerHTML == "Показать результат") el.innerHTML = "Скрыть результат";
	else if(el.innerHTML == "Скрыть результат") el.innerHTML = "Показать результат";
}

function sortStats(sort,date,type,username,needsort) {
	ajax.reset();
	ajax.setVar('sort', sort);
	ajax.setVar('date', date);
	ajax.setVar('type', type);
	ajax.setVar('username', username);
	ajax.setVar('needsort', needsort);
	if(document.forms['sort']) {
		ajax.setVar('lastsort', document.forms['sort'].sort.value);
		ajax.setVar('order', document.forms['sort'].order.value);
	}
	ajax.requestFile = 'http://rusaoc.ru/aoc/client/stats_requests.php';
	ajax.method = 'GET';
	ajax.element = 'div_stats';
	ajax.runAJAX();
}

function forumGame(gid) {
	ajax.reset();
	ajax.setVar('gid', gid);
	ajax.setVar('uid', uid);
	ajax.requestFile = 'http://rusaoc.ru/aoc/client/games_forum.php';
	ajax.method = 'POST';
	ajax.element = 'v_'+gid;
	ajax.runAJAX();
}

// загрузка информации по группам картам для игрока (playergames)
function getMapGames(uid,rtype,active) {
	ajax.reset();
	ajax.setVar('rtype', rtype);
	ajax.setVar('uid', uid);
	ajax.setVar('active', active);
	ajax.requestFile = 'http://rusaoc.ru/aoc/client/stats_requests.php';
	ajax.method = 'GET';
	ajax.element = 'maps';
	ajax.runAJAX();
}

function maps_spoiler(uid,rtype,group) {
	var groupname = '';	
	var ind = 0;
	for(var i=1; i<=8; i++) {
		groupname = "maps"+i;
		if(i == group && document.getElementById(groupname).style.display == 'none') {
			document.getElementById(groupname).style.display = 'block';
			getMapGames(uid,rtype,group);
			ind = 1;
		} else {
			document.getElementById(groupname).style.display = 'none';
		}
	}
	if(ind == 0) getMapGames(uid,rtype,0);
}

function getAlly(){
	ajax.reset();
	ajax.setVar("user", document.forms['ally'].user.value);
	ajax.setVar("ally", document.forms['ally'].allyname.value);
	ajax.requestFile = "http://rusaoc.ru/aoc/client/stats_requests.php";
	ajax.method = 'GET';
	ajax.element = 'ally';
	ajax.runAJAX();	
}

function makeGraph(){
	ajax.reset();
	ajax.setVar("user", document.forms['graph'].user.value);
	ajax.setVar("graph", document.forms['graph'].graph.value);
	ajax.requestFile = "http://rusaoc.ru/aoc/client/stats_requests.php";
	ajax.onLoading = document.getElementById('graph').innerHTML = "<p>Идет загрузка...</p>";
	ajax.method = 'GET';
	ajax.element = 'graph';
	ajax.runAJAX();	
}

function pollsUpdate(poll_id,uid) {
	var form = "pollvote"+poll_id;
	var inputs = document.forms[form].voteid;
	var selectedValue = new Array();
	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].checked) {
			selectedValue.push(inputs[i].value);
		}
	}
	ajax.reset();
	ajax.setVar("uid", uid);
	ajax.setVar("poll_id", poll_id);
	ajax.setVar("option_id", selectedValue);
	ajax.requestFile = "http://rusaoc.ru/aoc/polls_requests.php";
	ajax.method = 'POST';
	ajax.element = form;
	ajax.runAJAX();	
}
