/*
	Package: squash
	
	The squash package provides static containers and methods for automatically
	'Squashing' a submit button once it has been clicked - thus preventing a
	double submit error.
	
	NOTE: this package exists outside the 'Bepress' package because this file
	is inserted into the page 'at will' and possibly multiple times and there's
	less surity that the bepress-init.js script will already have been included.
	
	To use these features a CGI need only include this .js file and thereafter
	push the field names of the submit buttons to protect on the 
	<squash.fieldNames> array.  Thereafter any form containing these buttons must
	declare an onsubmit handler of squash.squashDoubleSubmits, passing the form
	as the function argument.  e.g.:
	
	<script src="/assets/cgi/js/squashDoubleSubmits.js"></script>
	<script type="text/javascript">
		squash.fieldNames.push( 'submission_btn', 'upload_btn' );
		squash.fieldNames.push( 'and_this_too' );
	</script>
	
	<form onsubmit="return squash.squashDoubleSubmits( this )">
	...etc...
	
	This package uses the window.onunload event handler to reset buttons to
	their original values when the page unloads, allowing users to hit their
	browser back button to return to a fully functional page.
*/

// intialize the squash 'namespace' and elements if it has not already
// been initialized
if ( typeof squash == 'undefined' )
{
	var squash = {};
	squash.fieldNames = new Array();
	squash.activeForms = {};
	
	// methods
	
	// squash.resetSquashedSubmits
	// This can be called at any time to reset the buttons being controlled
	// to their original 'values'.  It is most appropriate as a window.onunload
	// event handler and is used for that by default.
	squash.resetSquashedSubmits = function()
	{
		var
			// a form elment within the loop
			aForm,
			
			// the form identifier name for aForm 
			// (used as property name inside the squash.activeForms object)
			squashedFormProp,
			
			// used to iterate over the fields in squash.activeForms
			aField,

			// loop counter
			i			
			;
			
		for ( i = 0; i < document.forms.length ; i++ )
		{
			aForm = document.forms[ i ];
			
			squashedFormProp = 'form_' + aForm.id;
			
			// 
			
			for ( aField in squash.activeForms[ squashedFormProp ] )
			{
				if ( aForm.elements[ aField ] )
				{
					// 
					aForm.elements[ aField ].value = squash.activeForms[ squashedFormProp ][ aField ];
					// 
				}
			}
		}
	}

	// squash.squashDoubleSubmits
	// This method should be used as a form.onsubmit handler by any form wishing
	// to squash double submits.
	squash.squashDoubleSubmits = function( inForm )
	{
		var
			// the form identifier name for aForm 
			// (used as property name inside the squash.activeForms object)
			squashedFormProp,

			// used to iterate over the fields in squash.activeForms
			aField,
			// loop counter
			i
			;
		
		// Check each squash.fieldNames within the form to see
		// if it has altered and return false if it/any has.
		for ( i = 0; i < squash.fieldNames.length; i++ )
		{
			if ( inForm.elements[ squash.fieldNames[ i ] ].value == 'Processing...' )
			{
				//alert( "Double-click" );
				return false;
			}
		}
		
		// The original value for the submit buttons will be saved into the
		// squash.activeForms object, so that later the original value can
		// be restored on window unload.
		
		// Construct a property name for the individual form so that it
		// can be used as a property of the squash.activeForms 'object'.
		squashedFormProp = 'form_' + inForm.id;
		
		if ( ! squash.activeForms[ squashedFormProp ] )
		{
			// initialize it as a new object
			squash.activeForms[ squashedFormProp ] = {};
		}
	
		// Check each field name listed in squash.fieldNames against
		// the form.  It is possible that there are multiple forms on the
		// page all using these 
		for ( i = 0; i < squash.fieldNames.length; i++ )
		{
			aField = squash.fieldNames[ i ];
			if ( inForm.elements[ aField ] )
			{
				squash.activeForms[ squashedFormProp ][ aField ] = inForm.elements[ aField ].value;
				inForm.elements[ aField ].value = 'Processing...';
			}
		}
		
		return true;
	}

	// set the window.onunload handler to reset the submit button values
	window.onunload = squash.resetSquashedSubmits;
	
} // end if squash is undefined

/*
	Function: squash_double_submits
	
	** deprectated **
	This top-level function is what old scripts and templates would set the
	form onsubmit handler to.  It now calls the <squash.squashDoubleSubmits>
	method and will be removed in a future release.

*/
function squash_double_submits( inForm )
{
	return squash.squashDoubleSubmits( inForm );
}
