﻿// Everything needed for the javascript popup calendar

function calendarInit() {
    this.calendarTextbox = null;
    this.calendarAttributes = new Object();
    this.calendarContainer = document.getElementById('jscalendarContainer');
    this.calendarObject = document.getElementById('jscalendar');
    this.calendarDays = calendarObject.getElementsByTagName('td');
    this.calendarMonths = calendarObject.getElementsByTagName('select')[0];
    this.calendarYears = calendarObject.getElementsByTagName('select')[1];
    this.calendarLastSelected = calendarDays[0];
    this.calendarSelectedOldColor = ''
    this.today = new Date();
    calendarAttributes.daycurrent = today.getDate();
    calendarAttributes.monthcurrent = today.getMonth() + 1;
    calendarAttributes.yearcurrent = today.getFullYear() + (calendarAttributes.offset == undefined? 0: calendarAttributes.offset);
    calendarAttributes.offset = 3;
    calendarAttributes.showCal = true;
    addEvent(document.body, 'click', clickedBody);
    addEvent(calendarObject, 'click', clickedCal);
    addEvent(calendarMonths, 'change', updatedDropdown);
    addEvent(calendarYears, 'change', updatedDropdown);
    for (var i = 7; i < 49; ++i) {
        addEvent(calendarDays[i], 'mouseover', updatedDay);
        addEvent(calendarDays[i], 'click', clickedCalDay);
    }
}
function calendarSet(object,offset) {
    if (!object) return;
    if (typeof (calendarObject) == 'undefined')
        calendarInit();
    if (offset != undefined)
        calendarAttributes.offset = offset;
    else
        calendarAttributes.offset = 0;
    var i, children = object.childNodes;
    for (i = 0; i < children.length; ++i) {
        if (children[i].value != undefined) {
            calendarAttributes.changedBox = calendarTextbox != children[i];
            calendarTextbox = children[i];
            toggleCalendar();
            return;
        }
    }
    toggleCalendar();
}
function updateDays(offset, daynr, daynrPrev) {
    for (var td, i = 1; i <= 42; ++i) {
        td = calendarDays[i + 6];
        if (i <= offset) {
            td.innerHTML = daynrPrev - offset + i;
            td.style.color = 'gray';
        } else if (i - offset > daynr) {
            td.innerHTML = i - daynr - offset;
            td.style.color = 'gray'
        } else {
            td.innerHTML = i - offset;
            td.style.color = '';
            if (calendarAttributes.daycurrent == i - offset) {
                calendarLastSelected.style.backgroundColor = calendarSelectedOldColor;
                calendarLastSelected = td;
                calendarSelectedOldColor = td.style.backgroundColor;
                td.style.backgroundColor = '#5888C6';
            }
        }
    }
}
function toggleCalendar(show) {
    if (calendarObject.firstChild == null)
        calendarInit();
    if (typeof show != 'boolean')
        show = calendarAttributes.changedBox || calendarObject.style.display == 'none';
    calendarAttributes.showCal = show;
    if (show) {
        if (calendarObject.style.display != 'none' && !calendarAttributes.changedBox)
            return;
        getCurrents();
        update();
    }
    if (calendarAttributes.changedBox)
        calendarAttributes.changedBox = false;
    calendarObject.parentNode.removeChild(calendarObject);
    calendarTextbox.parentNode.appendChild(calendarObject);
    calendarObject.style.display = show ? 'block' : 'none';
    if (!show) {
        calendarObject.parentNode.removeChild(calendarObject);
        calendarContainer.appendChild(calendarObject);
    }
}
function updateTextbox(monthOffset) {
    var dispMonth = calendarAttributes.monthcurrent + (monthOffset || 0),
            dispYear = calendarAttributes.yearcurrent;
    if (dispMonth == 0) {
        dispMonth = 12;
        --dispYear;
    } else if (dispMonth == 13) {
        dispMonth = 1;
        ++dispYear;
    }
    calendarTextbox.value = (calendarAttributes.daycurrent < 10 ? '0' : '') + calendarAttributes.daycurrent + (dispMonth < 10 ? '-0' : '-') + dispMonth + '-' + dispYear;
    calendarTextbox.blur();
}
function updatedDay() {
    calendarLastSelected.style.backgroundColor = calendarSelectedOldColor;
    calendarLastSelected = this;
    calendarSelectedOldColor = this.style.backgroundColor;
    this.style.backgroundColor = '#5888C6';
    var newday = parseInt(this.innerHTML, 10);
    calendarAttributes.daycurrent = newday;
    updateTextbox((this.style.color == 'gray') ? (2 * (newday < 15) - 1) : 0);
}
function updatedDropdown() {
    calendarAttributes.monthcurrent = parseInt(calendarMonths.value, 10);
    calendarAttributes.yearcurrent = calendarAttributes.yearcurrent - parseInt(calendarYears.value, 10);
    update()
    updateTextbox();
}
function update() {
    fillYears();
    calendarMonths.childNodes[calendarAttributes.monthcurrent].selected = true;
    var prevMonth, todayCell, date = new Date(calendarAttributes.monthcurrent + '/1/' + calendarAttributes.yearcurrent), offset = (date.getDay() + 6) % 7;
    date.setDate(0);
    prevMonth = date.getDate();
    date.setDate(1);
    date.setMonth(calendarAttributes.monthcurrent);
    date.setDate(0);
    updateDays(offset, date.getDate(), prevMonth);
    if (calendarAttributes.monthcurrent == today.getMonth() + 1 && calendarAttributes.yearcurrent == today.getFullYear()) {
        calendarAttributes.todayCell = offset + 6 + today.getDate();
        todayCell = calendarDays[calendarAttributes.todayCell]
        todayCell.style.backgroundColor = '#F07F3D';
        todayCell.style.fontWeight = 'bold';
        if (todayCell == calendarLastSelected)
            calendarSelectedOldColor = todayCell.style.backgroundColor;
    } else if (calendarAttributes.todayCell) {
        calendarDays[calendarAttributes.todayCell].style.backgroundColor = '';
        calendarDays[calendarAttributes.todayCell].style.fontWeight = '';
    }
}
function getCurrents() {
    var matches, date, textDate, useNew = true;
    if (typeof calendarTextbox != 'undefined' && (textDate = calendarTextbox.value)) {
        if (matches = textDate.match(/^([0-9]+)(.)([0-9]+)\2([0-9]+)$/))
            textDate = matches[3] + '/' + matches[1] + '/' + matches[4];
        date = new Date(textDate);
        if (isNaN(date)) {
            useNew = false;
        }
    } else
        useNew = false;
    if (useNew) {
        calendarAttributes.daycurrent = date.getDate();
        calendarAttributes.monthcurrent = date.getMonth() + 1;
        calendarAttributes.yearcurrent = date.getFullYear();
    } else {
        calendarAttributes.daycurrent = today.getDate();
        calendarAttributes.monthcurrent = today.getMonth() + 1;
        calendarAttributes.yearcurrent = today.getFullYear() + (calendarAttributes.offset == undefined? 0: calendarAttributes.offset);
    }
    updateTextbox();
}
function fillYears() {
    var option, range = 100,
        offset = calendarAttributes.yearcurrent-today.getFullYear();
    while (calendarYears.hasChildNodes()) {
        calendarYears.removeChild(calendarYears.firstChild);
    }
    for (var i = offset+range; i >= offset-10; --i) {
        option = document.createElement('option');
        option.value = i;
        option.innerHTML = calendarAttributes.yearcurrent - i;
        calendarYears.appendChild(option);
    }
    calendarYears.childNodes[range+offset].selected = true;
}
function clickedBody() {
    if (typeof (calendarObject) == 'undefined' || calendarObject.style.display == 'none') return;
    if (calendarAttributes.showCal) {
        calendarAttributes.showCal = false;
        return;
    }
    toggleCalendar(false);
}
function clickedCal() { calendarAttributes.showCal = true; }
function clickedCalDay() {
    calendarAttributes.showCal = false;
    toggleCalendar(false);
    if (this.style.color == 'gray')
        calendarLastSelected.style.backgroundColor = calendarSelectedOldColor;
}
