
function redirectToLogin(loginUrl) {  
    window.location = loginUrl;  
} 

/* date selector class */
var DateSelector = Class.create();
DateSelector.prototype = {
    _selector        : null, // select element bound to class
    _targetFrom      : null, // text input element for start/from date
    _targetTo        : null, // text input element for end/to date
    _fiscalStartMonth: null, // month in which fiscal year starts
    initialize	: function ( params ) {
    /* arguments */
    this._selector = $(params["selector"]);
    this._targetTo = $(params["targetTo"]);
    this._targetFrom = $(params["targetFrom"]);
    this._fiscalYearMonth = params["fiscalYearMonth"];
    },
    bind	: function () {
        this._addOption(this._selector, "", "Custom Dates");
        this._addOption(this._selector, "CurrentWeek", "Current Week");
        this._addOption(this._selector, "CurrentMonth", "Current Month");
        this._addOption(this._selector, "CurrentQtr", "Current Quarter");
        this._addOption(this._selector, "CurrentYear", "Current Year");
        this._addOption(this._selector, "LastWeek", "Last Week");
        this._addOption(this._selector, "LastMonth", "Last Month");
        this._addOption(this._selector, "LastQtr", "Last Quarter");
        this._addOption(this._selector, "LastYear", "Last Year");
        this._addOption(this._selector, "Previous3", "Previous 3 Months");
        this._addOption(this._selector, "Previous12", "Previous 12 Months");
        // declare event listener for select element
        Event.observe(this._selector, 'change', this.showDates.bindAsEventListener(this), false);
    },
    _addOption : function (list, optionValue, optionText) {
        list[list.length] = new Option(optionText, optionValue);
    },
    showDates : function() {
        var startDate = "", endDate = "";
        var today = new Date();
        var thisDay = today.getDay();
        var thisMonth = today.getMonth()+1;
        var thisYear = today.getFullYear();
        var fiscalMonth = this._fiscalYearMonth
        switch (this._selector.options[this._selector.selectedIndex].value) {
            case "CurrentWeek":
                startDate = this._formatDate(today.setDate(today.getDate() - thisDay));
                endDate = this._formatDate(today.setDate(today.getDate() + 6));
                break;
            case "CurrentMonth":
                startDate = this._getStartDate(thisMonth, thisYear);
                endDate = this._getEndDate(thisMonth, thisYear);
                break;
            case "CurrentQtr":
                if (thisMonth > 0 && thisMonth <= 3) {
                    startDate = this._getStartDate(1, thisYear);
                    endDate = this._getEndDate(3, thisYear);}
                else if (thisMonth > 3 && thisMonth <= 6) {
                    startDate = this._getStartDate(4, thisYear);
                    endDate = this._getEndDate(6, thisYear);}
                else if (thisMonth > 6 && thisMonth <= 9) {
                    startDate = this._getStartDate(7, thisYear);
                    endDate = this._getEndDate(9, thisYear);}
                else {
                    startDate = this._getStartDate(10, thisYear);
                    endDate = this._getEndDate(12, thisYear);}
                break;
            case "CurrentYear":
                if (thisMonth >= fiscalMonth) {
                    startDate = this._getStartDate(fiscalMonth, thisYear);
                    if(fiscalMonth == 1)
                        endDate = this._getEndDate(12, thisYear);
                    else
                        endDate = this._getEndDate(fiscalMonth-1, (thisYear + 1));
                }
                else {
                    startDate = this._getStartDate(fiscalMonth, (thisYear - 1));
                    endDate = this._getEndDate(fiscalMonth-1, thisYear);
                }
                break;
            case "LastWeek":
                startDate = this._formatDate(today.setDate(today.getDate() - thisDay -7));
                endDate = this._formatDate(today.setDate(today.getDate() + 6));
                break;
            case "LastMonth":
                if (thisMonth > 1) {
                    startDate = this._getStartDate((thisMonth - 1), thisYear);
                    endDate = this._getEndDate((thisMonth - 1), thisYear);
                }
                else {
                    startDate = this._getStartDate(12, (thisYear - 1));
                    endDate = this._getEndDate(12, (thisYear - 1));
                }
                break;
            case "LastQtr":
                if (thisMonth > 0 && thisMonth <= 3) {
                    startDate = this._getStartDate(10, (thisYear - 1));
                    endDate = this._getEndDate(12, (thisYear - 1));
                }
                else if (thisMonth > 3 && thisMonth <= 6) {
                    startDate = this._getStartDate(1, thisYear);
                    endDate = this._getEndDate(3, thisYear);
                }
                else if (thisMonth > 6 && thisMonth <= 9) {
                    startDate = this._getStartDate(4, thisYear);
                    endDate = this._getEndDate(6, thisYear);
                }
                else {
                    startDate = this._getStartDate(7, thisYear);
                    endDate = this._getEndDate(9, thisYear);
                }
                break;
            case "LastYear":
                if (thisMonth >= fiscalMonth) {
                    startDate = this._getStartDate(fiscalMonth, (thisYear - 1));
                    if(fiscalMonth == 1)
                        endDate = this._getEndDate(12, thisYear - 1);
                    else
                        endDate = this._getEndDate(fiscalMonth-1, thisYear);
                }
                else {
                    startDate = this._getStartDate(fiscalMonth, (thisYear - 2));
                    endDate = this._getEndDate(fiscalMonth-1, (thisYear - 1));
                }
                break;
                
            case "Previous3":
                if (thisMonth > 3) {
                    startDate = this._getStartDate((thisMonth - 3), thisYear);
                    endDate = this._getEndDate((thisMonth - 1), thisYear);
                }
                else if (thisMonth == 3 || thisMonth == 2) {
                    startDate = this._getStartDate(9 + thisMonth, (thisYear - 1));
                    endDate = this._getEndDate((thisMonth - 1), thisYear);
                }
                else if (thisMonth == 1) {
                    startDate = this._getStartDate(10, (thisYear - 1));
                    endDate = this._getEndDate(12, (thisYear - 1));
                }
                break;
            case "Previous12":
                if (thisMonth == 1) {
                    startDate = this._getStartDate(1, (thisYear - 1));
                    endDate = this._getEndDate(12, (thisYear - 1));
                }
                else {
                    startDate = this._getStartDate(thisMonth, (thisYear - 1));
                    endDate = this._getEndDate((thisMonth - 1), thisYear);
                }
                break;
            default:
                break;
        }
        this._targetFrom.value = startDate;
        this._targetTo.value = endDate;
    },
    _getStartDate   : function(startMonth, startYear) {
        // get date for first day of specified month/year
        return startMonth + "/1/" + startYear;
    },
    _getEndDate     : function(endMonth, endYear) {
        // get date for last day of specified month/year
        var thisDate;
        if (endMonth == 12) 
            thisDate = new Date("1/1/" + (endYear + 1));
        else 
            thisDate = new Date((endMonth + 1) + "/1/" + endYear);
        // subtract 1 day to get end of desired month
        return this._formatDate(thisDate.setDate(thisDate.getDate() - 1));
    },
    _formatDate : function(value) {
        var thisDate = new Date(value);
        return (thisDate.getMonth()+1) + "/" + thisDate.getDate() + "/" + thisDate.getFullYear();
    }
}