﻿var pDayNames = new Array("Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс");

function JctlDate (__id) {
    this.__id = __id+"_d";
    this.__baseid = __id;
    var date_array = new Array();
    date_array = document.getElementById(this.__baseid).value.split(".")
    
    Jctl.widgets[this.__id] = this;
    
    this.__widget = document.createElement('div');
    this.__widget.setAttribute('id', this.__id);
    this.__widget.style.verticalAlign= "bottom";
    this.__widget.parent = this;
    this.__widget.className = 'jctl-date-widget';
    this.__widget.style.display = "inline";
    this.__widget.innerHTML = 'Показать календарь<br>';
    
//    this.__label = document.createElement('div');
//    this.__label.className = 'jctl-date-label';
//    this.__label.innerHTML = 'Показать календарь<br>';
//    this.__widget.appendChild(this.__label);
    
//    this.__text = document.createElement('div');
//    this.__text.className = 'jctl-date-text';
//    this.__widget.appendChild(this.__text);
/**    
    this.__input_year = document.createElement('input');
    this.__input_year.setAttribute('id', this.__id+'-year');
    this.__input_year.setAttribute('name', this.__id+'-year');
    this.__input_year.setAttribute('type', 'hidden');
    this.__widget.appendChild(this.__input_year);
    
    this.__input_month = document.createElement('input');
    this.__input_month.setAttribute('id', this.__id+'-month');
    this.__input_month.setAttribute('name', this.__id+'-month');
    this.__input_month.setAttribute('type', 'hidden');
    this.__widget.appendChild(this.__input_month);
    
    this.__input_day = document.createElement('input');
    this.__input_day.setAttribute('id', this.__id+'-day');
    this.__input_day.setAttribute('name', this.__id+'-day');
    this.__input_day.setAttribute('type', 'hidden');
    this.__widget.appendChild(this.__input_day);
/**/    
    this.__date = new Date();
    if (date_array.length == 3) {
        this.set_year(date_array[2]);
        this.set_month(date_array[1]-1);
        this.set_day(date_array[0]);
    } else {
        this.set_year(1900);
        this.set_month(0);
        this.set_day(1);
    }
    
    this.update_text();
    
    this.__calendar = document.createElement('div');
    this.__calendar.className = 'jctl-date-calendar';
    this.__calendar.style.position = 'absolute';
    this.__calendar.style.zIndex = 11015;
    this.__widget.appendChild(this.__calendar);
    this.__calendar.style.zIndex = 11015;
    
    this.__out_div = document.createElement('div');
    this.__out_div.style.position = 'absolute';
    this.__out_div.style.zIndex = 11014;
//    this.__out_div.style.backgroundColor = "#eeee00";
//    this.__out_div.style.backgroundImage = "url(bg_cl.gif)";
    this.__out_div.style.left = 0;
    this.__out_div.style.top = 0;
    this.__out_div.style.width = '100%';
    this.__out_div.style.height = '100%';
    document.body.appendChild(this.__out_div);
    
    this.__out_div.obj = this;
    this.__out_div.onclick = function () {this.obj.show_calendar(false)};
    
    this.__calendar_months = document.createElement('select');
    this.__calendar_months.setAttribute('id', this.__id+'-calendar-months');
    this.__calendar.appendChild(this.__calendar_months);
    
    for (i=0; i<JctlDate.months.length; i++) {
        this.__calendar_months.options[i] = new Option(JctlDate.months[i], i);
        if (i == this.__date.getMonth()) {
            this.__calendar_months.options[i].selected = true;
        }
    }
    
    this.__calendar_months.obj = this;
    this.__calendar_months.onchange = function () {this.obj.set_month(this.value); this.obj.update_calendar();};
    
    this.__calendar_year = document.createElement('input');
    this.__calendar_year.setAttribute('id', this.__id+'-calendar-year');
    this.__calendar_year.setAttribute('maxlength', 4);
    this.__calendar_year.value = this.__date.getFullYear();
    this.__calendar.appendChild(this.__calendar_year);
    
    this.__calendar_year.obj = this;
    this.__calendar_year.onchange = function () {this.obj.set_year(this.value); this.obj.update_calendar();}
    this.__calendar_year.onkeyup = function () {this.obj.set_year(this.value); this.obj.update_calendar();}
//    this.__calendar_year.onchanging = function () {this.obj.set_year(this.value); this.obj.update_calendar();}
    
    this.__calendar_table = document.createElement('table');
    this.__calendar.appendChild(this.__calendar_table);
    
    this.update_calendar();
    
//    this.__text.obj = this;
//    this.__text.onclick = function () {this.obj.show_calendar();}
    this.__widget.obj = this;
    this.__widget.onclick = function () {this.obj.show_calendar(true);}
    
    this.show_calendar(false);
    
    this.set_day(this.__date.getDate());
    this.set_month(this.__date.getMonth());
    this.set_year(this.__date.getFullYear());
}

JctlDate.prototype = new Jctl();
JctlDate.months = ['январь', 'февраль', 'март', 'апрель', 'май', 'июнь', 'июль', 'август', 'сентябрь', 'октябрь', 'ноябрь', 'декабрь'];

JctlDate.month_days = function (date) {
    var m = date.getMonth();
    if (m == 1 && ((!(date.getFullYear() % 4) && (date.getFullYear() % 100))||(date.getFullYear() % 400 == 0)) ) {
        return 29;
    } else {
        var dm = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        return dm[m];
    }
}

JctlDate.prototype.set_day = function (v) {
    this.__date.setDate(v);
//    this.__input_day.value = v;
    this.update_text();
}

JctlDate.prototype.set_month = function (v) {
    this.__date.setMonth(v);
//    this.__input_month.value = parseInt(v)+1;
    this.update_text();
    //this.update_calendar();
}

JctlDate.prototype.set_year = function (v) {
    this.__date.setFullYear(v);
//    this.__input_year.value = v;
    this.update_text();
    //this.update_calendar();
}

JctlDate.prototype.update_text = function () {
	if (this.__date.getFullYear()==1900) {
    document.getElementById(this.__baseid).value = "";
  } else {
    document.getElementById(this.__baseid).value = this.__date.getDate() + '.' + (this.__date.getMonth()+1) + '.' + this.__date.getFullYear();
  }
}

JctlDate.prototype.show_calendar = function (show) {
    if (show || this.__calendar.style.display == 'none') {
        this.__widget.setAttribute('title', 'Скрыть');
        this.__calendar.style.display = 'block';
        this.__out_div.style.display = 'block';
        this.__widget.className = 'jctl-date-widget-opened';
    } else {
        this.__widget.setAttribute('title', 'Показать');
        this.__calendar.style.display = 'none';
        this.__out_div.style.display = 'none';
        this.__widget.className = 'jctl-date-widget';
    }
}

JctlDate.prototype.set_current_day = function (v) {
    document.getElementById(this.__id+'-td-'+this.__date.getDate()).className = 'normal';
    document.getElementById(this.__id+'-td-'+v).className = 'current';
    this.set_day(v);
}

JctlDate.prototype.update_calendar = function () {
    while (this.__calendar_table.rows.length > 0) {
        this.__calendar_table.deleteRow(0);
    }
    var tmp_date = new Date();
    tmp_date.setFullYear(this.__date.getFullYear());
    tmp_date.setMonth(this.__date.getMonth());
    tmp_date.setDate(1);
    var w = (tmp_date.getDay()+6)%7;
    
    var r = 0;
    var c = 0;
    
    var tr = this.__calendar_table.insertRow(r++);

    for (i=0; i<7; i++) {
        td = tr.insertCell(i);
        td.className = 'void';
        td.innerHTML = pDayNames[i];
    }

    tr = this.__calendar_table.insertRow(r++);
    for (i=0; i<w; i++) {
        td = tr.insertCell(c++%7);
        td.className = 'void';
    }
    
    var d = JctlDate.month_days(tmp_date);
    
    for (i=0; i<d; i++) {
        w = (tmp_date.getDay()+6)%7;
        if (w == 0) {
            tr = this.__calendar_table.insertRow(r++);
        }
        td = tr.insertCell(c++%7);
        td.setAttribute('id', this.__id+'-td-'+tmp_date.getDate());
        td.innerHTML = tmp_date.getDate();
        if (tmp_date.getDate() == this.__date.getDate()) {
            td.className = 'current';
        } else {
            td.className = 'normal';
        }
        td.obj = this;
        td.onclick = function () {this.obj.set_current_day(this.innerHTML);}
        tmp_date.setDate(tmp_date.getDate()+1);
    }
}

