// FG - Ajax
function obj(id) {
	return document.getElementById(id);
}
function bool(value) {
	return new Boolean(value);
}
function tagValue(obj,name) {
	return obj.getElementsByTagName(name)[0].firstChild.nodeValue;
}
function ajaxRequest(method,url,parseFunc,async) {
	var t = this;
	method = method.toUpperCase();
	t.parseFunc = parseFunc;
	if (!async) { async = true; }
	if (window.XMLHttpRequest) { t.ajax = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { t.ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
	t.processRequest = processRequest;
	t.parseXML = parseXML;
	t.ajax.onreadystatechange = function() { t.processRequest(t.ajax); }
	t.ajax.open(method,url,async);
	if (method == "POST") {
		t.ajax.setRequestHeader("Connection","close");
		t.ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		t.ajax.setRequestHeader("Method","POST " + url + "HTTP/1.1");
	}
	t.ajax.send(null);
}
/*
readyState Status Codes:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
*/
function processRequest(o) {
	if (o.readyState == 4) {
		if (o.status != 200) {
//			alert (o.status + " " + o.statusText);
			this.parseFunc = "Error";
		}
		this.parseXML();
	}
}
function parseXML() {
	eval("parse"+this.parseFunc+"(this.ajax)");
}
function response(o) {
	var respXML = o.responseXML;
	var respText = o.responseText;
	var resp = respXML;
	if (window.XMLHttpRequest) { resp = (new DOMParser()).parseFromString(respText,"text/xml"); }
	return resp;
}

// -------------------------------------------

function initAjax() {
	getLoginStatus();
}

// Login status
// Data - [nick]
var loginProcessUrl = "https://"+window.location.host+"/srv/www/cs/user/loginProcess.do";
var loginStatusUrl = "/srv/www/cs/user/loginStatus.do?"+Math.random();
function getLoginStatus() {
	new ajaxRequest("get",loginStatusUrl,"Status");
}
function parseStatus(o) {
	var str = o.responseText;
	
	var htmlCode = '';
	if (str != "") {
		// Logged in
//		htmlCode += '<div>\n';
		htmlCode += '<strong class="title" onclick="toggleTab(\'r-login\')"><span>Přihlášený jako </span>' +str+ '</strong>\n';
		htmlCode += '<div class="cont">\n';
		htmlCode += '	<a href="/srv/www/cs/user/settings.do" class="noBlock">Nastavení</a><span> | </span>\n';
		htmlCode += '	<a href="/srv/www/cs/user/logout.do" class="noBlock">Odhlásit</a>\n';
		htmlCode += '</div>\n';
//		htmlCode += '</div>\n';
	} else {
		htmlCode += '<form name="LoginForm" method="post" action="'+loginProcessUrl+'">\n';
		htmlCode += '<strong class="title" onclick="toggleTab(\'r-login\')">Přihlášení</strong>\n';
		htmlCode += '<div class="cont">\n';
		htmlCode += '	<input type="text" name="nick" value="Nick" class="text" onfocus="clearInput(this)">\n';
		htmlCode += '	<input type="password" name="password" value="Heslo" class="text" onfocus="clearInput(this)">\n';
		htmlCode += '	<input type="submit" value="OK" class="btn"><span class="hide"> | </span>\n';
		htmlCode += '	<a href="/srv/www/cs/user/registration.do">Registrace</a>\n';
		htmlCode += '	<div class="clr"></div>\n';
		htmlCode += '</div>\n';
		htmlCode += '</form>\n';
	}
	obj("userStatus").innerHTML = htmlCode;
}