// set focus on:
// 1. first text input of first form on page
// 2. first input that isn't a button, if there are no text inputs or iFrames
// 3. first button, if there are no other qualifying inputs
function focusFirstTextInputOnPage() {
	var foundButton = false;
	var foundNonButton = false;

	// If this page has an item edit form set focus to its first field
	for (var i = 0; i < document.forms.length; i++) {
		var form = document.forms[i];
		if (form.name == 'itemedit') {
			for (var j = 0; j < form.elements.length; j++) {
				var element = form.elements[j];
				if (element.type == "select-one" || element.type == "text" || element.type == "textarea" || element.type == "password") {
					element.focus();
					return;
				}else if (!foundNonButton && (element.type == "checkbox" || element.type == "radio")) {
					// note: don't include "select-one" since mouse-wheeling will change the selection, rather than scrolling the page 
					element.focus();
					foundNonButton = true;
				} else if (!foundButton && !foundNonButton && (element.type == "submit" || element.type == "button")) {
					element.focus();
					foundButton = true;
				}
			}
		}
	}

	// If not set to the first field on the first form
	for (var i = 0; i < document.forms.length; i++) {
		var form = document.forms[i];
		for (var j = 0; j < form.elements.length; j++) {
			var element = form.elements[j];
			// alert(element.name + "\nis a\n" + element.type);
			if (element.type == "text" || element.type == "textarea" || element.type == "password") {
				// alert("focusing " + element.name);
				element.focus();
				return;
			} else if (!foundNonButton && (element.type == "checkbox" || element.type == "radio")) {
				// note: don't include "select-one" since mouse-wheeling will change the selection, rather than scrolling the page 
				// alert("focusing " + element.name);
				element.focus();
				foundNonButton = true;
			} else if (!foundButton && !foundNonButton && (element.type == "submit" || element.type == "button")) {
				// alert("focusing " + element.name);
				element.focus();
				foundButton = true;
			}
		}
	}
}