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 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", "/audio_mute.php?mute=" + m);
}


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

	if (obj)
		obj.src = '/confirm_number.php?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) {
		document.getElementById('dark_bg').style.display = 'block';
		document.getElementById('matwindow').style.display = 'block';
		document.getElementById('matwindow_in').style.display = 'block';
		document.getElementById('matwindow_in').innerHTML = xmlhttp.responseText;

        reg = /^.*MSIE 6.*$/;
        if (reg.test(navigator.appVersion)) {
            var sel = document.body.getElementsByTagName("SELECT");
            for (var i = 0; i < sel.length; i++)
                sel[i].style.visibility = "hidden";
        }
	}
}

function close_matwindow() {
	var obj = document.getElementById('matwindow');
    var obj2 = document.getElementById('matwindow_in');
    var obj3 = document.getElementById('dark_bg');

	if (obj) {
		obj2.innerHTML = '';
		obj.style.display = 'none';
		obj2.style.display = 'none';
	}

	if (obj3)
		obj3.style.display = 'none';

    reg = /^.*MSIE 6.*$/;
    if (reg.test(navigator.appVersion)) {
        var sel = document.body.getElementsByTagName("SELECT");
        for (var i = 0; i < sel.length; i++)
            sel[i].style.visibility = "visible";
    }
}

function close_matwindow2() {
	var obj = document.getElementById('matwindow2');
    var obj2 = document.getElementById('matwindow_in2');
    var obj3 = document.getElementById('dark_bg2');

	if (obj) {
		obj2.innerHTML = '';
		obj.style.display = 'none';
		obj2.style.display = 'none';
	}

	if (obj3)
		obj3.style.display = 'none';

    reg = /^.*MSIE 6.*$/;
    if (reg.test(navigator.appVersion)) {
        var sel = document.body.getElementsByTagName("SELECT");
        for (var i = 0; i < sel.length; i++)
            sel[i].style.visibility = "visible";
    }
}


function row_highlight(_id) {
    this.wr = document.getElementById(_id);
    if (this.wr == null) {
        alert("row_highlight error: bad id");
        return false;
    }

    this.set_events = function (_elem) {
        _elem.onblur = function () {
            this.parentNode.parentNode.className = "";
        }
        _elem.onclick = function () {
            this.parentNode.parentNode.className = "highlight";
        }
    }

    this.inputs = this.wr.getElementsByTagName("INPUT");
    this.selects = this.wr.getElementsByTagName("SELECT");

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

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

/*function IE6Hover() {
    var reg = /^.*MSIE 6.*$/;
    if (!reg.test(navigator.appVersion))
        return;

    this.cssClasses = ["li0", "li1", "li2", "l0", "l1", "l2", "l3", "l4",
                       "loginek", "kosicek", "lightbox", "history", "topn"];
    var elems = document.getElementsByTagName("*");

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

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

        return found;
    }

    var s = new Array();
    var i = 0;
    var j = 0;
    for (i = 0; i < elems.length; i++) {
        if (elems[i].className != "") {
            s = elems[i].className.split(' ');
            for (j = 0; j < s.length; j++) {
                if (this.InArray(s[j], cssClasses) > -1) {
                    if (elems[i].cls == undefined)
                        elems[i].cls = new Array();
                    elems[i].cls.push(s[j]);
                }
            }

            if (elems[i].cls != undefined) {
                elems[i].cln = elems[i].className + ' ';
                elems[i].ref = this;
                elems[i].onmouseover = function() {
                    for (var k = 0; k < this.cls.length; k++)
                        this.className = this.className.replace(new RegExp(this.cls[k] + "\\s", "g"), this.cls[k] + "hover ") + ' ';
                }
                elems[i].onmouseout = function() {
                    this.className = this.cln;
                }
            }
        }
    }
}*/

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 ImgRot(_bi, _aw) {
	var ref = this;
	this.bi = SJEL.$(_bi);
	this.loader = SJEL.CE("img");
	this.loader.src = "/images/loading.gif";
	this.loader.className = "loading";
	this.slSpeed = 4000;  // slideshow speed
	this.fadeSpeed = 460;
	this.slId = 0;
	this.thmbs = new Array();
	this.mo = new SJEL.Morph();
	this.as = SJEL.$T(_aw, "img");
	this.prev = -1;

	this.OnSlTimer = function () {
		ref.ShowBigImage(++ref.slId);
	}

	this.tsl = new SJEL.Timer(this.OnSlTimer);
	this.tsl.SetSpeed(this.slSpeed);

	for (var i = 0; i < this.as.length; i++) {
		this.as[i].i = i;
		this.as[i].onclick = function() {
			ref.ShowBigImage(this.i);
			ref.Stop();
		}
		this.as[i].onmouseover = function() {
			ref.ThumbOn(this.i);
		}
		this.as[i].onmouseout = function() {
			ref.ThumbOff(this.i);
		}
	}

	this.ThumbOn = function(_i) {
		SJEL.SStyle(this.as[_i], {opacity: 0.8});
	}

	this.ThumbOff = function(_i) {
		SJEL.SStyle(this.as[_i], {opacity: 1.0});
	}

	this.ShowBigImage = function (_id) {
		var id = _id;
		if (id == this.thmbs.length)
			id = 0;

		//this.ThumbOff(this.slId);
		this.slId = id;
		//this.ThumbOn(this.slId);
		var thmb = this.thmbs[id];
		//alert(thmb.bimg.src);
		if (thmb.bimg.src != "")
			this.FadeBigImage(thmb);
		else
			thmb.LoadBigImage();
	}

	this.FadeBigImage = function (_thmb) {
		this.bi.innerHTML = "";
		this.bi.href = _thmb.tl;
		SJEL.SStyle(_thmb.bimg, {opacity: 0.0});
		this.bi.appendChild(_thmb.bimg);
		this.mo.Init(_thmb.bimg, {opacity: 1.0}, this.fadeSpeed);
		this.mo.Morph();
	}

	this.Thumb = function (_l, _s, _i) {
		this.tl = _l;  // link
		this.bis = _s;  // big image src
		this.id = _i;
		this.bimg = SJEL.CE("img");
		this.bimg.r = this;

		this.bimg.onload = function() {
			ref.ShowBigImage(this.r.id);
		}

		this.LoadBigImage = function () {
			ref.bi.innerHTML = "";
			ref.bi.appendChild(ref.loader);
			this.bimg.src = this.bis;
		}
	}

	this.AddImg = function (_l, _s) {
		this.thmbs.push(new this.Thumb(_l, _s, this.thmbs.length));
	}

	this.Start = function () {
		if (this.thmbs.length > 1)
			this.tsl.Start(true);
	}

	this.Stop = function() {
		this.tsl.Stop();
	}
}

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;
}

/**
 * Trida, simulujici select. Navaze se na normalni select, pricemz puvodni
 * select se jenom schova, takze jeho normalni funkcnost je zachovana. Co se deje s MSelectem,
 * deje se i s puvodnim selectem. (i udalost onchange).
 * MSelect se da vytvorit i normalne bez navazani na nejaky normalni select - metoda AddOption ...
 * _sel - id selectu
 * _tam - element, do ktereho mam vlozit novy select (nepovinny parametr - nezadam-li, novy select se prida pred puvodni select)
 * TODO:
 */
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 = 1000 - 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;

	if (!(this.cont = GetElem(_cont))) {
		alert("MCheckBoxes: bad id '" + _cont + "'");
		return;
	}

	var sels = document.getElementsByTagName("select");
	for (var i = 0; i < sels.length; i++) {
		if (sels[i].name != "orderkoef")
			new MSelect(sels[i]);
	}
}

/**
 * Zmeni vsechny textarea prvky v elementu danem id _cont
 */
function ChangeTextareasAndInputs(_cont) {
	this.cont = null;

	if (!(this.cont = GetElem(_cont))) {
		alert("ChangeTextareasAndInputs: bad id '" + _cont + "'");
		return;
	}

	var chs = document.getElementsByTagName("textarea");
	for (var i = 0; i < chs.length; i++) {
		chs[i].className += " i_textareaa";
	}

	var inp = document.getElementsByTagName("input");
	for (var i = 0; i < inp.length; i++) {
		if ((inp[i].type == "text") || (inp[i].type == "password"))
			inp[i].className += " i_inptext";
	}
}

/*function IeProductsZIndex(_el, _class) {
	var el = SJEL.$(_el);
	var prds = SJEL.$A(el, "class", _class);

	for (var i = 0; i < prds.length; i++) {
		SJEL.SStyle(prds[i], {position: "relative", zIndex: 100 + i});
	}
}*/

function aresCall() {
	// Coro-moro od martina
    var ic = document.getElementById('field_ic').value;

	if (!send_xmlhttprequest(aresCallResponse, 'GET', '/index.php?ma=ajax&sid=ares&ic=' + ic))
		return true;

	return false;
}

function aresCallResponse(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		var error = xmlhttp.responseXML.getElementsByTagName('error')[0];

		if (error != null) {
			alert(error.childNodes[0].data);
		} else {
			var odpovedi = xmlhttp.responseXML.getElementsByTagName('message')[0].childNodes;

			for (var i = 0; i < odpovedi.length; i++) {
				if (odpovedi[i].childNodes.length > 0) {
					var obj = document.getElementById(odpovedi[i].nodeName);

					if (obj)
						obj.value = odpovedi[i].childNodes[0].data;
				}
			}
		}
	}
}
