// JScript File


function bsGetInnerText(oObject)
{
	if (oObject.innerText == undefined)
		return oObject.textContent; // Firefox
	else
		return oObject.innerText;
}

function bsIsNull(oObject)
{
	return ((oObject == null) || (oObject == undefined));
}

//=====================================================

function CTSCalendar_LeapYear(nYear)
{
    return (((nYear % 4 == 0) && (nYear % 100 != 0)) || (nYear % 400 == 0));
}


function CTSCalendar_GetDateNumber(oDate)
{
    return ((oDate.getFullYear() << 16) + (oDate.getMonth() << 8) + oDate.getDate());
}

function CTSCalendar_Refresh()
{
    var     oCalendar;

    oCalendar   = this;
    oCalendar.SelectDate(oCalendar.SelectedDate, true, true);
}


function CTSCalendar_Today()
{
    var     oCalendar, oDate;

    oCalendar   = this;
    oDate       = new Date();
    oCalendar.SelectDate(oDate.toDateString());
}



function CTSCalendar_MoveMonth(nOffset)
{
    var     oCalendar, oDate;
    var     aDaysInMonth, nDate, nDay, nMonth, nYear;

    aDaysInMonth    = [   31,   28,   31,   30,   31,   30,   31,   31,   30,   31,   30,   31];

    oCalendar   = this;
    with (oCalendar.SelectedDate)
    {
        nMonth      = getMonth();
        nDate       = getDate();  // Day within the month
        nYear       = getFullYear();
    }

    nMonth  += nOffset;
    while (nMonth < 0)
    {
        nYear--;
        nMonth  += 12;
    }
    while (nMonth >= 12)
    {
        nYear++;
        nMonth  -= 12;
    }
    
    if (nDate > 28)
    {
        if (nDate > aDaysInMonth[nMonth])
            nDate = aDaysInMonth[nMonth];
        
        // if in Feb, determine the last day, bacause of leap year etc
        if (nMonth == 1)
        {   // This is Feb (Jan=0)
            if (CTSCalendar_LeapYear(nYear) == true)
            {   // This is a leap year, hence 29th of Feb is valid for this year
                nDate = 29;
            }
        }
    }
    oDate       = new Date(nYear, nMonth, nDate);
    oCalendar.SelectDate(oDate.toDateString());
}



function CTSCalendar_Show(oDate)
{
    var     sMonths, aDaysInMonth;
    var     oCalendar, oRow, oCell;
    var     nDate, nLastDate, nMonth, nYear;
    var     nDateNumberBase;

    sMonths         = ["January","February","March","April","May","June","July","August","September","October","November","December"];
    aDaysInMonth    = [   31,   28,   31,   30,   31,   30,   31,   31,   30,   31,   30,   31];

    oCalendar  = this;

    // Workout the date of the first cell (can be -ve if 1st of the month is not on sunday)
    nDate   = (oDate.getDate() - oDate.getDay()) % 7;
    if (nDate > 1)
        nDate -= 7;
    
    nMonth          = oDate.getMonth();
    nYear           = oDate.getFullYear();
    nLastDate       = aDaysInMonth[nMonth];
    nDateNumberBase = CTSCalendar_GetDateNumber(new Date(nYear, nMonth, 1)) - 1;
    
    // Set the Date caption
    oCalendar.rows[0].cells[0].innerHTML = sMonths[nMonth] + "  " + oDate.getFullYear().toString();

    
    // if in Feb, determine the last day, bacause of leap year etc
    if (nMonth == 1)
    {   // This is Feb (Jan=0)
        if (CTSCalendar_LeapYear(nYear) == true)
        {   // This is a leap year, hence 29th of Feb is valid for this year
            nLastDate = 29;
        }
    }
    
    for(nRow=3; nRow<9; nRow++)
    {
        oRow = oCalendar.rows[nRow];
        for(nCol=0; nCol<7; nCol++)
        {
            oCell = oRow.cells[nCol];
            if ((nDate <= 0) || (nDate > nLastDate))
            {
                oCell.innerHTML     = "&nbsp;&nbsp;";
                oCell.style.cursor  = "";
                oCell.DateNumber    = -1;
            }
            else if (oCalendar.IsValidDateN(nDateNumberBase + nDate) == false)
            {
                oCell.innerHTML     = nDate.toString() + "&nbsp;&nbsp;";
                oCell.style.cursor  = "pointer";
                oCell.style.color   = oCell.fadedColor;
                oCell.DateNumber    = nDateNumberBase + nDate;
            }
            else
            {
                oCell.innerHTML     = nDate.toString() + "&nbsp;&nbsp;";
                oCell.style.cursor  = "pointer";
                oCell.style.color   = oCell.normalColor;
                oCell.DateNumber    = nDateNumberBase + nDate;
            }
            nDate++;
        }
    }
}



function CTSCalendar_MouseOver(nRow, nCol)
{
    var     oCalendar, oCell;

    oCalendar   = this;
    oCell   = oCalendar.rows[nRow].cells[nCol];
    if ((bsGetInnerText(oCell).length > 2) && (oCell.style.cursor != ""))
    {   // use the cursor as indicator for clickable cells
        oCell.LastBGColor           = oCell.style.backgroundColor;
        oCell.style.backgroundColor = "#e0e0d0";
    }
}



function CTSCalendar_MouseOut(nRow, nCol)
{
    var     oCalendar, oCell;

    oCalendar   = this;
    oCell   = oCalendar.rows[nRow].cells[nCol];
    oCell.style.backgroundColor = oCell.LastBGColor;
}



function CTSCalendar_MouseClick(nRow, nCol)
{
    var     oCalendar, oCell, oDate;

    oCalendar   = this;
    oCell       = oCalendar.rows[nRow].cells[nCol];
    if ((bsGetInnerText(oCell).length > 2) && (oCell.style.cursor != ""))
    {   // use the cursor as indicator for clickable cells
        if (bsIsNull(oCalendar.SelectedRow) == false)
        {
            with (oCalendar.rows[oCalendar.SelectedRow].cells[oCalendar.SelectedCol])
            {
                style.color             = oCalendar.IsValidDateN(DateNumber)?normalColor:fadedColor;
                style.backgroundColor   = "";
                LastBGColor             = style.backgroundColor;
            }
        }
        oCalendar.SelectedDate.setDate(parseInt(bsGetInnerText(oCell)));
        oCalendar.SelectedRow       = nRow;
        oCalendar.SelectedCol       = nCol;
        oCell.style.backgroundColor = "#bbbbaa";
        oCell.LastBGColor           = oCell.style.backgroundColor;
        oCell.style.color           = "white";
        if (bsIsNull(oCalendar.onDateChange) == false)
            eval(oCalendar.onDateChange);
    }
}



function CTSCalendar_SelectDate(sDate, bNoEvent, bForceRefresh)
{
    var     oCalendar, oCell, oDate;
    var     aDaysInMonth, nDate, nDay, nMonth, nYear, nFirstCellDate;

    aDaysInMonth    = [   31,   28,   31,   30,   31,   30,   31,   31,   30,   31,   30,   31];

    oCalendar   = this;
    if (sDate == "")
        oDate   = new Date();
    else
        oDate   = new Date(sDate);

    nDate   = oDate.getDate();  // Day within the month
    nDay    = oDate.getDay();   // Day of the week
    nMonth  = oDate.getMonth();
    nYear   = oDate.getFullYear();

    if (bsIsNull(oCalendar.SelectedDate) || (bForceRefresh == true))
    {   // Selected date is not defined; First time?, or force-refresh
        oCalendar.Show(oDate);
    }
    else
    {
        with (oCalendar.SelectedDate)
        {
            if ((getFullYear() != nYear) || (getMonth() != nMonth))
            {   // the correct month is not currently visible, show it
                oCalendar.Show(oDate);
            }
        }
    }

    // Workout the date of the first cell (can be -ve if 1st of the month is not on sunday)
    nFirstCellDate = (nDate - nDay) % 7;
    if (nFirstCellDate > 1)
        nFirstCellDate -= 7;

    nCol = nDay; //(nDay + nDate) % 7;
    nRow = parseInt((nDate - nFirstCellDate) / 7) + 3;
    oCell   = oCalendar.rows[nRow].cells[nCol];

    if (bsIsNull(oCalendar.SelectedRow) == false)
    {
        with (oCalendar.rows[oCalendar.SelectedRow].cells[oCalendar.SelectedCol])
        {
            style.color             = oCalendar.IsValidDateN(DateNumber)?normalColor:fadedColor;
            style.backgroundColor   = "";
            LastBGColor             = style.backgroundColor;
        }
    }

    oCalendar.SelectedDate      = oDate;
    oCalendar.SelectedRow       = nRow;
    oCalendar.SelectedCol       = nCol;
    oCell.style.backgroundColor = "#bbbbaa";
    oCell.LastBGColor           = oCell.style.backgroundColor;
    oCell.style.color           = "white"; //oCell.normalColor;
    if ((bsIsNull(oCalendar.onDateChange) == false) && (bNoEvent != true))
        eval(oCalendar.onDateChange);
}



function CTSCalendar_GetSelectedDate()
{
    var     oCalendar;

    oCalendar  = this;
    return(oCalendar.SelectedDate);
}

function CTSCalendar_ClearValidDates(bAllDatesAreValid)
{
    var     oCalendar;

    oCalendar  = this;
    oCalendar.aValidMinDate.length  = 0;
    oCalendar.aValidMaxDate.length  = 0;
    oCalendar.bAllDatesAreValid     = bAllDatesAreValid;
}


function CTSCalendar_AddValidDateRange(sStartDate, sEndDate)
{
    var     oCalendar;
    var     n;

    oCalendar  = this;
    n   = oCalendar.aValidMinDate.length;
    oCalendar.aValidMinDate[n]  = CTSCalendar_GetDateNumber(new Date(sStartDate));
    oCalendar.aValidMaxDate[n]  = CTSCalendar_GetDateNumber(new Date(sEndDate));
    oCalendar.bAllDatesAreValid = false;
}


function CTSCalendar_IsValidDateN(nDateNumber)
{
    var     oCalendar;
    var     n, nMax;

    oCalendar  = this;
    if (oCalendar.bAllDatesAreValid == true)
        return(true);

    nMax = oCalendar.aValidMinDate.length;
    if (nMax <= 0)
        return(false);      // no valid dates are specified
    for(n=0; n<nMax; n++)
    {
        if ((nDateNumber >= oCalendar.aValidMinDate[n]) && (nDateNumber <= oCalendar.aValidMaxDate[n]))
            return(true);   // this date is valid
    }
    return(false);  // date is not in valid date range
}


function CTSCalendar_IsValidDate(oDate)
{
    var     oCalendar;

    oCalendar  = this;
    return(oCalendar.IsValidDateN(CTSCalendar_GetDateNumber(oDate)));
}


function CTSCalendar_DisableSelection(target)
{
    /***********************************************
    * Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    ***********************************************/

    if (typeof target.onselectstart!="undefined") //IE route
	    target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	    target.style.MozUserSelect="none"
    else //All other route (ie: Opera)
	    target.onmousedown=function(){return false}
    target.style.cursor = "default"
}


 
function CTSCalendar(sName, sContainerTagName, sInitDate, sOnChangeCode)
{
    var     sHTML, sRowHTML;
    var     n, nRow, nCol;
    var     aDays, aColColor, aColFadedColor;
    var     sBorderBox, sBorderLB, sBorderLBR, sButtonStyle, sButtonEvents;
    var     oCalendar, oContainer;

    aDays           = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
    aColColor       = ["red","black","black","black","black","black","red"];
    aColFadedColor  = ["#F8ACAC","silver","silver","silver","silver","silver","#F8ACAC"];


    sBorderBox      = "border: #a0a0a0 1px solid;";
    sBorderLB       = "border-left: #a0a0a0 1px solid; border-bottom: #a0a0a0 1px solid;";
    sBorderB        = "border-bottom: #a0a0a0 1px solid;";
    sButtonStyle    = "height:16px; opacity: 0.7; filter:progid:DXImageTransform.Microsoft.Alpha(enabled=true,opacity=70) ;"
    sButtonEvents   = " onmouseover='this.style.opacity=1.0; this.filters[0].enabled=false;' onmouseout='this.style.opacity=0.7; this.filters[0].enabled=true;' ";

    sHTML = "<table id='" + sName + "' border=0 cellpadding=0 cellspacing=0 style='width: 100%; height: 100%; font-size: 11px; font-family: Tahoma, Verdana, Arial, Sans-Serif;" + sBorderBox + "'>" +
            "<tr class='TSCalendarHeader' style='height: 19px;  color: white; ' ><td colspan=7 align=center valign=middle style='" + sBorderB + " font-weight:bolder;'>Jan, 2009</td></tr>" +
            "<tr><td align=center valign=middle class='TSCalendarToolbar' title='Previous Month' style='cursor:pointer; " + sBorderLB + sButtonStyle + "' " + sButtonEvents + " ondblclick='this.onclick();' onclick='document.getElementById(\"" + sName + "\").MoveMonth(-1);'><span style='width:100%;font-size:13px;'><b>&#171;</b></span></td>" +
            "    <td align=center valign=middle class='TSCalendarToolbar' colspan=5 style='cursor:pointer;" + sBorderLB + sButtonStyle + "' " + sButtonEvents + " ondblclick='this.onclick();' onclick='document.getElementById(\"" + sName + "\").Today();'><span style='width=100%'>Today</span></td>" +
            "    <td align=center valign=middle class='TSCalendarToolbar' title='Next Month' style='cursor:pointer; " + sBorderLB + sButtonStyle + "' " + sButtonEvents + " ondblclick='this.onclick();' onclick='document.getElementById(\"" + sName + "\").MoveMonth(1);'><span style='width:100%;font-size:13px;'><b>&#187;</b></span></td></tr>" +
            "<tr style='height: 18px; background-color: #E9EAEC; color: black;'>";



    for(n=0; n<7; n++)
        sHTML += "<td align=right valign=middle style='width: 14%; " + sBorderB + " color: " + aColColor[n] + "'>" + aDays[n] + "&nbsp;</td>";
    sHTML += "</tr>";

    sRowHTML = "<tr style='background-color:#FAFAFA;'>";
    for(n=0; n<7; n++)
        sRowHTML += "<td align=right valign=middle style='cursor:pointer;color:" + aColColor[n] + ";' onmouseover='document.getElementById(\"" + sName + "\").MouseOver(##ROW##," + n + ");' onmouseout='document.getElementById(\"" + sName + "\").MouseOut(##ROW##," + n + ");'  ondblclick='this.onclick();' onclick='document.getElementById(\"" + sName + "\").MouseClick(##ROW##," + n + ");'>??&nbsp;</td>";
    sRowHTML += "</tr>";
   
    for(n=0; n<6; n++)
        sHTML += sRowHTML.replace(/##ROW##/gi, n+3);

    sHTML += "</table>";
    
    oContainer              = document.getElementById(sContainerTagName);
    oContainer.innerHTML    = sHTML;
    CTSCalendar_DisableSelection(oContainer);

    oCalendar                   = document.getElementById(sName);
    oCalendar.Refresh           = CTSCalendar_Refresh;
    oCalendar.Show              = CTSCalendar_Show;
    oCalendar.MoveMonth         = CTSCalendar_MoveMonth;
    oCalendar.Today             = CTSCalendar_Today;
    oCalendar.MouseOver         = CTSCalendar_MouseOver;
    oCalendar.MouseOut          = CTSCalendar_MouseOut;
    oCalendar.MouseClick        = CTSCalendar_MouseClick;
    oCalendar.SelectDate        = CTSCalendar_SelectDate;
    oCalendar.GetSelectedDate   = CTSCalendar_GetSelectedDate;

    oCalendar.ClearValidDates   = CTSCalendar_ClearValidDates;
    oCalendar.AddValidDateRange = CTSCalendar_AddValidDateRange;
    oCalendar.IsValidDateN      = CTSCalendar_IsValidDateN;
    oCalendar.IsValidDate       = CTSCalendar_IsValidDate;
    
    oCalendar.onDateChange      = sOnChangeCode;
    oCalendar.aValidMinDate     = new Array();
    oCalendar.aValidMaxDate     = new Array();
    oCalendar.bAllDatesAreValid = true;
    
    for(nRow=3; nRow<9; nRow++)
    {
        for(nCol=0; nCol<7; nCol++)
        {
		oCalendar.rows[nRow].cells[nCol].normalColor = aColColor[nCol]; 
		oCalendar.rows[nRow].cells[nCol].fadedColor= aColFadedColor[nCol];
        }
    }

    oCalendar.SelectDate(sInitDate, true);  // don't call onDateChange() for initial value

    return oCalendar;
}

