
 /************************************
****************************************
	Submit-by-return-key ASP.NET fix v1.0

	Subsero A/S (subsero.dk)
****************************************
 **************************************/

/*
::: Purpose :::

A page in ASP.NET can only contain <form runat="server"> element.
This means that when you have multiple "logical forms" on one page - for instance a contact form an a search form.
When you press the return key, an arbitrary logical form will be submitted, and you will need to press their respective submit buttons to get a correct result.
Needles to say that it is not user-friendly to have the user spend 10 minutes filling out the contact form, and then getting a search result displayed when they press enter.

::: Usage instructions :::

Surround each element with a span tag with an ID of formsection_ followed by the ID of the submit-button that should be pressed when the user presses enter/return in one of the contained form elements

Example:

	<form runat="server">

		<span id="formsection_SearchBtn">

			<label for="searchTxt">Search</label><br />
			<input type="text" id="searchTxt" name="searchTxt" runat="server"><br />
			<br />
			<button id="SearchBtn" OnServerClick="SearchBtnClicked" runat="server"></button>

		</span>

		<span id="formsection_ContactBtn">

			<label for="nameTxt">Name</label><br />
			<input type="text" id="nameTxt" name="nameTxt" runat="server" /><br />
			<br />
			<label for="emailTxt">Email</label><br />
			<input type="text" id="emailTxt" name="emailTxt" runat="server" /><br />
			<br />
			<button id="ContactBtn" OnServerClick="ContactBtnClicked" runat="server"></button>

		</span>
	</form>

::: Potential problems :::

This solution result in the onkeyevent being "hi-jacked", so if you have other javascript libraries with similar behaviour, there might be conflicts.
You should still be able to use onkeydown event handlers on other elements on you page, but that has not been thouroughly tested (e.g. on text inputs for "you have 230 characters left" functionality).

::: History :::

v1.1 (Torben Rohde, 4/7-2007)
--------------------------------
Bug-fix: Pressing enter in a password type input-field was not caught. Added "type == 'password'" in NetscapeEventHandler_KeyDown and MicrosoftEventHandler_KeyDown


v1.0 (Torben Rohde, 25/4-2007)
--------------------------------
Initial version
*/

	
//Checks a return key press and clicks the appropriate submit button (if any, otherwise the event proceeds with whatever it was doing)
function handleFormReturnKeyPress(eventOriginNode)
{
	var currentNode = eventOriginNode;
	var buttonToPressID = null;
	var buttonToPressObj = null;
	while (currentNode.parentNode != null)
	{
		if (currentNode.tagName == "FORM")
		{
			break;
		}
		if (currentNode.id.indexOf("formsection_") != -1)
		{
			buttonToPressID = currentNode.id.substring(12, currentNode.id.length);
			break;
		}
		currentNode = currentNode.parentNode;
	}

	if (buttonToPressID != null)
	{
		buttonToPressObj = document.getElementById(buttonToPressID)
		if (buttonToPressObj != null)
		{
			buttonToPressObj.click();
			return false;
		}
	}

	//We did not catch any span with a formsection_ ID, so let the event continue (or the button ID did not match any element on the page)
	return true;

}


//Hooks up to all return key presses on the page and clicks the appropriate submit button if the element is a form element such as text or select
var isNetscape = window.Event ? true : false; 
if (isNetscape) 
{ 
	window.captureEvents(Event.KEYDOWN); 
	window.onkeydown = NetscapeEventHandler_KeyDown;
}
else {
	document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) 
{ 
	if (e.which == 13 &&
			(e.target.type == 'text' || e.target.type == 'password'  || e.target.type == 'radio' || e.target.type == 'checkbox' || e.target.type == 'select')
	)
	{
		return handleFormReturnKeyPress(e.target);
	} 
	return true; 
} 

function MicrosoftEventHandler_KeyDown() 
{
	if (event.keyCode == 13 && 
		(event.srcElement.type == 'text' || event.srcElement.type == 'password' || event.srcElement.type == 'radio' || event.srcElement.type == 'checkbox' || event.srcElement.type == 'select')
	) 
	{
		return handleFormReturnKeyPress(event.srcElement);
	}
	return true; 
}
