/**
 * @author George
 */
Ext.ns('Regs');
// create pre-configured window class
Regs.NewForm = Ext.extend(Ext.form.FormPanel, {

	 initComponent:function() {

Ext.apply(this, {
		width: 370,
		title: 'Complete the simple form below to register',
		baseCls: 'x-plain',
		renderTo: 'form',
		baseCls: 'x-plain',
  		bodyStyle: 'padding: 5px;',
		defaultType: 'textfield',
		labelAlign: 'top',
		items: [{
			xtype: 'textfield',
			fieldLabel: 'Name',
			name: 'name',
			allowBlank: false,
			anchor: '100%'
		},{
			xtype: 'textfield',
			fieldLabel: 'Shop Name',
			name: 'shop',
			allowBlank: false,
			anchor: '100%'
		},{
			fieldLabel: 'Address Line 1',
			name: 'add1',
			allowBlank: false,
			anchor: '100%'
		},{
			fieldLabel: 'Address Line 2 (optional)',
			name: 'add2',
			anchor: '100%'
		},{
			fieldLabel: 'Town/City',
			name: 'town',
			allowBlank: false,
			anchor: '100%'
		},{
			fieldLabel: 'Postcode',
			name: 'postcode',
			allowBlank: false,
			anchor: '100%'
		},{
			fieldLabel: 'Telephone',
			name: 'tel',
			allowBlank: false,
			anchor: '100%'
		}],
		buttons: [{
			text: 'Submit Registration',
			tooltip: 'Submit your registration',
			handler: this.onFormSubmit,
            scope: this
		},{
			text: 'Clear Form',
			tooltip: 'Clear the form',
			handler: function(){
				this.form.getForm().reset();
			},
			scope:this
		}]
    });

// call parent
		Regs.NewForm.superclass.initComponent.apply(this, arguments);
},
		 onFormSubmit: function() {
		 	
	if (this.form.isValid()) {
				
				this.form.submit({
					waitMsg: 'Saving details...',
					url : 'http://lighterpromotions.co.uk/includes/add_new_registration.php',
					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: 'New Registration',
					width: 300,
					msg: 'Thank you for your registration, one of our agents will be contacting you shortly',
					icon: Ext.Msg.INFO,
					buttons: Ext.Msg.OK
				});
				form.reset();
				
	},
	checkField : function(f, nV){
		var name = f.getName();
		var url = 'includes/check_email.php';
		Ext.Ajax.request({
					url: url,
					waitMsg: 'Saving Data...',
					params: {
						type: name,
						value: nV
					},
					success: function(response, options){
						var res = Ext.decode(response.responseText);
						if(res.msg == 'No'){
							f.markInvalid('The '+name+' '+nV+' is already taken, choose another');
						}
					}
				});
	},
	clearVals : function(){
		this.form.form.reset();//the reg form
	}
});

// register component
Ext.reg('registrationform', Regs.NewForm);
