// kaskubar.org functions
// copyright 2007-2009 Bruce Kaskubar
var XHR;			// XMLhttpRequest object

function XHRrequest(url, parms, asynch, j) { // AJAX request initiator
// url: document to be retrieved; string
// parms: parameters for the respondent; string
// asynch: whether the request should be asynchronous or not; boolean
// j: javascript to execute after successful receipt; optional string
//	alert('XHRrequest\n' +url +'\n' +parms +'\n' +asynch +'\n' +j);
	try { XHR =new XMLHttpRequest(); }
	catch(e) {
		try { XHR =new ActiveXObject('MSXML2.XMLHTTP'); }
		catch (e) {
			try { XHR =new ActiveXObject('Microsoft.XMLHTTP'); }
			catch (e) {parent.location='forward.html';}
		}
	}
	if (XHR) {
		XHR.onreadystatechange =XHRreceiver;
		XHR.open('POST', url, asynch);
		XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//		XHR.send('XHRurl=' +url +'&XHRdest=' +destination +'&XHRj=' +j);
		XHR.send('');
	}
} //-------------------------------------------------------------------------------

function XHRreceiver() { // AJAX response receiver
	var e1 ="The requested information could not be retrieved. If the problem persists, we would surely appreciate a note or call about the trouble we're causing you.";
	var e2 ="Some information was retrieved but we have no idea what to do with it.";

	if (XHR.readyState !=4) return;
	if (XHR.status !=200) {
		alert(e1 +"\n\n" +XHR.statusText);
		return;
	}
	if (XHR.responseText.substr(0,5) =='ERROR') {
		alert("We're sorry. " +e1 +"\n\n" +XHR.responseText);
		return;
	}
//	alert(XHR.responseText);
	var x =XHR.responseXML;
	switch(x.documentElement.getAttribute('url')) {
		case "showThumbs":
			showThumbnails(x.getElementsByTagName('html')[0].firstChild.nodeValue);
			break;
		case "showPic":
			alert('XHR showPic\n' +getElementsByTagName('HTML')[0].firstChild.nodeValue);
			showPic(x.getElementsByTagName('HTML')[0].firstChild.nodeValue);
			break;
		case "metadata":
			showMetadata(x.getElementsByTagName('html')[0].firstChild.nodeValue);
			break;
		default:
			alert("We're sorry. " +e2 +"\n\n"+XHR.responseText);
			return;
	}
} //-------------------------------------------------------------------------------

