// 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;
	}
}

// contains function für arrays
function array_contains(a, e) { 
	var i;
	for(i=0; i<a.length; i++) 
    		if(a[i] == e) 
      			return true;

  	return false;
}

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) {
		// quote non-numbers that are not quoted 
                if(isNaN(value) && value.length > 0 && value[0] != '\'' && value[value.length-1] != '\'')
                        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"
                );
	}
}

// GetPageElement behaves like findid but emits alert if not found
function gpe(id) {
	var rv;
	
	rv = findid(id);
	if(rv)
		return rv;

	alert("Unknown element \'"+id+"\'");
	return null;
}

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 vhide(id) {
        if(findid(id))
                findid(id).style.visibility = 'hidden';
}

function vshow(id) {
        if(findid(id))
                findid(id).style.visibility = 'visible';
}

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


function rtrv(obj) {
	var rs, i;

	if(obj.type == "checkbox")
		return obj.checked?"true":"false";

	if(obj.type == "text" || obj.type == "file" || obj.type == "textarea" || obj.type == "hidden")
		return obj.value;
		
	if(obj.type == "select-one")
		return obj.options[obj.selectedIndex].value;

	if(obj.type == "select-multiple") {
		rs = "";
		for(i=0; i<obj.length; i++)
			if(obj.options[i].selected)
				rs = rs?(rs+"|"+obj.options[i].value):(obj.options[i].value);
		return rs;
	}

	if(obj.length) {
		if(obj[0].type == "radio") {
			for(i=0; i<obj.length; i++)
				if(obj[i].checked)
					return obj[i].value;
			return "";
		}
	} else if(obj.type == "radio") // single radio
		return obj.checked? obj.value : "";


	alert("Unknown input field '"+obj.name+"', please contact the webmaster!");
}


function dump(obj, name, indent, depth) {
	var MAX_DUMP_DEPTH = 2;

	if(depth > MAX_DUMP_DEPTH) 
             	return indent + name + ": <Maximum Depth Reached>\n";

        if(typeof obj == "object") {
                var child = null;
                var output = indent + name + "\n";
                indent += "\t";

                for(var item in obj) {
                      try {
                             child = obj[item];
                      } catch (e) {
                             child = "<Unable to Evaluate>";
                      }

                      if(typeof child == "object") 
                             output += dump(child, item, indent, depth + 1);
                      else 
                             output += indent + item + ": " + child + "\n";
                }

                return output;
	} else
                return obj;
}

