Location: PHPKode > scripts > ntSimpleForm > ntsimpleform/ntSimpleForm.class.php
<?
/**
 * ntSimpleForm: An all-in-one solution for form creation and processing.
 *
 * The all-in-one form creation/processing solution for php.  Please note that the purpose of this class is to create
 * forms of simple nature (no files, no multiple selection boxes, etc) that performs simple actions once submitted
 * and validated.  All validation and actions are also done by this class.
 *
 * @change - v1.0.2
 *		- added support for multiple checkbox options
 *		- fixed a bug with the return of completeText when a properly validated form is submitted
 * @change - v1.0.1
 * 		- added getHTML() function to return HTML content of form
 * 		- revised code for toString() to print content returned from getHTML()
 * @author  Roberto Piazza - NiA Tech
 * @version 1.0.1
 */

if (strcmp(phpversion(), "4.1.0") > 0)
    define("GLOBAL_SUPPORT", true);        
else
    define("GLOBAL_SUPPORT", false);
 
class ntSimpleForm 
{
    
    /**
     * Collection of actions for a form to carry out once submitted and validated.
     *
     * @var   [ARRAY]
     * @since 1.0.0
     */
    var $actions;
    
    /**
     * The text to be displayed to the user once the form has been submitted and validated.
     *
     * @var   STRING
     * @since 1.0.0
     */
    var $completeText;

    /**
     * The URL where the user will be redirected after the form has been submitted and validated.
     *
     * @var   STRING
     * @since 1.0.0
     */
    var $completeURL;

    /**
     * The list of errors reported during validation.
     *
     * @var   [STRING]
     * @since 1.0.0
     */
    var $errors;
    
    /**
     * Collection of fields an ntSimpleForm may contain.
     *
     * @var   [ARRAY]
     * @since 1.0.0
     */
    var $fields;
    
    /**
     * The title of the form.
     *
     * @var   STRING
     * @since 1.0.0
     */
    var $title;

    /**
     * Options for selection boxes, radio buttons and check boxes.
     *
     * @var   STRING
     * @since 1.0.0
     */
    var $options;
    
    /**
     * Boolean to tell if form has been submitted.
     *
     * @var   BOOLEAN
     * @since 1.0.0
     */
    var $beenSubmitted;
    
    /**
     * Submitted form data.
     *
     * @var   [ARRAY]
     * @since 1.0.0
     */
    var $data;

    
    /**
     * Create a new ntSimpleForm object and set the instance variables accordingly.
     *
     * @author  Roberto Piazza
     * @return  ntSimpleForm
     * @since   1.0.0
     * @version 1.0.0
     */
    function ntSimpleForm($title='')
    {
        
        if (! is_string($title))
        {
            print ("<b>ntSimpleForm Error(ntSimpleForm()):</b> Invalid first argument, expecting string.\n");
            return false;
        }

        $this->actions = NULL;
        $this->completeText = "Your form has been submitted.<br><br>Thank you.";
        $this->completeURL = "";
        $this->errors = NULL;
        $this->fields = NULL;
        $this->title = $title;
        $this->options = NULL;
        $this->beenSubmitted = false;
    }

    /**
     * Adds an error the ntSimpleForm object.  This is a private method only to be used by the form object.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $newError  The new error message to be added to the errors array
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function _addError($newError)
    {
        $success = true;
    
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(_addError()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($newError))
        {
            print ("<b>ntSimpleForm Error(_addError()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->errors[] = $newError;
        }
        
        return $success;
    }
    
    /**
     * When the form is submitted this method is called by the form to perform the validation and actions for the form.
     *
     * @author  Roberto Piazza
     * @access  private
     * @param   [ARRAY] $formData  The data submitted by the form to be validated.
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function _validate()
    {
        $success = true;
        
        if (! isset($this->data['inputForm']))
        {
            print ("<b>ntSimpleForm Error(_validate()):</b> Internal ntSimpleForm submission data error, please report the bug ".
                    "to hide@address.com\n");
            $success = false;
        }
        else
        {
            $data = $this->data['inputForm'];
            
            if (is_array($data['required']))
            {
                foreach ($data['required'] as $reqKey => $reqValue)
                {
                    if (empty($data[$reqValue]) || $data[$reqValue] == -1)
                    {
                        $this->errors[] = 'The "'.$this->fields[$reqValue]['label'].'" field must be filled out.';
                        $success = false;
                    }
                }
            }
            
            if (is_array($data['typeChecking']))
            {
                foreach ($data['typeChecking'] as $typeKey => $typeValue)
                {
                    switch ($typeValue)
                    {
                        case 'alpha':
                            preg_match_all("/[0-9]/", $data[$typeKey], $matches);
                            if (! empty($matches[0]))
                            {
                                $this->errors[] = 'The "'.$this->fields[$typeKey]['label'].'" field can contain '.
                                                  'letters only.';                                            
                                $success = false;
                            }
                            break;
                        case 'numeric':
                            preg_match_all("/[A-z]/", $data[$typeKey], $matches);
                            if (! empty($matches[0]))
                            {
                                $this->errors[] = 'The "'.$this->fields[$typeKey]['label'].'" field can contain '.
                                                  'numbers only.';                                            
                                $success = false;
                            }
                            break;
                    }
                }
            
            if (is_array($data['email']))
            {
                foreach ($data['email'] as $emailKey => $emailValue)
                {
                    if (! eregi("^[_\.0-9a-z-]+@([0-9a-z][-0-9a-z\.]+)\.([a-z]{2,3}$)", $data[$emailValue], $check)) 
                    { 
                        $this->errors[] = 'The have entered an invalid email address for the '.
                                          '"'.$this->fields[$emailValue]['label'].'" field.';                                            
                        $success = false;
                    } 
                }
            }
			
            if (is_array($actions = unserialize(str_replace('~', '"', $data['actions']))) && $success)
                {
                    foreach ($actions as $actionKey => $actionValue)
                    {
                        switch($actionValue['action'])
                        {
                            case 'emailSender':
                                if (! $success = $this->_doEmailSender($actionValue['param1'], $actionValue['param2'], 
                                           $actionValue['param3']))
                                    print ("<b>ntSimpleForm Error(_validate()):</b> Error during emailSender action.\n");
                                break;
                            case 'emailRecipient':
                                if (! $success = $this->_doEmailRecipient($actionValue['param1'], $actionValue['param2'],
                                           $actionValue['param3']))
                                    print ("<b>ntSimpleForm Error(_validate()):</b> Error during emailRecipient action.\n");                                           
                                break;
                            case 'writeToFile':
                                $success = $this->_doWriteToFile($actionValue['param1'], $actionValue['param2']);                            
                                break;
                            default:
                                print ("<b>ntSimpleForm Error(_validate()):</b> Invalid action '".$actionValue['action'].
                                       "', expecting string.\n");
                                $success = false;
                                break;
                        }
                    }
                }
            }            
        }   
        
        return $success;
    }
    

    /**
     * Sends an email to the sender of the form.
     *
     * This will only work if you have atleast one addEmail() call in the creation of the form.  
     *
     * @author  Roberto Piazza
     * @access  private
     * @param   STRING $emailSubject  The subject of the email
     * @param   STRING $emailText  The text to be sent in the email
     * @param   STRING $emailFrom  The from address to be displayed in the email
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function _doEmailSender($emailSubject, $emailText, $emailFrom)
    {
        $success = true;
        
        if (! isset($this->data['inputForm']))
        {
            print ("<b>ntSimpleForm Error(_doEmailSender()):</b> Internal ntSimpleForm submission data error, please report the bug ".
                    "to hide@address.com\n");
            $success = false;
        }
        else
        {
            $data = $this->data['inputForm'];

            if (is_array($data['email']))
            {
                foreach($data['email'] as $emailKey => $emailValue)
                {
                    $success = mail($data[$emailValue], $emailSubject, str_replace('<br>', "\n", $emailText), "From: ". 
                                    $emailFrom . "\n");
                }
            }
        }   
        
        return $success;     
    }
    
    /**
     * Sends an email to a recipient displaying the form information.
     *
	 * @change - v1.0.1
	 *		- added support for multiple checkboxes
     * @author  Roberto Piazza
     * @access  private
     * @param   STRING $recipientAddress  The address to send the data to
     * @param   STRING $emailSubject  The subject of the email
     * @param   STRING $emailText  The text to be sent in the email
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.1
     */
    function _doEmailRecipient($recipientAddress, $emailSubject, $emailText)
    {
        $success = true;
        
        if (! isset($this->data['inputForm']))
        {
            print ("<b>ntSimpleForm Error(_doEmailRecipient()):</b> Internal ntSimpleForm submission data error, please report the bug ".
                    "to hide@address.com\n");
            $success = false;
        }
        else
        {
            $data = $this->data['inputForm'];

            if (is_array($data['email']))
            {
                $fromAddress = $data[$data['email'][0]];
            }

            if (empty($fromAddress)) $fromAddress = "hide@address.com";
            
            $mailBody = $emailText;
            
            foreach($data as $dataKey => $dataValue)
            {
                if (! stristr($dataKey, 'email') && ! stristr($dataKey, 'typeChecking') && 
                    ! stristr($dataKey, 'action') && ! stristr($dataKey, 'required'))
                {
                    if (! strcmp($this->fields[$dataKey]['type'], 'checkbox') && is_array($dataValue))
					{
						$returnValue = '';
						foreach ($dataValue as $option)
						{
							if (strlen($returnValue) > 0) $returnValue .= ', ';
                        	$returnValue .= $option;
						}
						$dataValue = $returnValue;
					}
                        
                    if (! strcmp($this->fields[$dataKey]['type'], 'date'))
                        $dataValue = $dataValue['Month'] . ' ' . $dataValue['Day'] . ', ' . $dataValue['Year'];

                    $mailBody .= $this->fields[$dataKey]['label'] . ': ' . $dataValue . "\n";
                }
            }
            
            $success = mail($recipientAddress, $emailSubject, str_replace('<br>', "\n", $mailBody), "From: ". 
                            $fromAddress . "\n");
        }   
        
        return $success;     
    }   
    
    /**
     * Writes the form data to a flat file.
     *
     * @author  Roberto Piazza
     * @access  private
     * @param   STRING $fileName  The full path and filename to the file
     * @param   STRING $deliminator  The deliminator used in the file
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function _doWriteToFile($fileName, $deliminator)
    {
        $success = true;
        
        if (! isset($this->data['inputForm']))
        {
            print ("<b>ntSimpleForm Error(_doEmailRecipient()):</b> Internal ntSimpleForm submission data error, please report the bug ".
                    "to hide@address.com\n");
            $success = false;
        }
        else
        {
            $data = $this->data['inputForm'];
            
            if ($fp = fopen($fileName, "a+"))
            {
                $theStr = "";
                foreach($this->fields as $fieldKey => $fieldValue)
                {
                    if (strcmp($fieldValue['type'], 'submit') && strcmp($fieldValue['type'], 'reset'))
                    {
                        if (! strcmp($fieldValue['type'], 'checkbox') && strlen($data[$fieldKey]) == 0)
                            $data[$fieldKey] = 'Not Checked';
                            
                        if (! strcmp($fieldValue['type'], 'date'))
                            $data[$fieldKey] = $data[$fieldKey]['Month'] . ' ' . $data[$fieldKey]['Day'] . ', '.
                                               $data[$fieldKey]['Year'];
                        $theStr .= $data[$fieldKey] . $deliminator;
                    }
                }
                
                fwrite($fp, $theStr . "\n");
                fclose($fp);
            }
            else
            {
                print ("<b>ntSimpleForm Error(_doWriteToFile()):</b> Error opening file '".$fileName."'.<br>\n");
                $success = false;
            }    
            
        }
                
        return $success;     
    }     

    /**
     * Add an action to the ntSimpleForm object.  These actions will be executed once the form has been submitted and 
     * validated.  The actions that are currently available are:
     *
     * - emailSender - sends an email to the user submitting the form.  This action is only carried out of an 'Email' field has been added to the form.
     *   - The parameter for this action is:
     *       - emailSubject - the subject to be displayed in the email
     *       - emailText - the text to be sent in the email.
     *       - emailFrom - the from address to be displayed in the email
     *    
     * - emailRecipient - sends an email to some recipient of the form data.
     *   - The parameters for this action are:
     *       - recipientAddress - email address of the recipient(s)
     *       - emailSubject - the subject to be displayed in the email
     *       - emailText - the text to be displayed at the top of the form.
     *
     * - writeToFile - writes the form data to some form of delimited flat file.
     *   - The parameters for this action are:
     *       - fileName - the full pathname and filename to the file ot be updated.
     *       - deliminator - the string used to deliminate columns.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $newAction  The action to be added (emailSender, emailRecipient, or writeToFile)
     * @param   STRING $param1  Parameter one for the action
     * @param   STRING $param2  Parameter two for the action
     * @param   STRING $param3  Parameter three for the action
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addAction($newAction, $param1, $param2='', $param3='')
    {
        $success = true;
        
        $availableActions = array('emailSender', 'emailRecipient', 'writeToFile');
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($newAction))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! in_array($newAction, $availableActions))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid action '".$newAction."' is not supported.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'emailSender') && ! is_string($param1))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid second argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'emailSender') && ! is_string($param2))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid third argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'emailSender') && ! is_string($param3))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid fourth argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'emailRecipient') && ! is_string($param1))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid second argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'emailRecipient') && ! is_string($param2))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid third argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'emailRecipient') && ! is_string($param3))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid fourth argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'writeToFile') && ! is_string($param1))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid second argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (! strcmp($newAction, 'writeToFile') && ! is_string($param2))
        {
            print ("<b>ntSimpleForm Error(addAction()):</b> Invalid second argument for action '".$newAction."', ".
                   "expecting string.\n");
            $success = false;
        }
        
        if (! strcmp($newAction, 'writeToFile') && strlen($param2) == 0)
        {
            $param2 = '|';
        }

        if (! strcmp($newAction, 'emailRecipient') && strlen($param2) == 0)
        {
            $param2 = 'Form Submission';
        }
        
        if (! strcmp($newAction, 'emailRecipient') && strlen($param3) == 0)
        {
            $param3 = "A form has been submitted to you, here is the information:<br><br>";
        }

                
        if ($success)
        {
            $this->actions[] = array('action' => $newAction, 'param1' => $param1, 'param2' => $param2,
                                     'param3' => $param3);
        }
        
        return $success;
        
        
        
    }

    /**
     * Adds a text field to the ntSimpleForm object.  
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the text box
     * @param   STRING $label  The label to be displayed in front of the text box
     * @param   STRING $default  The default value in the text box
     * @param   STRING $typeChecking  The kind of type checking to use for the text box.  The types currently supported are:
     *                                  - alpha  
     *                                  - numeric
     * @param   INT $cols  The width of the text box
     * @param   INT $maxLength  The maximum length of data to be inputted into the text field
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addTextField($name, $label='', $default='', $required=false, $typeChecking='', $cols=32, $maxLength=0)
    {
        $success = true;
        
        $availableCheckings = array('alpha', 'numeric');

        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($default))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid third argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid fourth argument, expecting boolean.\n");
            $success = false;
        }
        else if (! is_string($typeChecking))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid fifth argument '".$typeChecking."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (is_string($typeChecking) && strcmp($typeChecking, "") && ! in_array($typeChecking, $availableCheckings))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Type checking '".$typeChecking."', ".
                   "is not supported.\n");
            $success = false;
        }
        else if (! is_int($cols))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid sixth argument, expecting integer.\n");
            $success = false;
        }
        else if (! is_int($maxLength))
        {
            print ("<b>ntSimpleForm Error(addTextField()):</b> Invalid seventh argument, expecting integer.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'text', 
                                          'label' => $label, 
                                          'default' => $default, 
                                          'required' => $required,
                                          'typeChecking' => $typeChecking, 
                                          'cols' => $cols, 
                                          'maxLength' => $maxLength);
        }
        
        return $success;
    }
    
    /**
     * Adds a text area field to the ntSimpleForm object.  
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the text box
     * @param   STRING $label  The label to be displayed in front of the text box
     * @param   STRING $default  The default value in the text box
     * @param   STRING $typeChecking  The kind of type checking to use for the text box.  The types currently supported are:
     *                                  - alpha  
     *                                  - numeric
     * @param   INT $cols  The width of the text box
     * @param   INT $rows  The height of the text box
     * @param   INT $maxLength  The maximum length of data to be inputted into the text field
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addTextArea($name, $label='', $default='', $required=false, $typeChecking='', $cols=32, $rows=4, $maxLength=0)
    {
        $success = true;
        
        $availableCheckings = array('alpha', 'numeric');

        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($default))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid third argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid fourth argument, expecting boolean.\n");
            $success = false;
        }
        else if (! is_string($typeChecking))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid fifth argument '".$typeChecking."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (is_string($typeChecking) && strcmp($typeChecking, "") && ! in_array($typeChecking, $availableCheckings))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Type checking '".$typeChecking."', ".
                   "is not supported.\n");
            $success = false;
        }
        else if (! is_int($cols))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid sixth argument, expecting integer.\n");
            $success = false;
        }
        else if (! is_int($rows))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid seventh argument, expecting integer.\n");
            $success = false;
        }
        else if (! is_int($maxLength))
        {
            print ("<b>ntSimpleForm Error(addTextArea()):</b> Invalid eighth argument, expecting integer.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'textarea', 
                                              'label' => $label, 
                                              'default' => $default, 
                                              'required' => $required,
                                              'typeChecking' => $typeChecking, 
                                              'cols' => $cols, 
                                              'rows' => $rows, 
                                              'maxLength' => $maxLength);
        }
        
        return $success;
    }    
    
    /**
     * Adds a checkbox field to the ntSimpleForm object.
     *
	 * @change - v1.0.1
	 *		- added support for checkbox options (multiple options)
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the checkbox
     * @param   STRING $label  The label to be displayed in front of the checkbox
     * @param   BOOLEAN $default  The default selection for the checkbox
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.1
     */
    function addCheckbox($name, $label='', $required=false, $default=false)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid third argument, expecting boolean.\n");
            $success = false;
        }
        else if (! is_bool($default))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid fourth argument, expecting boolean.\n");
            $success = false;
        }    
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'checkbox', 
                                              'label' => $label, 
                                              'required' => $required,
                                              'default' => $default);
        }
        
        return $success;
    }
    
	/**
	 * Adds an option to a checkbox object.
	 *
	 * @author Roberto Piazza
	 * @access public
	 * @param STRING $name  The name of the checkbox to add the option to
	 * @param STRING $value  The value of the option
	 * @param STRING $label  The label to be displayed next to the checkbox
	 * @return BOOLEAN
	 * @since 1.0.1
	 * @version 1.0.0
	 */
	function addCheckboxOption($name, $value, $label='')
	{
		$success = true;
		if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
		else if (! is_string($value))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addCheckbox()):</b> Invalid third argument, expecting string.\n");
            $success = false;
        }
		
		if ($success)
		{
			$this->options[] = array('selectName' => $name,
                                        'optionText' => $label, 
                                        'optionValue' => $value);
		}
		return $success;
	}
	
    /**
     * Adds a radio button field to the ntSimpleForm object.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the radio button
     * @param   STRING $value  The value of the radio button
     * @param   STRING $label  The label to be displayed in front of the radio button
     * @param   STRING $question  The label to be displayed in front of all the radio buttons with the same name
     * @param   BOOLEAN $default  The default selection for the radio button
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addRadioButton($name, $optionLabel='', $optionValue='', $label='', $required=false, $default=false)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addRadioButton()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addRadioButton()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($optionLabel))
        {
            print ("<b>ntSimpleForm Error(addRadioButton()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($optionValue))
        {
            print ("<b>ntSimpleForm Error(addRadioButton()):</b> Invalid third argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addRadioButton()):</b> Invalid fourth argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addRadioButton()):</b> Invalid fifth argument, expecting boolean.\n");
            $success = false;
        }
        else if (! is_bool($default))
        {
            print ("<b>ntSimpleForm Error(addRadioButton()):</b> Invalid sixth argument, expecting boolean.\n");
            $success = false;
        }    
        
        if ($success)
        {
            if (! isset($this->fields[$name]))
                $this->fields[$name] = array('type' => 'radio', 
                                             'required' => $required,
                                             'label' => $label);
            else if (strlen($label) > 0)
                $this->fields[$name] = array('type' => 'radio', 
                                             'required' => $required,
                                             'label' => $label);

            $this->options[] = array('selectName' => $name,
                                     'radioValue' => $optionValue,
                                     'radioLabel' => $optionLabel,
                                     'default' => $default);
        }
        
        return $success;    
    }    
    
    /**
     * Adds a drop-down selection box to the ntSimpleForm object.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the selection box
     * @param   STRING $label  The text to be displayed in front of the selection box
     * @param   BOOLEAN $default  The default invalid selection
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addSelect($name, $label='', $required=false)
    {
      
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addSelect()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addSelect()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addSelect()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addSelect()):</b> Invalid third argument, expecting boolean.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'select', 
                                            'label' => $label, 
                                            'required' => $required);
        }
        
        return $success; 
    }        
        
    /**
     * Adds an option to a selection box.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $selectName  The name of the selection box to add the option to
     * @param   STRING $optionText  The text displayed for the option
     * @parma   STRING $optionValue  The value used for the option (if this is not set then the option text will be used)
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addOption($selectName, $optionText='', $optionValue='')
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addOption()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($selectName))
        {
            print ("<b>ntSimpleForm Error(addOption()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! array_key_exists($selectName, $this->fields))
        {
            print ("<b>ntSimpleForm Error(addOption()):</b> Invalid select box name '".$selectName."' doesn't exist.\n");
            $success = false;
        }
        else if (! is_string($optionText))
        {
            print ("<b>ntSimpleForm Error(addOption()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($optionValue))
        {
            print ("<b>ntSimpleForm Error(addOption()):</b> Invalid third argument, expecting string.\n");
            $success = false;
        }    
        
        if ($success)
        {
            $this->options[] = array('selectName' => $selectName,
                                        'optionText' => $optionText, 
                                        'optionValue' => $optionValue);
        }
        
        return $success;     
    }  
    
    /**
     * Adds an invalid option to a selection box. (similar to addOption but puts a -1 in the value field)
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $selectName  The name of the selection box to add the option to
     * @param   STRING $optionText  The text displayed for the option
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addInvalid($selectName, $optionText='')
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addInvalid()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($selectName))
        {
            print ("<b>ntSimpleForm Error(addInvalid()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! array_key_exists($selectName, $this->fields))
        {
            print ("<b>ntSimpleForm Error(addInvalid()):</b> Invalid select box name '".$selectName."' doesn't exist.\n");
            $success = false;
        }
        else if (! is_string($optionText))
        {
            print ("<b>ntSimpleForm Error(addInvalid()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->options[] = array('selectName' => $selectName,
                                        'optionText' => $optionText, 
                                        'optionValue' => '-1');
        }
        
        return $success;     
    }           
    
    /**
     * Ads a date selection field to the form.
     *
	 * @change - v1.0.1
	 *		- added the functionality of start and end years
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the date field.
     * @param   STRING $label  The text to be displayed in front of the date field
     * @param   INT $useDate  The date to be used for the date field (in mktime() format, if this is not set then todays date will be used)
	 * @param 	INT $startYear  The lowest year to display in the years select box
	 * @param   INT $endYear  The highest year to display in the years select box
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.1
     */
    function addDate($name, $label='', $required=false, $useDate=0, $startYear=0, $endYear=0)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addDate()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addDate()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addDate()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addDate()):</b> Invalid third argument, expecting boolean.\n");
            $success = false;
        }
        else if (! is_int($useDate))
        {
            print ("<b>ntSimpleForm Error(addDate()):</b> Invalid fourth argument, expecting integer.\n");
            $success = false;
        }

        if ($success)
        {
            if ($useDate == 0) $useDate = mktime();
            $this->fields[$name] = array('type' => 'date', 
                                          'label' => $label, 
                                          'required' => $required,
                                          'useDate' => $useDate,
										  'startYear' => $startYear,
										  'endYear' => $endYear);
        }
        
        return $success;        
    }    
    
    /**
     * Adds a password field to the ntSimpleForm object.  
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the password box
     * @param   STRING $label  The label to be displayed in front of the password box
     * @param   STRING $typeChecking  The kind of type checking to use for the password box.  The types currently supported are:
     *                                  - alpha  
     *                                  - numeric
     * @param   INT $cols  The width of the password box
     * @param   INT $maxLength  The maximum length of data to be inputted into the password field
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addPassword($name, $label='', $required=false, $typeChecking='', $cols=32, $maxLength=0)
    {
        $success = true;
        
        $availableCheckings = array('alpha', 'numeric');

        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Invalid third argument, expecting boolean.\n");
            $success = false;
        }
        else if (! is_string($typeChecking))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Invalid fourth argument '".$typeChecking."', ".
                   "expecting string.\n");
            $success = false;
        }
        else if (is_string($typeChecking) && strcmp($typeChecking, "") && ! in_array($typeChecking, $availableCheckings))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Type checking '".$typeChecking."', ".
                   "is not supported.\n");
            $success = false;
        }
        else if (! is_int($cols))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Invalid fifth argument, expecting integer.\n");
            $success = false;
        }
        else if (! is_int($maxLength))
        {
            print ("<b>ntSimpleForm Error(addPassword()):</b> Invalid sixth argument, expecting integer.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'password', 
                                              'label' => $label,
                                              'required' => $required, 
                                              'typeChecking' => $typeChecking, 
                                              'cols' => $cols, 
                                              'maxLength' => $maxLength);
        }
        
        return $success;    
    }     
    
    /**
     * Adds a submit button to the ntSimpleForm object.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the submit button
     * @param   STRING $caption  The caption displayed on the submit button (if this is not set then the default is 'Submit')
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addSubmit($name, $caption='- Submit -')
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addSubmit()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addSubmit()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($caption))
        {
            print ("<b>ntSimpleForm Error(addSubmit()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'submit', 
                                            'caption' => $caption);
        }
        
        return $success;     
    }     
    
    /**
     * Adds a reset button to the ntSimpleForm object.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the reset button
     * @param   STRING $caption  The caption displayed on the reset button (if this is not set then the default is 'Clear')
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addReset($name, $caption='- Clear -')
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addReset()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addReset()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($caption))
        {
            print ("<b>ntSimpleForm Error(addReset()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'reset', 
                                           'caption' => $caption);
        }
        
        return $success;     
    }      
    
    /**
     * Adds a hidden form field to the form.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the hidden field
     * @param   STRING $value  The value of the hidden field
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addHidden($name, $value)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addHidden()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addHidden()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($value))
        {
            print ("<b>ntSimpleForm Error(addHidden()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'hidden', 
                                            'value' => $value);
        }
        
        return $success;     
    }    
    
    /**
     * Adds a blank field to the form.  This is useful for adding spaces (only when the toString method is used).
     *
     * @author  Roberto Piazza
     * @access  public
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addBlank()
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addBlank()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[] = array('type' => 'blank');
        }
        
        return $success;     
    }  
    
    /**
     * Adds a email field to the ntSimpleForm object.  
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the email box
     * @param   STRING $label  The label to be displayed in front of the email box
     * @param   STRING $default  The default value in the text box
     * @param   INT $cols  The width of the email box
     * @param   INT $maxLength  The maximum length of data to be inputted into the email field
     * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function addEmailField($name, $label='', $required=false, $default='', $cols=32, $maxLength=0)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(addEmailField()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($name))
        {
            print ("<b>ntSimpleForm Error(addEmailField()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        else if (! is_string($label))
        {
            print ("<b>ntSimpleForm Error(addEmailField()):</b> Invalid second argument, expecting string.\n");
            $success = false;
        }
        else if (! is_bool($required))
        {
            print ("<b>ntSimpleForm Error(addEmailField()):</b> Invalid third argument, expecting boolean.\n");
            $success = false;
        }
        else if (! is_string($default))
        {
            print ("<b>ntSimpleForm Error(addEmailField()):</b> Invalid fourth argument, expecting string.\n");
            $success = false;
        }
        else if (! is_int($cols))
        {
            print ("<b>ntSimpleForm Error(addEmailField()):</b> Invalid fifth argument, expecting integer.\n");
            $success = false;
        }
        else if (! is_int($maxLength))
        {
            print ("<b>ntSimpleForm Error(addEmailField()):</b> Invalid sixth argument, expecting integer.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->fields[$name] = array('type' => 'email', 
                                           'label' => $label, 
                                           'required' => $required,
                                           'default' => $default, 
                                           'cols' => $cols, 
                                           'maxLength' => $maxLength);
        }
        
        return $success;    
    }     
    
    /**
     * Sets the text to be displayed once a form has been submitted and validated.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $completeText  The text to be displayed.     
	 * @return  BOOLEAN
     * @since   1.0.0
     * @version 1.0.0
     */
    function setCompleteText($completeText)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(setCompleteText()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($completeText))
        {
            print ("<b>ntSimpleForm Error(setCompleteText()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->completeText = $completeText;
        }
        
        return $success;     
    }    
    
    /**
     * Sets the URL to redirect the user to once the form has been submitted and validated.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $completeURL  The url to redirect to.
     * @since   1.0.0
     * @version 1.0.0
     */
    function setCompleteURL($completeURL)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(setCompleteURL()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($completeURL))
        {
            print ("<b>ntSimpleForm Error(setCompleteURL()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->completeURL = $completeURL;
        }
        
        return $success;    
    }    
    
    /**
     * Sets the title of the form.  This text is only displayed (at the top of the form) when the toString() method is used.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $newTitle  The text to be displayed at the top of the form.
     * @since   1.0.0
     * @version 1.0.0
     */
    function setTitle($newTitle)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(setTitle()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }
        else if (! is_string($newTitle))
        {
            print ("<b>ntSimpleForm Error(setTitle()):</b> Invalid first argument, expecting string.\n");
            $success = false;
        }
        
        if ($success)
        {
            $this->title = $newTitle;
        }
        
        return $success;    
    }    
    
    /**
     * Returns a string representation of a field identified by the 'name' parameter.
     *
	 * @change - v1.0.2
	 *		- added support for multiple checkboxes
	 * @change - v1.0.1
	 * 		- added support for start and end year for addDate()
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The name of the field to return
     * @return  STRING
     * @since   1.0.0
     * @version 1.0.2
     */
    function returnField($name)
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(returnField()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }    
        else if (! isset($this->fields[$name]))
        {
            print ("<b>ntSimpleForm Error(returnField()):</b> The form element '".$name."' does not exist.\n");
            $success = false;
        }
        else
        {
            switch($this->fields[$name]['type'])
            {
                case 'text':
                case 'email':
                    $success = '<input class="ntSimpleFormField" type="'.$this->fields[$name]['type'].'" name="inputForm['.$name.']" ';
                    
                    if (! $this->beenSubmitted)
                        $success .= 'value="'.str_replace('"', "'", stripslashes($this->fields[$name]['default']));
                    else
                        $success .= 'value="'.str_replace('"', "'", stripslashes($this->data['inputForm'][$name]));
                    
                    $success .= '" size="'.$this->fields[$name]['cols'].'"';
                                 
                    if ($this->fields[$name]['maxLength']) $success .= ' max="'.$this->fields[$name]['maxLength'].'"';
                    
                    $success .= '>';
                    break;
                case 'textarea':
                    $success = '<textarea class="ntSimpleFormField" name="inputForm['.$name.']" '.
                                 'cols="'.$this->fields[$name]['cols'].'"'.
                                 'rows="'.$this->fields[$name]['rows'];

                    if ($this->fields[$name]['maxLength']) $success .= ' max="'.$this->fields[$name]['maxLength'].'"';

                    $success .= '">';
                    
                    if (! $this->beenSubmitted)
                        $success .= str_replace('"', "'", stripslashes($this->fields[$name]['default']));
                    else
                        $success .= str_replace('"', "'", stripslashes($this->data['inputForm'][$name]));

                    $success .= '</textarea>';
                    break;
                case 'checkbox':
                    if (is_array($this->options))
                    {
						$success = '';
                        foreach ($this->options as $optionKey => $optionValue)
                        {
                            if (! strcmp($optionValue['selectName'], $name))
							{
                                $success .= '<input type="'.$this->fields[$name]['type'].'" class="ntSimpleFormField" name="inputForm['.$name.'][]" value="'.
                                            str_replace('"', "'", stripslashes($optionValue['optionValue'])).'"';

								if (is_array($this->data['inputForm'][$name]))
								{
	                                if ($this->beenSubmitted && in_array($optionValue['optionValue'], $this->data['inputForm'][$name]))
    	                                $success .= ' checked';
								}
                                    
                                $success .= '>'.$optionValue['optionText']."<br>\n";
							}
                        }
                    }
                    break;
                    
                case 'radio':
                    if (is_array($this->options))
                    {
                        $success = '';
                        foreach ($this->options as $optionKey => $optionValue)
                        {
                            if (! strcmp($optionValue['selectName'], $name))
                            {
                                $success .= '<input class="ntSimpleFormField" type="'.$this->fields[$name]['type'].'" name="inputForm['.$name.']" value="'.
                                             $optionValue['radioValue'].'"';
                                             
                                if (($optionValue['default'] && ! $this->beenSubmitted) || ($this->beenSubmitted && 
                                    ! strcmp($this->data['inputForm'][$name], $optionValue['radioValue']))) 
                                        $success .= ' checked';
                                $success .= '> ' . $optionValue['radioLabel'];
                            }
                        }
                    }
                    break;                                    
                case 'select':
                    $success = '<select class="ntSimpleFormField" name="inputForm['.$name.']">'."\n";
                    
                    if (is_array($this->options))
                    {
                        foreach ($this->options as $optionKey => $optionValue)
                        {
                            if (! strcmp($optionValue['selectName'], $name))
                                $success .= '<option class="ntSimpleFormField" value="'.
                                            str_replace('"', "'", stripslashes($optionValue['optionValue'])).'"';
                                
                                if ($this->beenSubmitted && ! strcmp($this->data['inputForm'][$name], $optionValue['optionValue']))
                                    $success .= ' selected';
                                    
                                $success .= '>'.$optionValue['optionText']."</option>\n";
                        }
                    }
                    $success .= "</select>\n";
                    break;
                case 'password':
                    $success = '<input class="ntSimpleFormField" type="password" name="inputForm['.$name.']" '.
                                 'size="'.$this->fields[$name]['cols'].'"';
                                 
                    if ($this->fields[$name]['maxLength']) $success .= ' max="'.$this->fields[$name]['maxLength'].'"';
                    
                    $success .= '>';
                    break;
                case 'hidden':
                    $success = '<input type="'.$this->fields[$name]['type'].'" name="inputForm['.$name.']" value="'.
                                 str_replace('"', "'", stripslashes($this->fields[$name]['value'])).'">';
                    break;
                    
                case 'submit':
                case 'reset':
                    $success = '<input class="ntSimpleFormButton" type="'.$this->fields[$name]['type'].'" name="'.$this->fields[$name]['type'].
                               '" value="'.$this->fields[$name]['caption'].'">';
                    break;
                case 'date':
                    if (! $this->beenSubmitted)
                        $useDate = $this->fields[$name]['useDate'];
                    else
                    {
                        $monthArray = array('January' => '1',
                                            'February' => '2',
                                            'March' => '3',
                                            'April' => '4',
                                            'May' => '5',
                                            'June' => '6',
                                            'July' => '7',
                                            'August' => '8',
                                            'September' => '9',
                                            'October' => '10',
                                            'November' => '11',
                                            'December' => '12');
                    
                    }
                                                
                    $useMonth = $monthArray[$this->data['inputForm'][$name]['Month']];
                    $useDay = $this->data['inputForm'][$name]['Day'];
                    $useYear = $this->data['inputForm'][$name]['Year'];
                    
                    $success = '<select class="ntSimpleFormField" name="inputForm['.$name.'][Month]">'."\n";
                    
                    // create month selection box
                    for ($i=1; $i<13; $i++)
                    {
                        $val = date('F',mktime(0,0,0,$i,1,2002));
                        $success .= '<option value="'.$val.'"';
                        if ($useMonth == $i) $success .= ' selected';
                        $success .= '>'.$val.'</option>'."\n";
                    }
                    $success .= '</select>'."\n";

                    $success .= '<select class="ntSimpleFormField" name="inputForm['.$name.'][Day]">'."\n";                    
                    // create day selection box
                    for ($i=1; $i<32; $i++)
                    {
                        $success .= '<option value="'.$i.'"';
                        if ($useDay == $i) $success .= ' selected';
                        $success .= '>'.$i.'</option>'."\n";
                    }
                    $success .= '</select>'."\n";
                    
                    $success .= '<select class="ntSimpleFormField" name="inputForm['.$name.'][Year]">'."\n";                    
                    // create year selection box
                    if ($this->fields[$name]['startYear'] != 0) $startY = $this->fields[$name]['startYear'];
						else $startY = (date('Y', $this->fields[$name]['useDate'])-3);
					if ($this->fields[$name]['endYear'] != 0) $endY = $this->fields[$name]['endYear'];
						else $endY = (date('Y', $this->fields[$name]['useDate'])+3);
					for ($i=$startY; $i<$endY; $i++)
                    {
                        $success .= '<option value="'.$i.'"';
                        if ($useYear == $i) $success .= ' selected';
                        $success .= '>'.$i.'</option>'."\n";
                    }       
                    
                    $success .= '</select>' . "\n";         
                    break;
                default:
                    print ("<b>ntSimpleForm Error(returnField()):</b> The form type '".$this->fields[$name]['type'].
                           "' does not exist.\n");
                    $success = false;
                    break;                
                    
            }      
            
            if (! strcmp($this->fields[$name]['type'], 'email'))
                $success .= "\n".'<input type="hidden" name="inputForm[email][]" value="'.$name.'">';                  
            
            if ($this->fields[$name]['typeChecking'] != "")
                $success .= "\n".'<input type="hidden" name="inputForm[typeChecking]['.$name.']" value="'.
                                $this->fields[$name]['typeChecking'].'">';      
                                
            if ($this->fields[$name]['required'])
                $success .= "\n".'<input type="hidden" name="inputForm[required][]" value="'.$name.'">';                                      
        }
        
        return $success;
    }    
    
    /**
     * Prints out a string representation of a field identified by the 'name' parameter.
     *
     * @author  Roberto Piazza
     * @access  public
     * @param   STRING $name  The field to display
     * @return  STRING
     * @since   1.0.0
     * @version 1.0.0
     */
    function displayField($name)
    {
        $returnVar = $this->returnField($name);
        
        if (! is_bool($returnVar)) $returnVar .= "\n";        
        print $returnVar;
    }     
    
    /**
     * Retuns an string of errors caused by the validation method of a form.
     *
     * @author  Roberto Piazza
     * @access  public
     * @return  STRING
     * @since   1.0.0
     * @version 1.0.0
     */
    function returnErrors()
    {
        $success = true;
        
        if (is_null($this->title))
        {
            print ("<b>ntSimpleForm Error(returnField()):</b> Invalid ntSimpleForm object, make sure you have instantiated the ".
                   "object properly.\n");
            $success = false;
        }    
        else if (is_array($this->errors))
        {
            $success = '<p class="ntSimpleFormErrors">'."\n";
            
            foreach ($this->errors as $errorKey => $errorValue)
            {
                $success .= $errorValue . "<br>\n";
            }
            $success .= "</p>\n";
        }    
        
        return $success;
    }    
    
    /**
     * Prints out the errors generated by the validate method.
     *
     * @author  Roberto Piazza
     * @access  public
     * @return  STRING
     * @since   1.0.0
     * @version 1.0.0
     */
    function displayErrors()
    {
        $returnVar = $this->returnErrors();
        
        if (! is_bool($returnVar)) print $returnVar;
    }   
    
	/**
     * Returns a string representation of the form and its elements (render the form in HTML).
     *
	 * @change - v1.0.1
	 *		- fixed bug for returning completeText to the user
     * @author  Roberto Piazza
     * @access  public
     * @return  STRING
     * @since   1.0.1
     * @version 1.0.1
     */
    function getHTML()
    {
		$returnVar = "";
        if (GLOBAL_SUPPORT)
        {
            if (! empty($_POST))
            {
                $this->beenSubmitted = true;
                $this->data = $_POST;
                if ($this->_validate())
				{
					
					if (strlen($this->completeURL)==0)
					{
						$returnVar .= $this->completeText;
						return $returnVar;
					}
					else
					{
						print '<script language="JavaScript">' . "\n";
						print " window.location='" . $this->completeURL . "';\n";
						print '</script>' . "\n";
						return $returnVar;
					}
				}
				
            }
        }
        else
        {
            global $HTTP_POST_VARS;
            if (! empty($HTTP_POST_VARS))
            {
                $this->beenSubmitted = true;
                $this->data = $HTTP_POST_VARS;            
                if ($this->_validate())
				{
					if (strlen($this->completeURL)==0)
					{
						$returnVar .= $this->completeText;
						return $returnVar;
					}
					else
					{
						$returnVar .= '<script language="JavaScript">' . "\n";
						$returnVar .= " window.location='" . $this->completeURL . "';\n";
						$returnVar .= '</script>' . "\n";
						return $returnVar;
					}
				}
            }
        }

		if ($this->returnErrors() != '1') {
			$returnVar .= $this->returnErrors();    
		}
    
        $returnVar .= '<form name="ntSimpleForm" method="post" action="';
        
        if (GLOBAL_SUPPORT)     
            $returnVar .= $_SERVER['SCRIPT_NAME'] . "?" . $_SERVER['QUERY_STRING'];
        else
        {
            global $HTTP_SERVER_VARS;
            $returnVar .= $HTTP_SERVER_VARS['SCRIPT_NAME'] . "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
        }
        
        $returnVar .= '" enctype="multipart/form-data">' . "\n";
        
        if (isset($this->actions))
        {
            $returnVar .= '<input type="hidden" name="inputForm[actions]" value="'.
                  str_replace('"', '~', serialize($this->actions)).'">' . "\n";        
        }
        
        $returnVar .= '<table border="0" cellspacing="1" cellpadding="1" class="ntSimpleFormTable">'. "\n";
        $returnVar .= '<tr>'. "\n";        
        $returnVar .= '<td class="ntSimpleFormTableHeader" colspan="2">'. "\n";
        $returnVar .= $this->title . "\n";
        $returnVar .= '</td>' . "\n";
        $returnVar .= '</tr>' . "\n";
        
        $buttons = NULL;
        
        foreach ($this->fields as $fieldKey => $fieldValue)
        {
            if (strcmp($fieldValue['type'], 'submit') && strcmp($fieldValue['type'], 'reset'))
            {
                $returnVar .= '<tr>' . "\n";
                $returnVar .= '<td class="ntSimpleFormTableField">' . "\n";
                $returnVar .= $fieldValue['label'] . "\n";
                $returnVar .= '</td>' . "\n";
                $returnVar .= '<td class="ntSimpleFormTableField">' . "\n";
                $returnVar .= $this->returnField($fieldKey);
                $returnVar .= '</td>' . "\n";
                $returnVar .='</tr>' . "\n";
            }
            else
            {
                $buttons[] = $fieldKey;
            }
        }
        
        if (is_array($buttons))
        {
            $returnVar .= '<tr>' . "\n";
            $returnVar .= '<td class="ntSimpleFormTableField" colspan="2" align="right">' . "\n";

            foreach($buttons as $buttonKey => $buttonValue)
            {
                $returnVar .= $this->returnField($buttonValue);
            }

            $returnVar .= '</td>' . "\n";
            $returnVar .= '</tr>' . "\n";
        }
        
        $returnVar .= '</table>' . "\n";
        $returnVar .= '</form>' . "\n";
		
		return $returnVar;
    
    }
	
	
    /**
     * Prints the output of ->getHTML()
     *
	 * @change - v1.0.1
	 * 		- created getHTML(), revised code for toString() to only print the output of getHTML()
     * @author  Roberto Piazza
     * @access  public
     * @return  STRING
     * @since   1.0.0
     * @version 1.0.1
     */
    function toString()
    {
    	print $this->getHTML();
    }     

}

?>
Return current item: ntSimpleForm