Function.prototype.bind = function(context) {
    var __method = this, args = slice.call(arguments, 1);
    return function() {
        var a = merge(args, arguments);
        return __method.apply(context, a);
    }
}

var slice = Array.prototype.slice;

function update(array, args) {
    var arrayLength = array.length, length = args.length;
    while (length--) array[arrayLength + length] = args[length];
    return array;
}

function merge(array, args) {
    array = slice.call(array, 0);
    return update(array, args);
}

if(typeof wgo == 'undefined') {
    wgo = {}
}
if(typeof wgo.ui == 'undefined') {
    wgo.ui = {}
}

wgo.ui.plzchecker = {
    ci_field : null,
    pc_field : null,
    c_field : null,
    cs_field : null,

    ci_txt : $("<div style='display:inline;' id='ci_txt'></div>"),
    ci_lnk : $("<a style='margin-left: 5px; text-decoration: underline; cursor: pointer;'>Lieferung ins Ausland?</a>"),
    ci_lnk_txt : $("<input style='width: 0px; visibility: hidden;'/><b>Deutschland</b>&nbsp;&nbsp;&nbsp;"),
    txt_1 : "Bitte w&auml;hlen Sie zuerst ein Land",
    txt_2 : "Bitte w&auml;hlen Sie zuerst die Postleitzahl",
    txt_3 : "Die Postleitzahl ist ung&uuml;tig!",

    init: function() {



        if($("select[name=customer_country_id]").length > 0) {
            this.ci_field  = $("select[name=customer_country_id]");
            this.pc_field  = $("input[name=postcode]");
            this.c_field   = $("*[name=city]");
            this.cs_field  = $("<select name='city'></select>");
        } else {
            this.ci_field = $("select[name=ab_country_id]");
            this.pc_field = $("input[name=ab_postcode]");
            this.c_field  = $("*[name=ab_city]");
            this.cs_field  = $("<select name='ab_city'></select>");
        }

        this.c_field.attr('oldname', this.c_field.attr('name'));
        this.cs_field.attr('oldname', this.c_field.attr('name'));

        if(this.ci_field.val() == '00' || this.ci_field.val() == 'DE') {
            this.ci_field.val('DE');
            this.ci_field.hide();
            this.ci_field.after(this.ci_lnk);
            this.ci_field.after(this.ci_lnk_txt);
        }

        this.ci_lnk.click(function() {
           this.ci_lnk.hide();
           this.ci_lnk_txt.hide();
           this.ci_field.show();
           this.ci_field.val('00');
           this.checkState();
        }.bind(this));

        this.ci_field.change(function() {
            this.c_field.val(this.cs_field.val());
            this.checkState();
        }.bind(this));

        this.pc_field.change(function() {
            this.checkState();
        }.bind(this));

        this.ci_txt.hide();
        this.c_field.after(this.ci_txt);
        this.c_field.after(this.cs_field);

        this.cs_field.attr('name', 'dummy');
        this.cs_field.hide();
        this.checkState();
    },

    checkState: function() {
        if(this.ci_field.val() == "00") {
            // Postleitzahl leeren
            this.pc_field.hide();
            this.pc_field.val('');
            this.pc_field.attr('maxlength', 8);

            // Ortausswahlfeld entfernen, Orttextefeld leeren
            this.cs_field.attr('name', 'dummy');
            this.cs_field.hide();

            this.c_field.attr('name', this.c_field.attr('oldname'));
            this.c_field.show();

            this.c_field.hide();
            this.c_field.val('');

            // Hinweis anzeigen
            this.ci_txt.html(this.txt_1);
            this.ci_txt.show();
        }

        else if(this.ci_field.val() == "DE") {
            this.pc_field.attr('maxlength', 5);

            var result = {}
            if(this.pc_field.val() != "") {
                $.ajax({
                    url: 'ajaxgetcitybypostcode',
                    data: ({
                        plz : this.pc_field.val()
                        }),
                    dataType: "json",
                    async: false,
                    success: function(data) {
                        result = data;
                    }
                });

                if(result.cities.length > 0) {
                    if(result.cities.length == 1) {
                        this.pc_field.show();

                        this.cs_field.attr('name', 'dummy');
                        this.cs_field.hide();

                        //this.c_field.remove();
                        //this.pc_field.after(this.c_field);
                        this.c_field.hide();
                        this.c_field.val(result.cities[0]);
                        this.c_field.attr('name', this.c_field.attr('oldname'));
                        this.ci_txt.html(result.cities[0]);
                        this.ci_txt.show();
                    } else {
                        this.pc_field.show();
                        
                        this.c_field.attr('name', 'dummy');
                        this.c_field.hide();

                        //this.cs_field.remove();
                        this.cs_field.empty();
                        for(var x = 0; x < result.cities.length; x++) {
                            if(result.cities[x] == this.c_field.val()) {
                                this.cs_field.append($("<option selected='selected'>" + result.cities[x] + "</option>"));
                            } else {
                                this.cs_field.append($("<option>" + result.cities[x] + "</option>"));
                            }
                        }
                        //this.pc_field.after(this.cs_field);
                        this.cs_field.attr('name', this.c_field.attr('oldname'));
                        this.cs_field.show();
                        this.ci_txt.hide();
                    }
                } else {
                    this.pc_field.show();
                    this.c_field.hide();
                    this.c_field.val('');
                    this.ci_txt.html(this.txt_3);
                    this.ci_txt.show();
                }

            } else {
                this.pc_field.show();
                this.c_field.hide();
                this.c_field.val('');
                this.ci_txt.html(this.txt_2);
                this.ci_txt.show();
            }
        }

        else {
            this.pc_field.attr('maxlength', 8);
            this.cs_field.attr('name', 'dummy');
            this.cs_field.hide();
            
            this.pc_field.show();
            this.pc_field.after(this.c_field);
            this.c_field.show();
            if(this.c_field.val() == 'null') {
                this.c_field.val('');
            }
            this.ci_txt.hide();
        }
    }
}

