function handleHeaderCheckboxClick() {
	var checked = this.checked;
	$('.tableStyle td.selectControl :checkbox').each(function() {
		this.checked = checked;
	});
	return handleCheckboxClick();
}

function handleCheckboxClick() {
	var count = 0;
	var allowed = [];

	//Count number of checkboxes that are now ticked and collect custom IsAllowed callbacks
	$(".tableStyle td.selectControl :checkbox:checked").each(function() {
		if (typeof this.isAllowedButton != "undefined") {
			allowed.push(this.isAllowedButton);
		}
		count++;
	});

	switch (count) {
	case 0:
		$('.selectAction .singleAction, .selectAction .multipleAction').attr('disabled', true).addClass('disabled');
		break;
	case 1:
		$('.selectAction .singleAction, .selectAction .multipleAction')
			.each(function() {
				if (allowed.length == 0 || allowed[0].call(this, this.id)) {
					$(this).attr('disabled', false).removeClass('disabled');
				} else {
					$(this).attr('disabled', true).addClass('disabled');
				}
			})
		break;
	default:
		$('.selectAction .singleAction').attr('disabled', true).addClass('disabled');
		$('.selectAction .multipleAction')
			.each(function() {
				var enable = true;
				for (var i=0; i<allowed.length && enable; i++) {
					enable &= allowed[i].call(this, this.id);
				}
				if (enable) {
					$(this).attr('disabled', false).removeClass('disabled');
				} else {
					$(this).attr('disabled', true).addClass('disabled');
				}
			});
		break;
	}
	return true;
}

$(document).ready(function() {
	//Stripe table rows
	$(".tableStyle").each(function() {
		$("tr", this).removeClass("altRowTable").removeClass("rowTable");
		$("tr:odd", this).addClass("rowTable");
		$("tr:even", this).not(":first-child").addClass("altRowTable");
	});

	$('#clear-button').click(function(){
		var fieldset = $(this).parents('fieldset');
		$(':text, :password, textarea', fieldset).val("");
		$(':radio', fieldset).reverse().each(function() {
			this.checked = true;
		});
		$(':checkbox', fieldset).reverse().each(function() {
			this.checked = false;
		});
		$('select', fieldset).each(function() {
			this.selectedIndex = 0;
		});
		return false;
	});

	$('.tableStyle th.selectControl :checkbox').click(handleHeaderCheckboxClick);
	$('.tableStyle td.selectControl :checkbox').click(handleCheckboxClick);

	//Enable/disable buttons depending on checked state of checkboxes
	handleCheckboxClick();
	$(':button:disabled, :submit:disabled').addClass('disabled');
	$(':button:enabled, :submit:enabled').removeClass('disabled');

	$('.deleteButton').click(function() {
		return confirm("Are you sure you want to permanently delete all selected items?");
	});
	$('.deleteAllButton').click(function() {
		return confirm("Are you sure you want to permanently delete all items corresponding with the current search fields?");
	});
	$('.purgeButton').click(function() {
		return confirm("Are you sure you want to permanently purge all selected items?");
	});

	// bust out of frames
	/*
	$('#xrater-logo').each(function(){
		// if logo exists, we have the top frame UI
		if (top != self) {
			top.location.href = document.location.href;
		}
	});
	*/
});

