//<SCRIPT>
//// PopupCalendar.js ////
// ____________________________________________________________________________
//
//
// 10/13/2007 Created. - SC
//
// ____________________________________________________________________________

//alert("PopupCalendar.js");

if(typeof(SBNetWC) == "undefined")
{
	SBNetWC={};
}

SBNetWC.PC = {

    ShowCalendar: function(input) {
        //alert(input.Params.divCal.tagName);
        var params = input.Params;
        var div = params.divCal;
        var frm = params.frmCal;

        //Show the calendar if not visible.
        if (div.style.display == "none") {
            SBNetWC.Util.HideDivList();

            //Try to parse the date in the text box.
            var date = new Date(params.txtDate.value);
            if (!isNaN(date)) {
                params.dateSelected = date;
                params.dateCurrent = date;
            }
            //alert(params.dateSelected);
            //Fill the calendar HTML.
            this.fillCalendar(params);

            //Get the location of the calendar button.
            // We will use 'win' and 'scrl' later.
            var loc = SBNetWC.Util.getMyWindowXY(params.btnCal);
            var win = SBNetWC.Util.getWindowSize();
            var scrl = SBNetWC.Util.getScrollXY();

            //Prepare to show the DIV.
            div.style.position = "absolute";
            //Raise the z-index to move it in front of other objects.
            div.style.zIndex = "99990";

            var x = loc[0] + params.btnCal.clientWidth;
            var y = loc[1];

            //Make DIV visible.	You have to do this before you
            // can get the width and height.
            div.style.display = "block";

            //Get the DIV's with and height.
            var h = div.clientHeight;
            var w = div.clientWidth;

            //Locate the DIV next to the calendar buton, but don't
            // let it hang off the window.
            //Keep x on in the window.
            if ((x + w) > (win[0] + scrl[0]))
                x -= (x + w) - (win[0] + scrl[0]);
            div.style.left = x + "px";
            //Keep y in the window.
            if ((y + h) > (win[1] + scrl[1]))
                y -= (y + h) - (win[1] + scrl[1]);
            div.style.top = y + "px";

            //Move the frame behide the div.
            if (frm != null) {
                frm.style.left = div.style.left;
                frm.style.top = div.style.top;
                frm.style.position = "absolute";
                frm.style.zIndex = "99989";

                frm.style.display = "block";
                frm.style.height = div.offsetHeight + "px";
                frm.style.width = div.offsetWidth + "px";
            }
        }
        else {
            if (frm != null)
                frm.style.display = "none";
            div.style.display = "none"; //Hide it.
        }

        //Return false to cancel event bubble.
        return false;
    },

    HideCalendar: function(params) {
        //Hide the DIV.
        params.divCal.style.display = "none";
        if (params.frmCal != null)
            params.frmCal.style.display = "none";
    },

    initCalendar: function(tbID, btnID, divID, tblCal, frmID, bShowYear, postbackMethod, bAllowForwardDatesOnly, DaysOfWeekClosed, Holidays, TodaysEndOfDayTime) {
        //    initCalendar: function(tbID, btnID, divID, tblCal, frmID, bShowYear, postbackMethod, bAllowForwardDatesOnly) {
        //alert("initCalendar");
        //We have to keep track of the TextBox, ImageButton, DIV holding the calendar,
        // the calendar TABLE, and others. We will use the 'Params' object to hold the
        // information. A reference to this object can be given to other objects that
        // need it.
        var Params = {};
        //Remember the current date (the month we are viewing) and the selected date.
        Params.dateSelected = null;
        Params.dateCurrent = null;
        //Remember the HTML elements.	
        Params.txtDate = document.getElementById(tbID);
        Params.btnCal = document.getElementById(btnID);
        Params.divCal = document.getElementById(divID);
        Params.tblCal = document.getElementById(tblCal);

        if (frmID != null)
            Params.frmCal = document.getElementById(frmID);
        else
            Params.frmCal = frmID;
        Params.bShowYear = bShowYear;

        Params.postbackMethod = postbackMethod;
        Params.bAllowForwardDatesOnly = bAllowForwardDatesOnly;
        Params.DaysOfWeekClosed = DaysOfWeekClosed;
        Params.Holidays = Holidays;
        Params.TodaysEndOfDayTime = TodaysEndOfDayTime;

        //Add params to the text box, cal button, and table.
        if (Params.txtDate)
            Params.txtDate.Params = Params;
        if (Params.btnCal)
            Params.btnCal.Params = Params;
        if (Params.tblCal)
            Params.tblCal.Params = Params;

        //Add our DIV to the hide list. 
        if (Params.divCal != null) {
            SBNetWC.Util.AddToHideOnDocumentClickList(Params.divCal);
            if (Params.frmCal != null)
                SBNetWC.Util.AddToHideOnDocumentClickList(Params.frmCal);
        }
        //You don't have to call fillCalendar(...) until you are ready to show it.
        //this.fillCalendar(Params);
    },

    fillCalendar: function(params) {
        //alert("fillCalendar");
        //      alert(curDate);
        var sToday = new Date().toDateString();
        var sSelected = (params.dateSelected != null) ? params.dateSelected.toDateString() : "";
        var curDate = (params.dateCurrent != null) ? params.dateCurrent : new Date();
        var bAllowForwardDatesOnly = params.bAllowForwardDatesOnly;
        var TodaysEndOfDayTime = params.TodaysEndOfDayTime;

        //First day of the month.
        var date = new Date(curDate.getFullYear(), curDate.getMonth(), 1);

        var start = date.getDay();
        var stop = 42;
        var count = 0;
        var className = "";

        //Set date to first day visible on calendar.
        date.setDate(1 - start);
        var day = date.getDate();
        //If the 1st is on a Sunday add a week.
        if (day == 1) {
            date.setDate(-6);
            day = date.getDate();
            start += 7;
        }

        //Find our table and cells in the DOM.
        var table = params.tblCal;
        //alert("table = " + table);
        var tbody = table.getElementsByTagName("TBODY")[0];
        //alert("tbody = " + tbody);
        var rows = tbody.getElementsByTagName("TR");
        //alert("rows = " + rows.length);

        if (params.bShowYear) { }


        //Rows[0] is the year.
        if (params.bShowYear) {
            var cells = rows[0].getElementsByTagName("TD");
            cells[0].Params = params;
            cells[1].innerHTML = curDate.getFullYear();
            cells[2].Params = params;
        }
        else
            rows[0].style.display = "none";
        //Rows[1] is the month.
        cells = rows[1].getElementsByTagName("TD");
        cells[0].Params = params;

        var lnkShowAll = SBNetWC.Util.FindASPXControl("A", "DecreaseMonth", table);

        if (params.bAllowForwardDatesOnly && (new Date().getMonth() == curDate.getMonth()))
            lnkShowAll.style.display = "none";
        else
            lnkShowAll.style.display = "block";

        cells[1].innerHTML = this.monthList[curDate.getMonth()];
        if (!params.bShowYear)
            cells[1].innerHTML += (" " + curDate.getFullYear());
        cells[2].Params = params;

        //Start at row 4 (dates start here).
        for (var row = 3; row < rows.length; row++) {
            var tr = rows[row];
            //alert("tr = " + tr);
            var cols = tr.getElementsByTagName("TD");
            //alert("cols = " + cols.length);
            for (var col = 0; col < cols.length; col++) {
                //alert("col = " + col);
                var td = cols[col];
                //alert("td = " + td);
                var s = date.toDateString();
                var selectableDay = bAllowForwardDatesOnly == true ? (new Date(s) >= new Date(sToday)) : true;

                if (this.pastTodaysEndOfDayTime(date, TodaysEndOfDayTime))
                    selectableDay = false;

                var sameMonth = date.getMonth() == curDate.getMonth();
                if (sameMonth && selectableDay)
                    td.className = "calCurrentMonth";
                else
                    td.className = (selectableDay) ? "calOutsideCurrentMonth" : "calUnavailableDay";

                var closed = params.DaysOfWeekClosed.indexOf(this.DayOfWeek(s).toString())

                if (closed > -1 || this.isAHoliday(date, params.Holidays)) {
                    td.className = "calUnavailableDay";
                    selectableDay = false;
                }

                if (count < start) {
                    td.Params = params;

                    //Days in prior month on the calender.
                    this.drawDatePriorNext(td, day, s, selectableDay);
                }
                else
                    if (count < stop) {
                    td.Params = params;

                    //Days in this month on the calendar
                    if (s == sSelected)
                        this.drawDateSelected(td, day, s, selectableDay);
                    else if ((s == sToday) && selectableDay)
                        this.drawDateToday(td, day, s, selectableDay);
                    else
                        this.drawDateThisMonth(td, day, s, selectableDay);
                }
                else {
                    td.Params = params;
                    //Days in next month on the calendar
                    this.drawDatePriorNext(td, day, s, selectableDay);
                }

                //Get next day.
                day++;
                date.setDate(day);
                day = date.getDate();

                //Increment counter.
                count++;

                //When we pass the end of the month
                // set the stop count.
                if ((count > start) && (day == 1))
                    stop = count;
            }
        }
        //Remember the current date.
        params.dateCurrent = curDate;
    },

    newDateSelected: function(anchor, sDate) {
        //alert(sDate);
        //var params = anchor.parentNode.Params;
        var params = anchor.Params;
        var newDate = new Date(sDate);
        if (newDate != params.dateSelected) {
            params.dateSelected = newDate;
            params.dateCurrent = newDate;
            this.fillCalendar(params);
        }
        //Fill text box with date.
        params.txtDate.value = this.formatDate(newDate);
        //If using the SBNet.EC controrls, call TestControl.
        if (typeof (SBNet) != "undefined" && typeof (SBNet.EC) != "undefined")
            SBNet.EC.TestControl(params.txtDate);
        //Hide the calendar.
        SBNetWC.PC.HideCalendar(params);
        //Set the focus to the calendar button.
        params.btnCal.focus();

        //Do post back here if needed.
        if (params.postbackMethod != null)
            params.postbackMethod();

        //Return false to cancel event bubble.
        return false;
    },

    drawDateSelected: function(td, day, sDate, selectableDay) {
        td.className = "calCD"; //Change he class.
        td.innerHTML = day;
        if (selectableDay)
            td.onclick = function() { SBNetWC.PC.newDateSelected(td, sDate); }
        else
            td.onclick = null;
    },

    drawDateToday: function(td, day, sDate, selectableDay) {
        td.className = "calTD"; //Change he class.
        td.innerHTML = day;
        if (selectableDay)
            td.onclick = function() { SBNetWC.PC.newDateSelected(td, sDate); }
        else
            td.onclick = null;
    },

    drawDateThisMonth: function(td, day, sDate, selectableDay) {
        td.innerHTML = day;
        if (selectableDay)
            td.onclick = function() { SBNetWC.PC.newDateSelected(td, sDate); }
        else
            td.onclick = null;
    },

    drawDatePriorNext: function(td, day, sDate, selectableDay) {
        td.innerHTML = day;
        if (selectableDay)
            td.onclick = function() { SBNetWC.PC.newDateSelected(td, sDate); }
        else
            td.onclick = null;
    },

    DecreaseYear: function(anchor) {
        var params = anchor.parentNode.Params;
        params.dateCurrent = new Date(params.dateCurrent.getFullYear() - 1, params.dateCurrent.getMonth(), params.dateCurrent.getDate());
        this.fillCalendar(params);
        return false;
    },

    IncreaseYear: function(anchor) {
        var params = anchor.parentNode.Params;
        params.dateCurrent = new Date(params.dateCurrent.getFullYear() + 1, params.dateCurrent.getMonth(), params.dateCurrent.getDate());
        this.fillCalendar(params);
        return false;
    },

    DecreaseMonth: function(anchor) {
        var params = anchor.parentNode.Params;
        params.dateCurrent = new Date(params.dateCurrent.getFullYear(), params.dateCurrent.getMonth() - 1, params.dateCurrent.getDate());
        this.fillCalendar(params);
        return false;
    },

    IncreaseMonth: function(anchor) {
        var params = anchor.parentNode.Params;
        params.dateCurrent = new Date(params.dateCurrent.getFullYear(), params.dateCurrent.getMonth() + 1, params.dateCurrent.getDate());
        this.fillCalendar(params);
        return false;
    },

    formatDate: function(date) {
        var n = date.getMonth() + 1;
        var sDate = (n < 10) ? "0" : "";
        sDate += n.toString(10) + "/";

        n = date.getDate();
        sDate += (n < 10) ? "0" : "";
        sDate += n.toString(10) + "/";

        sDate += date.getFullYear().toString(10);
        return sDate;
    },

    DayOfWeek: function(date) {
        return ((new Date(date).getDay()) % 7);
    },

    isAHoliday: function(curDate, holidays) {
        var a = holidays.split(',');
        for (ii = 0; ii < a.length; ii++) {
            var holiday = new Date(a[ii]);
            if (curDate.getDay() == holiday.getDay() &&
                 curDate.getDate() == holiday.getDate() &&
                 curDate.getFullYear() == holiday.getFullYear())
                return true;

        }
        return false;
    },
    pastTodaysEndOfDayTime: function(curDate, todaysEndOfDayTime) {
        if (todaysEndOfDayTime == "")
            return false;

        var dtToday = new Date();
        if ((curDate.getDate() != dtToday.getDate()) ||
            (curDate.getMonth() != dtToday.getMonth()) ||
            (curDate.getFullYear() != dtToday.getFullYear()))
            return false;
        debugger;
        if( dtToday.getTime() >= new Date(todaysEndOfDayTime).getTime())
            return true;

        return false;
    }
}

SBNetWC.PC.monthList = ['January','February','March','April','May','June','July','August','September','October','November','December'];

//</SCRIPT>
