﻿function applyCalendar(pstrTextBoxID, pstrCulture) {
    var txtCalendar = document.getElementById(pstrTextBoxID);

    if (!txtCalendar) { return; }
    
    var objParent = txtCalendar.parentNode;
    var objBrother = txtCalendar.nextSibling;
    var btnCalendar = document.createElement("A");
    var imgCalendar = document.createElement("IMG");
    var divCalendar = document.createElement("DIV");
    
    if (document.all) {
        txtCalendar.onkeypress = function() {
            return verifyCalendarMask(event, this);
        };
    } else {
        txtCalendar.onkeypress = function(event) {
            return verifyCalendarMask(event, this);
        };
    }
    
    txtCalendar.maxLength = 10;
    
    txtCalendar.onfocus = function() {
        divCalendar.style.visibility = "hidden";
        showSelectElements("divGeral");
    }
    
    txtCalendar.onclick = function() {
        divCalendar.style.visibility = "hidden";
        showSelectElements("divGeral");
    }
    
    txtCalendar.onblur = function() {
        divCalendar.style.visibility = "hidden";
        showSelectElements("divGeral");
        return validateDate(this);
    }
    
    txtCalendar.style.marginRight = "5px";
    
    btnCalendar.href = "#";
    btnCalendar.className = "calendarlink";
    btnCalendar.onmouseover = function() { window.status = ""; };
    btnCalendar.onmouseout = function() { window.status = ""; };

    btnCalendar.onclick = function() {
        if (divCalendar.style.visibility == "visible") {
            divCalendar.style.visibility = "hidden";
           // showSelectElements("divGeral");
        } else {
           // hideSelectElements("divGeral");
            buildCalendar(createDate(txtCalendar.value), divCalendar, pstrCulture, 'D');
            divCalendar.style.visibility = "visible";
        }
        return false;
    };
    
    switch (pstrCulture) {
        case "pt-BR":
            imgCalendar.alt = "Exibir Calendário";
            break;
        case "es-ES":
            imgCalendar.alt = "Ver Calendario";
            break;
        default:
            imgCalendar.alt = "Show Calendar";
            break;
    }

    imgCalendar.title = imgCalendar.alt;
    imgCalendar.src = "imagens/icon_calendar.gif";
    imgCalendar.className = "calendarimage";
    
    btnCalendar.appendChild(imgCalendar);

    if (objBrother) {
        objParent.insertBefore(btnCalendar, objBrother);
    } else {
        objParent.appendChild(btnCalendar);
    }

    divCalendar.id = txtCalendar.id + "_divCalendar";
    divCalendar.className = "calendar";
   
    objParent.insertBefore(divCalendar, btnCalendar);
    
//    if (document.all) {
//        divCalendar.style.marginTop = "25px";
//        divCalendar.style.marginRight = "0px";
//        divCalendar.style.marginBottom = "0px";
//        divCalendar.style.marginLeft = "-167px";
//    } else {
//        divCalendar.style.marginTop = "5px";
//        divCalendar.style.marginRight = "0px";
//        divCalendar.style.marginBottom = "0px";
//        divCalendar.style.marginLeft = "0px";
//    }
    
    if (!document.all) {
        divCalendar.style.marginTop = "5px";
        divCalendar.style.marginRight = "0px";
        divCalendar.style.marginBottom = "0px";
        divCalendar.style.marginLeft = "0px";
    }
}

function buildCalendar(pobjDate, pobjDIV, pstrCulture, pchrBehaviorOpt) {
    switch (pchrBehaviorOpt) {
        default:
        case 'D':
            buildDayCalendar(pobjDate, pobjDIV, pstrCulture)
            break;
        case 'M':
            buildMonthCalendar(pobjDate, pobjDIV, pstrCulture)
            break;
        case 'Y':
            buildYearCalendar(pobjDate, pobjDIV, pstrCulture)
            break;
    }
}

function buildDayCalendar(pobjDate, pobjDIV, pstrCulture) {
    var objTable    = document.createElement("TABLE");
    var objTHead    = document.createElement("THEAD");
    var objTBody    = document.createElement("TBODY");
    var objTFoot    = document.createElement("TFOOT");
    var txtCalendar = document.getElementById(pobjDIV.id.replace("_divCalendar", ""));
    
    var objNewRow       = null;
    var objNewCol       = null;
    var arrCells        = null;
    var dttToday        = new Date();
    var dttSelectedDate = null;
    var intDayCount     = 0;
    var intLastDay      = 30;

    if (!pobjDate) {
        pobjDate = dttToday;
    }

    dttSelectedDate = createDate(txtCalendar.value);
    pobjDate = new Date(pobjDate.getFullYear(), pobjDate.getMonth(), 1);

    while (pobjDIV.hasChildNodes()) {
        pobjDIV.removeChild(pobjDIV.lastChild);
    }

    objTHead.className = "calendarheader";
    objTBody.className = "calendarbody";
    objTFoot.className = "calendarfooter";

    objTable.appendChild(objTHead);
    objTable.appendChild(objTBody);
    objTable.appendChild(objTFoot);
    objTable.className = "calendartable";
    objTable.cellSpacing = "0px";
    objTable.cellPadding = "0px";

    pobjDIV.appendChild(objTable);

    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarheaderrow";
    objTHead.appendChild(objNewRow);

    objNewCol = document.createElement("TH");
    objNewCol.className = "leftarrow";

    switch (pstrCulture) {
        case "pt-BR":
            objNewCol.title = "Mês Anterior";
            break;
        case "es-ES":
            objNewCol.title = "Mes Anterior";
            break;
        default:
            objNewCol.title = "Previous Month";
            break;
    }

    objNewRow.appendChild(objNewCol);
    
    if (pobjDate.getMonth() == 0) {
        objNewCol.onclick = function() {
            if (pobjDate.getFullYear() <= 1900) { return; }
            buildCalendar(new Date(pobjDate.getFullYear() - 1, 11, 1), pobjDIV, pstrCulture, 'D');
        };
    } else {
        objNewCol.onclick = function() {
            buildCalendar(new Date(pobjDate.getFullYear(), pobjDate.getMonth() - 1, 1), pobjDIV, pstrCulture, 'D');
        };
    }

    objNewCol = document.createElement("TH");
    objNewCol.colSpan = "3";
    objNewCol.className = "month";
    objNewCol.innerHTML = getMonthName(pobjDate.getMonth(), pstrCulture);
    objNewRow.appendChild(objNewCol);
    
    objNewCol.onclick = function() {
        buildCalendar(pobjDate, pobjDIV, pstrCulture, 'M');
    };
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };

    objNewCol = document.createElement("TH");
    objNewCol.colSpan = "2";
    objNewCol.className = "year";
    objNewCol.innerHTML = pobjDate.getFullYear().toString();
    objNewRow.appendChild(objNewCol);
    
    objNewCol.onclick = function() {
        buildCalendar(pobjDate, pobjDIV, pstrCulture, 'Y');
    };
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };

    objNewCol = document.createElement("TH");
    objNewCol.className = "rightarrow";

    switch (pstrCulture) {
        case "pt-BR":
            objNewCol.title = "Próximo Mês";
            break;
        case "es-ES":
            objNewCol.title = "Próximo Mes";
            break;
        default:
            objNewCol.title = "Next Month";
            break;
    }

    objNewRow.appendChild(objNewCol);

    if (pobjDate.getMonth() == 11) {
        objNewCol.onclick = function() {
            if (pobjDate.getFullYear() >= 2100) { return; }
            buildCalendar(new Date(pobjDate.getFullYear() + 1, 0, 1), pobjDIV, pstrCulture, 'D');
        };
    } else {
        objNewCol.onclick = function() {
            buildCalendar(new Date(pobjDate.getFullYear(), pobjDate.getMonth() + 1, 1), pobjDIV, pstrCulture, 'D');
        };
    }

    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarheaderrow";
    objTHead.appendChild(objNewRow);

    objNewCol = document.createElement("TH");
    objNewCol.innerHTML = getWeekDayName(0, pstrCulture).substr(0, 1);
    objNewCol.className = "weekday";
    objNewRow.appendChild(objNewCol);

    objNewCol = document.createElement("TH");
    objNewCol.className = "weekday";
    objNewCol.innerHTML = getWeekDayName(1, pstrCulture).substr(0, 1);
    objNewRow.appendChild(objNewCol);

    objNewCol = document.createElement("TH");
    objNewCol.className = "weekday";
    objNewCol.innerHTML = getWeekDayName(2, pstrCulture).substr(0, 1);
    objNewRow.appendChild(objNewCol);

    objNewCol = document.createElement("TH");
    objNewCol.className = "weekday";
    objNewCol.innerHTML = getWeekDayName(3, pstrCulture).substr(0, 1);
    objNewRow.appendChild(objNewCol);

    objNewCol = document.createElement("TH");
    objNewCol.className = "weekday";
    objNewCol.innerHTML = getWeekDayName(4, pstrCulture).substr(0, 1);
    objNewRow.appendChild(objNewCol);

    objNewCol = document.createElement("TH");
    objNewCol.className = "weekday";
    objNewCol.innerHTML = getWeekDayName(5, pstrCulture).substr(0, 1);
    objNewRow.appendChild(objNewCol);

    objNewCol = document.createElement("TH");
    objNewCol.className = "weekday";
    objNewCol.innerHTML = getWeekDayName(6, pstrCulture).substr(0, 1);
    objNewRow.appendChild(objNewCol);

    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarfooterrow";
    objTFoot.appendChild(objNewRow);

    objNewCol = document.createElement("TD");
    objNewCol.className = "gotoday";
    objNewCol.colSpan = 7;
    
    switch (pstrCulture) {
        case "pt-BR":
            objNewCol.innerHTML = "Hoje: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + ",<BR />" +
                dttToday.getDate().toString() + " de " +
                getMonthName(dttToday.getMonth(), pstrCulture) + " de " +
                dttToday.getFullYear().toString();
            break;
        case "es-ES":
            objNewCol.innerHTML = "Hoy: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + "<BR />" +
                dttToday.getDate().toString() + " de " +
                getMonthName(dttToday.getMonth(), pstrCulture) + " de " +
                dttToday.getFullYear().toString();
            break;
        default:
            objNewCol.innerHTML = "Today: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + ",<BR />" +
                getMonthName(dttToday.getMonth(), pstrCulture) + " " +
                dttToday.getDate().toString() + ", " +                
                dttToday.getFullYear().toString();
            break;
    }
    
    objNewRow.appendChild(objNewCol);

    objNewCol.onclick = function() {
        txtCalendar.value = (dttToday.getDate() < 10 ?
            "0" + dttToday.getDate().toString() : dttToday.getDate().toString()) +
            "/" + (dttToday.getMonth() < 9 ? "0" : "") +
            (dttToday.getMonth() + 1).toString() + "/" + dttToday.getFullYear();
        pobjDIV.style.visibility = "hidden";
        showSelectElements("divGeral");
    };
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };

    for (var i = 0; i < 6; i++) {
        objNewRow = document.createElement("TR");
        objNewRow.className = "calendarrow";
        objTBody.appendChild(objNewRow);

        for (var j = 0; j < 7; j++) {
            objNewCol = document.createElement("TD");
            objNewCol.className = "monthday";
            if (j == 0 || j == 6) {
                objNewCol.className = "weekend_monthday";
            }
            objNewCol.innerHTML = "&nbsp;";
            objNewRow.appendChild(objNewCol);
        }
    }

    arrCells = objTBody.getElementsByTagName("TD");

    if ("-0-2-4-6-7-9-11-".indexOf("-" + pobjDate.getMonth().toString() + "-") >= 0) {
        intLastDay = 31;
    } else if (pobjDate.getMonth() == 1) {
        intLastDay = (pobjDate.getFullYear() % 4 != 0) ? 28 : 29;
    }

    for (var i = pobjDate.getDay(); intDayCount < intLastDay; i++) {
        intDayCount++;
        try {
            arrCells[i].innerHTML = intDayCount.toString();
        } catch(e) {
            arrCells[i].innerText = intDayCount.toString();
        }
        
        if (intDayCount == dttToday.getDate() && pobjDate.getMonth() == dttToday.getMonth() &&
            pobjDate.getFullYear() == dttToday.getFullYear()) {
            arrCells[i].className = arrCells[i].className.replace("monthday", "today");
        }

        arrCells[i].onmouseover = function() {
            this.className = this.className + "_over";
        };

        arrCells[i].onmouseout = function() {
            this.className = this.className.replace("_over", "");
        };

        arrCells[i].onclick = function() {
            pobjDIV.style.visibility = "hidden";
            showSelectElements("divGeral");
            txtCalendar.value = (this.innerHTML.length == 1 ? "0" + this.innerHTML : this.innerHTML) +
                "/" + (pobjDate.getMonth() < 9 ? "0" : "") +
                (pobjDate.getMonth() + 1).toString() + "/" + pobjDate.getFullYear();
            txtCalendar.focus();
        };

        if (dttSelectedDate == null) {
            continue;
        }

        if (intDayCount == dttSelectedDate.getDate() && pobjDate.getMonth() == dttSelectedDate.getMonth() &&
            pobjDate.getFullYear() == dttSelectedDate.getFullYear()) {
            arrCells[i].className = arrCells[i].className + "_selected";
        }
    }
}

function buildMonthCalendar(pobjDate, pobjDIV, pstrCulture) {
    var objTable    = document.createElement("TABLE");
    var objTHead    = document.createElement("THEAD");
    var objTBody    = document.createElement("TBODY");
    var objTFoot    = document.createElement("TFOOT");
    var txtCalendar = document.getElementById(pobjDIV.id.replace("_divCalendar", ""));
    
    var objNewRow     = null;
    var objNewCol     = null;
    var arrCells      = null;
    var dttToday      = new Date();
    var intMonthCount = 0;

    if (!pobjDate) {
        pobjDate = dttToday;
    }

    pobjDate = new Date(pobjDate.getFullYear(), pobjDate.getMonth(), 1);

    while (pobjDIV.hasChildNodes()) {
        pobjDIV.removeChild(pobjDIV.lastChild);
    }

    objTHead.className = "calendarheader";
    objTBody.className = "calendarbody";
    objTFoot.className = "calendarfooter";

    objTable.appendChild(objTHead);
    objTable.appendChild(objTBody);
    objTable.appendChild(objTFoot);
    objTable.className = "calendartable";
    objTable.cellSpacing = "0px";
    objTable.cellPadding = "0px";

    pobjDIV.appendChild(objTable);
    
    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarheaderrow";
    objTHead.appendChild(objNewRow);

    objNewCol = document.createElement("TH");
    objNewCol.className = "emptyheadercell";
    objNewRow.appendChild(objNewCol);
    
    objNewCol = document.createElement("TH");
    objNewCol.colSpan = "3";
    objNewCol.className = "month";
    objNewCol.innerHTML = getMonthName(pobjDate.getMonth(), pstrCulture);
    objNewRow.appendChild(objNewCol);
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };
    
    objNewCol = document.createElement("TH");
    objNewCol.colSpan = "2";
    objNewCol.className = "year";
    objNewCol.innerHTML = pobjDate.getFullYear().toString();
    objNewRow.appendChild(objNewCol);
    
    objNewCol.onclick = function() {
        buildCalendar(pobjDate, pobjDIV, pstrCulture, 'Y');
    };
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };

    objNewCol = document.createElement("TH");
    objNewCol.className = "emptyheadercell";
    objNewRow.appendChild(objNewCol);
    
    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarheaderrow";
    objTHead.appendChild(objNewRow);

    for (var i = 0; i < 7; i++) {
        objNewCol = document.createElement("TH");
        objNewCol.className = "weekday";
        objNewCol.innerHTML = "&nbsp;";
        objNewRow.appendChild(objNewCol);
    }
    
    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarfooterrow";
    objTFoot.appendChild(objNewRow);

    objNewCol = document.createElement("TD");
    objNewCol.className = "gotoday";
    objNewCol.colSpan = 7;
    
    switch (pstrCulture) {
        case "pt-BR":
            objNewCol.innerHTML = "Hoje: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + ",<BR />" +
                dttToday.getDate().toString() + " de " +
                getMonthName(dttToday.getMonth(), pstrCulture) + " de " +
                dttToday.getFullYear().toString();
            break;
        case "es-ES":
            objNewCol.innerHTML = "Hoy: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + "<BR />" +
                dttToday.getDate().toString() + " de " +
                getMonthName(dttToday.getMonth(), pstrCulture) + " de " +
                dttToday.getFullYear().toString();
            break;
        default:
            objNewCol.innerHTML = "Today: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + ",<BR />" +
                getMonthName(dttToday.getMonth(), pstrCulture) + " " +
                dttToday.getDate().toString() + ", " +                
                dttToday.getFullYear().toString();
            break;
    }
    
    objNewRow.appendChild(objNewCol);

    objNewCol.onclick = function() {
        txtCalendar.value = (dttToday.getDate() < 10 ?
            "0" + dttToday.getDate().toString() : dttToday.getDate().toString()) +
            "/" + (dttToday.getMonth() < 9 ? "0" : "") +
            (dttToday.getMonth() + 1).toString() + "/" + dttToday.getFullYear();
        pobjDIV.style.visibility = "hidden";
        showSelectElements("divGeral");
    };
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };
    
    objNewRow = document.createElement("TR");
    objTBody.appendChild(objNewRow);
    
    objNewCol = document.createElement("TD");
    objNewCol.colSpan = 7;
    objNewRow.appendChild(objNewCol);
    
    objTable = document.createElement("TABLE");
    objTBody = document.createElement("TBODY");
    
    objTable.className = "calendarmonthtable";
    
    objNewCol.appendChild(objTable);
    objTable.appendChild(objTBody);
    
    for (var i = 0; i < 3; i++) {
        objNewRow = document.createElement("TR");
        objNewRow.className = "calendarmonthrow";
        objTBody.appendChild(objNewRow);
        for (var j = 0; j < 4; j++) {
            objNewCol = document.createElement("TD");
            if (intMonthCount == dttToday.getMonth()) {
                objNewCol.className = "monthnow";
            } else {
                objNewCol.className = "month";
            }
            objNewRow.appendChild(objNewCol);
            
            try {
                objNewCol.innerHTML = getMonthName(intMonthCount, pstrCulture).substr(0, 3);
            } catch(e) {
                objNewCol.innerText = getMonthName(intMonthCount, pstrCulture).substr(0, 3);
            }

            objNewCol.monthIndex = intMonthCount;
            
            objNewCol.onmouseover = function() {
                this.className += "_over";
            };
            
            objNewCol.onmouseout = function() {
                this.className = this.className.replace("_over", "");
            };
            
            objNewCol.onclick = function() {
                buildCalendar(new Date(pobjDate.getFullYear(), this.monthIndex, 1),
                    pobjDIV, pstrCulture, 'D');
            };
            
            if (intMonthCount == pobjDate.getMonth()) {
                objNewCol.className += "_selected";
            }
            
            intMonthCount ++;
        }
    }
}

function buildYearCalendar(pobjDate, pobjDIV, pstrCulture) {
    var objTable    = document.createElement("TABLE");
    var objTHead    = document.createElement("THEAD");
    var objTBody    = document.createElement("TBODY");
    var objTFoot    = document.createElement("TFOOT");
    var txtCalendar = document.getElementById(pobjDIV.id.replace("_divCalendar", ""));
    
    var objNewRow       = null;
    var objNewCol       = null;
    var arrCells        = null;
    var dttToday        = new Date();
    var intSelectedYear = 0;
    var intYearCount    = 0;

    if (!pobjDate) {
        pobjDate = dttToday;
    }
    
    intSelectedYear = pobjDate.getFullYear();
    
    intYearCount = intSelectedYear;
    
    while ((intYearCount % 10) > 0) {
        intYearCount--;
    }

    pobjDate = new Date(intYearCount, pobjDate.getMonth(), 1);

    while (pobjDIV.hasChildNodes()) {
        pobjDIV.removeChild(pobjDIV.lastChild);
    }

    objTHead.className = "calendarheader";
    objTBody.className = "calendarbody";
    objTFoot.className = "calendarfooter";

    objTable.appendChild(objTHead);
    objTable.appendChild(objTBody);
    objTable.appendChild(objTFoot);
    objTable.className = "calendartable";
    objTable.cellSpacing = "0px";
    objTable.cellPadding = "0px";

    pobjDIV.appendChild(objTable);
    
    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarheaderrow";
    objTHead.appendChild(objNewRow);

    objNewCol = document.createElement("TH");
    objNewRow.appendChild(objNewCol);
    objNewCol.className = "leftarrow";

    switch (pstrCulture) {
        case "pt-BR":
            objNewCol.title = "Década Anterior";
            break;
        case "es-ES":
            objNewCol.title = "Década Anterior";
            break;
        default:
            objNewCol.title = "Previous Decade";
            break;
    }
    
    objNewCol.onclick = function() {
        if ((intSelectedYear - 10) <= 1900) { return; }
        buildCalendar(new Date(intSelectedYear - 10, pobjDate.getMonth(), 1), pobjDIV, pstrCulture, 'Y');
    };
    
    objNewCol = document.createElement("TH");
    objNewCol.colSpan = "3";
    objNewCol.className = "month";
    objNewCol.innerHTML = getMonthName(pobjDate.getMonth(), pstrCulture);
    objNewRow.appendChild(objNewCol);
    
    objNewCol.onclick = function() {
        buildCalendar(new Date(intSelectedYear, pobjDate.getMonth(), 1), pobjDIV, pstrCulture, 'M');
    };
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };
    
    objNewCol = document.createElement("TH");
    objNewCol.colSpan = "2";
    objNewCol.className = "year";
    objNewCol.innerHTML = intSelectedYear.toString();
    objNewRow.appendChild(objNewCol);
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };

    objNewCol = document.createElement("TH");
    objNewRow.appendChild(objNewCol);
    objNewCol.className = "rightarrow";

    switch (pstrCulture) {
        case "pt-BR":
            objNewCol.title = "Próxima Década";
            break;
        case "es-ES":
            objNewCol.title = "Próxima Década";
            break;
        default:
            objNewCol.title = "Next Decade";
            break;
    }
    
    objNewCol.onclick = function() {
        if (intYearCount >= 2100) { return; }
        buildCalendar(new Date(intSelectedYear + 10, pobjDate.getMonth(), 1), pobjDIV, pstrCulture, 'Y');
    };
    
    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarheaderrow";
    objTHead.appendChild(objNewRow);

    for (var i = 0; i < 7; i++) {
        objNewCol = document.createElement("TH");
        objNewCol.className = "weekday";
        objNewCol.innerHTML = "&nbsp;";
        objNewRow.appendChild(objNewCol);
    }
    
    objNewRow = document.createElement("TR");
    objNewRow.className = "calendarfooterrow";
    objTFoot.appendChild(objNewRow);

    objNewCol = document.createElement("TD");
    objNewCol.className = "gotoday";
    objNewCol.colSpan = 7;
    
    switch (pstrCulture) {
        case "pt-BR":
            objNewCol.innerHTML = "Hoje: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + ",<BR />" +
                dttToday.getDate().toString() + " de " +
                getMonthName(dttToday.getMonth(), pstrCulture) + " de " +
                dttToday.getFullYear().toString();
            break;
        case "es-ES":
            objNewCol.innerHTML = "Hoy: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + "<BR />" +
                dttToday.getDate().toString() + " de " +
                getMonthName(dttToday.getMonth(), pstrCulture) + " de " +
                dttToday.getFullYear().toString();
            break;
        default:
            objNewCol.innerHTML = "Today: " +
                getWeekDayName(dttToday.getDay(), pstrCulture) + ",<BR />" +
                getMonthName(dttToday.getMonth(), pstrCulture) + " " +
                dttToday.getDate().toString() + ", " +                
                dttToday.getFullYear().toString();
            break;
    }
    
    objNewRow.appendChild(objNewCol);

    objNewCol.onclick = function() {
        txtCalendar.value = (dttToday.getDate() < 10 ?
            "0" + dttToday.getDate().toString() : dttToday.getDate().toString()) +
            "/" + (dttToday.getMonth() < 9 ? "0" : "") +
            (dttToday.getMonth() + 1).toString() + "/" + dttToday.getFullYear();
        pobjDIV.style.visibility = "hidden";
        showSelectElements("divGeral");
    };
    
    objNewCol.onmouseover = function() {
        this.className += "_over";
    };
    
    objNewCol.onmouseout = function() {
        this.className = this.className.replace("_over", "");
    };
    
    objNewRow = document.createElement("TR");
    objTBody.appendChild(objNewRow);
    
    objNewCol = document.createElement("TD");
    objNewCol.colSpan = 7;
    objNewRow.appendChild(objNewCol);
    
    objTable = document.createElement("TABLE");
    objTBody = document.createElement("TBODY");
    
    objTable.className = "calendarmonthtable";
    
    objNewCol.appendChild(objTable);
    objTable.appendChild(objTBody);
    
    for (var i = 0; i < 3; i++) {
        objNewRow = document.createElement("TR");
        objNewRow.className = "calendarmonthrow";
        objTBody.appendChild(objNewRow);
        for (var j = 0; j < 4; j++) {
            if (i == 2 && j == 3) {
                break;
            }
        
            objNewCol = document.createElement("TD");
            if (intYearCount == dttToday.getFullYear()) {
                objNewCol.className = "monthnow";
            } else {
                objNewCol.className = "month";
            }
            objNewRow.appendChild(objNewCol);
            
            try {
                objNewCol.innerHTML = intYearCount.toString();
            } catch(e) {
                objNewCol.innerText = intYearCount.toString();
            }
            
            objNewCol.onmouseover = function() {
                this.className += "_over";
            };
            
            objNewCol.onmouseout = function() {
                this.className = this.className.replace("_over", "");
            };
            
            objNewCol.onclick = function() {
                buildCalendar(new Date(parseFloat(this.innerHTML), pobjDate.getMonth(), 1),
                    pobjDIV, pstrCulture, 'D');
            };
            
            if (intYearCount == intSelectedYear) {
                objNewCol.className += "_selected";
            }
            
            intYearCount ++;
        }
    }
}

function createDate(pstrDate) {
    if (pstrDate == null || pstrDate.length == 0) {
        return null;
    }
    var arrDate = pstrDate.split("/");
    return new Date(arrDate[2], parseFloat(arrDate[1]) - 1, arrDate[0]);
}

function getWeekDayName(pintDay, pstrCulture) {
    switch (pstrCulture) {
        case "pt-BR":
            switch (pintDay) {
                default:
                case 0:
                    return "Domingo";
                case 1:
                    return "Segunda-feira";
                case 2:
                    return "Terça-feira";
                case 3:
                    return "Quarta-feira";
                case 4:
                    return "Quinta-feira";
                case 5:
                    return "Sexta-feira";
                case 6:
                    return "Sábado";
            }
            break;
        case "es-ES":
            switch (pintDay) {
                default:
                case 0:
                    return "Domingo";
                case 1:
                    return "Lunes";
                case 2:
                    return "Martes";
                case 3:
                    return "Miércoles";
                case 4:
                    return "Jueves";
                case 5:
                    return "Viernes";
                case 6:
                    return "Sábado";
            }
            break;
        default:
            switch (pintDay) {
                default:
                case 0:
                    return "Sunday";
                case 1:
                    return "Monday";
                case 2:
                    return "Tuesday";
                case 3:
                    return "Wednesday";
                case 4:
                    return "Thursday";
                case 5:
                    return "Friday";
                case 6:
                    return "Saturday";
            }
            break;
    }
}

function getMonthName(pintMonth, pstrCulture) {
    switch (pstrCulture) {
        case "pt-BR":
            switch (pintMonth) {
                default:
                case 0:
                    return "Janeiro";
                case 1:
                    return "Fevereiro";
                case 2:
                    return "Março";
                case 3:
                    return "Abril";
                case 4:
                    return "Maio";
                case 5:
                    return "Junho";
                case 6:
                    return "Julho";
                case 7:
                    return "Agosto";
                case 8:
                    return "Setembro";
                case 9:
                    return "Outubro";
                case 10:
                    return "Novembro";
                case 11:
                    return "Dezembro";
            }
            break;
        case "es-ES":
            switch (pintMonth) {
                default:
                case 0:
                    return "Enero";
                case 1:
                    return "Febrero";
                case 2:
                    return "Marzo";
                case 3:
                    return "Abril";
                case 4:
                    return "Mayo";
                case 5:
                    return "Junio";
                case 6:
                    return "Julio";
                case 7:
                    return "Agosto";
                case 8:
                    return "Septiembre";
                case 9:
                    return "Octubre";
                case 10:
                    return "Noviembre";
                case 11:
                    return "Diciembre";
            }
            break;
        default:
            switch (pintMonth) {
                default:
                case 0:
                    return "January";
                case 1:
                    return "February";
                case 2:
                    return "March";
                case 3:
                    return "April";
                case 4:
                    return "May";
                case 5:
                    return "June";
                case 6:
                    return "July";
                case 7:
                    return "August";
                case 8:
                    return "September";
                case 9:
                    return "October";
                case 10:
                    return "November";
                case 11:
                    return "December";
            }
            break;
    }
}
