var working_with = null;

function cShowCalendar(work_with) {
    working_with = work_with;
    var inp = $('visible_'+working_with);
    var inpValid = $(working_with);
    var now = new Date();
    var tmp, strDate, strDateValid;

    if (!(/^(\d{2})\.\d{2}\.\d{4}$/.test(inp.value))) {
        tmp = now.getDate();
        if (tmp < 10) {
            tmp = '0'+tmp;
        }
        strDate = tmp+'.';
        strDateValid = '-'+tmp;
        tmp = now.getMonth() + 1;
        if (tmp < 10) {
            tmp = '0'+tmp;
        }
        strDate += tmp+'.';
        strDateValid = '-'+tmp+strDateValid;
        strDate += now.getFullYear();
        strDateValid = now.getFullYear() + strDateValid;
        inp.value = strDate;
        inpValid.value = strDateValid;
    }

    cPrepareSelects();
    cWriteDays();
    $('calendar').style.display = 'block';
}

function cPrepareSelects() {
    if (working_with) {
        var mnth_select = $('c_mnth');
        var year_select = $('c_year');
        var dt_value = $('visible_'+working_with).value;

        var dt = /\d{2}\.(\d{2})\.(\d{4})/.exec(dt_value);

        if (dt) {
            for (i in mnth_select.childNodes) {
                if (mnth_select.childNodes[i].tagName == 'OPTION') {
                    if (mnth_select.childNodes[i].value == dt[1]) {
                        mnth_select.value = mnth_select.childNodes[i].value;
                    }
                }
            }

            for (i in year_select.childNodes) {
                if (year_select.childNodes[i].tagName == 'OPTION') {
                    if (year_select.childNodes[i].value == dt[2]) {
                        year_select.value = year_select.childNodes[i].value;
                    }
                }
            }
        }
    }
}

function cWriteDays(change_date) {
    var now = new Date();
    var days_div = $('c_days');
    var days_num = cCountDays();
    var days = new Array();
    var i, tmp, strDate;
    var dt_value = $('visible_'+working_with).value;

    var dt = /^(\d{2})\.\d{2}\.\d{4}$/.exec(dt_value);

    if (dt && (dt[1] > days_num)) {
        dt[1] = days_num;
    }

    for (j in days_div.childNodes) {
        while ((typeof(days_div.childNodes[j]) != 'undefined') && (days_div.childNodes[j].tagName == 'DIV')) {
            days_div.removeChild(days_div.childNodes[j]);
        }
    }

    var fakeCount = days_num[0] - 1;
    if (fakeCount < 0) {
        fakeCount = 6;
    }
    for (i = 1; i <= fakeCount; i++) {
        days[i] = document.createElement('div');
        days[i].id = 'day_fake_'+i;
        days[i].innerHTML = '&nbsp;';
        days_div.appendChild(days[i]);
    }

    for (i = 1; i <= days_num[1]; i++) {
        days[i] = document.createElement('div');
        days[i].id = 'day_'+i;
        days[i].innerHTML = '<a href="#" onclick="cSelectDay(this.parentNode); return false;">' + i + '</a>';
        days_div.appendChild(days[i]);
    }
    /*
    days['fake'] = document.createElement('div');
    days['fake'].className = 'contest_day default_cursor';
    days['fake'].id = 'day_fake';
    days['fake'].innerHTML = '';
    days_div.appendChild(days['fake']);
    */

    if (change_date) {
        var day_div = $('day_'+(dt[1]-1+1));
        cSelectDay(day_div);
    }
}

function cCountDays() {
    var mnth_select = $('c_mnth');
    var year_select = $('c_year');
    var i = 1;
    var calculated = false;
    var res = new Array();

    if ((typeof(mnth_select) != 'undefined') && (typeof(year_select) != 'undefined') && mnth_select && year_select) {
        var mnth = mnth_select.value - 1;
        var year = year_select.value;
        var dt = new Date(year, mnth, 1);
        var dt_tmp = new Date(year, mnth, 1);

        res[0] = dt.getDay();

        while ((i < 32) && (!calculated)) {
            dt_tmp.setDate(dt.getDate()+i);
            if (dt_tmp.getMonth() == dt.getMonth()) {
                i++;
            } else {
                calculated = true;
            }
        }
    }

    res[1] = i;

    return res;
}

function cSelectDay(day_div) {
    var matches = /day_(\d+)/.exec(day_div.id);
    var mnth_select = $('c_mnth');
    var year_select = $('c_year');
    var dt_visible_input = $('visible_'+working_with);
    var dt_input = $(working_with);

    var day = (matches[1] > 9) ? matches[1] : '0' + matches[1];
    var mnth = Number(mnth_select.value);
    mnth = (mnth > 9) ? mnth : '0' + mnth;
    var year = year_select.value;

    var dt = day + '.' + mnth + '.' + year;

    dt_visible_input.value = dt;
    dt_input.value = year+'-'+mnth+'-'+day;

    cOk();
}

function cOk() {
    $('calendar').style.display = 'none';
}

function cClear(work_with) {
    var inp = $('visible_'+working_with);
    var inpValid = $(working_with);

    inp.value = '';
    inpValid.value = '';
}
