Date.prototype.getWeek = function (dowOffset) {
  /*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

  dowOffset   = typeof(dowOffset) == 'number' ? dowOffset : 1; //default dowOffset to zero
  var newYear = new Date(this.getFullYear(),0,1);
  var day     = newYear.getDay() - dowOffset; //the day of week the year begins on
      day     = (day >= 0 ? day : day + 7);
  var daynum  = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
  var weeknum;

  //if the year starts before the middle of a week
  if(day < 4) {
    weeknum = Math.floor((daynum+day-1)/7) + 1;
    if(weeknum > 52) {
      nYear = new Date(this.getFullYear() + 1,0,1);
      nday = nYear.getDay() - dowOffset;
      nday = nday >= 0 ? nday : nday + 7;
      /*if the next year starts before the middle of
      the week, it is week #1 of that year*/
      weeknum = nday < 4 ? 1 : 53;
    }
  } else {
    weeknum = Math.floor((daynum+day-1)/7);
  }

  return weeknum;
};

Date.prototype.prevDay = function (prev) {
  prev     = (typeof(prev) == 'number') ? prev : 1;
  prevDate = this;
  prevDate.setDate(this.getDate()-prev);
  return prevDate;
}

Date.prototype.prevMonth = function (prev) {
  prev      = (typeof(prev) == 'number') ? prev : 1;
  prevMonth = this;
  prevMonth.setMonth(this.getMonth()-prev);
  return prevMonth;
}

Date.prototype.getMonthLastDay = function () {
  nextMonth    = this;
  nextMonth.setMonth(this.getMonth()+1);
  lastDay      = nextMonth.setDate(0);
  return lastDay;
}

Date.prototype.getWeekFirstDay = function (dowOffset) {
  dowOffset   = typeof(dowOffset) == 'number' ? dowOffset : 1; //default dowOffset to zero
  var day     = this.getDay() - dowOffset; //the day of week the year begins on
      day     = (day >= 0 ? day : day + 7);
  firstDay    = this;
  firstDay.setDate(this.getDate()-day);
  firstDay.setHours(0);
  firstDay.setMinutes(0);
  firstDay.setSeconds(0);
  return firstDay;
}

var startWriteFormat = 'yyyy-MM-dd HH:mm:ss';
var endWriteFormat   = 'yyyy-MM-dd kk:mm:ss';
var startReadFormat  = 'yyyy-MM-dd HH:mm:ss';
var endReadFormat    = 'yyyy-MM-dd kk:mm:ss';
/**
 * Ez fogja elküldeni a cuccot.
 */
function submitNewDates(newFrom, newTo, fromInput, toInput, formObj) {
  fromInput.value = formatDate(newFrom, startWriteFormat);
  toInput.value   = formatDate(newTo,   endWriteFormat);
  formObj.submit();
}

function datePopupHandler(form, fromInput, toInput) {
  this.fromInput  = $(fromInput);
  this.toInput    = $(toInput);
  this.form       = $(form);

  this.submit     = function(from, to) {
    return submitNewDates(from, to, this.fromInput, this.toInput, this.form);
  }

  this.submitWithRound = function(from, to) {
    //alert(from.toLocaleString());
    from.setHours(0);
    from.setMinutes(0);
    from.setSeconds(0);

    to.setHours(23);
    to.setMinutes(0);
    to.setSeconds(0);
    return this.submit(from, to);
  }

  this.currentDay = function() {
    from = new Date();
    to   = new Date();
    return this.submitWithRound(from, to);
  }

  this.currentWeek = function() {
    from = new Date();
    from = from.getWeekFirstDay();

    to   = new Date(from.getFullYear(),from.getMonth(),from.getDate()+6);
    return this.submitWithRound(from, to);
  }

  this.currentMonth = function() {
    from = new Date();
    from.setDate(1);

    to   = new Date();;
    to.getMonthLastDay();
    return this.submitWithRound(from, to);
  }

  this.getFrom = function() {
    return new Date(getDateFromFormat(this.fromInput.value, startReadFormat));
  }

  this.getTo   = function() {
    return new Date(getDateFromFormat(this.toInput.value, endReadFormat));
  }

  // Összehasonlítja, hogy az aktuális időpont az kisebb-e mint "ma", mert ha igen, akkor nem lépteti toávbb a jövőbe.
  this.compareFuture = function(enableFuture, currentTo) {
    ef      = (typeof(enableFuture)=='boolean') ? enableFuture : true;
    compareDate = new Date();
    compareDate.setHours(24);
    return (!ef && currentTo>compareDate);
  }

  this.prevDay  = function() {
    currentFrom = this.getFrom();
    from        = new Date(currentFrom.getFullYear(), currentFrom.getMonth(), currentFrom.getDate()-1);
    to          = new Date(currentFrom.getFullYear(), currentFrom.getMonth(), currentFrom.getDate()-1);
    return this.submitWithRound(from, to);
  }

  this.nextDay  = function(enableFuture) {
    currentTo   = this.getTo();
    if(this.compareFuture(enableFuture, currentTo)) {
      return false;
    }
    to          = new Date(currentTo.getFullYear(), currentTo.getMonth(), currentTo.getDate());
    from        = new Date(currentTo.getFullYear(), currentTo.getMonth(), currentTo.getDate());
    return this.submitWithRound(from, to);
  }

  this.prevWeek = function() {
    currentFrom = this.getFrom();
    from        = new Date(currentFrom.getFullYear(), currentFrom.getMonth(), currentFrom.getDate()-7);
    from        = from.getWeekFirstDay();
    to          = new Date(from.getFullYear(),from.getMonth(),from.getDate()+6);
    return this.submitWithRound(from, to);
  }

  this.nextWeek = function(enableFuture) {
    currentTo   = this.getTo();
    if(this.compareFuture(enableFuture, currentTo)) {
      return false;
    }
    to          = new Date(currentTo.getFullYear(), currentTo.getMonth(), currentTo.getDate()+6);
    from        = to.getWeekFirstDay();
    to          = new Date(from.getFullYear(),from.getMonth(),from.getDate()+6);
    return this.submitWithRound(from, to);
  }

  this.prevMonth = function() {
    currentFrom = this.getFrom();
    from        = new Date(currentFrom.getFullYear(), currentFrom.getMonth()-1, 1);
    to          = new Date(currentFrom.getFullYear(), currentFrom.getMonth(), 0);
    return this.submitWithRound(from, to);
  }

  this.nextMonth = function(enableFuture) {
    currentTo   = this.getTo();
    if(this.compareFuture(enableFuture, currentTo)) {
      return false;
    }
    // Ha a hónap első napja a záródátum, akkor az lesz a nyitó dátum, és nem lépkedünk.
    step        = (currentTo.getDate()==1) ? 0 : 1;
    from        = new Date(currentTo.getFullYear(), currentTo.getMonth()+step, 1);
    to          = new Date(currentTo.getFullYear(), currentTo.getMonth()+step+1, 0);
    return this.submitWithRound(from, to);
  }
  
  this.stepSeconds = function(n) {
    n           = (typeof(n)=='number') ? n : 0;
    currentFrom = this.getFrom();
    from        = new Date(currentFrom.getFullYear(), currentFrom.getMonth(), currentFrom.getDate(), currentFrom.getHours(), currentFrom.getMinutes(), currentFrom.getSeconds()+n);
    currentTo   = this.getTo();
    to          = new Date(currentTo.getFullYear(), currentTo.getMonth(), currentTo.getDate(), currentTo.getHours(), currentTo.getMinutes(), currentTo.getSeconds()+n);
    return this.submit(from, to);
  }

  this.stepDay = function(n) {
    n           = (typeof(n)=='number') ? n : 0;
    currentFrom = this.getFrom();
    from        = new Date(currentFrom.getFullYear(), currentFrom.getMonth(), currentFrom.getDate()+n);
    currentTo   = this.getTo();
    to          = new Date(currentTo.getFullYear(), currentTo.getMonth(), currentTo.getDate()+n);
    return this.submit(from, to);
  }

  this.stepWeek = function(n) {
    n           = (typeof(n)=='number') ? n : 0;
    return this.stepDay(n*7);
  }
}
