var Promocode = {

    config: {
        onSuccessClose: null
    },
    
    success: false,
    promotionName: null,
    modal: null,
    hasChoices: false,

    enterCode: function()
    {
        this.modal = new ModalObj({
            width: 350,
            onClose: function(){
                this.close();
            }.bind(this)
        });

        this.modal.loading(true);

        var url = '/signup/promocodemodal';
        new AjaxRequest(url, {
            onComplete: function(){
                this.modal.loading(false);
            }.bind(this),
            onSuccess: function(json){
                if (json.modalContents) {
                    this.modal.setContents(json.modalContents);
                    
                    var promocodeField = this.modal.down('.promocodeField input');
                    promocodeField.observe('focus', function(){
                        promocodeField.clear();
                        promocodeField.addClassName('active');
                    });

                    var promocodeSubmit = this.modal.down('input.submit');
                    promocodeSubmit.observe('click', function(){
                        this.submit(promocodeField.getValue());
                    }.bind(this));

                    this.modal.select('.closeButton').each(function(closeButton){
                        closeButton.observe('click', function(e){
                            e.stop();
                            this.modal.close();
                        }.bind(this));
                    }.bind(this));

                    this.hasChoices = json.hasChoices;
                    
                    document.observe('keypress', function(e){
                        if (e.keyCode==13 && this.modal.isOpen()) {
                            e.stop();
                            if (Promocode.success) {
                                this.modal.close();
                            }
                            else {
                                this.submit(promocodeField.getValue());
                            }
                        }
                    }.bind(this));
                }
            }.bind(this)
        });
    },

    close: function()
    {
        if (!this.success) {
            return;
        }

        if (this.config.onSuccessClose) {
            this.config.onSuccessClose();
        }
        else {
            if (this.hasChoices) {
                document.location = '/signup/your-details';
            }
            else {
                document.location = '/signup';
            }
        }
    },

    submit: function(promocode)
    {
        this.modal.loading(true);

        var onComplete = function()
        {
            this.modal.loading(false);
        }.bind(this);

        var onSuccess = function(json)
        {
            if (!json.promocodeValid) {
                this.modal.down('.promocodeInvalid').show();
            }
            else {
                this.success = true;
                this.promotionName = json.promotionName;

                this.modal.down('.promotionName').update(json.promotionName.escapeHTML());
                this.modal.down('.promotionType').update(json.promotionType.escapeHTML());

                if (json.promotionDeprecated) {
                    this.modal.down('.promotionDeprecated').show();
                }

                this.modal.down('.promocodeMessage').hide();
                this.modal.down('.promocodeEntry').hide();
                this.modal.down('.promocodeSuccess').show();
            }
            this.modal.growToFit();
        }.bind(this);

        this.request(promocode, onComplete, onSuccess);
    },

    request: function(promocode, onComplete, onSuccess)
    {
        new AjaxRequest('/signup/ajaxsetpromocode', {
            parameters: {
                promocode: promocode
            },
            onComplete: onComplete || function(){},
            onSuccess: onSuccess || function(){}
        })
    }
};
