/**
 * @author George Miller
 */
Ext.onReady(function(){
//initalize the quicktips
Ext.QuickTips.init();
//reference the default image
Ext.BLANK_IMAGE_URL = 'ext-2.1/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){
									var name = action.result.name;
									// this hideous block creates the bogus progress
									var f = function(v){
										return function(){
											if (v == 12) {
												Ext.MessageBox.hide();
												Ext.MessageBox.show({
							title: 'Successful Entry',
							width: 400,
							height: 200,
							msg: 'Thank you '+name+',<br>You have successfully entered your details, we will get back to you 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({
	width: 500,
	height:330,
	id: 'emailform',
    title: 'Enter Your Details',
	labelAlign: 'top',
	renderTo: 'form',
	labelWidth: 200,
	frame: true,
	keys: {
        key: [10,13],
        fn: function(){ submitForm() }
    },
	defaultType: 'textfield',
	url: '../includes/form_process.php',
	items: [{
		name: 'name',
		width: 200,
		allowBlank: false,
		fieldLabel: 'Name'
	},{
		name: 'email',
		width: 300,
		allowBlank: false,
		id: 'emailbox',
		vtype: 'email',
		fieldLabel: 'Email Address'
	},{
		name: 'subject',
		width: 485,
		allowBlank: false,		
		fieldLabel: 'Subject'
	},{
		name: 'message',
		width: 485,
		allowBlank: false,		
		fieldLabel: 'Your Message',
		xtype: 'textarea',
		height:100,
		preventScrollbars: true
		
	},{
		 xtype: 'hidden',
		name: 'uuid',
		allowBlank: false,
		value: uuid
	}]
       ,buttons: [{
            text: 'Submit',
			handler: function(){
						submitForm();
					}
        },{
            text: 'Cancel',
			handler: function(){
				Ext.getCmp('emailform').getForm().reset();
			}
        }]	
});


});
