"use strict";

function MMYYYY(m, y) {
    this.set(m, y);
}


MMYYYY.prototype.getMonth = function () {
    return this._m;
}
MMYYYY.prototype.getYear = function () {
    return this._y;
}


MMYYYY.prototype.set = function (m, y) {
    this._m = m;
    this._y = y;
}


// Date format: DD.MM.YYYY
function DDMMYYYY(date) {
    if ("undefined" === typeof(date))
        this.setFromDate((new Date()));
    else
        this.set(date);
}


DDMMYYYY.prototype.set = function (date) {
    if (date instanceof Date)
        this.setFromDate(date);
    else if (date instanceof DDMMYYYY)
        this.setFromDDMMYYYY(date);
    else
        this.setFromStr(date);
}


DDMMYYYY.prototype.setFromDate = function (date) {
    this._d = date.getDate();
    this._m = new MMYYYY(date.getMonth() + 1, date.getFullYear());
}


DDMMYYYY.prototype.setFromDDMMYYYY = function (date) {
    this._d = date._d;
    this._m = new MMYYYY(date._m.getMonth(), date._m.getYear());
}


DDMMYYYY.prototype.setFromStr = function (date) {
    // DD.MM.YYYY
    // 0123456789
    if (10 != date.length)
        throw "OMG";
    if (('.' != date[2]) || ('.' != date[5]))
        throw "OMG";

    var day_str = date.substr(0, 2);
    var month_str = date.substr(3, 2);
    var year_str = date.substr(6);

    this._d = parseInt(day_str);
    this._m = new MMYYYY(parseInt(month_str), parseInt(year_str));
}


DDMMYYYY.prototype.shiftDays = function (delta) {
    var date = new Date(this._m.getYear(), this._m.getMonth() - 1, this._d + delta);
    this.setFromDate(date);
}
DDMMYYYY.prototype.shiftMonths = function (delta) {

}
DDMMYYYY.prototype.shiftToBeginningOfWeek = function () {
    this.shiftDays(1 - this.getDayOfWeek());
}
DDMMYYYY.prototype.shiftToEndingOfWeek = function () {
    this.shiftDays(7 - this.getDayOfWeek());
}


DDMMYYYY.prototype.toString = function () {
    var d = ((this._d < 10) ? "0" : "") + this._d;
    var m = ((this._m.getMonth() < 10) ? "0" : "") + this._m.getMonth();
    return d + "." + m + "." + this._m.getYear();
}


DDMMYYYY.prototype.getDayOfWeek = function () {
    var date = new Date(this._m.getYear(), this._m.getMonth() - 1, this._d);
    var day_of_week = date.getDay();
    return (0 == day_of_week) ? 7 : day_of_week;
}
