// Define variable used for checking.
var Loading = false;

// Define function for AJAX-inclusion.
function AJAXinclude (Page) {
	// Define neccesary variables, and retrieve the target elements.
	var PageReq = false;
	var TargetObj = document.getElementById ("content_wrap");
	var ParentObj = document.getElementById ("mid");
	var Block = document.createElement ("div");
	Block.innerHTML = '';

	// Check if page is to be reset.
	if (Page == '') {
		// Display original element again, and remove AJAX-added child node.
		TargetObj.style.display = "block";
		ParentObj.removeChild (ParentObj.lastChild);

		// Stop HTML from working.
		return false;
	} else {
		// New page display, check if already loading.
		if (Loading) {
			// Is, stop any further action.
			return false;
		}

		// Get the element for the "loading" image, and set it to correct src.
		var Image = document.getElementById ("loader_img");
		Image.src = "images/pip/ajax-loader.gif";

		// Define path of server-side handler script.
		Page = "index.php?sub=clean&action=" + Page;

		// Check what type of browser is used.
		if (window.XMLHttpRequest) {
			// If Opera, Mozilla, Safari etc
			PageReq = new XMLHttpRequest ();
		} else if (window.ActiveXObject) {
			// If IE
			try {
				PageReq = new ActiveXObject ("Msxml2.XMLHTTP");
			}

			catch (err) {
				try {
					PageReq = new ActiveXObject ("Microsoft.XMLHTTP");
				}

				catch (err) {
					// No AJAX-compatible IE browser, fallback to HTML-code.
					return true;
				}
			}
		} else {
			// Non AJAX-compatible browser, fallback to HTML-code
			return true;
		}

		// Define that a page is loading.
		Loading = true;

		// Get page synchronously
		PageReq.open ('GET', Page, false);
		PageReq.send (null);

		// Check wether or not request was successful
		if (window.location.href.indexOf ("http") == -1 || PageReq.status == 200) {
			// Was, add the response to the new element created above.
			Block.innerHTML = PageReq.responseText;
		} else {
			// Display generic "404" message.
			Block.innerHTML = "<i>Sorry, no content for this page yet</i>";
		}

		// Hide original block
		TargetObj.style.display = "none";

		// Add new content block to content element.
		ParentObj.appendChild (Block);

		// Remove the loading image.
		Image.src = '/images/blank.gif';

		// Define loading of page complete.
		Loading = false;

		// Stop HTML from working.
		return false;
	}
}