* Name:     html_select_date
 *            - 1.0 initial release
 *            - 1.1 added support for +/- N syntax for begin
 *              and end year values. (Monte)
 *            - 1.2 added support for yyyy-mm-dd syntax for
 *              time value. (Jan Rosier)
 *            - 1.3 added support for choosing format for
 *              month values (Gary Loescher)
 *            - 1.3.1 added support for choosing format for
 *              day values (Marcus Bointon)
 *            - 1.3.2 support negative timestamps, force year
 *              dropdown to include given date unless explicitly set (Monte)
 *            - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
 *              of 0000-00-00 dates (cybot, boots)
 *            - 2.0 complete rewrite for performance,
 *              added attributes month_names, *_id
 *  
 *
 * @link     http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
 *           (Smarty online manual)
 * @version  2.0
 * @author   Andrei Zmievski
 * @author   Monte Ohrt 
 * @author   Rodney Rehm
 *
 * @param array $params parameters
 *
 * @return string
 */
function smarty_function_html_select_date($params)
{
    // generate timestamps used for month names only
    static $_month_timestamps = null;
    static $_current_year = null;
    if ($_month_timestamps === null) {
        $_current_year = date('Y');
        $_month_timestamps = array();
        for ($i = 1; $i <= 12; $i ++) {
            $_month_timestamps[$i] = mktime(0, 0, 0, $i, 1, 2000);
        }
    }
    /* Default values. */
    $prefix = "Date_";
    $start_year = null;
    $end_year = null;
    $display_days = true;
    $display_months = true;
    $display_years = true;
    $month_format = "%B";
    /* Write months as numbers by default  GL */
    $month_value_format = "%m";
    $day_format = "%02d";
    /* Write day values using this format MB */
    $day_value_format = "%d";
    $year_as_text = false;
    /* Display years in reverse order? Ie. 2000,1999,.... */
    $reverse_years = false;
    /* Should the select boxes be part of an array when returned from PHP?
       e.g. setting it to "birthday", would create "birthday[Day]",
       "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
    $field_array = null;
    /* 's of the different  tags.
       If not set, uses default dropdown. */
    $day_size = null;
    $month_size = null;
    $year_size = null;
    /* Unparsed attributes common to *ALL* the / or ' . $option_separator;
            if (isset($year_empty) || isset($all_empty)) {
                $_html_years .= '' . (isset($year_empty) ? $year_empty : $all_empty) . ' ' . $option_separator;
            }
            $op = $start_year > $end_year ? - 1 : 1;
            for ($i = $start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
                $_html_years .= '' . $i . ' ' . $option_separator;
            }
            $_html_years .= ' ';
        }
    }
    // generate month  or ' . $option_separator;
        if (isset($month_empty) || isset($all_empty)) {
            $_html_months .= '' . (isset($month_empty) ? $month_empty : $all_empty) . ' ' . $option_separator;
        }
        for ($i = 1; $i <= 12; $i ++) {
            $_val = sprintf('%02d', $i);
            $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i]));
            $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]);
            $_html_months .= '' . $_text . ' ' . $option_separator;
        }
        $_html_months .= ' ';
    }
    // generate day  or ' . $option_separator;
        if (isset($day_empty) || isset($all_empty)) {
            $_html_days .= '' . (isset($day_empty) ? $day_empty : $all_empty) . ' ' . $option_separator;
        }
        for ($i = 1; $i <= 31; $i ++) {
            $_val = sprintf('%02d', $i);
            $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
            $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
            $_html_days .= '' . $_text . ' ' . $option_separator;
        }
        $_html_days .= ' ';
    }
    // order the fields for output
    $_html = '';
    for ($i = 0; $i <= 2; $i ++) {
        switch ($field_order[$i]) {
            case 'Y':
            case 'y':
                if (isset($_html_years)) {
                    if ($_html) {
                        $_html .= $field_separator;
                    }
                    $_html .= $_html_years;
                }
                break;
            case 'm':
            case 'M':
                if (isset($_html_months)) {
                    if ($_html) {
                        $_html .= $field_separator;
                    }
                    $_html .= $_html_months;
                }
                break;
            case 'd':
            case 'D':
                if (isset($_html_days)) {
                    if ($_html) {
                        $_html .= $field_separator;
                    }
                    $_html .= $_html_days;
                }
                break;
        }
    }
    return $_html;
}