function getXMLHTTP() {
	try
	{
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(ex)
	{
	//either this is not IE, or it is a version of IE which does not support XMLHTTP
		var notIECompatibleXMLHTTP=true;
	}
	if(notIECompatibleXMLHTTP==true)
	{
		try
		{
			request_o = new XMLHttpRequest();
		}
		catch(ex)
		{
			//we can't use AJAX because this browser is not compatible.
			request_o = false;
		}
	}
	return request_o;
}

/*var XMLHTTP_o;
if(XMLHTTP_o=getXMLHTTP())
{
alert('YAY we can do AJAX.');
alert(XMLHTTP_o.toString());
}
else
{
alert('WAAAH not compatible.');
}
*/

/* The variable http will hold our new XMLHttpRequest object. */
var http = getXMLHTTP(); 
var http2 = getXMLHTTP(); 
var tabNext = new Array("photo_title","photo_description","photo_date","photo_title");

/* Function called to get the product categories list */
function getPhoto(photoId, dir){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http.open('get', 'internalRequest.php?function=generatePhoto&photoId=' + photoId 
					+ '&dir=' + dir);
		/*3?action=get_products&id=' 
			+ document.form_category_select.select_category_select.selectedIndex);*/
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handlePhoto; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
	getPhotoTitle(photoId, dir);
}

function getPhotoTitle(photoId, dir){
	http2.open('get', 'internalRequest.php?function=getPhotoTitle&photoId=' + photoId+ '&dir=' + dir);
	http2.onreadystatechange = handlePhotoTitle; 
	http2.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handlePhoto(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('photo').innerHTML = response;
	}
}

function handlePhotoTitle(){
	if(http2.readyState == 4){
		if (http2.status == 200) {
			var response = http2.responseText;
			document.title = response;
			document.getElementById('page_title').innerHTML = response;
		} else {
			alert(http2.status);
		}
	}
}

function handleUpdateTextField1(){
	if(http.readyState == 4){
		if (http.status == 200) {
			var response = http.responseText.split("|");
			document.getElementById(response[0]).innerHTML = response[1];
			var field =document.getElementById(response[0]); 
			field.setAttribute('onClick','changeTextField("'+response[0]+'")');
		} else {
			alert(http.status);
		}
	}
}

function handleUpdateTextField(){
	if(http2.readyState == 4){
		if (http2.status == 200) {
			var response = http2.responseText.split("|");
			document.getElementById(response[0]).innerHTML = response[1];
			var field =document.getElementById(response[0]); 
			field.setAttribute('onClick','changeTextField("'+response[0]+'")');
		} else {
			alert(http2.status);
		}
	}
}

function updatePhotoDescriptionField(fieldName) {
//	alert('Yo!');
	var fieldValue = document.getElementById(fieldName+"_input").value;
	var photoId = document.getElementById('photo_id').value;
	http2.open('get', 'internalRequest.php?function=updatePhotoDescriptionField&photoId=' + photoId + '&fieldName=' + fieldName +'&fieldValue=' + fieldValue);
	http2.onreadystatechange = handleUpdateTextField;
	http2.send(null);

}

function changeTextField(fieldName)
{
	var field =document.getElementById(fieldName); 
	field.setAttribute('onclick','');
	field.innerHTML = '<input type="string" id="' + fieldName + '_input" value="'+ document.getElementById(fieldName).innerHTML  +'"/>';
	field.setAttribute('onkeydown','if( event.keyCode==9 ) {changeTextField("'+tabNext[tabNext.indexOf(fieldName)+1]+'");};');
	field.setAttribute('onchange','updatePhotoDescriptionField("'+ fieldName +'");');
//	field.childNodes[0].focus();
	field.childNodes[0].setAttribute('onBlur','updatePhotoDescriptionField("'+ fieldName +'");');
};

function changeLocation()
{
	var photoId = document.getElementById('photo_id').value;
	locationWindow = window.open("gmaps.php","locationWindow");
}

function updateLocationWrapper(lng, lat)
{
	window.setTimeout('updateLocation('+lng+', '+lat+');',0);
}

function updateLocation(lng, lat)
{
	var photoId = document.getElementById('photo_id').value;
	var fieldName = 'photo_location_lat';
	// alert("Yo!" + lng + ", " +lat);
	http2.open('get', 'internalRequest.php?function=updatePhotoDescriptionField&photoId=' + photoId + '&fieldName=' + fieldName +'&fieldValue=' + lng);
	http2.onreadystatechange = handleUpdateTextField;
	http2.send(null);

	fieldName = 'photo_location_lng';
	http.open('get', 'internalRequest.php?function=updatePhotoDescriptionField&photoId=' + photoId + '&fieldName=' + fieldName +'&fieldValue=' + lat);
	http.onreadystatechange = handleUpdateTextField1;
	http.send(null);
}

