/* Copyright (c) 2005 RunningAHEAD.com.  All rights reserved. */

var g_pDatePicker = null;
var g_vecShortMonth = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                      "Aug", "Sep", "Oct", "Nov", "Dec"];
var g_vecDaysOfWeek = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
var BackgroundColor = "#DBEAF5";
var CalendarColor = "#FFFFFF";
var TodayColor = "#DDDDDD";

function ShowDatePicker(idInput, strFormat, nFDW)
{
    if (g_pDatePicker == null)
    {
        g_pDatePicker = new OLDatePicker(strFormat, nFDW);
        g_pDatePicker.Init(idInput);
    }

    if (g_pDatePicker.IsVisible())
        return;

    g_pDatePicker.m_pInput = GetElement(idInput);
    g_pDatePicker.SetDate();
    g_pDatePicker.CreateCalendar();
    g_pDatePicker.Show();
}

function OLDatePicker(strFormat, nFDW)
{
    this.Init = DPK_Init;
    this.Show = DPK_Show;
    this.Hide = DPK_Hide;
    this.IsVisible = DPK_IsVisible;
    this.MouseOver = DPK_MouseOver;
    this.MouseOut = DPK_MouseOut;
    this.Select = DPK_Select;
    this.SetDate = DPK_SetDate;
    this.PrevMonth = DPK_PrevMonth;
    this.NextMonth = DPK_NextMonth;
    this.CreateCalendar = DPK_CreateCalendar;

    this.m_pDate = null;
    this.m_pInput = null;
    this.m_pDiv = null;
    this.m_strDateFormat = strFormat;
    this.m_bFirstClick;

    if (nFDW < 0) nFDW = 0;
    this.m_nFirstDayOfWeek = nFDW % 7;
}

function DPK_Init(idInput)
{
    this.m_pInput = GetElement(idInput);
    if (this.m_pInput == null)
    {
        alert("cannot find text box with id=" + idInput);
        return;
    }
    this.m_pDiv = document.createElement("DIV");
    this.m_pDiv.style.visibility = "hidden";
    this.m_pDiv.style.position = "absolute";
    this.m_pDiv.style.flow = "left";
    this.m_pDiv.style.padding = "7px";
    this.m_pDiv.style.backgroundColor = BackgroundColor;
    this.m_pDiv.style.border = "1px solid black";
    document.body.appendChild(this.m_pDiv);
}

function DPK_SetDate()
{
    if (this.m_pDate == null)
        this.m_pDate = new Date();
}

function DPK_CreateCalendar()
{
    this.m_pDiv.innerHTML = "";

    var pDate = new Date(GetYear(this.m_pDate), this.m_pDate.getMonth(),
                         this.m_pDate.getDate());

    var nMonth = pDate.getMonth();
    var nDay = 1;
    var strDate = "";

    var pTbl = document.createElement("TABLE");
    pTbl.setAttribute("cellPadding", "0");
    pTbl.setAttribute("cellSpacing", "0");
    pTbl.style.backgroundColor = CalendarColor;

    var pTblBody = document.createElement("TBODY");
    var pRow = document.createElement("TR");
    var pCell = document.createElement("TD");
    pCell.innerHTML = "&lt;";
    pCell.style.textAlign = "left";
    pCell.style.cursor = "pointer";
    pCell.style.backgroundColor = BackgroundColor;
    pCell.style.paddingBottom = "5px";
    pCell.onclick = function() { g_pDatePicker.PrevMonth(); }
    pRow.appendChild(pCell);
    pCell = document.createElement("TD");
    pCell.innerHTML = "<b>" + GetMonthString(this.m_pDate.getMonth() + 1)
        + " " + GetYear(this.m_pDate) + "</b>";
    pCell.setAttribute("colSpan", "5");
    pCell.style.textAlign = "center";
    pCell.style.paddingBottom = "5px";
    pCell.style.backgroundColor = BackgroundColor;
    pRow.appendChild(pCell);
    pCell = document.createElement("TD");
    pCell.innerHTML = "&gt;";
    pCell.style.textAlign = "right";
    pCell.style.cursor = "pointer";
    pCell.style.paddingBottom = "5px";
    pCell.style.backgroundColor = BackgroundColor;
    pCell.onclick = function() { g_pDatePicker.NextMonth(); }
    pRow.appendChild(pCell);
    pTblBody.appendChild(pRow);

    pRow = document.createElement("TR");
    for (var i = 0; i < 7; ++i)
    {
        pCell = document.createElement("TD");
        pCell.innerHTML = GetDayOfWeekString((this.m_nFirstDayOfWeek + i) % 7);
        pCell.style.textAlign = "center";
        pCell.style.width = "1.5em";
        pCell.style.borderBottom = "1px solid black";
        pRow.appendChild(pCell);
    }
    pTblBody.appendChild(pRow);
    pRow = document.createElement("TR");

    pDate.setDate(nDay);
    var nDayOfWeek = pDate.getDay();
    for (var i = this.m_nFirstDayOfWeek; i != nDayOfWeek; i = (i + 1) % 7)
    {
        pCell = document.createElement("TD");
        pCell.innerHTML = "&nbsp;";
        pRow.appendChild(pCell);
    }

    var pToday = new Date();
    do
    {
        if (this.m_nFirstDayOfWeek == pDate.getDay())
        {
            pTblBody.appendChild(pRow);
            pRow = document.createElement("TR");
        }
        pCell = document.createElement("TD");
        pCell.innerHTML = nDay;
        pCell.onmouseover = function() { g_pDatePicker.MouseOver(this); }
        pCell.onmouseout = function() { g_pDatePicker.MouseOut(this); }
        pCell.onclick = function() { g_pDatePicker.Select(this.innerHTML); }
        pCell.style.height = "1em";
        pCell.style.padding = "2px";
        pCell.style.textAlign = "right";
        pCell.style.cursor = "pointer";
        if (GetYear(pToday) == GetYear(pDate)
            && pToday.getMonth() == pDate.getMonth()
            && pToday.getDate() == pDate.getDate())
        {
            pCell.style.backgroundColor = TodayColor;
        }
        pRow.appendChild(pCell);

        pDate.setDate(++nDay);
    } while (pDate.getMonth() == nMonth);

    while (pRow.childNodes.length < 7)
    {
        pCell = document.createElement("TD");
        pCell.innerHTML = "&nbsp;";
        pRow.appendChild(pCell);
    }
    pTblBody.appendChild(pRow);

    pRow = document.createElement("TR");
    pCell = document.createElement("TD");
    pCell.setAttribute("colSpan", "7");
    pCell.innerHTML = "today";
    pCell.onclick
        = function() { g_pDatePicker.m_pDate = new Date(); 
                       g_pDatePicker.Select(g_pDatePicker.m_pDate.getDate()); }
    pCell.style.paddingTop = "5px";
    pCell.style.backgroundColor = BackgroundColor;
    pCell.style.cursor = "pointer";
    pCell.style.textAlign = "center";
    pRow.appendChild(pCell);
    pTblBody.appendChild(pRow);

    pTbl.appendChild(pTblBody);
    this.m_pDiv.appendChild(pTbl);
}

function DPK_Show()
{
    document.onclick = function() { g_pDatePicker.Hide(); }
    this.m_bFirstClick = true;
    if (window.CalOnBeforeShow)
        CalOnBeforeShow();
    
    this.m_pDiv.style.left = GetOffsetLeft(this.m_pInput) + GetWidth(this.m_pInput) + 5 + "px";
    this.m_pDiv.style.top = GetOffsetTop(this.m_pInput) + this.m_pInput.offsetHeight + 5 + "px";
    this.m_pDiv.style.visibility = "visible";
}

function DPK_Hide()
{
    if (this.m_bFirstClick)
    {
        this.m_bFirstClick = false;
        return;
    }
    this.m_pDiv.style.visibility = "hidden";
    if (window.CalOnAfterHide)
        CalOnAfterHide();
    document.onclick = null;
}
function DPK_IsVisible()
{
    return this.m_pDiv.style.visibility == "visible";
}

function DPK_MouseOut(pDate)
{
    pDate.style.border = "";
    pDate.style.padding = "2px";
}
function DPK_MouseOver(pDate)
{
    pDate.style.border = "1px solid black";
    pDate.style.padding = "1px";
}
function DPK_Select(pDate)
{
    var strFormatedDate = this.m_strDateFormat;

    // format month
    var nMonth = this.m_pDate.getMonth() + 1;
    var strPrevDate = strFormatedDate.toString();
    strFormatedDate
        = strFormatedDate.replace("MMM", GetMonthString(nMonth));
    if (strPrevDate == strFormatedDate)
    {
        strFormatedDate
            = strFormatedDate.replace("MM", ToLeadingZeroString(nMonth));
        strFormatedDate = strFormatedDate.replace("M", nMonth.toString());
    }

    // format day
    var nDate = parseInt(pDate);
    strFormatedDate
        = strFormatedDate.replace("dd", ToLeadingZeroString(nDate));
    strFormatedDate = strFormatedDate.replace("d", nDate);

    // format year
    var nYear = GetYear(this.m_pDate);
    strFormatedDate = strFormatedDate.replace("yyyy", nYear.toString());
    strFormatedDate
        = strFormatedDate.replace("yy", ToLeadingZeroString(nYear % 100));

    this.m_pInput.value = strFormatedDate;
    this.Hide();
}
function GetMonthString(nMonth)
{
    return g_vecShortMonth[nMonth - 1];
}
function GetDayOfWeekString(nDay)
{
    return g_vecDaysOfWeek[nDay];
}
function GetYear(pDate)
{
    var nYear = pDate.getYear();
    if (nYear < 2000)
        nYear += 1900;
    return nYear;
}
function ToLeadingZeroString(nVal)
{
    var strVal = "";
    if (nVal < 10) strVal = "0";
    strVal += nVal.toString();
    return strVal;
}
function DPK_PrevMonth()
{
    var nMonth = this.m_pDate.getMonth() - 1;
    if (nMonth < 0)
    {
        nMonth = 11;
        this.m_pDate.setYear(GetYear(this.m_pDate) - 1);
    }
    this.m_pDate.setMonth(nMonth);
    this.CreateCalendar();
}
function DPK_NextMonth()
{
    var nMonth = this.m_pDate.getMonth() + 1;
    if (nMonth == 12)
    {
        nMonth = 0;
        this.m_pDate.setYear(GetYear(this.m_pDate) + 1);
    }
    this.m_pDate.setMonth(nMonth);
    this.CreateCalendar();
}
