/**
 * Client script used by the Aspidistra.Web.OneClickButton control.
 *
 * Copyright Aspidistra Software Ltd 07/01/2006
 */
 
var aspidistraSubmitInProgress = false;

function aspidistraSubmitOK()
{
	return true;
	//return (typeof(Page_BlockSubmit) == 'undefined' || event.returnValue)
}

function aspidistraSubmitOnce( e )
{
    //alert( e );
	//if(aspidistraSubmitInProgress)
	//	return false;
		
	//aspidistraSubmitInProgress = true;
	return true;
}

if(document.addEventListener){ 
document.addEventListener("keypress", HandleEnterKey, true); 
} 
else{ 
document.attachEvent("onkeypress", HandleEnterKey); 
}


// Handle the enter key for a section of a form, binding it to the provided submit buton 
function HandleEnterKey(event) 
{ 
	if (window.Event) 
	{ 
		return NetscapeEventHandler_KeyDown(event); 
	} 
	else 
	{ 
		return MicrosoftEventHandler_KeyDown(); 
	} 
} 

function NetscapeEventHandler_KeyDown(e) 
{ 
	if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') 
	{ 
		e.returnValue = false; 
		e.cancel = true; 
		e.preventDefault(); 
		var att = e.target.attributes['SubmitControl']; 
		if(att!=null) 
			CallSubmit(att.value) ;
		return false; 
	} 
	return true; 
} 

function MicrosoftEventHandler_KeyDown() 
{ 
	if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit') 
	{ 
		event.returnValue = false; 
		event.cancel = true; 
		var att = event.srcElement.attributes['SubmitControl']; 
		if(att!=null) 
			CallSubmit(att.value) ;
		return false; 
	} 
	return true; 
}


// Simulate click on the named submit button - required because asp.net inspects which button was clicked
function CallSubmit( buttonName )
{
	var inputs = document.getElementsByTagName( "input" );
	
	for( var i = 0; i < inputs.length; i++ )
	{
		var input = inputs[ i ];
		
		if( input.type == "submit" || input.type == "image" )
		{
			if( input.name.indexOf( buttonName ) != -1 )
			{
				input.click();
				return;
			}
		}
	}
}
