﻿function DatePicker(objectName, hdnSelectedDateID, txtSelectedDateID, lblMonthID, tbDaysID)
{
    var hdnSelectedDate = document.getElementById(hdnSelectedDateID);
    var txtSelectedDate = document.getElementById(txtSelectedDateID);
    var lblMonth = document.getElementById(lblMonthID);
    var tbDays = document.getElementById(tbDaysID);

    var VisibleMonth = 0;
    var VisibleYear = 0;
    
    this.SelectedDateChanged = null;
    
    this.construct = function()
    {
        if(txtSelectedDate != null)
            txtSelectedDate.onchange = function() { hdnSelectedDate.value = txtSelectedDate.value; }
        
        var d = this.GetSelectedDate();
        if(!d)
            d = new Date();
            
        this.ShowMonth(d.getMonth() + 1, d.getFullYear());
    }
    
    function MonthName(month)
    {
        switch(month) {
            default:
            case 1: return "January";
            case 2: return "February";
            case 3: return "March";
            case 4: return "April";
            case 5: return "May";
            case 6: return "June";
            case 7: return "July";
            case 8: return "August";
            case 9: return "September";
            case 10: return "October";
            case 11: return "November";
            case 12: return "December";
        }
    }
    
    this.SetSelectedDate = function(month, day, year, update)
    {
        var d1 = this.GetSelectedDate();
        
        hdnSelectedDate.value = month + "/" + day + "/" + year;
        if(txtSelectedDate != null)
            txtSelectedDate.value = hdnSelectedDate.value;
            
        if(update == true)
            this.ShowMonth(month, year);

        var d2 = this.GetSelectedDate();
        if(this.SelectedDateChanged != null && (d1 == null || d1.getTime() != d2.getTime()))
            this.SelectedDateChanged(d2);
    }
    
    this.GetSelectedDate = function()
    {
        if(hdnSelectedDate.value.length > 0) {
            var a = hdnSelectedDate.value.split("/");
            
            var d = new Date();
            d.setTime(0);
            try {
                d.setDate(parseInt(a[1]));
                d.setFullYear(parseInt(a[2]), parseInt(a[0]) - 1);
            } catch(ex) {
                d = null;
            }
            
            if(d == "Invalid Date")
                d = null;
            
            return d;
        }
        
        return null;
    }
    
    this.ShowMonth = function(month, year)
    {
        VisibleMonth = month;
        VisibleYear = year;
        
        // show month/year in month label
        lblMonth.innerHTML = MonthName(month) + " " + year;
        
        // create date object based on the given month
        var d = new Date();
        d.setTime(0);
        d.setDate(1);
        d.setFullYear(year, month - 1);
        
        // go to first day to be displayed
        d.setDate(d.getDate() - d.getDay());
        
        // remove existing day rows from table body
        while(tbDays.hasChildNodes() && tbDays.childNodes.length > 0)
            tbDays.removeChild(tbDays.firstChild);

        // loop and display days until the last day to be displayed is reached
        var tr = null;
        for(;;) {
            // add new row if necessary
            if(d.getDay() == 0) {
                tr = document.createElement("tr");
                tbDays.appendChild(tr);
            }
            
            var SelectedDate = this.GetSelectedDate();
            if(!SelectedDate)
                SelectedDate = new Date();
            
            // create table cell for day
            var td = document.createElement("td");
            
            if(SelectedDate != null && d.getTime() == SelectedDate.getTime())
                td.className = "Selected";
            else if(d.getMonth() + 1 == month && d.getFullYear() == year)
                td.className = "Normal";
            else
                td.className = "Disabled";

            td.innerHTML = d.getDate();
            
            var f = objectName + ".SetSelectedDate(" + (d.getMonth() + 1) + ", " + d.getDate() + ", " + d.getFullYear() + ", true)";
            if(txtSelectedDate != null)
                f += "; " + objectName + "_ToggleCalendar()";
            td.onclick = new Function(f);

            tr.appendChild(td);
            
            d.setDate(d.getDate() + 1);
            
            if(d.getMonth() + 1 != month && d.getDay() == 0)
                break;
        }
    }
    
    this.ShowPrevMonth = function()
    {
        var month = VisibleMonth - 1;
        var year = VisibleYear;
        
        if(month < 1) {
            month = 12;
            year--;
        }
        
        this.ShowMonth(month, year);
    }
    
    this.ShowNextMonth = function()
    {
        var month = VisibleMonth + 1;
        var year = VisibleYear;
        
        if(month > 12) {
            month = 1;
            year++;
        }
        
        this.ShowMonth(month, year);
    }
    
    this.ShowPrevYear = function()
    {
        var month = VisibleMonth;
        var year = VisibleYear - 1;
        
        if(year < 0) {
            month = 1;
            year = 0;
        }
        
        this.ShowMonth(month, year);
    }
    
    this.ShowNextYear = function()
    {
        var month = VisibleMonth;
        var year = VisibleYear + 1;
        
        this.ShowMonth(month, year);
    }
    
    this.construct();
}
