/**
 * DatePickerFormatter class for matching and stringifying dates.
 *
 * By Arturas Slajus <x11@arturaz.net>.
 */
var DatePickerFormatter = Class.create();
DatePickerFormatter.prototype = {
    /**
     * Create a DatePickerFormatter.
     *
     * format: specify a format by passing 3 value array consisting of
     *   "yyyy", "mm", "dd". Default: ["yyyy", "mm", "dd"].
     *
     * separator: string for splitting the values. Default: "-".
     *
     * Use it like this:
     *   var df = new DatePickerFormatter(["dd", "mm", "yyyy"], "/");
     *   df.current_date();
     *   df.match("7/7/2007");
     */
    initialize: function(format, separator) {
        if (typeof(format) == "undefined") { format = ["yyyy", "mm", "dd"] }
        if (typeof(separator) == "undefined") { separator = "-" }

        this._format = format;
        this.separator = separator;
                
        this._format_year_index = format.indexOf("yyyy");
        this._format_month_index = format.indexOf("mm");
        this._format_day_index = format.indexOf("dd");
                
        this._year_regexp = /^\d{4}$/;
        this._month_regexp = /^0\d|1[012]|\d$/;
        this._day_regexp = /^0\d|[12]\d|3[01]|\d$/;
    },
    
    /**
     * Match a string against date format.
     * Returns: [year, month, day]
     */
    match: function(str) {
        d = str.split(this.separator);
        
        if (d.length < 3) {
            return false;
        }
        
        year = d[this._format_year_index].match(this._year_regexp);
        if (year) { year = year[0] } else { return false }
        
        month = d[this._format_month_index].match(this._month_regexp);
        if (month) { month = month[0] } else { return false }

        day = d[this._format_day_index].match(this._day_regexp);
        if (day) { day = day[0] } else { return false }
        
        return [year, month, day];
    },
    
    /**
     * Return current date according to format.
     */
    current_date: function() {
        var d = new Date;
        return this.date_to_string(
            d.getFullYear(),
            d.getMonth() + 1,
			d.getDate()
       );
    },
    
    /**
     * Return a stringified date according to format.
     */
    date_to_string: function(year, month, day, separator) {
        if (typeof(separator) == "undefined") { separator = this.separator }

        var a = [0, 0, 0];
        a[this._format_year_index] = year;
        a[this._format_month_index] = this.leftpad_with_zeroes(month, 2);
        a[this._format_day_index] = this.leftpad_with_zeroes(day, 2);
        
        return a.join(separator);
    },
    
    /**
     * Pad string from left with zeroes.
     *
     * Shameleslly stolen from http://www.eulerian.com/misc/datepicker/
     */
    leftpad_with_zeroes: function(str, padToLength) {
        var result	= '';
        for (var i = 0; i < (padToLength - String(str).length); i++)
            result	+= '0';
        return result + str;
    }
}; 
