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;

	if ((method == "POST") && (headers == undefined)) {
		headers = new Array();
		headers["Content-type"] = "application/x-www-form-urlencoded";
		headers["Content-length"] = content.length;
		headers["Connection"] = "close";
 	}
 	
	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;
	var 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 Show2(_id, _show) {
    var el = document.getElementById(_id);
    if (el == null)
        return false;
        
    if (_show)
        el.style.display = 'block';
    else
        el.style.display = 'none';
}


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)
		SWindow.Open(359, 0, xmlhttp.responseText);
}

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

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 prevod_jednotky_click(_conv) {
	if (!send_xmlhttprequest(prevod_jednotky_r, 'GET', '/index.php?ma=ajax&sid=prevod_jednotky&conv=' + _conv)) {
		return true;
	}

	return false;
}

function prevod_jednotky(formular) {
	var measure = formular.measure.value;
	var from_value = formular.from_value.value;
	var from_unit = formular.from_unit.value;

//	if (!send_xmlhttprequest(prevod_jednotky_r, 'GET', '/index.php?ma=ajax&sid=prevod_jednotky&from_to=' + from_to + '&from=' + from)) {
	if (!send_xmlhttprequest(prevod_jednotky_r, 'POST', '/index.php?ma=ajax&sid=prevod_jednotky', 'measure=' + measure + '&from_value=' + from_value + '&from_unit=' + from_unit)) {
		return true;
	}

	return false;
}

function prevod_jednotky_r(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		var elpr = document.getElementById('elprevodnik');
		elpr.innerHTML = xmlhttp.responseText;
		convFx.Init('conversions', 'tabs_right', 'tabs_top');	
	}
}

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

/**
 * _el - wrap
 * _t - div s pravyma tabama 
 * _tt - div s hornima tabama 
 */ 
function Conversions() {
	var ref = this;

	this.Init = function (_el, _t, _tt) {
		this.el = SJEL.$$(_el);
		this.t =  SJEL.$$(_t);
		this.tt =  SJEL.$$(_tt);
	
		SJEL.SStyle(this.t, {display: "block"});
		this.wi = this.t.offsetWidth;
		SJEL.SStyle(this.t, {display: "none"});
	
		this.tin = SJEL.CE("div");
		this.tin = this.t.cloneNode(true);
		this.tin.id = "";
		this.tin.className = "";
		this.t.innerHTML = "";
		this.t.appendChild(this.tin);
		SJEL.SStyle(this.tin, {width: this.wi + "px", display: "block"});
	
		this.m = new SJEL.Mouse();
		this.m.OnMouseMove(OnMouseMove);
		this.mo = new SJEL.Morph("easyout");
		this.show = true;
		this.hide = true;
	}
	
	function OnMouseMove() {
		if (SJEL.IsIn(this.mmevel, ref.el)) {
			if (ref.show) {
				SJEL.SStyle(ref.t, {display: "block", borderLeft: "1px solid #afb2b5"});
				ref.mo.Init(ref.tin, {marginLeft: 0 + "px"}, 500);
				ref.mo.Morph();
				ref.mo.OnMorphFinished(function(){
					SJEL.SStyle(ref.t, {borderLeft: "none"});
				});				
				ref.show = false;
				ref.hide = true;
			}
		} else {
			if (ref.hide) {
				SJEL.SStyle(ref.t, {borderLeft: "1px solid #afb2b5"});
				ref.mo.Init(ref.tin, {marginLeft: -ref.wi + "px"}, 500);
				ref.mo.Morph();
				ref.mo.OnMorphFinished(function(){
					SJEL.SStyle(ref.t, {display: "none", borderLeft: "none"});
				});				
				ref.show = true;
				ref.hide = false;
			}
		}
	}
}




function delete_cart(xmlhttp) {
    
	if (xmlhttp.readyState == 4) {
		var cart = '';
		var count = parseInt(xmlhttp.responseXML.getElementsByTagName('count')[0].firstChild.data);

		var nodes = xmlhttp.responseXML.getElementsByTagName('cart')[0].childNodes;

		for (var i = 0; i < nodes.length; i++) {
			cart += nodes[i].data;
		}

		if (count <= 0) {
			var cartLink = SJEL.$("cart_link");
			if (cartLink && !SJEL.ClassExists(cartLink, "nodisplay"))
				SJEL.AddClass(cartLink, "nodisplay");

			document.getElementById('cart').innerHTML = "";
			document.getElementById('cart_vt').className = "ltit";
		} else {
			document.getElementById('cart_in').innerHTML = cart;
			document.getElementById('cart_vt').className = "ltit jetamneco";
		}

		document.getElementById('cart').className = '';
		document.getElementById('cart_vt_in').innerHTML = xmlhttp.responseXML.getElementsByTagName('text')[0].firstChild.data;

	}
	
}

function delete_demand(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		var demand = '';
		var count = parseInt(xmlhttp.responseXML.getElementsByTagName('count')[0].firstChild.data);

		var nodes = xmlhttp.responseXML.getElementsByTagName('cart')[0].childNodes;

		for (var i = 0; i < nodes.length; i++) {
			demand += nodes[i].data;
		}
		
		if (count <= 0) {
			var demandLink = SJEL.$("demand_link");
			if (demandLink && !SJEL.ClassExists(demandLink, "nodisplay"))
				SJEL.AddClass(demandLink, "nodisplay");

			document.getElementById('demand').innerHTML = "";
			document.getElementById('demand_vt').className = "ltit";
		} else {
			document.getElementById('demand_in').innerHTML = demand;
			document.getElementById('demand_vt').className = "ltit jetamneco";
		}

		document.getElementById('demand').className = '';
		document.getElementById('demand_vt_in').innerHTML = xmlhttp.responseXML.getElementsByTagName('text')[0].firstChild.data;

//		document.getElementById('supercartos').innerHTML = cart;
	}
}

function delete_cart_demand( id , _delete_cart ) {


	var retFunc = null;
	var sid = null;
	if (_delete_cart == "delete_cart") {
		retFunc = delete_cart;
		sid = 'delete_cart';
	} else {
		retFunc = delete_demand;
		sid = 'delete_demand';
	}
	var script = '/index.php?ma=ajax&sid=' + sid + '&drop=' + id;
//	alert(script);
	if (!send_xmlhttprequest(retFunc, 'GET', script)) {
		return true;
	}

	return false;
}

function data() {
    var ic = document.getElementById('id_ic').value;
    
	if (!send_xmlhttprequest(data_r, 'GET', '/index.php?ma=ajax&sid=ares_message&ic=' + ic)) {
		return true;
	}
	
	return false;  
}

function data_r(xmlhttp) {
    
	if (xmlhttp.readyState == 4) {
            var error = xmlhttp.responseXML.getElementsByTagName('error')[0];
            if(error != null) {
                //mattwindow_message(error.childNodes[0].data);
                SWindow.Open(416, 0, 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.type == 'checkbox' && odpovedi[i].childNodes[0].data > 0 )
        				    obj.checked = true;
        				if (obj)
        					obj.value = odpovedi[i].childNodes[0].data;
        			}
        		}
            }
	    }
	
}
function mattwindow_message(message) {
		
	var db = document.getElementById('dark_bg');
	db.style.display = 'block';
	var mw = document.getElementById('matwindow');
	mw.style.display = 'block';		
	document.getElementById('matwindow_in').style.display = 'block';
	var mi = document.getElementById('matwindow_in');
	mi.innerHTML = message;
	var kr = document.createElement("div");
	kr.className = "krizek";
	kr.onclick = function() {
		db.style.display = "none";
		mw.style.display = "none";
	}
	mi.appendChild(kr);
	//hide_selects_ie6();    
}
