Object.prototype.contains = function(datum) {
	for (var i in this) {
		if (this[i] == datum) {
			return true;
		}
	}
	return false;
}

String.prototype.startsWith = function(needle) {
	if (this.substr(0, needle.length) == needle) {
		return true;
	} else {
		return false;
	}
}

function allBlock(allBox) {
	var myForm = allBox.form;
	var choiceBoxes = new Array('areas[]', 'schools[]', 'levels[]', 'statuses[]', 'worker[]');
	var allBoxes = new Array('mailall', 'maileveryone');

	//can't use var i in myForm.elements because of IE 7
	for (var i=0;i<myForm.elements.length;i++) {
		element = myForm.elements[i];
		
		if (choiceBoxes.contains(element.name) || element.name.startsWith('exclude')) {
			//this is a check box for choosing a category or class
			if (allBox.checked) {
				if (element.name.startsWith('exclude')) {
					element.checked = false;
				} else {
					element.checked = true;
				}
				element.disabled = true;
			} else {
				element.checked = false;
				element.disabled = false;
			}
		} else if ((allBox.name != element.name) && allBoxes.contains(element.name)) {
			//this is an all box, but not the one we just changed
			element.checked = false;
		}
	}
}

function uniqueOption(chosenOption, otherOptions) {
	if (chosenOption.checked) {
		myForm = chosenOption.form;
		if (otherOption = document.getElementById(otherOptions)) {
			if (otherOption.checked) {
				otherOption.checked = false;
			}
		}
	}
}

function excludeBlock(excludeBox) {
	if (excludeBox.checked) {
		var myForm = excludeBox.form;
		for (var i=0;i<myForm.elements.length;i++) {
			element = myForm.elements[i];
			
			if (element.name.startsWith('exclude')) {
				if (element != excludeBox) {
					element.checked = false;
				}
			}
		}
	}
}


function sure(message) {
	return window.confirm(message);
}

function cleanup() {
	if (sendtoall = document.getElementById('mailall')) {
		if (sendtoall.checked) {
			allBlock(sendtoall);
		}
	}
	
	if (statusRetiree = document.getElementById('status_retiree')) {
		if (statusRetiree.checked) {
			uniqueOption(statusRetiree, 'include_retirees');
		}
	}
}

window.onload = cleanup;
