* @copyright   2001-2011 The PHP Group
 * @license     http://www.php.net/license/3_01.txt PHP License 3.01
 * @version     CVS: $Id$
 * @link        http://pear.php.net/package/HTML_QuickForm
 */
/**
 * HTML class for a text field
 */
require_once 'HTML/QuickForm/text.php';
/**
 * HTML class for an autocomplete element
 *
 * Creates an HTML input text element that
 * at every keypressed javascript event checks in an array of options
 * if there's a match and autocompletes the text in case of match.
 *
 * For the JavaScript code thanks to Martin Honnen and Nicholas C. Zakas
 * See {@link http://www.faqts.com/knowledge_base/view.phtml/aid/13562} and
 * {@link http://www.sitepoint.com/article/1220}
 *
 * Example:
 * 
 * $autocomplete =& $form->addElement('autocomplete', 'fruit', 'Favourite fruit:');
 * $options = array("Apple", "Orange", "Pear", "Strawberry");
 * $autocomplete->setOptions($options);
 * 
 *
 * @category    HTML
 * @package     HTML_QuickForm
 * @author      Matteo Di Giovinazzo 
 * @version     Release: 3.2.16
 * @since       3.2
 */
class HTML_QuickForm_autocomplete extends HTML_QuickForm_text
{
    // {{{ properties
    /**
     * Options for the autocomplete input text element
     *
     * @var       array
     * @access    private
     */
    var $_options = array();
    /**
     * "One-time" javascript (containing functions), see bug #4611
     *
     * @var     string
     * @access  private
     */
    var $_js = '';
    // }}}
    // {{{ constructor
    /**
     * Class constructor
     *
     * @param     string    $elementName    (optional)Input field name attribute
     * @param     string    $elementLabel   (optional)Input field label in form
     * @param     array     $options        (optional)Autocomplete options
     * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
     *                                      or an associative array. Date format is passed along the attributes.
     * @access    public
     * @return    void
     */
    function HTML_QuickForm_autocomplete($elementName = null, $elementLabel = null, $options = null, $attributes = null)
    {
        $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
        $this->_persistantFreeze = true;
        $this->_type = 'autocomplete';
        if (isset($options)) {
            $this->setOptions($options);
        }
    } //end constructor
    // }}}
    // {{{ setOptions()
    /**
     * Sets the options for the autocomplete input text element
     *
     * @param     array    $options    Array of options for the autocomplete input text element
     * @access    public
     * @return    void
     */
    function setOptions($options)
    {
        $this->_options = array_values($options);
    } // end func setOptions
    // }}}
    // {{{ toHtml()
    /**
     * Returns Html for the autocomplete input text element
     *
     * @access      public
     * @return      string
     */
    function toHtml()
    {
        // prevent problems with grouped elements
        $arrayName = str_replace(array('[', ']'), array('__', ''), $this->getName()) . '_values';
        $this->updateAttributes(array(
            'onkeypress' => 'return window.autocomplete(this, event, ' . $arrayName . ');'
        ));
        if ($this->_flagFrozen) {
            $js = '';
        } else {
            $js = "";
        }
        return $js . parent::toHtml();
    }// end func toHtml
    // }}}
} // end class HTML_QuickForm_autocomplete
?>