
/**
 * Show the edit album form
 */
function editAlbum () {
	if ( !document.forms.albumForm ) return false;
	document.forms.albumForm.style.display = "";
	document.forms.albumForm.name.focus();
}


/**
 * Load a given photo int he album viewer
 * @param int photo
 */
function loadPhoto ( photo ) {
	if ( !document.getElementById("mainPhoto") ) return false;
	if ("" == photo || !document.getElementById("photo" + photo)) {
		document.getElementById("mainPhoto").style.width = "0px";
		document.getElementById("mainPhoto").style.height = "0px";
		return false;
	}
	
	// figure the right dimensions
	var bigPhoto = document.getElementById("mainPhoto");
	bigPhoto.style.width = "";
	bigPhoto.style.height = "";
	bigPhoto.src = document.getElementById("photo"+photo).src;
	resizePhoto(bigPhoto,520,390);
		
	// show the caption
	var caption = document.getElementById("photo"+photo+"Caption").innerHTML;
	document.getElementById("mainPhotoCaption").innerHTML = caption;
	
	// setup the form, if available
	if (hidePhotoForm) {
		if (document.forms.photoForm) {
			document.forms.photoForm.photoID.value = photo;
			document.forms.photoForm.caption.value = caption;
			document.forms.photoForm.rotate.value = "";
		}
	}
	
	bigPhoto.scrollIntoView();
}


/**
 * Resize the given photo to fit within the specified restraints
 * @param {Object} photo
 * @param int width
 * @param int height
 */
function resizePhoto ( photo, widthMax, heightMax ) {
	if ( !photo ) return false;
	var width = photo.width;
	var height = photo.height;
	var factor;
	if ( width > height ) factor = widthMax/width;
	else factor = heightMax/height;
	if ( factor > 2 ) factor = 2;  // don't blow up more than 2x
	photo.style.width = (factor * width)+"px";
	photo.style.height = (factor * height)+"px";	
}


/**
 * Setup and show the form to edit the photo
 */
function editPhoto () {
	if ( !document.forms.photoForm ) return false;	
	document.forms.photoForm.caption.value = document.getElementById("mainPhotoCaption").innerHTML;
	document.forms.photoForm.rotate.value = "";
	document.getElementById("photoForm").style.display = "";
	document.forms.photoForm.caption.focus();
}


/**
 * Hide the photo edit form
 * @return boolean Success
 */
function hidePhotoForm () {
	if ( !document.forms.photoForm ) return false;	
	document.forms.photoForm.style.display = "none";
	return true;
}


/**
 * Set up the form to rotate the photo
 */
function rotatePhoto ( rotate ) {
	if ( !document.forms.photoForm ) return false;	
	document.forms.photoForm.rotate.value = rotate;
	document.getElementById("photoFormSave").click();
}


/**
 * Populate the form
 * @param int file
 */
function editFile ( file ) {
	if ( !document.getElementById("file"+file+"Name") ) return false;
	document.forms.fileUploadForm.name.value = document.getElementById("file"+file+"Name").innerHTML;
	document.forms.fileUploadForm.description.value = document.getElementById("file"+file+"Description").innerHTML;
	document.forms.fileUploadForm.fileID.value = file;
	document.forms.fileUploadForm.save.value = "Save";
	document.forms.fileUploadForm.save2.style.display = "";
	document.getElementById("uploadField").style.display = "none";
	document.getElementById("fileFormHeader").innerHTML = "Edit this file";
	setTimeout("document.forms.fileForm.name.focus()",500);
	return true;
}


/**
 * Populate the form
 * @param int file
 */
function editUser ( user ) {
	document.forms.userForm.style.display = "";
	if (!document.getElementById("user" + user + "Email")) {  // no user specified, show the "new user" version
		document.forms.userForm.firstName.value = "";
		document.forms.userForm.lastName.value = "";
		document.forms.userForm.email.value = "";
		document.forms.userForm.password.value = "";
		document.forms.userForm.userID.value = "";
		document.forms.userForm.save.value = "Save";
		document.forms.userForm.save2.style.display = "none";
		document.forms.userForm.firstName.focus();
		alert("boo");
		return true;
	}
	document.forms.userForm.firstName.value = document.getElementById("user"+user+"FirstName").innerHTML;
	document.forms.userForm.lastName.value = document.getElementById("user"+user+"LastName").innerHTML;
	document.forms.userForm.email.value = document.getElementById("user"+user+"Email").innerHTML;
	document.forms.userForm.password.value = document.getElementById("user"+user+"Password").innerHTML;
	document.forms.userForm.userID.value = user;
	document.forms.userForm.save.value = "Update";
	document.forms.userForm.save2.style.display = "";
	document.forms.userForm.firstName.focus();
	return true;
}


/**
 * Validate the form
 * @return boolean Success
 */
function validateUserForm () {
	if ( "" == document.forms.userForm.email.value ) {
		alert("Sorry, email address and password are required.");
		return false;
	}
	if ( "" == document.forms.userForm.password.value ) {
		alert("Sorry, password and email address are required.");
		return false;
	}
	return true;
}



/**
 * Validate the form
 * @return boolean Success
 */
function validateContactForm () {
	if ( "" == document.forms.contactForm.name.value ) {
		alert("Sorry, your name and a message are required.");
		return false;
	}
	if ( "" == document.forms.contactForm.message.value || "Please type your message here." == document.forms.contactForm.message.value ) {
		alert("Sorry, your name and a message are required.");
		return false;
	}
	return true;
}
