<?php
require_once(searchinc('base.inc'));

    define('HTMLBASE_APPEND_CLASSNAME', true);
    define('HTMLBASE_REPLACE_CLASSNAME', false);
    class htmlbase extends base
    {
        var $_tag;				// set by subclass
        var $_req_close_tag;	// set by subclass

        var $_instance;

        var $_id;				// set by subclass or dynamically
        var $_css_classname;	// set by subclass or dynamically may be multiple names in an array
        var $_title;
        var $_tabindex;
        var $_obfusticate = false;
        var $_style = array();	// set by subclass or dynamically

        // HTML5 attributes (new or changed to be global)
        var $_accesskey;
        var $_contenteditable;
        var $_dir;
        var $_draggable;
        var $_dropzone;
        var $_hidden;
        var $_lang;
        var $_spellcheck;
        var $_translate;

        function __construct($attrs=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);

            parent::__construct(isset($attrs['mode'])?$attrs['mode']:DEFAULT_TRIGGER_ON_ERROR);
            if ($this->is_ok())
            {
                if (isset($attrs['id'])) { $this->_id = $attrs['id']; }
                if (isset($attrs['css_classname'])) { $this->css_classname($attrs['css_classname']); }
                if (isset($attrs['title'])) { $this->title($attrs['title']); }
                if (isset($attrs['style'])) { $this->style($attrs['style']); }
                if (isset($attrs['tabindex'])) { $this->tabindex($attrs['tabindex']); }
                if (isset($attrs['obfusticate'])) { $this->obfusticate($attrs['obfusticate']); }
                if (isset($attrs['accesskey'])) { $this->accesskey($attrs['accesskey']); }
                if (isset($attrs['contenteditable'])) { $this->contenteditable($attrs['contenteditable']); }
                if (isset($attrs['dir'])) { $this->dir($attrs['dir']); }
                if ($this->is_ok())
                {
                    if (isset($attrs['draggable'])) { $this->draggable($attrs['draggable']); }
                    if ($this->is_ok())
                    {
                        if (isset($attrs['dropzone'])) { $this->dropzone($attrs['dropzone']); }
                        if ($this->is_ok())
                        {
                            if (isset($attrs['hidden'])) { $this->hidden($attrs['hidden']); }
                            if (isset($attrs['lang'])) { $this->lang($attrs['lang']); }
                            if (isset($attrs['spellcheck'])) { $this->spellcheck($attrs['spellcheck']); }
                            if (isset($attrs['translate'])) { $this->translate($attrs['translate']); }
                        }
                    }
                }
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            if (!$this->is_ok())
            {
                $this->trigger_error();
            }
        }
		
        function emit_str($attrs=null, $contents=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $this->dbgecho($proc.'object - '.look_r($this), DEBUG_MINUTIA_TRACE);

            $str = '<'.$this->_tag.
                   (isset($this->_id)?' id="'.$this->_id.'"':'').
                   (isset($this->_css_classname)?' class="'.$this->_classname_to_str().'"':'').
                   (isset($this->_title)?' title="'.$this->_title.'"':'').
                   (isset($this->_tabindex)?' tabindex="'.$this->_tabindex.'"':'').
                   (isset($this->_accesskey)? ' accesskey="'.$this->_accesskey.'"':'').
                   (isset($this->_contenteditable)? ' contenteditable="'.($this->_contenteditable?'true':'false').'"':'').
                   (isset($this->_dir)? ' dir="'.$this->_dir.'"':'').
                   (isset($this->_draggable)? ' draggable="'.$this->_draggable.'"':'').
                   (isset($this->_dropzone)? ' dropzone="'.$this->_dropzone.'"':'').
                   (isset($this->_hidden)?
                        ($this->_hidden ? 
                                (' hidden'.(((isset($GLOBALS['use_xhtml_style'])) && $GLOBALS['use_xhtml_style']) ? '="hidden"' :
                                '')):
                            ''):
                        '').
                   (isset($this->_lang)? ' lang="'.$this->_lang.'"':'').
                   (isset($this->_spellcheck)? ' spellcheck="'.($this->_spellcheck?'true':'false').'"':'').
                   (isset($this->_translate)? ' translate="'.($this->_translate?'yes':'no').'"':'');

            if (count($this->_style)>0)
            {
                $str .= ' style="';
                foreach($this->_style as $property => $value)
                {
                    $str .= $property.': '.$value.'; ';
                }
                $str .= '"';
            }

            $str .= (isset($attrs)?$attrs:'').
                        ((!$this->_req_close_tag && ((isset($GLOBALS['use_xhtml_style'])) && $GLOBALS['use_xhtml_style']))?' /':'').'>'.
                        ($this->_req_close_tag?($this->_do_obfusticate($contents).'</'.$this->_tag.'>'):'');

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str, RETURN_NULL_OK);
        }

        function _classname_to_str($entry=null)
        {
            $classstr = "";
            if (!isset($entry)) { $entry = $this->_css_classname; }

            if (isset($entry))
            {
                if (is_array($entry))
                {
                    foreach($entry as $name)
                    {
                        $classstr .= $this->_classname_to_str($name).' ';
                    }
                }
                else
                {
                    $classstr = $entry;
                }
            }

            return trim($classstr);
        }

        function _do_obfusticate($str)
        {
            if ($this->obfusticate()) { $str = html_obfusticate($str); }
            return $str;
        }

        function emit()
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();
    
            echo $this->emit_str();
    
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        function style($property, $value=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (is_array($property))
            {
                foreach($property as $key => $value)
                {
                    if (isset($value)) $this->_style[$key] = $value;
                    $property[$key] = $this->_style[$key];
                }
            }
            else
            {
                if (isset($value)) $this->_style[$property] = $value;
                $ret = $this->_style[$property];
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok(isset($ret)?$ret:NULL, RETURN_NULL_OK);
        }
		
        function clear_style($property)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (is_array($property))
            {
                foreach($property as $key => $value)
                {
                    unset($this->_style[$key]);
                }
            }
            else
            {
                unset($this->_style[$property]);
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        function id($new=null)
        {
            if (isset($new)) { $this->_id = $new; }
            return $this->_id;
        }

        function css_classname($new=null, $append=HTMLBASE_REPLACE_CLASSNAME)
        {
            if (isset($new))
            {
                if (!$append)
                {
                    $this->_css_classname = explode(' ', $new);
                }
                else
                {
                    if (!is_array($this->_css_classname))
                    {
                        $this->_css_classname = (is_empty($this->_css_classname)?array():explode(' ', $this->_css_classname));
                    }
                    $this->_css_classname[] = explode(' ', $new);
                }
            }
            return $this->_classname_to_str();
        }

        function title($new=null)
        {
            if (isset($new))
            {
                $this->_title = $new;
            }
            return $this->_title;
        }
		
        function tabindex($new=null)
        {
            if (isset($new))
            {
                $this->_tabindex = $new;
            }
            return $this->_tabindex;
        }
		
        function obfusticate($new=null)
        {
            if (isset($new))
            {
                $this->_obfusticate = $new;
            }
            return $this->_obfusticate;
        }

        function accesskey($new=null)
        {
            if (isset($new))
            {
                $this->_accesskey = $new;
            }
            return $this->_accesskey;
        }

        function contenteditable($new=null)
        {
            if (isset($new))
            {
                if ($new == 'true') { $new = true; }
                elseif ($new == 'false') { $new = false; }
                else { $new = ($new == true); }

                $this->_contenteditable = $new;
            }
            return $this->_contenteditable;
        }

        function dir($new=null)
        {
            $this->clear_error();
            if (isset($new))
            {
                if ($new == 'rtl' || $new == 'ltr' || $new == 'auto')
                {
                    $this->_dir = $new;
                }
                else
                {
                    $this->set_error('Attenpt to set invalid dir attribute value: '.$new, __FILE__, __LINE__);
                }
            }
            return $this->is_ok($this->_dir, RETURN_NULL_OK);
        }

        function draggable($new=null)
        {
            $this->clear_error();
            if (isset($new))
            {
                if ($new === true ) { $new = 'true'; }
                if ($new === false) { $new = 'false'; }
                if ($new == 'true' || $new == 'false' || $new == 'auto')
                {
                    $this->_draggable = $new;
                }
                else
                {
                    $this->set_error('Attenpt to set invalid draggable attribute value: '.$new, __FILE__, __LINE__);
                }
            }
            return $this->is_ok($this->_draggable, RETURN_NULL_OK);
        }

        function dropzone($new=null)
        {
            $this->clear_error();
            if (isset($new))
            {
                if ($new == 'copy' || $new == 'move' || $new == 'link')
                {
                    $this->_dropzone = $new;
                }
                else
                {
                    $this->set_error('Attenpt to set invalid dropzone attribute value: '.$new, __FILE__, __LINE__);
                }
            }
            return $this->is_ok($this->_dropzone, RETURN_NULL_OK);
        }

        function hidden($new=null)
        {
            if (isset($new))
            {
                if ($new == 'true') { $new = true; }
                elseif ($new == 'false') { $new = false; }
                else { $new = ($new == true); }

                $this->_hidden = $new;
            }
            return $this->_hidden;
        }

        function lang($new=null)
        {
            if (isset($new))
            {
                $this->_lang = $new;
            }
            return $this->_lang;
        }

        function spellcheck($new=null)
        {
            if (isset($new))
            {
                if ($new == 'true') { $new = true; }
                elseif ($new == 'false') { $new = false; }
                else { $new = ($new == true); }

                $this->_spellcheck = $new;
            }
            return $this->_spellcheck;
        }

        function translate($new=null)
        {
            if (isset($new))
            {
                if ($new == 'true' || $new == 'yes') { $new = true; }
                elseif ($new == 'false' || $new == 'no') { $new = false; }
                else { $new = ($new == true); }

                $this->_translate = $new;
            }
            return $this->_translate;
        }
    }
	
    define('HTMLBASE_SCRIPT_SCOPE_CLASS', 0);
    define('HTMLBASE_SCRIPT_SCOPE_INSTANCE', 1);
    define('HTMLBASE_EMIT_FLAG_FORMAT_AS_DATE', 2);
    define('HTMLBASE_EMIT_FLAG_FORMAT_AS_TIME', 4);
    define('HTMLBASE_EMIT_FLAG_FORMAT_AS_DATETIME', 6);
    define('HTMLBASE_EMIT_FLAG_APPEND_BRACKETS', 8);
    class htmlcontainer extends htmlbase
    {
        var $_name;				// set by subclass or dynamically
        var $_event_handlers;	// set by subclass or dynamically

        var $_contents = array();

        var $_classes = array();

        var	$_parent;

        function __construct($contents=null, $attrs=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
			
            static $eventnames = array('onclick', 'ondblclick',
                                       'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout',
                                       'onwheel',
                                       'onkeypress', 'onkeydown', 'onkeyup',
                                       'onblur', 'onchange', 'oncontextmenu', 'onfocus', 'oninput', 'oninvalid', 'onreset',
                                       'onsearch', 'onselect', 'onsubmit');
			
            parent::__construct($attrs);
            if ($this->is_ok())
            {
                if (isset($attrs['name'])) $this->_name = $attrs['name'];
                if (isset($this->_name) && !isset($this->_id))
                {
                    $this->_id = $this->_cvt_name_to_id($this->_name);
                }
                if (isset($contents))
                {
                    $this->set_contentsx($contents);
                }
                if (is_array($attrs))
                {
                    foreach($attrs as $attr => $val)
                    {
                        if (in_array(strtolower($attr), $eventnames))
                        {
                           $this->dbgecho($proc.'setting event handler for '.strtolower($attr).' as '.$val, DEBUG_MINUTIA_TRACE);
                           $this->set_event_handler(strtolower($attr), $val);
                        }
                    }
                }
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            if (!$this->is_ok())
            {
                $this->trigger_error();
            }
        }
		
        function name($new=null)
        {
            if (isset($new))
            {
                $this->_name = $new;
            }
            return $this->_name;
        }
                
        function get_name()
        {
            return $this->name();
        }
                
        function set_name($new)
        {
            return $this->name($new);
        }
		
        function _interpret_contents($contents, $flags)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            $str = '';
            if (is_array($contents))
            {
                for($i=0; $i<count($contents); $i++)
                {
                    $str .= $this->_interpret_contents($contents[$i], $flags);
                }
            }
            else if (is_a($contents, __CLASS__))
            {
                $str = $contents->emit_str(null, $flags);
                if (!$contents->is_ok())
                {
                    $this->copy_error($contents);
                    $this->set_error('Unable to get text representation for '.get_class($contents).' object', __FILE__, __LINE__);
                }
            }
            else if (is_a($contents, 'htmlbase'))
            {
                $str = $contents->emit_str();
                if (!$contents->is_ok())
                {
                    $this->copy_error($contents);
                    $this->set_error('Unable to get text representation for '.get_class($contents).' object', __FILE__, __LINE__);
                }
            }
            else if (isset($contents))
            {
                switch ($flags & HTMLBASE_EMIT_FLAG_FORMAT_AS_DATETIME)
                {
                case HTMLBASE_EMIT_FLAG_FORMAT_AS_DATETIME:
                    $str = date('m-d-Y H:i:s', strtotime($contents));
                    break;
                case HTMLBASE_EMIT_FLAG_FORMAT_AS_DATE:
                    $str = date('m-d-Y', strtotime($contents));
                    break;
                case HTMLBASE_EMIT_FLAG_FORMAT_AS_TIME:
                    $str = date('H:i:s', strtotime($contents));
                    break;
                default:
                    $str = $contents;
                }
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str, RETURN_NULL_OK);
        }
		
        function & find_htmlobject($attrs=array(), $instance=1)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $obj = & $this->_int_find_htmlobject($attrs, $instance, $this->_contents);

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?get_class($obj):'failed'), DEBUG_PROC_TRACE);
            if (!$this->is_ok())
            {
                return false;
            }
            else
            {
                return $obj;
            }
        }

        function & _int_find_htmlobject($attrs, &$instance, &$contents)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.get_class($this).'-'.$this->_instance.','.$this->name().') entering', DEBUG_PROC_TRACE);
            $this->dbgecho($proc.'attrs='.look_r($attrs), DEBUG_DETAIL_TRACE);
            $this->clear_error();

            $obj = null;
            $keys = array_keys($contents);
            $this->dbgecho($proc.'object contains '.count($keys).' contents items', DEBUG_DETAIL_TRACE);
            foreach($keys as $key)
            {
                $this->dbgecho($proc.'checking contents item at index '.$key, DEBUG_DETAIL_TRACE);
                if (is_a($contents[$key], 'htmlbase'))
                {
                    $this->dbgecho($proc.'object found at index '.$key, DEBUG_DETAIL_TRACE);
                    // check this object
                    $match = true;
                    foreach($attrs as $attr => $val)
                    {
                        if (method_exists($contents[$key], $attr))
                        {
                            $this->dbgecho($proc.'checking method '.$contents[$key]->{$attr}().' against '.$val, DEBUG_DETAIL_TRACE);
                            $match = ($contents[$key]->{$attr}() === $val);
                        }
                        else
                        {
                            $this->dbgecho($proc.'checking member '.$contents[$key]->{$attr}.' against '.$val, DEBUG_DETAIL_TRACE);
                            $match = ($contents[$key]->{$attr} === $val);
                        }
                        if (!$match) { break; }
                    }
                    if ($match)
                    {
                        $this->dbgecho($proc.get_class($contents[$key]).' object at index '.$key.' matches', DEBUG_DETAIL_TRACE);
                        if (--$instance == 0)
                        {
                            // found it
                            $obj = & $contents[$key];
                            break;
                        }
                        else
                        {
                            $this->dbgecho($proc.'searching for '.$instance.' instance'.($instance>1?'s':''), DEBUG_DETAIL_TRACE);
                        }
                    }
                    if (is_a($contents[$key], __CLASS__))
                    {
                        $this->dbgecho($proc.'searching for object in nested object at index '.$key, DEBUG_DETAIL_TRACE);
                        $obj = & $this->_int_find_htmlobject($attrs, $instance, $contents[$key]->_contents);
                        if (false !== $obj) { break; }
                    }
                }
                elseif (is_array($contents[$key]))
                {
                    $this->dbgecho($proc.'searching for object in nested array at index '.$key, DEBUG_DETAIL_TRACE);
                    $obj = & $this->_int_find_htmlobject($attrs, $instance, $contents[$key]);
                    if (false !== $obj) { break; }
                }
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?get_class($obj):'failed'), DEBUG_PROC_TRACE);
            if (!$this->is_ok())
            {
                return false;
            }
            else
            {
                return $obj;
            }
        }

        function _cvt_name_to_id($name)
        {
            return strtr($name, '[]', '__');
        }

        function emit_str($tag_attrs=null, $flags=0)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $this->dbgecho($proc.'object - '.look_r($this), DEBUG_MINUTIA_TRACE);

            $attrstr = (isset($this->_name)?' name="'.$this->_name.
                                                ((($flags & HTMLBASE_EMIT_FLAG_APPEND_BRACKETS)==HTMLBASE_EMIT_FLAG_APPEND_BRACKETS)?'[]':'').
                                                '"':'');
            if (is_array($this->_event_handlers))
            {
                foreach($this->_event_handlers as $event => $code)
                {
                    $attrstr .= ' '.$event.'="'.addslashes($code).'"';
                }
            }
            $cstr = $this->_interpret_contents($this->_contents, $flags);
            if ($this->is_ok())
            {
                $str = parent::emit_str($attrstr.$tag_attrs, $cstr);
            }
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str, RETURN_NULL_OK);
        }
		
        function emit($tag_attrs=null, $flags=0)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            echo $this->emit_str($tag_attrs, $flags);

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }

        // the follwing three functions, classdef(), clear_classdef() and emit_classdefs() are 
        // intended for managing css classes used by this object and it's children.  The returned 
        // value of emit_classdefs() contains all class definitions of this object and it's children 
        // ind is suitable for placing in a <style> container in the html page head.

        // set or return the style associated with a class name
        // style input may be either in the form of string with style componnets separated 
        // by semi-colons ";", OR may be in the form of an array with keys as the style 
        // attributes and values as the style attribute values.
        function classdef($name, $style=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (isset($style))
            {
                if (!is_array($style)) $style = $this->_style_format_cvt($style);
                $this->_classes[$name] = $style;
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($this->_classes[$name], RETURN_NULL_OK);
        }
		
        function clear_classdef($name)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            unset($this->_classes[$name]);

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        function emit_classdefs($arr=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $classes = '';
            if (is_array($arr))
            {
                $tmparr = $arr;
            }
            else
            {
                $tmparr = $this->_contents;
            }

            for($i=0; $i<count($tmparr); $i++)
            {
                if (is_a($tmparr[$i], __CLASS__))
                {
                    $classes .= $tmparr[$i]->emit_classdefs();
                    if (!$tmparr[$i]->is_ok())
                    {
                        $this->copy_error($tmparr[$i]);
                    }
                }
                elseif (is_array($tmparr[$i]))
                {
                    $classes .= $this->emit_classdefs($tmparr[$i]);
                }
                if (!$this->is_ok()) { break; }
            }

            if (!is_array($arr) && $this->is_ok())
            {
                foreach ($this->_classes as $name => $style)
                {
                    $classes .= $name.' {'.$this->_style_format_cvt($style)."}\n";
                }
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?look_r($classes):'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($classes);
        }

        // converts between the string and array representations
        // of style attribute values
        function _style_format_cvt($in)
        {
            if (is_array($in))
            {
                $out = '';
                foreach($in as $property => $value)
                {
                    $out .= $property.': '.$value.'; ';
                }
            }
            else
            {
                $out = array();
                $tmp = explode(';', $in);
                foreach($tmp as $element)
                {
                    $sep = explode(':', $element);
                    $out[$sep[0]] = $sep[1];
                }
            }
            return $out;
        }
		
		
        // the following four functions manage client-side scripts associated with 
        // this object and it's children.  Scripts can be managed at both class and 
        // instance scope
        function emit_class_scripts($arr=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);

            $str = $this->_class_scripts('emit', null, null, null, null);

            // TBD - emit the scripts of all contained objects
            if (is_array($arr))
            {
                $tmparr = $arr;
            }
            else
            {
                $tmparr = $this->_contents;
            }

            for($i=0; $i<count($tmparr); $i++)
            {
                if (is_a($tmparr[$i], __CLASS__))
                {
                    $str .= $tmparr[$i]->emit_class_scripts();
                    if (!$tmparr[$i]->is_ok())
                    {
                        $this->copy_error($tmparr[$i]);
                        // set_error?
                    }
                }
                elseif (is_array($tmparr[$i]))
                {
                    $str .= $this->emit_class_scripts($tmparr[$i]);
                }
                if (!$this->is_ok()) { break; }
            }

            $this->dbgecho($proc.' leaving', DEBUG_PROC_TRACE);
            return $this->is_ok($str, RETURN_NULL_OK);
        }
		
        function set_class_script($code, $scope, $instance_id=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$scope.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $this->_class_scripts('set', $code, $scope, (isset($instance_id)?$instance_id:$this->_instance));

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        function get_class_script($code, $scope, $instance_id=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$scope.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $str = $this->_class_scripts('get', $code, $scope, (isset($instance_id)?$instance_id:$this->_instance));

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str, RETURN_NULL_OK);
        }
		
        function _class_scripts($action, $code, $scope, $instance_id)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$action.', '.$scope.') entering', DEBUG_PROC_TRACE);

            static $htmlbase_class_script;
            static $htmlbase_instance_scripts;

            $str = '';
            if ($action == 'emit')
            {
                if (isset($htmlbase_class_script))
                {
                    $str .= $htmlbase_class_script."\n";
                }
                if (is_array($htmlbase_instance_scripts))
                {
                    // class level
                    foreach($htmlbase_instance_scripts as $inst_entry)
                    {
                        $str .= $inst_entry."\n";
                    }
                }
            }
            elseif ($action == 'set')
            {
                if ($scope == HTMLBASE_SCRIPT_SCOPE_CLASS)
                {
                    $htmlbase_class_script = $code;
                }
                elseif($scope == HTMLBASE_SCRIPT_SCOPE_INSTANCE)
                {
                    if (!is_array($htmlbase_instance_scripts))
                    {
                        $htmlbase_instance_scripts = array();
                    }
                    $htmlbase_instance_scripts[$instance_id] = $code;
                }
            }
            elseif ($action == 'get')
            {
                if ($scope == HTMLBASE_SCRIPT_SCOPE_CLASS)
                {
                    $str = $htmlbase_class_script;
                }
                elseif($scope == HTMLBASE_SCRIPT_SCOPE_INSTANCE)
                {
                    if (!is_array($htmlbase_instance_scripts))
                    {
                        $htmlbase_instance_scripts = array();
                    }
                    $str = $htmlbase_instance_scripts[$instance_id];
                }
            }
			
            $this->static_dbgecho($proc.' leaving', DEBUG_PROC_TRACE);
            return $str;
        }
		
        function set_event_handler($event, $code=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$event.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (isset($code))
            {
                $this->_event_handlers[$event] = $code;
            }
            else
            {
                unset($this->_event_handlers[$event]);
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        function get_event_handler($event)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$event.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($this->_event_handlers[$event], RETURN_NULL_OK);
        }
		
        // replaces or inserts contents in this HTML object
        //
        // if a value for pos is provided the contents passed in will be inserted 
        // at that position, with any following items moved back
        // if contents is an array, each item in the array will be inserted starting
        // at the index given in pos.
        //
        // in no pos value is specified, the passed contents will completely replace
        // any existing contents
        function set_contents(&$contents, $pos=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (!isset($pos))
            {
                $this->dbgecho($proc.'replacing entire contents', DEBUG_DETAIL_TRACE);
                // replace entire contents
                // first get rid of the old
                $this->_contents = array();
                // then indicate that new is appended
                $pos = 0;
            }

            $current_count = is_array($this->_contents)?count($this->_contents):0;
            if ($pos < 0)
            {
                $this->dbgecho($proc.'adjusting pos '.$pos.' up to 0', DEBUG_DETAIL_TRACE);
                // make sure pos is not less than zero
                $pos = 0;
            }
            elseif ($pos > $current_count)
            {
                $this->dbgecho($proc.'adjusting pos '.$pos.' down to '.$current_count, DEBUG_DETAIL_TRACE);
                // make sure pos is not greater than the number already have
                $pos = $current_count;
            }

            $this->dbgecho($proc.'inserting new contents at index '.$pos.' out of '.$current_count.' items', DEBUG_DETAIL_TRACE);
            // first move any trailing elements to make room
            $added_contents_count = (is_array($contents)?count($contents):1);
            for($i=$current_count-1; $i>=$pos; $i--)
            {
                $this->dbgecho($proc.'moving old contents at '.$i.' up to '.($i+$added_contents_count), DEBUG_DETAIL_TRACE);
                $this->_contents[$i+$added_contents_count] =& $this->_contents[$i];
            }

            // then insert the new contents
            if (is_array($contents))
            {
                for($i=0; $i<count($contents); $i++)
                {
                    $this->dbgecho($proc.'placing new contents at '.($pos+$i), DEBUG_DETAIL_TRACE);
                    $this->_contents[$pos+$i] =& $contents[$i];
                }
            }
            else
            {
                $this->dbgecho($proc.'placing new contents at '.($pos), DEBUG_DETAIL_TRACE);
                $this->_contents[$pos] =& $contents;
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        // same as set_contents, but does not pass references
        function set_contentsx($contents, $pos=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (is_array($contents))
            {
                $tmp = array();
                for($i=0; $i<count($contents); $i++)
                {
                    $tmp[$i] = $contents[$i];
                }
            }
            else
            {
                $tmp = $contents;
            }
            $this->set_contents($tmp, $pos);

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        // returns a reference to the contents in this HTML object
        //
        // returns a reference to the contents at array position specified by the 
        // pos parameter.  If no contents at that position, an error is generated
        // and false is returned
        //
        function & get_contents($pos)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$pos.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (!isset($this->_contents[$pos]))
            {
                $this->set_error('No contents exist at position '.$pos, __FILE__, __LINE__);
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            if (!$this->is_ok())
            {
                $res = false;
                return $res;
            }
            else
            {
                return $this->_contents[$pos];
            }
        }
		
        // removes contents in this HTML object
        //
        // if a value for pos is provided the contents at that position and the (n-1
        // following will be removed.
        //
        // if no pos value is specified, all contents will completely removed
        function clear_contents($pos=null, $num=1)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$pos.','.$num.') entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            if (isset($pos))
            {
                if (!is_array($this->_contents))
                {
                    $this->set_error('Unable to clear contents at position '.$pos.'. Contents not an array.', __FILE__, __LINE__);
                }
                elseif ($pos>=0 && $pos<count($this->_contents))
                {
                    array_splice($this->_contents, $pos, 1);
                }
            }
            else
            {
                $this->_contents = null;
            }

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
		
        function num_contents()
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $num = is_array($this->_contents)?count($this->_contents):0;

            $this->dbgecho($proc.' leaving - '.($this->is_ok()?$num:'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($num);
        }
    }
 ?>