/**
 * 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.*;
import objectivehtml.oms.exception.*;
import java.util.*;

/**
 * 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
{
	private HashMap m_hmPostCodes = new HashMap();

	/**
	 * Constructor
	 */
	public CustomerForm()
			throws Exception
	{
		super();

		// lets connect all our signals and slots
		try
		{
			connect(slot("saveForm()"), m_btnSave.signal("clicked()"));
			connect(slot("updatePostCodeFields(String)"), m_txtPostCode.signal("valueChanged(String,String)"));
		} // end try to connect
		catch (InvalidSlotException e)
		{
			e.printStackTrace();	// shouldn't happen
		} // end catch

		// lets set the update order
		m_txtPostCode.setUpdateOrder(10);	// 10 provides us with a buffer for later on
		m_btnSave.setUpdateOrder(20);		// 20 provides us with a buffer for later on

		// create all the postcodes
		m_hmPostCodes.put("2000", new PostCode("2000", "SYDNEY", "NEW SOUTH WALES"));
		m_hmPostCodes.put("3000", new PostCode("3000", "MELBOURNE", "VICTORIA"));
		m_hmPostCodes.put("4000", new PostCode("4000", "BRISBANE", "QUEENSLAND"));
		m_hmPostCodes.put("5000", new PostCode("5000", "ADELAIDE", "SOUTH AUSTRALIA"));
		m_hmPostCodes.put("6000", new PostCode("6000", "PERTH", "WESTERN AUSTRALIA"));
		m_hmPostCodes.put("7000", new PostCode("7000", "HOBART", "TASMANIA"));
		m_hmPostCodes.put("0800", new PostCode("0800", "DARWIN", "NORTHERN TERRITORY"));
		m_hmPostCodes.put("2600", new PostCode("2600", "CANBERRA", "AUST. CAPITAL TERRITORY"));

	} // 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 updates the postcode fields with the default values if applicable.
	 */
	public void updatePostCodeFields(String sNewPostCode)
	{
		// lets try to set the default for the other fields
		PostCode objPostCode = (PostCode)m_hmPostCodes.get(sNewPostCode);
		if (objPostCode != null)
		{
			m_txtSuburb.setText(objPostCode.getSuburb());
			m_txtState.setText(objPostCode.getState());
		} // end if postcode exists
	} // end updatePostCodeFields

	/**
	 * 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