/*\
|*|  FormButton
|*|
|*|  Copyright (c) 2001 Dolphin Inc.  All rights reserved.
|*|  Dave Sugar 4/20/2001
|*|
|*|  Use:
|*|    This use of this FormButton object is to handle graphical PushButtons
|*|    when they need to submit a form that they exist in.  Just like the
|*|    PushButton object you specify the document an the basepath of the
|*|    image.  Here you also specify the name of the variable to be set when
|*|    the button is pressed.
|*|    You can pass any optional parameters that you would pass to the
|*|    PushButton object.  They will be called as appropriate, the name paramter
|*|    will be overridden by the name variable passed in on contruction.  In the
|*|    onClick function, your code will be called, but you should NOT submit the
|*|    form yourself, it is done automatically.
|*|
|*|  Prereqresite:
|*|    The form that you want to submit when this button is pressed must be
|*|    open when the button is created.
|*|
|*|  Base Class:
|*|    PushButton
|*|
|*|  Creation:
|*|    var x = new FormButton (document, variable name, image name);
|*|
|*|  Internals:
|*|    When this item is created it outputs HTML, the PushButton outputs stuff
|*|    for the images and the href, this item outputs a hidden field.  The value
|*|    of the hidden field is initially 0 and is set to 1 when the button is
|*|    pressed.  This is needed to propogate the value through the form.
|*|
|*|  Example 1:
|*|    var x = new FormButton (document, 'u_bEmail', 'images/blah.gif');
|*|
|*|  Note 1:
|*|    In a function called from 'onClick' if you return false the form will not get
|*|    submitted if the function returns true the form will be submitted.
|*|
|*|  Note 2:
|*|     Now the 'variable name' field does not need to be unique per-page.  If
|*|     the same name is used more than once only a single input field will be
|*|     created with that name and all form buttons that have the same name will
|*|     set the value of the single hidden field.
|*|
|*|  Optional Arguments (Also see list in push_button.js):
|*|    ¥ value 				- value to set the hidden field to when submitted.
|*|							  If no value specified '1' will be used.
|*|
\*/


function FormButton (i_Doc, i_szItemName, i_szBasePath)
{
	FormButton.prototype.buildHiddenField = function ()
	{
		var szName = this.getParam ("name");
		this.m_szHiddenFieldName = szName;
		szName = (szName == "" ? "" : "name='" + szName + "' ");

		// create the hidden field
		if (szName != "")
		{
			var nElements = this.m_document.forms[this.m_nFormIndex].elements.length;
			if (!eval ("document.forms[" + this.m_nFormIndex + "]." + this.m_szHiddenFieldName))
			{
				this.m_nHiddenFieldIndex = nElements;
				this.m_document.writeln ("<input type='hidden' " + szName + " value='0'>");
			}
			else
			{
				for (var i = 0; i < nElements; i++)
				{
					if (this.m_document.forms[this.m_nFormIndex].elements[i].name == this.m_szHiddenFieldName)
					{
						this.m_nHiddenFieldIndex = i;
						break;
					}
				}
			}
		}
	}

	// called when the button is clicked
	FormButton.prototype.click = function ()
	{
		if (this.m_bEnable)
		{
			this.m_bPress = true;
			this.updateImage ();
			var bSubmit = this.execParam ("onclick");

			this.m_bPress = false;
			this.updateImage ();

				// only submit once!
			if (bSubmit == true && this.m_bSubmitted == false)
			{
				this.m_bSubmitted = true;
				var szValue = this.getParam ("value");
				if (szValue == "")
					szValue = "1";

				// set the value of the hidden field to 1
				this.m_document.forms[this.m_nFormIndex].elements[this.m_nHiddenFieldIndex].value = szValue;
	
				// submit the form
				this.m_document.forms[this.m_nFormIndex].submit ();			
			}
		}
		
		return (false);		// needs to return false so it doesn't link to the anchor
	};

	// current state
	this.m_bEnable = true;
	this.m_bPress = false;
	this.m_bInside = false;
	this.m_bSubmitted = false;

	this.m_aImages = new Array (4);
	this.m_aParams = new Object ();

	this.m_document = i_Doc;
	this.m_szBasePath = i_szBasePath;

	this.m_nHiddenFieldIndex = 0;
	this.m_szHiddenFieldName = "";
	this.m_nFormIndex = this.m_document.forms.length -1;
	
	this.parseParams (arguments, 3);
	this.setParam ("name", i_szItemName);
	
	this.buildHiddenField ();
	this.buildAnchor ();
};

FormButton.prototype = new PushButton ("", "");
