<?php
// document head functions
function pageTitle($dom,$head,$title) {
   
$xmlTitle $dom->createElement('title',$title);
   
$head->appendChild($xmlTitle);
   }
   
function 
mkCss($dom,$stylesheet) {
   
$xmlStyle $dom->createElement('link');
   
$xmlStyle->setAttribute('href',$stylesheet);
   
$xmlStyle->setAttribute('type','text/css');
   
$xmlStyle->setAttribute('rel','stylesheet');
   return (
$xmlStyle);
   }
   
function 
addCss($dom,$head,$stylesheet) {
   
$node mkCss($dom,$stylesheet);
   
$head->appendChild($node);
   }
   
function 
addJs($dom,$head,$script) {
   
$xmlScript $dom->createElement('script');
   
$xmlScript->setAttribute('src',$script);
   
$xmlScript->setAttribute('type','text/javascript');
   
$head->appendChild($xmlScript);
   }
   
function 
addMeta($dom,$head,$name,$content) {
   
$meta $dom->createElement('meta');
   
$meta->setAttribute('name',$name);
   
$meta->setAttribute('content',$content);
   
$head->appendChild($meta);
   }
   
function 
addHTTPEquiv($dom,$head,$equiv,$content) {
   
$meta $dom->createElement('meta');
   
$meta->setAttribute('http-equiv',$equiv);
   
$meta->setAttribute('content',$content);
   
$head->appendChild($meta);
   }
   
// document style functions
function mkClearDiv($dom) {
   
$xmlDiv $dom->createElement('div');
   
$xmlDiv->setAttribute('class','cleardiv');
   return (
$xmlDiv);
   }
   
function 
addClearDiv($dom,$parent) {
   
$node mkClearDiv($dom);
   
$parent->appendChild($node);
   }
   
// general body functions
function mkImage($dom,$id,$src,$width,$height,$alt='',$class='',$style='') {
   
$xmlImage $dom->createElement('img');
   
$xmlImage->setAttribute('src',$src);
   if (
strlen($id) > 0) {
      
$xmlImage->setAttribute('id',$id);
      }
   if (
strlen($width) > 0) {
      
$xmlImage->setAttribute('width',$width);
      }
   if (
strlen($height) > 0) {
      
$xmlImage->setAttribute('height',$height);
      }
   if (
strlen($alt) > 0) {
      
$xmlImage->setAttribute('alt',$alt);
      } else {
      
$xmlImage->setAttribute('alt','[Image: ' basename($src) . ']');
      }
   if (
strlen($class) > 0) {
      
$xmlImage->setAttribute('class',$class);
      }
   if (
strlen($style) > 0) {
      
$xmlImage->setAttribute('style',$style);
      }
   return (
$xmlImage);
   }
   
function 
addImage($dom,$parent,$id,$src,$width,$height,$alt='',$class='',$style='') {
   
$node mkImage($dom,$id,$src,$width,$height,$alt,$class,$style);
   
$parent->appendChild($node);
   }

function 
mkLink($dom,$href,$string='',$title='') {
   if (
strlen($string) > 0) {
      
$xmlLink $dom->createElement('a',$string);
      } else {
      
$xmlLink $dom->createElement('a');
      }
   
$xmlLink->setAttribute('href',$href);
   if (
strlen($title) > 0) {
      
$xmlLink->setAttribute('title',$title);
      }
   return (
$xmlLink);
   }
   
function 
addLink($dom,$parent,$href,$string='',$title='') {
   
$node mkLink($dom,$href,$string,$title);
   
$parent->appendChild($node);
   }
   
function 
mkSpan($dom,$string,$class='',$style='') {
   
// for spanning text
   
$xmlSpan $dom->createElement('span',$string);
   if (
strlen($class) > 0) {
      
$xmlSpan->setAttribute('class',$class);
      }
   if (
strlen($style) > 0) {
      
$xmlSpan->setAttribute('style',$style);
      }
   return (
$xmlSpan);
   }
   
function 
addSpan($dom,$parent,$string,$class='',$style='') {
   
$node mkSpan($dom,$string,$class,$style);
   
$parent->appendChild($node);
   }
   
function 
mkTimeString($dom,$string,$posixTime) {
   
$xmlDate $dom->createElement('time',$string);
   
$xmlDate->setAttribute('datetime',date('c',$posixTime));
   return (
$xmlDate);
   }
   
function 
addTimeString($dom,$parent,$string,$posixTime) {
   
$node mkTimeString($dom,$string,$posixTime);
   
$parent->appendChild($node);
   }
   
function 
addStrong($dom,$parent,$string) {
   
$xmlStrong $dom->createElement('strong',$string);
   
$parent->appendChild($xmlStrong);
   }
   
function 
addEm($dom,$parent,$string) {
   
$xmlEmph $dom->createElement('em',$string);
   
$parent->appendChild($xmlEmph);
   }
   
function 
addText($dom,$parent,$string) {
   
$xmlText $dom->createTextNode($string);
   
$parent->appendChild($xmlText);
   }
   
function 
addBr($dom,$parent) {
   
$xmlBr $dom->createElement('br');
   
$parent->appendChild($xmlBr);
   }
   
function 
addHr($dom,$parent,$class='',$style='') {
   
$xmlHr $dom->createElement('hr');
   if (
strlen($class) > 0) {
      
$xmlHr->setAttribute('class',$class);
      }
   if (
strlen($style) > 0) {
      
$xmlHr->setAttribute('style',$style);
      }
   
$parent->appendChild($xmlHr);
   }
   
// content management
   
function addDefinition($dom,$defList,$dterm,$def) {
   
$defTerm $dom->createElement('dt',$dterm);
   
$defDef  $dom->createElement('dd',$def);
   
$defList->appendChild($defTerm);
   
$defList->appendChild($defDef);
   }
   
// form stuff
function addLegend($dom,$parent,$id,$string) {
   
$xmlLegend $dom->createElement('legend',$string);
   if (
strlen($id) > 0) {
      
$xmlLegend->setAttribute('id',$id);
      }
   
$parent->appendChild($xmlLegend);
   }
   
function 
mkFieldset($dom,$id,$legend='',$class='',$style='') {
   
$legendid='';
   
$xmlFieldset $dom->createElement('fieldset');
   if (
strlen($id) > 0) {
      
$xmlFieldset->setAttribute('id',$id);
      
$legendid='legend_' $id;
      }
   if (
strlen($class) > 0) {
      
$xmlFieldset->setAttribute('class',$class);
      }
   if (
strlen($style) > 0) {
      
$xmlFieldset->setAttribute('style',$style);
      }
   if (
strlen($legend) > 0) {
      
addLegend($dom,$xmlFieldset,$legendid,$legend);
      }
   return(
$xmlFieldset);
   }

function 
addLabel($dom,$parent,$id,$string,$for='',$class='',$style='') {
   
$xmlLabel $dom->createElement('label',$string);
   if (
strlen($id) > 0) {
      
$xmlLabel->setAttribute('id',$id);
      }
   if (
strlen($for) > 0) {
      
$xmlLabel->setAttribute('for',$for);
      }
   if (
strlen($class) > 0) {
      
$xmlLabel->setAttribute('class',$class);
      }
   if (
strlen($style) > 0) {
      
$xmlLabel->setAttribute('style',$style);
      }
   
$parent->appendChild($xmlLabel);
   }

function 
mkTextInput($dom,$id,$name,$size,$value='',$autocomplete=true) {
   
$xmlInput $dom->createElement('input');
   
$xmlInput->setAttribute('id',$id);
   
$xmlInput->setAttribute('name',$name);
   
$xmlInput->setAttribute('type','text');
   
$xmlInput->setAttribute('size',$size);
   if (! 
$autocomplete) {
      
$xmlInput->setAttribute('autocomplete','off');
      }
   if (
strlen($value) > 0) {
      
$xmlInput->setAttribute('value',$value);
      }
   return (
$xmlInput);
   }
   
function 
addTextInput($dom,$parent,$id,$name,$size,$value='',$autocomplete=true) {
   
$node mkTextInput($dom,$id,$name,$size,$value,$autocomplete);
   
$parent->appendChild($node);
   }
   
function 
mkPasswordInput($dom,$parent,$id,$name,$size,$autocomplete=true) {
   
$xmlInput $dom->createElement('input');
   
$xmlInput->setAttribute('id',$id);
   
$xmlInput->setAttribute('name',$name);
   
$xmlInput->setAttribute('type','password');
   
$xmlInput->setAttribute('size',$size);
   if (! 
$autocomplete) {
      
$xmlInput->setAttribute('autocomplete','off');
      }
   return (
$xmlInput);
   }
   
function 
addPasswordInput($dom,$parent,$id,$name,$size,$autocomplete=true) {
   
$node mkPasswordInput($dom,$id,$name,$size,$autocomplete);
   
$parent->appendChild($node);
   }
   
function 
mkCheckboxInput($dom,$id,$name,$value,$checked=false) {
   
$xmlInput $dom->createElement('input');
   
$xmlInput->setAttribute('id',$id);
   
$xmlInput->setAttribute('name',$name);
   
$xmlInput->setAttribute('type','checkbox');
   
$xmlInput->setAttribute('value',$value);
   if (
$checked) {
      
$xmlInput->setAttribute('checked','checked');
      }
   return (
$xmlInput);
   }
   
function 
addCheckboxInput($dom,$parent,$id,$name,$value,$checked=false) {
   
$node mkCheckboxInput($dom,$id,$name,$value,$checked);
   
$parent->appendChild($node);
   }
   
function 
mkRadioInput($dom,$parent,$id,$name,$value,$checked=false) {
   
$xmlInput $dom->createElement('input');
   if (
strlen($id) > 0) {
      
$xmlInput->setAttribute('id',$id);
      }
   
$xmlInput->setAttribute('name',$name);
   
$xmlInput->setAttribute('type','radio');
   
$xmlInput->setAttribute('value',$value);
   if (
$checked) {
      
$xmlInput->setAttribute('checked','checked');
      }
   return (
$xmlInput);
   }
   
function 
addRadioInput($dom,$parent,$id,$name,$value,$checked=false) {
   
$node mkRadioInput($dom,$id,$name,$value,$checked);
   
$parent->appendChild($node);
   }
   
function 
mkFileInput($dom,$id,$name,$class='',$style='') {
   
$xmlInput=$dom->createElement('input');
   
$xmlInput->setAttribute('type','file');
   
$xmlInput->setAttribute('id',$id);
   
$xmlInput->setAttribute('name',$name);
   if (
strlen($class) > 0) {
      
$xmlInput->setAttribute('class',$class);
      }
   if (
strlen($style) > 0) {
      
$xmlInput->setAttribute('style',$style);
      }
   return (
$xmlInput);
   }
   
function 
addFileInput($dom,$parent,$id,$name,$class='',$style='') {
   
$node mkFileInput($dom,$id,$name,$class,$style);
   
$parent->appendChild($node);
   }
   
function 
mkTextArea($dom,$id,$name,$value='',$rows='',$cols='',$class='',$style='') {
   
$xmlTextArea $dom->createElement("textarea",$value);
   
$xmlTextArea->setAttribute('id',$id);
   
$xmlTextArea->setAttribute('name',$name);
   if (
strlen($rows) > 0) {
      
$xmlTextArea->setAttribute('rows',$rows);
      }
   if (
strlen($cols) > 0) {
      
$xmlTextArea->setAttribute('cols',$cols);
      }
   if (
strlen($class) > 0) {
      
$xmlTextArea->setAttribute('class',$class);
      }
   if (
strlen($style) > 0) {
      
$xmlTextArea->setAttribute('style',$style);
      }
   return (
$xmlTextArea);
   }
   
function 
addTextArea($dom,$parent,$id,$name,$value='',$rows='',$cols='',$class='',$style='') {
   
$node mkTextArea($dom,$id,$name,$value,$rows,$cols,$class,$style);
   
$parent->appendChild($node);
   }
   
function 
addHiddenInput($dom,$parent,$id,$name,$value) {
   
$xmlInput $dom->createElement('input');
   if (
strlen($id) > 0) {
      
$xmlInput->setAttribute('id',$id);
      } else {
      
$xmlInput->setAttribute('id','hidinput_' $name);
      }
   
$xmlInput->setAttribute('name',$name);
   
$xmlInput->setAttribute('type','hidden');
   
$xmlInput->setAttribute('value',$value);
   
$parent->appendChild($xmlInput);
   }
   
function 
mkSubmit($dom,$id,$name,$value='Submit') {
   
$xmlInput $dom->createElement('input');
   if (
strlen($id) > 0) {
      
$xmlInput->setAttribute('id',$id);
      }
   if (
strlen($name) > 0) {
      
$xmlInput->setAttribute('name',$name);
      }
   
$xmlInput->setAttribute('type','submit');
   
$xmlInput->setAttribute('value',$value);
   return (
$xmlInput);
   }
   
function 
addSubmit($dom,$parent,$id,$name,$value='Submit') {
   
$node mkSubmit($dom,$id,$name,$value);
   
$parent->appendChild($node);
   }
   
// custom stuff
   
function embedPHP($dom,$parent,$source,$id,$objClass='',$divClass='') {
   
// useful for displaying phps code in it's own object window in a web page
   
$xmlObject $dom->createElement('object');
   
$xmlObject->setAttribute('type','text/html');
   
$xmlObject->setAttribute('data',$source);
   
$xmlObject->setAttribute('id',$id);
   if (
strlen($objClass) > 0) {
      
$xmlObject->setAttribute('class',$objClass);
      }
   
$xmlDiv $dom->createElement('div');
   if (
strlen($divClass) > 0) {
      
$xmlDiv->setAttribute('class',$divClass);
      }
   
$xmlPar $dom->createElement('p');
   
addLink($dom,$xmlPar,$source,$source);
   
$xmlDiv->appendChild($xmlPar);
   
$xmlObject->appendChild($xmlDiv);
   
$parent->appendChild($xmlObject);
   }
   
// syntax highlighting
@include_once('Text/Highlighter.php');
   
function 
codeString($dom,$code,$lang='TXT') {
   
$lang strtoupper($lang);
   
$THL_LANGS[] = Array();
   if (
class_exists('Text_Highlighter')) {
      
$THL_LANGS[] = 'ABAP';
      
$THL_LANGS[] = 'CPP';
      
$THL_LANGS[] = 'CSS';
      
$THL_LANGS[] = 'DIFF';
      
$THL_LANGS[] = 'DTD';
      
$THL_LANGS[] = 'HTML';
      
$THL_LANGS[] = 'JAVA';
      
$THL_LANGS[] = 'JAVASCRIPT';
      
$THL_LANGS[] = 'MYSQL';
      
$THL_LANGS[] = 'PERL';
      
$THL_LANGS[] = 'PHP';
      
$THL_LANGS[] = 'PYTHON';
      
$THL_LANGS[] = 'RUBY';
      
$THL_LANGS[] = 'SQL';
      
$THL_LANGS[] = 'XML';
      }
   if (
in_array($lang,$THL_LANGS)) {
      
$dom->formatOutput false;
      
$hl =& Text_Highlighter::factory($lang);
      
$out $hl->highlight($code);
      
$tmpDOM = new DOMDocument('1.0','UTF-8');
      
$tmpDOM->loadXML($out);
      
$divList $tmpDOM->getElementsByTagName('div');
      
$impDIV $divList->item(0);
      
$node $dom->importNode($impDIV,true);
      } else {
      
$node $dom->createElement('div');
      
$node->setAttribute('class','mono');
      
$pre $dom->createElement('pre',$code);
      
$node->appendChild($pre);
      }
   return(
$node);
   }
   
function 
codeBlock($dom,$parent,$myArray,$lang='TXT') {
   
//$myArray is a single dimension array containing strings to
   //  be represented on one line each
   
$string $myArray[0];
   for (
$i=1;$i<sizeof($myArray);$i++) {
      
$string .= "\n" $myArray[$i];
      }
   
$myNode codeString($dom,$string,$lang);
   
$parent->appendChild($myNode);
   }
   
// misc functions
function getNodeTextChild($dom,$node) {
   
// for a node that can only have one text child, this returns the child.
   //  otherwise it returns ''
   
$return='';
   if (
$node->hasChildNodes()) {
      
$n $node->childNodes->length;
      if (
$n == 1) {
         
$children=$node->childNodes;
         foreach (
$children as $child) {
            if (
$child->nodeType == XML_TEXT_NODE) {
               
$return $child->nodeValue;
               }
            }
         }
      }
   return 
$return;
   }
   
// useful class(es)
class navMenu {
   public 
$navPrefix '';
   
   public function 
navMenu($dom,$id,$html5nav=false) {
      
$this->dom $dom;
      if (
$html5nav) {
         
$this->container $this->dom->createElement('nav');
         } else {
         
$this->container $this->dom->createElement('div');
         }
      if (
strlen($id) > 0) {
         
$this->container->setAttribute('id',$id);
         }
      
$this->topList $this->dom->createElement('ul');
      
$this->container->appendChild($this->topList); 
      } 
// end navMenu function
      
   
public function addNavItem($parent,$catid,$anchorid,$item,$makeanchor=true) {
      
$catid $this->navPrefix $catid;
      if (
strlen($anchorid) > 0) {
         if (
$makeanchor) {
            
$href '#' $anchorid;
            
$xmlAnchor $this->dom->createElement('a','');
            
$xmlAnchor->setAttribute('id',$anchorid);
            
$parent->appendChild($xmlAnchor);
            
$anchorid '#' $anchorid;
            }
         
$node $this->dom->createElement('a',$item);
         
$node->setAttribute('href',$anchorid);
         } else {
         
$node $this->dom->createTextNode($item);
         }
      
// is category already populated ??
      // grrr - getElementById only works with $dom->validateOnParse = true;
      //        which we don't want, because DOMDocument may not know about
      //        our chosen DTD and it really slows things down
      
$myListList $this->topList->getElementsByTagName('li');
      for (
$i $myListList->length; --$i >= 0;) {
         
$listItem $myListList->item($i);
         if (
$listItem->hasAttribute('id')) {
            
$listID $listItem->getAttribute('id');
            if (
strcmp($listID,$catid) == 0) {
               
$categoryFound $listItem;
               }
            }
         }
      if (isset(
$categoryFound)) {
         
// does it have a sublist already?
         
$subListSearch $categoryFound->getElementsByTagName('ul');
         for (
$i $subListSearch->length; --$i >= 0;) {
            
$sublist $subListSearch->item(0);
            }
         if (! isset(
$sublist)) {
            
$sublist $this->dom->createElement('ul');
            
$categoryFound->appendChild($sublist);
            }
         
$listItem $this->dom->createElement('li');
         
$listItem->appendChild($node);
         
$sublist->appendChild($listItem);
         } else {
         
// make it         
         
$listItem $this->dom->createElement('li');
         
$listItem->setAttribute('id',$catid);
         
$listItem->appendChild($node);
         
$this->topList->appendChild($listItem);
         }        
      } 
// end addNavItem
      
   
public function submenuAddClass($catid,$class) {
      
// if you need to specify a particular class for a
      // submenu list, this does it
      
$catid $this->navPrefix $catid;
      
$nodelist $this->topList->getElementsByTagName('li');
      for (
$i $nodelist->length; --$i >= 0;) {
         
$listItem $nodelist->item($i);
         if (
$listItem->hasAttribute('id')) {
            
$listID $listItem->getAttribute('id');
            if (
strcmp($listID,$catid) == 0) {
               
$listList $listItem->getElementsByTagName('ul');
               
$nodeOfInterest $listList->item(0);
               
$nodeOfInterest->setAttribute('class',$class);
               }
            }
         }
      }
      
   public function 
returnMenuNode() {
      
// in case manipulation is desired that class doesn't do
      
return $this->container;
      }
      
   public function 
addMenu($parent) {
      
$parent->appendChild($this->container);
      } 
// end addMenu
   
}
   
?>