//*** XmlHttp request and response handling routines

function BeginRequest(photoID, href, viewerName, viewerPhotoName, imgDescription, waitMessageName)
{
    var vals;
    var post;
    var len;
    
	if(GlobalPhotoViewer == null)
		new PhotoViewer(viewerName, viewerPhotoName, imgDescription, waitMessageName);
		
	if(!CanXmlHttpRequest) 
	{
		GlobalPhotoViewer.SetWaitMessage("<p>Sorry, it appears your browser does not support XmlHttpRequest functionality. I'm afraid you can't currently view my photos. (click message to close)</p>");
		GlobalPhotoViewer.ShowWaitMessage(150);
		return false;
	}
	
	//Show wait message
	GlobalPhotoViewer.ShowWaitMessage(150);
	
	//Reset photo
	GlobalPhotoViewer.ResetPhotoViewer("clear.gif");
	
	//Instantiate request
	vals = href.substr(href.indexOf("?") + 1);
	post = vals.substr(vals.indexOf("op=") + 3);
	len = post.indexOf("&") >= 0 ? post.indexOf("&") : post.length;
	currentPhotoID = photoID;
	currentPostID = post.substr(0, len);
	new RemoteRequest(photoID, currentPostID, '/XmlHttpHandler/photo.ashx', HandleResponse, HandleError);
}

var currentPhotoID;
var currentPostID;

function CanXmlHttpRequest()
{
	var canRemote = true;
	
	if(!window.XMLHttpRequest && !window.ActiveXObject)
		 canRemote = false;
	
	return canRemote;
}

function HandleResponse(result)
{
	var responseElement = result.documentElement;
	var imageDescription = responseElement.childNodes[0].firstChild.data;
	var imagePath =  responseElement.childNodes[1].firstChild.data;
	var imageHeight = responseElement.childNodes[2].firstChild.data;
	var imageWidth = responseElement.childNodes[3].firstChild.data;
	var lastPhoto = responseElement.childNodes[4].firstChild.data;
	var nextPhoto = responseElement.childNodes[5].firstChild.data;
	
	GlobalPhotoViewer.SetPhotoViewer(imageDescription, imagePath, imageHeight, imageWidth, currentPostID, currentPhotoID, lastPhoto, nextPhoto);
	GlobalPhotoViewer.HideWaitMessage();
	GlobalPhotoViewer.ShowPhotoViewer();
}

function HandleError(result)
{
	GlobalPhotoViewer.ShowError(result, "1000px", "800px");
}
