/**
 * @author George
 */
Ext.ns('Regs');
// create pre-configured window class
Regs.CallForm = Ext.extend(Ext.Panel, {

	 initComponent:function() {
	 	
this.form = new Ext.form.FormPanel({
		url : 'includes/add_callback.php',
		baseCls: 'x-plain',
		labelAlign: 'top',
  		bodyStyle: 'padding: 5px;',
  		defaultType: 'textfield',
			items: [{
						fieldLabel: 'Am I A...',
						xtype: 'combo',
						anchor: '50%',
						hiddenName: 'type',
						mode: 'remote',
						displayField: 'value',
        				triggerAction: 'all',
        				selectOnFocus:true,
        				listClass:'x-combo-list-small',
						store: new Ext.data.SimpleStore({
				fields: ['value'],
				data: [
				['Contractor'],['Agency']
				]
			}),
			mode: 'local',
			valueField: 'value',
		allowBlank: false,
        emptyText:'please select...',
        listeners: {
        	select: {
        		fn: function(combo){
        			if(combo.getValue() == 'Contractor'){
        				this.form.form.findField('aname').setDisabled(true);
        			}else{
        				this.form.form.findField('aname').setDisabled(false);
        			}
        		},
        		scope: this
        	}
        }
		},{
		fieldLabel: 'Agency Name',
		name: 'aname',
		allowBlank: false,
		anchor: '60%'
		},{
		fieldLabel: 'First Name',
		name: 'fname',
		allowBlank: false,
		anchor: '60%'
		},{
		fieldLabel: 'Surname',
		name: 'sname',
		allowBlank: false,
		anchor: '60%'
		},{
		fieldLabel: 'Telephone',
		name: 'tel',
		anchor: '60%',
		allowBlank: false
		},{
		xtype: 'fieldset',
		layout: 'column',
		autoHeight: true,
		title: 'Select a date and time to call',
		items: [{
			width: 120,
			baseCls: 'x-plain',
			layout: 'form',
			items: [{
		xtype: 'datefield',
        fieldLabel: 'Date',
		name: 'date',
		format: 'd-m-Y',
        allowBlank: false
	}]
		},{
			width: 200,
			baseCls: 'x-plain',
			layout: 'form',
			items: [
			new Ext.form.TimeField({
				name: 'time',
				width: 100,
				fieldLabel: 'Time',
    			minValue: '9:00 AM',
    			maxValue: '5:00 PM',
    			increment: 15
				})
				]
				}]
		},{
			fieldLabel: 'Additional Comments (optional)',
		xtype: 'textarea',
		name: 'comments',
		anchor: '100%',
		height: 60
		}],
		buttons: [{
			text: 'Submit Callback',
			tooltip: 'Submit your callback request',
			handler: this.onFormSubmit,
            scope: this
		},{
			text: 'Clear Form',
			tooltip: 'Clear the form',
			handler: function(){
				this.form.getForm().reset();
			},
			scope:this
		}]
});		
	

Ext.apply(this, {
		iconCls: 'icon-callback',
		width: 370	,
		baseCls: 'x-plain',
		renderTo: 'form',
		items: this.form
    });

// call parent
		Regs.CallForm.superclass.initComponent.apply(this, arguments);
},
		 onFormSubmit: function() {
		 	
	if (this.form.form.isValid()) {
				
				this.form.form.submit({
					waitMsg: 'Saving details...',
					scope: this,
					params: {
							action: 'submit'
							},
						success: this.showSuccess,
						failure: function(form, action){
						Ext.Msg.alert('Form Error', action.result.msg);
								}
							});
						}
						else {
							Ext.Msg.alert('Form Error', 'Please correct the indicated errors');
						}			
    },
	showSuccess: function(form, action){						
				
				Ext.Msg.show({
					title: 'Callback Request',
					width: 300,
					msg: 'Thank you for your callback request, one of our agents will be contacting you shortly',
					icon: Ext.Msg.INFO,
					buttons: Ext.Msg.OK
				});
				form.reset();
				
	},
	clearVals : function(){
		this.form.form.reset();//the reg form
	},
	setAgency: function(v){
		this.form.form.findField('type').setValue(v);
		if(v == 'Contractor'){
        				this.form.form.findField('aname').setDisabled(true);
	}
	}
});

// register component
Ext.reg('callbackform', Regs.CallForm);
