var whichButton;
var focusElementId;

$(document).ready(function() {
	initForms();
});

function initForms() {
	// associate buttons with form elements
	whichButton = new Array();
	whichButton['login_username'] = 'moodleLoginSubmit';
	whichButton['login_password'] = 'moodleLoginSubmit';
	whichButton['searchTerm'] = 'submit';
	whichButton['TextBox1'] = 'btnSubmit';
	whichButton['DropDownList1'] = 'btnSubmit';
	$("input, select").focus( function() {
		focusElementId = $(this).attr("id");
		// alert("focusElementId: " + focusElementId + "\nbuttonId: " + whichButton[focusElementId]);
	});
	
	// add onkeypress event handler
	$("form").keypress(function(e) {
		// if [enter] key
		if(e.which == 13) {
			buttonId = whichButton[focusElementId];
			// trigger click event on appropriate button
			if ( buttonId != undefined ) {
				// alert("click: " + buttonId);
				$("#" + buttonId).click();
				return false;
			} else {
				return true;
			} 
		} else {
			return true;
		}
	});
	
	// add onclick event handler to moodleLogin button
	$("#moodleLoginSubmit").click( function() {
		$("#Form1").attr("action", "https://moodle.selby.ac.uk/login/index.php");
		$("#Form1").submit();
	});
	
}
