function submitForm(formID) {
	document.getElementById(formID).submit();
}

function confirmCancel() {
	msg = 'Are you sure you wish to cancel? All new information will be lost.';
	return confirm(msg);
}

function checkAll(formName, fieldName) {
	alert('check all');
	var boxes = document.getElementById(formName).getElementsByTagName('input');
	
	for (i = 0; i < boxes.length; i++) {
		if (boxes[i].type == 'checkbox') {
			boxes[i].checked = true;
		}
	}
}

function clearAll(formName, fieldName) {
	alert('check all');
	var boxes = document.getElementById(formName).getElementsByTagName('input'); 
	
	for (i = 0; i < boxes.length; i++) {
		if (boxes[i].type == 'checkbox') {
			boxes[i].checked = false;
		}
	}
}

function toggleCheckboxes(formID, fieldName, checked) {
	var thisForm = document.getElementById(formID);
	for (var i = 0; i < thisForm.elements.length; i++)  {
		if (thisForm.elements[i].name == fieldName + '[]') {
			thisForm.elements[i].checked = checked;
		}
	}
}

function submitMultiActions(thisForm, fieldName, actionID) {
	var isValid = true;
	var itemsChecked = 0;
	for (var i = 0; i < thisForm.elements.length; i++)  {
		if (thisForm.elements[i].name == fieldName + '[]') {
			if (thisForm.elements[i].checked) itemsChecked ++;
		}
	}
	actionObj = document.getElementById(actionID);
	thisAction = actionObj.options[actionObj.selectedIndex].value;
	
	if (itemsChecked == 0) {
		isValid = false;
		alert('No items have been selected');
	} else if (thisAction == '') {
		isValid = false;
		alert('No action has been specified');
		actionObj.focus();
	} else if (thisAction == 'active_2') {
		var s = (itemsChecked > 1) ? 's' : '';
		isValid = confirm('You are about to delete ' + itemsChecked + ' selected item' + s + '. Continue?');
	}
	
	return isValid;
}

function timeInputPrompt(initialVal) {
	timeVal = initialVal;
	
	do {
		isValid = false;
		var msg = 'Enter a time\n';
		msg += '\nThe value can be in 24 hour or AM/PM format';
		msg += '\ne.g. 3pm, 15:40, 3:40pm, 03.40PM, 3:40p\n';
		msg += '\nLeave blank for no time (e.g. an all-day event).';
		var timeVal = prompt(msg, timeVal);
		
		if (timeVal == null) {
			// cancelled prompt, return initial val
			timeVal = initialVal;
			isValid = true;
		} else if (timeVal == '') {
			isValid = true;
		} else if (timeVal.match(/^([0-1]?[0-9]|2[0-3])([:.-][0-5][0-9]|)(am|pm|a|p)?$/i)) {
			// Hours are (0)0 to 23; 
			// (optional) separator is colon, dot or hyphen
			// (optional) mins are 00 - 59
			// AM/PM is either the 1st char or both, not case sensitive
			isValid = true;
		} else {
			alert('The value specified does not appear to be a valid time.');
		}
	} while (!isValid);
	
	return timeVal;
}
