function webmap_checkAll(form)
{
	var elems = form.getElementsByTagName("input");

	for (var i = 0; i < elems.length; i++)
	{
		if (elems.item(i).type == 'checkbox')
			elems.item(i).checked = true;
	}
}

function webmap_checkOrUncheckAllChildren(id)
{
	var e = document.getElementById(id)

	var list = e.getElementsByTagName("input");

	if (list.length == 0)
		return;

	var checked = null;

	for (var i = 0; i < list.length; i++)
	{
		if (list.item(i).type == 'checkbox')
		{
			if (checked == null)
				checked = !list.item(i).checked;

			list.item(i).checked = 	checked;
		}
	}
}

function webmap_showOrHideElement(id)
{
	var elem = document.getElementById(id);

	if (elem == null)
		return;

	if (elem.style.display == "")
		elem.style.display = "none";
	else
		elem.style.display = "";
}

function webmap_confirm(question, address_on_confirm)
{
	if (confirm(question))
	{
		window.location.replace(address_on_confirm);
	}
}

function webmap_doZoom(direction)
{
	var zoomDir = document.getElementById("zoom_dir");

	if (zoomDir == null)
		return;

	zoomDir.value = direction;
}

function webmap_zoomToLevel(level)
{
	var zoomLevel = document.getElementById("zoom_level");

	if (zoomLevel == null)
		return;

	zoomLevel.value = level;
}

function webmap_selectTool(tool)
{
	var selectedToolInput = document.getElementById("selected_tool");

	if (selectedToolInput == null)
		return;

	var selectedTool = document.getElementById(selectedToolInput.value);

	if (selectedTool != null)
		selectedTool.className = "tool";

	selectedTool = document.getElementById(tool);

	if (selectedTool == null)
		return;

	selectedTool.className = "selected_tool";

	selectedToolInput.value = tool;
}


// tooltip stuff

function webmap_MapPoint(minx, miny, maxx, maxy, toolTip, link)
{
	this.minx = minx;
	this.miny = miny;
	this.maxx = maxx;
	this.maxy = maxy;
	this.toolTip = toolTip;
	this.link = link;
}

var mapPoints = new Array();
var toolTip;

function webmap_showToolTip(x, y, content)
{
	if (!toolTip)
	{
		toolTip = document.getElementById("tooltip");

		// make sure the tooltip has the document body as it's parent
		toolTip.parentNode.removeChild(toolTip);
		document.body.appendChild(toolTip);
	}

	toolTip.innerHTML = content;

	toolTip.style.left = x + 'px';
	toolTip.style.top = y + 'px';

	toolTip.style.display = "block";
}

function webmap_hideToolTip()
{
	if (toolTip)
		toolTip.style.display = "none";
}

function webmap_mouseOverMap(e)
{
	if (!e)
		var e = window.event;

	var pos = webmap_getPositionOverTarget(e);

	/*var pos_x = document.getElementById("postarg_x");
	pos_x.value = pos.x;
	var pos_y = document.getElementById("postarg_y");
	pos_y.value = pos.y;*/

	var targetPos = webmap_getTargetPosition(e);

	/*var pos_x = document.getElementById("targ_x");
	pos_x.value = targetPos.x;
	var pos_y = document.getElementById("targ_y");
	pos_y.value = targetPos.y;*/

	var noPoint = true;
	var x = 0;
	var y = 0;
	var content = '';

	for (i = 0; i < mapPoints.length; i++)
	{
		if (pos.x > mapPoints[i].minx && pos.x < mapPoints[i].maxx && pos.y > mapPoints[i].miny && pos.y < mapPoints[i].maxy)
		{
			var scrolling = webmap_getScrolling(e);

			x = targetPos.x + mapPoints[i].maxx - scrolling.x;
			y = targetPos.y + mapPoints[i].miny - scrolling.y;
			
			if (mapPoints[i].link)
				content += "- <a href=\"javascript:webmap_openLink('" + mapPoints[i].link + "');\">" + mapPoints[i].toolTip + "</a><br />";
			else
				content += "- " + mapPoints[i].toolTip + "<br />";

			noPoint = false;
		}
	}

	if (noPoint)
		webmap_hideToolTip();
	else
		webmap_showToolTip(x, y, content);
}

function webmap_findPosX(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 webmap_findPosY(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 webmap_getPositionOverTarget(e)
{
	if (!e)
		var e = window.event;

	var pos = {x:0, y:0};

	// this simple alternative gives the position over the target directly
	// works at least with IE and Opera
	if (e.offsetX || e.offsetY)
	{
		pos.x = e.offsetX;
		pos.y = e.offsetY;

		return pos;
	}

	// this should work in at least Firefox
	if (e.clientX || e.clientY)
	{
		pos.x = e.clientX;
		pos.y = e.clientY;

		var scrolling = webmap_getScrolling(e);

		pos.x += scrolling.x;
		pos.y += scrolling.y;
	}

	/*var pos_x = document.getElementById("pos_x");
	pos_x.value = pos.x;
	var pos_y = document.getElementById("pos_y");
	pos_y.value = pos.y;*/

	// now we've got the position relative to the document
	// we just have to subtract the target position

	var targetPos = webmap_getTargetPosition(e);

	pos.x -= targetPos.x;
	pos.y -= targetPos.y;

	return pos;
}

function webmap_getScrolling(e)
{
	var pos = {x:0, y:0};

	var targ;

	if (e.target)
		targ = e.target;
	else if (e.srcElement)
		targ = e.srcElement;

	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	while (targ)
	{
		if (targ.scrollLeft)
			pos.x += targ.scrollLeft;

		if (targ.scrollTop)
			pos.y += targ.scrollTop;

		targ = targ.offsetParent;
	}

	return pos;
}

function webmap_getTargetPosition(e)
{
	var pos = {x:0, y:0};

	var targ;

	if (e.target)
		targ = e.target;
	else if (e.srcElement)
		targ = e.srcElement;

	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	pos.x = webmap_findPosX(targ);
	pos.y = webmap_findPosY(targ);

	return pos;
}

function webmap_openLink(href)
{
	if (window.opener)
		window.opener.location = href;
}

var webmap_hideFromPrintElementIds = new Array("webmap_toolbar", "webmap_pan_nw", "webmap_pan_n", "webmap_pan_ne", "webmap_pan_w", "webmap_pan_e", "webmap_pan_sw", "webmap_pan_s", "webmap_pan_se", "webmap_print_map", "webmap_set_location_submit", "webmap_set_location_cancel");

function webmap_printMap()
{
	webmap_setElementDisplay(webmap_hideFromPrintElementIds, "none");
	
	window.print();

	// keep the elements hidden for a while, otherwise at least Firefox will print them
	setTimeout("webmap_setElementDisplay(webmap_hideFromPrintElementIds, '')", 5000);
}

function webmap_setElementDisplay(ids, display)
{
	for (i = 0; i < ids.length; i++)
	{
		var elem = document.getElementById(ids[i]);

		if (elem)
			elem.style.display = display;
	}
}


