/**
 * @author George Miller
 */
Ext.onReady(function(){
	
//initalize the quicktips
Ext.QuickTips.init();
//reference the default image
Ext.BLANK_IMAGE_URL = 'ext-3.0.0/resources/images/default/s.gif';

function submitForm(){
	var form = Ext.getCmp('emailform');
						
						if (form.form.isValid()) {
							Ext.MessageBox.show({
            title: 'Please wait',
           msg: 'Saving Edited Data...',
           progressText: 'Collecting items...',
           width:300,
           progress:true,
           closable:false
       });
							form.form.submit({
								params: {
									action: 'submit'
								},
								success: function(form, action){
								
									// this hideous block creates the bogus progress
									var f = function(v){
										return function(){
											if (v == 12) {
												Ext.MessageBox.hide();
												Ext.MessageBox.show({
							title: 'Email Sent Successfully',
							width: 400,
							height: 200,
							msg: 'Thank you for your enquiry, one of our team will reply to your message as soon as possible',
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.INFO
						});
											}
											else {
												var i = v / 11;
												Ext.MessageBox.updateProgress(i, Math.round(100 * i) + '% completed');
											}
										};
									};
									for (var i = 1; i < 13; i++) {
										setTimeout(f(i), i * 200);
									}
							form.reset();
							
								},
								failure: function(form, action){
									if (action.result.errors.length > 0) {
										Ext.MessageBox.show({
							title: 'Error',
							width: 250,
							height: 150,
							msg: action.result.errors,
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.WARNING
						})
									}
								}
							});
						}
						else {
							Ext.MessageBox.show({
							title: 'Error',
							width: 250,
							height: 150,
							msg: 'Please correct indicated errors',
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.WARNING
						})
						}
}

var form = new Ext.form.FormPanel({
	baseCls: 'x-plain',
	renderTo: 'contactform',
	labelAlign: 'left',
	layout: 'form',
	id: 'emailform',
	labelWidth: 90,
	frame: true,
	keys: {
        key: [10,13],
        fn: function(){ submitForm() }
    },
	defaultType: 'textfield',
	url: 'includes/form_process.php',
	items: [{
		name: 'fname',
		width: 170,
		allowBlank: false,
		fieldLabel: 'First Name'
	},{
		name: 'lname',
		width: 170,
		allowBlank: false,
		fieldLabel: 'Last Name'
	},{
		name: 'tel',
		width: 170,
		allowBlank: true,
		fieldLabel: 'Contact Tel.'
	},{
		name: 'otel',
		width: 170,
		allowBlank: true,
		fieldLabel: 'Other Tel.'
	},{
		name: 'email',
		width: 170,
		allowBlank: false,
		vtype: 'email',
		fieldLabel: 'Email Address'
	},{
		xtype: 'combo',
		hiddenName: 'message',
		fieldLabel: 'Total Debt',
	    width: 170,
		allowBlank: true,
		store: Ext.data.Question1,
	    forceSelection: true,
	    triggerAction: 'all',
	    emptyText:'select...',
	    selectOnFocus:true
	}],
		   buttons: [{
            text: 'Submit',
			handler: function(){
						submitForm();
					}
        },{
            text: 'Cancel',
			handler: function(){
				Ext.getCmp('emailform').getForm().reset();
			}
        }]	
});


});
