

// CheckOnLoad is called standard in the body onLoad of all PM 2.5 instances.
function CheckOnLoad ( ) 
{		
	// 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 = null;
    if ( fieldName.match ( /^ASPIP[0-9]+_[0-9]+_/i ) )
    {
	    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>");

var keyPressHandlingSetupRun = false;

// 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 && ! keyPressHandlingSetupRun )
	{
		keyPressHandlingSetupRun = true;
		// 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()



// addLoadEvent
// Enables us to hook into the browser onload event.
// DEPRECATED: [addLoadEvent] has been deprecated, please use [addPMJavaScriptEvent] to add all javascript events.
function addLoadEvent ( func )
{
  if ( window.addEventListener )
  {
    window.addEventListener ( 'load', func, true );
  }
  else if ( window.attachEvent )
  {
    window.attachEvent ( 'onload', func );
  }
  else
  {
    window.onload = func;
  }
}


// addPMJavaScriptEvent
// Enables us to hook into the browser JavaScript events.
function addPMJavaScriptEvent ( func, domEventName, ieEventName )
{
	//DOM method for binding an event
	if ( window.addEventListener )
	{
		window.addEventListener ( domEventName, func, true );
	}
	//IE exclusive method for binding an event
	else if ( window.attachEvent )
	{
		window.attachEvent ( ieEventName, func );
	}
	//Older browsers
	else
	{
		window.onload = func;
	}
}

// addPMJavaScriptEvent
// Enables us to hook into the browser JavaScript events.
function removePMJavaScriptEvent ( func, domEventName, ieEventName )
{
	//DOM method for binding an event
	if ( window.removeEventListener )
	{
		window.removeEventListener ( domEventName, func, true );
	}
	//IE exclusive method for binding an event
	else if ( window.detachEvent )
	{
		window.detachEvent ( ieEventName, func );
	}
	//Older browsers
	else
	{
		window.onload = null;
	}
}


// add keppress handler to load event
addLoadEvent ( KeyPressHandlingSetup );
