if (window.XMLHttpRequest) {
	xmlhttp = new XMLHttpRequest();
} else {
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlhttp = false;
		}
	}
}

if (window.addEventListener) {
	window.addEventListener("load", setupEvents_onload, false);
	window.addEventListener("unload", setupEvents_onunload, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", setupEvents_onload);
	window.attachEvent("onunload", setupEvents_onunload);
} else {
	window.onload = setupEvents_onload;
	window.onunload = setupEvents_onunload;
}

function setupEvents_onload(evnt) {
}

function setupEvents_onunload(evnt) {
	staty_czas_na_stronie_zapisz();
}

function komentarz_zapisz(evnt) {
	var komentarz = document.getElementById('komentarz').value;
	var podpis = document.getElementById('podpis').value;
	var id_gry = document.getElementById('id_gry').value;
	var ramka = document.getElementById('ramka1');
	if (komentarz && podpis) {
		komentarz = komentarz.replace('&', '%26');
		ramka.innerHTML='<p class="gra-informacja"><img src="pics/ajax-loader.gif" alt=""> Twój komentarz jest właśnie zapisywany</p>';
		xmlhttp.open('POST', 'ajax/komentuj-gra.php', true);
		xmlhttp.onreadystatechange = komentarz_zapisany;
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send("id_gry="+id_gry+"&komentarz="+encodeURI(komentarz)+"&podpis="+encodeURI(podpis));
	} else {
		alert('Wprowadź treść komentarza i podpisz się');
	}
	return true;
}

function komentarz_zapisany() {
	var ramka = document.getElementById('ramka1');
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		ramka.innerHTML = xmlhttp.responseText;
	} else if (xmlhttp.readyState == 4) {
		ramka.innerHTML='<p class="gra-informacja">Błąd komunikacji na serwerze :(</p>';
	}
}

function ocena_zapisz(evnt) {
	var id_gry = document.getElementById('id_gry').value;
	for (i=1; i<6; i++) {
		var obj = document.getElementById('ocen'+i);
		if (obj.checked) {
			var ocena = i;
			break;
		}
	}
	if (ocena) {
		var ramka = document.getElementById('ramka2');
		ramka.innerHTML='<img src="pics/ajax-loader.gif" alt="">';
		xmlhttp.open('POST', 'ajax/ocen-gra.php', true);
		xmlhttp.onreadystatechange = ocena_zapisana;
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send("id_gry="+id_gry+"&ocena="+ocena);
	} else {
		alert('Wybierz swoją ocenę');
	}
	return true;
}

function ocena_zapisana() {
	var ramka = document.getElementById('ramka2');
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		ramka.innerHTML = xmlhttp.responseText;
	}
}

function ocena_komentarz_zapisz(komentarz_id, ocena) {
	ocena = ocena>0 ? 1 : -1;
	xmlhttp.open('POST', 'ajax/ocen-komentarz.php', true);
	xmlhttp.onreadystatechange = ocena_komentarz_zapisana;
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("id_kom="+komentarz_id+"&ocena="+ocena);
}

function ocena_komentarz_zapisana() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		alert(xmlhttp.responseText);
	}
}

function ocena_dowcip_zapisz(komentarz_id, ocena) {
	ocena = ocena>0 ? 1 : -1;
	xmlhttp.open('POST', 'ajax/ocen-dowcip.php', true);
	xmlhttp.onreadystatechange = ocena_komentarz_zapisana;
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("id_kom="+komentarz_id+"&ocena="+ocena);
}

function ocena_dowcip_zapisana() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		alert(xmlhttp.responseText);
	}
}

function staty_policja_czytaj() {
	xmlhttp.open('POST', 'ajax/staty_policja.php', true);
	xmlhttp.onreadystatechange = staty_policja_wczytana;
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send();
}

function staty_policja_wczytana() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var obj = document.getElementById('policja-pl-staty');
		if (obj) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
}

function staty_czas_na_stronie_zapisz() {
	xmlhttp.open('POST', 'ajax/staty_czas_na_stronie.php', true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send();
}
