/**
 * Copyright (c) 2001, Keith Wong
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package mypackage;

import objectivehtml.htmlwidget.*;
import objectivehtml.htmlwidget.exception.*;

/**
 * CustomerForm
 * This is an example of how to use the objective html toolkit. Please read all the comments
 * in the code as they give handy tips in how to be use this toolkit.
 * The example was written by Keith Wong, email: keithwong@optushome.com.au
 */
public class CustomerForm extends CustomerFormDesign
{
	/**
	 * Constructor
	 */
	public CustomerForm()
			throws Exception
	{
		super();
	} // end constructor

	/**
	 * This method validates the form details first.
	 * @return 	true if the form data is ok, false otherwise
	 */
	private boolean validateForm()
	{
		boolean bValid = true;
		// if title is blank
		if (m_selTitle.isSelected(0) == true)
		{
			bValid = false;
			// sets the class attribute of the table cell with "Title *" text
			m_tblLayout.getTableCell(0, 0).setClassAttribute("error");
		} // end if title is blank

		// if first name is blank
		if (m_txtFirstName.getText().trim().length() == 0)
		{
			bValid = false;
			m_tblLayout.getTableCell(1, 0).setClassAttribute("error");
		} // end if first name is blank

		// if last name is blank
		if (m_txtLastName.getText().trim().length() == 0)
		{
			bValid = false;
			m_tblLayout.getTableCell(2, 0).setClassAttribute("error");
		} // end if last name is blank

		// if email is blank
		if (m_txtEmail.getText().trim().length() == 0)
		{
			bValid = false;
			m_tblLayout.getTableCell(11, 0).setClassAttribute("error");
		} // end if email is blank

		// if invalid then lets set a user message
		if (bValid == false)
		{
			m_htmUserMessage.setText("Please fill in the mandatory fields highlighted below.");
			m_parUserMessage.setVisible(true);
			m_parUserMessage.setClassAttribute("error");
		} // end if not valid

		return bValid;
	} // end validateForm

	/**
	 * This method should be called when the save action is triggered.
	 */
	public void saveForm()
		throws InvalidAttributeException
	{

		// lets clear out all class styles set for errors
		// you can do this by calling the overloaded version of setAttribute()
		// provided by all container widgets
		// n.b giving the setAttribute a null value will clear out the attribute,
		// so that the attribute is not shown at all when printHtml is called
		m_tblLayout.setAttribute("class", null, true);	// apply to all children

		// lets set the attribute back to the normal
		m_tblLayout.setAttribute("class", "normal");	// only applies to the table itself

		if (validateForm())
		{
			// ...
			// do some database stuff... save to database..
			// this part left out... well because this is only an example after all :)

			m_htmUserMessage.setText("Your customer details have been saved.");
			m_parUserMessage.setVisible(true);
			m_parUserMessage.setClassAttribute("info");
		} // end if form is valid
	} // end saveForm

} // end class