(function() {

    function IntervalCalendar(container, cfg) {
        
        this._iState = 0;
        
        cfg = cfg || {};
        cfg.multi_select = true;

        
        IntervalCalendar.superclass.constructor.call(this, container, cfg);

        
        this.beforeSelectEvent.subscribe(this._intervalOnBeforeSelect, this, true);
        this.selectEvent.subscribe(this._intervalOnSelect, this, true);
        this.beforeDeselectEvent.subscribe(this._intervalOnBeforeDeselect, this, true);
        this.deselectEvent.subscribe(this._intervalOnDeselect, this, true);
    }

    
    IntervalCalendar._DEFAULT_CONFIG = YAHOO.widget.CalendarGroup._DEFAULT_CONFIG;

    YAHOO.lang.extend(IntervalCalendar, YAHOO.widget.CalendarGroup, {

       
        _dateString : function(d) {
            var a = [];
            a[this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.MDY_MONTH_POSITION.key)-1] = (d.getMonth() + 1);
            a[this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.MDY_DAY_POSITION.key)-1] = d.getDate();
            a[this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.MDY_YEAR_POSITION.key)-1] = d.getFullYear();
            var s = this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.DATE_FIELD_DELIMITER.key);
            return a.join(s);
        },

       
        _dateIntervalString : function(l, u) {
            var s = this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.DATE_RANGE_DELIMITER.key);
            return (this._dateString(l)
                    + s + this._dateString(u));
        },

        
        getInterval : function() {
           
            var dates = this.getSelectedDates();
            if(dates.length > 0) {
                
                var l = dates[0];
                var u = dates[dates.length - 1];
                return [l, u];
            }
            else {
                
                return [];
            }
        },

        
        setInterval : function(d1, d2) {
            
            var b = (d1 <= d2);
            var l = b ? d1 : d2;
            var u = b ? d2 : d1;
            
            this.cfg.setProperty('selected', this._dateIntervalString(l, u), false);
            this._iState = 2;
        },

        resetInterval : function() {
            this.cfg.setProperty('selected', [], false);
            this._iState = 0;
        },

        
        _intervalOnBeforeSelect : function(t,a,o) {
           
            this._iState = (this._iState + 1) % 3;
            if(this._iState == 0) {
                
                this.deselectAll();
                this._iState++;
            }
        },

       
        _intervalOnSelect : function(t,a,o) {
            
            var dates = this.getSelectedDates();
            if(dates.length > 1) {
                
                var l = dates[0];
                var u = dates[dates.length - 1];
                this.cfg.setProperty('selected', this._dateIntervalString(l, u), false);
            }
                this.render();
        },

       
        _intervalOnBeforeDeselect : function(t,a,o) {
            if(this._iState != 0) {
                return false;
            }
        },

        
        _intervalOnDeselect : function(t,a,o) {
            if(this._iState != 0) {
                
                this._iState = 0;
                this.deselectAll();

               
                var d = a[0];
                var date = YAHOO.widget.DateMath.getDate(d[0], d[1] - 1, d[2]);
                var page = this.getCalendarPage(date);
                if(page) {
                    
                    page.beforeSelectEvent.fire();
                    this.cfg.setProperty('selected', this._dateString(date), false);
                    page.selectEvent.fire([d]);
                }
                
                return false;
            }
        }
    });

    YAHOO.namespace("example.calendar");
    YAHOO.example.calendar.IntervalCalendar = IntervalCalendar;
})();

var cal;
function init_calendar() {
    var inTxt = YAHOO.util.Dom.get("in"),
        outTxt = YAHOO.util.Dom.get("out"),
        inDate, outDate, interval;

    inTxt.value = "";
    outTxt.value = "";

    cal = new YAHOO.example.calendar.IntervalCalendar("cal1Container", {pages:2});
	
		cal.selectEvent.subscribe(function() {
        interval = this.getInterval();

   if (interval.length == 2) {
            inDate = interval[0];
            inTxt.value = (inDate.getDate()) + "/" + (inDate.getMonth() + 1) + "/" + inDate.getFullYear();
			if (interval[0].getTime() != interval[1].getTime()) {
                outDate = interval[1];
                outTxt.value = (outDate.getDate()) + "/" + (outDate.getMonth() + 1) + "/" + outDate.getFullYear();
            } else {
                outTxt.value = "";
            }
        }
    }, cal, true);
    
    cal.render();
}
function resetCal()
{
cal.clear();
document.getElementById('lblMessage').innerHTML="";
return true;
}