<?php
require_once(searchinc('base.inc'));
require_once(searchinc('htmlgen.inc'));
	
	// flags for emit_str method
	define('EDITITEM_EMIT_FLAG_PREFIX_WITH_VALUE', 65536);
	define('EDITITEM_EMIT_FLAG_READONLY', 131072);
        
        if (!isset($GLOBALS['NEOFS_DEFAULT_CHARSET'])) { $GLOBALS['NEOFS_DEFAULT_CHARSET'] = 'ISO-8859-1'; }
        if (!isset($GLOBALS['NEOFS_DEFAULT_DB_CHARSET'])) { $GLOBALS['NEOFS_DEFAULT_DB_CHARSET'] = 'latin1'; }
	
	// this is a base class for all classes representing HTML form inputs.
	class edititem extends htmlcontainer
	{
		var $_type;					// value of HTML type attribute.   Must be set by
									// subclass
		var $_value_embed = true;	// value attribute is imbeded in the control tag
		var $_size;					// value of HTML size attributes.  Only use for types
									// having a size attribute.  Set by subclass.
		var $_default;				// default value for control.  Set by subclass.
		var $_disabled = false;		// whether disabled attribute is set for control.
		var  $_readonly =false;		// whether readonly attribute is set forcontrol.
		var $_value;				// array of one or more values for this item or item
									// list.  Set via constructor $value parameter or
									// add_value (remove_value) method.
									
		var $_invalid_style = array();	// style attributes for control to signal invalid contents
		var $_invalid_css_class;		// name of css class for control to signal invalid contents
		var $_is_invalid;				// true if control has invalid contents.  This should be set by
										// class constructor after contents have been initialized from
										// post variables
		var	$_datasrc = 'POST';		// use source for control data, valid ar POST, GET, COOKIE or any concatenation of these 
		var $_invalid_msg = 'Invalid Value';	// user understandable message describing validity of contents
		
		// function returning whether this control has invalid contents.
		function is_valid(&$msg)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'is '.($this->_is_invalid?('invalid: '.$this->_invalid_msg):'valid'), DEBUG_DETAIL_TRACE);
            if ($this->_is_invalid) { $msg = $this->_invalid_msg; }
			return (boolean)!$this->_is_invalid;
		}
									
		var $_val_from_request = false;	// whether value was initialized from a request
		
		//	appropriate for most input controls, override where necessary
		var $_tag = 'input';
		//	not required for most input controls, override where necessary
		var $_req_close_tag	= false;
		
		// initialize an object representing an HTML input item or list of such.
		// parameters:
		//		$attrs	- initializes HTML attributes of input control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			static $eventnames = array('onfocus', 'onblur',
			                           'onselect', 'onchange');
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
                if (isset($attrs['size'])) { $this->size($attrs['size']); }
                if (isset($attrs['default'])) { $this->defaultt($attrs['default']); }
                if (isset($attrs['value'])) { $this->value($attrs['value']); }
                if (isset($attrs['disabled'])) { $this->disabled($attrs['disabled']); }
                if (isset($attrs['readonly'])) { $this->readonly($attrs['readonly']); }
                if (isset($attrs['invalid_classname'])) { $this->_invalid_css_class = $attrs['invalid_classname']; }
				if ((isset($attrs['invalid_style'])) && (is_array($attrs['invalid_style'])))
				{
					foreach($attrs['invalid_style'] as $property => $value)
					{
						if (isset($value)) {
							$this->_invalid_style[$property] = $value;
						} else {
							unset($this->_invalid_style[$property]);}
					}
				}
                if (isset($attrs['datasrc'])) { $this->_datasrc = $attrs['datasrc']; }
				
				$this->dbgecho($proc.'$this->_datasrc = '.look_r($this->_datasrc), DEBUG_DETAIL_TRACE);
				
				$this->dbgecho($proc.'strpos(POST) = '.look_r(strpos($this->_datasrc, 'POST')), DEBUG_DETAIL_TRACE);
				$this->dbgecho($proc.'strpos(GET) = '.look_r(strpos($this->_datasrc, 'GET')), DEBUG_DETAIL_TRACE);
				$this->dbgecho($proc.'strpos(COOKIE) = '.look_r(strpos($this->_datasrc, 'COOKIE')), DEBUG_DETAIL_TRACE);
				
				if ((false !== strpos($this->_datasrc, 'POST')) && isset($_POST[$this->name()]))
				{
					$this->dbgecho($proc.'Post var $_POST['.$this->name().'] = '.look_r($_POST[$this->name()]), DEBUG_DETAIL_TRACE);
					$this->_value = (smart_strip_gpc_quotes($_POST[$this->name()]));
					$this->_val_from_request = true;
				}
				elseif ((false !== strpos($this->_datasrc, 'GET')) && isset($_GET[$this->name()]))
				{
					$this->dbgecho($proc.'Post var $_GET['.$this->name().'] = '.look_r($_GET[$this->name()]), DEBUG_DETAIL_TRACE);
					$this->_value = (smart_strip_gpc_quotes($_GET[$this->name()]));
					$this->_val_from_request = true;
				}
				elseif ((false !== strpos($this->_datasrc, 'COOKIE')) && isset($_COOKIE[$this->name()]))
				{
					$this->dbgecho($proc.'Post var $_COOKIE['.$this->name().'] = '.look_r($_COOKIE[$this->name()]), DEBUG_DETAIL_TRACE);
					$this->_value = (smart_strip_gpc_quotes($_COOKIE[$this->name()]));
					$this->_val_from_request = true;
				}
				else
				{
					$this->dbgecho($proc.'No Request var $_REQUEST['.$this->name().']', DEBUG_DETAIL_TRACE);
					if (!isset($this->_value))
					{
						$this->value($this->defaultt());
					}
				}
				if (is_array($attrs))
				{
					foreach($attrs as $attr => $val)
					{
						if (in_array(strtolower($attr), $eventnames))
						{
							$this->set_event_handler(strtolower($attr), $val);
						}
					}
				}
				$this->dbgecho('Initialized edititem = '.look_r($this), DEBUG_MINUTIA_TRACE);
			}
			else
			{
				$this->set_error('Unable to initialize base class of '.$this->_type.' item named '.$attrs['name'].'.', __FILE__, __LINE__);
			}
		
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function has_val_from_request()
		{
			return $this->_val_from_request;
		}
		
		// generates an HTML text string instantiating the input control.
		// Subclasses must override this to provide customized control contents, either
		// by passing custom contents in the $embedded parameter, or by post-processing
		// the output of this emit_str instance.  For array type controls, either a
		// single instance of the control can be generated by passing a valid $index
		// parameter, or ALL intances can be generated by passing a null instead. Single
		// type controls igore the $index parameter.
		//
		// parameters:
		//		$attrs - content to be placed in tag as attributes.
		//		$flags - flags controlling element expression
		//
		// returns:
		//		string	- HTML text
		//		false	- error
		//
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().','.$flags.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$str='';
			$val = $this->value();
			$attribs = (isset($this->_type)?' type="'.$this->_type.'"':'').
					   (($this->_value_embed && isset($val))?(' value="'.$val.'"'):'').
					   ($this->_disabled?' disabled="true"':'').
					   ($this->_readonly?' readonly="true"':'').
					   (isset($this->_size)?' size="'.$this->_size.'" ':'').
					   ' '.$attrs;
			if (($flags & EDITITEM_EMIT_FLAG_READONLY)== EDITITEM_EMIT_FLAG_READONLY)
			{
				$tmp = new htmlspan($val, $attribs);
				$str .= $tmp->emit_str(null, $flags);
			}
			else
			{
				// set css class and style if invalid
				if ($this->_is_invalid)
				{
					// handle css class
					if (isset($this->_invalid_css_class))
					{
						$tmpcss = $this->_css_classname;
						$this->_css_classname = $this->_invalid_css_class;
					}
					// handle style
					$tmpstyle = array();
					foreach($this->_invalid_style as $property => $value)
					{
						$tmpstyle[$property] = $this->style($property);
						$this->style($property, $value);
					}
				}
				$str .= ((($flags & EDITITEM_EMIT_FLAG_PREFIX_WITH_VALUE) == EDITITEM_EMIT_FLAG_PREFIX_WITH_VALUE)?$val:'').
							parent::emit_str($attribs, $flags)."\n";
				// restore css class and style if invalid
				if ($this->_is_invalid)
				{
					// restore css class
                    if (isset($tmpcss)) { $this->_css_classname = $tmpcss; }
					// restore style
					foreach($tmpstyle as $property => $value)
					{
						if (isset($value)) {
							$this->style($property, $value);
						} else {
							$this->clear_style($property); }
					}
				}
			}
            if ($this->is_ok()) { $this->dbgecho('HTML for '.$this->_name.' is: '.$str, DEBUG_DETAIL_TRACE); }
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}

		// sets and/or returns the current value of the input control object.
		//
		// parameters:
		//		$new   - if set, the new value to be set.  This shou;d always be a simple
		//				 scalar value.
		//
		// returns:
		//		string	- scalar value
		//		null	- no value set
		//		false	- error
		//
		function value($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->dbgecho('Setting value for item '.$this->name().'='.
									look_r($new), DEBUG_DETAIL_TRACE);
				$this->_value = $new;
			}
			$this->dbgecho('Value for item '.$this->name().' = '.look_r($this->_value), DEBUG_DETAIL_TRACE);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?$this->_value:'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->_value, RETURN_NULL_OK);
		}
		
		// clears the value of the input control object.  For array type controls, the value
		// of a single element of the control can be cleared by passing a valid $index parameter.
		// Single type controls ignore the $index parameter.  If attempting to clear multiple 
		// elements from an array type control, use method array_clear()
		//
		// parameters:
		//		$index - index of array element to be cleared.  This parameter is
		//				 ignored by single type objects.
		//
		// returns:
		//		true	- success
		//		false	- error
		//
		function clear()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->dbgecho('Clearing value of item '.$this->name(), DEBUG_DETAIL_TRACE);
			
			$this->_value = null;
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		function size($new=null)
		{
			if (isset($new))
			{
				$this->_size = $new;
			}
			return $this->_size;
		}
		
		function defaultt($new=null)
		{
			if (isset($new))
			{
				$this->_default = $new;
			}
			return $this->_default;
		}
		
		function disabled($new=null)
		{
			if (isset($new))
			{
				$this->_disabled = $new;
			}
			return $this->_disabled;
		}
		
		function readonly($new=null)
		{
			if (isset($new))
			{
				$this->_readonly = $new;
			}
			return $this->_readonly;
		}
	}

	//
	// CLASS: edithidden
	//
	// class to instantiate hidden input controls.  very easy to implement as there 
	// is no other functionality other than what is implemented in the base class
	//
	class edithidden extends edititem
	{
		var $_type = 'hidden';
		
		// initialize an object representing an HTML hidden control or list of such.
		// parameters:
		//		$name	- initializes HTML name attribute of hidden control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct(null, $attrs);
			if (!$this->is_ok())
			{
				$this->set_error('Unable to initialize parent edititem class of '.$this->_type.' item named '.$attrs['name'].'.', __FILE__, __LINE__);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().','.$flags.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			// turn off read-only flag as hidden is always read-only
			$flags = ($flags & ~EDITITEM_EMIT_FLAG_READONLY);
			$str = parent::emit_str($attrs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
	}
	
	//
	// CLASS: edittext
	//
	// class to instantiate text input controls.
	//
	class edittext extends edititem
	{
		var $_type = 'text';
		var $_maxlength;
		var $_readonly;
		
		// initialize an object representing an HTML text control or list of such.
		// parameters:
		//		$attrs	- initializes HTML attribute of text control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct(null, $attrs);
			if ($this->is_ok())
			{
                if (isset($attrs['maxlength'])) { $this->maxlength($attrs['maxlength']); }
                if (isset($attrs['readonly'])) { $this->readonly($attrs['readonly']); }
			}
			else
			{
				$this->set_error('Unable to initialize parent edititem class of '.$this->_type.' item named '.$attrs['name'].'.', __FILE__, __LINE__);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}

		// overloads edititem to generate HTML text string instantiating the text
		// input control.
		//
		// parameters:
		//		$attrs - content to be placed in tag as attributes.
		//
		// returns:
		//		string	- HTML text
		//		false	- error
		//
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attribs = (isset($this->_maxlength)?'maxlength="'.$this->_maxlength.'" ':'').
					   (isset($this->_readonly)?($this->_readonly?' readonly="true"':''):'').
					   $attrs;
			
			// convert any & in value to &amp; form
			// convert any special html characters to an encoded form - protects against code injection
			$tmpval = $this->value();
//			if ($tmpval !== null)  $this->value(str_replace('&', '&amp;', $tmpval));
            if ($tmpval !== null) { $this->value(htmlspecialchars($tmpval, (ENT_QUOTES | ENT_HTML5), $GLOBALS['NEOFS_DEFAULT_CHARSET'])); }
			
			$str = parent::emit_str($attribs, $flags);
			
			// restore original value
        if ($tmpval !== null) { $this->value($tmpval); }
			
            if ($this->is_ok()) { $this->dbgecho($proc.'HTML: '.$str, DEBUG_DETAIL_TRACE); }
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function maxlength($new=null)
		{
			if (isset($new))
			{
				$this->_maxlength = $new;
			}
			return $this->_maxlength;
		}
		
		function readonly($new=null)
		{
			if (isset($new))
			{
				$this->_readonly = (boolean)$new;
			}
			return $this->_readonly;
		}
	}
	
	class editpassword extends edittext
	{
		var $_type = 'password';
	}
	
	//
	// CLASS: edittextarea
	//
	// class to instantiate textarea input controls.
	//
	class edittextarea extends edititem
	{
		var $_tag = 'textarea';
		var $_type = null;
		var $_req_close_tag	= true;
		
		var $_cols;
		var $_rows;
		var $_wrap;
		var	$_readonly;
		
		var $_value_embed = false;
		
		// initialize an object representing an HTML textarea control or list of such.
		// parameters:
		//		$attrs - content to be placed in tag as attributes.
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.
                                (isset($attrs['id'])?$attrs['id'].', ':'').
                                (isset($attrs['cols'])?$attrs['cols'].', ':'').
                                (isset($attrs['rows'])?$attrs['rows'].', ':'').
                                (isset($attrs['wrap'])?$attrs['wrap'].', ':'').
                                (isset($attrs['readonly'])?($attrs['readonly']?'readonly':'readwrite'):'').
                                ') entering', DEBUG_PROC_TRACE);
			
			// value for this control is placed in control contents
                        $contents='';
                        if (isset($attrs['value']))
                        {
                            $contents = $attrs['value'];
                            unset($attrs['value']);
                        }
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
            if (isset($attrs['cols'])) { $this->cols($attrs['cols']); }
            if (isset($attrs['rows'])) { $this->rows($attrs['rows']); }
            if (isset($attrs['wrap'])) { $this->wrap($attrs['wrap']); }
                if (isset($attrs['readonly'])) { $this->readonly($attrs['readonly']); }
				unset($this->_type);
				if ($this->has_val_from_request())
				{
					$this->value(parent::value());
					parent::clear();
				}
			}
			else
			{
				$this->set_error('Unable to initialize parent edititem class of '.$this->_type.' item named '.$attrs['name'], __FILE__, __LINE__);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		// generates an HTML text string instantiating the textarea control.
		// This function overrides the edititem implementaton of this method, but calls
		// it to make use of it's base functionality.
		//
		// Unlike most other HTML controls, textarea do not contain their value in a value
		// attribute contained within the tag.  In textareas, the value is set between the
		// beginning and end tags.  Also, textareas are their own tag type, and not a 
		// subtype of the <input> tag.  This requires some special handling when creating 
		// the HTML text in this method.
		//
		// First, the _value_embed member is set to false in the class definition, to keep
		// it from being included as a tag attribute.
		// Next, the _type_is_tag member is set to true in the class definition, to generate
		// the correct type of tag.  Finally the value and closing tag are appended to create 
		// the complete textarea HTML.
		//
		// Because the HTML text of each seperate control instance of an array-type control 
		// needs to be processed like this, the processing is done, one instance at a time, 
		// within a foreach loop.
		//
		// parameters:
		//		$attrs - content to be placed in tag as attributes.
		//		$flags - 
		//
		// returns:
		//		string	- HTML text
		//		false	- error
		//
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$str = '';
			$readonly = $this->_readonly || (($flags & EDITITEM_EMIT_FLAG_READONLY)==EDITITEM_EMIT_FLAG_READONLY);
			$tmpflags = ($readonly)?($flags & ~EDITITEM_EMIT_FLAG_READONLY):$flags;
			
			$attribs = (isset($this->_cols)?' cols="'.$this->_cols.'"':'').
					   (isset($this->_rows)?' rows="'.$this->_rows.'"':'').
					   (isset($this->_wrap)?' wrap="'.$this->_wrap.'"':'').
					   (isset($this->_readonly)?($this->_readonly?' readonly="true"':''):'').
					   $attrs;
					   
			// convert any & in value to &amp; form
			// convert any special html characters to an encoded form - protects against code injection
			$tmpval = $this->value();
//			if ($tmpval !== null) $this->value(str_replace('&', '&amp;', $tmpval));
            if ($tmpval !== null) { $this->value(htmlspecialchars($tmpval, (ENT_QUOTES | ENT_HTML5), $GLOBALS['NEOFS_DEFAULT_CHARSET'])); }
			
			$str .= parent::emit_str($attribs, $tmpflags);
			
			// restore original value
            if ($tmpval !== null) {$this->value($tmpval); }
			
        if ($this->is_ok()) { $this->dbgecho($proc.'HTML: '.$str, DEBUG_DETAIL_TRACE); }
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		// sets and/or returns the current value of the input control object.
		//
		// parameters:
		//		$new   - if set, the new value to be set.  This shou;d always be a simple
		//				 scalar value.
		//
		// returns:
		//		string	- scalar value
		//		null	- no value set
		//		false	- error
		//
		function value($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->dbgecho('Setting value for item '.$this->name().'='.
									look_r($new), DEBUG_DETAIL_TRACE);
				$this->set_contentsx($new);
			}
			if ($this->is_ok() && ($this->num_contents()>0))
			{
				$cont = $this->get_contents(0);
			}
			else
			{
				$cont = null;
			}
			$this->dbgecho('Value for item '.$this->name().' = '.look_r($cont), DEBUG_DETAIL_TRACE);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?look_r($cont):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($cont, RETURN_NULL_OK);
		}
		
		// clears the value of the input control object.  For array type controls, the value
		// of a single element of the control can be cleared by passing a valid $index parameter.
		// Single type controls ignore the $index parameter.  If attempting to clear multiple 
		// elements from an array type control, use method array_clear()
		//
		// parameters:
		//		$index - index of array element to be cleared.  This parameter is
		//				 ignored by single type objects.
		//
		// returns:
		//		true	- success
		//		false	- error
		//
		function clear()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'() entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->dbgecho('Clearing value of item '.$this->name(), DEBUG_DETAIL_TRACE);
			
			$this->clear_contents();
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		function cols($new=null)
		{
			if (isset($new))
			{
				$this->_cols = $new;
			}
			return $this->_cols;
		}
		
		function rows($new=null)
		{
			if (isset($new))
			{
				$this->_rows = $new;
			}
			return $this->_rows;
		}
		
		function wrap($new=null)
		{
			if (isset($new))
			{
				$this->_wrap = $new;
			}
			return $this->_wrap;
		}
		
		function readonly($new=null)
		{
			if (isset($new))
			{
				$this->_readonly = (boolean)$new;
			}
			return $this->_readonly;
		}
	}
	
	//
	// CLASS: htmloption
	//
	// class to instantiate select option element
	//
	class htmloption extends htmlcontainer
	{
		var $_tag = 'option';
		var $_req_close_tag = true;
		
		var $_value;
		var $_selected;
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
                if (isset($attrs['value'])) { $this->value($attrs['value']); }
                if (isset($attrs['selected'])) { $this->selected($attrs['selected']); }
				$this->dbgecho($proc.'_value='.look_r($this->_value).', _selected='.look_r($this->_selected), DEBUG_DETAIL_TRACE);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->dbgecho($proc.'_value='.look_r($this->_value).', _selected='.look_r($this->_selected), DEBUG_DETAIL_TRACE);
			$attribs = (isset($this->_value)?' value="'.$this->_value.'"':'').
			           (isset($this->_selected)?($this->_selected?' selected="true"':''):'').$attrs;
			
			$str = parent::emit_str($attribs, $flags);
											
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function value($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->_value = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?look_r($this->_value):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->_value, RETURN_NULL_OK);
		}
		
		function selected($new=null)
		{
			if (isset($new))
			{
				$this->_selected = (boolean)$new;
			}
			return $this->_selected;
		}
	}

	//
	// CLASS: editselect
	//
	// class to instantiate select input controls.
	//
	class editselect extends edititem
	{
//		var $_debug = DEBUG_DETAIL_TRACE;
		
		var $_tag = 'select';
		var $_type = null;
		var $_req_close_tag	= true;
		
		var $_value_embed = false;
		
		var $_options = array();
		var $_optcssclass;
		var $_multiple;
		
		var $_selected= array();
		
		// initialize an object representing an HTML select control or list of such.
		// parameters:
		//		$options	- (optional) initializes the list of selectable options.  Value of
		//					  should be passed as an array of $values => $descriptions. The key 
		//					  portion ($value) will be set as the value attribute of the option, 
		//					  while the value portion ($description) will be the visible text in
		//					  the select list.
		//		$attrs	- content to be placed in tag as attributes.
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($options=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct(null, $attrs);
			if ($this->is_ok())
			{
            if (isset($attrs['options_css_classname'])) { $this->options_css_classname($attrs['options_css_classname']); }
                if (isset($attrs['multiple'])) { $this->multiple($attrs['multiple']); }
				
				// add any options
				if (is_array($options))
				{
					foreach($options as $val => $desc)
					{
						$this->add_option($val, $desc);
					}
				}
				
				if ($this->has_val_from_request())
				{
					$selvalues = parent::value();
				}
				else if (isset($attrs['selected']))
				{
					$selvalues = $attrs['selected'];
				}
				
				// select specified option(s)
				if (isset($selvalues))
				{
					if (is_array($selvalues))
					{
						if (!$this->multiple() && (count($selvalues)>1))
						{
							$this->set_error(__CLASS__.' element  named '.$this->_name.' does not have the multiple attribute but '.count($selvalues).' options are selected', __FILE__, __LINE__);
						}
						else
						{
							foreach($selvalues as $val)
                            {
				 				$this->select($val);
                            }
						}
					}
					else
					{
				 		$this->select($selvalues);
				 	}
				}
				unset($this->_type);
			}
			else
			{
				$this->set_error('Unable to initialize parent edititem class of '.$this->_type.' item named '.$attrs['name'].'.', __FILE__, __LINE__);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		// generates an HTML text string instantiating the select control.
		// This function overrides the edititem implementaton of this method, but calls
		// it to make use of it's base functionality.
		//
		// Unlike most other HTML controls, selects do not contain their value in a value
		// attribute contained within the tag.  In selects, a set of options is declared 
		// between the beginning and end tags.  One (or more in the case of multiple select 
		// controls) of these options may be selected.  Also, selects are their own tag 
		// type, and not a subtype of the <input> tag.  This requires some special handling 
		// when creating the HTML text in this method.
		//
		// Because the HTML text of each seperate control instance of an array-type control 
		// needs to be processed like this, the processing is done, one instance at a time, 
		// within a foreach loop.
		//
		// parameters:
		//		$index - index of array element to be generated.  If null, all array
		//				 elements are generated, separated by "\n" characters.  This
		//				 parameter is ignored by single type objects.
		//		$embedded - content to be placed in tag as attributes.
		//
		// returns:
		//		string	- HTML text
		//		false	- error
		//
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			// create option objects
			$this->clear_contents();
			foreach($this->_options as $key => $description)
			{
				$this->dbgecho($proc.'creating option val: '.look_r($key).', desc: '.look_r($description), DEBUG_PROC_TRACE);
				$opt_attribs = array();
            if (isset($this->_optcssclass)) { $opt_attribs['css_classname'] = $this->_optcssclass; }
                if ($this->selected($key)) { $opt_attribs['selected'] = true; }
				$opt_attribs['value'] = $key;
				
				$this->set_contentsx(new htmloption($description, $opt_attribs), $this->num_contents());
			}
			
			$attribs = (isset($this->_multiple)?($this->_multiple?' multiple="true"':''):'').$attrs;
			
			if ($this->multiple()) $flags = ($flags | HTMLBASE_EMIT_FLAG_APPEND_BRACKETS);
			$str = parent::emit_str($attribs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function multiple($new=null)
		{
			if (isset($new))
			{
				$this->_multiple = (boolean)$new;
			}
			return $this->_multiple;
		}
		
		function value($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->_selected = array();
				if (is_array($new))
				{
					foreach($new as $key)
					{
						$this->_selected[$key] = true;
					}
				}
				else
				{
					$this->_selected[$new] = true;
				}
			}
			$val = array_keys($this->_selected);
			if (isset($val[0]) && !$this->_multiple)
			{
				$val = $val[0];
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded:'.look_r($val):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok((count($val)>0?$val:null), RETURN_NULL_OK);
		}
		
		// this attribute doesn't apply to select controls
		// so intercept it here
		function readonly($new=null)
		{
			// only way to make select controls read-only is to disable them
			return $this->disabled($new);
		}
		
		function clear()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'() entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->_selected = array();
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		// Sets one option to be selected.
		//
		// parameters:
		//		$key	- the value of the option to be selected.  If selecting multiple 
		//				  options is required, call this method multiple times.
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function select($key)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$key.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (!$this->_multiple)
			{
				$this->_selected = array();
			}
			$this->_selected[$key] = true;
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		// Makes one or more options not selected.
		//
		// parameters:
		//		$key	- the value of the option to be unselected.  If selecting multiple 
		//				  options is required, call this method multiple times.
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function unselect($key)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$key.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			unset($this->_selected[$key]);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		// Checks whether an option is selected.
		//
		// parameters:
		//		$key	- the value of the option to be checked.  If checking multiple 
		//				  options is required, call this method multiple times.
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function selected($key)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$key.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$sel = array_key_exists($key, $this->_selected);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($sel);
		}
		
		// Adds another option to the select control.
		//
		// parameters:
		//		$key			- the value of the option to be added.
		//		$description	- the text to display to the user for this option.
		//		$where			- where in the option list to insert this. Positive
		//						  values insert at that position from the start of
		//						  the option list (0 = first.)  Negitive values 
		//						  insert at that position from the end of the list
		//						  (-1 = last.)
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function add_option($key, $description, $where=-1)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$key.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();

			// start here			
			if ($where == -1)
			{
				$this->_options[$key] = $description;
			}
			else
			{
				$new = array();
				$indices = array_keys($this->_options);
				$this->dbgecho($proc.'inserting at '.$where.', initial indicies - '.look_r($indices), DEBUG_DETAIL_TRACE);
				for($i=0; $i<min($where,count($this->_options)); $i++)
				{
					$new[$indices[$i]] = $this->_options[$indices[$i]];
				}
				$this->dbgecho($proc.'inserting at '.$where.', copy initial - '.look_r($new), DEBUG_DETAIL_TRACE);
				$new[$key] = $description;
				$this->dbgecho($proc.'inserting at '.$where.', inserted - '.look_r($new), DEBUG_DETAIL_TRACE);
				for($j=$i; $j<count($this->_options); $j++)
				{
					$new[$indices[$j]] = $this->_options[$indices[$j]];
				}
				$this->dbgecho($proc.'inserting at '.$where.', copy final - '.look_r($new), DEBUG_DETAIL_TRACE);
				$this->_options = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		// Removes an option to the select control.
		//
		// parameters:
		//		$key			- the value of the option to be removed.
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function remove_option($key)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$key.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			unset($this->_options[$key]);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
	}
	
	//
	// CLASS: editckbox
	//
	// class to instantiate a raw checkbox input controls.
	//
	class editckbox extends edititem
	{
		var $_type = 'checkbox';
		
		var $_checked = false;
		var $_sav_value;
		
		// initialize an object representing an HTML checkbox control.
		//
		// parameters:
		//		$name		- initializes HTML name attribute of checkbox control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			// save away value
			if (isset($attrs['value']))
			{
				$this->dbgecho($proc.'storing value '.look_r($attrs['value']).') in _sav_value', DEBUG_DETAIL_TRACE);
				$this->_sav_value = $attrs['value'];
				unset($attrs['value']);
			}
			
			parent::__construct(null, $attrs);
			if ($this->is_ok())
			{
				$this->dbgecho($proc.'base edit item initialized', DEBUG_DETAIL_TRACE);
				if ($this->has_val_from_request() || (count($_POST)>0))
				{
					$this->dbgecho($proc.'POST has occured: post variable is '.($this->has_val_from_request()?'':'un').'set.', DEBUG_DETAIL_TRACE);
					$this->_checked = (isset($this->_value)?true:false);
				}
				elseif (isset($attrs['checked']))
				{
					$this->dbgecho($proc.'No POST has occured: setting _checked to '.($attrs['checked']?'true':'false'), DEBUG_DETAIL_TRACE);
					$this->checked($attrs['checked']);
				}
			}
			else
			{
				$this->set_error('Unable to initialize parent edititem class of '.$this->_type.' item named '.$attrs['name'].'.', __FILE__, __LINE__);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		// generates an HTML text string instantiating the checkbox control.
		// This function overrides the edititem implementaton of this method, but calls
		// it to make use of it's base functionality.
		//
		// Because the HTML text of each seperate control instance of an array-type control 
		// needs to be processed to set the "checked" attribute, the processing is done, one 
		// instance at a time, within a foreach loop.
		//
		// parameters:
		//		$index - index of array element to be generated.  If null, all array
		//				 elements are generated, separated by "\n" characters.  This
		//				 parameter is ignored by single type objects.
		//		$embedded - content to be placed in tag as attributes.
		//
		// returns:
		//		string	- HTML text
		//		false	- error
		//
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (($flags & EDITITEM_EMIT_FLAG_READONLY)== EDITITEM_EMIT_FLAG_READONLY)
			{
				$onclickhandler = $this->get_event_handler('onclick');
				$this->set_event_handler('onclick', $onclickhandler.((strlen($onclickhandler)>0)?';':'').'return false;');
			}
			
			if (!$this->checked())
			{
				parent::value($this->_sav_value);
			}
			$attribs = ($this->checked()?' checked="true"':'').$attrs;
			$str = parent::emit_str($attribs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		// Sets checkbox to be checked or not.
		//
		// parameters:
		//		$new - new value of checked state.
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function checked($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().', '.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->_checked = $new;
				if ($this->_checked)
				{
                    if (!isset($this->_value)) { parent::value($this->_sav_value); }
				}
				else
				{
					if (isset($this->_value))
					{
						$this->_sav_value = $this->_value;
						parent::clear();
					}
				}
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?(!$this->_checked?'un':'').'checked':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->_checked);
		}
		
		function value($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().', '.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				if ($this->checked())
				{
					parent::value($new);
				}
				else
				{
					$this->_sav_value = $new;
				}
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok(parent::value(), RETURN_NULL_OK);
		}
	}
	
	class editradio extends editckbox
	{
//		var $_debug = DEBUG_DETAIL_TRACE;
		
		var $_type = 'radio';
		
		// initialize an object representing an HTML radio button control.
		//
		// parameters:
		//		$name		- initializes HTML name attribute of radio button control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct($attrs);
			if ($this->is_ok())
			{
				$this->dbgecho($proc.'base editckbox item initialized', DEBUG_DETAIL_TRACE);
				if ($this->has_val_from_request())
				{
					if ($this->_sav_value != $this->_value)
					{
						// this is meant for some other radio button, so make sure we are unchecked
						// the value is cleared and we are not reporting that the value has been set 
						// from the request
						$this->clear();
						$this->_checked = false;
						$this->_val_from_request = false;
					}
				}
			}
			else
			{
				$this->set_error('Unable to initialize parent editckbox class of '.$this->_type.' item named '.$attrs['name'].'.', __FILE__, __LINE__);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
/*		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$flags = ($flags | HTMLBASE_EMIT_FLAG_APPEND_BRACKETS);
			$str = parent::emit_str($attribs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		} */
	}
	
	//
	// CLASS: editbutton
	//
	// class to instantiate custom button input controls.
	//
	class editbutton extends edititem
	{
		var $_type = 'button';
		
		var $_pressed = false;
		
		// initialize an object representing an HTML button control or list of such.
		//
		// parameters:
		//		$name		- initializes HTML name attribute of button control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($caption=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			$attrs['value'] = $caption;
			
			parent::__construct(null, $attrs);
			if (!$this->is_ok())
			{
				$this->set_error('Unable to initialize edititem.', __FILE__, __LINE__);
			}
			else
			{
				if ($this->has_val_from_request())
                {
					$this->_pressed = ($this->value()==$caption);
                }
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function was_pressed()
		{
			return $this->_pressed;
		}
	}
	
	//
	// CLASS: editsubmit
	//
	// class to instantiate submit button input controls.
	//
	class editsubmit extends editbutton
	{
		var $_type = 'submit';
	}
	
	//
	// CLASS: editreset
	//
	// class to instantiate submit button input controls.
	//
	class editreset extends editbutton
	{
		var $_type = 'reset';
	}
	
	//
	// CLASS: editfileupload
	//
	// class to instantiate file upload controls.
	//
	class editfileupload extends edititem
	{
		var $_type = 'file';
		var $_value_embed = false;
		var $_fsize = 1048576;				// maximum file size 1 MEG default
		var $_invalid_msg = 'Invalid File';	// user understandable message describing validity of contents
		
		var $tmp_file;	// temorary name of file uploaded
		var $file_size;	// size of file uploaded
		var $file_type;	// alledged type of file uploaded
		var $file_name;	// original name (last component only) of file uploaded
						
		function __construct($maxsize=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			// create a default name
            if (!isset($attrs['name'])) { $attrs['name'] = 'loadfile'; }
			
			parent::__construct(null, $attrs);
			if ($this->is_ok())
			{
                if (isset($maxsize)) { $this->_fsize = $maxsize; }
				if (get_config_number(ini_get('post_max_size'))<$this->_fsize)
				{
					// scale down 
					$this->_fsize = get_config_number(ini_get('post_max_size'));
					$this->trigger_warning('Maximum file size lowered to match post_max_size configuration of '.ini_get('post_max_size'));
				}
				if (get_config_number(ini_get('upload_max_filesize'))<$this->_fsize)
				{
					// scale down 
					$this->_fsize = get_config_number(ini_get('upload_max_filesize'));
					$this->trigger_warning('Maximum file size lowered to match upload_max_filesize configuration of '.ini_get('upload_max_filesize'));
				}
				$this->dbgecho($proc.'Cfg value \'file_uploads\': '.look_r(ini_get('file_uploads')), DEBUG_DETAIL_TRACE);
				
				// determine whether a file has been loaded
				if (isset($_FILES[$attrs['name']]))
				{
					$this->dbgecho($proc.'File uploaded. Info: '.look_r($_FILES[$attrs['name']]), DEBUG_DETAIL_TRACE);
					$finfo = $_FILES[$attrs['name']];
					if ($finfo['error']!==UPLOAD_ERR_OK)
					{
						$this->_is_invalid = false;
						switch($finfo['error'])
						{
						case UPLOAD_ERR_INI_SIZE:
							$this->_invalid_msg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
							break;
						case UPLOAD_ERR_FORM_SIZE:
							$this->_invalid_msg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
							break;
						case UPLOAD_ERR_PARTIAL:
							$this->_invalid_msg = 'The uploaded file was only partially uploaded';
							break;
						case UPLOAD_ERR_NO_FILE:
							$this->_invalid_msg = 'No file was uploaded';
							break;
						case UPLOAD_ERR_NO_TMP_DIR:
							$this->_invalid_msg = 'Missing a temporary folder'; 
							break;
						case UPLOAD_ERR_CANT_WRITE:
							$this->_invalid_msg = 'Failed to write file to disk'; 
							break;
						default:
							$this->_invalid_msg = 'Unexpected file upload error code '.$finfo['error']; 
						}
						$this->dbgecho($proc.'File upload invalid: ['.$finfo['error'].'] '.look_r($this->_invalid_msg), DEBUG_DETAIL_TRACE);
					}
					elseif ($finfo['size']===0)
					{
						$this->_is_invalid = false;
						$this->_invalid_msg = 'Empty file uploaded'; 
						$this->dbgecho($proc.'File upload invalid: '.look_r($this->_invalid_msg), DEBUG_DETAIL_TRACE);
					}
					elseif (!isset($finfo['tmp_name']))
					{
						$this->set_error('Unknown error uploading file. Filename missing', __FILE__, __LINE__);
					}
					elseif (!is_uploaded_file($finfo['tmp_name']))
					{
						$this->set_error('Attempted file insertion attack detected', __FILE__, __LINE__);
					}
					if ($this->_val_from_request = $this->is_ok())
					{
						$this->tmp_file = $finfo['tmp_name'];
						$this->file_size = $finfo['size'];
						$this->file_type = $finfo['type'];
						$this->file_name = $finfo['name'];
					}
				}
				else
				{
					$this->dbgecho($proc.'File uploaded. Info: '.look_r($_FILES), DEBUG_DETAIL_TRACE);
					$this->_val_from_request = false;
				}
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().','.$flags.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$str='';
			if ($this->do_once_for_class('file_upload_emit_hidden'))
			{
				$ctrl = new edithidden(array('name' => 'MAX_FILE_SIZE',
											 'value' => $this->_fsize));
				$str = $ctrl->emit_str();
			}
			
			$str .= parent::emit_str($attrs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function value($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			// ignore new value
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?(look_r($this->tmp_file)):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->tmp_file, RETURN_NULL_OK);
		}
		
		function file_size()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?(look_r($this->file_size)):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->file_size, RETURN_NULL_OK);
		}
		
		function file_type()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?(look_r($this->file_type)):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->file_type, RETURN_NULL_OK);
		}
		
		function file_org_name()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?(look_r($this->file_name)):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->file_name, RETURN_NULL_OK);
		}
		
		function copy_to($newpath)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().', '.$newpath.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (!move_uploaded_file($this->tmp_file, $newpath))
			{
				$this->set_error('Unable to move uploaded file', __FILE__, __LINE__);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
	}
	
	class htmlform extends htmlcontainer
	{
		var $action; // action is set to generating script by default
		var $method = 'post';
		var $target;
		var $enctype;
		
		var $_tag = 'form';
		var $_req_close_tag = true;
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
				$this->action(isset($attrs['action'])?$attrs['action']:$_SERVER['PHP_SELF']);
				if (isset($attrs['method'])) $this->method($attrs['method']);
				if (isset($attrs['target'])) $this->target($attrs['target']);
				if (isset($attrs['enctype'])) $this->enctype($attrs['enctype']);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function emit_str($tag_attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attrs = ' action="'.$this->action.'" method="'.strtoupper($this->method).'"'.
			         (isset($this->target)?' target="'.$this->target.'"':'').
			         (isset($this->enctype)?' enctype="'.$this->enctype.'"':'').$tag_attrs;
			
			$str = parent::emit_str($attrs, $flags)."\n";
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function action($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->action = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->action, RETURN_NULL_OK);
		}
		
		function method($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->method = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->method, RETURN_NULL_OK);
		}
		
		function target($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->target = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->target, RETURN_NULL_OK);
		}
		
		function enctype($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->enctype = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->enctype, RETURN_NULL_OK);
		}
	}
 ?>