var formCheckboxes = new Array();
var maxLength = 5;

addLoadEvent(fiveMaxInit);

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


function fiveMaxInit() {
	if(!document.getElementById('myform')) return false;
	var inputs = document.getElementById('myform');
	for(i = 0; i < inputs.length; i++) {
		if(inputs[i].type == "checkbox") {
			formCheckboxes[formCheckboxes.length] = inputs[i];
			inputs[i].prototype = new checkbox(inputs[i]);
		}
	}
}

function checkbox(me) {
	me.onclick = doChecked;
	function doChecked() {
		if(me.checked) {
			if(toManySelected()) {
				me.checked = false;
				alert("Five books are the most you are allowed to request at this time.\nTo request this book please first unselect another book.");
			}
		}
	}
}

function toManySelected() {
	var numSelected = 0;
	for(i = 0; i < formCheckboxes.length; i++) {
		if(formCheckboxes[i].checked == true) {
			numSelected += 1;
		}
	}
	if(numSelected  > maxLength) {
		return true;
	} else {
		return false;
	}
}
