// Function to return XMLHttpRequest to use for AJAX calls
function getXMLHTTPRequest() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
		return xmlHttp;
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			return xmlHttp;
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				return xmlHttp;
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}
// allow firefox to handle a click - code possibly by Jason Karl Davis
if (typeof HTMLElement != 'undefined' && !HTMLElement.prototype.click)
	HTMLElement.prototype.click = function() {
		var evt = this.ownerDocument.createEvent('MouseEvents');
		evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView,
				1, 0, 0, 0, 0, false, false, false, false, 0, null);
		this.dispatchEvent(evt);
	}
function toggleTabVisible(tabId) {
	// Hide all tabs
	var tabs = document.getElementById("tabs");
	var children = tabs.childNodes;
	for ( var i = 0; i < children.length; i = i + 1) {
		if (children[i].style) {
			children[i].style.display = "none";
		}
	}
	// Change all the play buttons to off
	var tabList = document.getElementById("tabList");
	children = tabList.childNodes;
	for (i = 0; i < children.length; i = i + 1) {
		if (children[i].style) {
			children[i].style.background = "url(/images/play_off.gif) no-repeat center left";
		}
	}
	var tabDiv = document.getElementById(tabId + 'Div');
	tabDiv.style.display = "block";
	var tabLi = document.getElementById(tabId + 'Li');
	tabLi.style.background = "url(/images/play.gif) no-repeat center left";
}
// Change the display attribute of a(ny) element to display
function showElement(elementId) {
	var elToShow = document.getElementById(elementId);
	elToShow.style.display = "block";
}
// Change the display attribute of a(ny) element to none
function hideElement(elementId) {
	var elToHide = document.getElementById(elementId);
	elToHide.style.display = "none";
}
// Function to show the send-response div of enquiry
function showReplyDiv() {
	var replyTextArea = document.getElementById('replyText');
	replyTextArea.value = "";
	hideElement('replyButton');
	showElement('replyDiv');
	return false;
}
// Function to hide the add-response div of enquiry
function hideReplyDiv() {
	showElement('replyButton');
	hideElement('replyDiv');
	return false;
}
// Function to mark a page
function markPage(entityId) {
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var str = xmlHttp.responseText;
			var el = document.getElementById("numMarkedPagesSpan");
			if (el) {
				var num = xmlHttp.responseText;
				el.innerHTML = num;
				if (num > 0) {
					var el2 = document.getElementById("viewMarkedPagesSpan");
					el2.style.display = "inline";
				}
			}
		}
	};
	xmlHttp.open("GET", "/admin/markpage.action?id=" + entityId, true);
	xmlHttp.send(null);
	return false;
}
// Function to remove a bookmarked page
function removeMarkedPage(id) {
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var liName = id + "MarkedPage";
			var liEl = document.getElementById(liName);
			if (liEl) {
				var listEl = liEl.parentNode;
				listEl.removeChild(liEl);
			}
			var str = xmlHttp.responseText;
			var el = document.getElementById("numMarkedPagesSpan");
			if (el) {
				var num = xmlHttp.responseText;
				el.innerHTML = num;
				if (num > 0) {
					var el2 = document.getElementById("viewMarkedPagesSpan");
					el2.style.display = "inline";
				}
			}
		}
	};
	xmlHttp.open("GET", "/admin/removeMarkpage.action?id=" + id, true);
	xmlHttp.send(null);
	return false;
}
//Function to remove all bookmarked pages
function removeAllMarkedPages() {
	confirm('Do you want to remove all book marked pages from the list?');
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var str = xmlHttp.responseText;
			var el = document.getElementById("numMarkedPagesSpan");
			if (el) {
				var num = xmlHttp.responseText;
				el.innerHTML = num;
				var el2 = document.getElementById("viewMarkedPagesSpan");
				el2.style.display = "none";
			}
			var olEl = document.getElementById("bookmarkedList");
			if (olEl) {
				olEl.parentNode.removeChild(olEl);
			}
		}
	};
	xmlHttp.open("GET", "/admin/removeAllMarkedPagespage.action", true);
	xmlHttp.send(null);
	return false;
}
// Function to display popup for emailing
function openContactForm() {
	var contactWindow = window.open("/contact.action", "_blank", "width=600, height=300, toolbar=no, scrollbars=yes");
	return false;
}