var xmlHttp = createXmlHttpRequestObject();

function getPhotoList()  {
	document.photoinfo.photoalterList.value="";
	document.photoinfo.caption.value="";
	if (document.photoinfo.photoFolder.options[document.photoinfo.photoFolder.options.selectedIndex].value == "x")  {
		clearSelect2();
	} else {
		processPhoto();
	}
}

function processPhoto()  {
	if (xmlHttp)  {
		try  {
			var folderID = document.photoinfo.photoFolder.options[document.photoinfo.photoFolder.options.selectedIndex].value;
			xmlHttp.open("GET","getphotos.php?folderID="+folderID,true);
			xmlHttp.onreadystatechange = handleRequestStateChangePhoto;
			xmlHttp.send(null);
		}
		catch (e)  {
			alert("Can't connect to the server: \n" + e.toString());
		}
	}
}

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

function handleServerResponsePhoto()  {
	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");
	var html = "";
	clearSelect2();
	for (var x=0;x<valueArray.length;x++)  {
		var thisOption=new Array();
		thisOption["name"] = valueArray.item(x).firstChild.data;
		x++;
		thisOption["id"] = valueArray.item(x).firstChild.data;
		var value=thisOption["id"];
		document.getElementById("photoList").options[document.getElementById("photoList").options.length] = new Option(thisOption["name"],value);
	}
}


function clearSelect2()  {
	var selectList = document.getElementById("photoList");
	while(selectList.lastChild){
    	selectList.removeChild(selectList.lastChild);
	}
}

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;
	}
}
