/*
JavaScript for tabbed navigation
Copyright (c) 2007 Andreas Blixt
MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// CSS helper functions
CSS = {
	// Adds a class to an element.
	AddClass: function (e, c) {
		if (!e.className.match(new RegExp("\\b" + c + "\\b", "i")))
			e.className += (e.className ? " " : "") + c;
	},

	// Removes a class from an element.
	RemoveClass: function (e, c) {
		e.className = e.className.replace(new RegExp(" \\b" + c + "\\b|\\b" + c + "\\b ?", "gi"), "");
	}
};

// Functions for handling tabs.
Tabs = {
	// Changes to the tab with the specified ID.
	GoTo: function (contentId, skipReplace) {
		// This variable will be true if a tab for the specified
		// content ID was found.
		var foundTab = false;

		// Get the TOC element.
		var toc = document.getElementById("toc");
		if (toc) {
			var lis = toc.getElementsByTagName("li");
			for (var j = 0; j < lis.length; j++) {
				var li = lis[j];

				// Give the current tab link the class "current" and
				// remove the class from any other TOC links.
				var anchors = li.getElementsByTagName("a");
				for (var k = 0; k < anchors.length; k++) {
					if (anchors[k].hash == "#" + contentId) {
						CSS.AddClass(li, "current");
						foundTab = true;
						break;
					} else {
						CSS.RemoveClass(li, "current");
					}
				}
			}
		}

		// Show the content with the specified ID.
		var divsToHide = [];
		var divs = document.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++) {
			var div = divs[i];

			if (div.className.match(/\bcontent_table\b/i)) {
				if (div.id == "_" + contentId)
					div.style.display = "block";
				else
					divsToHide.push(div);
			}
		}

		// Hide the other content boxes.
		for (var i = 0; i < divsToHide.length; i++)
			divsToHide[i].style.display = "none";

		// Change the address bar.
		if (!skipReplace) window.location.replace("#" + contentId);
	},

	OnClickHandler: function (e) {
		// Stop the event (to stop it from scrolling or
		// making an entry in the history).
		if (!e) e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;

		// Get the name of the anchor of the link that was clicked.
		Tabs.GoTo(this.hash.substring(1));
	},

	Init: function () {
		if (!document.getElementsByTagName) return;

		// Attach an onclick event to all the anchor links on the page.
		var anchors = document.getElementsByTagName("a");
		for (var i = 0; i < anchors.length; i++) {
			var a = anchors[i];
			if (a.hash) a.onclick = Tabs.OnClickHandler;
		}

		var contentId;
		if (window.location.hash) contentId = window.location.hash.substring(1);

		var divs = document.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++) {
			var div = divs[i];

			if (div.className.match(/\bcontent_table\b/i)) {
				if (!contentId) contentId = div.id;
				div.id = "_" + div.id;
			}
		}

		if (contentId) Tabs.GoTo(contentId, true);
	}
};

// Functions for handling an AJAX-enabled feedback form.
FeedbackForm = {
	RequestStatus: null,
	RequestStatusText: null,
	Request: null,

	Init: function () {
		// Create an element which will hold a status message.
		var span = document.createElement("span");
		span.className = "requestStatus";
		FeedbackForm.RequestStatus = span;

		var text = document.createTextNode("");
		span.appendChild(text);
		FeedbackForm.RequestStatusText = text;

		var button = document.getElementById("SubmitFeedback");
		button.parentNode.appendChild(span);

		// Set a submit event on the form.
		var form = document.getElementById("FeedbackForm");
		if (form) form.onsubmit = FeedbackForm.OnSubmitHandler;
	},

	OnRequestEventHandler: function () {
		switch (FeedbackForm.Request.readyState) {
			case 4:
				var d = FeedbackForm.Request.responseText;

				// Either the script returns something starting with "OK",
				// or it's an error.
				if (d.substring(0, 2) == "OK") {
					// If the submission went OK, clear the form.
					FeedbackForm.RequestStatus.style.color = "#080";
					document.getElementById("Name").value = "";
					document.getElementById("Email").value = "";
					document.getElementById("Message").value = "";

					// Cut out the "OK " part.
					d = d.substring(3);
				} else {
					// Make the message dreadfully red.
					FeedbackForm.RequestStatus.style.color = "#c00";
				}

				// Show the message that was returned.
				FeedbackForm.RequestStatusText.nodeValue = d;

				// Enable the form.
				document.getElementById("Name").disabled = "";
				document.getElementById("Email").disabled = "";
				document.getElementById("Message").disabled = "";
				document.getElementById("SubmitFeedback").disabled = "";
			break;
		}
	},

	OnSubmitHandler: function () {
		// Get a request object.
		var r = null;
		if (FeedbackForm.Request) {
			r = FeedbackForm.Request;
		} else {
			if (window.XMLHttpRequest) {
				try {
					r = new XMLHttpRequest();
				} catch (e) {
					r = null;
				}
			}

			if (!r && window.ActiveXObject) {
				try {
					r = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						r = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						r = null;
					}
				}
			}

			if (!r && window.createRequest) {
				try {
					r = window.createRequest();
				} catch (e) {
					r = null;
				}
			}

			FeedbackForm.Request = r;
		}

		// No request object could be created, proceed with standard
		// form submission.
		if (!r) return true;

		// Disable the controls while waiting for a response.
		document.getElementById("Name").disabled = "disabled";
		document.getElementById("Email").disabled = "disabled";
		document.getElementById("Message").disabled = "disabled";
		document.getElementById("SubmitFeedback").disabled = "disabled";

		// Notify the user that the script is waiting.
		FeedbackForm.RequestStatus.style.color = "#000";
		FeedbackForm.RequestStatusText.nodeValue = "Please wait...";

		// Send the request.
		r.open("POST", "feedback.php?xhr=1", true);
		r.onreadystatechange = FeedbackForm.OnRequestEventHandler;
		r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		r.send("name=" + encodeURIComponent(document.getElementById("Name").value) + "&email=" + encodeURIComponent(document.getElementById("Email").value) + "&message=" + encodeURIComponent(document.getElementById("Message").value));

		// Cancel the standard form submission.
		return false;
	}
};

// Hook up the OnLoad event to the tab initialization function.
//window.onload = function () { Tabs.Init(); FeedbackForm.Init(); };
window.onload = function () { Tabs.Init(); };

// Hide the content while waiting for the onload event to trigger.
var contentId = window.location.hash || "#Introduction";

if (document.createStyleSheet) {
	var style = document.createStyleSheet();
	style.addRule("div.content_table", "display: none;");
	style.addRule("div" + contentId, "display: block;");
} else {
	var head = document.getElementsByTagName("head")[0];
	if (head) {
		var style = document.createElement("style");
		style.setAttribute("type", "text/css");
		style.appendChild(document.createTextNode("div.content_table { display: none; } div" + contentId + " { display: block; }"));
		head.appendChild(style);
	}
}
