// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
// modified: Pavel Polythov <skillz@fromru.com>
//
// This methods only to override original
// ===================================================================


var select_day_calendar = new CalendarPopup();
select_day_calendar.setReturnFunction("setMultipleCalendarValues");
select_day_calendar.showYearNavigation();
select_day_calendar.type = 'DIV';
select_day_calendar.setMonthNames('Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь');
select_day_calendar.setDayHeaders('Вс','Пн','Вт','Ср','Чт','Пт','Сб');
select_day_calendar.setWeekStartDay(1);
select_day_calendar.setTodayText("Сегодня");

function setMultipleCalendarValues(y,m,d)
{
  var month = m < 10 ?  "0" + m: m;

  document.calendar.year.value=y;
  document.calendar.month.value=month;
  document.calendar.day.value=d;
}

// write calendar to hardcoded layer/div
function layerWrite(text) {
  var divid = 'caldiv';
  if (document.getElementById) {
    x = document.getElementById(divid);
    x.innerHTML = '';
    x.innerHTML = text;
  }
  else if (document.all) {
    x = document.all[divid];
    x.innerHTML = text;
  }
  else if (document.layers) {
    x = document.layers[divid];
    text2 = '<P class=calclass>' + text + '</P>';
    x.document.open();
    x.document.write(text2);
    x.document.close();
  }
}

// Overriden: Refresh the contents of the calendar display
function CalendarPopup_refreshCalendar(index) {
  if (arguments.length>1) {
    layerWrite(select_day_calendar.getCalendar(arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]));
  }
  else {
    layerWrite(select_day_calendar.getCalendar());
  }
}


// Overriden: Populate the calendar and display it
function CalendarPopup_showCalendar(anchorname) {
  layerWrite(select_day_calendar.getCalendar());
}


// Overriden: Return a string containing all the calendar code to be displayed
function CalendarPopup_getCalendar() {
  var now = new Date();
  // Reference to window
  if (this.type == "WINDOW") { var windowref = "window.opener."; }
  else { var windowref = ""; }
  var result = "";
  // If POPUP, write entire HTML document
  if (this.type == "WINDOW") {
    result += "<HTML><HEAD><TITLE>Calendar</TITLE>"+this.getStyles()+"</HEAD><BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0>\n";
    result += '<CENTER><TABLE WIDTH=100% BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>\n';
  }
  else {
    //result += '<TABLE width=100% border=0 cellspacing=0 cellpadding=0>\n';
    //result += '<TR><TD align=center>\n';
    //result += '<CENTER>\n';
  }

  // Code for DATE display (default)
  // -------------------------------
  if (this.displayType=="date" || this.displayType=="week-end") {
    if (this.currentDate==null) { this.currentDate = now; }
    if (arguments.length > 0) { var month = arguments[0]; }
      else { var month = this.currentDate.getMonth()+1; }
    if (arguments.length > 1) { var year = arguments[1]; }
      else { var year = this.currentDate.getFullYear(); }
    var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
    if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) {
      daysinmonth[2] = 29;
    }
    var current_month = new Date(year,month-1,1);
    var display_year = year;
    var display_month = month;
    var display_date = 1;
    var weekday= current_month.getDay();
    var offset = 0;
    if (weekday >= this.weekStartDay) {
      offset = weekday - this.weekStartDay;
    }
    else {
      offset = 7-this.weekStartDay+weekday;
    }
    if (offset > 0) {
      display_month--;
      if (display_month < 1) { display_month = 12; display_year--; }
      display_date = daysinmonth[display_month]-offset+1;
    }
    var next_month = month+1;
    var next_month_year = year;
    if (next_month > 12) { next_month=1; next_month_year++; }
    var last_month = month-1;
    var last_month_year = year;
    if (last_month < 1) { last_month=12; last_month_year--; }
    var date_class;
    if (this.type!="WINDOW") {
         result += '<TABLE width=100% border=0 borderwidth=0 cellspacing=5 cellpadding=3>\n';
    }
    var refresh = 'javascript:'+windowref+'CalendarPopup_refreshCalendar';
    if (this.isShowYearNavigation) {
                    result += '<TR>\n';
                    result += '  <TD align=center valign=middle width=10><A href="'+refresh+'('+this.index+','+month+','+(year-1)+');"><img src=/images/calendar_left.gif width=24 height=13 border=0 class=nob></A></TD>\n';
                    result += '  <TD bgcolor=#c0cae3 align=center valign=middle class=td_year>'+year+'</TD>\n';
                    result += '  <TD align=center valign=middle width=10><A href="'+refresh+'('+this.index+','+month+','+(year+1)+');"><img src=/images/calendar_right.gif width=24 height=13 border=0 class=nob></A></TD>\n';
                    result += '</TR>\n';
                    result += '<TR>\n';
                    result += '  <TD align=center valign=middle width=10><A href="'+refresh+'('+this.index+','+last_month+','+last_month_year+');"><img src=/images/calendar_left.gif width=24 height=13 border=0 class=nob></A></TD>\n';
                    result += '  <TD bgcolor="#c0cae3" align="center" valign="middle" class=td_month>'+this.monthNames[month-1]+'</TD>\n';
                    result += '  <TD align=center valign=middle width=10><A href="'+refresh+'('+this.index+','+next_month+','+next_month_year+');"><img src=/images/calendar_right.gif width=24 height=13 border=0 class=nob></A></TD>\n';
                    result += '</TR>\n';
    }
    else {
            result += '<TR BGCOLOR="#C0C0C0">\n';
        result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="'+refresh+'('+this.index+','+last_month+','+last_month_year+');">&lt;&lt;</A></B></TD>\n';
        result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=100 ALIGN=CENTER>'+this.monthNames[month-1]+' '+year+'</TD>\n';
        result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="'+refresh+'('+this.index+','+next_month+','+next_month_year+');">&gt;&gt;</A></B></TD>\n';
                    result += '</TR>';
    }
    result += '</TABLE><BR>\n';

    result += '<TABLE width=100% border=0 cellspacing=1 cellpadding=1 align=center>\n';
    result += '<TR valign=middle bgcolor=#c0cae3>\n';
    var td = '  <TD align=right class=week_day>';
    for (var j=0; j<7; j++) {
      result += td+this.dayHeaders[(this.weekStartDay+j)%7]+'</TD>\n';
    }
    result += '</TR>\n';
    //result += '<TR><TD COLSPAN=7 ALIGN=CENTER><IMG SRC="graypixel.gif" WIDTH=120 HEIGHT=1></TD></TR>\n';
    for (var row=1; row<=6; row++) {
      result += '<TR align=right valign=middle>\n';
      for (var col=1; col<=7; col++) {
        if (display_month == month) {
          date_class = "calthismonth";
        }
        else {
          date_class = "calothermonth";
        }
        if ((display_month == this.currentDate.getMonth()+1) && (display_date==this.currentDate.getDate()) && (display_year==this.currentDate.getFullYear())) {
          td_class="caltoday";
        }
        else {
          td_class="calmonth";
        }
        if (this.disabledWeekDays[col-1]) {
          date_class="calnotclickable";
          result += ' <TD CLASS="'+td_class+'"><SPAN CLASS="'+date_class+'">'+display_date+'</SPAN></TD>\n';
        }
        else {
          var selected_date = display_date;
          var selected_month = display_month;
          var selected_year = display_year;
          if (this.displayType=="week-end") {
            var d = new Date(selected_year,selected_month-1,selected_date,0,0,0,0);
            d.setDate(d.getDate() + (7-col));
            selected_year = d.getYear();
            if (selected_year < 1000) { selected_year += 1900; }
            selected_month = d.getMonth()+1;
            selected_date = d.getDate();
          }
          result += ' <TD CLASS="'+td_class+'"><A HREF="javascript:'+windowref+this.returnFunction+'('+selected_year+','+selected_month+','+selected_date+');" CLASS="'+date_class+'">'+display_date+'</A></TD>\n';
        }
        display_date++;
        if (display_date > daysinmonth[display_month]) {
          display_date=1;
          display_month++;
        }
        if (display_month > 12) {
          display_month=1;
          display_year++;
        }
      }
      result += '</TR>';
    }
    var current_weekday = now.getDay();
    //result += '<TR><TD COLSPAN=7 ALIGN=CENTER><IMG SRC="graypixel.gif" WIDTH=120 HEIGHT=1></TD></TR>\n';
    result += '<TR>\n';
    result += ' <TD colspan=7 align=center valign=middle>\n';
    if (this.disabledWeekDays[current_weekday+1]) {
      result += '   <SPAN CLASS="but_today">'+this.todayText+'</SPAN>\n';
    }
    else {
      result += '   <A CLASS="but_today" HREF="javascript:'+windowref+this.returnFunction+'(\''+now.getFullYear()+'\',\''+(now.getMonth()+1)+'\',\''+now.getDate()+'\');'+windowref+'CalendarPopup_refreshCalendar('+this.index+','+(now.getMonth()+1)+','+now.getFullYear()+');">'+this.todayText+'</A>\n';
    }
    result += '   <BR>\n';
    result += ' </TD></TR></TABLE>\n';
    //result += '</CENTER></TD></TR></TABLE>\n';
  }

  // Code common for MONTH, QUARTER, YEAR
  // ------------------------------------
  if (this.displayType=="month" || this.displayType=="quarter" || this.displayType=="year") {
    if (arguments.length > 0) { var year = arguments[0]; }
    else {
      if (this.displayType=="year") { var year = now.getFullYear()-this.yearSelectStartOffset; }
      else { var year = now.getFullYear(); }
    }
    if (this.displayType!="year" && this.isShowYearNavigation) {
      result += '<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>\n';
      result += '<TR BGCOLOR="#C0C0C0">\n';
      result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'CalendarPopup_refreshCalendar('+this.index+','+(year-1)+');">&lt;&lt;</A></B></TD>\n';
      result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=100 ALIGN=CENTER>'+year+'</TD>\n';
      result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'CalendarPopup_refreshCalendar('+this.index+','+(year+1)+');">&gt;&gt;</A></B></TD>\n';
      result += '</TR></TABLE>\n';
    }
  }

  // Code for MONTH display (default)
  // -------------------------------
  if (this.displayType=="month") {
    // If POPUP, write entire HTML document
    result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>\n';
    for (var i=0; i<4; i++) {
      result += '<TR>';
      for (var j=0; j<3; j++) {
        var monthindex = ((i*3)+j);
        result += '<TD WIDTH=33% ALIGN=CENTER><A CLASS="textlink" HREF="javascript:'+windowref+this.returnMonthFunction+'('+year+','+(monthindex+1)+');" CLASS="'+date_class+'">'+this.monthAbbreviations[monthindex]+'</A></TD>';
      }
      result += '</TR>';
    }
    result += '</TABLE></CENTER></TD></TR></TABLE>\n';
  }

  // Code for QUARTER display (default)
  // ----------------------------------
  if (this.displayType=="quarter") {
    result += '<BR><TABLE WIDTH=120 BORDER=1 CELLSPACING=0 CELLPADDING=0 ALIGN=CENTER>\n';
    for (var i=0; i<2; i++) {
      result += '<TR>';
      for (var j=0; j<2; j++) {
        var quarter = ((i*2)+j+1);
        result += '<TD WIDTH=50% ALIGN=CENTER><BR><A CLASS="textlink" HREF="javascript:'+windowref+this.returnQuarterFunction+'('+year+','+quarter+');" CLASS="'+date_class+'">Q'+quarter+'</A><BR><BR></TD>';
      }
      result += '</TR>';
    }
    result += '</TABLE></CENTER></TD></TR></TABLE>\n';
  }

  // Code for YEAR display (default)
  // -------------------------------
  if (this.displayType=="year") {
    var yearColumnSize = 4;
    result += '<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>\n';
    result += '<TR BGCOLOR="#C0C0C0">\n';
    result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=50% ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'CalendarPopup_refreshCalendar('+this.index+','+(year-(yearColumnSize*2))+');">&lt;&lt;</A></B></TD>\n';
    result += ' <TD BGCOLOR="#C0C0C0" CLASS="cal" WIDTH=50% ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'CalendarPopup_refreshCalendar('+this.index+','+(year+(yearColumnSize*2))+');">&gt;&gt;</A></B></TD>\n';
    result += '</TR></TABLE>\n';
    result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>\n';
    for (var i=0; i<yearColumnSize; i++) {
      for (var j=0; j<2; j++) {
        var currentyear = year+(j*yearColumnSize)+i;
        result += '<TD WIDTH=50% ALIGN=CENTER><A CLASS="textlink" HREF="javascript:'+windowref+this.returnYearFunction+'('+currentyear+');" CLASS="'+date_class+'">'+currentyear+'</A></TD>';
      }
      result += '</TR>';
    }
    result += '</TABLE></CENTER></TD></TR></TABLE>\n';
  }
  // Common
  if (this.type == "WINDOW") {
    result += "</BODY></HTML>\n";
  }
  return result;
}

