function showResults(cnt)
{
    if(document.getElementById("results"+cnt).style.display == 'none')
	    document.getElementById("results"+cnt).style.display = 'block';
	else
	    document.getElementById("results"+cnt).style.display = 'none';
}
function colorSelectionChanged(obj)
{
	if(obj == 'full')
    	GetFullCountriesList("changeColor", "");
	else
    {
		changeColor(obj, "top10men");
        changeColor(obj, "top10women");
    }
}
function speedChanged(id, id2, sex, season, lang)
{
	httpObject = getHTTPObject();
    if (httpObject != null)
    {
    	httpObject.open("GET", "include/chart_speed.php?id="+id+"&id2="+id2+"&sex="+sex+"&season="+season+"&lang="+lang, true);
        httpObject.send(null);
        httpObject.onreadystatechange = speedHandlerFunction;
        document.getElementById("speed").innerHTML = "<table width='500' height='250'><tr align=center><td><img src='../img/ajax-loader.gif'></td></tr></table>";
    }
}
function seasonSResultsChanged(id, season)
{
	httpObject = getHTTPObject();
    if (httpObject != null)
    {
    	httpObject.open("GET", "include/sportsman_results.php?id="+id+"&season="+season, true);
        httpObject.send(null);
        httpObject.onreadystatechange = resultsHandlerFunction;
        document.getElementById("results").innerHTML = "<table width='500' height='250'><tr align=center><td><img src='../img/ajax-loader.gif'></td></tr></table>";
    }
}
function speedHandlerFunction()
{
    if(httpObject.readyState == 4)
    {
        document.getElementById("speed").innerHTML = httpObject.responseText;
    }
}
function resultsHandlerFunction()
{
    if(httpObject.readyState == 4)
    {
        document.getElementById("results").innerHTML = httpObject.responseText;
    }
}
function GetFullCountriesList(id, module)
{
	httpObject = getHTTPObject();
    if (httpObject != null)
    {
    	httpObject.open("GET", "ajax_GetCountriesList.php?module="+module, true);
        httpObject.send(null);
        httpObject.onreadystatechange = function()
        {
	        if(httpObject.readyState == 4)
	        {
	            document.getElementById(id).innerHTML = httpObject.responseText;
	        }
        };
    }
}
function ShowAllSportsmen(type, table, id, sex, idcolor, season, where)
{
    httpObject = getHTTPObject();
    if (httpObject != null)
    {
        httpObject.open("GET", "ajax_ShowAllSportsmen.php?type="+type+"&table="+table+"&colordiv="+id+"&sex="+sex+"&season="+season+"&where="+where, true);
        httpObject.send(null);
        httpObject.onreadystatechange = function()
        {
   	        if(httpObject.readyState == 4)
   	        {
                generateSportsmenHTML(id, httpObject.responseText);
                changeColor(idcolor, id);
                document.getElementById(id+"_all").parentNode.removeChild(document.getElementById(id+"_all"));
   	        }
        };
    }
}
// data = cnt,country_id,image,name,points,sportsman_id,colordiv;
function generateSportsmenHTML (id, data)
{
    var table = document.getElementById(id);
    var arr = data.split('\n');
    for(var i in arr)
    {
        val = arr[i].split('\t');
        if(val[0])
        {
	        myRow = table.insertRow(-1);
	        myRow.setAttribute("id", val[1]);
	        myRow.setAttribute("class", "td_top10w");

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("align", "center");
	        myCell.setAttribute("width", "20");
	        myCell.innerHTML = val[0];

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("width", "20");
	        myCell.innerHTML = '<img style="Cursor: pointer;" onclick=\'changeColor('+val[1]+', "'+val[6]+'");\' src="uploads/'+val[2]+'">';

	        myCell = myRow.insertCell(-1);
	        myCell.innerHTML = '<a href="profile.php?id='+val[5]+'">'+val[3]+'</a>';

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("width", "20");
	        myCell.setAttribute("align", "center");
	        myCell.innerHTML = val[4];
		}
    }
}
function ShowAllSportsmenStatistics(race_id, idcolor, parm_id, loop, start, limit, delete_show_all, stage)
{
    httpObject = getHTTPObject();
    if (httpObject != null)
    {
        httpObject.open("GET", "ajax_ShowStatistics.php?race_id="+race_id+"&colordiv="+idcolor+"&parm_id="+parm_id+"&loop="+loop+"&start="+start+"&limit="+limit+"&stage="+stage, true);
        httpObject.send(null);
        httpObject.onreadystatechange = function()
        {
   	        if(httpObject.readyState == 4)
   	        {
                if(delete_show_all == 0)
	                delete_rows("statistics");
                generateSportsmenHTMLStatistics(httpObject.responseText);
                changeColorStatistics(idcolor);
                if(delete_show_all == 1)
					document.getElementById("statistics_all").style.visibility = "hidden";
                else
					document.getElementById("statistics_all").style.visibility = "visible";

	                //document.getElementById("statistics_all").parentNode.removeChild(document.getElementById("statistics_all"));

   	        }
        };
    }
}
// data = cnt,country_id,image,name,sportsman_id,colordiv,time,behind,rank,shooting;
function generateSportsmenHTMLStatistics(data)
{
    var table = document.getElementById("statistics");
    var arr = data.split('\n');
    for(var i in arr)
    {
        val = arr[i].split('\t');
        if(val[0])
        {
	        myRow = table.insertRow(-1);
	        myRow.setAttribute("id", val[1]);
	        myRow.setAttribute("class", "td_top10w");

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("align", "center");
	        myCell.setAttribute("width", "20");
	        myCell.innerHTML = val[0];

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("width", "20");
	        myCell.innerHTML = '<img style="Cursor: pointer;" onclick=\'changeColorStatistics('+val[1]+');\' src="uploads/'+val[2]+'">';

	        myCell = myRow.insertCell(-1);
	        myCell.innerHTML = '<a href="profile.php?id='+val[4]+'">'+val[3]+'</a>';

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("width", "20");
	        myCell.setAttribute("align", "center");
	        myCell.innerHTML = (val[9] ? val[9] : '');

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("width", "50");
	        myCell.setAttribute("align", "center");
	        myCell.innerHTML = val[6];

	        myCell = myRow.insertCell(-1);
	        myCell.setAttribute("width", "50");
	        myCell.setAttribute("align", "center");
	        myCell.innerHTML = (val[7] ? val[7] : '');
		}
    }
}
function delete_rows(id)
{
    var table = document.getElementById(id);
    var rows = table.getElementsByTagName("tr");
    var limit = rows.length;
    for(var i = 0; i < limit; i++)
    {
        table.deleteRow(-1);
    }
}
function changeColor(country_id, id)
{
	if(country_id == 'full')
    	GetFullCountriesList("changeColor", "results");
	else
    {
	    var table = document.getElementById(id);
	    var rows = table.getElementsByTagName("tr");
	    for(i = 0; i < rows.length; i++)
	    {
	        if(country_id == rows[i].id)
	        {
	            rows[i].className = "td_top10r";
	        }
	        else
	        {
	            rows[i].className = "td_top10w";
	        }
	    }
	    setVariable('color', country_id);
	}
}
function changeColorStatistics(country_id)
{
	if(country_id == 'full')
    	GetFullCountriesList("changeColor", "statistics");
	else
    {
	    var table = document.getElementById("statistics");
	    var rows = table.getElementsByTagName("tr");
	    for(i = 0; i < rows.length; i++)
	    {
	        if(country_id == rows[i].id)
	        {
	            rows[i].className = "td_top10r";
	        }
	        else
	        {
	            rows[i].className = "td_top10w";
	        }
	    }
	    setVariable('color', country_id);
	}
}
function getHTTPObject()
{
	if (window.ActiveXObject)
	    return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
	    return new XMLHttpRequest();
	else
	{
	    //alert("Your browser does not support AJAX.");
	    return null;
	}
}
function setVariable(variable, value)
{
	httpObject = getHTTPObject();
    if (httpObject != null)
    {
    	httpObject.open("GET", "setVariable.php?variable="+variable+"&value="+value, true);
        httpObject.send(null);
        //httpObject.onreadystatechange = setOutput;
    }
}
function setOutput()
{
	if(httpObject.readyState == 4)
    {
        document.getElementById('outputText').value = httpObject.responseText;
    }
}
