var xmlHttp = createXmlHttpRequestObject();

function getPhotoCaption()  {
	processPhotoCaption();
}

function processPhotoCaption()  {
	if (xmlHttp)  {
		try  {
			var photoID = document.photoinfo.photoList.options[document.photoinfo.photoList.options.selectedIndex].value;
			<!--var theID=photoID.split(",")-->
			document.photoinfo.photoalterList.value=photoID;
			xmlHttp.open("GET","getphotocaption.php?photoID="+photoID,true);
			xmlHttp.onreadystatechange = handleRequestStateChangePhotoCaption;
			xmlHttp.send(null);
		}
		catch (e)  {
			alert("Can't connect to the server: \n" + e.toString());
		}
	}
}

function handleRequestStateChangePhotoCaption()  {
	if (xmlHttp.readyState == 4)  {
		if (xmlHttp.status == 200)  {
			try  {
				handleServerResponsePhotoCaption();
			}
			catch (e)  {
				alert("Error reading the response: " + e.toString());
			}
		} else {
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponsePhotoCaption()  {
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)  {
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror")  {
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	}
	xmlRoot = xmlResponse.documentElement;
	valueArray = xmlRoot.getElementsByTagName("value");
	for (var x=0;x<valueArray.length;x++)  {
		if (valueArray.item(x).firstChild.data == ".")  {
			document.photoinfo.caption.value="";
			document.photoinfo.charLeft.value=150;
		} else {
			document.photoinfo.caption.value=valueArray.item(x).firstChild.data;
			document.photoinfo.charLeft.value=(150-valueArray.item(x).firstChild.data.length);
		}
	}
}

function createXmlHttpRequestObject()  {
	var xmlHttp;
	try  {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)  {
		var xmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
		for (var i=0;i<xmlHttpVersions.length && !xmlHttp; i++)  {
			try  {
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			}
			catch (e)  { }
		}
	}
	if (!xmlHttp)  {
		alert("Error creating the XMLHttpRequest object.");
	} else {
		return xmlHttp;
	}
}
