﻿function DateTimeControl(Id) {
    this.InputDate = document.getElementById(Id + '_Date');
    this.InputDateHolder = document.getElementById(Id + '_DateHolder');
    this.InputTime = document.getElementById(Id + '_Time');
    this.InputOnOff = document.getElementById(Id + '_OnOff');
    this.FunctionOnChange = new Function(Id + '_OnChange()');

    // Declare vars (that must be overridden through page
    this.ValueMinimum = new Date();
    this.ValueMaximum = new Date();
};

DateTimeControl.prototype.Render = function () {
    with (this) {
        $(document).ready(function () {
            $(InputDate).datepicker({
                showOn: 'button',
                buttonImage: '../images/btnCalendar.gif',
                buttonImageOnly: true,
                yearRange: YearRange(),
                onSelect: function (dateText, inst) {
                    FunctionOnChange();
                }
            });
        });
    }
}

DateTimeControl.prototype.YearRange = function () {
    with (this) {
        return ValueMinimum.getFullYear().toString() + ':' + ValueMaximum.getFullYear().toString();
    }
}

DateTimeControl.prototype.OnChange = function () {
    with (this) {
        var dt = GetDateTime();
        SetDateTime(dt);
        FunctionOnChange();
    }
}

DateTimeControl.prototype.GetDateTime = function () {
    with (this) {
        var dt = $(InputDate).datepicker('getDate');
        if (InputTime) {
            dt = DateTools.CombineDateTime(dt, InputTime.value);
        } else {
            dt = DateTools.OnlyDatePart(dt);
        }
        return dt;     
    }
}

DateTimeControl.prototype.SetDateTime = function (dt) {
    with (this) {
        if (InputTime) { InputTime.value = DateTools.OnlyTimePartLocalized(dt); }
        $(InputDate).datepicker('setDate',dt);
        SetOnOff(true);       
    }
}

DateTimeControl.prototype.SetOnOff = function (i) {
    with (this) {
        if (InputOnOff) { InputOnOff.checked = i; }
        if (i) {
            InputDateHolder.style.display = '';
            if (InputTime) { InputTime.style.display = '' };
        } else {
            InputDateHolder.style.display = 'none';
            if (InputTime) { InputTime.style.display = 'none' };
        }
    }
}

function DateTools() { }

// Add a specific number of days to a date
DateTools.AddDaysToDate = function (dt, ds) {
    dt.setDate(dt.getDate() + ds);
    return DateTools.OnlyDatePart(dt);
}

// Remove any time part from a date and return only the date.
DateTools.OnlyDatePart = function (dt) {
    var dtMonth = dt.getMonth();
    var dtDay = dt.getDate();
    var dtYear = dt.getFullYear();
    return new Date(dtYear, dtMonth, dtDay);
}

// Combine a date and an entry from the time field
DateTools.CombineDateTime = function (dt, tm) {
    var dtMonth = dt.getMonth() + 1;
    var dtDay = dt.getDate();
    var dtYear = dt.getFullYear();
    var dtStr = dtMonth + '/' + dtDay + '/' + dtYear + ' ' + DateTools.TimeToCultureInvariant(tm) + ':00';
    return new Date(dtStr);
}

// Get localized time part from a date.
DateTools.OnlyTimePartLocalized = function (dt) {
    var dtHour = dt.getHours();
    var dtMinute = dt.getMinutes();
    var result = Right('0' + dtHour, 2) + DateTools.TimeSeparator + Right('0' + dtMinute, 2);
    return result;
}

/// Process a string to time field with CultureInvariant separator (":")
DateTools.TimeToCultureInvariant = function (s) {
    return DateTools.ValidTime(s, ":");
}

/// Process a string to time field with Localized separator (":",".")
DateTools.TimeToLocalized = function (s) {
    return DateTools.ValidTime(s, DateTools.TimeSeparator);
}

// Process and validate time onBlur in input box to Localized date
DateTools.TimeLocalized_OnBlur = function (obj) {
    obj.value = DateTools.TimeToLocalized(obj.value);
}

DateTools.ValidTime = function (s, separator) {
    var sp = s.split(/[^0-9]/);
    if (sp.length < 2) { return '00' + separator + '00' };
    var hr = parseInt(sp[0], 10);
    var min = parseInt(sp[1], 10);

    var output = ((hr >= 0 && hr < 24) ? Right('0' + hr.toString(), 2) : '00');
    output += separator;
    output += ((min >= 0 && min < 60) ? Right('0' + min.toString(), 2) : '00');

    return output;
}

DateTools.DatePicker_OnChange = function (i) {
    var dt = new Date();
    try {
        dt = $.datepicker.parseDate(DateTools.JQueryDateFormat, i.value);
    } catch (error) {
        dt = $.datepicker.parseDate(DateTools.JQueryDateFormat, i.defaultValue);
    }
    $(i).datepicker('setDate', dt);
}
