

// CheckOnLoad is called standard in the body onLoad of all PM 2.5 instances.
function CheckOnLoad( ) 
{	
	// KeyPressHandlingSetup should always exist because it's in this file
	if( window.KeyPressHandlingSetup )
	{
		KeyPressHandlingSetup();
	}
	
	// InstanceCheckOnLoad is optional
	if( window.InstanceCheckOnLoad )
	{
		InstanceCheckOnLoad();
	}
}

// CheckOnResize is called standard in the body onResize of all PM 2.5 instances.
function CheckOnResize() 
{
	if(window.RunOnResize) 
	{
		RunOnResize() 
	}
}



//returns the form prefix extracted from a field name
function getFieldPrefix(fieldName) 
{
	var prefix = fieldName.match(/^ASPIP[0-9]+_[0-9]+_/i)[0];
	return prefix==null?'':prefix;
}



// include the Form Validation javascript - _required, _validEmail etc.
document.write("<script type=\"text/javascript\" src=\"/svr_pmroot/svr_includes/js/checkForm_insert.js\"></script>");



// init event on valid form elements to assign themselves to the above var when they get focus
function KeyPressHandlingSetup()
{
	// for valid clients
	if ( document.getElementById )
	{
		// for each of the forms on the page
		for ( var f=0 ; f < document.forms.length ; f ++ )
		{
			// for each item in the form
			for ( var i=0 ; i < document.forms[ f ].elements.length ; i ++ )
			{
				// get the current form element	
				var currentElement = document.forms[ f ].elements[ i ];
					
				// check it's not a text area
				if( currentElement.type != "textarea" && currentElement.type != "hidden" )
				{
					// assign the onkeypress event handler
					if( currentElement.onkeypress != null )
					{
						currentElement.onkeypress += KeyPressHandler;
					}
					else
					{
						currentElement.onkeypress = KeyPressHandler;
					}									
				}
			}
		}
	}
	
}// END function KeyPressHandlingSetup()
// handler for all KeyPresses
function KeyPressHandler( e )
{
	// set up srcElement variable to gather either
	// event.srcElement (IE) or
	// e.target (NS/Moz)
	var srcEl = ( e ) ? e.target : window.event.srcElement;
	var srcEvent = ( e ) ? e : window.event;
	var srcForm = srcEl.form;
	// handle mac charCode instead of win KeyCode
	var srcKey = ( srcEvent.charCode ) ? srcEvent.charCode : srcEvent.keyCode;
	var passedSrcEl = false;
	var clickButton = null;

	
	// if the current input is not a textarea or 
	if( srcEl.type != "textarea" )
	{
		// if the key pressed was an enter character
		// 13 = PC .. 3 = Mac
		if( srcKey == 13 || srcKey == 3 )
		{					
			// for each item in the form
			for ( var i=0 ; i < srcForm.elements.length ; i ++ )
			{			
				var currentElement = srcForm.elements[ i ];
				
				// check if the element is the current element id
				if ( currentElement.id == srcEl.id && currentElement.name == srcEl.name )
				{
					passedSrcEl = true;
				}
				
				// check it's not a text area
				if( passedSrcEl == true && currentElement.type == "submit" )
				{
					// pass the clickbutton back
					clickButton = currentElement;
					// break out of the loop
					// i don't like doing this but it's necessary
					break;
				}
			}
		}
	}
	
	// if we're to click a button
	if( clickButton != null )
	{
		// run the onClick event for the button
		clickButton.click();
		// return false to avoid the browser thinking you clicked more than one button.
		return false;
	}
} // END function KeyPressHandler()

