function GetAjaxResult (postdata) {
// tool function: posts postdata to the ajax parser and returns response text
	var oXMLHttp = zXmlHttp.createRequest();
	oXMLHttp.open ('POST', 'getAjax.php', false);
	oXMLHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	oXMLHttp.send (postdata);

	if (oXMLHttp.status == 200) {
		return oXMLHttp.responseText;
	} else {
		return 0;
	};
};

function GetAjaxAsync (postdata, onok) {
//tool function: posts postdata to the ajax parser, and executes onok function if ok.
	var oXMLHttp = zXmlHttp.createRequest();
	oXMLHttp.open ('POST', 'getAjax.php', true);
	oXMLHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	oXMLHttp.onreadystatechange = function () {
		if(oXMLHttp.readyState == 4){ 
			if (oXMLHttp.status == 200) {
				onok (oXMLHttp.responseText);
			} else {
				alert ("AJAX parser error - " + oXMLHttp.responseText);
			};
		};
	};
	oXMLHttp.send (postdata);

};

//functions related to Alpha layer & popup
function MoveAlpha() {
	alpha = document.getElementById ('alphaframe');
	p1 = document.getElementById ('popupouter');
	if (alpha.style.display != 'none') {
		doc = document.documentElement;
		alpha.style.top = 0;
		alpha.style.left = 0;
		alpha.style.height = (doc.scrollHeight) + 'px';
		alpha.style.width = (doc.scrollWidth) + 'px';
		if (p1) {
			p1.style.top = (doc.scrollTop) + 'px';
			p1.style.left = (doc.scrollLeft) + 'px';
			p1.style.height = (doc.clientHeight) + 'px';
			p1.style.width = (doc.clientWidth) + 'px';
		};
	};
	return false;
};
function ShowAlpha() {
	if (!document.getElementById ('alphaframe')) {
		document.body.style.zIndex=0;
		var filter = document.createElement('div');
		filter.setAttribute ('id', 'alphaframe');
		filter.innerHTML = "<div></div>";
		body = document.getElementsByTagName('body').item(0); 
		body.appendChild (filter);

		window.onscroll = MoveAlpha;
		window.onresize = MoveAlpha;
	};

	alpha = document.getElementById ('alphaframe');
	alpha.style.display='block';
	MoveAlpha();
	return false;
};

function HideAlpha() {
	alpha = document.getElementById ('alphaframe');
	alpha.style.display='none';
	return false;
};

function ShowPopup(s, w) {
	ShowAlpha();

	p = document.getElementById ('popup');
	if (!p) {
		body = document.getElementsByTagName('body').item(0); 

		newdiv = document.createElement('div');
		newdiv.setAttribute ('id',  "popupouter");
		popuptable = document.createElement('div');
		popuptable.setAttribute ('id',  "popuptable");
		popupcell = document.createElement('div');
		popupcell.setAttribute ('id',  "popupcell");
		p = document.createElement('div');
		p.setAttribute('id', "popup");

		popupcell.appendChild (p);
		popuptable.appendChild (popupcell);
		newdiv.appendChild(popuptable);
		body.appendChild (newdiv);
	};
	p.innerHTML = s;
	p1 = document.getElementById ('popupouter');
	if (w) {
		p.style.width = w + 'px';	
		p2 = document.getElementById ('popuptable');
	};
	p1.style.display='block';
	MoveAlpha();

	return false;
	
};
function HidePopup() {
	HideAlpha();
	p = document.getElementById ('popupouter'); p.style.display='none';
	return false;
};


function onok_ShowLogin(result) {ShowPopup (result, 350)};

function ShowLogin (id) {
	if (zXmlHttp.isSupported()) {
		GetAjaxAsync ('action=LOGINBOX&id='+id, onok_ShowLogin);
		return false;	
	} else {
		return true;
	};
};


function EntitiesToChars(theStr) {
	var newDiv = document.createElement(newDiv);
	newDiv.innerHTML = theStr;
	return newDiv.innerHTML;
};

function onok_DoLogon(result) {
			if (result == "OK") {
				window.location.reload();
			} else {
				alert (EntitiesToChars(result));
		};
};

function DoLogon () {
	if (zXmlHttp.isSupported()) {
		postdata = "action=DOLOGIN";
		x = document.getElementById ("user");	postdata = postdata + "&user=" + encodeURIComponent(x.value);
		x = document.getElementById ("pass");	postdata = postdata + "&pass=" + encodeURIComponent(x.value);

		result = GetAjaxResult (postdata);
		GetAjaxAsync (postdata, onok_DoLogon);
		return false;
	} else {
		return true;
	};
};

function onok_DoRegister (result) {
	alert (EntitiesToChars(result.substring(1)));
	if (result.substring(0,1) == '1') HideLogin();
};

function DoRegister () {
	if (zXmlHttp.isSupported()) {
		postdata = "action=DOREGISTER";

		x = document.getElementById ("newuser");	postdata += "&user=" + encodeURIComponent(x.value);
		x = document.getElementById ("newpass");	postdata += "&pass=" + encodeURIComponent(x.value);
		x = document.getElementById ("newname");	postdata += "&name=" + encodeURIComponent(x.value);
		x = document.getElementById ("newemail");	postdata += "&email=" + encodeURIComponent(x.value);

		GetAjaxAsync(postdata, onok_DoRegister);
		return false;
	} else {
		return true;
	};
};

function onok_DoSendPassword (result) {
	alert (EntitiesToChars(result.substring(1)));
	if (result.substring(0,1) == '1') HideLogin();
};

function DoSendPassword() {
	x = document.getElementById ("email");
	postdata = "action=DOSENDPASSWORD&email=" + encodeURIComponent(x.value);

	GetAjaxAsync(postdata, onok_DoSendPassword);
	return false;
}

function DoLogout() {
	GetAjaxAsync('action=DOLOGOUT', window.location.reload);
	return false;
};

function HideLogin () {
	HidePopup();
	return false;
};

function onok_ShowFotos(result) {
	x = document.getElementById ('fotos');
	x.innerHTML = result;
};

function ShowFotos(catid) {
	GetAjaxAsync('action=FOTOS&id=' + catid, onok_ShowFotos);
	return false;
};

function onok_ShowFoto (result) {
	if (result.substring(0,1) == '1') {
		result = result.substring(1);
		w = result.substring(0, result.indexOf(';'));
		result = result.substring (result.indexOf(';')+1);
		ShowPopup(result,w);
	} else {
		alert(result.substring(1));
	};
};

function ShowFoto(fotoid) {
	GetAjaxAsync('action=FOTO&id=' + fotoid, onok_ShowFoto);
	return false;
};
