﻿(function($) {
    $.extend($.rcn, {
        customer: {
            lookupPersonalData: function(civicNumber, onSuccess, onError) {
                var url = jQuery.rcn.settings.url.lookupPersonalData
                    + "?civicnumber=" + civicNumber
                    + "&cid=" + jQuery.rcn.settings.cacheKey
                    + "&method=?";

                $.getJSON(url, function(info) {
                    if (info) {
                        onSuccess(info);
                        jQuery.rcn.resetCache();
                    }
                    else
                        onError(info);
                });
            },
            create: function(customer, onSuccess, onError) {
                var url = jQuery.rcn.settings.url.createCustomer
                    + "?firstName=" + customer.firstName
                    + "&civicNumber=" + customer.civicNumber
                    + "&applyForCredit=" + customer.applyForCredit
                    + "&lastName=" + customer.lastName
                    + "&street=" + customer.street
                    + "&zipCode=" + customer.zipCode
                    + "&city=" + customer.city
                    + "&coAddress=" + customer.coAddress
                    + "&mobilePhone=" + customer.mobilePhone
                    + "&gender=" + customer.gender
                    + "&postalDeliveryNotification=" + customer.postalDeliveryNotification
                    + "&marketingchannel=" + customer.marketingchannel                                                            
                    + "&acceptTerms=" + customer.acceptTerms
                    + "&cid=" + jQuery.rcn.settings.cacheKey
                    + "&method=?";
                    
                url = encodeURI(url);

                $.ajax({
                    url: url,
                    type: "GET",
                    dataType: "json",
                    success: function(messages, textStatus) {
                        onSuccess(messages);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        onError();
                    }
                });
                jQuery.rcn.resetCache();
            }
        }
    });
})(jQuery);

