﻿NewsletterSignup = Ext.extend(Ext.FormPanel, {
            constructor: function(config){
                                config.title='Scholl Newsletter Signup';   
                                config.url= 'services/webinars.aspx';
                                config.items=[{
                                        fieldLabel: 'First Name',
                                        name: 'fn',
                                        allowBlank:false,
                                        value: ''
                                    },{
                                        fieldLabel: 'Last Name',
                                        name: 'ln',
                                        value: ''
                                    },{
                                        fieldLabel: 'Email',
                                        name: 'email',
                                        vtype:'email',
                                        allowBlank:false,
                                        value:''
                                    }
                                ];
                                config.buttons = [{
                                        text: 'Subscribe Now',
                                        formBind: true,
                                        type:'submit',
                                        handler: function() { this.ownerCt.subscribe(); }
                                     
                                    },{
                                        text: 'Cancel',
                                        onClick: function() { this.ownerCt.cancel(); }
                                    }];
                                config.labelWidth= 75;
                                config.renderTo= Ext.getBody();
                                config.frame=true;
                                config.bodyStyle='padding:5px 5px 0';
                                config.width=350;
                                config.x=100; 
                                config.y=100;
                                config.floating=true;
                                config.defaultType='textfield';
                                config.autoHeight=true;
                                config.monitorValid=true;
                                config.collapsible=true;
                                config.defaults={width: 230},
                                config.draggable={
                                                    insertProxy: false,
                                                    onDrag : function(e){
                                                                            var pel = this.proxy.getEl();
                                                                            this.x = pel.getLeft(true);
                                                                            this.y = pel.getTop(true);
                                                                                var s = this.panel.getEl().shadow;
                                                                                if (s) {
                                                                                    s.realign(this.x, this.y, pel.getWidth(), pel.getHeight());
                                                                                }
                                                    },
                                                    endDrag : function(e){
                                                                            this.panel.setPosition(this.x, this.y);
                                                                        }
                                };
                                NewsletterSignup.superclass.constructor.apply(this, arguments);
                                this.url = config.url;
                                this._init();
                            },
            _init: function(){
                Ext.form.Field.prototype.msgTarget = 'side';
            },
            subscribe: function(){
                                      var f = this.getForm();
                                      this.collapse(true);
                                      f.submit(
                                                {
                                                    params:{m:'news'},
                                                    scope:this,
                                                    url:this.url,
                                                    success: this._signupComplete,
                                                    failure: this._signupFailure
                                                }
                                             );
            },
            cancel: function(){
                        this.hide();
            },
            _signupComplete: function(form, eventArgs){
                                var f = form;
                                if(eventArgs.result.calendars.Status=='-1')
                                {
                                        Ext.Msg.show({
                                           title:'Signup Complete',
                                           msg: 'Thank you for subscribing.\r\nYou will be receiving a confirmation email shortly.',
                                           buttons: Ext.Msg.OK,
                                           icon: Ext.MessageBox.INFO
                                        });
                                    this.hide();
                                    return;
                                }
                                this._signupFailure();
                            },
            _signupFailure: function(){
                                this.enable();
                                this.expand();
                                Ext.Msg.show({
                                           title:'Signup Failure',
                                           msg: 'Failed to signup for the newsletter. Please try again.',
                                           buttons: Ext.Msg.OK,
                                           icon: Ext.MessageBox.ERROR
                                        });
                            }
            
});