// General Javascript functions

function create_xhr(stateChangeHandler) {
	var xhr = null;

	try {
		xhr = new ActiveXObject("Msxml2.XMLHTTP"); // later IE
	} catch (e) {
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP"); // earlier IE
		} catch (e2) {
			xhr = null;
		}
	}

	if (xhr == null) {
		xhr = new XMLHttpRequest(); // IE7, Firefox, Safari
	}

	xhr.onreadystatechange = function() {
		stateChangeHandler(xhr);
	}
	return xhr;
}

function popup(mylink, windowname) {
	if (!window.focus)
		return true;
	var href;
	if (typeof (mylink) == 'string')
		href = mylink;
	else
		href = mylink.href;
	window.open(href, windowname, 'width=640,height=300,scrollbars=yes');
	return false;
}

/**
 * Crawl up and find a form in a node's ancestors.
 */
function domGetForm(node) {
	while (node) {
		if (node.nodeName == 'FORM')
			return node;
		node = node.parentNode;
	}
	return null;
}

/**
 * Remove all of a node's children.
 */
function domClearChildren(node) {
	child = node.firstChild;
	while (child) {
		next = child.nextSibling;
		node.removeChild(child);
		child = next;
	}
}

/**
 * Set the text value of an element.
 */
function domSetValue(node, value) {
	domClearChildren(node);
	node.appendChild(document.createTextNode(value));
}

function domMakeLink(url, label) {
	node = document.createElement('a');
	domSetValue(node, label);
	node.setAttribute('href', url);
	return node;
}

function submitForm(node) {
	node.className = "flash";
	form = domGetForm(node);
	if (form)
		form.submit();
	else
		alert("Internal error: no form found");
}

function insert_tag(tag) {
	var node = document.getElementById('field.tags');
	var value = node.getAttribute('value') + ' ' + tag;
	node.setAttribute('value', value);
}

/**
 * Look up a DOM element by id.
 * 
 * Supports legacy browsers (probably no point).
 */
function findElementById(id) {
	var node = null;

	if (document.getElementById) {
		// DOM standard
		node = document.getElementById(id);
	} else if (document.all) {
		// Old MSIE
		node = document.all[id];
	} else if (document.layers) {
		// Netscape 4
		node = document.layers[id];
	}

	return node;
}

/**
 * Toggle visibility of a markup element.
 */
function toggleLayer(id, displayType) {
	var node = findElementById(id);
	var style = node.style;

	if (style.display == displayType) {
		style.display = 'none';
	} else {
		style.display = displayType;
	}
}

/**
 * UI callback for toggling airport markers.
 */
function ui_toggle_markers() {
	var link = document.getElementById("link-toggle-markers");
	toggle_markers();
	if (markers_visible) {
		domSetValue(link, "hide airport markers");
	} else {
		domSetValue(link, "show airport markers");
	}
}

/**
 * UI callback for toggling poi markers.
 */
function ui_toggle_pois() {
	var link = document.getElementById("link-toggle-pois");
	toggle_pois();
	if (pois_displayed) {
		domSetValue(link, "hide points of interest");
	} else {
		domSetValue(link, "show points of interest");
	}
}

/**
 * Show a link in a popup window.
 */
function popupLink($url, $title) {
	if ($title == null) {
		$title = 'Menu';
	}
	window.open($url, $title, "width=400,height=600,scrollbars=1");
	return false;
}

/**
 * Show a link in the parent window of a popup.
 * 
 * @param $url
 * @return
 */
function parentLink($url) {
	if (window.opener) {
		opener.location = $url;
		self.close();
		return false;
	} else {
		return true;
	}
}
