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 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 ShowLoading(_el) {
	var bg = SJEL.CE("div");
	var wh = SJEL.GWindowWH();
	var l = document.createElement("img");
	document.body.appendChild(bg);
	document.body.appendChild(l);
	l.onload = function() {
		//alert(_el.href);
		window.location = _el.href;
	}
	l.src = "/images/loader2.gif";
	SJEL.SStyle(bg, {position: "fixed", zIndex: 5999, top: 0, left: 0, width: wh[0], height: wh[1], backgroundColor: "#001946", opacity: 0.7});
	SJEL.SStyle(l, {position: "fixed", zIndex: 6000, top: (wh[1] / 2) - 106, left: (wh[0] / 2) - 16});
	if (SJEL.ie == 6) {
		SJEL.SStyle(bg, {position: "absolute"});
		SJEL.SStyle(l, {position: "absolute"});
	}
}

function MCursor (_dx, _dy) {
	var ref = this;
	this.m = null;
	this.el = null;
	this.dx = _dx||10;
	this.dy = _dy||10;

	this.Show = function(_el) {
		if ((_el != undefined) && (_el != null)) {
			this.m = new SJEL.Mouse();
			this.m.OnMouseMove(OnMouseMove);
			this.el = _el;
			document.body.appendChild(this.el);
			SJEL.SStyle(this.el, {position: "absolute", left: this.m.mx + this.dx + "px", top: this.m.my + this.dy + "px"});
		}
	}

	this.Hide = function(_el) {
		if ((_el != undefined) && (_el != null) && (this.m != null)) {
			if (_el.parentNode == document.body)
				document.body.removeChild(_el);
			this.el = null;
			this.m.Close();
			delete this.m;
			this.m = null;
		}
	}

	function OnMouseMove() {
		if (ref.el != null)
			SJEL.SStyle(ref.el, {left: ref.m.mx + ref.dx + "px", top: ref.m.my + ref.dy + "px"});
	}
}

function LoaderCursor() {
	this.cur = new MCursor(16, 16);
	this.e = SJEL.CE("div");
	this.e.className = "loader";

	this.Create = function() {
		this.cur.Show(this.e);
	}

	this.Destroy = function(_xmlhttp) {
		if (_xmlhttp != undefined) {
			if (_xmlhttp.readyState == 4) {
				this.cur.Hide(this.e);
			}
		} else
			this.cur.Hide(this.e);
	}
}

function AjaxTable (_el) {
	var ref = this;
	var el = SJEL.$$(_el);
	var trs = SJEL.$T(el, "tr");
	this.link = "/index.php?ma=ajax&sid=requisition&id=";
	this.items = new Array();
	this.loaderCur = new LoaderCursor();
	
	for (var i = 0; i < trs.length; i++) {
		this.items.push(new Itemo(trs[i], i));
	}
	
	function Itemo(_tr, _id) {
		var r = this;
		this.tr = _tr;
		this.id = _id;
		this.id2 = parseInt(this.tr.id);
		this.loaded = false;
		this.loading = false;
		this.show = false;
		this.itrs = new Array();
		this.a = SJEL.$('a_' + this.id2);
		if (this.a == null)
			return
//		alert(this.a);
		
//		this.tr.onclick = function() {
		this.a.onclick = function() {
			if (r.loading)
				return false;
				
			if (!r.loaded) {
				r.loading = true;
				ref.loaderCur.Create();
				if (!send_xmlhttprequest(r.ParseXML, 'GET', ref.link + r.id2)) {
					return false;
				}
			} else
				r.Show();
				
			return false;
		}
		
		this.Show = function() {
			for (var ii = 0; ii < this.itrs.length; ii++)
				if (this.show)
					SJEL.RemoveClass(this.itrs[ii], "nodisplay");
				else
					SJEL.AddClass(this.itrs[ii], "nodisplay");
			
			this.show = !this.show;
		}
		
		this.ParseXML = function(_xmlhttp) {
			if (_xmlhttp.readyState == 4) {
				var trs = _xmlhttp.responseXML.getElementsByTagName('tr');
				for (var i = 0; i < trs.length; i++) {
					var t = SJEL.CE("tr");
					t.className = "graytr";
					
					var tds = trs[i].getElementsByTagName('td');
					for (var j = 0; j < tds.length; j++) {
						var td = SJEL.CE("td");
						if (tds[j].getAttribute("colspan") != null) {
							td.setAttribute((SJEL.ie == 6) ? "colSpan" : "colspan", tds[j].getAttribute("colspan"));
						}

						if (tds[j].firstChild != null) {
							if (tds[j].firstChild.nodeType == 3)
								td.innerHTML = tds[j].firstChild.data;
							else if (tds[j].firstChild.nodeType == 1) {
								if (tds[j].firstChild.tagName == "a") {
									var aa = SJEL.CE("a");
									aa.href = tds[j].firstChild.getAttribute("href");
									aa.className = "pdf";
									aa.innerHTML = tds[j].firstChild.childNodes[0].nodeValue;
									td.appendChild(aa);
								}
							}
						}
						
						t.appendChild(td);
					}
					
					r.itrs.push(t);
				}
				
				for (var i = 0; i < r.itrs.length; i++) {
					if (r.id + 1 == ref.items.length)
						r.tr.parentNode.appendChild(r.itrs[i]);
					else
						r.tr.parentNode.insertBefore(r.itrs[i], ref.items[r.id + 1].tr);
				}

				r.loading = false;
				r.loaded = true;
				
				ref.loaderCur.Destroy(_xmlhttp);
			}
		}
	}
}

//SJEL.OnPageLoad(function(){SJEL.CreateLog();});