var DT_PostalcodeChecker = function(pcElement, houseNumberElement, streetElement, cityElement) {

    this.pcElement = pcElement;
    this.houseNumberElement = houseNumberElement;
    this.streetElement = streetElement;
    this.cityElement = cityElement;

    this.onChangeElement = function(e) {

        // get pc and house number
        var pc = $(this.pcElement).val();
        var hn = $(this.houseNumberElement).val();

        if (pc && hn) {

            // only check when postalcode is a valid dutch one
            if (/^\d{4}\s?[a-z]{2}$/i.test(pc)) {

                this.disableElements();

                var that = this;

                var opts = {};
                opts.data = {'postalcode' : pc, 'house_number' : hn};
                opts.dataType = 'json';
                opts.url = '/?module=DJO&action=GetAddressInfo&output=json';
                opts.success = function(result) { that.updateData(result); };

                $.ajax(opts);

            } else {
                this.enableElements();
            }
        } else {
            this.enableElements();
        }
    }

    this.enableElements = function()
    {
        $(this.streetElement).removeAttr('readonly').removeClass('disabled');
        $(this.cityElement).removeAttr('readonly').removeClass('disabled');
    }

    this.disableElements = function()
    {
        $(this.streetElement).attr('readonly', 'readonly').addClass('disabled');
        $(this.cityElement).attr('readonly', 'readonly').addClass('disabled');
    }

    this.updateData = function(result) {

        if (result) {
            var street = result['street'];
            var city = result['city'];

            if (street && city) {

                $(this.streetElement).val(street);
                $(this.cityElement).val(city);
            }
        }
    }

    this.init = function() {

        var that = this;

        $(this.pcElement).change(function() { that.onChangeElement(); });
        $(this.houseNumberElement).change(function() { that.onChangeElement(); });

    }

    var that = this;
    $(function() { that.init(); });

}