/* ********************************************************************************
* Source Code Copyright (c) 2008 Congruent Media, LLC.  All rights reserved.
* 
* This source code is the intellectual property of Congruent Media, LLC., 
* except where otherwise noted, and may not be copied in whole or in part, 
* except for backup purposes. You may not resell or otherwise distribute 
* this source code, or any code derived from this source code, without 
* express written permission from Congruent Media, LLC.
* 
* This source code is sold "as is" and WITHOUT ANY WARRANTY; without even 
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*  
* http://www.CongruentMedia.com | info@CongruentMedia.com | +1 410-534-6800
*  
** ********************************************************************************/

// DATE: 12/28/2007
// MAKE SURE TO COMMENT THESE OUT AND SET THEM TO THE APPROPRIATE VALUE FOR YOUR SITE.
// ALTERNATIVELY, YOU CAN OVERLOAD THEM ON THE SpryFormSubmit FUNCTION
// OR BY REASSIGNING THEIR VALUES AT THE BOTTOM OF YOUR HTML PAGE
//var CM_SPRY_FORM_SUBMISSION_HANDLER_TYPE = '';
//var CM_SPRY_FORM_SUBMISSION_FAILURE_MESSAGE = 'FAILURE';

function SpryFormSubmit(formElement, type, failureMessage, containerId)
{
	if (typeof formElement == 'string')
		formElement = document.getElementById(formElement);

	if (!Spry || Spry.Widget.Form.validate(formElement))
	{
		return true;
	}
	else
	{
		var handler = SpryFormSubmit_Invalid;
		
		if (type == null)
		{
			if (CM_SPRY_FORM_SUBMISSION_HANDLER_TYPE)
			{
				type = CM_SPRY_FORM_SUBMISSION_HANDLER_TYPE;
			}
			else
			{
				type = 'POPUP';
			}
		}
		
		if (failureMessage == null)
		{
			if (CM_SPRY_FORM_SUBMISSION_FAILURE_MESSAGE)
			{
				failureMessage = CM_SPRY_FORM_SUBMISSION_FAILURE_MESSAGE;
			}
			else
			{
				failureMessage = 'FAILURE';
			}
		}
		
		switch (type.toUpperCase())
		{
			case "POPUP":
				handler = SpryFormSubmit_PopUp;
				break;
			case "INLINE":
				handler = SpryFormSubmit_Inline;
				break;
			case "SILENT":
				handler = SpryFormSubmit_Silent;
				break;
			case "DEBUG":
				handler = SpryFormSubmit_Debug;
				break;
			default:
				break;
		}

		return handler(formElement, failureMessage, containerId);
	}
}


function SpryFormSubmit_Invalid(formElement, failureMessage, containerId)
{
	alert('Invalid SpryFormSubmit handler type.  TYPE = [ POPUP, INLINE, SILENT, DEBUG ]');
	return false;
}

function SpryFormSubmit_PopUp(formElement, failureMessage, containerId)
{
	alert(failureMessage);
	return false;
}

function SpryFormSubmit_Inline(formElement, failureMessage, containerId)
{
	if (showError)
	{
		showError(failureMessage, containerId);
		return false;
	}
	else
	{
		return SpryFormSubmit_PopUp(formElement, failureMessage, containerId);
	}
}

function SpryFormSubmit_Silent(formElement, failureMessage, containerId)
{
	return false;
}

function SpryFormSubmit_Debug(formElement, failureMessage, containerId)
{
	alert('DEBUG MODE NOT IMPLEMENTED');
	return false;
}