﻿var oDateBox;
var oShim;

function CreateDateBox(oContainer, sNameOfObject, sDate, sFormName, sOnUpdate)
{
    try
    {
        var oElem = CreateElementByName('INPUT', sNameOfObject);
        oElem.setAttribute('id', sNameOfObject);
        oElem.setAttribute('type', 'text');
        oElem.setAttribute('size', '10');
        oElem.setAttribute('maxlength', '10');
        oElem.setAttribute('onFocus', 'this.blur()');
        oElem.className = 'cal-TextBox';
        //oElem.onchange = new Function(sOnUpdate);
        oElem.readonly = true;
        oElem.value = sDate;
        oElem.setAttribute('onchange', sOnUpdate);

        oContainer.appendChild(oElem);

        oContainer.innerHTML = oContainer.innerHTML + '&nbsp;';

        var oA = document.createElement("A");
        oA.className = 'so-BtnLink';
        oA.setAttribute('href', 'javascript:void(null);');
        oA.onclick = new Function("CC_OpenCalendar('" + sNameOfObject + "','BTN_date_" + sNameOfObject + "');return false;");

        var oImg = CreateElementByName("IMG", 'BTN_date_' + sNameOfObject);
        oImg.setAttribute('align', 'absmiddle');
        oImg.setAttribute('border', '0');
        oImg.setAttribute('id', 'BTN_date_' + sNameOfObject);
        oImg.setAttribute('src', '/Images/img_sys_calendar.gif');
        oImg.width = "22";
        oImg.height = "21";

        oA.appendChild(oImg);
        oContainer.appendChild(oA);

        return oContainer;
    }
    catch (oErr)
    {
        JSError('CreateDateBox', oErr);
        return false;
    }
}
function CC_CheckCalendarDate(oObject)
{
    try
    {
        if (!CheckDate(oObject.value))
        {
            alert('Please enter a valid date.');
            oObject.focus();
        }
        else
        {
            if (oObject.value != '')
            {
                oObject.value = Replace(Replace(oObject.value, '.', '/'), '-', '/');

                var aDatePart = oObject.value.split('/');

                if (aDatePart[2].length == 2)
                {
                    aDatePart[2] = '20' + aDatePart[2];
                }

                oObject.value = parseInt(aDatePart[0], 10).toString() + '/' + parseInt(aDatePart[1], 10).toString() + '/' + aDatePart[2];
            }
        }
        //alert(CheckDate(oObject.value));
        return true;
    }
    catch (oErr)
    {
        JSError('CC_CheckCalendarDate', oErr);
        return false;
    }
}
function CC_ChangeYearByOne(iMoveBy)
{
    try
    {
        var frm = document.CalenderControl;

        var iYear = parseInt(frm.DateSelector_YearDDL.value) + iMoveBy;

        if (iYear < frm.MinYear.value)
        {
            iYear = frm.MinYear.value;
        }
        else if (iYear > frm.MaxYear.value)
        {
            iYear = frm.MaxYear.value;
        }

        frm.DateSelector_YearDDL.value = iYear;

        CC_BuildCalendar();
    }
    catch (oErr)
    {
        JSError('CC_ChangeYearByOne', oErr);
        return false;
    }
}
function CC_ChangeMonthByOne(iMoveBy)
{
    try
    {
        var frm = document.CalenderControl;

        var iMonth = parseInt(frm.DateSelector_MonthDDL.value) + iMoveBy;

        if (iMonth == 0)
        {
            iMonth = 12;

            if (frm.DateSelector_YearDDL.selectedIndex > 0)
            {
                frm.DateSelector_YearDDL.selectedIndex = frm.DateSelector_YearDDL.selectedIndex - 1;
            }
        }
        else if (iMonth == 13)
        {
            iMonth = 1;

            if (frm.DateSelector_YearDDL.selectedIndex < frm.DateSelector_YearDDL.length - 1)
            {
                frm.DateSelector_YearDDL.selectedIndex = frm.DateSelector_YearDDL.selectedIndex + 1;
            }
        }

        frm.DateSelector_MonthDDL.value = iMonth;

        CC_BuildCalendar();
    }
    catch (oErr)
    {
        JSError('CC_ChangeMonthByOne', oErr);
        return false;
    }
}
function CalendarControl_Click(oElem, iRow)
{
    try
    {
        var frm = document.CalenderControl;

        var oDateBox_ID = document.getElementById('SelectDateBox_ID');
        //*******************************************************************
        //var oDateBox = document.getElementById(oDateBox_ID.value);

        var iDay = parseInt(oElem.innerHTML);
        var iYear = parseInt(frm.DateSelector_YearDDL.value)
        var iMonth = parseInt(frm.DateSelector_MonthDDL.value);

        if (oElem.className == 'CalendarControl_NotThisMonth')
        {
            if (iRow <= 1)
            {
                iMonth--;
                if (iMonth == 0)
                {
                    iYear--;
                    iMonth = 12;
                }
            }
            else
            {
                iMonth++;
                if (iMonth == 13)
                {
                    iYear++;
                    iMonth = 1;
                }
            }
        }

        oDateBox.value = iMonth + '/' + iDay + '/' + iYear;

        oDateBox.onblur();

        CC_CloseCalendar();
    }
    catch (oErr)
    {
        JSError('CalendarControl_Click', oErr + '\n\noElem=' + oElem);
        return false;
    }
}
function CC_CloseCalendar()
{
    try
    {
        var oTbl = document.getElementById("Tbl_CalendarControl");

        oShim.style.display = 'none';
        oTbl.style.display = 'none';
    }
    catch (oErr)
    {
        JSError('CC_CloseCalendar', oErr);
        return false;
    }
}
function CC_GetShim()
{
    var iErrorOnLine = 0;
    try
    {
        if (!oShim)
        {
            oShim = document.getElementsByName('shim_CalendarControl');
            if (oShim)
            {
                oShim = CreateFrameByName('shim_CalendarControl');
                oShim.style.position = 'absolute';
                oShim.style.display = 'none';
                oShim.style.frameBorder = 0;
                oShim.src = '/Blank.htm';

                document.forms[0].appendChild(oShim);
            }
            else
            {
                oShim = oShim[0];
            }
        }

        return oShim;
    }
    catch (oErr)
    {
        JSError('CC_GetShim', oErr);
        return false;
    }
}

function CC_OpenCalendar(oLink, sDateBox_ID, sOpenCalImg_ID)
{
    var iErrorOnLine = 0;
    try
    {
        window.focus();
        iErrorOnLine = 1;

        var oDateBox_ID = document.getElementById('SelectDateBox_ID');

        // document.CalenderControl.DateBox_ID.value = sDateBox_ID;
        oDateBox_ID.value = sDateBox_ID;

        var iX, iY

        iErrorOnLine = 9;

        var oImage = oLink.childNodes[0]; //document.getElementById(sOpenCalImg_ID);

        iErrorOnLine = 10;

        iX = GetOffsetLeft(oImage);

        if (oImage.height)
        {
            iY = GetOffsetTop(oImage) + oImage.height;
        }
        else
        {
            iY = GetOffsetTop(oImage) + oImage.Height;
        }

        iErrorOnLine = 20;
        var oTbl = document.getElementById('Tbl_CalendarControl');
        
        CC_GetShim();

        oDateBox = oLink.parentNode;

        iErrorOnLine = 22;
        while (oDateBox.tagName != 'DIV' && oDateBox.tagName != 'TR')
        {
            oDateBox = oDateBox.parentNode;
        }

        iErrorOnLine = 24;

        oDateBox = oDateBox.getElementsByTagName('input')[0];

        iErrorOnLine = 25;


        if (oTbl.style.left.replace('px', '') == iX && oTbl.style.top.replace('px', '') == iY && oTbl.style.display == '')
        {
            CC_CloseCalendar();
            return false;
        }

        iErrorOnLine = 301;
        oTbl.style.left = iX;
        oTbl.style.top = iY;

        iErrorOnLine = 33;
        oShim.style.left = iX;
        oShim.style.top = iY;

        iErrorOnLine = 35;
        var aDate;
        var iYear;
        var iMonth;
        var iDay;

        iErrorOnLine = 40;
        if (oDateBox.value == '')
        {
            iErrorOnLine = 41;
            aDate = new Date();

            iErrorOnLine = 42;
            iMonth = aDate.getMonth() + 1;

            iErrorOnLine = 43;
            iYear = aDate.getYear();

            iErrorOnLine = 44;
            document.CalenderControl.SelectedDay.value = '';
            iErrorOnLine = 45;
        }
        else
        {
            iErrorOnLine = 46;
            aDate = oDateBox.value.split(' ')[0].split('/');

            iErrorOnLine = 47;
            iMonth = parseInt(aDate[0]);

            iErrorOnLine = 48;
            iYear = parseInt(aDate[2]);

            iErrorOnLine = 49;
            document.CalenderControl.SelectedDay.value = (new Date(iYear, iMonth - 1, aDate[1])) - 0;
            iErrorOnLine = 50;
        }

        iErrorOnLine = 51;
        var oMonthDDL = document.CalenderControl.DateSelector_MonthDDL;
        var oYearDDL = document.CalenderControl.DateSelector_YearDDL;

        oMonthDDL.value = iMonth;
        oYearDDL.value = iYear;

        iErrorOnLine = 55;
        CC_BuildCalendar(iDay);

        oTbl.style.display = '';

        oShim.style.width = oTbl.offsetWidth;
        oShim.style.height = oTbl.offsetHeight;

        oShim.style.display = '';
        iErrorOnLine = 60;
    }
    catch (oErr)
    {
        JSError('CC_OpenCalendar', oErr + '\n\n iErrorOnLine=' + iErrorOnLine);
        return false;
    }
}
function CC_BuildCalendar()
{
    try
    {

        var iDayValue = 24 * 60 * 60 * 1000;

        var iDay = document.CalenderControl.SelectedDay.value;
        var iMonth = parseInt(document.CalenderControl.DateSelector_MonthDDL.value) - 1;
        var oTbl = document.getElementById("CalendarControlTable");

        var oTds = oTbl.getElementsByTagName("TD");

        var dToday = new Date();
        var dMinDate = new Date(document.CalenderControl.DateSelector_YearDDL.value, iMonth, 1);
        var dMaxDate = new Date(new Date(document.CalenderControl.DateSelector_YearDDL.value, iMonth + 1, 1) - iDayValue);

        dMinDate = new Date(dMinDate - (iDayValue * dMinDate.getDay()));

        dMaxDate = new Date(dMaxDate - (iDayValue * (dMaxDate.getDay() - 6)) + (15 * 60));
        dToday = new Date(dToday.getYear(), dToday.getMonth(), dToday.getDate());

        var dDate = dMinDate;
        var i = 0;
        var iMaxDayMod = 0;
        var iLastDay = -1;

        while ((dDate - 0) <= (dMaxDate - 0) + iMaxDayMod)
        {
            if (iLastDay == dDate.getDate())
            {
                dDate = new Date((dDate - 0) + iDayValue);
                iMaxDayMod += iDayValue;
            }

            iLastDay = dDate.getDate();

            oTds[i].innerHTML = iLastDay;
            oTds[i].style.display = '';

            if (dDate.getMonth() == iMonth)
            {
                if (dDate - 0 == iDay - 0)
                {
                    oTds[i].className = 'CalendarControl_Selected';
                }
                else if (dDate - 0 == dToday - 0)
                {
                    oTds[i].className = 'CalendarControl_Today';
                }
                else
                {
                    oTds[i].className = 'CalendarControl_Default';
                }
            }
            else
            {
                oTds[i].className = 'CalendarControl_NotThisMonth';
            }
            i = i + 1;
            dDate = new Date((dDate - 0) + iDayValue);
        }
        for (i = i; i < 42; i++)
        {
            oTds[i].style.display = 'none';
        }
    }
    catch (oErr)
    {
        JSError('CC_BuildCalendar', oErr);
        return false;
    }
}
function cJDate(sDate)
{
    try
    {
        sDate = sDate + ' ';
        var aDateTime = sDate.split(' ');
        var aDate = aDateTime[0].split('/');
        var sDate = new Date(aDate[2], aDate[0] - 1, aDate[1]);
        return sDate;
    }
    catch (oErr)
    {
        JSError('cJDate', oErr);
    }
}
function cJDateTime(sDate)
{
    try
    {
        //alert(sDate);

        sDate = sDate + ' ';
        var aDateTime = sDate.split(' ');
        var aDate = aDateTime[0].split('/');
        var sTime;
        var aTime;
        var iHourModifier;
        var dDate;

        //alert(aDateTime[2]);
        
        if (aDateTime[2])
        {
            if (aDateTime[2] == 'PM')
            {
                iHourModifier = 0;
            }
            else
            {
                iHourModifier = 0;
            }
        }
        else
        {
            iHourModifier = 0;
        }

        //alert(aDateTime[1]);
        if (aDateTime[1])
        {
            aTime = aDateTime[1].split(':');

            aTime[0] = parseInt(aTime[0]) + iHourModifier;

            dDate = new Date(aDate[2], aDate[0] - 1, aDate[1],aTime[0],aTime[1],0);
            //alert(aDate[2] + ' / ' + (aDate[0] - 1) + ' / ' + aDate[1] + '   ' + aTime[0] + ' : ' + aTime[1] + '\n\n' + dDate);
        }
        else
        {
            dDate = new Date(aDate[2], aDate[0] - 1, aDate[1]);
        }


        return dDate;
    }
    catch (oErr)
    {
        JSError('cJDate', oErr);
    }
}