<?php
require_once(searchinc('htmlforms.inc'));
require_once(searchinc('datetime.inc'));

    class multicontrol extends edititem
    {
	// override to describe component controls of this multi-control
                                    // example:
                                    // array('component_name' => 
                                    //              array('classname' => 'editcontrol',
                                    //			  'name_suffix' => '_ctrl1',
                                    //			  'caption' => 'thingy',
                                    //			  'attrs' = array('size' => 40),  // normally attributes
                                    //			  'param2' = array(1, 2)),        // optional
        var $_ctrl_desc = array();
        
	var $_ctrls = array();

	var $_tag = 'span';
	var $_req_close_tag	= true;
	var $_value_embed = false;
		
	var $_contents_populated = false;
		
	function __construct($attrs=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($attrs['name'])?$attrs['name']:'').') entering', DEBUG_PROC_TRACE);
			
            // initialize components
            foreach($this->_ctrl_desc as $id => $info)
            {
		if (!isset($info['classname']) || !isset($info['name_suffix']))
		{
                    $this->set_error('Missing required classname and name_suffix parameters for control', __FILE__, __LINE__);
		}
		else
		{
                    $ctrl_attrs = isset($attrs)?$attrs:array();
                    if (isset($info['attrs'])) $ctrl_attrs = array_Merge_v4($ctrl_attrs, $info['attrs']);
                    $ctrl_attrs['name'] = $ctrl_attrs['name'].$info['name_suffix'];
                    if (isset($this->_default[$id])) $ctrl_attrs['default'] = $this->_default[$id];
                    if (isset($this->_disabled[$id])) $ctrl_attrs['disabled'] = $this->_disabled[$id];
                    if (isset($this->_readonly[$id])) $ctrl_attrs['readonly'] = $this->_readonly[$id];
                    if (isset($this->_size[$id])) $ctrl_attrs['size'] = $this->_size[$id];
                    if (isset($this->_value[$id])) $ctrl_attrs['value'] = $this->_value[$id];
                    if (isset($this->_invalid_css_class[$id])) $ctrl_attrs['invalid_classname'] = $this->_invalid_css_class[$id];
                    if (isset($this->_invalid_style[$id])) $ctrl_attrs['invalid_style'] = $this->_invalid_style[$id];
				
                    if (!isset($info['param2']))
                    {
                        $this->_ctrls[$id] = new $info['classname']($ctrl_attrs);
                    }
                    else
                    {
                        $this->_ctrls[$id] = new $info['classname']($ctrl_attrs,  $info['param2']);
                    }
                    if (!$this->_ctrls[$id]->is_ok())
                    {
			$this->copy_error($this->_ctrls[$id]);
			$this->set_error('Unable to create control '.$id.'of class '.$info['classname'], __FILE__, __LINE__);
                    }
		}
                if (!$this->is_ok()) break;
            }
			
            if ($this->is_ok())
            {
		parent::__construct(null, $attrs);
            }
			
            if (!$this->is_ok())
            {
		$this->trigger_error();
            }
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
	}
		
	function is_valid(&$msg, $ctrlid=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.' entering', DEBUG_PROC_TRACE);
			
            if (isset($ctrlid))
            {
		$isvalid = $this->_ctrls[$ctrlid]->is_valid($msg);
            }
            else
            {
		$isvalid = true;
		foreach($this->_ctrls as $id => $ctrl)
		{
                    if (!$ctrl->is_valid($tmp))
                    {
			$msg = $tmp;
			$isvalid = false;
			break;
                    }
		}
            }
            $this->dbgecho($proc.'is '.(!$isvalid?('invalid: '.$msg):'valid'), DEBUG_DETAIL_TRACE);
            return $isvalid;
	}
		
	function & get_control($ctrlid)
	{
            if (isset($this->_ctrls[$ctrlid]))
            {
		$ctrl =& $this->_ctrls[$ctrlid];
            }
            else
            {
		$this->set_error('Nonexistant component id used: '.$ctrlid, __FILE__, __LINE__);
            }
			
            if (!$this->is_ok())
            {
		return false;
            }
            else
            {
		return $ctrl;
            }
        }
		
	function has_val_from_request($ctrlid=null)
	{
            $this->clear_error();
			
            if (isset($ctrlid))
            {
		$has = $this->_ctrls[$ctrlid]->has_val_from_request();
            }
            else
            {
		$has = false;
		foreach($this->_ctrls as $id => $ctrl)
		{
                    if ($ctrl->has_val_from_request())
                    {
			$has = true;
			break;
                    }
		}
            }
			
            return $this->is_ok($has);
	}
		
	function value($new=null, $ctrlid=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($new)?$new:'').', '.$ctrlid.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (isset($new))
            {
		if (isset($ctrlid))
		{
                    $this->_ctrls[$ctrlid]->value($new);
		}
		elseif (is_array($new))
		{
                    foreach($new as $id => $newval)
                    {
			if (isset($this->_ctrls[$id]))
			{
                            $this->_ctrls[$id]->value($newval);
			}
			else
			{
                            $this->set_error('Nonexistant component id used: '.$id, __FILE__, __LINE__);
			}
                    }
		}
		else
		{
		$this->set_error('Passed value not an array'.$id, __FILE__, __LINE__);
		}
            }
			
            if (isset($ctrlid))
            {
                $val = $this->_ctrls[$ctrlid]->value();
            }
            else
            {
                $val = array();
                foreach($this->_ctrls as $id => $ctrl)
		{
                    $val[$id] = $ctrl->value();
		}
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($val, RETURN_NULL_OK);
	}
		
	function clear($ctrlid=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($ctrlid)?$ctrlid:'').') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (isset($ctrlid))
            {
		$this->_ctrls[$ctrlid]->clear();
            }
            else
            {
		foreach($this->_ctrls as $id => $ctrl)
		{
                    $def = $ctrl->defaultt();
                    if ($ctrl->is_ok() && isset($def))
                    {
                        $this->_ctrls[$id]->value($def);
                    }
                    else
                    {
                        $this->_ctrls[$id]->clear();
                    }
		}
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
	}
		
	function size($new=null, $ctrlid=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($new)?$new:'').', '.(isset($ctrlid)?$ctrlid:'').') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (isset($new))
            {
		if (isset($ctrlid))
		{
                    $this->_ctrls[$ctrlid]->size($new);
		}
		elseif (is_array($new))
		{
                    foreach($new as $id => $newval)
                    {
			if (isset($this->_ctrls[$id]))
			{
                            $this->_ctrls[$id]->size($newval);
			}
			else
			{
                            $this->set_error('Nonexistant component id used: '.$id, __FILE__, __LINE__);
			}
                    }
                }
                else
                {
                    $this->set_error('Passed value not an array'.$id, __FILE__, __LINE__);
                }
            }
			
            if (isset($ctrlid))
            {
		$size = $this->_ctrls[$ctrlid]->size();
            }
            else
            {
		$size = array();
		foreach($this->_ctrls as $id => $ctrl)
		{
                    $size[$id] = $ctrl->size();
		}
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($size, RETURN_NULL_OK);
	}
		
	function defaultt($new=null,$ctrlid=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($new)?$new:'').', '.(isset($ctrlid)?$ctrlid:'').') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (isset($new))
            {
		if (isset($ctrlid))
		{
                    $this->_ctrls[$ctrlid]->defaultt($new);
		}
		elseif (is_array($new))
		{
                    foreach($new as $id => $newval)
                    {
                        if (isset($this->_ctrls[$id]))
			{
                            $this->_ctrls[$id]->defaultt($newval);
			}
			else
			{
                            $this->set_error('Nonexistant component id used: '.$id, __FILE__, __LINE__);
			}
                    }
		}
		else
		{
		$this->set_error('Passed value not an array'.$id, __FILE__, __LINE__);
		}
            }
			
            if (isset($ctrlid))
            {
		$def = $this->_ctrls[$ctrlid]->defaultt();
            }
            else
            {
		$def = array();
		foreach($this->_ctrls as $id => $ctrl)
		{
                    $def[$id] = $ctrl->defaultt();
		}
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($def, RETURN_NULL_OK);
	}
		
	function disabled($new=null, $ctrlid=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($new)?$new:'').', '.(isset($ctrlid)?$ctrlid:'').') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (isset($new))
            {
		if (isset($ctrlid))
		{
                    $this->_ctrls[$ctrlid]->disabled($new);
		}
		elseif (is_array($new))
		{
                    foreach($new as $id => $newval)
                    {
			if (isset($this->_ctrls[$id]))
			{
                            $this->_ctrls[$id]->disabled($newval);
			}
			else
			{
                            $this->set_error('Nonexistant component id used: '.$id, __FILE__, __LINE__);
			}
                    }
		}
		else
		{
                    $this->set_error('Passed value not an array'.$new, __FILE__, __LINE__);
		}
            }
			
            if (isset($ctrlid))
            {
		$dis = $this->_ctrls[$ctrlid]->disabled();
            }
            else
            {
		$dis = array();
		foreach($this->_ctrls as $id => $ctrl)
		{
                    $dis[$id] = $ctrl->disabled();
		}
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($dis, RETURN_NULL_OK);
	}
		
	function readonly($new=null, $ctrlid=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($new)?$new:'').', '.(isset($ctrlid)?$ctrlid:'').') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (isset($new))
            {
		if (isset($ctrlid))
		{
                    $this->_ctrls[$ctrlid]->readonly($new);
		}
		elseif (is_array($new))
		{
                    foreach($new as $id => $newval)
                    {
			if (isset($this->_ctrls[$id]))
			{
                            $this->_ctrls[$id]->readonly($newval);
			}
			else
			{
                            $this->set_error('Nonexistant component id used: '.$id, __FILE__, __LINE__);
			}
                    }
                }
                else
                {
                    $this->set_error('Passed value not an array'.$id, __FILE__, __LINE__);
                }
            }
			
            if (isset($ctrlid))
            {
		$rdony = $this->_ctrls[$ctrlid]->readonly();
            }
            else
            {
		$rdony = array();
		foreach($this->_ctrls as $id => $ctrl)
		{
                    $rdony[$id] = $ctrl->readonly();
		}
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($rdony, RETURN_NULL_OK);
	}
		
        // the remainder of methods are copied from htmlcustom_inline and htmlcustom_block classes 
	// defined in htmlcustom.inc.  These allow child classes to override the populate_contents 
	// method and have all the output stuff (emit_*) work transparently
		
	// override this in derived classes to create custom contents
	function populate_contents()
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            // by default, fill with captions and controls
            $firsttime = true;
            foreach($this->_ctrls as $id => $ctrl)
            {
		$this->set_contentsx(array(($firsttime?'':', ').$this->_ctrl_desc[$id]['caption'].': ',
								$this->_ctrls[$id]),
								$this->num_contents());
		$firsttime = false;
            }
			
            $this->_contents_populated = $this->is_ok();
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
	}
		
	// Don't override the following methods unless you really have thought hard about it.
	function emit_str($tag_attrs='', $flags=0)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (!$this->_contents_populated) $this->populate_contents();
            if ($this->is_ok())
            {
		$str = parent::emit_str($tag_attrs, $flags);
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str);
	}
		
	function emit_classdefs($arr=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
				
            if (!$this->_contents_populated) $this->populate_contents();
            if ($this->is_ok())
            {
		$str = parent::emit_classdefs($arr);
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?look_r($str):'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str);
	}
		
	function emit_class_scripts($arr=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            if (!$this->_contents_populated) $this->populate_contents();
            if ($this->is_ok())
            {
		$str = parent::emit_class_scripts($arr);
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str, RETURN_NULL_OK);
        }
    }

    define('EDITDATE_FLAG_PORTION_DATE_TIME', 0);
    define('EDITDATE_FLAG_PORTION_DATE_ONLY', 1);
    define('EDITDATE_FLAG_PORTION_TIME_ONLY', 2);
    // used for validation of editdate value
    // this is not a class method in order to avoid it being
    // accidentally overridden
    function editdate_validate($date_ar, $which, &$mesg)
    {
//	$proc = __CLASS__.'::'.__FUNCTION__.': ';
//	$this->dbgecho($proc.'('.look_r($date_ar).') entering', DEBUG_PROC_TRACE);
		
	$valid = true;
	// make sure that the date and time sections are either complete or empty
	if ((($which & EDITDATE_FLAG_PORTION_TIME_ONLY) != EDITDATE_FLAG_PORTION_TIME_ONLY) &&
	    !empty($date_ar['year']) || !empty($date_ar['month']) || !empty($date_ar['day']))
	{
            $valid = (!empty($date_ar['year']) && !empty($date_ar['month']) && !empty($date_ar['day']));
            if (!$valid)
            {
//              $this->dbgecho($proc.'Missing year, month, or day.', DEBUG_DETAIL_TRACE);
                $mesg = 'Date is incomplete';
            }
            else
            {
		$valid = checkdate ($date_ar['month'], $date_ar['day'], $date_ar['year']);
		if (!$valid)
		{
//                  $this->dbgecho($proc.'Invalid date.', DEBUG_DETAIL_TRACE);
                    $mesg = 'This is not a real date';
		}
            }
        }
	if ($valid && (($which & EDITDATE_FLAG_PORTION_DATE_ONLY) != EDITDATE_FLAG_PORTION_DATE_ONLY) &&
	    (!empty($date_ar['hour']) || !empty($date_ar['minute']) || !empty($date_ar['second']) || !empty($date_ar['meridiem'])))
	{
            $valid = (!empty($date_ar['hour']) && ($date_ar['minute']!=''));
            if (!$valid)
            {
//		$this->dbgecho($proc.'Missing hour or minute.', DEBUG_DETAIL_TRACE);
		$mesg = 'Time is incomplete';
            }
            else
            {
		// check that time is real
		$valid = (($date_ar['hour'] <= 12) && ($date_ar['hour'] >= 1) &&
			($date_ar['minute'] >= 0) && ($date_ar['minute'] <= 59) &&
			($date_ar['second'] >= 0) || ($date_ar['second'] <= 59));
		if (!$valid)
		{
//                  $this->dbgecho($proc.'Bad hour, minute or second.', DEBUG_DETAIL_TRACE);
                    $mesg = 'Time is not a real time';
		}
            }
	}
		
//      $this->dbgecho($proc.' leaving - '.($valid?'valid':'invalid'), DEBUG_PROC_TRACE);
        return $valid;
    }
	
class editdate extends edititem
{
//  var $_debug = DEBUG_DETAIL_TRACE;
		
    var $_value_embed = false;	// value attribute is imbeded in the control tag
    var $_default;				// default value for control.  Set by subclass.
		
    var $_value;				// value for this item.  Set via constructor $value 
									// parameter or value (clear) method.
									
//  appropriate for most input controls, override where necessary
    var $_tag = 'span';
//  not required for most input controls, override where necessary
    var $_req_close_tag	= true;
		
    var $_attrs;	// saved creation attributes
		
    var $_dateonly;
    var $_timeonly;
		
    var $_timezone;
		
    var $_yearctl;
    var $_monctl;
    var $_dayctl;
    var $_hourctl;
    var $_minctl;
    var $_dictl;
		
    //  initialize an object representing a  date-time input item.
    //  parameters:
    //      $attrs	- initializes HTML attributes of input control
    //
    //  returns:
    //      true - success
    //	false - failure
    //
    function __construct($attrs=null, $date=null)
    {
	$proc = __CLASS__.'::'.__FUNCTION__.': ';
	$this->dbgecho($proc.'('.look_r($attrs['name']).', '.look_r($date).') entering', DEBUG_PROC_TRACE);
			
	static $eventnames = array('onclick', 'ondblclick',
	                           'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout',
				   'onkeypress', 'onkeydown', 'onkeyup',
				   'onfocus', 'onblur',
	                           'onselect', 'onchange');
			
	$this->_timezone = isset($attrs['timezone'])?$attrs['timezone']:get_client_tz();
			
	$now = getdate(isset($date)?(is_a($date, 'dateobj')?$date->timestamp():strtotime($date)):time());
			
	// ensure that date is a dateobj
	if (isset($date) && !is_a($date, 'dateobj'))
	{
            $olddate = $date;
            $date = new dateobj($date, $this->_timezone);
            if (!$date->is_ok())
            {
		$this->copy_error($date);
		$this->set_error('Unable to initialize date object with ['.$olddate.']', __FILE__, __LINE__);
            }
        }
			
	if ($this->is_ok())
	{
            if (isset($attrs['dateonly'])) $this->_dateonly = (boolean)$attrs['dateonly'];
            if (isset($attrs['timeonly']))
            {
		if ($this->_dateonly)
		{
                    $this->set_error('Both date only and time only attributes set for control.', __FILE__, __LINE__);
		}
		else
		{
                    $this->_timeonly = (boolean)$attrs['timeonly'];
		}
            }
            if ($this->is_ok())
            {
		$yroptions[0] = '';
		for($i = $now['year']-10; $i < $now['year']+11; $i++)
		{
                    $yroptions[$i] = (string)$i;
		}
		$monoptions = array(0 => '', 1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr',
	        	            5 => 'May', 6 => 'Jun', 7 => 'Jul', 8 => 'Aug',
                                    9 => 'Sep', 10 => 'Oct', 11 => 'Nov', 12 => 'Dec');
		$dayoptions[0] = '';
		for($i = 1; $i <= 31; $i++)
		{
                    $dayoptions[$i] = (string)$i;
		}
		$hroptions[0] = '';
		for($i = 1; $i <= 12; $i++)
		{
                    $hroptions[$i] = (string)$i;
		}
		$minoptions = array('' => ' ', 0 => '00', 15 => '15', 30 => '30', 45 => '45');
		$dioptions = array('' => ' ', 'am' => 'AM', 'pm' => 'PM');
					
		$name = $attrs['name'];
		$ctls_ar = array();
					
		if (!$this->_timeonly)
		{
                    $yrattrs = $attrs;
                    $yrattrs['name'] = $name.'_y';
                    if (isset($date))
                    {
			$this->dbgecho($proc.'Date: '.look_r($date), DEBUG_DETAIL_TRACE);
			$yrattrs['selected'] = $date->year();
                    }
                    $this->_yearctl = new editselect($yroptions, $yrattrs);
						
                    $monattrs = $attrs;
                    $monattrs['name'] = $name.'_mo';
                    if (isset($date))
                    {
			$monattrs['selected'] = $date->month();
                    }
                    $this->_monctl = new editselect($monoptions, $monattrs);
						
                    $dayattrs = $attrs;
                    $dayattrs['name'] = $name.'_d';
                    if (isset($date))
                    {
			$dayattrs['selected'] = $date->day();
                    }
                    $this->_dayctl = new editselect($dayoptions, $dayattrs);
		}
		if (!$this->_dateonly)
		{
                    $hrattrs = $attrs;
                    $hrattrs['name'] = $name.'_hr';
                    if (isset($date))
                    {
			$hr = $date->hour();
			$hrattrs['selected'] = ($hr>12?$hr-12:($hr>0?$hr:12));
                    }
                    $this->_hourctl = new editselect($hroptions, $hrattrs);
						
                    $minattrs = $attrs;
                    $minattrs['name'] = $name.'_m';
                    if (isset($date))
                    {
			$minattrs['selected'] = $date->minute();
                    }
                    $this->_minctl = new editselect($minoptions, $minattrs);
						
                    $diattrs = $attrs;
                    $diattrs['name'] = $name.'_di';
                    if (isset($date))
                    {
			$hr = $date->hour();
			$diattrs['selected'] = ($hr>=12?'pm':'am');
                    }
                    $this->_dictl = new editselect($dioptions, $diattrs);
		}
					
		parent::__construct($ctls_ar, $attrs);
		if ($this->is_ok())
		{
                    $this->dbgecho('Post var $_POST = '.look_r($_POST), DEBUG_DETAIL_TRACE);
                    if ($this->has_val_from_request())
                    {
			if (!$this->_timeonly)
			{
                            $year = $this->_yearctl->value();
                            $this->dbgecho('Year ctrl initialized with '.look_r($year), DEBUG_DETAIL_TRACE);
                            $month = $this->_monctl->value();
                            $this->dbgecho('Month ctrl initialized with '.look_r($month), DEBUG_DETAIL_TRACE);
                            $day = $this->_dayctl->value();
                            $this->dbgecho('Day ctrl initialized with '.look_r($day), DEBUG_DETAIL_TRACE);
                            if (!empty($year)) $dt['year'] = $year;
                            if (!empty($month)) $dt['month'] = $month;
                            if (!empty($day)) $dt['day'] = $day;
                        }
                        if (!$this->_dateonly)
                        {
                            $hour = $this->_hourctl->value();
                            $this->dbgecho('Hour ctrl initialized with '.look_r($hour), DEBUG_DETAIL_TRACE);
                            $minute = $this->_minctl->value();
                            $this->dbgecho('Minute ctrl initialized with '.look_r($hour), DEBUG_DETAIL_TRACE);
                            $di = $this->_dictl->value();
                            $dt['hour'] = ($hour?
                                             ($hour+(($hour<12)&&($di=='pm')?12:
                                                (($hour==12)&&($di=='am')?-12:0))):
                                           $hour);
                            $dt['minute'] = $minute;
                            $dt['second'] = (($hour || isset($minute))?0:null);
                        }
                        if (count($dt)>0)
                        {
                            $this->_value = new dateobj($dt, $this->_timezone);
                        }
                        else
                        {
                            $this->_value = null;
                        }
                        $this->_val_from_request = true;
                    }
                    else
                    {
                        $this->dbgecho('No Post var $_POST['.$this->name().']', DEBUG_DETAIL_TRACE);
                        if (!isset($date)) $date = new dateobj($this->defaultt(), $this->_timezone);
                        $this->_value = $date;
                        $this->_init_ctrls_from_value();
                    }
                    if (is_array($attrs))
                    {
                        foreach($attrs as $attr => $val)
                        {
                            if (in_array(strtolower($attr), $eventnames))
                            {
                                $this->set_event_handler(strtolower($attr), $val);
                            }
                        }
                    }
                    if (!editdate_validate(array('year' => $_POST[$name.'_y'],
                                                 'month' => $_POST[$name.'_mo'],
                                                 'day' => $_POST[$name.'_d'],
                                                 'hour' => $_POST[$name.'_hr'],
                                                 'minute' => $_POST[$name.'_m'],
                                                 'second' => '0',
                                                 'meridiem' => $_POST[$name.'_di']),
                                           (($this->_dateonly?EDITDATE_FLAG_PORTION_DATE_ONLY:0)|
                                            ($this->_timeonly?EDITDATE_FLAG_PORTION_TIME_ONLY:0)),
                                           $mesg))
                    {
                        $this->_is_invalid = true;
                        $this->_invalid_msg = $mesg;
                        $this->dbgecho($proc.'is '.($this->_is_invalid?('invalid: '.$this->_invalid_msg):'valid'), DEBUG_DETAIL_TRACE);
                    }
                    else
                    {
                        $this->_is_invalid = false;
                    }
                    $this->_attrs = $attrs;
                    $this->dbgecho('Initialized editdate = '.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()
    {
	if ($this->_timeonly)
	{
            return ($this->_hourctl->has_val_from_request() ||
			$this->_minctl->has_val_from_request() ||
			$this->_dictl->has_val_from_request());
	}
	elseif ($this->_dateonly)
	{
            return ($this->_yearctl->has_val_from_request() ||
			$this->_monctl->has_val_from_request() ||
			$this->_dayctl->has_val_from_request());
	}
	else
	{
            return ($this->_yearctl->has_val_from_request() ||
			$this->_monctl->has_val_from_request() ||
			$this->_dayctl->has_val_from_request() ||
			$this->_hourctl->has_val_from_request() ||
			$this->_minctl->has_val_from_request() ||
			$this->_dictl->has_val_from_request());
	}
    }
		
    function _init_ctrls_from_value()
    {
        $proc = __CLASS__.'::'.__FUNCTION__.': ';
        $this->dbgecho($proc.'('.
            (is_a($this->_value, 'dateobj')?$this->_value->display('Y/m/j G:i:s'):look_r($this->_value)).
            ') entering', DEBUG_PROC_TRACE);
        $this->clear_error();
			
        if (!$this->_timeonly)
        {
            if (is_a($this->_value, 'dateobj'))
            {
                // TBD make sure this year is in the option list
                $this->_yearctl->value($this->_value->year($this->_timezone));
                $this->_monctl->value($this->_value->month($this->_timezone));
                $this->_dayctl->value($this->_value->day($this->_timezone));
            }
            else
            {
                $this->_yearctl->clear();
                $this->_monctl->clear();
                $this->_dayctl->clear();
            }
	}
	if (!$this->_dateonly)
	{
            if (is_a($this->_value, 'dateobj'))
            {
		$hr = $this->_value->hour($this->_timezone);
		$this->dbgecho($proc.'hour: '.$hr, DEBUG_DETAIL_TRACE);
		$min = ((integer)($this->_value->minute($this->_timezone)/15))*15;	// minutes in 15 min granularity
		$this->dbgecho($proc.'raw min: '.$this->_value->minute($this->_timezone).', adj. min: '.$min, DEBUG_DETAIL_TRACE);
		$this->_hourctl->value($hr>12?$hr-12:($hr>0?$hr:12));
		$this->_minctl->value($min);
		$this->_dictl->value($this->_value->hour($this->_timezone)>=12?'pm':'am');
            }
            else
            {
		$this->_hourctl->clear();
		$this->_minctl->clear();
		$this->_dictl->clear();
            }
        }
			
	$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
	return $this->is_ok();
    }
		
    function value($new=null)
    {
	$proc = __CLASS__.'::'.__FUNCTION__.': ';
	$this->dbgecho($proc.'('.
		(is_a($new, 'dateobj')?$new->display('Y/m/j G:i:s'):look_r($new)).
		') entering', DEBUG_PROC_TRACE);
	$this->clear_error();
	$this->dbgecho($proc.'_value: '.(is_a($this->_value, 'dateobj')?$this->_value->display('Y/m/j G:i:s'):look_r($this->_value)), DEBUG_DETAIL_TRACE);
			
	if (isset($new))
	{
            $this->_value = new dateobj($new, $this->_timezone, $this->_mode);
            if (!$this->_value->is_ok())
            {
		$this->copy_error($this->_value);
		$this->set_error('Value ('.$new.') cannot be interpreted as a date or time', __FILE__, __LINE__);
            }
            else
            {
		$this->_init_ctrls_from_value();
            }
        }
			
	$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
	return $this->is_ok($this->_value, RETURN_NULL_OK);
    }
		
    function clear()
    {
	$proc = __CLASS__.'::'.__FUNCTION__.': ';
	$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
	$this->clear_error();
			
	if (parent::clear())
	{
            $this->_init_ctrls_from_value();
	}
			
	$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
	return $this->is_ok();
    }
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$label_style = isset($this->_attrs['style'])?$this->_attrs['style']:array();
			$label_style['font-size'] = 'smaller';
			$label_style['font-style'] = 'italic';
			$label_style['text-align'] = 'center';
			
			$cells = array();
			if (!$this->_timeonly)
			{
				$cells[] = new htmltabdata(array($this->_monctl,
												 new htmlbr(),
												 new htmlspan('Month',
															  array_Merge_v4($this->_attrs,
																		  array('style' => $label_style)))
												 ),
										   $this->attrs);
				$cells[] = new htmltabdata(array($this->_dayctl,
												 new htmlbr(),
												 new htmlspan('Day',
															  array_Merge_v4($this->_attrs,
																		  array('style' => $label_style)))
												 ),
										   $this->attrs);
				$cells[] = new htmltabdata(array($this->_yearctl,
												 new htmlbr(),
												 new htmlspan('Year',
															  array_Merge_v4($this->_attrs,
																		  array('style' => $label_style)))
												 ),
										   $this->attrs);
			}
			if (!$this->_timeonly && !$this->_dateonly)
			{
				$cells[] = new htmltabdata('&nbsp;',
										   $this->attrs);
			}
			if (!$this->_dateonly)
			{
				$cells[] = new htmltabdata(array($this->_hourctl,
												 new htmlbr(),
												 new htmlspan('Hour',
															  array_Merge_v4($this->_attrs,
																		  array('style' => $label_style)))
												 ),
										   $this->attrs);
				$cells[] = new htmltabdata(array($this->_minctl,
												 new htmlbr(),
												 new htmlspan('Min',
															  array_Merge_v4($this->_attrs,
																		  array('style' => $label_style)))
												 ),
										   $this->attrs);
				$cells[] = new htmltabdata(array($this->_dictl,
												 new htmlbr(),
												 new htmlspan('AM/PM',
															  array_Merge_v4($this->_attrs,
																		  array('style' => $label_style)))
												 ),
										   $this->attrs);
			}
			$contents = new htmltable(new htmlrow($cells, $this->attrs),
									  array_Merge_v4($this->attrs,
												  array('cellpadding' => 0,
														'cellspacing' => 0)));
			
			$this->set_contentsx($contents);
			
			$str = parent::emit_str($tag_attrs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
		
		function to_string($tz=null)
		{
			return $this->_value->to_string($tz);
		}
		
		function clear_date()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (!$this->_timeonly)
			{
				$this->_yearctl->clear();
				$this->_monctl->clear();
				$this->_dayctl->clear();
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
		
		function clear_time()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (!$this->_dateonly)
			{
				$this->_hourctl->clear();
				$this->_minctl->clear();
				$this->_dictl->clear();
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
    }
	
	define('EDITDURATION_SECONDS', 0);
	define('EDITDURATION_TENSOF_SECONDS', 1);
	define('EDITDURATION_MINUTES', 10);
	define('EDITDURATION_TENSOF_MINUTES', 11);
	define('EDITDURATION_QUARTER_HOURS', 12);
	define('EDITDURATION_HOURS', 20);
	define('EDITDURATION_DAYS', 30);
	class editduration extends edititem
	{
//		var $_debug = 5;
		
		var $_value_embed = false;	// value attribute is imbeded in the control tag
		var $_default;				// default value for control.  Set by subclass.
		
		var $_value;				// value for this item.  Set via constructor $value 
									// parameter or value (clear) method.
									
		//	appropriate for most input controls, override where necessary
		var $_tag = 'span';
		//	not required for most input controls, override where necessary
		var $_req_close_tag	= true;
		
		var $_resolution;	// finiest grain resolution (minimum seconds)
		var $_magnitude;	// largest granularity (maximum days)
		
		var $_value_units;
		
		var $_seconds;
		var $_minutes;
		var $_hours;
		var $_days;
		
		// initialize an object representing a  date-time input item.
		// parameters:
		//		$attrs	- initializes HTML attributes of input control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs=null, $duration=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$attrs['name'].') entering', DEBUG_PROC_TRACE);
			
			static $eventnames = array('onclick', 'ondblclick',
			                           'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout',
									   'onkeypress', 'onkeydown', 'onkeyup');
			
			$this->_resolution = isset($attrs['resolution'])?$attrs['resolution']:EDITDURATION_QUARTER_HOURS;
			$this->_magnitude = isset($attrs['magnitude'])?$attrs['magnitude']:EDITDURATION_DAYS;
			if (($this->_resolution > $this->_magnitude) ||
				(($this->_resolution != $this->_magnitude) && 
					((integer)($this->_resolution/10)>=(integer)($this->_magnitude/10))))
			{
				$this->set_error('Resolution ['.$this->_resolution.'] and magnitude ['.$this->_magnitude.'] are incompatable', __FILE__, __LINE__);
			}
			else
			{
				$this->dbgecho($proc.'resolution='.$this->_resolution.', magnitude='.$this->_magnitude, DEBUG_DETAIL_TRACE);
				$get_days = (((integer)($this->_resolution/10))<=3) && 
							(((integer)($this->_magnitude/10))>=3);
				if ($get_days) $this->_value_units = 'day';
				$this->dbgecho($proc.'$get_days='.($get_days?'true':'false'), DEBUG_DETAIL_TRACE);
				$get_hours = (((integer)($this->_resolution/10))<=2) && 
							 (((integer)($this->_magnitude/10))>=2);
				if ($get_hours) $this->_value_units = 'hour';
				$this->dbgecho($proc.'$get_hours='.($get_hours?'true':'false'), DEBUG_DETAIL_TRACE);
				$get_minutes = (((integer)($this->_resolution/10))<=1) && 
							   (((integer)($this->_magnitude/10))>=1);
				if ($get_minutes) $this->_value_units = 'minute';
				$this->dbgecho($proc.'$get_minutes='.($get_minutes?'true':'false'), DEBUG_DETAIL_TRACE);
				$get_seconds = (((integer)($this->_resolution/10))==0) && 
							   (((integer)($this->_magnitude/10))>=0);
				if ($get_seconds) $this->_value_units = 'second';
				$this->dbgecho($proc.'$get_seconds='.($get_seconds?'true':'false'), DEBUG_DETAIL_TRACE);
				$this->dbgecho($proc.'$_value_units='.$this->_value_units, DEBUG_DETAIL_TRACE);
				
				$this->dbgecho('Post var $_POST = '.look_r($_POST), DEBUG_DETAIL_TRACE);
				
				if ($get_seconds)
				{
					$secoptions[null] = '';
					switch ($this->_resolution)
					{
					case EDITDURATION_TENSOF_SECONDS:
						$increment = 10;
						break;
					default:
						$increment = 1;
					}
					for($i=0; $i<60; $i+=$increment)
					{
						$secoptions[$i] = (string)$i;
					}
					$this->_seconds = new editselect($secoptions, array_Merge_v4($attrs, array('name' => $attrs['name']._sc)));
					$this->_val_from_request |= $this->_seconds->has_val_from_request();
					$this->dbgecho($proc.'$_seconds created'.look_r($this->is_ok()), DEBUG_DETAIL_TRACE);
				}
				
				if ($get_minutes)
				{
					$minoptions[null] = '';
					switch ($this->_resolution)
					{
					case EDITDURATION_TENSOF_MINUTES:
						$increment = 10;
						break;
					case EDITDURATION_QUARTER_HOURS:
						$increment = 15;
						break;
					default:
						$increment = 1;
					}
					for($i=0; $i<60; $i+=$increment)
					{
						$minoptions[$i] = (string)$i;
					}
					$this->_minutes = new editselect($minoptions, array_Merge_v4($attrs, array('name' => $attrs['name'].'_mc')));
					$this->_val_from_request |= $this->_minutes->has_val_from_request();
					$this->dbgecho($proc.'$_minutes created'.look_r($this->is_ok()), DEBUG_DETAIL_TRACE);
				}
				
				if ($get_hours)
				{
					$hroptions[null] = '';
					switch ($this->_resolution)
					{
					default:
						$increment = 1;
					}
					for($i=0; $i<24; $i+=$increment)
					{
						$hroptions[$i] = (string)$i;
					}
					$this->_hours = new editselect($hroptions, array_Merge_v4($attrs, array('name' => $attrs['name'].'_hc')));
					$this->_val_from_request |= $this->_hours->has_val_from_request();
					$this->dbgecho($proc.'$_hours created'.look_r($this->is_ok()), DEBUG_DETAIL_TRACE);
				}
				
				if ($get_days)
				{
					$dyoptions[0] = '';
					switch ($this->_resolution)
					{
					default:
						$increment = 1;
					}
					for($i=1; $i<13; $i+=$increment)
					{
						$dyoptions[$i] = (string)$i;
					}
					$this->_days = new editselect($dyoptions, array_Merge_v4($attrs, array('name' => $attrs['name'].'_dc')));
					$this->_val_from_request |= $this->_days->has_val_from_request();
					$this->dbgecho($proc.'$_days created'.look_r($this->is_ok()), DEBUG_DETAIL_TRACE);
				}
				
				parent::__construct(null, $attrs);
				if ($this->is_ok())
				{
					if (!$this->_val_from_request)
					{
						// save duration as value
						if (isset($duration))
						{
							$this->_value = $duration;
							$this->dbgecho($proc.'$_value set to duration of '.$this->_value.' '.$this->_value_units, DEBUG_DETAIL_TRACE);
						}
						$this->_init_ctrls_from_value();
					}
					if ($this->is_ok())
					{
						if (is_array($attrs))
						{
							foreach($attrs as $attr => $val)
							{
								if (in_array(strtolower($attr), $eventnames))
								{
									$this->set_event_handler(strtolower($attr), $val);
								}
							}
						}
						$this->_is_invalid = false;
						
						// save attributes with name removed
						unset($attrs['name']);
						$this->attrs = $attrs;
					}
					$this->dbgecho('Initialized editduration = '.look_r($this), DEBUG_MINUTIA_TRACE);
				}
				else
				{
					$this->set_error('Unable to initialize edititem base class 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 ((isset($this->_seconds) && $this->_seconds->has_val_from_request()) ||
					(isset($this->_minutes) && $this->_minutes->has_val_from_request()) ||
					(isset($this->_hours) && $this->_hours->has_val_from_request()) ||
					(isset($this->_days) && $this->_days->has_val_from_request()));
		}
		
		function _init_ctrls_from_value()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->_value.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$duration = $this->_value;
			
			switch ((integer)($this->_resolution/10))
			{
			case 0:
				$seconds = $duration % 60;
				$duration = ($duration-$seconds)/60;
				if (isset($this->_seconds))
				{
					if ($duration || $seconds)
					{
						$this->_seconds->value($seconds);
					}
					else
					{
						$this->_seconds->clear();
					}
				}
				$this->dbgecho($proc.'$duration='.$duration.' $_seconds set to '.$seconds, DEBUG_DETAIL_TRACE);
				case 1:
				$minutes = $duration % 60;
				$duration = ($duration-$minutes)/60;
				if (isset($this->_minutes))
				{
					if ($duration || $minutes)
					{
						$this->_minutes->value($minutes);
					}
					else
					{
						$this->_minutes->clear();
					}
				}
				$this->dbgecho($proc.'$duration='.$duration.' $_minutes set to '.$minutes, DEBUG_DETAIL_TRACE);
			case 2:
				$hours = $duration % 24;
				$duration = ($duration-$hours)/24;
				if (isset($this->_hours))
				{
					if ($duration || $hours)
					{
						$this->_hours->value($hours);
					}
					else
					{
						$this->_hours->clear();
					}
				}
				$this->dbgecho($proc.'$duration='.$duration.' $_hours set to '.$hours, DEBUG_DETAIL_TRACE);
			case 3:
				if (isset($this->_days))
				{
					if ($duration)
					{
						$this->_days->value($duration);
					}
					else
					{
						$this->_days->clear();
					}
				}
				$this->dbgecho($proc.'$_days set to '.$duration, DEBUG_DETAIL_TRACE);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		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->_init_ctrls_from_value();
			}
			if ($this->is_ok())
			{
				$val = null;
				if (isset($this->_days)) $val = (integer)$this->_days->value();
				if (isset($this->_hours)) $val = ((integer)$this->_hours->value())+($val*24);
				if (isset($this->_minutes)) $val = ((integer)$this->_minutes->value())+($val*60);
				if (isset($this->_seconds)) $val = ((integer)$this->_seconds->value())+($val*60);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?look_r($val):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($val, RETURN_NULL_OK);
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$label_style = isset($this->attrs['style'])?$this->attrs['style']:array();
			$label_style['font-size'] = 'smaller';
			$label_style['font-style'] = 'italic';
			$label_style['text-align'] = 'center';
			
			$cells = array();
			if (isset($this->_days))
			{
				$cells[] = new htmltabdata(array($this->_days,
												 new htmlbr(),
												 new htmlspan('Days',
															  array_Merge_v4($this->attrs,
																		  array('style' => $label_style)))),
										   $this->attrs);
			}
			if (isset($this->_hours))
			{
				$cells[] = new htmltabdata(array($this->_hours,
												 new htmlbr(),
												 new htmlspan('Hours',
															  array_Merge_v4($this->attrs,
																		  array('style' => $label_style)))),
										   $this->attrs);
			}
			if (isset($this->_minutes))
			{
				$cells[] = new htmltabdata(array($this->_minutes,
												 new htmlbr(),
												 new htmlspan('Mins',
															  array_Merge_v4($this->attrs,
																		  array('style' => $label_style)))),
										   $this->attrs);
			}
			if (isset($this->_seconds))
			{
				$cells[] = new htmltabdata(array($this->_seconds,
												 new htmlbr(),
												 new htmlspan('Secs',
															  array_Merge_v4($this->attrs,
																		  array('style' => $label_style)))),
										   $this->attrs);
			}
			$contents = new htmltable(new htmlrow($cells, $this->attrs),
									  array_Merge_v4($this->attrs, array('cellpadding'=> 0,
									  								  'cellspacing'=>0)));
			
			$this->set_contentsx($contents);
			
			$str = parent::emit_str($tag_attrs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
		
		function to_string($tz=null)
		{
			return $this->_value.' '.$this->_value_units.($this->_value>1?'s':'');
		}
	}
	
	define('EDITDTRANGE_RANGE_STYLE_KEY', 'rangestyle');
	define('EDITDTRANGE_START_END_STYLE', 0);
	define('EDITDTRANGE_START_AND_PERIOD', 1);
	class editdaterange extends htmlspan
	{
//		var $_debug = 5;
		
		var $_start_ctrl;
		var $_finish_ctrl;
		var $_duration_ctrl;
		
		var $_range_style;
		
		var $_default_period = 0;	// in minutes
		var $_datectrl_classname = 'editdate';
		var $_durationctrl_classname = 'editduration';
		
		var $_val_from_request = false;
		
		// initialize an object representing a  date-time range input item.
		// parameters:
		//		$attrs	- initializes HTML attributes of input control
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs=null, $stdate=null, $endate=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$attrs['name'].') entering', DEBUG_PROC_TRACE);
			
			parent::__construct(null, $attrs);
			
			$datetime_ctrl_classname = (isset($attrs['datectrl_classname'])?
											$attrs['datectrl_classname']:
											$this->_datectrl_classname);
			$duration_ctrl_classname = (isset($attrs['durationctrl_classname'])?
											$attrs['durationctrl_classname']:
											$this->_durationctrl_classname);
			$start_ctrl = new $datetime_ctrl_classname(array_Merge_v4($attrs, array('name' => $attrs['name'].'_st')), 
													   $stdate);
			if ($start_ctrl->is_ok())
			{
				$this->copy_error($start_ctrl);
				$this->set_error('Unable to initialize start date control from ['.$stdate.']', __FILE__, __LINE__);
			}
			else
			{
				$this->_start_ctrl = $start_ctrl;
				
				$this->_range_style = isset($attrs[EDITDTRANGE_RANGE_STYLE_KEY])?$attrs[EDITDTRANGE_RANGE_STYLE_KEY]:EDITDTRANGE_START_AND_PERIOD;
				
				if (isset($endate))
				{
					$finish = new dateobj($endate, $start_ctrl->_timezone);
				}
				else
				{
					$startdate = $start_ctrl->value();
					$finish = new dateobj($startdate->timestamp()+($this->_default_period*60), $start_ctrl->_timezone);
				}
				
				if (!$finish->is_ok())
				{
					$this->copy_error($finish);
					$this->set_error('Unable to initialize finish date', __FILE__, __LINE__);
				}
				else
				{
					$finish_ctrl = new $datetime_ctrl_classname(array_Merge_v4($attrs, array('name' => $attrs['name'].'_fn')), 
																$finish);
					if (!$finish_ctrl->is_ok())
					{
						$this->copy_error($finish_ctrl);
						$this->set_error('Unable to initialize finish control', __FILE__, __LINE__);
					}
					else
					{
						$this->_finish_ctrl = $finish_ctrl;
						
						$duration_ctrl = new $duration_ctrl_classname(array_Merge_v4($attrs, array('name' => $attrs['name'].'_du')), 
																	  (integer)($startdate->diff_seconds($finish)/60));
						if (!$duration_ctrl->is_ok())
						{
							$this->copy_error($duration_ctrl);
							$this->set_error('Unable to initialize duration control', __FILE__, __LINE__);
						}
						else
						{
							$this->_duration_ctrl = $duration_ctrl;
						
							if ($this->_range_style == EDITDTRANGE_START_AND_PERIOD)
							{
								$this->_val_from_request = $start_ctrl->has_val_from_request() ||
														   $duration_ctrl->has_val_from_request();
								if ($this->_val_from_request)
								{
									// initialized finish_ctrl from duration_ctrl
									$start = $start_ctrl->value();
									$duration = $duration_ctrl->value();	// duration in minutes
									$finish = new dateobj($start->timestamp()+($duration*60), $start_ctrl->_timestamp);
									$this->_finish_ctrl->value($finish);
								}
							}
							else
							{
								$this->_val_from_request = $start_ctrl->has_val_from_request() ||
														   $finish_ctrl->has_val_from_request();
							}
						}
					}
				}
			}
			
			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;
		}
		
		function value($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				if (!is_array($new))
				{
					$this->set_error('Parameter must be an array', __FILE__, __LINE__);
				}
				elseif (!(isset($new['start']) && isset($new['finish'])) &&
						!(isset($new['start']) && isset($new['duration'])))
				{
					$this->set_error('Parameter must have elements \'start\' and \'finish\' or \'start\' and \'duration\'', __FILE__, __LINE__);
				}
				else
				{
					if (!isset($new['finish']))
					{
						$startdate = new dateobj($new['start'], $this->_start_ctrl->_timezone);
						if (!$startdate->is_ok())
						{
							$this->copy_error($startdate);
							$this->set_error('Unable to interpret starting date and time', __FILE__, __LINE__);
						}
						else
						{
							$new['finish'] = $startdate->timestamp()+($new['duration']*(60*60));
						}
					}
					if ($this->is_ok())
					{
						$this->_start_ctrl->value($new['start']);
						if (!$this->_start_ctrl->is_ok())
						{
							$this->copy_error($this->_start_ctrl);
							$this->set_error('Unable to load starting date and time', __FILE__, __LINE__);
						}
						else
						{
							$this->_finish_ctrl->value($new['finish']);
							if (!$this->_finish_ctrl->is_ok())
							{
								$this->copy_error($this->_finish_ctrl);
								$this->set_error('Unable to load finishing date and time', __FILE__, __LINE__);
							}
						}
					}
				}
			}
			$value = array();
			$value['start'] = $this->_start_ctrl->value();
			$value['finish'] = $this->_finish_ctrl->value();
			$value['duration'] = (integer)($value['start']->diff_seconds($value['finish'])/60); // duration in munutes
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($value);
		}
		
		function to_string($tz=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$start = $this->_start_ctrl->value();
			$finish = $this->_finish_ctrl->value();
			
			$str = $start->to_string();
			if ($this->_range_style == EDITDTRANGE_START_AND_PERIOD)
			{
				$duration = (integer)($start->diff_seconds($finish)/60);
				$hours = (integer)($duration/60);
				$minutes = ($duration%60);
				$str .= ' for '.
						($hours?(sprintf("%02d", $hours).':'):'').
						sprintf("%02d", $minutes);
			}
			else
			{
				$str .=' - '.$finish->to_string();
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function emit_str($attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			// initialize contents
			$contents = array('Start: ',
			                  $this->_start_ctrl,
							  HTML_SEPERATOR);
			if ($this->_range_style == EDITDTRANGE_START_AND_PERIOD)
			{
				// calculate duration
				$start = $this->_start_ctrl->value();
				$finish = $this->_finish_ctrl->value();
				$duration = (integer)($start->diff_seconds($finish)/60);
				
				// prime the control
				$this->_duration_ctrl->value($duration);
				
				$contents[] = 'Duration: ';
				$contents[] = $this->_duration_ctrl;
			}
			else
			{
				$contents[] = 'Finish: ';
				$contents[] = $this->_finish_ctrl;
			}
			// create the contents
			$this->set_contentsx($contents);
			
			$str = parent::emit_str($attrs=null, $flags=0);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
	}
	
	class select_ck extends editckbox
	{
		var $_options = array();
		var $_multiple;
		
		var $_selected = array();
		var $_tok;					// hidden control acting as a stand-in.  Presence of this
									// indicates that this section has been initialized by the 
									// request but no options were selected by the user
		
		var $_attrs;
		
		function __construct($options=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
			
			$this->_tok = new edithidden(array('name' => $attrs['name'].'_tk'));
			if (!$this->_tok->is_ok())
			{
				$this->copy_error($this->_tok);
				$this->set_error('Unable to create token control', __FILE__, __LINE__);
			}
			else
			{
				if (isset($attrs['multiple'])) $this->multiple($attrs['multiple']);
				
				parent::__construct($attrs);
				if ($this->is_ok())
				{
					// 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
					{
						$this->_tok->value(1);
						$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);
					 	}
					}
					$this->_attrs = $attrs;
				}
			}
			
			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->_tok->has_val_from_request();
		}
		
		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.': $this='.look_r($this), DEBUG_DETAIL_TRACE);
			
			$spacer = ' ';
			$eol = "<br>\n";
			
			$tmp_attrs = $this->_attrs;
//			if (!isset($tmp_attrs['align'])) $tmp_attrs['align'] = 'right';
			$cont = new htmldiv($this->_tok, $tmp_attrs);
			if ($this->multiple())
			{
				$tmp_attrs['name'] = $tmp_attrs['name'].'[]';
			}
			
			// create a list of descriptions with associated checkbox or radio button objects
			foreach($this->_options as $key => $description)
			{
				$this->dbgecho($proc.'creating option val: '.look_r($key).', desc: '.look_r($description), DEBUG_PROC_TRACE);
				
				$tmp_attrs['value'] = $key;
				$tmp_attrs['checked'] = $this->selected($key);
				if ($this->_multiple)
				{
					$ckbx = new editckbox($tmp_attrs);
				}
				else
				{
					$ckbx = new editradio($tmp_attrs);
				}
				if ($this->selected($key)) $ckbx->checked(true);
				
				if ($tmp_attrs['align']=='right')
				{
					$line = array($description, $spacer, $ckbx, $eol);
				}
				else
				{
					$line = array($ckbx, $spacer, $description, $eol);
				}
				$cont->set_contents($line, $cont->num_contents());
			}
			
			$str = $cont->emit_str($attrs, $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.'('.$key.') 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 (!$this->_multiple)
			{
				$val = $val[0];
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($val);
		}
		
		function clear()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$key.') 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();
		}
	}
	
	define('EDITCOMBO_IMPLICIT_SELECT_KEY', 'implicit_select');
	define('EDITCOMBO_IMPLICIT_SELECT', 1);
	define('EDITCOMBO_EXPLICIT_SELECT', 0);
	class editcombo extends edititem
	{
//		var $_debug = DEBUG_DETAIL_TRACE;
		
		var $_value_embed = false;	// value attribute is imbeded in the control tag
		var $_default;				// default value for control.  Set by subclass.
		
		var $_value;				// value for this item.  Set via constructor $value 
									// parameter or value (clear) method.
									
		//	appropriate for most input controls, override where necessary
		var $_tag = 'span';
		//	not required for most input controls, override where necessary
		var $_req_close_tag	= true;
		
		var $_implicit_select = true;		// override to default whether selection of select vs direct edit 
											// is specified implicitly, or explicitly by the user.
		var $_select_list = array();		// items to initialize selection.  May be initialized by optional
											// parameter in constructor, calling the add_option method, or
											// overriding this member with an array of items indexed by key value.
		var $_editctrl_class = 'edittext';	// override this to specify the class to use for creating the 
											// control used for direct data entry.
		
		// handles for the actual controls
		var $_selection;
		var $_editctrl;
		var $_use_selection;
		var $_use_editctrl;
		
		
		// initialize an object representing a combo-box type control.
		// can be used for select one of a set of options, or add a different
		// option is none in the selection are suitable
		
		// parameters:
		//		$attrs       - initializes HTML attributes of input control
		//		$select_list - optional list of selection choices
		//
		// returns:
		//		true - success
		//		false - failure
		//
		function __construct($attrs=null, $select_list=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$attrs['name'].') entering', DEBUG_PROC_TRACE);
			
			static $eventnames = array('onclick', 'ondblclick',
			                           'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout',
									   'onkeypress', 'onkeydown', 'onkeyup');
			
			// initialize component controls
			$this->_selection = new editselect($this->_select_list, array_Merge_v4($attrs, array('name' => $attrs['name']._sel)));
			if (!$this->_selection->is_ok())
			{
				$this->copy_error($this->_selection);
				$this->set_error('Unable to create select control component', __FILE__, __LINE__);
			}
			else
			{
				$this->_val_from_request |= $this->_selection->has_val_from_request();
				
				$this->_editctrl = new $this->_editctrl_class(array_Merge_v4($attrs, array('name' => $attrs['name']._edt)));
				if (!$this->_editctrl->is_ok())
				{
					$this->copy_error($this->_editctrl);
					$this->set_error('Unable to create direct edit control component', __FILE__, __LINE__);
				}
				else
				{
					$this->_val_from_request |= $this->_editctrl->has_val_from_request();
					
					if (isset($attrs[EDITCOMBO_IMPLICIT_SELECT_KEY])) $this->_implicit_select = $attrs[EDITCOMBO_IMPLICIT_SELECT_KEY];
					if (!$this->_implicit_select)
					{
						$this->_use_selection = new editradio(array_Merge_v4($attrs, array('name' => $attrs['name']._which,
						                                                                'value' => 'use_selection')));
						if (!$this->_use_selection->is_ok())
						{
							$this->copy_error($this->_use_selection);
							$this->set_error('Unable to create select radio button', __FILE__, __LINE__);
						}
						else
						{
							$this->_val_from_request |= $this->_use_selection->has_val_from_request();
							
							$this->_use_editctrl = new editradio(array_Merge_v4($attrs, array('name' => $attrs['name']._which,
						    	                                                            'value' => 'use_editcrtl')));
							if (!$this->_use_editctrl->is_ok())
							{
								$this->copy_error($this->_use_editctrl);
								$this->set_error('Unable to create direct edit control radio button', __FILE__, __LINE__);
							}
						}
					}
				}
			}
			
			if ($this->is_ok())
			{
				parent::__construct(null, $attrs);
				if ($this->is_ok())
				{
					if (!$this->_val_from_request)
					{
						$this->_init_ctrls_from_value();
					}
					if ($this->is_ok())
					{
						if (is_array($attrs))
						{
							foreach($attrs as $attr => $val)
							{
								if (in_array(strtolower($attr), $eventnames))
								{
									$this->set_event_handler(strtolower($attr), $val);
								}
							}
						}
						$this->_is_invalid = false;
						
						// save attributes with name removed
						unset($attrs['name']);
						$this->attrs = $attrs;
					}
					$this->dbgecho('Initialized editcombo = '.look_r($this), DEBUG_MINUTIA_TRACE);
				}
				else
				{
					$this->set_error('Unable to initialize edititem base class 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);
		}
		
		function _init_ctrls_from_value()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.look_r($this->value()).') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$tmp = $this->value();
			if (isset($tmp))
			{
				if (is_object($tmp)) $tmp = $tmp->to_string();
				// see if we can select on of the select options using the value
				if (is_array($tmp) ||
					!$this->_selection->select($tmp) ||
				    !$this->_selection->selected($tmp))
				{
					// nope, so try to set the value to the direct edit 
					$this->_editctrl->value($tmp);
					if (!$this->_editctrl->is_ok())
					{
						$this->set_error('Unable to initialize control components from value assigned', __FILE__, __LINE__);
					}
				}
			}
			else
			{
				$this->_selection->clear();
				$this->_editctrl->clear();
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		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->_init_ctrls_from_value();
			}
			if ($this->is_ok())
			{
				if ($this->_implicit_select)
				{
					$sel_val = $this->_selection->value();
					$this->dbgecho($proc.'implicit selection - select control value: '.look_r($sel_val), DEBUG_DETAIL_TRACE);
					if (!($from_selection = ((strlen($sel_val)>0) && (true !== $sel_val))))
					{
						$edt_val = $this->_editctrl->value();
						$this->dbgecho($proc.'implicit selection - direct edit control value: '.look_r($edt_val), DEBUG_DETAIL_TRACE);
						$from_editctrl = ((strlen($edt_val)>0) && (true !== $edt_val));
						if ($from_editctrl)
						{
							$this->dbgecho($proc.'implicit selection - getting from direct edit control', DEBUG_DETAIL_TRACE);
						}
						else
						{
							$this->dbgecho($proc.'implicit selection - getting neither control', DEBUG_DETAIL_TRACE);
						}
					}
					else
					{
						$this->dbgecho($proc.'implicit selection - getting from select control', DEBUG_DETAIL_TRACE);
					}
				}
				else
				{
					if ($this->_use_selection->checked())
					{
						$this->dbgecho($proc.'explicit selection - getting from select control', DEBUG_DETAIL_TRACE);
						$from_selection = true;
					}
					elseif ($this->_use_editctrl->checked())
					{
						$this->dbgecho($proc.'explicit selection - getting from direct edit control', DEBUG_DETAIL_TRACE);
						$from_editctrl = true;
					}
					else
					{
						$this->dbgecho($proc.'explicit selection - getting neither control', DEBUG_DETAIL_TRACE);
					}
				}
				$val = null;
				if ($from_selection)
				{
					$val = $this->_selection->value();
				}
				elseif ($from_editctrl)
				{
					$val = $this->_editctrl->value();
				}
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?look_r($val):'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($val, 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.'('.$index.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->dbgecho('Clearing value of item '.$this->name(), DEBUG_DETAIL_TRACE);
			
			$this->_selection->clear();
			if (!$this->_selection->is_ok())
			{
				$this->copy_error($this->_selection);
				$this->set_error('Unable to clear select control component', __FILE__, __LINE__);
			}
			else
			{
				$this->_editctrl->clear();
				if (!$this->_editctrl->is_ok())
				{
					$this->copy_error($this->_editctrl);
					$this->set_error('Unable to clear direct edit control component', __FILE__, __LINE__);
				}
				else
				{
					if ($this->_implicit_select)
					{
						$this->_use_selection->checked(false);
						$this->_use_editctrl->checked(false);
					}
				}
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		// 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();
			
			$this->_selection->add_option($key, $description, $where);
			if (!$this->_selection->is_ok())
			{
				$this->copy_error($this->_selection);
				$this->set_error('Unable to add option to select control component', __FILE__, __LINE__);
			}
			
			$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();
			
			$this->_selection->remove_option($key);
			if (!$this->_selection->is_ok())
			{
				$this->copy_error($this->_selection);
				$this->set_error('Unable to remove option from select control component', __FILE__, __LINE__);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->attrs['name'].') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$lable_style = isset($this->attrs['style'])?$this->attrs['style']:array();
			$lable_style['font-size'] = 'smaller';
			$lable_style['font-style'] = 'italic';
			$lable_style['text-align'] = 'right';
			
			$row1=array();
			if (!$this->_implicit_select)
			{
				$row1[] = new htmltabdata(array('Use ', $this->_use_selection),
										  array_Merge_v4($this->attrs,
													  array('style' => $lable_style)));
			}
			$row1[] = new htmltabdata($this->_selection);
			
			$row2=array();
			if (!$this->_implicit_select)
			{
				$row2[] = new htmltabdata(array('Use ', $this->_use_editctrl),
										  array_Merge_v4($this->attrs,
													  array('style' => $lable_style)));
			}
			$row2[] = new htmltabdata($this->_editctrl);
			
			$contents = new htmltable(array(new htmlrow($row1, $this->attrs),
											new htmlrow($row2, $this->attrs)),
									  array_Merge_v4($this->attrs, array('cellpadding'=> 0,
									  								  'cellspacing'=>0)));
			
			$this->set_contentsx($contents);
			
			$str = parent::emit_str($tag_attrs, $flags);
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
		
		function to_string()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->attrs['name'].') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if ($this->_implicit_select)
			{
				if (!($from_selection = strlen($this->_selection->value())>0))
				{
					$from_editctrl = (null !== $this->_editctrl->value());
				}
			}
			else
			{
				if ($this->_use_selection->checked())
				{
					$from_selection = true;
				}
				elseif ($this->_use_editctrl->checked())
				{
					$from_editctrl = true;
				}
			}
			$this->dbgecho($proc.'$from_selection='.look_r($from_selection).', $from_editctrl='.look_r($from_editctrl), DEBUG_DETAIL_TRACE);
			
			$str = null;
			if ($from_selection)
			{
				$key = $this->_selection->value();
				$this->dbgecho($proc.'selection->value='.look_r($key), DEBUG_DETAIL_TRACE);
				$str = $this->_selection->_options[$key];
			}
			elseif ($from_editctrl)
			{
				if (method_exists($this->_editctrl, 'to_string'))
				{
					$str = $this->_editctrl->to_string();
				}
				else
				{
					$val = $this->_editctrl->value();
					$str = (string)$val;
					$this->dbgecho($proc.'editctrl->value='.look_r($val), DEBUG_DETAIL_TRACE);
				}
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?$str:'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
	}
 ?>