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 = '/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, multishop) {
	var email = formular.email.value;

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

	return false;
}

function hide_selects_ie6() {
    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 get_scroll_xy () {
	var x = 0;
	var y = 0;

	if (typeof(window.pageYOffset) == 'number') {
		x = window.pageXOffset;
		y = window.pageYOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}

	return [x, y];
}

function get_win_wh() {
	var w = 0;
	var h = 0;

	if(typeof(window.innerWidth) == 'number') {
		w = window.innerWidth;
		h = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	return [w, h];
}

function open_window() {
	var mtw = document.getElementById('matwindow');
	var wh = get_win_wh();
	var ww = 500;

	document.getElementById('dark_bg').style.display = 'block';
	mtw.style.display = 'block';
	document.getElementById('matwindow_in').style.display = 'block';
	mtw.style.left = ((wh[0] - ww) / 2) + "px";
		var ofh = mtw.offsetHeight;
		if (ofh > 10)
			mtw.style.top = ((wh[1] - mtw.offsetHeight) / 2) + "px";
    reg = /^.*MSIE 6.*$/;
    if (reg.test(navigator.appVersion)) {
    	var xy = get_scroll_xy();
    	mtw.style.top = xy[1] + ((wh[1] - mtw.offsetHeight) / 2) + "px";
	}
}

function anonym_email_r(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		document.getElementById('matwindow_in').innerHTML = xmlhttp.responseText;

		hide_selects_ie6();
		open_window();
	}
}

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 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 = 100 - 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++) {
		new MSelect(sels[i]);
	}
}

function MCheckBox(_chkb, _tam) {
	this.tam = null;
	this.chkb = null;

	if (typeof(_chkb) == "string") {
		if (!(this.chkb = GetElem(_chkb))) {
			alert("MCheckBox: bad id '" + _chkb + "'");
			return;
		}
	} else
		this.chkb = _chkb;

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

	/* takhle vypada checkbox:
		<span class="nchkb [checked]">&nbsp</span>
	*/

	this.nchkb = document.createElement("span");  // drzi novy checkbox
	this.nchkb.innerHTML = "&nbsp;";
	this.nchkb.className = "nchkb";
	this.nchkb.ref = this;

	this.checked = false;
	this.value = null;
	this.onClickFunc = this.p1 = this.p2 = this.p3 = null;

	this.Set = function(_v, _c) {
		this.value = _v;
		if ((_c != undefined) && _c) {
			this.checked = true;
			AddClass(this.nchkb, "checked")
		}
	}

	if (this.chkb) {
		this.onClickFunc = this.chkb.onclick;
		this.Set(this.chkb.value, this.chkb.checked);
		this.chkb.style.display = "none";
	}

	this.nchkb.onclick = function() {
		if (this.ref.checked) {
			this.ref.checked = false;
			RemoveClass(this.ref.nchkb, "checked")
		} else {
			this.ref.checked = true;
			AddClass(this.ref.nchkb, "checked")
		}
		this.ref.chkb.checked = this.ref.checked;
		if (this.ref.onClickFunc)
			this.ref.onClickFunc(this.ref.p1, this.ref.p2, this.ref.p3);
	}

	if (this.tam == null)
		this.chkb.parentNode.insertBefore(this.nchkb, this.chkb);
	else
		this.tam.appendChild(this.nchkb);
}

/**
 * Zmeni vsechny checkboxy a input text (password) v elementu danem id _cont
 */
function ChangeCheckboxesAndITs(_cont) {
	this.cont = null;

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

	var chs = document.getElementsByTagName("input");
	for (var i = 0; i < chs.length; i++) {
		if (chs[i].type == "checkbox")
			new MCheckBox(chs[i]);
		else if ((chs[i].type == "text") || (chs[i].type == "password"))
			chs[i].className += " i_text";
	}
}

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

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

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

/**
 * Zmeni vsechny prvky v elementu danem id _cont
 */
function ChangeFormElements(_cont) {
	ChangeCheckboxesAndITs(_cont);
	ChangeSelects(_cont);
	ChangeTextareas(_cont);
}

/**
 * menu prepinatko, trida
 * _id - id obalovaciho elementu
 * _class - trida (obv. active), ktera se prida k tabu, je-li na nej kliknuto (aktivni tab)
 *        - je-li tato trida jiz pridana defaultne na tabu, pak se tento na pocatku neschova
 * _h - povoli schovavani elementu pri kliknuti na jiz vysviceny tab
 */
function MenuSwitcher(_id, _class, _h) {
	this.el = null;
	if (!(this.el = GetElem(_id))) {
		alert("MenuSwitcher: bad id '" + _id + "'");
		return;
	}

	if (_h != undefined)
		this.hideSelf = _h
	else
		this.hideSelf = false;
	this.el.className = this.el.className + " menu_switcher";

	this.classActive = _class;
	this.buts = new Array();
	this.boxes = new Array();

	this.AddPair = function (_but, _box) {
		var but = box = null;
		if (!(but = GetElem(_but))) return;
		if (!(box = GetElem(_box))) return;

		var ta = but.className.split(' ');
		if (InArray(this.classActive, ta) == -1)
			box.style.display = "none";

		but.ref = this;
		but.onclick = function () {
			var idx = -1;
			var ta = null;
			//alert(this.ref.hideSelf);
			for (var i = 0; i < this.ref.buts.length; i++) {
				if ((this.ref.buts[i] != this) || (this.ref.hideSelf && (this.ref.boxes[i].style.display == "block"))) {
					this.ref.boxes[i].style.display = "none";
					ta = this.ref.buts[i].className.split(' ');
					idx = InArray(this.ref.classActive, ta);
					if (idx != -1) {
						ta.splice(idx, 1);
						this.ref.buts[i].className = ta.join(' ');
					}
				} else {
					this.ref.boxes[i].style.display = "block";
					this.className += " " + this.ref.classActive;
				}
			}
		}

		this.buts.push(but);
		this.boxes.push(box);
	}
}

function IeLinks (_id) {
	if (!IE) return;

	var el = GetElem(_id);
	if (!el) return;

	var as = el.getElementsByTagName("a");
	for (var i = 0; i < as.length; i++) {
		as[i].onclick = function () {
			window.location=this.href;
			//return false;
		}
	}
}

function CreateBookmarkLink() {
	var title = "Eroticcity";
	var url = "http://www.eroticcity.cz";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }
