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

	class htmlhead extends htmlcontainer
	{
		var $title;
		
		var $_tag = 'head';
		var $_req_close_tag = true;
		
		function __construct($title=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			
			parent::__construct(null, $attrs);
			if ($this->is_ok())
			{
				$this->title = new htmltitle($title, $attrs); 
				if (!$this->title->is_ok())
				{
					$this->copy_error($this->title);
					$this->set_error('Unable to initialze title object', __FILE__, __LINE__);
				}
				else
				{
					$this->set_contents($this->title);
                    if ($this->is_ok() && isset($attrs['mobil_responsive']) && ($attrs['mobil_responsive']==true))
                    {
                        $this->set_contentsx('<meta name="viewport" content="width=device-width, initial-scale=1>', 1);
                    }
				}
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function & title($empty=null)
		{
			return $this->title;
		}
	}
	
	class htmlbody extends htmlcontainer
	{
//		var $_debug = DEBUG_DETAIL_TRACE;
		
		var $_background_img;
		var $_background_color;
		var $_background_prop;
		var $_text;
		var $_link;
		var $_vlink;
		
		var $_tag = 'body';
		var $_req_close_tag = true;
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			
			parent::__construct(null, $attrs);
			if ($this->is_ok())
			{
				if (isset($attrs['background'])) $this->background($attrs['background']);
				if (isset($attrs['bgcolor'])) $this->bgcolor($attrs['bgcolor']);
				if (isset($attrs['bgproperties'])) $this->bgproperties($attrs['bgproperties']);
				if (isset($attrs['text'])) $this->text($attrs['text']);
				if (isset($attrs['link'])) $this->link($attrs['link']);
				if (isset($attrs['vlink'])) $this->vlink($attrs['vlink']);
				
				$this->set_contents($contents);
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function background($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			if (isset($new))
			{
				$this->_background_img = $new;
			}
			return $this->_background_img;
		}
		
		function bgcolor($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			if (isset($new))
			{
				$this->_background_color = $new;
			}
			return $this->_background_color;
		}
		
		function bgproperties($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			if (isset($new))
			{
				$this->_background_prop = $new;
			}
			return $this->_background_prop;
		}
		
		function text($src=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			if (isset($new))
			{
				$this->_text = $new;
			}
			return $this->_text;
		}
		
		function link($src=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			if (isset($new))
			{
				$this->_link = $new;
			}
			return $this->_link;
		}
		
		function vlink($src=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			if (isset($new))
			{
				$this->_vlink = $new;
			}
			return $this->_vlink;
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attrs = (isset($this->_background_img)?' background="'.$this->_background_img.'"':'').
					 (isset($this->_background_color)?' bgcolor="'.$this->_background_color.'"':'').
					 (isset($this->_background_prop)?' bgproperties="'.$this->_background_prop.'"':'').
					 (isset($this->_text)?' text="'.$this->_text.'"':'').
					 (isset($this->_link)?' link="'.$this->_link.'"':'').
					 (isset($this->_vlink)?' vlink="'.$this->_vlink.'"':'');
			
			$this->dbgecho($proc.'attrs: '.$attrs, DEBUG_DETAIL_TRACE);
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags);
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
	}
	
	class htmltitle extends htmlcontainer
	{
		var $_tag = 'title';
		var $_req_close_tag = true;
	}
	
	class htmlstyle extends base
	{
		var $_type = 'text/css';
		var $_classes = array();
		
		function __construct($classes=null, $mode=DEFAULT_TRIGGER_ON_ERROR)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			
			parent::__construct($mode);
			if ($this->is_ok())
			{
				if (is_array($classes))
				{
					$this->_classes = $classes;
				}
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function set_class($name, $style)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$this->_classes[$name] = $style;
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		function clear_class($name)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'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_str()
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (count($this->_classes)>0)
			{
				$str = '<style TYPE="'.$this->_type.'">'."\n".
					   '<!-- '."\n";
				foreach($this->_classes as $name => $style)
				{
					$str .= $name.' {';
					foreach($style as $property => $value)
					{
						$str .= ' '.$property.': '.$value.';';
					}
					$str .= " }\n";
				}
				$str .= " -->\n".
						'</style>'."\n";
			}
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str, RETURN_NULL_OK);
		}
	}
	
	class htmlhtml extends htmlcontainer
	{
//		var $headers = array('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">');
		var $headers = array('<!DOCTYPE HTML>');
		var $head;
		var $body;
		
		var $_tag = 'html';
		var $_req_close_tag = true;
		
		function __construct($title='Untitled', $contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
                if (isset($attrs['header'])) { $this->header[] = $attrs['header']; }
				
				// check for existance of head and body parts
				for($i=0; $i<count($this->_contents); $i++)
				{
					if (is_object($this->_contents[$i]))
					{
						if (is_a($this->_contents[$i], 'htmlhead'))
						{
							if (!isset($this->head))
							{
								$this->head = & $this->_contents[$i];
							}
							else
							{
								$this->set_error('Duplicate head element found in html element', __FILE__, __LINE__);
							}
						}
						elseif (is_a($this->_contents[$i], 'htmlbody'))
						{
							if (!isset($this->body))
							{
								$this->body = & $this->_contents[$i];
							}
							else
							{
								$this->set_error('Duplicate body element found in html element', __FILE__, __LINE__);
							}
						}
					}
				}
				
				if ($this->is_ok())
				{
					// create head and body if non-existant
					if (!isset($this->head))
					{
						$this->head = new htmlhead($title, $attrs);
						if (!$this->head->is_ok())
						{
							$this->copy_error($this->head);
							$this->set_error('Unable to initialze head object', __FILE__, __LINE__);
						}
						else
						{
							$this->set_contents($this->head, $this->num_contents());
						}
					}
					if ($this->is_ok() && !isset($this->body))
					{
						$this->body = new htmlbody(null, $attrs);
						if (!$this->body->is_ok())
						{
							$this->copy_error($this->body);
							$this->set_error('Unable to initialze body object', __FILE__, __LINE__);
						}
						else
						{
							$this->set_contents($this->body, $this->num_contents());
						}
					}
				}
			}
			
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
//			return $this->is_ok();
		}
		
		function emit_str($tag_attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			// insert style into head component
			$classes = $this->emit_classdefs();
			if (strlen($classes)>0)
			{
				$classes = '<style TYPE="text/css">'."\n".'<!-- '."\n".$classes.' -->'."\n".'</style>'."\n";
				$this->head->set_contents($classes, $this->head->num_contents());
			}
			
			// insert global scripts into head component
            $classscripts = $this->body->emit_class_scripts();
			if (strlen($classscripts)>0)
			{
				$classes = '<script>'."\n".$classscripts."\n".'</script>'."\n";
				$this->head->set_contents($classes, $this->head->num_contents());
			}
			
			$str = implode("\n", $this->headers)."\n".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($tag_attrs=null, $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			echo $this->emit_str($tag_attrs, $flags);
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		// restricts inserted contents to text, head or body objects
		//
		function set_contents(&$contents, $pos=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$check = (is_array($contents)?$contents:array($contents));
			for($i=0; $i<count($check); $i++)
			{
				if (is_a($check[$i], 'htmlbase') && (!is_a($check[$i], 'htmlhead') && !is_a($check[$i], 'htmlbody')))
				{
					$this->set_error('Can\'t add '.get_class($check[$i]).' element as contents of html element.', __FILE__, __LINE__);
					break;
				}
			}
			if ($this->is_ok())
			{
				parent::set_contents($contents, $pos);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		function & header($new=null)
		{
			if (isset($new))
			{
				$this->headers = $new;
			}
			return $this->headers;
		}
		
		function & head()
		{
			return $this->head;
		}
		
		function & body()
		{
			return $this->body;
		}
	}
	
	class _htmlinline extends htmlcontainer
	{
		var $_tag;
		var $_req_close_tag = true;
		
		// restricts inserted contents to text, non-container or non-block objects
		//
		function set_contents(&$contents, $pos=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$check = (is_array($contents)?$contents:array($contents));
			for($i=0; $i<count($check); $i++)
			{
				if (is_a($check[$i], 'htmlcontainer') && is_a($check[$i], '_htmlblock'))
				{
					$this->set_error('Can\'t add '.get_class($check[$i]).' element as contents of inline type element.', __FILE__, __LINE__);
					break;
				}
			}
			if ($this->is_ok())
			{
				parent::set_contents($contents, $pos);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
	}
	
	class htmlspan extends _htmlinline
	{
		var $_tag = 'span';
		var $_req_close_tag = true;
	}
	
	class _htmlh extends _htmlinline
	{
		var $_align;
		
		var $_tag;
		var $_req_close_tag = true;
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
				if (isset($attrs['align'])) $this->align($attrs['align']);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attrs = (isset($this->_align)?' align="'.$this->_align.'"':'');
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags)."\n";
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function align($new=null)
		{
			if (isset($new))
			{
				$this->_align = $new;
			}
			return $this->_align;
		}
	}
	
	class htmlh1 extends _htmlh
	{
		var $_tag = 'h1';
	}
	
	class htmlh2 extends _htmlh
	{
		var $_tag = 'h2';
	}
	
	class htmlh3 extends _htmlh
	{
		var $_tag = 'h3';
	}
	
	class htmlh4 extends _htmlh
	{
		var $_tag = 'h4';
	}
	
	class htmlh5 extends _htmlh
	{
		var $_tag = 'h5';
	}
	
	class htmlh6 extends _htmlh
	{
		var $_tag = 'h6';
	}

    // this class defines labels for HTML form inputs.
    class htmllabel extends htmlspan
    {
        var $_for;                          // id of element this is a lable for
        var $_form;                         // id of form this lable is for
        var $_tag = 'label';                // HTML tag attribute.

        function __construct($desc=null, $ctrl=null, $attrs=null)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.(isset($attrs['id'])?$attrs['id']:'').') entering', DEBUG_PROC_TRACE);
            
            $ctrlobj = null;

            // desc parameter must be a string for the label
            if (!is_string($desc))
            {
                $this->set_error('Label description must be a string. Invalid type '.gettype($desc), _FILE__, __LINE__);
            }
            else
            {
                // ctrl parameter may either either:
                // 1) a single edititem object OR
                // 2) an array giving the id string of the labeled control at index 0 or 'for' and optionally
                //    the id string of the form object/tag itself at index 2 or 'form'
            
                if (is_a($ctrl, 'edititem'))
                {
                    $ctrlobj = $ctrl;
                }
                elseif (is_array($ctrl))
                {
                    if (isset($ctrl['for']))
                    {
                        $this->_for = $ctrl['for'];
                    }
                    else if (isset($ctrl[0]))
                    {
                        $this->_for = $ctrl[0];
                    }
                    if (isset($ctrl['form']))
                    {
                        $this->_form = $ctrl['form'];
                    }
                    else if (isset($ctrl[1]))
                    {
                        $this->_form = $ctrl[1];
                    }
                    if (isset($this->_for) && !is_string($this->_for))
                    {
                        $this->set_error('"for" attribute must be a string. Invalid type '.gettype($this->_for), _FILE__, __LINE__);
                    }
                    elseif (isset($this->_form) && !is_string($this->_form))
                    {
                        $this->set_error('"form" attribute must be a string. Invalid type '.gettype($this->_form), _FILE__, __LINE__);
                    }
                }
                else
                {
                    $this->set_error('"ctrl" paramater must be a edititem type object or an array of string(s). Invalid type '.gettype($ctrl), _FILE__, __LINE__);
                }
            }
            if ($this->is_ok())
            {
                $contents = array($desc);
                if (isset($ctrlobj)) { $contents[] = $ctrlobj; }
                if (isset($this->_for)) { $attrib['for'] = $this->_for; }
                if (isset($this->_form)) { $attrib['form'] = $this->_form; }
                
                $this->dbgecho($proc.'contents are '.look_r($contents), DEBUG_DETAIL_TRACE);
                parent::__construct($contents, $attrs);
            }
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            if (!$this->is_ok())
            {
                $this->trigger_error();
            }
        }

        function control($new=null)
        {
            if (isset($new))
            {
                if (!is_a($new, 'edititem'))
                {
                    $this->set_error('paramater must be a edititem type object. Invalid type '.gettype($new), _FILE__, __LINE__);
                }
                else
                {
                    // replace any imbeded control
                    $this->clear_contents(1, 1);
                    parent::set_contents($new, 1);
                }
            }
            return ($this->get_contents(1));
        }
        
        function for($new=null)
        {
            if (isset($new))
            {
                $this->_for = $new;
                // remove any contained edit control
                $this->clear_contents();
            }
            return $this->_for;
        }

        function form($new=null)
        {
            if (isset($new))
            {
                $this->_form = $new;
                // remove any contained edit control
                $this->clear_contents();
            }
            return $this->_form;
        }
        
        function emit_str($tag_attrs=null, $flags=0)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$this->name().') entering', DEBUG_PROC_TRACE);
            $this->clear_error();
            
            $attrs = (isset($this->_for)?' for="'.$this->_for.'"':'').
                     (isset($this->_form)?' form="'.$this->_form.'"':'').$tag_attrs;
            
            $str = parent::emit_str($attrs, $flags)."\n";

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

	class _htmlblock extends htmlcontainer
	{
		var $_align;
		var $_height;
		var $_width;
                var $_title;
		
		var $_tag;
		var $_req_close_tag = true;
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
				if (isset($attrs['align'])) $this->align($attrs['align']);
				if (isset($attrs['height'])) $this->height($attrs['height']);
				if (isset($attrs['width'])) $this->width($attrs['width']);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
//			return $this->is_ok();
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attrs = (isset($this->_align)?' align="'.$this->_align.'"':'').
					 (isset($this->_height)?' height="'.urlencode($this->_height).'"':'').
					 (isset($this->_width)?' width="'.urlencode($this->_width).'"':'');
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags)."\n";
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function align($new=null)
		{
			if (isset($new))
			{
				$this->_align = $new;
			}
			return $this->_align;
		}
		
		function height($new=null)
		{
			if (isset($new))
			{
				$this->_height = $new;
			}
			return $this->_height;
		}
		
		function width($new=null)
		{
			if (isset($new))
			{
				$this->_width = $new;
			}
			return $this->_width;
		}
		
		function title($new=null)
		{
			if (isset($new))
			{
				$this->_title = $new;
			}
			return $this->_title;
		}
	}
	
	class htmldiv extends _htmlblock
	{
		var $_tag = 'div';
	}
	
	class htmlcenter extends _htmlblock
	{
		var $_tag = 'center';
	}
	
	class htmlp extends _htmlblock
	{
		var $_tag = 'p';
	}
	
	class htmlul extends _htmlblock
	{
		var $_tag = 'ul';
	}
	
	class htmlol extends _htmlblock
	{
		var $_tag = 'ol';
	}
	
	class htmlli extends _htmlblock
	{
		var $_tag = 'li';
	}
	
	class htmltable extends _htmlblock
	{
		var $_border;
		var $_background_img;
		var $_background_color;
		var $_cell_padding;
		var $_cell_spacing;
		var $_cols;
		var $_vspace;
		var $_hspace;
		
		var $_tag = 'table';
		
		function __construct($contents, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['name'])?$attrs['name']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
				if (isset($attrs['border'])) $this->border($attrs['border']);
				if (isset($attrs['background'])) $this->background($attrs['background']);
				if (isset($attrs['bgcolor'])) $this->bgcolor($attrs['bgcolor']);
				if (isset($attrs['cellpadding'])) $this->cellpadding($attrs['cellpadding']);
				if (isset($attrs['cellspacing'])) $this->cellspacing($attrs['cellspacing']);
				if (isset($attrs['cols'])) $this->cols($attrs['cols']);
				if (isset($attrs['vspace'])) $this->vspace($attrs['vspace']);
				if (isset($attrs['hspace'])) $this->hspace($attrs['hspace']);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
//			return $this->is_ok();
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attrs = (isset($this->_border)?' border="'.$this->_border.'"':'').
					 (isset($this->_background_img)?' background="'.urlencode($this->_background_img).'"':'').
					 (isset($this->_background_color)?' bgcolor="'.urlencode($this->_background_color).'"':'').
					 (isset($this->_cell_padding)?' cellpadding="'.urlencode($this->_cell_padding).'"':'').
					 (isset($this->_cell_spacing)?' cellspacing="'.urlencode($this->_cell_spacing).'"':'').
					 (isset($this->_cols)?' cols="'.urlencode($this->_cols).'"':'').
					 (isset($this->_vspace)?' vspace="'.urlencode($this->_vspace).'"':'').
					 (isset($this->_hspace)?' hspace="'.urlencode($this->_hspace).'"':'');
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags)."\n";
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		// restricts inserted contents to text or htmlrow objects
		//
		function set_contents(&$contents, $pos=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (is_array($contents))
			{
				for($i=0; $i<count($contents); $i++)
				{
					if (is_a($contents[$i], 'htmlbase') && !is_a($contents[$i], 'htmlrow'))
					{
						$this->set_error('Can\'t add non-row element as contents of table element.'.$pos, __FILE__, __LINE__);
					}
				}
			}
			else
			{
				if (is_a($contents, 'htmlbase') && !is_a($contents, 'htmlrow'))
				{
					$this->set_error('Can\'t add non-row element as contents of table element.'.$pos, __FILE__, __LINE__);
				}
			}
			if ($this->is_ok())
			{
				parent::set_contents($contents, $pos);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		function border($new=null)
		{
			if (isset($new))
			{
				$this->_border = $new;
			}
			return $this->_border;
		}
		
		function background($new=null)
		{
			if (isset($new))
			{
				$this->_background_img = $new;
			}
			return $this->_background_img;
		}
		
		function bgcolor($new=null)
		{
			if (isset($new))
			{
				$this->_background_color = $new;
			}
			return $this->_background_color;
		}
		
		function cellpadding($new=null)
		{
			if (isset($new))
			{
				$this->_cell_padding = $new;
			}
			return $this->_cell_padding;
		}
		
		function cellspacing($new=null)
		{
			if (isset($new))
			{
				$this->_cell_spacing = $new;
			}
			return $this->_cell_spacing;
		}
		
		function cols($new=null)
		{
			if (isset($new))
			{
				$this->_cols = $new;
			}
			return $this->_cols;
		}
		
		function vspace($new=null)
		{
			if (isset($new))
			{
				$this->_vspace = $new;
			}
			return $this->_vspace;
		}
		
		function hspace($new=null)
		{
			if (isset($new))
			{
				$this->_hspace = $new;
			}
			return $this->_hspace;
		}
	}
	
	class htmlrow extends _htmlblock
	{
		var $_background_img;
		var $_background_color;
		var $_valign;
		
		var $_tag = 'tr';
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['name'])?$attrs['name']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
				if (isset($attrs['background'])) $this->background($attrs['background']);
				if (isset($attrs['bgcolor'])) $this->bgcolor($attrs['bgcolor']);
				if (isset($attrs['valign'])) $this->valign($attrs['valign']);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
//			return $this->is_ok();
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attrs = (isset($this->_background_img)?' background="'.urlencode($this->_background_img).'"':'').
					 (isset($this->_background_color)?' bgcolor="'.urlencode($this->_background_color).'"':'').
					 (isset($this->_valign)?' valign="'.urlencode($this->_valign).'"':'');
			unset($this->_width); // no width for rows
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags);
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		// restricts inserted contents to text or htmltabhead or htmltabdata objects
		//
		function set_contents(&$contents, $pos=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$this->_name.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (is_array($contents))
			{
				for($i=0; $i<count($contents); $i++)
				{
					if (is_a($contents[$i], 'htmlbase') && (!is_a($contents[$i], 'htmltabhead') && !is_a($contents[$i], 'htmltabdata')))
					{
						$this->set_error('Can\'t add non-table data or head element ['.(get_class($contents[$i])).'] as contents of row element.'.$pos, __FILE__, __LINE__);
					}
				}
			}
			else
			{
				if (is_a($contents, 'htmlbase') && (!is_a($contents, 'htmltabhead') && !is_a($contents, 'htmltabdata')))
				{
					$this->set_error('Can\'t add non-table data or head element ['.(get_class($contents)).'] as contents of row element.'.$pos, __FILE__, __LINE__);
				}
			}
			if ($this->is_ok())
			{
				parent::set_contents($contents, $pos);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
		
		function background($new=null)
		{
			if (isset($new))
			{
				$this->_background_img = $new;
			}
			return $this->_background_img;
		}
		
		function bgcolor($new=null)
		{
			if (isset($new))
			{
				$this->_background_color = $new;
			}
			return $this->_background_color;
		}
		
		function valign($new=null)
		{
			if (isset($new))
			{
				$this->_valign = $new;
			}
			return $this->_valign;
		}
	}
	
	class htmltabdata extends _htmlblock
	{
		var $_background_img;
		var $_background_color;
		var $_valign;
		var $_colspan;
		var $_rowspan;
		var $_nowrap;
		var $_char;
		
		var $_tag = 'td';
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['name'])?$attrs['name']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
				if (isset($attrs['background'])) $this->background($attrs['background']);
				if (isset($attrs['bgcolor'])) $this->bgcolor($attrs['bgcolor']);
				if (isset($attrs['valign'])) $this->valign($attrs['valign']);
				if (isset($attrs['colspan'])) $this->colspan($attrs['colspan']);
				if (isset($attrs['rowspan'])) $this->rowspan($attrs['rowspan']);
				if (isset($attrs['nowrap'])) $this->nowrap($attrs['nowrap']);
				if (isset($attrs['char'])) $this->char($attrs['char']);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
//			return $this->is_ok();
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			$attrs = (isset($this->_background_img)?' background="'.$this->_background_img.'"':'').
					 (isset($this->_background_color)?' bgcolor="'.$this->_background_color.'"':'').
					 (isset($this->_valign)?' valign="'.urlencode($this->_valign).'"':'').
					 (isset($this->_colspan)?' colspan="'.urlencode($this->_colspan).'"':'').
					 (isset($this->_rowspan)?' rowspan="'.urlencode($this->_rowspan).'"':'').
					 ((isset($this->_nowrap) && $this->_nowrap)?' nowrap':'').
					 (isset($this->_char)?' char="'.$this->_char.'"':'');
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags);
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function background($new=null)
		{
			if (isset($new))
			{
				$this->_background_img = $new;
			}
			return $this->_background_img;
		}
		
		function bgcolor($new=null)
		{
			if (isset($new))
			{
				$this->_background_color = $new;
			}
			return $this->_background_color;
		}
		
		function valign($new=null)
		{
			if (isset($new))
			{
				$this->_valign = $new;
			}
			return $this->_valign;
		}
		
		function colspan($new=null)
		{
			if (isset($new))
			{
				$this->_colspan = $new;
			}
			return $this->_colspan;
		}
		
		function rowspan($new=null)
		{
			if (isset($new))
			{
				$this->_rowspan = $new;
			}
			return $this->_rowspan;
		}
		
		function nowrap($new=null)
		{
			if (isset($new))
			{
				$this->_nowrap = (boolean)$new;
			}
			return (boolean)$this->_nowrap;
		}
		
		function char($new=null)
		{
			if (isset($new))
			{
				$this->_char = $new;
			}
			return $this->_char;
		}
	}
	
	class htmltabhead extends htmltabdata
	{
		var $_tag = 'th';
	}
	
	class htmlcaption extends htmlrow
	{
		var $_tag = 'caption';
	}
	
	class htmlanchor extends htmlcontainer
	{
		var $_tag = 'a';
		var $_req_close_tag = true;
		
		var $_href;
		var $_target;
		var $_rel;
		
		function __construct($contents=null, $attrs=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.(isset($attrs['name'])?$attrs['name']:'').') entering', DEBUG_PROC_TRACE);
			
			parent::__construct($contents, $attrs);
			if ($this->is_ok())
			{
				if (isset($attrs['href'])) $this->href($attrs['href']);
				if (isset($attrs['target'])) $this->target($attrs['target']);
				if (isset($attrs['rel'])) $this->rel($attrs['rel']);
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			if (!$this->is_ok())
			{
				$this->trigger_error();
			}
//			return $this->is_ok();
		}
		
		function emit_str($tag_attrs='', $flags=0)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
//			$attrs = (isset($this->_href)?' href="'.urlencode($this->_href).'"':'').
			$attrs = (isset($this->_href)?' href="'.html_obfusticate($this->_href).'"':'').
					 (isset($this->_target)?' target="'.$this->_target.'"':'').
					 (isset($this->_rel)?' rel="'.$this->_rel.'"':'');
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags);
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($str);
		}
		
		function href($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->_href = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->_href, RETURN_NULL_OK);
		}
		
		function target($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->_target = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->_target, RETURN_NULL_OK);
		}
		
		function rel($new=null)
		{
			$proc = __CLASS__.'::'.__FUNCTION__.': ';
			$this->dbgecho($proc.'('.$new.') entering', DEBUG_PROC_TRACE);
			$this->clear_error();
			
			if (isset($new))
			{
				$this->_rel = $new;
			}
			
			$this->dbgecho($proc.' leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok($this->_rel, RETURN_NULL_OK);
		}
	}
	
	class htmlnav extends _htmlblock
	{
		var $_tag = 'nav';
	}
	
	class htmlheader extends _htmlblock
	{
		var $_tag = 'header';
	}
	
	class htmlfooter extends _htmlblock
	{
		var $_tag = 'footer';
	}
        
	class htmlbr extends htmlbase
	{
		var $_tag = 'br';
		var $_req_close_tag = false;
	}
	
	class htmlhr extends htmlbase
	{
		var $_tag = 'hr';
		var $_req_close_tag = false;
	}
	
	class apppage extends htmlhtml
	{
		var $urlpath;
	
		// NON-static function to launch this app page
		// this function emits redirect headers and the appropriate 
		// parameters for launching this app page the the browser window
		// a possible extension is to lanuch the the app page in some other
		// frame or window - TBD
		function launch($param=null, $frame=null)
		{
			$proc=__CLASS__.'::'.__FUNCTION__.' ';
			$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
			
			$appurl = 'http'.($_SERVER['HTTPS']=='on'?'s':'').'://'.
							$_SERVER['HTTP_HOST'].
							(((($_SERVER['HTTPS']=='on') && ($_SERVER['SERVER_PORT']=='443')) ||
							  (($_SERVER['HTTPS']!='on') && ($_SERVER['SERVER_PORT']=='80')))?'':(':'.$_SERVER['SERVER_PORT'])).
							get_basedir().
							$this->urlpath;
			if (is_array($param))
			{
				$query = '';
				foreach($param as $key => $value)
				{
					if (strlen($query)>0) $query .= '&';
					$query .= urlencode($key).'='.urlencode($value);
				}
				if (strlen($query)>0) $appurl .= '?'.$query;
			}
			session_write_close();
			header('Location: '.$appurl);
			
			$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
			return $this->is_ok();
		}
	}
 ?>