template = function() {	operations(); }


function wrapDiv(idToWrap, wrapperId, wrapperClasses)
{
	var wrapperDiv = createElement('div'),
		toWrap = getElementById(idToWrap),
		parent = toWrap.parentNode;

	if (!(wrapperDiv.id = wrapperId)) wrapperDiv.id = "";
	if (!(wrapperDiv.className = wrapperClasses)) wrapperDiv.className = "";

	parent.insertBefore(wrapperDiv, toWrap);
	wrapperDiv.appendChild(toWrap);
}

function loadingDiv(newDivId, newDivClasses)
{
	var newDiv = createElement("div");
	if (!(newDiv.id = newDivId)) newDiv.id = "";
	if (!(newDiv.className = newDivClasses)) newDiv.className = "";

	var loadingDiv = createElement("div");
	loadingDiv.className = "loading";

	newDiv.appendChild(loadingDiv);

	return newDiv;
}

function divBefore(idRelative, newDivId, newDivClasses, contentURL)
{
	var newDiv = loadingDiv(newDivId, newDivClasses),
		relativeDiv = getElementById(idRelative),
		parent = relativeDiv.parentNode;

	parent.insertBefore(newDiv, relativeDiv);
	loadXMLDoc(contentURL, newDiv);
}

function divAfter(idRelative, newDivId, newDivClasses, contentURL)
{
	var newDiv = loadingDiv(newDivId, newDivClasses),
		relativeDiv = getElementById(idRelative),
		parent = relativeDiv.parentNode;

	parent.insertBefore(newDiv, relativeDiv);
	parent.insertBefore(relativeDiv, newDiv);
	loadXMLDoc(contentURL, newDiv);
}

function layoutSnap()
{
	bodyRef().className += " postload";
}

/* swiped from http://www.w3schools.com/xml/tryit.asp?filename=try_xmlhttprequest_js1 */

function loadXMLDoc(url, targetDiv)
{
	var xmlhttp = getHTTPObject();

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState != 4)
			return;
		if (xmlhttp.status == 200)
		{
			targetDiv.innerHTML = xmlhttp.responseText
		}
	};

	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}


/* idea swiped from the sIFR scripts: http://www.mikeindustries.com/sifr/  */

function createElement(sTagName) { return document.createElement(sTagName); };
function getElementById(sId) { return document.getElementById(sId); };
function bodyRef() { return document.getElementsByTagName("body").item(0); }

/* swiped from Jeremy Keith: http://adactio.com/elsewhere/javascript/ajax.js */

function getHTTPObject() {
	try {
		var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			try {
				var xmlhttp = new XMLHttpRequest();
			} catch (e) {
				var xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}


window.onload = template;
