
function addComment() {
    var form = new Ext.form.FormPanel({
        baseCls: 'x-plain',
        labelWidth: 55,
        url:'/postcomment.aspx',
        defaultType: 'textfield',
        
        items: [{
            fieldLabel: 'Title',
            name: 'title',
            anchor:'100%'  // anchor width by percentage
        },{
            fieldLabel: 'Name',
            name: 'name',
            anchor: '100%'  // anchor width by percentage
        },{
            fieldLabel: 'Email',
            name: 'email2',
            anchor: '100%'  // anchor width by percentage
        },{
            fieldLabel: 'Email',
            xtype: 'hidden',
            hideLabel: true,
            name: 'email',
            anchor: '100%'  // anchor width by percentage
        },{
            fieldLabel: 'Page',
            xtype: 'hidden',
            hideLabel: true,
            name: 'page',
            value: page_path,
            anchor: '100%'  // anchor width by percentage
        }, {
            xtype: 'textarea',
            hideLabel: true,
            name: 'body',
            anchor: '100% -53'  // anchor width by percentage and height by raw adjustment
        }]
    });

    var window = new Ext.Window({
        title: 'Post Comment',
        width: 500,
        height:400,
        minWidth: 300,
        minHeight: 200,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        modal: true,
        items: form
    });
    
    window.addButton('Post Comment', function() {
        form.getForm().submit({
            url: '/postcomment.aspx', 
            waitMsg:'Saving comment...', 
            success: function(form, action) {postSuccess(window, form, action);},
            failure: function(form, action) {postFailure(window, form, action);}
          });
    });

    window.show();
}

function postSuccess(win, form, action) {
    var result = action.result;
    var comments = Ext.get('s-comments');
    Ext.DomHelper.append(comments.dom, {
        id: 'comment' + commentid, cls: 'post-footer', children: [
            {cls: 'commenttitle', children: [
                {tag: 'a', name: 'comment' + commentid},
                result['title']
                ]},
            {html: '<a href="mailto:' + result['email'] + '">' + result['name'] + '</a>' +
                ' on ' + result['date']},
            {cls: 'comment', html: result['message']}
        ]
    });
    win.hide(Ext.get('comment' + commentid));
    win.close();
    window.location='#comment' + commentid;
    commentid ++;
}

function postFailure(window, form, action) {
    Ext.MessageBox.alert("Error", action.response.responseText);
}
