<?PHP
/*
* in config_inc.php add the following line
*/
$g_use_date_picker_javascript			= ON;

/*
* in core/date_api.php replace the function print_date_selection_set with the one listed below
*/


function print_date_selection_set( $p_name, $p_format, $p_date = 0, $p_default_disable = false, $p_allow_blank = false, $p_year_start = 0, $p_year_end = 0 ) {
    if (ON == config_get( 'use_date_picker_javascript' )
                && "/return_dynamic_filters.php" != $_SERVER["SCRIPT_NAME"]
       ) {
        $p_date = is_numeric( $p_date ) ? $p_date : time();
        $t_date = preg_split( '/-/', date( 'Y-m-d', $p_date ), -1, PREG_SPLIT_NO_EMPTY );
        $t_date_to_display = $t_date ? $t_date[0] . "-". $t_date[1] . "-". $t_date[2] : '';
        print "<input ".helper_get_tab_index()." type=\"text\" size=\"14\" id=\"$p_name\" name=\"$p_name\" size=\"20\" maxlength=\"12\" value=\"".$t_date_to_display."\" />";
        date_print_calendar("trigger".$p_name);
        date_finish_calendar( $p_name, "trigger".$p_name);
    } else {
        $t_chars = preg_split( '//', $p_format, -1, PREG_SPLIT_NO_EMPTY );
        if( $p_date != 0 ) {
            $t_date = preg_split( '/-/', date( 'Y-m-d', $p_date ), -1, PREG_SPLIT_NO_EMPTY );
        } else {
            $t_date = array(
                0,
                0,
                0,
            );
        }
 
        $t_disable = '';
        if( $p_default_disable == true ) {
            $t_disable = ' disabled="disabled"';
        }
        $t_blank_line = '';
        if( $p_allow_blank == true ) {
            $t_blank_line = "<option value=\"0\"></option>";
        }

        foreach( $t_chars as $t_char ) {
            if( strcmp( $t_char, "M" ) == 0 ) {
                echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_month\"$t_disable>";
                echo $t_blank_line;
                print_month_option_list( $t_date[1] );
                echo "</select>\n";
            }
            if( strcmp( $t_char, "m" ) == 0 ) {
                echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_month\"$t_disable>";
                echo $t_blank_line;
                print_numeric_month_option_list( $t_date[1] );
                echo "</select>\n";
            }
            if( strcasecmp( $t_char, "D" ) == 0 ) {
                echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_day\"$t_disable>";
                echo $t_blank_line;
                print_day_option_list( $t_date[2] );
                echo "</select>\n";
            }
            if( strcasecmp( $t_char, "Y" ) == 0 ) {
                echo "<select ", helper_get_tab_index(), " name=\"" . $p_name . "_year\"$t_disable>";
                echo $t_blank_line;
                print_year_range_option_list( $t_date[0], $p_year_start, $p_year_end );
                echo "</select>\n";
            }
        }
    }
}

/*
* in core/gpc_api.php replace the function gpc_isset_custom_field with the one listed below
*/
function gpc_isset_custom_field( $p_var_name, $p_custom_field_type ) {
	$t_field_name = 'custom_field_' . $p_var_name;

	switch ($p_custom_field_type ) {
		case CUSTOM_FIELD_TYPE_DATE:
            if (ON == config_get( 'use_date_picker_javascript' )) {
                return gpc_isset( $t_field_name ) ;
            } else {
                return gpc_isset( $t_field_name . '_day' ) &&
                    gpc_get_int( $t_field_name . '_day', 0 ) != 0 &&
                    gpc_isset( $t_field_name . '_month' ) &&
                    gpc_get_int( $t_field_name . '_month', 0 ) != 0 &&
                    gpc_isset( $t_field_name . '_year' ) &&
                    gpc_get_int( $t_field_name . '_year', 0 ) != 0 ;
            }
		case CUSTOM_FIELD_TYPE_STRING:
		case CUSTOM_FIELD_TYPE_NUMERIC:
		case CUSTOM_FIELD_TYPE_FLOAT:
		case CUSTOM_FIELD_TYPE_ENUM:
		case CUSTOM_FIELD_TYPE_EMAIL:
			return gpc_isset( $t_field_name ) && !is_blank( gpc_get_string( $t_field_name ) );
		default:
			return gpc_isset( $t_field_name );
	}
}

/*
* in core/gpc_api.php replace the function gpc_get_custom_field with the one listed below
*/
function gpc_get_custom_field( $p_var_name, $p_custom_field_type, $p_default = null ) {
	switch( $p_custom_field_type ) {
		case CUSTOM_FIELD_TYPE_MULTILIST:
		case CUSTOM_FIELD_TYPE_CHECKBOX:
		    // ensure that the default is an array, if set
		    if ( ($p_default !== null) && !is_array($p_default) ) {
		        $p_default = array( $p_default );
		    }
			$t_values = gpc_get_string_array( $p_var_name, $p_default );
			if( is_array( $t_values ) ) {
				return implode( '|', $t_values );
			} else {
				return '';
			}
			break;
		case CUSTOM_FIELD_TYPE_DATE:
            if (ON == config_get( 'use_date_picker_javascript' )) {
                return strtotime( gpc_get_string( $p_var_name, time() ));
            } else {
                $t_day = gpc_get_int( $p_var_name . '_day', 0 );
                $t_month = gpc_get_int( $p_var_name . '_month', 0 );
                $t_year = gpc_get_int( $p_var_name . '_year', 0 );
                if(( $t_year == 0 ) || ( $t_month == 0 ) || ( $t_day == 0 ) ) {
                    if( $p_default == null ) {
                        return '';
                    } else {
                        return $p_default;
                    }
                } else {
                    return strtotime( $t_year . '-' . $t_month . '-' . $t_day );
                }
            }
 			break;
		default:
			return gpc_get_string( $p_var_name, $p_default );
	}
}