﻿function RouteControl(id, popup_behavior, calendar_id, input_id, min, max, handler) {
    this._format_1 = "%m/%d/%Y";
    this._format_2 = "%d/%m/%Y";
    this._id = id;
    this._popup_behavior = popup_behavior;  
    this._calendar_id = calendar_id;    
    this._input_id = input_id;
    this._min = Date.parseDate(min, this._format_1);
    this._max = Date.parseDate(max, this._format_1); 
    this._handler = handler;
    
    this.Changed = function(sender) {
        var date = Date.parseDate(sender.value, this._format_1);
        if (this.DateStatus(date, '', '', '')) {
            date = Date.parseDate(sender.value, this._format_2);
            if (this.DateStatus(date, '', '', '')) {
                date = new Date();
            }
        }
        sender.value = date.print(this._format_1);
        $find(this._popup_behavior).hidePopup();
        eval(this._handler);
    }

    this.OnShowing = function() {
        $get(this._calendar_id).innerHTML = ''; 
        
        var t1 = new Function('sender', this._id + '.DateClicked(sender);');
        var t2 = new Function('date', 'y', 'm', 'd', 'return ' + this._id + '.DateStatus(date, y, m, d);');
        
        Calendar.setup({
            inputField : this._input_id,
            ifFormat : this._format_1,
            flat : this._calendar_id,
            flatCallback : t1,
            dateStatusFunc : t2,
            weekNumbers : false,
            range : [this._min.getFullYear(), this._max.getFullYear()],
            date : $get(this._input_id).value,
            electric : false
        });
    }

    this.DateClicked = function(sender) {
        if (sender.dateClicked) {
            $find(this._popup_behavior).hidePopup();
        }
    }

    this.DateStatus = function(date, y, m, d) {   
        return (date < this._min) || (date > this._max);
    }
}

if(typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();