var xmlHttp;

function ajax(id, page) {
	xmlHttp = createXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Jūsų naršyklė nepalaiko AJAX./nPrašome naudoti naujausias naršyklių versijas./n/n"+
					 "Your browser doesn't support AJAX./nPlease use latest version of Your browser.");
		return;
	}
	var url = "response.php?id="+id+"&page="+page;

	add_indicator();

	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", url+"&type=content", true);
	xmlHttp.send(null);
};

function load_images(id) {
	xmlHttp = createXmlHttpObject();
	add_indicator();
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", "response.php?id="+id+"&type=gallery", true);
	xmlHttp.send(null);
};

function load_article(id) {
	xmlHttp = createXmlHttpObject();
	add_indicator();
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", "response.php?id="+id+"&type=article", true);
	xmlHttp.send(null);
};

function send(type) {
	if (valid()) {
		add_indicator();
		str = "name=" + encodeURIComponent(document.getElementById("name").value.replace(/\\/g, "\\\\")) + "&" +
					"email=" + encodeURIComponent(document.getElementById("email").value.replace(/\\/g, "\\\\")) + "&" +
					"message=" + encodeURIComponent(document.getElementById("message").value.replace(/\\/g, "\\\\"));
		xmlHttp = createXmlHttpObject();
		xmlHttp.onreadystatechange = stateChanged;
		xmlHttp.open("POST", "response.php?type="+type, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send(str);
	}
};

function stateChanged() {
	if (xmlHttp.readyState == 4) {
		xmlDoc = xmlHttp.responseXML;
		if (xmlDoc.getElementsByTagName('menu_name').length != 0) {
			document.getElementById("menu_name").innerHTML = decode_special_xml_entities(xmlDoc.getElementsByTagName('menu_name')[0].childNodes[0].nodeValue);
		}
		content_str = "";
		for (i = 0; i < xmlDoc.getElementsByTagName('content')[0].childNodes.length; i++) { // nes firefox turi 4096kb limita vienam node
			content_str = content_str + xmlDoc.getElementsByTagName('content')[0].childNodes[i].nodeValue;
		}
		document.getElementById("content").innerHTML = decode_special_xml_entities(content_str);
		heights();
		remove_indicator();
		window.scrollTo(0, 0);
	}
};

function createXmlHttpObject() {
	var object = null;
	try {
		object = new XMLHttpRequest();
	}
	catch (e) {
		try {
			object = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			object = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return object;
};

function decode_special_xml_entities(str) {
	str = str.replace(/_lt_/g, "<");
	str = str.replace(/_gt_/g, ">");
	str = str.replace(/_apos_/g, "'");
	str = str.replace(/_quot_/g, "\"");
	str = str.replace(/_amp_/g, "&");
	return str;
};