// misc functions and classes

function getWindowHeight() {
    // adapted from sample at http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
  var winHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //non-IE
    winHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ 'standards compliant mode'
    winHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 
    winHeight = document.body.clientHeight;
  }
  return winHeight;
}

function redirectToLogin(loginUrl) {
    window.location = loginUrl;
}

function toggleRequiredValidation(ctrl, needsValidation) {
    var lbl = $(ctrl.id + 'Label');
    if (needsValidation){
        $(ctrl).addClassName('required');
        if(lbl) $(lbl).addClassName('required');
        var icon = $('advice-required-' + ctrl.id);
        if (icon) {icon.style.display = 'block'}; //re-set error icon, if exists
    }
    else {
        $(ctrl).removeClassName('required');
        if(lbl) $(lbl).removeClassName('required');
        clearError(ctrl);
    }
}
function clearError(ctrl) {
    var icon = $('advice-required-' + ctrl.id);
    if (icon) {icon.style.display = 'none'}; //clear error icon
    $(ctrl).removeClassName('validation-failed');
}

/* Multi-value checkbox with other */
function SetMultiValueCheckboxItems(sender, hiddenFieldId, className, otherFieldId) {
    var checkboxes = $$("input." + className);
    var newValue = "";
    var otherCheckbox = checkboxes.last(); //assert other is always last check box

    if (sender == otherCheckbox && otherFieldId != null) {
        toggleRequiredValidation($(otherFieldId), sender.checked);
        // clear other text box
        if (!sender.checked) {
            $(otherFieldId).clear();
        }
    }

    checkboxes.each(function(item) {
        if (item.checked) {
            if (newValue != "") {
                newValue += ", ";
            }
            newValue += $(item).getValue();
        }
    });

    $(hiddenFieldId).value = newValue;
}

/* 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();
    }
}