//showbox show/hide
var showBoxBool = false;
function showBoxShow() {
	if (document.getElementById("showboxdiv")) {
		if (showBoxBool == true) {
			document.getElementById("showboxdiv").style.visibility="visible";
			document.getElementById("mask").style.visibility="visible";
			showBoxBool = false;
		} else {
			document.getElementById("showboxdiv").style.visibility="hidden";
			document.getElementById("mask").style.visibility="hidden";
			showBoxBool = true;
		}
	}
}

//showbox hide
function showBoxHide() {
	if (document.getElementById("showboxdiv")) {
		document.getElementById("showboxdiv").style.visibility="hidden";
		document.getElementById("mask").style.visibility="hidden";
	}
}

//AJAX show loading..... gif
function displayLoading(element) {
	while (element.hasChildNodes()) {
		element.removeChild(element.lastChild);
	}
	var image = document.createElement("img");
	image.setAttribute("src","images/loading.gif");
	image.setAttribute("alt","Loading...");
	element.appendChild(image);
}

//AJAX XMLHttpRequest
function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	}
	return xhr;
}

//AJAX onreadystatechange handler
function grabFile(file) {
	var request = getHTTPObject();
	if (request) {
		displayLoading(document.getElementById("loading"));
		request.onreadystatechange = function() {
			parseResponse(request);
		};
		request.open("GET", file, true);
		request.send(null);
		return true;
	} else {
		return false;
	}
}

//AJAX response handler
function parseResponse(request) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var details = document.getElementById("showboxdiv");
			details.innerHTML = request.responseText;
			document.getElementById("showboxdiv").style.visibility="visible";
			document.getElementById("mask").style.visibility="visible";
		}
	}
}

//AJAX prepare links for onclick()
function prepareLinks() {
	if (!document.getElementById || !document.getElementsByTagName) {
		return;
	}
	if (!document.getElementById("steps")) {
		return;
	}
	var list = document.getElementById("steps");
	var links = list.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		links[i].onclick = function() {
			var query = this.getAttribute("href").split("?")[1];
			var url = "showbox.php?"+query;
			return !grabFile(url);
		};
	}
	document.getElementById("mask").style.height = document.body.offsetHeight + 500;
}

//AJAX load prepareLinks()
window.onload = prepareLinks;