﻿// USAGE:
//	The default button on a page should have class "btnDefault".  Pressing Enter on a textbox
//	or password textbox will trigger the CLICK event of the button with css class "btnDefault".
//	A page can only have one default button.  To force a default button per div/panel, use the
//	clickButton() function instead.

// Javascript / jQuery that is used by most of this project.  If you have a script that's only
//	used on one page, just leave the script in that page for code clarity and maintainability.
//	The only real advantage of having this in a standalone file is the ability to cache it in
//	the user's web browser, and we don't want to cache a lot of data that isn't used often.

// This code runs whenever a page is finished loading.  Anything done here will be truly
//	"global", and will override whatever UI you've coded in the .aspx.vb files.  This has
//	the advantage over normal javascript because .ready() functions can be stacked on top
//	of each other - all .ready() functions will run, regardless of if they're in the basepage,
//	contentpage, or controls.
$(document).ready(function() {
	// Create an event listener instead of scanning the dom for something like .SubmitOnEnter on each keypress.
	// We need this because form.defaultbutton and form.defaultfocus only fire after something has been typed
	// on the form.  The exception is when certain types of asp:validator controls are used, but rather than code a 
	// bunch of exceptions we're just going to blanket this across the ssl project until we have a reason to be more selective.
	$('.main_content input:text:visible').bind('keypress', function(e) {
		if ($(this).parents("form").find(".btnDefault").length <= 0)
			return true;
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$(this).parents("form").find(".btnDefault").click();
			return false;
		} else {
			return true;
		}
	});
	$('.main_content input:password:visible').bind('keypress', function(e) {
		if ($(this).parents("form").find("btnDefault").length <= 0)
			return true;
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$(this).parents("form").find("btnDefault").click();
			return false;
		} else {
			return true;
		}
	});
	$('#recaptcha_response_field').bind('keypress', function(e) {
		if ($(this).parents("form").find("btnDefault").length <= 0)
			return true;
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$(this).parents("form").find("btnDefault").click();
			return false;
		} else {
			return true;
		}
	});

	// Bind focus to the first input field of .main_content.  If scrollx or scrolly is not zero, then this is a postback.
	if ( ($("#__SCROLLPOSITIONX").text() == "0") && ($("#__SCROLLPOSITIONX").text() == "0") ) {
		$(".main_content input:visible:first", document.forms[0]).focus();
	}
});

// This function triggers a button click from the aspx.  It doesn't save a postback, but
//	lets us trigger this behavior without codebehind.  
function clickButton(e, buttonref) {
	var bt = document.getElementById(buttonref);
	if (typeof bt == 'object') {
		if (navigator.appName.indexOf("Netscape") > (-1)) {
			if (e.keyCode == 13) {
				bt.click();
				return false;
			} 
		}
		if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1)) {
			if (event.keyCode == 13) {
				bt.click();
				return false;
			} 
		} 
	} 
} 

