﻿;if(window.jQuery) (function($){
    $.fn.addOption = function()
    {
	    var add = function(el, v, t, sO)
	    {
		    var option = document.createElement("option");
		    option.value = v, option.text = t;
		    // get options
		    var o = el.options;
		    // get number of options
		    var oL = o.length;
		    if(!el.cache)
		    {
			    el.cache = {};
			    // loop through existing options, adding to cache
			    for(var i = 0; i < oL; i++)
			    {
				    el.cache[o[i].value] = i;
			    }
		    }
		    // add to cache if it isn't already
		    if(typeof el.cache[v] == "undefined") el.cache[v] = oL;
		    el.options[el.cache[v]] = option;
		    if(sO)
		    {
			    option.selected = true;
		    }
	    };
    	
	    var a = arguments;
	    if(a.length == 0) return this;
	    // select option when added? default is true
	    var sO = true;
	    // multiple items
	    var m = false;
	    // other variables
	    var items, v, t;
	    if(typeof(a[0]) == "object")
	    {
		    m = true;
		    items = a[0];
	    }
	    if(a.length >= 2)
	    {
		    if(typeof(a[1]) == "boolean") sO = a[1];
		    else if(typeof(a[2]) == "boolean") sO = a[2];
		    if(!m)
		    {
			    v = a[0];
			    t = a[1];
		    }
	    }
	    this.each(
		    function()
		    {
			    if(this.nodeName.toLowerCase() != "select") return;
			    if(m)
			    {
				    for(var item in items)
				    {
					    add(this, item, items[item], sO);
				    }
			    }
			    else
			    {
				    add(this, v, t, sO);
			    }
		    }
	    );
	    return this;
    };
    $.fn.removeOption = function()
    {
	    var a = arguments;
	    if(a.length == 0) return this;
	    var ta = typeof(a[0]);
	    var v, index;
	    // has to be a string or regular expression (object in IE, function in Firefox)
	    if(ta == "string" || ta == "object" || ta == "function" )
	    {
		    v = a[0];
		    // if an array, remove items
		    if(v.constructor == Array)
		    {
			    var l = v.length;
			    for(var i = 0; i<l; i++)
			    {
				    this.removeOption(v[i], a[1]); 
			    }
			    return this;
		    }
	    }
	    else if(ta == "number") index = a[0];
	    else return this;
	    this.each(
		    function()
		    {
			    if(this.nodeName.toLowerCase() != "select") return;
			    // clear cache
			    if(this.cache) this.cache = null;
			    // does the option need to be removed?
			    var remove = false;
			    // get options
			    var o = this.options;
			    if(!!v)
			    {
				    // get number of options
				    var oL = o.length;
				    for(var i=oL-1; i>=0; i--)
				    {
					    if(v.constructor == RegExp)
					    {
						    if(o[i].value.match(v))
						    {
							    remove = true;
						    }
					    }
					    else if(o[i].value == v)
					    {
						    remove = true;
					    }
					    // if the option is only to be removed if selected
					    if(remove && a[1] === true) remove = o[i].selected;
					    if(remove)
					    {
						    o[i] = null;
					    }
					    remove = false;
				    }
			    }
			    else
			    {
				    // only remove if selected?
				    if(a[1] === true)
				    {
					    remove = o[index].selected;
				    }
				    else
				    {
					    remove = true;
				    }
				    if(remove)
				    {
					    this.remove(index);
				    }
			    }
		    }
	    );
	    return this;
    };
    $.fn.bindyear = function(value, count){
        var el = this;
        for (var i = value; i > value - count; i--)
            el.addOption(i, i);
        this.children()[0].selected = true;
        //el.options[0].selected = true;
    }
    $.fn.bindmonth = function(){
        for(var i = 1; i < 13; i++)
            $(this).addOption(i, i, false);
    }
    $.fn.bindday = function(){
        var args = arguments;
        var y = $(args[0]);
        var m = $(args[1]);
        var bind = function(el){
            var year = y.val();
            var month = m.val();
            $(el).removeOption(/./);
            for(var day = 1; day < 31 + 1; day++)
            {
                if (isDate(year, month, day))
                    $(el).addOption(day, day, false);
                else
                    break;
            }
        };
        var isDate = function(year, month, day) {
            // month argument must be in the range 1 - 12
            month = month - 1; // javascript month range : 0- 11
            var tempDate = new Date(year,month,day);
            if ((getYear(tempDate.getYear()) == year) &&
                (month == tempDate.getMonth()) &&
                (day == tempDate.getDate()))
                return true;
            else
                return false
        }
        var el = this;
        
        $(y).change(function(){bind(el);});
        $(m).change(function(){bind(el);});
    }
})(jQuery);