
	var sAgent = navigator.userAgent.toLowerCase();

	function isIe() {
		return (!isPresto() && !isGecko() && document.all) ? true : false;
	}

	function isIe6() {
		return (isIe() && document.all && /MSIE (6)/.test(navigator.userAgent)) ? true : false;
	}

	function isGecko() {
		return sAgent.match(/gecko/i);
	}

	function isPresto() {
		return sAgent.match(/presto/i);
	}
	
	function createMethodReference(object, method) {
		return function () {
			return method.apply(object, arguments);
		};
	}
	
	function dbg(v) {
		return debug(v);
	}
	
	function debug(v) {
		try {
			if (is_def(console))
				console.info(v);
		} catch (e) {
		}
	}
	
	function replaceAll(strTarget, strSubString, strText) {
		var intIndexOfMatch = strText.indexOf(strTarget);
		
		while (intIndexOfMatch != -1) {
			strText = strText.replace(strTarget, strSubString);
			intIndexOfMatch = strText.indexOf(strTarget);
		}
		
		return strText;
	}

	var IE = (typeof document.all != 'undefined' && !(typeof window.opera == 'Opera'));
	//if (!IE) document.captureEvents(Event.MOUSEMOVE);
	var tempX, tempY;
	
	var attachEvent = function (o, sEventName, oCallback) {
		if (typeof o.addEventListener != 'undefined') {
		  o.addEventListener(
			sEventName,
			oCallback,
			false
		  );
		} else if (typeof o.attachEvent != 'undefined') {
		  o.attachEvent(
			'on' + sEventName,
			oCallback
		  );
		}
	};
	
	var _mouse = {x:0, y:0};
	function getMouseXY() {
		return _mouse;
	}
	
	function getBodyElement() {
		if (document.documentElement) {
			return document.documentElement;
		} else {
			return document.body;
		}
	}
	
	if (document) {
		attachEvent(document, 'mousemove', function (e) {
			if (isIe()) {
				_mouse = {
					x: e.clientX + getBodyElement().scrollLeft,
					y: e.clientY + getBodyElement().scrollTop
				};
			} else if (e && e.pageX) { 
				_mouse = {
					x: e.pageX,
					y: e.pageY
				};
			} else {
				_mouse = {x:0, y:0};
			}
		});
	}
	
	function get_mouse() {
		return getMouseXY();
	}
	
	function getElementsByClass(searchClass,node,tag) {
		if (node == null)
			node = document;
			
		if (document.getElementsByClassName) 
			return node.getElementsByClassName(searchClass);
		
		var classElements = new Array();
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		//var pattern = new RegExp("(^|[ 	]*)"+searchClass+"([ 	]*|$)");
		for (i = 0, j = 0; i < elsLen; i++) {
			//if ( pattern.test(els[i].className) ) {
			if (is_subclass_of(els[i], searchClass)) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}

	function is_subclass_of(element, className) {
		return (element.className == className || element.className.indexOf(" "+className) !== -1 || element.className.indexOf(className+" ") !== -1);
		className = new String(className);
		className = className.replace(/-/, '\\-');
		
		var pattern = new RegExp("(^|[ 	]+)"+className+"([ 	]+|$)");
		return pattern.test(element.className);
		
		return (new String(element.className)).match("/(^|[ 	]*)" + className + "([ 	]*|$)/");
	}

	function $(id) {
		return document.getElementById(id);
	}

	function n(type, id, count) {
		if (count > 0) {
			var res = [];
			for (var i = 0; i < count; i ++) {
				res.push(n(type, id?id+i:null));
			}
			return res;
		} else {
			var o = document.createElement(type);
			if (!id) 
				id = 'object' + (new Date()).getTime();
			o.setAttribute('id', id);
			
			return o;
		}
	}

	function getAC() {
		var res;
		if (window.XMLHttpRequest) {
			try {
				res = new XMLHttpRequest();
		   } catch(e) {
				res = false;
		   }
		} else if (window.ActiveXObject) {
		   try {
			res = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				try {
					res =  new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					res = false;
				}
			}
		}

		return res;
	}

	var TConnection = function (sUrl, fOnData, settings) {
		this.onData = null;
		this.oConn = getAC();
		this.sMethod = 'GET';
		this.sUrl = '';
		this.status = 'none';
		this.aPost = {};
		this.aSettings = {
			timeout: 1000,
			queue: null,
			status_point: null
		};

		this.partialData = null;
		
		if (typeof settings == 'object') {
			for (k in settings) {
				if (typeof this.aSettings[k] != 'undefined') {
					this.aSettings[k] = settings[k];
				}
			}
		}
		
		this.oConn.onreadystatechange = createMethodReference(this, function () {
			if (this.oConn.readyState == 4) {
				if (this.onData) 
					this.onData(this.oConn, this);
			}
		});
		
		this.onData = fOnData;
		
		if (sUrl) {
			this.sUrl = sUrl;
			this.open();
		}
	};
	TConnection.prototype.onData = null;
	TConnection.prototype.sMethod = 'GET';
	TConnection.prototype.sUrl = '';
	TConnection.prototype.status = '';
	TConnection.prototype.bCacheProtection = true;
	TConnection.prototype.aPost = {};

	TConnection.prototype.addPost = function (k, v) {
		this.aPost[k] = v;
	};

	TConnection.prototype.cacheProtect = function () {
		if (this.sUrl.match(/_t=[0-9]+/)) {
			this.sUrl.replace(/_t=[0-9]+/, '_t' + (new Date()).getTime());
		} else {
			this.sUrl += (this.sUrl.match(/\?/) ? '&' : '?') + '_t=' + (new Date()).getTime() ;
		}
	};

	TConnection.prototype.open = function () {
		this.cacheProtect();
		this.oConn.open(this.sMethod, this.sUrl, true);
		if (this.sMethod == 'POST') {
			var parameters = this.getParameters();
			this.oConn.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.oConn.setRequestHeader("Content-length", parameters.length);
			this.oConn.setRequestHeader("Connection", "close");
			this.oConn.send(parameters);
		} else {
			this.oConn.send('');
		}

		//document.body.style.cursor = 'wait';
	};

	TConnection.prototype.getParameters = function () {
		var sRes = '';
		
		for (var k in this.aPost) {
			sRes += urlencode(k) + '=' + urlencode(this.aPost[k]) + '&';
		}
		
		return sRes;
	};
	
	function urlencode( str ) {                                     
		var ret = new String(str);
		
		ret = encodeURIComponent(ret);
		ret = ret.replace(/%20/g, '+');
	 
		return ret;
	}

	function send_form(form, onload) {
		if (_is_loading) 
			return;
			
		view_loading();
		var conn = new TConnection;
		
		var act = form.getAttribute('action');
		conn.sUrl = get_url(act, {partial: 1});
		
		if (_active_page) {
			//_active_page.url = conn.sUrl;
		}
		
		conn.onData = createMethodReference(onload, function (conn) {
			this(conn.responseText);
			evaluate_jcode();
			hide_loading();
		});
		
		if (form.getAttribute('method') == 'post') {
			conn.sMethod = 'POST';
		}
		
		setFormDataToConnection(form, conn);
		
		conn.open();
	}
	
	function setFormDataToConnection(form, conn) {
		var ip = get_by_tag(form, 'input', []);
		
		for (k = 0; k < ip.length; k ++) {
			if (ip[k] && ip[k].getAttribute('name') != null) {
				if (ip[k].getAttribute('type') == 'radio' || ip[k].getAttribute('type') == 'checkbox') {
					if (!ip[k].checked) {
						continue;
					}
				}
				
				if (ip[k].getAttribute('name') == 'partial') {
					continue;
				}
				
				if (form.getAttribute('method') == 'post') {
					conn.aPost[ip[k].getAttribute('name')] = ip[k].value;
				} else {
					conn.sUrl += '&' + urlencode(ip[k].getAttribute('name')) + '=' + urlencode(ip[k].value);
				}
			}
		}
		
		var t = form.getElementsByTagName('textarea');
		for (l = 0; l < t.length; l ++) {
			if (t[l] && t[l].getAttribute('name') != null) {
				if (form.getAttribute('method') == 'post') {
					conn.addPost(t[l].getAttribute('name'), t[l].value);
				} else {
					conn.sUrl += '&' + urlencode(t[l].getAttribute('name')) + '=' + urlencode(t[l].value);
				}
			}
		}
		
		t = form.getElementsByTagName('select');
		for (l = 0; l < t.length; l ++) {
			if (t[l] && t[l].getAttribute('name') != null) {
				if (form.getAttribute('method') == 'post') {
					conn.addPost(t[l].getAttribute('name'), t[l].value);
				} else {
					conn.sUrl += '&' + urlencode(t[l].getAttribute('name')) + '=' + urlencode(t[l].value);
				}
			}
		}
	}

	function get_by_tag(e, t, res) {
		if (e.childNodes) {
			for (var i = 0; i < e.childNodes.length; i ++) {
				if (e.childNodes[i].nodeName.toLowerCase() == t) {
					res.push(e.childNodes[i]);
				}
				get_by_tag(e.childNodes[i], t, res);
			}
		}
		return res;
	}
	
	var shadestep_to;
	function shadestep(el, value, stepsize, hideafter) {
		value -= stepsize;
		if (value < 0) {
			value = 0;
			if (hideafter || is_subclass_of(el, 'loading-hideafter')) {
				el.style.display = 'none';
			} else {
				el.style.visibility = 'hidden';
			}
			el.style.opacity = 1;
			return;
		}
		
		el.style.opacity = value/100;
		shadestep_to = setTimeout(createMethodReference({el:el, value:value, stepsize:stepsize, hideafter:hideafter}, function () {
			shadestep(this.el, this.value, this.stepsize, this.hideafter);
		}), 100);
	}
	
	function shadeoff(el, stepsize, hideafter) {
		if (typeof el == 'string') el = $(el);
		if (!el) return;
		if (!stepsize) stepsize = 7;
		
		shadestep(el, 100, stepsize, hideafter);
	}
	
	function shadeoff_timeout(timeo, el, stepsize, hideafter) {
		var func = createMethodReference({el:el, stepsize:stepsize, hideafter:hideafter}, function () {
			shadeoff(this.el, this.stepsize, this.hideafter);
		});
		
		setTimeout(func, timeo);
	}

	function select_all(name, strict, none) {
		if (typeof name == 'object') {
			for (var k = 0; k < name.length; k ++) {
				if (name[k].type == 'checkbox') {
					if (strict)
						name[k].checked = !none; 
					else
						name[k].checked = !name[k].checked; 
				}
			} 
		} else {
			var radio = document.getElementsByTagName('input');
			for (var k = 0; k < radio.length; k ++) {
				if (radio[k].type == 'checkbox' && strpos(radio[k].name, name) === 0) {
					if (strict)
						radio[k].checked = !none;
					else
						radio[k].checked = !radio[k].checked; 
				}
			}
		}
	}

	function get_checked(name) {
		var radio = document.getElementsByTagName('input');
		var res = [];
		for (var k = 0; k < radio.length; k ++) {
			if (radio[k].type == 'checkbox' && strpos(radio[k].name, name) === 0 && radio[k].checked) 
				res.push(radio[k]);
		}
		return res;
	}

	function style_by_class(className, styles) {
		var it = getElementsByClass(className);
		for (var k = 0; k < it.length; k ++) {
			for (var s in styles) {
				it[k].style[s] = styles[s];
			}
		}
	}

	function check_by_class(className, strict) {
		var it = getElementsByClass(className);

		if (it.length > 0)
			var stv = it[0].checked;
			
		for (var k = 0; k < it.length; k ++) 
			it[k].checked = strict ? !stv : !it[k].checked;
	}
	
	function style_by_id(id, styles) {
		var it = $(id);
		for (var s in styles) {
			it.style[s] = styles[s];
		}
	}

	function show_id(cls) {
		style_by_id(cls, {display: ''});
	}

	function hide_id(cls) {
		style_by_id(cls, {display: 'none'});
	}

	function show_class(cls) {
		style_by_class(cls, {display: ''});
	}

	function hide_class(cls) {
		style_by_class(cls, {display: 'none'});
	}
	
	var _url = new String(window.location);
	
	function get_url(url, arg) {
//		var query = '';
		
		if (!url)
			url = _url;
		
		url = new String(url);
//
//		if (_url.match(/\?/)) {
//			query = _url.replace(/.*\?/, '');
//		}
		
		if (!url.match(/\?/)) 
			url += '?';
		
		for (var k in arg) {
			url = url.replace(new RegExp(k + "(\[.*?\])*=.*?(&|$)", "g"), '');
			url += '&' + encodeURI(k) + '=' + encodeURI(arg[k]);
		}
		url = url.replace(/\?\&+/, "?");
		url = url.replace(/\&+/, "&");
		
		return url;
	}

	function strpos( haystack, needle, offset){
		var i = haystack.indexOf( needle, offset );
		return i >= 0 ? i : false;
	}
	
	function on_window_load(func) {
		if (isIe()) {
			attachEvent(document, 'readystatechange', createMethodReference({func:func}, function () {
				if (document.readyState == 'complete')
					this.func();
			}));
		} else
			attachEvent(window, 'load', func);
	}

	on_window_load(function () {
		show_class('js-show');
		hide_class('js-hide');
	});
	
	function in_array(needle, haystack) {
		if (haystack.length) {
			for (var k = 0; k < haystack.length; k ++) {
				if (haystack[k] == needle) 
					return true;
			}
			return false;
		}
		
		for (var k in haystack) {
			if (haystack[k] == needle) 
				return true;
		}
		return false;
	}	
	
	function is_def(variable) {
		return typeof(variable) !== 'undefined';
	}
	
	function get_element_absolute(element) {
		var res = {
			top: element.offsetTop,
			left: element.offsetLeft
		}
		
		if (element.offsetParent) {
			var parent = get_element_absolute(element.offsetParent);
			res.top += parent.top;
			res.left += parent.left;
		}
		
		return res;
	}
	
	function register_function(name, cbx) {
		window[name] = cbx;
	}

	var noptions_group = {};

	function getForm(obj) {
		if (obj.nodeName.toLowerCase() == 'form')
			return obj;
		return getForm(obj.parentNode);
	}
	
	function clone(obj) {
		var cn = {};
		for (p in obj) {
			cn[p] = obj[p];
		}
		return cn;
	}
	
	function preventDefault(event) {
		if (event.preventDefault) {
			event.preventDefault();
		} else {
			event.returnValue = false;
		}
	}

	function substr(s, st, len) {
		s = new String(s);

		if (st < 0) {
			st += s.length;
		}

		if (len == undefined) {
			len = s.length;
		} else if (len < 0){
			len += s.length;
		} else {
			len += st;
		}

		if (len < st) {
			len = st;
		}

		return s.substring(st, len);
	}

	function trim(s) {
		return ltrim(rtrim(s));
	}

	function ltrim(s) {
		return (new String(s)).replace(new RegExp("^[ \t\r\n]+", "g"), "");
	}

	function rtrim(s) {
		return (new String(s)).replace(new RegExp("[ \t\r\n]+$", "g"), "");
	}

	function l(s) {
		for (var i = 1; i < arguments.length; i ++) {
			s = s.replace('%' + (i-1), arguments[i]);
		}

		return s;
	}

	function time() {
		return _thetime;
	}

	function array() {
		var res = [];
		for (var i = 0; i < arguments.length; i ++)
			res.push(arguments[i]);

		return res;
	}

	function get_html_translation_table(table) {
		var entities = {}, histogram = {}, decimal = 0, symbol = '';

		entities['38'] = '&amp;';
		entities['60'] = '&lt;';
		entities['62'] = '&gt;';

		for (decimal in entities) {
			symbol = String.fromCharCode(decimal)
			histogram[symbol] = entities[decimal];
		}

		return histogram;
	}

	function htmlspecialchars (s, quote_style) {
		var tmp_str = new String(s);

		var histogram = {}, symbol = '', i = 0;

		if (false === (histogram = get_html_translation_table('HTML_SPECIALCHARS', quote_style))) {
			return false;
		}

		for (symbol in histogram) {
			entity = histogram[symbol];
			tmp_str = tmp_str.split(symbol).join(entity);
		}

		return tmp_str;
	}

	function array_unshift(a, val) {
		return a.unshift(val);
	}

	function array_pop(a) {
		return a.pop();
	}
	function array_push(a, val) {
		return a.push(val);
	}


	on_window_load(function () {
		if (typeof(console) == 'undefined') {
			var console = {"log": function () {}};
		}
	});

	function list_changePage(p, loc) {
		window.scroll(0, 0);
		return changePage(p, loc);
	}
	
	var block_reloading = null;
	
	function reload_block(name, objid, href, onload) {
		if (isIe6())
			return true;
			
		var obj = $(objid);
		
		if (!href) {
			if (onload)
				onload();
			return false;
		}
		
		if (!obj) {
			if (onload)
				onload();
			return true;
		}

		href = href.replace(/#.*$/, '');
		
		var c = new CElement;
		c.setElement(obj);
		
		var d = n('div');
		d.style.backgroundColor = '#FFEDCC';
		d.style.backgroundImage = 'url("' + JS_HOST + 'view/images/loading.gif")';
		d.style.width = '130px';
		d.style.height = '25px';
		d.style.lineHeight = '25px';
		d.style.paddingLeft = '25px';
		d.style.backgroundPosition = '5px center';
		d.style.position = 'absolute';
		d.style.border = '1px solid #FFA500';
		d.style.top = (c.getAbsoluteTop() + c.getHeight()/2 - 20) + 'px';
		d.style.left = (c.getAbsoluteLeft() + c.getWidth()/2 - 60) + 'px';
		d.innerHTML = 'Ładowanie ...';
		
		setTimeout(createMethodReference(d, function () {
			document.body.appendChild(this);
		}), 500);

		if (block_reloading)
			block_reloading.d.style.display = 'none';

		block_reloading = {
			"id": (new Date).getTime() + Math.random(),
			"d": d,
			"onload": onload
		};
		
		new TConnection(get_url(href, {"block": name}), createMethodReference({obj:obj, d:d, id:block_reloading.id}, function (c) {
			if (this.id == block_reloading.id) {
				this.obj.innerHTML = c.responseText;
				this.d.style.display = 'none';
			}
			
			if (block_reloading.onload) {
				block_reloading.onload();
			}
		}));
		
		return false;
	}

	on_window_load(function () {
		var i = getElementsByClass('autoselect');
		for (var k = 0; k < i.length; k ++) {
			attachEvent(i[k], 'focus', createMethodReference(i[k], function () {
				this.select();
			}));
		}
	});