// thanks spammers.  it's hardly worth it.  really!
var FILE_ACTIONS = "siteactions.php";
var COMMENT_FORM = "commentform";

// be sure scriptaculous has loaded before this
function initlist(list, id, colid) {
	if ($(list+"list")) {
		new Element.toggle(list +"list");
		new Ajax.Updater(list +"list", FILE_ACTIONS+"?action=get_"+ list +"list&id="+ id +"&collectionid="+ colid);
	}
}

function addUploadBoxes(placeholderid, copyfromid, num) {
	var placeholder = document.getElementById(placeholderid);
	var copyfrom = document.getElementById(copyfromid);
	var maxboxes = 20;
	
	for (i=0; i<num; i++) {
		if (window.totalinputs >= maxboxes) {
			alert("If "+ maxboxes +" upload boxes isn't enough you can finish this batch and come back to upload more.")
			return;
		}
		var newdiv = document.createElement('div');
		newdiv.innerHTML = copyfrom.innerHTML;
		newdiv.className = copyfrom.className;
		placeholder.parentNode.insertBefore(newdiv, placeholder);
		window.totalinputs++;
	}
}

function isEmpty(field) {
	if (field.value == "" || field.value == null) {
		return true;
	} else {
		return false;
	}
}

function highlightFocus(field) {
	field.focus();
	field.className = "highlightfield";
}

function validate(f, rand) {
	var isValid = false;

	// upload form
	if (f.name == "photoupload") {
		if (isEmpty(f.contributor)) {
			alert("Please enter your name.");
			highlightFocus(f.contributor);
		} else if (isEmpty(f.collection_id)) {
			alert("Please select a party to add these pictures to.");
			highlightFocus(f.collection_id);
		} else {
			isValid = true;
		}
	}

	// comment form
	if (f.name == COMMENT_FORM) {
		if (isEmpty(f["yourname"+rand])) {
			alert("Please enter your name.");
			highlightFocus(f["yourname"+rand]);
		} else if (isEmpty(f["comment"+rand])) {
			alert("Please enter your comment.");
			highlightFocus(f["comment"+rand]);
		} else {
			// update via ajax so the page doesn't have to refresh. helpful for commenting without disrupting video playback
			new Ajax.Updater('comments', FILE_ACTIONS, {
							 							asynchronous:true,
														parameters: Form.serialize(COMMENT_FORM)+"&rand="+rand+"&action=addcomment"
													 });
			f.reset();
			new Element.toggle(COMMENT_FORM);
		}
	}

	// invite list form
	if (f.name == "inviteform") {
		if (isEmpty(f.yourname)) {
			alert("Please enter your name.");
			highlightFocus(f.yourname);
		} else if (isEmpty(f.email)) {
			alert("Please enter your email address.");
			highlightFocus(f.email);
		} else {
			isValid = true;
		}
	}
	
	return isValid;
}