function getTarget(e) {
	var targ;
	if (!e) {
		e = window.event;
	}
	if (e.target) {
	  	targ = e.target;
	} else if (e.srcElement) {
	  	targ = e.srcElement;
	}
	if (targ.nodeType==3) { // defeat Safari bug
	  	targ = targ.parentNode;
	}
	return targ;	
}

function getMouseOverRelatedElement(event) {
	var related;
	
	if (window.event) {
		related = window.event.fromElement;
	} else {
	    related = event.relatedTarget;
	}
	
	return related;
}

function getMouseExitRelatedElement(event) {
	var related;
	
	if (window.event) {
		related = window.event.toElement;
	} else {
	    related = event.relatedTarget;
	}
	
	return related;
}

function highlight(e) {
	var target = getTarget(e);
	target.style.backgroundColor = "MediumSeaGreen";
	target.style.cursor = "pointer";
}

function unhighlight(e) {
	var target = getTarget(e);
	target.style.backgroundColor = "SeaGreen";
	target.style.cursor = "default";
}

function getAjaxRequest() {
	try {
		// Opera 8.0+, Firefox, Safari. IE 7+
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		// Older Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				return false;
			}
		}
	}
	return ajaxRequest;
}

// Two globals used for navigating
var CurrentPage = "don't match";

// For the main nav bar
function navigatePanel(page, parameters) {
	CurrentPage = page;
	window.location.hash = page;
	var url = PageNames[page];
	if (parameters != "") {
		url += "?" + parameters;
	}
	//alert("Going to page " + page + " at URL " + url);
	var div = window.document.getElementById("panel");
	if (!div) {
		alert("Error -- couldn't find panel DIV");
		window.location = url;
		return;
	}
	var ajaxRequest = getAjaxRequest();
	if (ajaxRequest) {
		if (parameters != "") {
			url += "&panel=1";
		} else {
			url += "?panel=1";
		}
		ajaxRequest.onreadystatechange = function() {
	        if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
	        	div.innerHTML = ajaxRequest.responseText;
	        }
	    }
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else {
		// No ajax, so fake it with an IFRAME
		div.innerHTML = "<iframe src='" + url + "' />";
	}
}

/*
function highlightMenuCell(id, depth) {
	var target = window.document.getElementById(id);
	if (depth == 0) {
		target.style.backgroundColor = "LightSeaGreen";
	} else {
		target.style.backgroundColor = "Yellow";
	}
}

function unhighlightMenuCell(id, depth) {
	var target = window.document.getElementById(id);
	if (depth == 0) {
		target.style.backgroundColor = "SeaGreen";
	} else {
		target.style.backgroundColor = "Wheat";
	}
}
*/
function getElementLocation(elem) {
	var loc = new Object();
	loc.top = elem.offsetTop;
	loc.left = elem.offsetLeft;
	
	var e = elem.offsetParent;
	while (e.nodeName != "BODY") {
		loc.top += e.offsetTop;
		loc.left += e.offsetLeft;
		e = e.offsetParent;
	}
	
	return loc;
}

function contains(container, elem) {
	while (elem.nodeName != "BODY") {
		if (elem == container) {
			return true;
		}
		elem = elem.parentNode;
	}
	return false;
}

function showSubmenu(event, divId) {
	var target = getTarget(event);
	// target.style.backgroundColor = "LightSeaGreen";
	var targetLoc = getElementLocation(target);
	var div = window.document.getElementById(divId);
	if (div == null || div == undefined) {
		alert("Can't find menu div");
	}
	div.style.position = "absolute";
	targetLoc.top += target.offsetHeight;
	div.style.top = "" + targetLoc.top + "px";
	div.style.left = "" + targetLoc.left + "px";
	if (div.style.width < target.style.width) {
		div.style.width = target.style.width;
	}
	div.style.display = "block";
}

function hideSubmenu(event, divId) {
	var target = getTarget(event);
	// target.style.backgroundColor = "SeaGreen";
	var to = getMouseExitRelatedElement(event);
	var div = window.document.getElementById(divId);
	if (div == null || div == undefined) {
		alert("Can't find menu div");
	}
	if (!contains(div, to)) {
		div.style.display = "none";
	}
}

function toggleSubmenu(event, divId) {
	var div = window.document.getElementById(divId);
	if (div == null || div == undefined) {
		alert("Can't find menu div");
	}
	if (div.style.display == "block") {
		div.style.display = "none";
	} else {
		var target = getTarget(event);
		var targetLoc = getElementLocation(target);
		targetLoc.top += target.offsetHeight;
		div.style.top = "" + targetLoc.top + "px";
		div.style.left = "" + targetLoc.left + "px";
		if (div.style.width < target.style.width) {
			div.style.width = target.style.width;
		}
		div.style.display = "block";
	}
}

function closeSubmenuAndNavigatePanel(divId, navTarget) {
	var div = window.document.getElementById(divId);
	if (div == null || div == undefined) {
		alert("Can't find menu div");
	}
	div.style.display = "none";
	navigatePanel(navTarget, "");
}

function navigatePanelToHash() {
	var page = window.location.hash;
	if ((page == "") || (page == null) || (page == undefined)) {
		navigatePanel("home", "");
	} else {
		navigatePanel(page.substring(1), "");
	}
}

// For the "About Fencing" page
function toggleVisible(name)
{
	var field = window.document.getElementById("field-" + name);
	var label = window.document.getElementById("label-" + name);
	if (field.className == "contracted") {
		//field.style.visibility = "visible";
		//field.style.display = "inline";
		field.className = "expanded";
		label.src = "img/small-up-darkgreen.gif";
		label.alt = "Click to contract";
	} else {
		//field.style.visibility = "hidden";
		//field.style.display="none";
		field.className = "contracted";
		label.src = "img/small-down-darkgreen.gif";
		label.alt = "Click to expand";
	} 
}

// For the calendar forward and back
function navigateCalendar(month, year)
{
	navigatePanel("calendar", "month=" + month + "&year=" + year);
}

// For the event calendar, to see event details
function viewEvent(id)
{
	//alert("View event " + id);
	navigatePanel("event", "id=" + id);
}

// This function is executed every half-second to see if the user has 
// navigated their browser and the hash has changed
function checkHash()
{
	if (CurrentPage != window.location.hash.substring(1)) {
		navigatePanelToHash();
	}
	var t = setTimeout("checkHash();", 500);
}

// This function is used as an event handler to call sendEmailForm.
// Note that to use this function, the form has to have ID "form"
// and the result div has to have ID "div"; also, the email has to
// be intended for the treasurer.
function sendIt() {
	// alert("About to send");
	var result = sendEmailForm("treasurer", "form");
	if (result) {
		var div = window.document.getElementById("div");
		//div.innerHTML = result;
		div.innerHTML = "<p>Thank you! Your request has been submitted.</p>";
	} else {
		div.innerHTML = "<p>There has been an error processing your request. Your browser " +
			"may not support AJAX, or there may have been a server error.</p>";
	}
}

// This function is used to send an email from a form
function sendEmailForm(to, formId) {
	var req = getAjaxRequest();
	if (!req) {
		alert("Your browser does not support AJAX; please upgrade.");
		return false;
	}
	var form = window.document.getElementById(formId);
	if (!form) {
		alert("There is no form named " + formId + " on this page");
		return false;
	}

	var loc = window.location;
	var url = loc.protocol + "//" + loc.host + "/send_mail.php";
	var data = "subject=" + encodeURIComponent(form.name);
	data += "&password=floogle";
	data += "&to=" + to;
	
	var elements = form.elements;
	var n = elements.length;
	for (var i = 0; i < n; i++) {
		var field = elements[i];
		if (field.type == "text") {
			if (field.value != "") {
				data += "&" + field.id + "=" + encodeURIComponent(field.value);
			}
		}
	}
	
	//alert("url is " + url);
	
	req.open("POST", url, false); // false makes this synchronous
	req.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	req.send(data);
	return req.responseText;
}