
Ext.onReady(function(){

Ext.util.CSS.createStyleSheet('.x-form-item-label { text-align:right !important; } ' );

Ext.each(Ext.select('.email-friend', true), function(el) {

el.on('click',function() {

    if( Ext.getCmp('email-friend-win') )
        Ext.getCmp('email-friend-win').destroy();

    var ef = new Ext.form.FormPanel({
        id:'email-friend-form-panel',
        formId: 'email-friend-form',
        frame: true,
        defaults: { anchor: '95%',xtype:'textfield', labelSeparator:'' },
        items:[
             { id: 'article_subject',xtype:'panel', html:''  },
             { id: 'article_url',  name:'article_url',xtype:'hidden' },
             { id: 'article_title',  name:'article_title',xtype:'hidden' },
             { id: 'to', fieldLabel:'to', name:'to' },
             { id: 'from', fieldLabel:'from', name:'from' },
             { xtype:  'panel',  html:'message [255 char limit]' },
             { id: 'message',  xtype:'textarea', name:'message',hideLabel:true,maxLength:255 },
             { xtype: 'panel', style:'padding-top:10px',height:90,
               html:'CSM Worldwide will use the email addresses provided solely for the purpose of delivering a link to selected article.' }

        ]
        ,buttons: [
          { text: 'Send',
            handler:function(){

                Ext.getCmp('article_url').setValue(location.href);
                Ext.getCmp('article_title').setValue(document.title);

              form = Ext.getCmp('email-friend-form-panel').getForm();



              form.submit({
                  url: '/csminsights/json-email-friend',
                  method: 'POST',
                  success:function(form,action) {  
                      Ext.getCmp('email-friend-win').hide();
                      Ext.Msg.show({title: 'Email Sent', msg: action.result.reason, width:400, height:340,buttons:Ext.Msg.OK});
                  },
                  failure:function(form,action) {  
                      Ext.Msg.show({title: 'Errors', msg: action.result.reason, width:400, height:340,buttons:Ext.Msg.OK});
                  },
                  waitMsg:'Processing ...'
              });
          
           }
        }]

    });  

    var efw = new Ext.Window({
            id          : 'email-friend-win'
            ,layout      : 'fit'
            ,width       : 400
            ,height      : 400
            ,closeAction :'hide'
            ,plain       : true
            ,title: ' &nbsp;&nbsp; email this article to a friend '
            ,items       : [ef]
    
    });
    
    console.log(Ext.getCmp('article_subject'));
    Ext.getCmp('article_subject').html = '<b>Article from CSM Worldwide: ' + document.title + '</b>';
    efw.show();


}); // el.on

}); // Ext.each 

Ext.quicktips.init();

}); // Ext.onReady
