// Basic javascript functions (included in rootlayer)

// IE < 5.5 does not implement Array.push, Array.pop, Array.shift 
if (!Array.prototype.push) {
        Array.prototype.push = function () {
                for(var i=0; i<arguments.length; i++)
                        this[this.length] = arguments[i];
                return this.length;      // push() should return the new array length
        };
}

if (!Array.prototype.pop) {
        Array.prototype.pop = function() {
                var rv = this[this.length - 1];
                this.length--;
                return rv;
        };
}

if(!Array.prototype.shift) {
	Array.prototype.shift = function() {
		var rv = this[0];
		for(var i=0; i<this.length-1; i++)
			this[i] = this[i+1];
		this.length--;
		return rv;
	}
}

function sprintf(str) {
    	var i=1;
	args = sprintf.arguments;

	// test for array-parameter 2
	if(args.length == 2 && typeof(args[1]) == "object" && args[1].length) {
		args = args[1];
		i = 0;
	}

	// replace all %s and %d
	for(; i<args.length; i++) 
		str = str.replace(/(%s|%d)/, String(args[i])); 

	return str;
}

function OpenPopup(url,x,y) {
        var w = open(url,
                 "",
                 "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+x+",height="+y
        );
        if(w.opener==null) 
                w.opener = self;
}

function AclEditor(mode, table, column, pkey, value, fork, display) {
	var w;

        if (!display)
		display = 'display';
        if (column && pkey && value) {
                if (isNaN(value))
                        value = "'"+value+"'";
		if (fork == null)
			fork_param = "";
		else
			fork_param = "/fork/"+fork;

                w = open(ssl_prefix+"/" + mode + "/acl/"+display+"/"+table+"/"+column+"/"+pkey+"/"+value+fork_param, "",
                         "toolbar=0,dependent=yes,location=0,directories=0,status=0,menubar=0,"+
                        "scrollbars=1,resizable=1,width=420,height=450"
                );
        } else {
		// works only for readonly (html) mode: only id is required: (due to compatibility)
                w = open(ssl_prefix+"/" + mode + "/acl/"+display+"/"+table, "",
                         "toolbar=0,dependent=yes,location=0,directories=0,status=0,menubar=0,"+
                         "scrollbars=1,resizable=1,width=420,height=450"
                );
	}
}

function findid(id) {
	if(document.layers)
		return;

	if(document.getElementById)
		return document.getElementById(id);
	
	// msie	
	return document.all[id];
}

function hide(id) {
        if(findid(id))
                findid(id).style.display = "none";
}

function show(id) {
        if(findid(id))
                findid(id).style.display = "block";
}

function trim(s) {
	return s.replace(/(^\s*)|(\s*$)/g,'');
}

function write_email (user, domain) {
	write_emailx(user, domain, null, null);
}

function write_emailx (user, domain, name, subject) {
	if(!name)
		name = user+'@'+domain;
        document.write('<a href=\"mailto:' + user + '@' + domain + (subject ? '?subject='+subject : '') + '\">' + name + '</a>');
}

function pos_x(obj) {
	var curleft = 0;
	if(obj.offsetParent) {
		while(obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function pos_y(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function win_h() {
	if(self.innerHeight)  // not ie 
		return self.innerHeight;

	if(document.documentElement && document.documentElement.clientHeight) // ie6 strict 
		return document.documentElement.clientHeight;
	else if(document.body) // ie other
		return document.body.clientHeight;

	return 0;
}

function win_w() {
	if(self.innerWidth)  // not ie 
		return self.innerWidth;

	if(document.documentElement && document.documentElement.clientWidth) // ie6 strict 
		return document.documentElement.clientWidth;
	else if(document.body) // ie other
		return document.body.clientWidth;

	return 0;
}

function page_h() {
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if(test1 > test2) // not ie mac
		return document.body.scrollHeight;
	else // ie mac (also ie6 strict, moz, safari)
		return document.body.offsetHeight;
}

function scroll_y(w) {
	if(!w)
		w=self;

	if(w.pageYOffset) // not ie 
		return w.pageYOffset;
	
	if(w.document.documentElement && w.document.documentElement.scrollTop)
		return w.document.documentElement.scrollTop;
	
	if(w.document.body) 
		return w.document.body.scrollTop;

	return 0;
}

function on_load_handler() {
	params = document.location.search;
	pos = params.indexOf("move_to=");
	if(pos > 0) {
		arg = params.substring(pos+8);
		pos = arg.indexOf("&");
		if(pos > 0)
			arg = arg.substring(0, pos);

		if(arg.indexOf("id_") == 0) { // move object to 1/4 of win_h
			obj = findid(arg);
			if(obj) {
				y = Math.min(pos_y(obj) - win_h()/4, page_h() - win_h());
				if(y > 0) 
					self.scrollBy(0,y);
			} else
				alert("Object '"+arg+"' not found.");
		} else // move to pixel y 
			self.scrollBy(0,parseInt(arg));
	}
	
}



