var financingEnrollmentModel = function (data) {
    var self = this;
    ko.mapping.fromJS(data, {}, self);
    
    self.ShowPlanSelection = function() {
        return !self.SsnEntryVisible();
    };

    self.SSNChanged = function() {
        try {
            //Post up the entire view model to save selected financing
            var postUrl = self.BaseUrl() + "/Enrollment/ProcessSSNChange/";

            $.ajax({
                dataType: "json",
                method: "post",
                url: postUrl,
                cache: false,
                data: { financeEnrollmentModel: ko.toJSON(self) },
                success: function (returnObject) {
                    ko.mapping.fromJS(returnObject, {}, self);
                    self.SsnEntryVisible(false);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log("Error : " + jqXHR.responseText);
                }
            });
        } catch (exception) {
            console.log(exception.message);
        }
    };

    self.UpdatePaymentSchedule = function(device) {
        try {

            var postUrl = self.BaseUrl() + "/Enrollment/RecalculatePaymentPlans/";

            $.ajax({
                dataType: "json",
                method: "post",
                url: postUrl,
                cache: false,
                data: { updateDevice: ko.toJSON(device) },
                success: function (returnObject) {
                    device.DownPayment(returnObject.DownPayment);
                    device.PaymentPlans(returnObject.PaymentPlans);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log("Error : " + jqXHR.responseText);
                }
            });
        } catch(exception) {
            console.log(exception.message);
        }
    };

    self.SubmitFinancing = function(m) {
        try {
            //Post up the entire view model to save selected financing
            var postUrl = self.BaseUrl() + "/Enrollment/SavePaymentPlans/";

            $.ajax({
                dataType: "json",
                method: "post",
                url: postUrl,
                cache: false,
                data: { financeEnrollmentModel: ko.toJSON(self) },
                success: function (returnObject) {
                    window.parent.$.colorbox.close();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log("Error : " + jqXHR.responseText);
                }
            });
            
        } catch(exception) {
            console.log(exception.message);
        }
    };

    self.ClearPlanSelection = function(device) {
        try {
            device.SelectedFinancingPlanId(0);
        } catch(exception) {
            console.log(exception.message);
        }
    };

};

$(function() {
    ko.bindingHandlers.currency = {
        symbol: ko.observable('$'),
        update: function(element, valueAccessor, allBindingsAccessor) {
            return ko.bindingHandlers.text.update(element, function() {
                var value = +(ko.utils.unwrapObservable(valueAccessor()) || 0),
                    symbol = ko.utils.unwrapObservable(allBindingsAccessor().symbol === undefined
                        ? allBindingsAccessor().symbol
                        : ko.bindingHandlers.currency.symbol);
                return symbol + value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
            });
        }
    };
    
    ko.bindingHandlers.trueFalseRadioButton =
    {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            // event handler for the element change event
            var changeHandler = function () {
                var elementValue = $(element).val();
                var observable = valueAccessor();      // set the observable value to the boolean value of the element value
                observable($.parseJSON(elementValue));
            };    // register change handler for element
            ko.utils.registerEventHandler(element,
                                          "change",
                                          changeHandler);
        },
        update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var elementValue = $.parseJSON($(element).val());
            var observableValue = ko.utils.unwrapObservable(valueAccessor()); if (elementValue === observableValue) {
                element.checked = true;
            }
            else {
                element.checked = false;
            }
        }
    };
});
