//Dave Howes 6 December 2006. Edited DAH 4/5/07. Copyright HowesComms 2006, 2007.

var displayMsg = true;

function showMsg() {
	var lyrRef = document.getElementById('msg');
	if (displayMsg) {
		lyrRef.style.display = "block";
		displayMsg = false;
		timerRef = window.setTimeout("showMsg()", "3000");
	} else {
		lyrRef.style.display = "none";
		displayMsg = true;
	}
}

function changePic(newPic) {
	document.mainpic.src = "graphic.php?img=" + newPic;
	document.f.main.value = newPic;
	processGet(newPic);
	showMsg();
}

var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject() {
	try {
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			xmlHttp = false;
		}
	}
	if (!xmlHttp) {
		alert("Error: the picture captions can not be displayed with the web browser you are using.");
	} else {
		return xmlHttp;
	}
}

function processGet(lookupID) {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
		xmlHttp.open("GET", "caption.php?id=" + lookupID, true);
		xmlHttp.onreadystatechange = handleResponse;
		xmlHttp.send(null);
	} else {
		setTimeout("processGet()", 1000);
	}
}

function handleResponse() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var xmlResponse = xmlHttp.responseXML;
			var xmlDocumentElement = xmlResponse.documentElement;
			var picCaption = xmlDocumentElement.firstChild.data;
			var pictureText = document.getElementById("picCap");
			pictureText.firstChild.nodeValue = picCaption;
		} else {
			alert("Error - problem accessing the picture caption database. Error message: " + xmlHttp.statusText);
		}
	}
}
