// 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 (typeof console != 'undefined') {
		console.log(formElement);
		console.log(Spry);
	}
	*/

	if (!Spry || Spry.Widget.Form.validate(formElement))
	{
		return true;
	}
	else
	{
		if (typeof(savingListerNum) != 'undefined') {
			return false;
		}
		
		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 (typeof(showError) == "function")
	{
		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;
}


if (typeof(RegisterTab) == "function") {
	/* SPRY INTEGRATION WITH TABS */
	function FindParentTab(node) {
		while (node.parentNode
			&& node.parentNode.className != "TabDisplay"
			&& node.parentNode.nodeName != 'BODY') {
				node = node.parentNode;
		}
	
		if (node.parentNode) {
			return node.parentNode;
		} else {
			return null;
		}
	}
	
	function GoToNextTab(node, validate){
		var thisTab = FindParentTab(node);
		var okay = true;
		if (validate != null && validate) {
			okay = ValidateTab(thisTab);
		}
		if (okay) {
			var next = $(thisTab).next('.TabDisplay').get(0);
			$(next.__button).click();
		}
		return okay;
	}
	
	function ValidateTab(tab) {
		var q = Spry.Widget.Form.onSubmitWidgetQueue;
		
		var parentTab = null;
		var isValid = true;
		
		for (var i = 0; i < q.length; i++) {
			if (!q[i].isDisabled() && typeof(q[i].element) != "undefined") {
				parentTab = FindParentTab(q[i].element);
				if (parentTab == tab) {
					//parentTab.__button.style.backgroundColor='';
					if (!q[i].validate()) {
						isValid = false;
					}
				}
			}
		}

		if (isValid) {
			$(tab.__button).removeClass("error");
		} else {
			$(tab.__button).addClass("error");
		}

		return isValid;
	}
	
	function AddToArray(array, value) {
		var found = false;
		for (var i=0; i<array.length; i++) {
			if (array[i] == value) {
				found = true;
				break;
			}
		}
		if (!found) { array.push(value); }
		
		return array;
	}
	
	function OnSaveDataSuccessSpry(msg, messenger) {
		/* Ugh, a hack with a global variable */
		if (typeof(savingListerNum) != 'undefined') {
			Spry.Widget.Form.destroyAll();
			Spry.Widget.Form.validate = function(vform) { return true; };
			
			$('#lister_' + savingListerNum).parents('form').find('input:submit[name=save],input:submit[name=Submit]').click();
		}
	}
	
	if (Spry.Widget.Form.validate) {
		Spry.Widget.Form.superValidate = Spry.Widget.Form.validate;
	} else {
		Spry.Widget.Form.superValidate = function(vform) {};
	}

	Spry.Widget.Form.validate = function(vform) {
		/* This is a hack to hook into the tabbed form functionality */
		var isValid = Spry.Widget.Form.superValidate(vform);
		var q = Spry.Widget.Form.onSubmitWidgetQueue;
		var qlen = q.length;
		
		if (!isValid) {
			var parentTab = null;
			var errTabs = new Array();
			for (var i = 0; i < qlen; i++) {
				if (!q[i].isDisabled() && q[i].form == vform) {
					if (typeof(q[i].element) != "undefined") {
						parentTab = FindParentTab(q[i].element);
						if (parentTab) {
							//parentTab.__button.style.backgroundColor='';
							if (!q[i].validate()) {
								errTabs = AddToArray(errTabs, parentTab);
							}
							else{
								$(parentTab.__button).removeClass("error");
							}
						}
					}
				}
			}
			
			for (var i = 0; i < errTabs.length; i++) {
				//errTabs[i].__button.style.backgroundColor = '#FF0000';
				$(errTabs[i].__button).addClass("error");
				
			}
			
			if(errTabs.length > 0) {
				$(errTabs[0].__button).click();
			}
		}
		/* *** */		
		
		/* This is a hack to hook into the lister save functionality */
		if (isValid && qlen > 0 && typeof(commitChanges) == "function") {
			/* This is global on purpose - not a good idea but it works
			We use this value in the callback to get back to the form to sbumit it */
			savingListerNum = $(vform).find('.lister').attr('id').substr(7);

			commitChanges(null, savingListerNum, true);

			/* return false so that the can trigger page submit in OnSaveDataSuccess - otherwise
			the page could submit before the lister saves its data and all will be lost */
			return false;
		}
		/* *** */
		
		return isValid;
	}
}