/**
 * selectbox-utils for jQuery
 * 
 * Copyright (c) 2007 Yoshiomi KURISU
 * Licensed under the MIT (MIT-LICENSE.txt)  licenses.
 * 
 * @example  $('#year1').numericOptions({from:2007,to:2011});
 * @example  $('#month1').numericOptions({from:1,to:12});
 * @example  $('#date1').numericOptions().datePulldown({year:$('#year1'),month:$('#month1')});
 * 
 */
function reloadDays(_month, _day, _year) {
    if (_month != "")                          //if no month selected, don't do anything" & vbCrLf)
    {
        var nDays;
        var oldOptionZero = null;
        if (_day.options[0] != null) oldOptionZero = _day.options[0].value;
        var nDays;
        var selectedDay = _day.selectedIndex;
        var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        if (_year != "" && (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0)))  //check for leap year
        {
            monthDays[1] = 29;
        }

        nDays = monthDays[_month - 1];

        if (nDays != _day.options.length)         //verify we need to modify the dropdown
        {
            for (i = 1; i <= _day.options.length; i++)   //clear old values
            {
                _day.options[i - 1] = null;
            }

            for (i = 1; i <= nDays; i++)                 //write new day values to dropdown
            {
                _day.options[i - 1] = new Option(i);
                _day.options[i - 1].value = i;
            }

            if (selectedDay < _day.options.length) //reset selected day, being smart about it
            {
                if (_day.options[0].value == oldOptionZero) {
                    _day.selectedIndex = selectedDay;
                }
                else {
                    _day.selectedIndex = selectedDay - 1  //correct for loss of option[0] null value when repopulating the day;
                }
            }
            else {
                _day.selectedIndex = _day.options.length - 1;
            }
        }
    }
}
(function() {
    //obj is Array
    // Array : [[label,value],[label,value],....] 
    //set options to select node
    //obj is null or obj is number
    //get options from select node
    $.fn.options = function(obj) {
        if (obj || obj == 0) {
            if (obj instanceof Array) {
                this.each(function() {
                    this.options.length = 0;
                    for (var i = 0, len = obj.length; i < len; i++) {
                        var tmp = obj[i];
                        if (tmp.length && tmp.length == 2) {
                            this.options[this.options.length] = new Option(tmp[0], tmp[1]);
                        }
                    }
                });
                return this;
            } else if (typeof obj == 'number') {
                return $('option:eq(' + obj + ')', this);
            } else if (obj == 'selected') {
                return this.val();
            }
        } else {
            return $('option', this)
        }
        return $([]);
    }
    $.fn.numericOptions = function(settings) {
        settings = jQuery.extend({
            remove: true
			, from: 1
			, to: 31
			, selectedIndex: 0
			, valuePadding: 0
			, namePadding: 0
			, labels: []
			, exclude: null
			, startLabel: null
			, type: null
			, reverse: false
        }, settings);
        //error check
        if (!(settings.from + '').match(/^\d+$/) || !(settings.to + '').match(/^\d+$/) || !(settings.selectedIndex + '').match(/^\d+$/) || !(settings.valuePadding + '').match(/^\d+$/) || !(settings.namePadding + '').match(/^\d+$/)) return;
        if (settings.from > settings.to) return;
        if (settings.to - settings.from < settings.selectedIndex) return;
        //add options
        if (settings.remove) this.children().remove();
        var padfunc = function(v, p) {
            if (('' + v).length < p) {
                for (var i = 0, l = p - (v + '').length; i < l; i++) {
                    v = '0' + v;
                }
            }
            return v;
        }
        var exclude_strings = (settings.exclude && settings.exclude instanceof Array && settings.exclude.length > 0) ? ' ' + settings.exclude.join(' ') + ' ' : '';
        this.each(function() {
            this.options.length = 0
            //set startLabel
            var sl = settings.startLabel;
            if (sl && sl.length && sl.length == 2) {
                this.options[0] = new Option(sl[0], sl[1]);
            }
        });

        if (settings.reverse) {
        
            for (var i = settings.to, j = 0; i >= settings.from; i--) {
                this.each(function() {
                    if (j == 0)
                    this.options[this.options.length] = new Option(" ", 0);
                    var val = padfunc(i, settings.valuePadding);
                    if (exclude_strings.indexOf(' ' + val + ' ') < 0) {
                        var lab = (settings.labels[j]) ? settings.labels[j] : padfunc(i, settings.namePadding);
                        this.options[this.options.length] = new Option(lab, val);
                        j++;
                    }
                });
            }
        }
        else {
            for (var i = settings.from, j = 0; i <= settings.to; i++) {
                this.each(function() {
                if (j == 0)
                    this.options[this.options.length] = new Option(" ", 0);
                    var val = padfunc(i, settings.valuePadding);
                    if (exclude_strings.indexOf(' ' + val + ' ') < 0) {
                        var lab = (settings.labels[j]) ? settings.labels[j] : padfunc(i, settings.namePadding);
                        this.options[this.options.length] = new Option(lab, val);
                        j++;
                    }
                });
            }
        }
        this.each(function() {
            if (jQuery.browser.opera) {
                this.options[settings.selectedIndex].defaultSelected = true;
            } else {
                this.selectedIndex = settings.selectedIndex;
            }
        });
        return this;
    };
    //
    $.fn.datePulldown = function(settings) {
        if (!settings.year || !settings.month) return;
        var y = settings.year;
        var m = settings.month;
        if (!y.val() || !m.val()) return;
        if (!y.val().match(/^\d{1,4}$/)) return;
        if (!m.val().match(/^[0][1-9]$|^[1][1,2]$|^[0-9]$/)) return;

        var self = this;
        var fnc = function() {
            var tmp = new Date(new Date(y.val(), m.val()).getTime() - 1000);
            var lastDay = tmp.getDate() - 0;
            self.each(function() {
                var ind = (this.selectedIndex < lastDay - 1) ? this.selectedIndex : lastDay - 1;
                this.selectedIndex = ind;
                $(this).numericOptions({ to: lastDay, selectedIndex: ind });
            });
        }
        y.change(fnc);
        m.change(fnc);
        return this;
    };
})(jQuery);

