// JavaScript Document
var errBox;

var queryParam = window.location.search.toQueryParams();

Behaviour.register
(
	{
		'#box_error' : function(element)
		{
			errBox = new jsMsgBox(element, 'Errors');
			errBox.onhide = function()
			{
				$('entry_form').enable();
				$('entry_form').style.backgroundColor = '';
			};
			
			var error = '';
			switch (queryParam.error)
			{
				case 'duplicate': error = 'The Secret Code you have entered has already been used.'; break;
				default: error = ''; break;
			}
			
			if (! error.empty()) errBox.show(error);
		},
		'#entry_submit' : function(element)
		{
			$(element).observe('click', function(event)
			{
				var Error = '';
				
				if ($F('entry_firstname').strip().length < 2)
				{
					Error += '  - First Name appears to be too short\n';
				}
				
				if ($F('entry_lastname').strip().length < 2)
				{
					Error += '  - Last Name appears to be too short\n';
				}

				if (! valEmail($F('entry_email').strip()) || $F('entry_email').strip().empty())
				{
					Error += '  - Email appears to be invalid\n';
				}
				
				if ($F('entry_email').strip() != $F('confirm_email').strip())
				{
					Error += '  - Email does not match Confirm Email\n';
				}
				
				if ($F('entry_zip').strip().length != 5 && $F('entry_zip').strip().length != 10)
				{
					Error += '  - Zip Code must be either a 5 or 10 dgiti zip code\n';
				} else if (! (/[0-9]{5}(-[0-9]{4})?/).test($F('entry_zip').strip()))
				{
					Error += '  - Zip Code must be a valid 5 or 10 dgiti zip code\n';
				}

				
				if ($F('visit_yes') && ! (/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/).test($F('entry_visit').strip()))
				{
					Error += '  - Estimated date of arrival appears invalid (expecting mm/dd/yyyy)\n';
				}

				if (Error == '')
				{
					$('action').setValue('entry');
					$('entry_form').submit();
					return true;
				}
				
				$('entry_form').disable();
				$('entry_form').style.backgroundColor = '#EEEEEE';
				errBox.show('The following errors were encounted when trying to submit your entry:\n\n' + Error.strip() + '\n\n');
			});
		}
	}
);

