function trim (str)
{
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function get_cookiePos() {
	name = "bookmark_"+escape(location.search);
	rc = getCookie("bookmark_"+escape(location.search));
	return rc;
}

function set_cookiePos(pos) {
	days = 365*20; // cookies will be valid within ~20 years...
	expires = new Date();
	expires.setTime(expires.getTime()+(days*24*60*60*1000))
	name = "bookmark_"+escape(location.search);
	setCookie(name, pos, expires, "/", "ruslit.net");
}

function delete_bookmark(path, fname)
{
	name = "bookmark_"+escape("?path="+escape(path)+"&fname="+escape(fname));
	deleteCookie(name, "/", "ruslit.net");
}

// scan cookies and extract all bookmarks
// returns list of triples (path, fname, scroll_pos)
function list_bookmarks()
{
	cookies_list = list_cookies();
	bookmarks_list = new Array();
	for(i=0; i<cookies_list.length; i++) {
		key = trim(cookies_list[i][0]);
		if (key.substr(0, 9) == "bookmark_") {
			key = unescape(key.substr(9));
			val = cookies_list[i][1];

			args = key.substr(1).split("&"); // remove the "?" and split into individual key/value pairs
			path = "";
			fname = "";
			for (j=0; j<args.length; j++) {
				pair = args[j].split("=");
				if (pair[0] == "path")
					path = unescape(pair[1]);
				if (pair[0] == "fname")
					fname = unescape(pair[1]);
			}
			if (path!="" && fname!="") {
				entry = new Array(path, fname, val);
				bookmarks_list.push(entry);
			}
		}
	}
	return bookmarks_list;
}
