﻿function DateSelectForm(options) {
    this.yearList = options.yearList;
    this.monthList = options.monthList;
    this.dayList = options.dayList;
    this.dateField = options.dateField;

    var _instance;

    this.getDateString = function() {
        var dateString = this.yearList.val() + '/' + this.monthList.val() + '/' + this.dayList.val();
        return dateString;
    }
    this.populateYearList = function() {
        $.ajax({
            url: "/Ajax/RegistrationDetailsService.svc/GetYearsNumeric",
            global: false,
            type: "GET",
            contentType: "application/json",
            dataType: "json",
            success: function(data) {
                if (data.Result == 1) { // Success
                    $.each(data.Object, function(index) {
                        $(_instance.yearList).
                            append($("<option></option>").attr("value", data.Object[index].Key).text(data.Object[index].Value));
                    });
                    $(_instance.yearList).enable();
                    $(_instance.yearList).change(function() {
                        _instance.populateMonthList();
                        _instance.dateField.val(_instance.getDateString());
                    });
                }
            }
        });
    };
    this.populateMonthList = function() {
        $.ajax({
            url: "/Ajax/RegistrationDetailsService.svc/GetMonthsNumeric",
            global: false,
            type: "GET",
            contentType: "application/json",
            dataType: "json",
            success: function(data) {
                if (data.Result == 1) { // Success
                    $.each(data.Object, function(index) {
                        $(_instance.monthList).
                            append($("<option></option>").attr("value", data.Object[index].Key).text(data.Object[index].Value));
                    });
                    $(_instance.monthList).enable();
                    $(_instance.monthList).change(function() {
                        _instance.populateDayList();
                        _instance.dateField.val(_instance.getDateString());
                    });
                }
            }
        });
    };
    this.populateDayList = function() {
        $.ajax({
            url: "/Ajax/RegistrationDetailsService.svc/GetDaysNumeric",
            global: false,
            type: "GET",
            contentType: "application/json",
            dataType: "json",
            success: function(data) {
                if (data.Result == 1) { // Success
                    $.each(data.Object, function(index) {
                        $(_instance.dayList).
                            append($("<option></option>").attr("value", data.Object[index].Key).text(data.Object[index].Value));
                    });
                    $(_instance.dayList).enable();
                    $(_instance.dayList).change(function() {
                        _instance.dateField.val(_instance.getDateString());
                    });
                }
            }
        });
    };
    _instance = this;
}
