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 błędzie
	if (!xmlHttp)
		alert("Error przy inicjalizacji obiektu XMLHttpRequest!");
	else
		return xmlHttp;
}


function entity(str, mode) {
	str = (str) ? str : "";
	mode = (mode) ? mode : "string";

	var e = document.createElement("div");
	e.innerHTML = str;

	if (mode == "numeric") {
		return "&#" + e.innerHTML.charCodeAt(0) + ";";
	}
	else if (mode == "utf16") {
		var un = e.innerHTML.charCodeAt(0).toString(16);
		while (un.length < 4) un = "0" + un;
		return "\\u" + un;
	}
	else return e.innerHTML;
}


var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadstatustext = "<img src=\"/loading.gif\" alt=\"\" style=\"display:inline\" /> Trwa wczytywanie danych...";
var bustcacheparameter = "";


function loadPage(id,divId) {
	document.getElementById(divId).innerHTML=loadstatustext

	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1))
			document.getElementById(divId).innerHTML=xmlHttp.responseText
	}

	var url = "/funkcje/pobierz_historie_sis.php?id="+id;
	if (bustcachevar) //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()

	xmlHttp.open('GET', url+bustcacheparameter, true)
	xmlHttp.send(null)

	//Stop any link loading normaly
	return false;
}


function loadBiuletyn(id,divId) {
	document.getElementById(divId).innerHTML=loadstatustext

	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1))
			document.getElementById(divId).innerHTML=xmlHttp.responseText
	}

	var url = "/funkcje/pobierz_historie_biuletynu.php?id="+id;
	if (bustcachevar) //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()

	xmlHttp.open('GET', url+bustcacheparameter, true)
	xmlHttp.send(null)

	//Stop any link loading normaly
	return false;
}


function LoadPage2(id,divId) {
	document.getElementById(divId).innerHTML = '<img src="/img/strona/male/a/ajax_loader.gif" alt="" />';
	xmlHttp.onreadystatechange = function(){
		//Check page is completed and there were no problems.
		if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) {
			//Write data returned to page
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			//document.getElementById(divId).innerHTML = xmlHttp.responseText;
			var sis = xmlDocumentElement.getElementsByTagName("sis");
			var div = document.getElementById(divId);
			foo = '';
			//div.firstChild.nodeValue = '';
			document.getElementById(divId).innerHTML = '';
			if(sis.length>0) {
				for (var x=0; x<sis.length; x++) {

					var linek = document.createElement("a");
                    linek.setAttribute("href", "/sis/"+sis[x].childNodes[3].firstChild.nodeValue+","+sis[x].childNodes[0].firstChild.nodeValue+",,.html");
                    var nazwa = document.createTextNode(sis[x].childNodes[1].firstChild.nodeValue+" "+entity(unescape(sis[x].childNodes[2].firstChild.nodeValue)));
					linek.appendChild(nazwa);
                    div.appendChild(linek);

                    br = document.createElement('br')
                    div.appendChild(br);
				}
			}
			else {
				var nazwa = document.createTextNode("Wystąpił błąd przy pobieraniu archiwum SIS");
				div.appendChild(nazwa);
			}
		}
	}

	xmlHttp.open("GET", "/funkcje/pobierz_historie_sis.php?id="+id, true);
	xmlHttp.send(null);
	//Stop any link loading normaly
	return false;
}
