/* -*- mode: c; tab-width: 8; c-basic-offset: 8 -*- */
// General stuff.

//document.write ('<div id="debug" onmouseover="this.innerHTML = \'\'"></div>');

function objGet (path, nopath, doc) {
	if (typeof doc == 'undefined')
		doc = document;
	if (nopath)
		names = Array (path);
	else
		names = path.replace (/\/+/g, '/').split ('/');
	
	for (var i = 0; i < names.length; i++) {
		//debug (names[i] + ': ' + typeof doc); 
		if (names[i] == '') {
			doc = top;
			continue;
		}
		if (names[i] == '..') {
			doc = doc.defaultView.parent.document;
			continue;
		}
		if (names[i] == '.')
			continue;
		node = doc.getElementById (names[i]);
		if (!node) {
			//debug ('objGet: error: ' + names[i] + ' not found');
			return;
		}
		doc = node.contentDocument;
	}

	return node;
}

function objGetChildren (node, res) {
	if (!res)
		res = new Array ();
	var children = node.childNodes;
	for (var i = 0; i < children.length; i++) {
		res.push (children[i]);
		objGetChildren (children[i], res);
	}
	return res;
}

function objGetAncestorByTag (node, tagName) {
	var parent = node.parentNode;
	while (parent.tagName != tagName)
		parent = parent.parentNode;
	return parent;
}

function objRemove (node) {
	return node.parentNode.removeChild (node);
}

function debug_do (str) {
	document.getElementById ('debug').innerHTML = document.getElementById ('debug').innerHTML + 
		'(' + (DEBUG_ID ++) + ')>	' + str + '<br>';
}

function debug (obj) {
	var type = typeof obj;
	if (type == 'undefined') 
		obj = '';
	setTimeout ('debug_do ("(' + type + ')	" + ' + obj.toString ().toSource () + ')', 1000);
}

var ID_SEQ = -1;
var DEBUG_ID = 0;

function id_seq_next () {
	ID_SEQ ++;
	return ID_SEQ;
}

function id_seq_get () {
	return ID_SEQ;
}

function handlerFalse (evt) {
	return false;
}

function handlerTrue (evt) {
	return true;
}

function getPosTop (node) {
	var res = node.offsetTop;
	while (node = node.offsetParent)
		res += node.offsetTop;
	return res;
}

function getPosLeft (node) {
	var res = node.offsetLeft;
	while (node = node.offsetParent)
		res += node.offsetLeft;
	return res;
}

/* Detect browser, taken from nzjonboy at
   http://www.developerfusion.com/show/2186/ */

var useragent = navigator.userAgent;
var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;
var pos = useragent.indexOf('MSIE');
if (pos > -1) {
	bVer = useragent.substring(pos + 5);
	var pos = bVer.indexOf(';');
	var bVer = bVer.substring(0,pos);
}
var pos = useragent.indexOf('Opera');
if (pos > -1)    {
	bVer = useragent.substring(pos + 6);
	var pos = bVer.indexOf(' ');
	var bVer = bVer.substring(0, pos);
}
if (bName == "Netscape") {
	var bVer = useragent.substring(8);
	var pos = bVer.indexOf(' ');
	var bVer = bVer.substring(0, pos);
}
if (bName == "Netscape" && parseInt(navigator.appVersion) >= 5) {
	var pos = useragent.lastIndexOf('/');
	var bVer = useragent.substring(pos + 1);
}

bLang = navigator.language ? navigator.language : navigator.userLanguage;

function cookieCreate (name,value,days) {
	var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function cookieRead (name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}



