/****************************************************************************************************
Author: brhurley@jamestower.com
Date: 07/07/2005
Purpose: Collection of functions used by the button family of elements in the com.jamestower.magrathea.formelements
			package of java form builder classes.
Dependencies: Common.js
Revision History:
****************************************************************************************************/
/**
 * global button property vars, initialized to defaults & updated whenever a button is clicked
 */
var BTN_blnConfirm = false;
var BTN_strConfirmMsg = '';
var BTN_blnValidate = true;
var BTN_blnClear = false;
var BTN_blnPrompt = false;
var BTN_strPromptMsg = '';
var BTN_strPromptField = '';
var BTN_strPromptDefault = '';
var BTN_strTarget = '';
var BTN_strFormAction = '';
var BTN_strAction = '';
var BTN_strOriginalTarget = '';
var BTN_strOriginalFormAction = '';
var BTN_strOriginalAction = '';

/** 
 * sets the global button property vars from the passed in parameters
 */
function BTN_setGlobalProps(blnConfirm,strConfirmMsg,blnValidate,blnClear,blnPrompt,strPromptMsg,strPromptField,strPromptDefault,strTarget,strFormAction,strAction)
{
	BTN_blnConfirm = getBoolean(blnConfirm);
	BTN_strConfirmMsg = strConfirmMsg;
	BTN_blnValidate = getBoolean(blnValidate);
	BTN_blnClear = getBoolean(blnClear);
	BTN_blnPrompt = getBoolean(blnPrompt);
	BTN_strPromptMsg = strPromptMsg;
	BTN_strPromptField = strPromptField;
	BTN_strPromptDefault = strPromptDefault;
	BTN_strTarget = strTarget;
	BTN_strFormAction = strFormAction;
	BTN_strAction = strAction;
}

/**
 * standard button click event handler, evaluates the button properties and takes the appropriate actions
 * does not trigger the validation of the form, it is left up to the form submit handler to do that
 */
function BTN_standardClickHandler(objForm)
{
	var blnReturn = true;

	// if the originals haven't been set, set them from the form
	if(BTN_strOriginalTarget.length == 0)
	{
		BTN_strOriginalTarget = objForm.target;
	}
	if(BTN_strOriginalFormAction.length == 0)
	{
		BTN_strOriginalFormAction = objForm.action;
	}
	if(isDefined(objForm.strAction) && BTN_strOriginalAction.length == 0)
	{
		BTN_strOriginalAction = objForm.strAction.value;
	}
	
	// if a clear was requested, clear out all the user editable fields
	if(BTN_blnClear)
	{
		for(var i = 0; i < objForm.elements.length; i++)
		{
			if(objForm.elements[i].type == "text" || objForm.elements[i].type == "password" || objForm.elements[i].type == "textarea")
			{
				objForm.elements[i].value = '';
			}
			else if(objForm.elements[i].type == "select-one" || objForm.elements[i].type == "select-multiple")
			{
				objForm.elements[i].selectedIndex = -1;
			}
			else if(objForm.elements[i].type == "radio" || objForm.elements[i].type == "checkbox")
			{
				objForm.elements[i].checked = false;
			}
		}
	}

	// if a prompt was requested
	if(BTN_blnPrompt)
	{
		eval('objForm.' + BTN_strPromptField).value = prompt(BTN_strPromptMsg, BTN_strPrompDefault);
	}
	
	// if a confirm was requested
	if(BTN_blnConfirm)
	{
		blnReturn = confirm(BTN_strConfirmMsg);
	}
	
	// if nothing has failed, update the form stuff
	if(blnReturn)
	{
		// if an alternate form target was defined for the button
		if(BTN_strTarget.length > 0)
		{
			objForm.target = BTN_strTarget;
		}
		// else reset to the original form target
		else
		{
			objForm.target = BTN_strOriginalTarget;
		}
		// if an alternate form action was defined for the button
		if(BTN_strFormAction.length > 0)
		{
			objForm.action = BTN_strFormAction;
		}
		// else reset to the original form action
		else
		{
			objForm.action = BTN_strOriginalFormAction;
		}
		// if the form has a strAction field
		if(isDefined(objForm.strAction))
		{
			// if an action was defined for the button use that
			if(BTN_strAction.length > 0)
			{
				objForm.strAction.value = BTN_strAction;
			}
			// else reset to the original action
			else
			{
				objForm.strAction.value = BTN_strOriginalAction;
			}
		}		
	}
	return blnReturn;
}