/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){
	Ext.BLANK_IMAGE_URL = '../js/extjs/resources/images/default/s.gif';
    Ext.QuickTips.init();
    var sendButton = Ext.get('show-btn');

    sendButton.on('click', function(){
		if(!sendWindow.isVisible()) {
			sendArticle.getForm().reset();
			sendWindow.show();
		} else {
			sendWindow.toFront();
		}
    });
    
    Ext.form.Field.prototype.msgTarget = 'side';
		
	var sendArticle = new Ext.FormPanel({
        labelAlign: 'top',
        bodyStyle:'padding:5px',
        width: 600,
        items: [{
            layout:'column',
            border:false,
            items:[articleID, {
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: 'From (Name)',
                    allowBlank: false, 
                    name: 'fromName',
                    anchor:'90%'
                }, {
                    xtype:'textfield',
                    fieldLabel: 'To (Name)',
                    allowBlank: false, 
                    name: 'toName',
                    anchor:'90%'
                }]
            },{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: 'From (Email)',
                    allowBlank: false, 
                    name: 'fromEmail',
                    vtype:'email',
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'To (Email)',
                    allowBlank: false, 
                    name: 'toEmail',
                    vtype:'email',
                    anchor:'90%'
                }]
            }]
        },{
            xtype:'htmleditor',
            id:'Comments',
            height: 150,
            width: 510,
            fieldLabel:'Comments'
        }],

        buttons: [
			{
		        text: 'Send',
		        handler: function(){
		            if(sendArticle.getForm().isValid()){
		                sendArticle.getForm().submit({
		                    method: 'POST',
		                    url: 'send',
							waitTitle: 'Sending...',
							waitMsg: 'Please wait while the article is being sent.',
							
							success:function() {
								Ext.Msg.alert('Status', 'The article was sent', function(btn, text) {
									if (btn == 'ok') {
										sendWindow.hide();
									}
								});
							},
							
							failure:function(form, action) {
								if(action.failureType == 'server') {
									obj = Ext.util.JSON.decode(action.response.responseText);
									Ext.Msg.alert('Error', obj.errors.reason);
								} else {
									Ext.Msg.alert('Warning!', 'Server is unreachable : ' + action.response.responseText);
								}
							}
		                });
		            }
		        }
		    },
			{ text: 'Cancel', handler: function() { sendArticle.getForm().reset(); sendWindow.hide(); } }
		]
    });
    
    var sendWindow = new Ext.Window({
        title    : 'Send Article to Friend',
        closable : false,
        modal	 : true,
        width    : 540,
        height   : 350,
        border   : false,
        plain    : true,
        layout   : 'fit',
        items    : sendArticle
    });
});