
Ext.onReady(function(){

    Ext.QuickTips.init();

    Ext.form.Field.prototype.msgTarget = 'side';

    var specialtyStore = new Ext.data.Store({
        // load using HTTP
        url: 'getSpecialties.asp',

        // the return will be XML, so lets set up a reader
        reader: new Ext.data.XmlReader({
               // records will have an "Item" tag
               record: 'Speciality'
           }, [
               // set up the fields mapping into the xml doc
               // The first needs mapping, the others are very basic
               'Name'
           ])
    });

    specialtyStore.load();

    var fs = new Ext.FormPanel({
        standardSubmit: true,
        frame: true,
        collapsible: true,
        title:'Search Facilities',
        labelAlign: 'right',
        labelWidth: 150,
        width:500,
        waitMsgTarget: true,

        // reusable eror reader class defined at the end of this file
        errorReader: new Ext.form.XmlErrorReader(),

        items: [
            new Ext.form.FieldSet({
                title: 'Search Criteria',
                autoHeight: true,
                defaultType: 'textfield',
                width: 480,
                items: [{
 	                xtype: 'radiogroup',
                    id: 'rbSpec',
                    hideLabel: true,
 	                columns: [300],
 	                vertical: true,
                    style: 'text-align:left;padding-left:20px;',
 	                items: [
 	                    {boxLabel: 'All Specialties', name: 'rbSpecialty', id: 'rbAllSpecialties', inputValue: 1, checked: true,
                            handler: function() {
                                var rbAll = Ext.getCmp('rbAllSpecialties');
                                var rbOne = Ext.getCmp('rbOneSpecialty');
                                var cmbSpecialty = Ext.getCmp('specialty');
                                cmbSpecialty.setDisabled(rbAll.checked);
                            }
                        },
 	                    {boxLabel: 'Specialty', name: 'rbSpecialty', id: 'rbOneSpecialty', inputValue: 2}
 	                ]
 	            }, 
                    new Ext.form.ComboBox({
                        hideLabel: true,
                        id:'specialty',
                        hiddenName:'specialtyCode',
                        store: specialtyStore,
                        valueField:'Name',
                        displayField:'Name',
                        typeAhead: false,
                        editable: false,
                        disabled: true,
                        resizable: true,
                        width: 250,
                        triggerAction: 'all',
                        emptyText:'Select a specialty...',
                        style: 'text-align:left;padding-left:20px;',
                        selectOnFocus:true,
                        forceSelection: true
                    })
                ]
            })
        ],

        buttons: [{
            text: 'Search',
            handler: function(){
                if (fs.getForm().isValid()) {
                    fs.getForm().submit({
                        waitMsg:'Searching...'
                    });
                }else{
                        Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
                }
            }
        }]
    });

    fs.render('theForm');

});

// A reusable error reader class for XML forms
Ext.form.XmlErrorReader = function(){
    Ext.form.XmlErrorReader.superclass.constructor.call(this, {
            record : 'field',
            success: '@success'
        }, [
            'id', 'msg'
        ]
    );
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);


