// this is a simple way to set required fields for a form...
// all it does is check to see if every form element defined in the
// array "validator_required_fields" below has a value
// it checks in the order specified in the array, so you can
// set them in the order you want people to fill out your forms




// === REQIRED INCLUDES ===
// jjValidator.js
// jjFormElement.js
//

//********************
// === FUNCTIONS ===
//********************


//~~~~~~~~~~~~~~~~~~~~
// NAME: set_constants
// DESC: local config settings, call inside a function to pull in these values
//
// INPUTS: NONE
// RETURNS: NOTHING
//~~~~~~~~~~~~~~~~~~~~
function set_constants()
{   	
	//the list of required fields to load into the validator (replace with list of your values)
	//args are id of element, name of element you would tell user if error, and type of
	//field for validation (see jjValidator.js, you can leave this arg off for no validation)
	g_validator_required_fields =
	[
		new jjFormElement( 'Name', "Name", 'oneline'),
		new jjFormElement( 'Email', "E-mail Address", 'email'),
		new jjFormElement( 'Comments', "Questions/Comments", null)
		
	];

	//id of the form
	g_form_id = 'contactform';
	
} //end set_constants()



//~~~~~~~~~~~~~~~~~~~~
// NAME: allowFormSubmitTF
// DESC: true if ok to submit, false if we need to short-circuit (failed validation)
//
// INPUTS: NONE
// RETURNS: boolean
//~~~~~~~~~~~~~~~~~~~~
function allowFormSubmitTF()
{
	//get our constants
	set_constants();
	
	//assume success until otherwise shown	
	var validator = new jjValidator( g_validator_required_fields );
	
	//check to see if any elements are empty
	var showError = true;
	var setFocus = true;
	
	var haveReqFields = validator.requiredFieldsAreFilled(showError, setFocus);
	
	return haveReqFields;
	
} //end allowFormSubmitTF



//********************
// === MAIN ===
//********************


//register event handlers for when the page loads
Event.observe(
	window,
	'load',
	function()
	{
		set_constants();
		
		//attach a form submission watcher if the contact form is on this page
		if ( $( g_form_id ) != null)
		{
			Event.observe(
				g_form_id,
				'submit',
				function (inEvent)
				{
					if (! allowFormSubmitTF())
					{
						//stop this event from propagating
						Event.stop(inEvent);
					}
				}
			);
		}
	} //end function registering individual element observers

); //end window load event handler
