<?php
  class formfield {
    var $name;
    var $label;
    var $type;

    function __construct($name, $label, $type, $value='', $options = null, $extraattributes = null) {
      $this->name = $name;
      $this->label = $label;
      $this->id;
      $this->type = $type;
      $this->value = $value;
      $this->options = $options;
      $this->extraattributes = $extraattributes;
      return $this;
    }

    function getHtml() {
        $this->id = $this->name;
        /** INPUT fields **/
        $html = '';
        if ($this->type == 'text' or $this->type == 'date') {
            if ($this->type=='date') {
                $size=10;
                $maxlength=10;
            } else {
                $size=32;
                $maxlength=64;
            }
            $html .= '<input type="text" name="' . $this->name .
                '" id="' . $this->id .
                '" value="' . $this->value .
                '" size="' . $size .
                '" maxlength="' . $maxlength . '" ' . 
                $this->extraattributes . ' />';
            if ($this->type == 'date') {
              $html .= '<script type="text/javascript">$(function() { $("#' . $this->name . '").datepicker();});</script>';
            }

        }


        /** SELECT **/
        if ($this->type == 'select') {
            $html .= '<select type="text" name="' . $this->name .
                '" id="' . $this->id . '" ' . $this->extraattributes .  ' />'; 
            foreach ($this->options as $option) {
              $value = $option['value'];
              $text = $option['text'];
              $html .= '<option value="' . $value . '" ' .
                  (($value==$this->value)?'selected="selected"':'') . '>' .
                  $text . '</option>';
            }
            $html .= '</select>';
        }

        /** RADIO **/
        if ($this->type == 'radio') {
            $value = $this->options['value'];
            $radiolabel = $this->options['name'];
            $index = str_replace('&','_',$value);
            $index = str_replace('/','_',$index);
            $index = str_replace(' ','_',$index);
            $this->id = $this->id .  $index;
            $html .= '<input type="radio" name="' . $this->name .
                '" id="' . $this->id . $this->index . '" ' . $this->extraattributes . 
                'value="' . $value . '" ';
            if ($value == $this->value && ! ($value == '')) {
                $html .= 'checked="checked"';
            }
            $html .=  ' />'; 
            $html .= '<label for="' . $this->id . '">' . $radiolabel . '</label>'; 
            $this->id = '';
        }


        /** TEXTAREA **/
        if ($this->type == 'textarea') {
            $html .= '<textarea name="' . $this->name . '" id="' . $this->name . '" ' . $this->extraattributes . ' >' . $this->value . '</textarea>';
        }

        /** SUBMIT **/
        if ($this->type == 'submit') {
            $html .= '<input type="submit" name="' .$this->name . '" value="' . $this->value . '" />';
        }

        $outputhtml = '<div class="field">';
        $outputhtml  .= '<label for="' . $this->id . '">' . $this->label . ($this->label?':':'&nbsp;') . '</label>';
        $outputhtml .= $html;
        $outputhtml .= '<div class="clear">&nbsp;</div></div>' . "\n";
        return $outputhtml;
    }

    static public function show($name, $label, $type, $value='', $options=null, $extraattributes=null) {
        $field = new formfield($name, $label, $type, $value,$options,$extraattributes);
        return $field->getHtml();
    }
  }
