function getID(id,doc)
{
	if (!doc) doc = document;
	if (document.all) return doc.all[id];
	else return doc.getElementById(id);
}
function getChildren(el)
{
	if (el.children) return el.children;
	return el.childNodes;
}
function killEvent(e)
{
	if (e.preventDefault) e.preventDefault();
	else e.returnValue = false;
}
function totalOffset(o)
{
	var off = {x:0,y:0};
	var s = "";
	while (o.offsetParent) {
		off.x += o.offsetLeft;
		off.y += o.offsetTop;
		s += " "+o.tagName+" "+o.offsetTop;
		o = o.offsetParent;
	}
//	print(s+" "+o.tagName+" "+off.y); 
	return off;	
}
function initIcon(doc,id,pix,code)
{
	var el = getID(doc,id);
	el.innerHTML = '<a href="javascript:'+code+
		'"><img style="vertical-align:bottom" border="0" src="'+
		vmgr.base+pix+'" /></a>';
}
function instof(o,tp)
{
	if (o.constructor) return o.constructor == tp;
	return o.__proto__ == tp.prototype;
}
function getStackTrace()
{
	var c = arguments.callee; var s = null; 
	var cnt = 10;
	if (!c) return "unavailable";
	while (c.caller && --cnt) {
		var n = c.caller.toString();
		var st = n.indexOf("function ")+9;
		n = n.substr(st,n.indexOf("(")-st);
		s = (s?s+"\n":"")+n+(c.callerln?":"+c.callerln:"");
		c = c.caller;
	}
	return s;
}
function report(t)
{
	if (t) alert("Assertion: "+t+"\n"+getStackTrace());
}
function extendBrowser(w)
{
	if (typeof(w.Array.prototype.push) != "function") {
		w.Array.prototype.push = new Function("o",'this[this.length]=o');
	}
}

extendBrowser(self);

function zeroPad(n)
{
	return n > 9 ? n.toString() : "0"+n;
}

function fmt_date(d)
{
	return d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear();
}
function zero_time(d)
{
	d.setMinutes(0);
	d.setSeconds(0);
	d.setHours(0);
	if (d.setMilliseconds) d.setMilliseconds(0);
}
function fmt_datetime(d)
{
	return fmt_date(d)+
		" "+zeroPad(d.getHours())+":"+zeroPad(d.getMinutes());
}
function parse_datetime(s)
{
	if (!s || !s.length) return null;
	var re = /^(\d+)[\/.](\d+)[\/.](\d+)( (\d+):(\d+))?$/i;
	var ma = s.match(re);
	if (!ma) {
		return false;
	}
	var r = new Date(ma[3],ma[2]-1,ma[1],ma[5]?ma[5]:0,ma[6]?ma[6]:0);
	return r;
}

/* Generates proxy object for calling obj.meth. It has method
   code(arg) which returns JS call code to the method with
   arg or event if null. */
function genProxy(obj,meth)
{
	var o = new Object();
	o.code = proxy_code;
	o.meth = meth; o.obj = obj;
	if (!self.objMap) self.objMap = new Array();
	var id = self.objMap.length;
	o.id = id;
	self.objMap[id] = o;
	return o;

	var code = genProxyCode(obj,meth,arg);
	if (arg !== null || self.event /* IE way */) 
		return new self.Function(code);
	return new self.Function("event",code); // DOM way
}
function proxy_code(arg)
{
	if (arg==null) arg = 'event';
	else if (typeof(arg)=="string") arg = "'"+arg+"'";
	return "_proxy("+this.id+","+arg+")";
}

function _proxy(id,arg)
{
	var o = self.objMap[id];
	if (!o) report("invalid id="+id);
	o.obj[o.meth](arg);
}

/* displays popup div and positions it under given ID */
function popup(div,under)
{
	var d = getID(div);
	var u = getID(under);
	off = totalOffset(u);
	d.style.left = off.x+'px';
	d.style.top = (off.y+u.offsetHeight)+'px';
	d.style.display = "block";
}

_starts = null;
function autoStart(fn)
{
	if (!_starts) _starts = new Array();
	_starts.push(fn);
}

function doAutoStart()
{
	if (!_starts) return;
	var i;
	for (i=0;i<_starts.length;i++) {
		self[_starts[i]]();
	}
}

function openWin(path,width,height) {
	anewWin = window.open(path,"stranky","width="+width+",height="+height+",toolbar=0,directories=0,menubar=0,status=0,resizable=0,location=0,scrollbars=1");
}

function targetBlank (url) {
	blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
	return  blankWin;
}
