/*
* 
* Autor: Piotr Leżoń http://www.piio.info
* This notice must stay intact
*/
var xmlHttp=createXmlHttpRequestObject();
var cache=new Array();
var adresy=new Array("91.214.237.10","91.214.237.11","91.214.237.12","91.214.237.13","91.214.237.14","91.214.237.16","91.214.237.17","91.214.237.44","91.214.237.46","91.214.237.48","91.214.237.50","91.214.237.52","91.214.237.54","91.214.237.56","91.214.237.58","91.214.237.60","91.214.237.62","91.214.237.7","91.214.237.73","91.214.237.75","91.214.237.77","91.214.237.79","91.214.237.8","91.214.237.84","91.214.237.86","91.214.237.88","91.214.237.9");
var porty=new Array(443,8074);

function createXmlHttpRequestObject()
{
    var xmlHttp;
    try
    {
    xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
    var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
	try
	{
	    xmlHttp=new ActiveXObject(XmlHttpVersions[i]);
	}
	catch(e) {}
    }
    }
    
    if (!xmlHttp)
    document.getElementById("status_container").innerHTML += "<div class=error>Błąd podczas tworzenia obiektu XMLHttpRequest</div>";
    else
    return xmlHttp;
}

function process()
{
    if(cache)
    {
    if(xmlHttp)
    {
    try
    {
	if((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length >0)
	{
	var cacheEntry=cache.shift();
	document.getElementById("status_container").innerHTML += "<div class=pobieranie>Sprawdzanie " + cacheEntry +" </div>";
	xmlHttp.open("GET",cacheEntry,true);
	xmlHttp.onreadystatechange = handleRequestStateChange;
	xmlHttp.send(null);
	}
	
    }
    catch(e)
    {
	document.getElementById("status_container").innerHTML += "<div class=error>Nie można połączyć się z serwerem: " + e.toString() + "</div>";
    }
    }
    }
}

function handleRequestStateChange()
{
    if(xmlHttp.readyState == 4)
    {
	if(xmlHttp.status == 200)
	{
	try
	{
	//document.getElementById("debug").innerHTML = xmlHttp.getResponseHeader("Content-Encoding");    
	handleServerResponse();
	}
	catch(e)
	{
	    document.getElementById("status_container").innerHTML += "<div class=error>Błąd odczytu odpowiedzi: " + e.toString() + "</div>";
	}
	}
	else
	{
	    document.getElementById("status_container").innerHTML += "<div class=error>Problem z pozyskaniem danych " + xmlHttp.statusText + "</div>";
	}
    }

}

function handleServerResponse()
{
    var xmlResponse=xmlHttp.responseXML;
    var xmlRoot=xmlResponse.documentElement;
    var adres=xmlRoot.getElementsByTagName("adres")[0].childNodes[0].nodeValue;
    var port=xmlRoot.getElementsByTagName("port")[0].childNodes[0].nodeValue;
    var status=xmlRoot.getElementsByTagName("stat")[0].childNodes[0].nodeValue;
    var html=adres + ":" + port;
    if(status != "alive")
    {
	document.getElementById("fail").innerHTML+="<div class=fail>" + html + "</div>";
    }
    else
    {
	document.getElementById("ok").innerHTML+="<div class=ok>" + html + "</div>";
    }
    document.getElementById("status_container").innerHTML += "<div class=checked>Sprawdzono " + adres + ".</div>";
}

function pokaz(id)
{
    document.getElementById(id).style.display="";
    document.getElementById("hideshow").innerHTML = "<div class=hs><a href=\"javascript:ukryj('"+id+"');\">[Ukryj status]</a></div>";
}

function ukryj(id)
{
    document.getElementById(id).style.display="none";
    document.getElementById("hideshow").innerHTML = "<div class=hs><a href=\"javascript:pokaz('"+id+"');\">[Pokaż status]</a></div>";
}

function start()
{

document.getElementById("fail").innerHTML += "Nie działają:";
document.getElementById("ok").innerHTML += "Działają:";
document.getElementById("status").innerHTML += "<div id=hideshow><div class=hs><a href=\"javascript:pokaz('status_container');\">[Pokaż status]</a></div></div>";
document.getElementById("status").innerHTML += "<div id=status_container style=display:none;></div>";
adres='servercheck.php5?adres=appmsg.gadu-gadu.pl&port=80';
cache.push(adres);
for(var a in adresy)
{
    for(var p in porty)
    {
    adres='servercheck.php5?adres='+adresy[a]+'&port='+porty[p];
    cache.push(adres);
    document.getElementById("status_container").innerHTML += "<div class=cache>Zakolejkowano " + adresy[a] + ":" + porty[p] + " do sprawdzenia.</div>";
    window.setInterval("process();",100);
    }
}
}


function debug(msg)
{
    document.getElementById("debug").innerHTML+=msg;
}

