// 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 toggleRequiredFields(nrq, theForm, type, errMsg, containerId) {
	var q = Spry.Widget.Form.onSubmitWidgetQueue;
	var r = new Array();
		
	for (var i=0; i < q.length; i++) {
		if (q[i].isRequired) {
			q[i].isRequired = false;
			r.push(q[i]);
		}
	}

	for (var i=0; i < nrq.length; i++) {		
		nrq[i].isRequired = true;
	}
	
	if (SpryFormSubmit(theForm, type, errMsg, containerId)) {
		return true;
	}
	
	for (var i=0; i < r.length; i++) {
		r[i].isRequired = true;
	}
	
	for (var i=0; i < nrq.length; i++) {		
		nrq[i].isRequired = false;
	}
				
	return false;
}

function SpryFormSubmit(theForm, type, errMsg, containerId) {
	if (typeof theForm == 'string') {
		theForm = document.getElementById(theForm);
	}

	if (!Spry) {
		return true;
	} else {
		Spry.Widget.Form.isBeingSubmitted = true;

		var isValid = Spry.Widget.Form.validate(theForm);
		
		if (!isValid) {
			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 (errMsg == null) {
				if (CM_SPRY_FORM_SUBMISSION_FAILURE_MESSAGE) {
					errMsg = CM_SPRY_FORM_SUBMISSION_FAILURE_MESSAGE;
				} else {
					errMsg = '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;
			}
	
			isValid = handler(theForm, errMsg, containerId);
		}
		
		Spry.Widget.Form.isBeingSubmitted = false;

		return isValid;
	}
}

function SpryFormSubmit_Invalid(theForm, errMsg, containerId) {
	alert('Invalid SpryFormSubmit handler type.  TYPE = [ POPUP, INLINE, SILENT, DEBUG ]');
	return false;
}

function SpryFormSubmit_PopUp(theForm, errMsg, containerId) {
	alert(errMsg);
	return false;
}

function SpryFormSubmit_Inline(theForm, errMsg, containerId) {
	if (typeof(showError) == "function") 	{
		showError(errMsg, containerId);
		return false;
	} else {
		return SpryFormSubmit_PopUp(theForm, errMsg, containerId);
	}
}

function SpryFormSubmit_Silent(theForm, errMsg, containerId) {
	return false;
}

function SpryFormSubmit_Debug(theForm, errMsg, 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;
	}
}

if (Spry) {
	Spry.Widget.Form.isBeingSubmitted = false;
}
