function DateRange( fromDate, toDate ) {
    if( fromDate != null && toDate != null && fromDate.getTime() > toDate.getTime() ) {
        this.fromDate = toDate;
        this.toDate = fromDate;
        this.switchedDates = true;
    } else {
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.switchedDates = false;
    }
};

DateRange.prototype.isSwitchedDates = function() {
    return this.switchedDates;
};
DateRange.prototype.getFromDate = function() {
    return this.fromDate;
};
DateRange.prototype.getToDate = function() {
    return this.toDate;
};

DateRange.prototype.isDateValid = function( dateToVerifiy ) {
    return dateToVerifiy != null && dateToVerifiy.getTime() != NaN;
};
DateRange.prototype.hasValidFromDate = function() {
    return this.isDateValid( this.fromDate );
};
DateRange.prototype.hasValidToDate = function() {
    return this.isDateValid( this.toDate );
};

DateRange.prototype.isInRange = function( dateToVerify ) {
    var inRange = false;
    if( this.isDateValid( dateToVerify ) ) {
        if( this.hasValidFromDate() ) {
            inRange = this.fromDate.getTime() <= dateToVerify.getTime();
        }
        if( this.hasValidToDate() ) {
            inRange = this.toDate.getTime() >= dateToVerify.getTime();
        }
    }
    return inRange;
};

DateRange.prototype.getMinimumYear = function() {
    var year = 0;
    if( this.hasValidFromDate() ) {
        year = this.fromDate.getFullYear();
    }
    else
    if( this.hasValidToDate() ) {
        year = this.toDate.getToYear();
    }
    return year;
};
DateRange.prototype.getMaximumYear = function() {
    var year = 0;
    if( this.hasValidToDate() ) {
        year = this.toDate.getFullYear();
    }
    else
    if( this.hasValidFromDate() ) {
        year = this.fromDate.getToYear();
    }
    return year;
};

DateRange.prototype.getDurationYears = function() {
    var duration = NaN;
    if( this.hasValidFromDate() ) {
        if( this.hasValidToDate() ) {
            duration = this.toDate.getFullYear() - this.fromDate.getFullYear();
        }
        else {
            duration = Infinity;
        }
    }
    else
    if( this.hasValidToDate() ) {
        duration = -Infinity;
    }

    return duration;
};

DateRange.prototype.getDurationMonths = function() {
    var duration = 0;

    var years = this.getDurationYears();
    if( years != NaN ) {

        if( Math.abs( years ) != Infinity ) {
                        
            var calendarFrom = new SimpleCalendar( this.fromDate );
            var calendarTo = new SimpleCalendar( this.toDate );
            var minYear = this.getMinimumYear();
            var maxYear = this.getMaximumYear();

            if( minYear == maxYear ) {
                duration = calendarTo.getTime().getMonth() - calendarFrom.getTime().getMonth();
            } else {
                duration = 12 - calendarFrom.getTime().getMonth();

                for( var year = minYear + 1 ; year < maxYear; year++ ) {
                    duration += 12;
                }

                duration += calendarTo.getTime().getMonth();
            }

        } else {
            duration = years;
        }
    }

    return duration;
};

DateRange.prototype.getDurationDays = function() {
    var duration = 0;

    var years = this.getDurationYears();
    if( years != NaN ) {

        if( Math.abs( years ) != Infinity ) {


            var calendarFrom = new SimpleCalendar( this.fromDate );
            var calendarTo = new SimpleCalendar( this.toDate );
            var minYear = this.getMinimumYear();
            var maxYear = this.getMaximumYear();

            if( minYear == maxYear ) {
                duration = calendarTo.getDayInYear() - calendarFrom.getDayInYear();
            }
            else {

                duration = calendarFrom.getDaysInYear() - calendarFrom.getDayInYear();

                for( var year = minYear + 1 ; year < maxYear; year++ ) {
                    duration += calendarFrom.getDaysInYear( year );
                }

                duration += calendarTo.getDayInYear();
            }

        } else {
            duration = years;
        }
    }

    return duration;
};

DateRange.prototype.getRangeAsString = function(){
    var asString = "";
    if( this.hasValidFromDate() ) {
        asString += this.getFromDate().getDate() + "." + (this.getFromDate().getMonth()+1) + "." + this.getFromDate().getFullYear();
    } else {
    }
    asString += " - " ;
    if( this.hasValidToDate() ) {
        asString += this.getToDate().getDate() + "." + (this.getToDate().getMonth()+1) + "." + this.getToDate().getFullYear();
    }

    return asString;
};
