/*-------------------------------------------------------------------------
*
*                HeaderPanel.js
*
*   Programmer:  John Major
*                Pierce & Associates, P.C.
*
*   Date:        01/10/11
*
*   Description: Sale Bid Information Panel (Filter) 
*-------------------------------------------------------------------------
*   Special  Instructions:
*	  *None
*-------------------------------------------------------------------------
*       Modifications by Pierce & Associates, P.C.
*-------------------------------------------------------------------------
* Mod#  |   Date   | Programmer   |   Description
*-------------------------------------------------------------------------
*       |          |              | 
*       |          |              | 
*-------------------------------------------------------------------------
*
*
*-------------------------------------------------------------------------*/
Ext.ns('saleBidExt.panel');

saleBidExt.panel.HeaderPanel = Ext.extend(Ext.Panel, {

	// any "config" options specified at this level can be overriden when instantiated...
	//
	//bodyCssClass  : 'x-detail-header-panel',
	
	// we will always override the "initComponent" method
	//
	initComponent : function(){
		
		// anything that we specify in the config object cannot be overriden from the outside...
		//
		
		var config = {
				frame		  : true,
				title		  : 'Filter Property Listing',
				iconCls    	  : 'find',
				collapsible	  : true,
				height	      : 120,
				items		  : this.buildPanelItems(),
				keys 		  : this.fnFunctionKeys()
		};
		
		Ext.apply(this,Ext.apply(this.initialConfig,config));
		saleBidExt.panel.HeaderPanel.superclass.initComponent.call(this);
		
		this.addEvents({
			search : true
		});
    },
    
    buildPanelItems: function(){   
        return [{
            xtype: 'fieldset',
            autoHeight: true,
            layout: "table",
			style : 'padding:2px',
            layoutConfig: {
                columns: 3
            },
            border: false,
            items: [{
                layout: "form",
                labelWidth: 90,
                width: 200,
                items: [{
                	xtype: 'datefield',
					id: 'search_FromDate',
					fieldLabel: 'Sale Start Date:',
					format: 'Y-m-d',
                    labelSeparator: ''
                }]
            }, {
                layout: "form",
                labelWidth: 90,
                width: 200,
                items: [{
                	xtype: 'datefield',
					id: 'search_EndDate',
					fieldLabel: 'Sale End Date:',
					format: 'Y-m-d',
                    labelSeparator: ''
                }]
            },{
                layout: "form",
                labelWidth: 105,
                width: 330,
                items: [{
                	xtype: 'textfield',
                    fieldLabel: 'Common Address:',
    				id: 'search_Address',
                    labelSeparator: '',
                    anchor : '95%'
                }]
            }]
        },{
            xtype: 'fieldset',
            autoHeight: true,
			style : 'padding:2px',
            layout: "table",
            layoutConfig: {
                columns: 5
            },
            border: false,
            items: [{
                layout: "form",
                labelWidth: 135,
                width: 175,
                items: [{
                	xtype: 'checkbox',
					id: 'search_BidAmount',
					fieldLabel: 'Has an Anticipated Bid?:',
					checked: false,
                    labelSeparator: '',
        			listeners: {
                		check: function() {
            				//If Checked Show Min / Max
                			if (Ext.getCmp('search_BidAmount').getValue()==true) {
                				Ext.getCmp('bid_min').enable();
                				Ext.getCmp('bid_max').enable();
                			} else {
                				//If unChecked Hide Min / Max
                				Ext.getCmp('bid_min').disable();
                				Ext.getCmp('bid_min').clearValue();
                				Ext.getCmp('bid_max').disable();
                				Ext.getCmp('bid_max').clearValue();
                			}
                		}
                	}
                }]
            },{
                layout: "form",
                labelWidth: 45,
                width: 200,
                items: [{
                	xtype: 'combo',
                	disabled: true,
        			id: 'bid_min',
                    mode: 'local',
                    value: '',
                    triggerAction: 'all',
                    forceSelection: true,
                    editable: false,
                    width: 120,
                    fieldLabel: 'Bid Min',
                    name: 'bidmin',
                    hiddenName: 'bidmin',
                    displayField: 'name',
                    valueField: 'value',
                    store: new Ext.data.JsonStore({
                        fields: ['name', 'value'],
                        data: [{
                            name: '$10,000',
                            value: 10000.00
                        }, {
                            name: '$20,000',
                            value: 20000.00
                        }, {
                            name: '$30,000',
                            value: 30000.00
                        }, {
                            name: '$40,000',
                            value: 40000.00
                        }, {
                            name: '$50,000',
                            value: 50000.00
                        }, {
                            name: '$100,000',
                            value: 100000.00
                        }, {
                            name: '$200,000',
                            value: 200000.00
                        }, {
                            name: '$300,000',
                            value: 300000.00
                        }, {
                            name: '$400,000',
                            value: 400000.00
                        }, {
                            name: '$500,000+',
                            value: 500000.00
                        }]
                    })
                }]
            },{
                layout: "form",
                labelWidth: 50,
                width: 185,
                items: [{
                	xtype: 'combo',
                	disabled: true,
        			id: 'bid_max',
                    mode: 'local',
                    value: '',
                    triggerAction: 'all',
                    forceSelection: true,
                    editable: false,
                    width: 120,
                    fieldLabel: 'Bid Max',
                    name: 'bidmax',
                    hiddenName: 'bidmax',
                    displayField: 'name',
                    valueField: 'value',
                    store: new Ext.data.JsonStore({
                        fields: ['name', 'value'],
                        data: [{
                            name: '$10,000',
                            value: 10000.00
                        }, {
                            name: '$20,000',
                            value: 20000.00
                        }, {
                            name: '$30,000',
                            value: 30000.00
                        }, {
                            name: '$40,000',
                            value: 40000.00
                        }, {
                            name: '$50,000',
                            value: 50000.00
                        }, {
                            name: '$100,000',
                            value: 100000.00
                        }, {
                            name: '$200,000',
                            value: 200000.00
                        }, {
                            name: '$300,000',
                            value: 300000.00
                        }, {
                            name: '$400,000',
                            value: 400000.00
                        }, {
                            name: '$500,000+',
                            value: 500000.00
                        }]
                    })
                }]
            },{
                layout: "form",
                width: 100,
                items: [{
                	xtype: 'button',
                	text: '<b><u>Clear Filter</b></u>',
        			iconCls: 'delete',
        			tooltip: 'Clear Filter Requirments',
        			labelSeparator: '',
					scope: this,
        			handler: function(){
        				//Clear Fields Here
        				Ext.getCmp('search_FromDate').setValue('');
        				Ext.getCmp('search_EndDate').setValue('');
        				Ext.getCmp('search_Address').setValue('');
        				Ext.getCmp('search_BidAmount').reset('');
        				
        				searchParms = {
    						 	searchFmDate : '',
            					searchEndDate : '',
            					searchAddress : ''
    						}						
            				this.searchGrid.onRebuildSaleBidSearch(searchParms);
        			}
                }]
            },{
                layout: "form",
                //labelWidth: 100,
                width: 90,
                items: [{
                	xtype: 'button',
                	text: '<b><u>Search</b></u>',
        			iconCls: 'accept',
        			tooltip: 'Search Property(s)',
        			labelSeparator: '',
					scope: this,
        			handler: this.searchPropertys
                }]
            }]
        }];
        
    },
    //Get the Property Grid Object Ref
	setGridSearchRef: function(ref) {
		this.searchGrid=ref;
	},
	
	searchPropertys: function() {

			//Check to see if dates were entered and if so make sure FromDate is less than or equal to EndDate
			if (Ext.getCmp('search_FromDate').getValue() != '' && Ext.getCmp('search_EndDate').getValue() != '') {
				if (Ext.util.Format.date(Ext.getCmp('search_FromDate').getValue(), 'Y-m-d') > Ext.util.Format.date(Ext.getCmp('search_EndDate').getValue(), 'Y-m-d')) {
					Ext.Msg.show({
						title: 'Sale Date Filter Issue',
						msg: 'Sale Start Date can not be greater than Sale End Date.',
						buttons: Ext.Msg.OK,
						icon: Ext.Msg.ERROR
					});
					return;
				}
			}
			
			//Check to see bid amounts were choosen and if so make sure min is not greater than max
			if (Ext.getCmp('bid_min').getValue() != '' && Ext.getCmp('bid_max').getValue() != '') {
				if (Ext.getCmp('bid_min').getValue() > Ext.getCmp('bid_max').getValue()) {
					Ext.Msg.show({
						title: 'Bid Amount Filter Issue',
						msg: 'Minimum Bid amount can not be greater than Maximum Bid amount.',
						buttons: Ext.Msg.OK,
						icon: Ext.Msg.ERROR
					});
					return;
				}
			}
			
			if (Ext.getCmp('search_BidAmount').getValue() == true) {
				var bidAmountBox = 'Y';
			}
			else {
				var bidAmountBox = 'N';
			}
			
			this.fireEvent('search');
			
			searchParms = {
				searchFmDate: Ext.util.Format.date(Ext.getCmp('search_FromDate').getValue(), 'Y-m-d'),
				searchEndDate: Ext.util.Format.date(Ext.getCmp('search_EndDate').getValue(), 'Y-m-d'),
				searchAddress: Ext.getCmp('search_Address').getValue(),
				bidAmount: bidAmountBox,
				bidMin: Ext.getCmp('bid_min').getValue(),
				bidMax: Ext.getCmp('bid_max').getValue()
			}
			this.searchGrid.onRebuildSaleBidSearch(searchParms);
	
	},
	
	//Map the Enter Key to the Filter Panel so when that has focus and Enter is Hit it performs the Property Search
    fnFunctionKeys: function(){
		return [{
		    	scope: this,
		        key: [Ext.EventObject.ENTER],
		        handler: this.searchPropertys
		        }];
    }
});

