// ALIEN

function ATrim (_s) {
		return _s.replace(/^\s*/, "").replace(/\s*$/, "");
}

function AClearMenu(_tid1, _tid2) {
	var ta1 = document.getElementById(_tid1);
	var ta2 = document.getElementById(_tid2);
	if (ta1 == null)
		return;

	var trs = new Array();
	var trsTmp = ta1.getElementsByTagName("TR");

	for (var i = 1; i < trsTmp.length; i++)
		trs.push(trsTmp[i]);
	
	if (ta2 != null) {
		trsTmp = ta2.getElementsByTagName("TR");
		for (var i = 0; i < trsTmp.length; i++)
			trs.push(trsTmp[i]);
	}

	for (var i = 0; i < trs.length; i++) {
		i1 = trs[i].childNodes[0].firstChild;
		i2 = trs[i].childNodes[1].firstChild;
		i3 = trs[i].childNodes[2].firstChild;
		
		if (i1.tagName == "INPUT")
			i1.value = "";
		if (i2.tagName == "INPUT")
			i2.value = "";
		if (i3.tagName == "INPUT")
			i3.value = "";
	}
}

function AParseMenu(_s, _tid1, _tid2)	 {
	var menu = _s.split('\n');
	var ta1 = document.getElementById(_tid1);
	var ta2 = document.getElementById(_tid2);
	if (ta1 == null)
		return;
	
	this.MenuItem = function(_g, _n, _p) {
		this.grams = _g;
		this.name = _n;
		this.price = _p;
	}
	
	result = new Array();
	for (var i = 0; i < menu.length; i++) {
		var m = /(\d+[\,\.]?\d*[gmksdcl]+)(.*)\s+(\d+)/i.exec(menu[i]);
		var m2 = /(.*)\s+(\d+)/i.exec(menu[i]);  // bez gramaze
		var m3 = /(\d+[\,\.]?\d*[gmksdcl]+)(.*)/i.exec(menu[i]);  // bez ceny
		if ((m != null) && (m.length > 1))
			result.push(new this.MenuItem(m[1], ATrim(m[2]), m[3]));
		else if ((m2 != null) && (m2.length > 1)) {
			result.push(new this.MenuItem("", ATrim(m2[1]), m2[2]));
		} else if ((m3 != null) && (m3.length > 1)) {
			result.push(new this.MenuItem(ATrim(m3[1]), ATrim(m3[2]), ""));
		} else
			result.push(new this.MenuItem("", ATrim(menu[i]), ""));
	}
	
	var trs = new Array();
	var trsTmp = ta1.getElementsByTagName("TR");

	for (var i = 1; i < trsTmp.length; i++)
		trs.push(trsTmp[i]);
	
	if (ta2 != null) {
		trsTmp = ta2.getElementsByTagName("TR");
		for (var i = 0; i < trsTmp.length; i++)
			trs.push(trsTmp[i]);
	}
	
	if (trs.length > 0) {
		var i1 = i2 = i3 = null;
		for (var i = 0; i < result.length; i++) {
			if (i > trs.length - 1)
				break;
			
			i1 = trs[i].childNodes[0].firstChild;
			i2 = trs[i].childNodes[1].firstChild;
			i3 = trs[i].childNodes[2].firstChild;
			
			if (i1.tagName == "INPUT")
				i1.value = result[i].grams;
			if (i2.tagName == "INPUT")
				i2.value = result[i].name;
			if (i3.tagName == "INPUT")
				i3.value = result[i].price;
		}
	}
}

// AJAX
function SendXmlHttpRequest(_func, _method, _url, _params, _content, _headers) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	if (!xmlhttp)
		return false;

	xmlhttp.open(_method, _url);

	if (_func != null)
		xmlhttp.onreadystatechange = function() {
			_func(xmlhttp, _params);
		};

	if (_headers) {
		for (var key in _headers)
			xmlhttp.setRequestHeader(key, _headers[key]);
	}

	xmlhttp.send(_content);
	return true;
}

function SendXmlHttpRequest2(_func, _method, _url, _funcParams, _params, _headers) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
	var headers = _headers;

	if (!xmlhttp)
		return false;

	if ((_method == "POST") && (_headers == undefined) && (_params != undefined)) {
		headers = new Array();
		headers["Content-type"] = "application/x-www-form-urlencoded";
		headers["Content-length"] = _params.length;
		headers["Connection"] = "close";
 	}

	xmlhttp.open(_method, _url);

	if (_func != null)
		xmlhttp.onreadystatechange = function() {
			_func(xmlhttp, _funcParams);
		};

	if (headers) {
		var ii = 0;
		for (var key in headers) {
			if (++ii > 3)
				break;
			xmlhttp.setRequestHeader(key, headers[key]);
		}
	}

	xmlhttp.send(_params);
	return true;
}

function SetContent(xmlhttp, _id) {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.responseText != null) {
//            document.getElementById(_id).innerHTML = "";
			//alert(xmlhttp.responseText);
            document.getElementById(_id).innerHTML = xmlhttp.responseText;
        }
    }
}

function ChangeContent(_idContent, _script, _method) {
    if (_method == undefined)
        _method = 'GET';
        
    if (!SendXmlHttpRequest(SetContent, _method, _script, _idContent)) {
        alert("chyba 'ChangeContent'"); 
        return false;
    }
}

function ChangeContent2(_idContent, _script, _method, _params) {
	if ((_method == undefined) || (_method == ''))
		_method = 'GET';
	
	if (!SendXmlHttpRequest2(SetContent, _method, _script, _idContent, _params)) {
		alert("chyba 'ChangeContent'"); 
		return false;
	}
}
function ShowOkresy(_r) {
	ChangeContent('okresy_cont', '/scripts/ajax_ukaz_okresy.php?r=' + _r);
}

function SetOkresySelect(_id) {
	var el = document.getElementById('okres');
	if (el == null)
		return;
		
	var opts = el.getElementsByTagName('option');
	for (var i = 0; i < opts.length; i++) {
		if (opts[i].value == _id)
			opts[i].selected = true;
	}
	
	var elf = document.getElementById('elformo');
	if (elf != null)
		elf.submit();
}

function AddToFavourite(_id) {
	ChangeContent('oblibene', '/scripts/ajax_oblibene.php?obl=' + _id);
}

function RemoveFavourite(_id) {
	ChangeContent('oblibene', '/scripts/ajax_oblibene_odebrat.php?obl=' + _id);
}

function OnOff() {
	this.conts = new Array();
	
	this.Add = function(_show, _hide, _cont) {
		var show = document.getElementById(_show);
		var hide = document.getElementById(_hide);
		var cont = document.getElementById(_cont);
		
		if (cont == null)
			return;
		
		if (show != null) {
			show.onclick = function() {
				cont.style.display = "block";
				return false;
			}
		}

		if (hide != null) {
			hide.onclick = function() {
				cont.style.display = "none";
				return false;
			}
		}

		this.conts.push(document.getElementById(_cont));
	}
	
	this.ShowAll = function() {
		for (var i = 0; i < this.conts.length; i++)
			this.conts[i].style.display = "block";
	}

	this.HideAll = function() {
		for (var i = 0; i < this.conts.length; i++)
			this.conts[i].style.display = "none";
	}
}

function SendEmailAjax(_el, _elMessage, _emails, _subject, _body) {
	var el = document.getElementById(_el);
	var elMessage = document.getElementById(_elMessage);
	if (!el || !elMessage) {
		alert("SendEmailAjax error");
		return;
	}
	
	var par = new Array();
	var inps = el.getElementsByTagName("input");
	for (var i = 0; i < inps.length; i++) {
		if (inps[i].name == _emails)
			par.push("emails=" + inps[i].value);
		else if (inps[i].name == _subject)
			par.push("subject=" + inps[i].value);
		else if (inps[i].name == _body)
			par.push("body=" + inps[i].value);
	}
	
	ChangeContent2(_elMessage, "/scripts/ajax_send_email.php", "POST", par.join('&'));
}
// END ALIEN