// przechowuje odwolanie do obiektu XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject(); 

// zwraca obiekt XMLHttpRequest
function createXmlHttpRequestObject() {
	var xmlHttp = false;
	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (trymicrosoft) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed) {
				xmlHttp = false;
			}  
		}
	}
	// zwraca utworzony obiekt lub wyswietla komunikat o bledzie
	if (!xmlHttp)
		alert("Blad inicjalizacji obiektu XMLHttpRequest!");
	else
		return xmlHttp;
}

// wysyla asynchroniczne zadanie protokolem HTTP korzystajac z obiektu XMLHttpRequest
function process()
{
	// kontynuuje jedynie jesli obiekt xmlHttp nie jest zajety
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
	// wykonanie polecen z tekst_na_pierwszej.php na serwerze
	xmlHttp.open("GET", "/functions/tekst_na_pierwszej.php", true);  
	// definiuje metode obslugi odpowiedzi serwera
	xmlHttp.onreadystatechange = handleServerResponse;
	// wysyla zadanie do serwera
	xmlHttp.send(null);
	}
	else {
	// jesli polaczenie jest zajete, ponawia probe po 1 sekundzie
	setTimeout('process()', 1000);
	}
}


// funkcja wykonywana automatycznie po otrzymaniu odpowiedzi z serwera
function handleServerResponse() 
{
	// kontynuuje jedynie jesli transakcja zostala zakonczona
	if (xmlHttp.readyState == 4) {
		// status 200 oznacza pomyslne ukonczenie transakcji
		if (xmlHttp.status == 200) {
			// wyodrebnia wiadomosc XML wyslana z serwera
			var xmlResponse = xmlHttp.responseXML;
			// pobiera element nadrzedny ze struktury pliku XML
			var xmlDocumentElement = xmlResponse.documentElement;

			document.getElementById('temat_p').firstChild.nodeValue = xmlDocumentElement.childNodes[0].firstChild.nodeValue;
			if(xmlDocumentElement.childNodes[1].firstChild.nodeValue=='hidden') {
				document.getElementById('zajawka_p').firstChild.style.display = 'none';
				document.getElementById('zajawka_p').firstChild.firstChild.nodeValue = '&nbsp;';
			} else {
				document.getElementById('zajawka_p').firstChild.style.display = 'block';
				document.getElementById('zajawka_p').firstChild.firstChild.nodeValue = xmlDocumentElement.childNodes[1].firstChild.nodeValue;
			}
			
			if(xmlDocumentElement.childNodes[2].firstChild.nodeValue=='hidden') {
				document.getElementById('skrot_p').firstChild.style.display = 'none';
				document.getElementById('skrot_p').firstChild.firstChild.nodeValue = '&nbsp;';
			} else {
				document.getElementById('skrot_p').firstChild.style.display = 'block';
				document.getElementById('skrot_p').firstChild.firstChild.nodeValue = xmlDocumentElement.childNodes[2].firstChild.nodeValue;
			}
			
			document.getElementById('ikona_p').setAttribute("href","http://" + xmlDocumentElement.childNodes[4].firstChild.nodeValue);
			document.getElementById('ikona_p').firstChild.setAttribute("src","./img/strony/male/" + xmlDocumentElement.childNodes[3].firstChild.nodeValue);
			document.getElementById('link_p').firstChild.setAttribute("href","http://" + xmlDocumentElement.childNodes[4].firstChild.nodeValue);
			// ponawia sekwencje
			setTimeout('process()', 10000);
		}
		// dla statusu protokolu HTTP innego niz 200 zglasza blad
		else {
			alert("Wystapil blad podczas uzyskiwania dostepu do serwera: " + xmlHttp.statusText);
		}
	}
}
