function send_xmlhttprequest(obsluha, method, url, content, headers) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	if (!xmlhttp)
		return false;

	xmlhttp.open(method, url);

	if (obsluha != null)
		xmlhttp.onreadystatechange = function() {
			obsluha(xmlhttp);
		};

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

	xmlhttp.send(content);
	return true;
}

function open_popup(dokument, sirka, vyska, scrollbars) {
	so = screen.width;
	vo = screen.height;
	zleva = ((so - sirka) / 2) - 13;
	shora = 120;
	popup_win = window.open(dokument,"popup_okno","width="+sirka+",height="+vyska+",left="+zleva+",top="+shora+",location=0,menubar=0,resizable=1,scrollbars="+scrollbars+",status=0,titlebar=0,toolbar=0");
	popup_win.focus();
}

function open_popup2(dokument, sirka, vyska, title) {
	var iframe = SJEL.CE("iframe");
	iframe.style.visibility = "hidden";  // schovame iframe, protoze behem loadingu je tam bile pozadi, ktereho se nejde zbavit
	iframe.src = dokument;
	iframe.width = sirka - 20;
	iframe.height = vyska - 78;
	iframe.setAttribute("frameborder", 0);  // ie8 to potrebuje, jinak vykresluje ramecek
	// az se iframe nahraje, tak ho zobrazime
	SJEL.AddEvent(iframe, "load", function(_e){var th = SJEL.$ET(_e); th.style.visibility = "visible";});
	SWindow.Open(sirka, vyska, iframe, 'popup_swin', title);
}

function Show(id, show) {
  if (show)
    document.getElementById(id).style.visibility='visible';
  else
    document.getElementById(id).style.visibility='hidden';
}

function mute(m) {
    if ((m == "on") || (m == "off"))
        send_xmlhttprequest(null, "GET", "/index.php?ma=amute&mute=" + m);
}


function reload_confirm_image() {
	var d = new Date();
	var obj = document.getElementById('confirm_data_image_image');

	if (obj)
		obj.src = '/index.php?ma=cnumber&new&time=' + d.getTime();
}

function change_confirm_style(second) {
	var obj1 = document.getElementById('confirm_data_image');
	var obj2 = document.getElementById('confirm_data_flash');

	if (second) {
		obj1.style.display = 'none';
		obj2.style.display = 'block';
	} else {
		obj2.style.display = 'none';
		obj1.style.display = 'block';
	}
}

function on_off(_id) {
    var elem = document.getElementById(_id);

    if (elem == null)
        return;

    if (elem.style.display == "none")
        elem.style.display = "block";
    else
        elem.style.display = "none";
}

function anonym_email(formular, profile, language) {
	var email = formular.email.value;

	if (!send_xmlhttprequest(anonym_email_r, 'GET', '/index.php?ma=ajax&sid=aemail&p=' + profile + '&l=' + language + '&e=' + email)) {
		return true;
	}

	return false;
}

function anonym_email_r(xmlhttp) {
	if (xmlhttp.readyState == 4)
		SWindow.Open(505, 0, xmlhttp.responseText);
}

function add_to_compare(m, p) {
	if (send_xmlhttprequest(add_to_compare_r, 'GET', '/index.php?ma=ajax&sid=compare&m=' + m + '&idp=' + p))
		return false;
	else
		return true;
}

function add_to_compare_r(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		var el = document.getElementById('compare_l');

		if (el) {
			el.innerHTML = xmlhttp.responseXML.getElementsByTagName('count')[0].firstChild.data;
		}

		SWindow.Open(505, 0, xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data);
	}
}

function Vyjizdec (_w, _t, _c, _t2, _ro) {
	/*if (SJEL.ie) {
		this.w = SJEL.$(_w);
		SJEL.RemoveClass(this.w, "nojs");
		var ms = new MenuSwitcher(_w, '', true);
		ms.AddPair(_t, _c);
		return;
	}*/
	this.w = SJEL.$(_w);
	this.t = SJEL.$(_t);
	this.c = SJEL.$(_c);
	this.t2 = null;
	if ((_t2 != undefined) && (_t2 != null))
		this.t2 = SJEL.$(_t2);

	this.rol = _ro||false;
	if (this.rol) {
		SJEL.SStyle(this.c, {display: "block"});
		this.cHe = this.c.offsetHeight;
		SJEL.SStyle(this.c, {display: "none"});
	}

	if (!this.w || !this.t || !this.c)
		return;

	var cc = this.c;
    this.mo = new SJEL.Morph();
    this.ch = 0;
    this.o = false;  // otevreny

	SJEL.RemoveClass(this.w, "nojs");

	this.Close = function () {
		if (this.rol)
			this.mo.Init(this.c, {opacity: 0.0, height: "0px"}, 280);
		else
			this.mo.Init(this.c, {opacity: 0.0}, 280);

		this.mo.OnMorphFinished(function(){SJEL.SStyle(cc, {display: "none"});});
		this.mo.Morph();
		this.o = false;
	}

	this.t.ref = this;
	this.t.onclick = function () {
		if (!this.ref.o) {
			SJEL.AddClass(this, "arr");
			SJEL.RemoveClass(this, "arr2");
			SJEL.SStyle(this.ref.c, {display: "block", opacity: 0.0, overflow: "hidden"});
			if (this.ref.rol) {
				SJEL.SStyle(this.ref.c, {height: "0px"});
				this.ref.mo.Init(this.ref.c, {opacity: 1.0, height: this.ref.cHe + "px"}, 280);
			} else
				this.ref.mo.Init(this.ref.c, {opacity: 1.0}, 280);
			this.ref.mo.OnMorphFinished(function(){});
			this.ref.mo.Morph();
			this.ref.o = true;
		} else {
			SJEL.RemoveClass(this, "arr");
			SJEL.AddClass(this, "arr2");
			this.ref.Close();
		}
	}

	if (this.t2 != null) {
		this.t2.ref = this;
		this.t2.onclick = function () {
			if (this.ref.o)
				this.ref.Close();
		}
	}
}

function add_messages(_id, _classes) {
	var messa = SJEL.$(_id);
	SWindow.Open(505, 0, messa, 'message_win' + _classes);
}

function ie6_hover(_id, _cl) {
	if (SJEL.ie != 6)
		return;

	var cl = SJEL.$A(_id, "class", _cl);
	for (var i = 0; i < cl.length; i++) {
		cl[i].onmouseover = function() {
			SJEL.AddClass(this, _cl + "hover");
		}
		cl[i].onmouseout = function() {
			SJEL.RemoveClass(this, _cl + "hover");
		}
	}
}

function row_highlight(_id) {
    this.wr = SJEL.$(_id);

    this.set_events = function (_elem) {
        _elem.onblur = function () {
        	var n = this.parentNode.parentNode;
        	if (n.tagName == "TD")
        		n = n.parentNode;
            SJEL.RemoveClass(n, "highlight");
        }
        _elem.onfocus = function () {
        	var n = this.parentNode.parentNode;
        	if (n.tagName == "TD")
        		n = n.parentNode;
        	if (!SJEL.ClassExists(n, "highlight")) {
				this.classAdded = true;
            	SJEL.AddClass(n, "highlight");
            }
        }
        _elem.onclick = function () {
        	var n = this.parentNode.parentNode;
        	if (n.tagName == "TD")
        		n = n.parentNode;
        	if (!SJEL.ClassExists(n, "highlight")) {
				this.classAdded = true;
	            SJEL.AddClass(n, "highlight");
	        }
        }
    }

    this.inputs = SJEL.$T(this.wr, "INPUT");
    this.selects = SJEL.$T(this.wr, "SELECT");

    for (var i = 0; i < this.inputs.length; i++)
        if ((this.inputs[i].type == "text") || (this.inputs[i].type == "password"))
            this.set_events(this.inputs[i]);

    for (var i = 0; i < this.selects.length; i++)
        this.set_events(this.selects[i]);
}

function show_box(_hide, _click) {
	var ref = this;
	this.h = SJEL.$(_hide);
	this.c = SJEL.$(_click);
	this.v = true;

	if ((this.h == null) || (this.c == null)) {
		alert('show_box error');
		return;
	}

	this.Show = function(_vis) {
		this.v = _vis;
		if (!this.v) {
			SJEL.SStyle(this.h, {display: "none"});
			if (!SJEL.ClassExists(this.c.parentNode, "hidenos"))
				SJEL.AddClass(this.c.parentNode, "hidenos");
		} else {
			SJEL.SStyle(this.h, {display: "block"});
			SJEL.RemoveClass(this.c.parentNode, "hidenos");
		}
	}

	this.Show(this.v);

	this.c.onclick = function() {
		if (this.checked) {
			SJEL.SStyle(ref.h, {display: "block"});
			SJEL.RemoveClass(this.parentNode, "hidenos");
		} else {
			SJEL.SStyle(ref.h, {display: "none"});
			if (!SJEL.ClassExists(this.parentNode, "hidenos"))
				SJEL.AddClass(this.parentNode, "hidenos");
		}
	}
}

function ajaxLogin(_sl, _t) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	if (!xmlhttp)
		return true;

	xmlhttp.open('GET', '/index.php?ma=ajax&sid=login&link=' + _sl);
	xmlhttp.onreadystatechange = function() {
		ajaxLoginShowBox(xmlhttp, _t);
	};

	xmlhttp.send();

	return false;
}

function ajaxLoginOnly(_t) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	if (!xmlhttp)
		return true;

	xmlhttp.open('GET', '/index.php?ma=ajax&sid=loginonly');
	xmlhttp.onreadystatechange = function() {
		ajaxLoginShowBox(xmlhttp, _t);
	};

	xmlhttp.send();

	return false;
}

function ajaxLoginShowBox(_x, _t) {
	if (_x.readyState == 4) {
		SWindow.Open(505, 0, _x.responseText, '', _t);
	}
}
function MSelect(_sel,_tam) {
	// budu ukladat jednotlive instance kvuli situaci, kdyz je otevreny nejaky select
	// a ja otevru jiny, tak aby se ten puvodni zavrel
	if (MSelect.instances == undefined)
		MSelect.instances = new Array();

	if (MSelect.numInstances == undefined)
		MSelect.numInstances = 0;
	else
		MSelect.numInstances++;

	this.id = MSelect.numInstances;
	MSelect.instances[this.id] = this;

	if (MSelect.opened == undefined)
		MSelect.opened = -1;  // opened - id otevreneho selectu, -1 => zadny neni otevreny

	this.tam = null;
	this.sel = null;

	if (typeof(_sel) == "string") {
		if (!(this.sel = GetElem(_sel))) {
			alert("MSelect: bad id '" + _sel + "'");
			return;
		}
	} else
		this.sel = _sel;

	if ((_tam != undefined) && (typeof(_tam) == "string")) {
		if (!(this.tam = GetElem(_tam))) {
			alert("MCheckBox: bad id '" + _tam + "'");
			return;
		}
	}

	this.GetOptionIndex = function(_opts, _opt) {
		var idx = -1;
		var cn = _opts.childNodes;
		for (var i = 0; i < cn.length; i++)
			if (cn[i] == _opt)
				idx = i;
		return idx;
	}

	this.selected = null;
	this.firstOption = true;
	this.onChangeFunc = this.p1 = this.p2 = this.p3 = null;

	if (this.sel) {
		this.onChangeFunc = this.sel.onchange;
		this.sel.style.display = "none";
	}

	/* takhle vypada select:
		<div class="nsel">
			<div class="sel_title">
				<div class="item"></div>
				<div class="decor"></div>
			</div>
			<div class="options">
				<div class="item [selected]"></div>
				...
			</div>
		</div>
	*/

	//this.tam.style.display = "block";
	this.nsel = document.createElement("div");  // drzi novy select
	this.nsel.className = "nsel";

	// toto je kvuli zasranym IE6, IE7, ..., jinak by 1 select neprekryl 2.
	// samozrejme to nebude fungovat ve vsech pripadech
	/*if (IE) {
		this.nsel.style.position = "relative";
		this.nsel.style.zIndex = 2 - this.id;
	}*/

	this.options = document.createElement("div");
	this.options.className = "options";
	this.selTitle = document.createElement("div");
	this.selTitle.className = "sel_title";
	this.selItem = document.createElement("div");
	this.selItem.className = "item";
	this.selDecor = document.createElement("div");
	this.selDecor.className = "decor";

	// je-li na puvodnim selectu trida, vytahne z ni sirku a nastavi ji
	// neni-li v te classe zadana sirka, tak v normalnich prohlizecich vezme puvodni sirku!
	if (this.sel && this.sel.className) {
		var w = parseInt(GetStyle(this.sel, "width"));
		if (w) {
			this.nsel.style.width = w + "px";
			this.options.style.width = w - 2 + "px";
		}
	}

	this.GetSelectedValue = function() {
		return this.selected.value;
	}

	this.SetOnChange = function(_func, _p1, _p2, _p3) {
		if (typeof(_func) == "function") {
			this.onChangeFunc = _func;
			this.p1 = _p1;
			this.p2 = _p2;
			this.p3 = _p3;
		}
	}

	this.Select = function(_opt) {
		RemoveClass(this.selected, "selected");
		AddClass(_opt, "selected");
		this.selItem.innerHTML = _opt.innerHTML;
	}

	// _v - value, _t - text (innerHTML), _s - selected, _d - disabled
	this.AddOption = function(_v, _t, _s, _d) {
		var it = document.createElement("div");
		it.ref = this;
		it.className = "item";
		it.value = _v;
		if (_d != undefined)
			it.disabled = _d;
		else
			it.disabled = false;

		if (it.disabled)
			AddClass(it, "disabled");

		if (!_t)
			it.innerHTML = "&nbsp";
		else
			it.innerHTML = _t;

		if (this.firstOption) {
			this.Select(it);
			this.selected = it;
			this.firstOption = false;
		}
		if (((_s != undefined) && (_s == true))) {
			this.Select(it);
			this.selected = it;
		}
		it.onclick = function() {
			if (this.disabled)
				return;

			MSelect.opened = -1;
			this.ref.Select(this);
			this.ref.options.style.display = "none";
			if (this.ref.sel)
				this.ref.opts[this.ref.GetOptionIndex(this.ref.options, this.ref.selected)].selected = false;
			this.ref.selected = this;
			if (this.ref.sel)
				this.ref.opts[this.ref.GetOptionIndex(this.ref.options, this.ref.selected)].selected = true;
			if ((this.ref.onChangeFunc != undefined) && (this.ref.p1 != null))
				this.ref.onChangeFunc(this.ref.p1, this.ref.p2, this.ref.p3);
			else if (this.ref.onChangeFunc != undefined)
				this.ref.onChangeFunc();
		}
		it.onmouseover = function() {
			if (!this.disabled)
				AddClass(this, "item_over");
		}
		it.onmouseout = function() {
			RemoveClass(this, "item_over");
		}
		this.options.appendChild(it);
	}

	this.RemoveAllOptions = function() {
		this.options.innerHTML = "";
		this.selItem.innerHTML = "";
		this.firstOption = true;
	}

	if (this.sel) {
		this.opts = this.sel.getElementsByTagName("option");
		for (var i = 0; i < this.opts.length; i++) {
			var opt = this.opts[i];
			this.AddOption(opt.value, opt.innerHTML, opt.selected, opt.disabled);
		}
	}

	this.selTitle.ref = this;
	this.selTitle.onclick = function() {
		if ((this.ref.options.style.display == "") || (this.ref.options.style.display == "none")) {
			this.ref.options.style.display = "block";
			if (MSelect.opened > -1)
				MSelect.instances[MSelect.opened].options.style.display = "none";  // zavreme otevreny select
			MSelect.opened = this.ref.id;
		} else {
			this.ref.options.style.display = "none";
			MSelect.opened = -1;
		}
	}
	this.selTitle.onmouseover = function() {
		AddClass(this, "title_over");
	}
	this.selTitle.onmouseout = function() {
		RemoveClass(this, "title_over");
	}

	this.selTitle.appendChild(this.selItem);
	this.selTitle.appendChild(this.selDecor);
	this.nsel.appendChild(this.selTitle);
	this.nsel.appendChild(this.options);
	if (this.tam == null)
		this.sel.parentNode.insertBefore(this.nsel, this.sel);
	else
		this.tam.appendChild(this.nsel);
}

/**
 * Zmeni vsechny selecty v elementu danem id _cont
 */
function ChangeSelects(_cont) {
	this.cont = null;

	var sel = document.getElementById("changesearch");
	new MSelect(sel);
}

function GetElem(_id) {
	var e = document.getElementById(_id);
	if (e == null) {
		//alert("bad id '" + _id +"'");
		return null;
	} else
		return e;
}

// vrati index prvniho nalezeneho prvku, jinak -1
function InArray(_item, _array) {
    var i = 0, f = -1;

    for (i = 0; i < _array.length; i++)
        if (_item == _array[i]) {
            f = i;
            break;
        }

    return f;
}

function AddClass(_el, _cl) {
	if (_el != null)
		_el.className += " " + _cl;
}

function RemoveClass(_el, _cl) {
	if (_el == null) return;
	var ta = _el.className.split(' ');
	var idx = InArray(_cl, ta);
	if (idx != -1) {
		ta.splice(idx, 1);
		_el.className = ta.join(' ');
	}
}

var rsdfsdf = /^.*Microsoft.*$/;
var IE = 0;
if (rsdfsdf.test(navigator.appName))
	IE = 1;
/**
 * Vrati externi styl prvku
 */
function GEStyle(_e) {
	if (!IE)
		return document.defaultView.getComputedStyle(_e, "");
	else
		return _e.currentStyle;
}

/**
 * Vrati styl prvku, bud inline nebo externi.
 */
function GetStyle(_e, _s) {
	if ((_e.style[_s] != "") && (_e.style[_s] != undefined))
		return _e.style[_s];
	else {
	 	var es = GEStyle(_e);
		if ((es[_s] != "") && (es[_s] != undefined))
			return es[_s];
	}

	return 0;
}
