/* uuid.js */

Ext.onReady(function(){

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'under';

    /*
     * ================  Simple form  =======================
     */
    var simple = new Ext.FormPanel({
        labelWidth: 125, // label settings here cascade unless overridden
        labelAlign: 'right',
        url:'JavaScript://',
        frame:true,
        title: 'Live Example',
        bodyStyle:'padding:5px 5px 0',
        width: 450,
        defaults: {width: 295},
        defaultType: 'textarea',

        items: [{
                fieldLabel: 'Samples',
                xtype: 'textarea',
                name: 'samples',
                id: 'f-samples',
                value: '',
                allowBlank:true,
                grow: true,
                growMin:30,
                growMax:90
        }],

        buttons: [{
            text: 'Add Client-side UUID'
          , handler: function () {
                var form = simple.getForm();
                var samples = form.findField('f-samples').getValue();
                var uuid = new Ext.ux.UUID();
                form.findField('f-samples').setValue(samples + ((samples) ? '\n' : '') + uuid);
                form.findField('f-samples').el.scroll('bottom', 5000, true);
            }
        },{
            text: 'Add Server-side UUID'
          , handler: function () {
                new Ext.ux.UUID({
                  url: 'uuid.cfm'
                , callback: function(options, success, response) {
                    var responseJSON = Ext.decode(response.responseText);
                    var uuid = (responseJSON && responseJSON.uuid) ? responseJSON.uuid : '';
                    var form = simple.getForm();
                    var samples = form.findField('f-samples').getValue();
                    form.findField('f-samples').setValue(samples + ((samples) ? '\n' : '') + uuid);
                    form.findField('f-samples').el.scroll('bottom', 5000, true);
                  }
                });
            }
        },{
            text: 'Clear'
          , handler: function () {
                var form = simple.getForm();
                form.findField('f-samples').reset();
            }
        }]
    });

    simple.render('simple');
});
