// All XML HTTP calls on this page are SYNCHRONOUS!
function DeletePicture(spanid, dir) {
	if (confirm("Delete this picture?")) {
		// Get the span id
		objSpan = document.getElementById(spanid);
		// Call delete on the image
		var imageNode = objSpan.getElementsByTagName('img');
		var command = "delete=1";
		
		http.open("POST", "Pictures/ResizeImage.aspx?im=" + escape(dir) + "/" + escape(spanid), false);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.send(command);
		if (http.responseText == "1") {
			document.getElementById(spanid).style.display = "none";
		} else {
			alert(http.responseText);
		}
	}
}

function Rotate(spanid, dir, degrees) {
	objSpan = document.getElementById(spanid);
	// Call rotate on the image
	var imageNode = objSpan.getElementsByTagName('img');
	var command = "rotate=" + degrees;
	
	http.open("POST", "Pictures/ResizeImage.aspx?im=" + escape(dir) + "/" + escape(spanid), false);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(command);
	if (http.responseText == "1") {
		rTime = new Date();
		imageNode[0].src = "Pictures/" + escape(dir) + "/_" + escape(spanid) + "?r=" + rTime.getMilliseconds();
		objSpan.style.backgroundColor = "green";
	} else {
		alert(http.responseText);
	}
}	

function checkEnter(e, spanid, dir)
{
	if (e && e.keyCode == 13) {
		SavePicture(spanid, dir);
	}
}

function SavePicture(spanid, dir) {
	objSpan = document.getElementById(spanid);
	// Call save on the image
	var imageNode = objSpan.getElementsByTagName('img');
	var titleNode = objSpan.getElementsByTagName('input');
	
	var command = "save=1&title=" + escape(titleNode[0].value);

	http.open("POST", "Pictures/ResizeImage.aspx?im=" + escape(dir) + "/" + escape(spanid) + "&save=true", false);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(command);
	if (http.responseText == "1") {
		objSpan.style.backgroundColor = "green";
	} else {
		alert(http.responseText);
	}
}	

function ShowPicture(dir, image) {
	var dirPic = new Array();
	dirPic[0] = dir;
	dirPic[1] = image;
	dhtmlHistory.add("pic=" + (image) + "&dir=" + (dir), dirPic);

	var url = "Pictures/GetPictures.aspx?dir=" + dir + "&pic=" + image;
	document.getElementById("centercontent").innerHTML = "Retrieving pictures...";
	http.open("GET", url, true); 
	http.onreadystatechange = function() { populatePicture(dir, image); }
	http.send(null);
}

function populatePictures() {
	if (http.readyState == 4) {
		document.getElementById("centercontent").innerHTML = http.responseText;
	}
}

function populatePicture(dir, image) {
	if (http.readyState == 4) {
		document.getElementById("centercontent").innerHTML = http.responseText;
		GetMessages(dir, image);
	}
}

function UpdateCategory(dir) {
	titleinput = document.getElementById("AddPictures1_txtTitle");
	descinput = document.getElementById("AddPictures1_txtDescription");
	monthnum = document.getElementById("AddPictures1_selMonth").options[document.getElementById("AddPictures1_selMonth").selectedIndex].value;
	daynum = document.getElementById("AddPictures1_selDay").options[document.getElementById("AddPictures1_selDay").selectedIndex].value;
	yearnum = document.getElementById("AddPictures1_selYear").options[document.getElementById("AddPictures1_selYear").selectedIndex].value;
	var catLoc = "Pictures/UpdateCategory.aspx?dir=" + escape(dir);
	http.open("POST", catLoc, false);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var command = "title=" + escape(titleinput.value) + "&desc=" + escape(descinput.value) + "&month=" + monthnum + "&day=" + daynum + "&year=" + yearnum;
	http.send(command);
	//	alert(http.responseText);
}