﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

var lps = function()
{
	/// <summary>
	/// General utilities.
	/// </summary>
	//--------------------------------------------------------------------------		
	function initFormDefaultSubmitKeyListener()
	{
		/// <summary>
		///	http://beardscratchers.com/journal/fixing-the-enter-keypress-event-in-aspnet-with-jquery
		/// </summary>
		/// <param>
		/// </param>
		/// <returns>void</returns>
		var CLASSNAME_PREFIX = 'submit-'; // The prefix of a classname of an input element.
		var CLASSNAME_PREFIX_LENGTH = 7;

		$('fieldset').bind('keypress', function(e)
		{
			var $parentFieldset;
			var classnames;
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			var target = e.target.tagName.toLowerCase();
			

			if (target !== 'input')
			{
				// Do nothing if the focused element isn't an input or a select element.
				return;
			}

			if (key !== 13)
			{
				// Do nothing if the key pressed isn't enter/return.
				return;
			}

			e.preventDefault();
			$parentFieldset = $(e.target).parents('fieldset').filter('[class*=' + CLASSNAME_PREFIX + ']').eq(0);

			if ($parentFieldset.length < 1)
			{
				return;
			}

			classnames = $parentFieldset.attr('class').split(' ');

			for (var i = 0, classname, $buttons, $button; classname = classnames[i]; i++)
			{
				if (classname.substring(0, CLASSNAME_PREFIX_LENGTH) == CLASSNAME_PREFIX)
				{
					$buttons = $($parentFieldset.find('a.' + classname + ', button.' + classname + ', input.' + classname, $(this)).eq(0));

					if ($buttons.length > 0)
					{
						$button = $($buttons.get(0));

						if (typeof ($button.onclick) == 'function')
						{
							// The button has a listener bound to its click event, let's trigger the click.
							$button.trigger('click');
						}
						else if ($button.attr('href'))
						{
							// The button is an a element with a href attribute, let's go to its url.
							window.self.location = $button.attr('href');
						}
						else
						{
							// Simulate a click.
							$button.trigger('click');
						}
					}

					break;
				}
			}
		});
	};
	//--------------------------------------------------------------------------
	function init()
	{
		/// <summary>
		///	Initalises the lps class.
		/// </summary>
		initFormDefaultSubmitKeyListener();
	};
	//--------------------------------------------------------------------------
	return {
		/// <summary>
		/// Public properties and methods.
		/// </summary>
		init: init
	};
	//--------------------------------------------------------------------------	
} ();