// Function for highlighting subject
function hilight (id)
{
	if (oldhilight != null)
	{
		var oldspan = document.getElementById(oldhilight);
		oldspan.className = 'normal';

		// Remove extra # inside, if we were empty
		if (oldhilight.match(/-0$/))
		{
			oldspan.innerHTML = '';
		}

		// Enable the last <br> tag again
		if (lastid != null && oldhilight == lastid)
		{
			var lastbr = document.getElementById('lastbr');
			if (lastbr != null)
			{
				lastbr.style.display = 'inline';
			}
		}
	}

	var newspan = document.getElementById(id);
	newspan.className = 'hilight';

	// add extra # inside, if we are empty
	if (id.match(/-0$/))
	{
		newspan.innerHTML = '#';
	}

	// Hide last <br> element in subject, since it would show otherwise
	if (lastid != null && id == lastid)
	{
		var lastbr = document.getElementById('lastbr');
		if (lastbr != null)
		{
			lastbr.style.display = 'none';
		}
	}

	oldhilight = id;
	showBox('cursubject');

	return true;
}


// Shows popup tooltip
function showHelp (caller, id)
{
	var help = document.getElementById("tooltip");

	help.style.top = (findPosY(caller) + 10) + "px";
	help.style.left = (findPosX(caller) + 10) + "px";
	help.style.display = "block";

	document.getElementById("tooltip-title").innerHTML = lang_name[id];
	document.getElementById("tooltip-text").innerHTML = lang_text[id]
}

// Hides popup tooltip
function hideHelp (key)
{
	document.getElementById("tooltip").style.display = "none";
}

// Show box from the plus sign
function showBox (key)
{
	var box = document.getElementById(key + "-box");
	box.style.display = "block";

	// Remove possible entries from cookiedata
	if (cookiedata.match(new RegExp("(^| )" + key + "( |$)")))
	{
		cookiedata = cookiedata.replace(new RegExp("(^| )" + key + "( |$)"), '');
		saveMyCookie();
	}

	return false;
}

// Hide box from the minus sign
function hideBox (key)
{
	var box = document.getElementById(key + "-box");
	box.style.display = "none";

	// Add entry to cookie data, if needed
	if (!cookiedata.match(new RegExp("(^| )" + key + "( |$)")))
	{
		cookiedata = cookiedata.length > 0 ? cookiedata + ' ' + key : key;
		saveMyCookie();
	}

	return false;
}

// Check upon script loading if menu is hidden
function hideCheck (key)
{
	if (cookiedata.match(new RegExp("(^| )" + key + "( |$)")))
	{
		var box = document.getElementById(key + "-box");
		box.style.display = "none";
	}
}

// Load the cookie data
function getMyCookie ()
{
	var cookie = document.cookie;
	var regex = /(^|; *)regextool_hide=([^;]*)(;|$)/
	var match = regex.exec(cookie);

	return match == null ? '' : match[2];
}

// Save hide data into cookie
function saveMyCookie ()
{
	var date = new Date();
	date.setTime(date.getTime() + 60 * 60 * 24 * 30 * 1000);

	var cookiestring =
		'regextool_hide=' + cookiedata +
		'; expires=' + date.toUTCString() +
		'; path=/';

	document.cookie = cookiestring;
}

/*
 * findPos functions by Peter-Paul Koch & Alex Tingle
 *
 * http://www.quirksmode.org/js/findpos.html
 * http://blog.firetree.net/2005/07/04/javascript-find-position/
 */

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

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