function add_result()
{
	 postData = 'cc_playername=' + document.getElementById('cc_playername').value;
	 postData = postData + '&cc_score=' + document.getElementById('cc_score').value;
	 postData = postData + '&cc_device_id=' + document.getElementById('cc_device_id').value;
	 postData = postData + '&cc_gamename=' + document.getElementById('cc_gamename').value;
	 postData = postData + '&cc_hash=' + document.getElementById('cc_hash').value;
	 postData = postData + '&cc_prot_ver=' + document.getElementById('cc_prot_ver').value;

 // Передача данных
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	 {
		 if (req.readyState != 4) return;
		 document.getElementById('results').innerHTML = req.responseText;
		 alert('Данные добавлены!');
	 }
 //асинхронный POST-метод
	 req.open("POST", "table_model.php", true);
	 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 req.setRequestHeader("Content-Length", postData.length);
 	req.send(postData);
}

var results = false;
var show_results = function(ver)
{
    var req = getXmlHttpRequest();
    req.onreadystatechange = function()
    {
        if (req.readyState != 4) return;
        data = JSON.parse(req.responseText);
        jsonData = data.scores;
        
        /*if (false) {
        text = "<div align=\"center\" class=\"hs-bg\">"
          + "<table cellpadding=0 cellspacing=0 style=\"width: 200px;\">";
        var h = 23;
        for (i=0;i<jsonData.length;i++)
        {
            j = i+1;
            h = h == 23 ? 18 : 23;
            text += "<tr style=\"height:" + h + "px\"><td align=left width=290>" + jsonData[i].playername + "</td><td align=center width=50>" +jsonData[i].score + "</td></tr>";
        }
        text += "</table></div>";
        text += "Total players: "+data.count;
        } else {*/
        text = "<div align=\"center\" class=\"hs-bg\"><div align=\"left\"><div align=\"center\" class=\"hs-best\">";
        var h = 23;
        var first = true;
        for (i=0;i<jsonData.length;i++)
        {
            if (first)
            {
                first = false;
                text += 'Best score: '
                    + jsonData[i].score + '<br>' + jsonData[i].playername
                    + "</div></div>"
                    + "<table cellpadding=0 cellspacing=0 style=\"width: 240px;margin-top:92px\">";
            }
            j = i+1;
            h = h == 23 ? 18 : 23;
            text += "<tr style=\"height:" + h + "px\"><td align=left width=270 style=\"padding-left:10px\">" + j + ". " + jsonData[i].playername + "</td><td align=right width=50>" +jsonData[i].score + "</td></tr>";
        }
        text += "</table></div>";
        text += "<div style=\"fontfamily:verdana;font-weight:bold;font-size:1.5em\">Total players: "+data.count+"</div>";
        //}
        
        results = document.getElementById('results').innerHTML;
        document.getElementById('results').innerHTML = text;
        document.getElementById('results').className = 'main-about-mid main-about-mid-hs';
        document.getElementById('top_buttons').src = 'img/buttons_big_to_home.jpg';
        document.getElementById('scrtr').style.display = 'none';
        document.getElementById('scrnbarmid').className = 'main-tbl-highscore-mid';
        document.getElementById('pBtn').src = 'img/idiwo_pressed_to_home.png';
    }
    req.open("GET", "table_model.php?gamename=goldkeeper"+ver+"&offset=0&limit=15&from=site", true);
    req.send(null);
}
var show_home = function()
{
    document.getElementById('results').innerHTML = results;
    document.getElementById('results').className = 'main-about-mid';
    document.getElementById('top_buttons').src = 'img/buttons_big.jpg';
    document.getElementById('scrtr').style.display = document.all ? 'block' : 'table-row';
    document.getElementById('scrnbarmid').className = 'main-tbl-scrnbar-mid';
    document.getElementById('pBtn').src = 'img/idiwo_pressed.jpg';
    results = false;
}
function get_result(ver)
{
    return results ? show_home() : show_results(ver);
}

function get_main()
{
	document.getElementById('scores').innerHTML = '';
	document.getElementById('content_main').style.display = 'block';
	document.getElementById('scores').style.display = 'none';
}

function get_results(ver)
{
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text ="<img src=\"img/files_ru/to_main_button_ru.png\" onclick=\"get_main()\" style=\"cursor: pointer; position:relative;\"/>";
		text +="<div id=\"old_ver_link\" style=\"cursor: pointer; position: relative;color:#646464; font-wieght:bold; font-size:small; text-decoration:underline; font-family:arial;\" onclick=\"get_results('')\">Рекорды версии 1.5</div>";
		text +="<div id=\"hiscore\" style=\"display:block; background-image: url(img/files_ru/top_back_ru.png); background-position: top; background-repeat: no-repeat;\">";
		text += "<div style=\"position: absolute; margin-top: 54px; margin-left:293px; font-weight:bold; font-size: 18pt; font-family:arial; color:#646464;\">"+data.count+"</div>";
		text += "<br /><br /><br /><br /><br /><br /><br /><br /><br /></div>";
		text += "<div id=\"best\" style=\"margin-top:0px;\"><table border=0 cellpadding=0 cellspacing=0>";
		text += "<tr><td style=\"height:72px; width: 91px; background: URL(img/files_ru/top_L.png) top no-repeat;\"></td>";
		text += "<td style=\"background: URL(img/files_ru/top_flexible.png) top repeat-x; text-align: center; vertical-align: top;\">";
		text += "<span style=\"color:#d3d3d3; font-wieght:bold; font-size:x-large; font-family:arial;\">"+jsonData[0].playername+"</span><br>";
		text += "<span style=\"color:#646464; font-wieght:bold; font-size:22pt; font-family:arial;\">"+jsonData[0].score+"</span></td>";
		text += "<td style=\"height:72px; width: 92px; background: URL(img/files_ru/top_R.png) top no-repeat;\"></td></tr></table></div>";
		text += "<div id=\"scoretable\" style=\"background: URL(img/files_ru/table_header_ru.png) top no-repeat; margin-right:35px;font-weight:bold; font-size: large; color:#3d3d3d; margin-top:10px;\">&nbsp;";
		
		for (i=0;i<jsonData.length;i++)
		{
			j = i+1;
			if (j%2 == 0)
			{
				text += "<div style=\"background: URL(img/files_ru/line_divider.png) top no-repeat; font:16pt Tahoma bold; color:#646464;\"><table border=0 cellpadding=0 cellspacing=0 width=340><tr><td align=center width=\"100\">";
				text += j+"</td><td align=left>"+jsonData[i].playername+"</td><td align=right>"+jsonData[i].score+"</td></tr></table></div>";				
				
			}
			else
			{
				text += "<div style=\" font:16pt Tahoma bold; color:#646464;\"><table border=0 cellpadding=0 cellspacing=0 width=340><tr><td align=center width=\"100\">";
				text += j+"</td><td align=left>"+jsonData[i].playername+"</td><td align=right>"+jsonData[i].score+"</td></tr></table></div>";				
			}
		}
		
		 document.getElementById('scores').innerHTML = text;
		 document.getElementById('content_main').style.display = 'none';
		 document.getElementById('scores').style.display = 'block';
		 
	 }
	 req.open("GET", "table_model.php?gamename=Strongholds"+ver+"&offset=0&limit=15&from=site", true);
	 req.send(null);
}

function get_results_eng(ver)
{
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text ="<img src=\"img/files_us/to_main_button_us.png\" onclick=\"get_main()\" style=\"cursor: pointer; position:relative;\"/>";
		text +="<div id=\"old_ver_link\" style=\"cursor: pointer; position: relative;color:#646464; font-wieght:bold; font-size:small; text-decoration:underline; font-family:arial;\" onclick=\"get_results_eng('')\">Ver. 1.5 records</div>";
		text +="<div id=\"hiscore\" style=\"display:block; background-image: url(img/files_us/top_back_us.png); background-position: top; background-repeat: no-repeat;\">";
		text += "<div style=\"position: absolute; margin-top: 54px; margin-left:293px; font-weight:bold; font-size: 18pt; font-family:arial; color:#646464;\">"+data.count+"</div>";
		text += "<br /><br /><br /><br /><br /><br /><br /><br /><br /></div>";
		text += "<div id=\"best\" style=\"margin-top:0px;\"><table border=0 cellpadding=0 cellspacing=0>";
		text += "<tr><td style=\"height:72px; width: 91px; background: URL(img/files_us/top_L.png) top no-repeat;\"></td>";
		text += "<td style=\"background: URL(img/files_us/top_flexible.png) top repeat-x; text-align: center; vertical-align: top;\">";
		text += "<span style=\"color:#d3d3d3; font-wieght:bold; font-size:x-large; font-family:arial;\">"+jsonData[0].playername+"</span><br>";
		text += "<span style=\"color:#646464; font-wieght:bold; font-size:22pt; font-family:arial;\">"+jsonData[0].score+"</span></td>";
		text += "<td style=\"height:72px; width: 92px; background: URL(img/files_us/top_R.png) top no-repeat;\"></td></tr></table></div>";
		text += "<div id=\"scoretable\" style=\"background: URL(img/files_us/table_header_us.png) top no-repeat; margin-right:35px;font-weight:bold; font-size: large; color:#3d3d3d; margin-top:10px;\">&nbsp;";
		
		for (i=0;i<jsonData.length;i++)
		{
			j = i+1;
			if (j%2 == 0)
			{
				text += "<div style=\"background: URL(img/files_us/line_divider.png) top no-repeat;font:16pt Tahoma bold; color:#646464;\"><table border=0 cellpadding=0 cellspacing=0 width=340><tr><td align=center width=\"100\">";
				text += j+"</td><td align=left>"+jsonData[i].playername+"</td><td align=right>"+jsonData[i].score+"</td></tr></table></div>";				
				
			}
			else
			{
				text += "<div style=\"font:16pt Tahoma bold; color:#646464;\"><table border=0 cellpadding=0 cellspacing=0 width=340><tr><td align=center width=\"100\">";
				text += j+"</td><td align=left>"+jsonData[i].playername+"</td><td align=right>"+jsonData[i].score+"</td></tr></table></div>";				
			}
		}
		
		 document.getElementById('scores').innerHTML = text;
		 document.getElementById('content_main').style.display = 'none';
		 document.getElementById('scores').style.display = 'block';
		 
	 }
	 req.open("GET", "table_model.php?gamename=Strongholds"+ver+"&offset=0&limit=15&from=site", true);
	 req.send(null);
}

function getXmlHttpRequest()
{
 if (window.XMLHttpRequest)
 {
 try
 {
 return new XMLHttpRequest();
 }
 catch (e){}
 }
 else if (window.ActiveXObject)
 {
 try
 {
 return new ActiveXObject('Msxml2.XMLHTTP');
 } catch (e){}
 try
 {
 return new ActiveXObject('Microsoft.XMLHTTP');
 }
 catch (e){}
 }
 return null;
}

